From 9d88c1578b218777dcea1d7be83606fb4eee8d4e Mon Sep 17 00:00:00 2001 From: qvink Date: Tue, 25 Mar 2025 22:31:44 -0600 Subject: [PATCH 1/9] don't format messages with undefined content --- public/script.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/public/script.js b/public/script.js index 001cc72fc..7e288ca22 100644 --- a/public/script.js +++ b/public/script.js @@ -5301,6 +5301,11 @@ function formatMessageHistoryItem(chatItem, isInstruct, forceOutputSequence) { const itemName = chatItem.is_user ? chatItem['name'] : characterName; const shouldPrependName = !isNarratorType; + // Don't format messages with undefined content + if (chatItem.mes === undefined) { + return "" + } + // Don't include a name if it's empty let textResult = chatItem?.name && shouldPrependName ? `${itemName}: ${chatItem.mes}\n` : `${chatItem.mes}\n`; From abb908e62c690d1ad42636d0fcf5c14604d32938 Mon Sep 17 00:00:00 2001 From: qvink Date: Tue, 25 Mar 2025 22:36:05 -0600 Subject: [PATCH 2/9] semi --- public/script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/script.js b/public/script.js index 7e288ca22..ad6d810e0 100644 --- a/public/script.js +++ b/public/script.js @@ -5303,7 +5303,7 @@ function formatMessageHistoryItem(chatItem, isInstruct, forceOutputSequence) { // Don't format messages with undefined content if (chatItem.mes === undefined) { - return "" + return ''; } // Don't include a name if it's empty From 72f91a4994dd4c50e26d98c349ece1b9b6e743f9 Mon Sep 17 00:00:00 2001 From: qvink Date: Wed, 26 Mar 2025 09:28:42 -0600 Subject: [PATCH 3/9] use custom flag instead --- public/script.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/public/script.js b/public/script.js index ad6d810e0..73aefe7fb 100644 --- a/public/script.js +++ b/public/script.js @@ -5301,8 +5301,9 @@ function formatMessageHistoryItem(chatItem, isInstruct, forceOutputSequence) { const itemName = chatItem.is_user ? chatItem['name'] : characterName; const shouldPrependName = !isNarratorType; - // Don't format messages with undefined content - if (chatItem.mes === undefined) { + // If this flag is set, completely ignore the message. + // This can be used to hide messages without affecting the number of messages in the chat. + if (chatItem.ignore_formatting) { return ''; } From 251d242a0d26a555b67eb3ba112cbbb012c58f6d Mon Sep 17 00:00:00 2001 From: qvink Date: Wed, 26 Mar 2025 13:10:50 -0600 Subject: [PATCH 4/9] custom flag in message.extra, also apply to chat completion --- public/script.js | 2 +- public/scripts/openai.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/public/script.js b/public/script.js index 73aefe7fb..941626660 100644 --- a/public/script.js +++ b/public/script.js @@ -5303,7 +5303,7 @@ function formatMessageHistoryItem(chatItem, isInstruct, forceOutputSequence) { // If this flag is set, completely ignore the message. // This can be used to hide messages without affecting the number of messages in the chat. - if (chatItem.ignore_formatting) { + if (chatItem.extra?.ignore) { return ''; } diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 695db39a0..d2d4d978e 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -527,6 +527,12 @@ function setOpenAIMessages(chat) { let role = chat[j]['is_user'] ? 'user' : 'assistant'; let content = chat[j]['mes']; + // If this flag is set, completely ignore the message + if (chat[j].extra?.ignore) { + j++; + continue; + } + // 100% legal way to send a message as system if (chat[j].extra?.type === system_message_types.NARRATOR) { role = 'system'; From f1a053c3b83609ae10dabd326110261d01312d10 Mon Sep 17 00:00:00 2001 From: qvink Date: Thu, 27 Mar 2025 09:31:52 -0600 Subject: [PATCH 5/9] using symbols instead --- public/script.js | 5 +++-- public/scripts/constants.js | 6 ++++++ public/scripts/openai.js | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/public/script.js b/public/script.js index 941626660..877c932f5 100644 --- a/public/script.js +++ b/public/script.js @@ -174,7 +174,7 @@ import { saveBase64AsFile, uuidv4, } from './scripts/utils.js'; -import { debounce_timeout } from './scripts/constants.js'; +import { debounce_timeout, IGNORE_SYMBOL } from './scripts/constants.js'; import { doDailyExtensionUpdatesCheck, extension_settings, initExtensions, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from './scripts/extensions.js'; import { COMMENT_NAME_DEFAULT, executeSlashCommandsOnChatInput, getSlashCommandsHelp, initDefaultSlashCommands, isExecutingCommandsFromChatInput, pauseScriptExecution, processChatSlashCommands, stopScriptExecution } from './scripts/slash-commands.js'; @@ -5295,6 +5295,7 @@ export function getBiasStrings(textareaText, type) { * @param {boolean} isInstruct Whether instruct mode is enabled. * @param {boolean|number} forceOutputSequence Whether to force the first/last output sequence for instruct mode. */ + function formatMessageHistoryItem(chatItem, isInstruct, forceOutputSequence) { const isNarratorType = chatItem?.extra?.type === system_message_types.NARRATOR; const characterName = chatItem?.name ? chatItem.name : name2; @@ -5303,7 +5304,7 @@ function formatMessageHistoryItem(chatItem, isInstruct, forceOutputSequence) { // If this flag is set, completely ignore the message. // This can be used to hide messages without affecting the number of messages in the chat. - if (chatItem.extra?.ignore) { + if (chatItem.extra?.[IGNORE_SYMBOL]) { return ''; } diff --git a/public/scripts/constants.js b/public/scripts/constants.js index f95a8e146..5daa7b1bc 100644 --- a/public/scripts/constants.js +++ b/public/scripts/constants.js @@ -14,3 +14,9 @@ export const debounce_timeout = { /** [5 sec] For delayed tasks, like auto-saving or completing batch operations that need a significant pause. */ extended: 5000, }; + +/* +Used as an ephemeral key in message extra metadata. +When set, the message will be excluded from context. + */ +export const IGNORE_SYMBOL = Symbol.for('ignore'); diff --git a/public/scripts/openai.js b/public/scripts/openai.js index d2d4d978e..0f9ccc5ca 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -75,6 +75,7 @@ import { Popup, POPUP_RESULT } from './popup.js'; import { t } from './i18n.js'; import { ToolManager } from './tool-calling.js'; import { accountStorage } from './util/AccountStorage.js'; +import {IGNORE_SYMBOL} from "./constants.js"; export { openai_messages_count, @@ -528,7 +529,7 @@ function setOpenAIMessages(chat) { let content = chat[j]['mes']; // If this flag is set, completely ignore the message - if (chat[j].extra?.ignore) { + if (chat[j].extra?.[IGNORE_SYMBOL]) { j++; continue; } From 1dcd837eb12c8dda935a671df82a8c75c85dfca4 Mon Sep 17 00:00:00 2001 From: qvink Date: Thu, 27 Mar 2025 09:38:14 -0600 Subject: [PATCH 6/9] lint --- public/scripts/openai.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 0f9ccc5ca..a8f7587a9 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -75,7 +75,7 @@ import { Popup, POPUP_RESULT } from './popup.js'; import { t } from './i18n.js'; import { ToolManager } from './tool-calling.js'; import { accountStorage } from './util/AccountStorage.js'; -import {IGNORE_SYMBOL} from "./constants.js"; +import { IGNORE_SYMBOL } from './constants.js'; export { openai_messages_count, From dac5f6910c9a4e3fda00dcac856b229deff3cba4 Mon Sep 17 00:00:00 2001 From: qvink Date: Thu, 27 Mar 2025 09:40:42 -0600 Subject: [PATCH 7/9] adding comments --- public/script.js | 3 +-- public/scripts/constants.js | 3 ++- public/scripts/openai.js | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/public/script.js b/public/script.js index 877c932f5..e04a308f8 100644 --- a/public/script.js +++ b/public/script.js @@ -5295,14 +5295,13 @@ export function getBiasStrings(textareaText, type) { * @param {boolean} isInstruct Whether instruct mode is enabled. * @param {boolean|number} forceOutputSequence Whether to force the first/last output sequence for instruct mode. */ - function formatMessageHistoryItem(chatItem, isInstruct, forceOutputSequence) { const isNarratorType = chatItem?.extra?.type === system_message_types.NARRATOR; const characterName = chatItem?.name ? chatItem.name : name2; const itemName = chatItem.is_user ? chatItem['name'] : characterName; const shouldPrependName = !isNarratorType; - // If this flag is set, completely ignore the message. + // If this symbol flag is set, completely ignore the message. // This can be used to hide messages without affecting the number of messages in the chat. if (chatItem.extra?.[IGNORE_SYMBOL]) { return ''; diff --git a/public/scripts/constants.js b/public/scripts/constants.js index 5daa7b1bc..e1f632905 100644 --- a/public/scripts/constants.js +++ b/public/scripts/constants.js @@ -17,6 +17,7 @@ export const debounce_timeout = { /* Used as an ephemeral key in message extra metadata. -When set, the message will be excluded from context. +When set, the message will be excluded from generation prompts without affecting the number of chat messages, + which is needed to preserve world info timed effects. */ export const IGNORE_SYMBOL = Symbol.for('ignore'); diff --git a/public/scripts/openai.js b/public/scripts/openai.js index a8f7587a9..30efc6e03 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -528,7 +528,8 @@ function setOpenAIMessages(chat) { let role = chat[j]['is_user'] ? 'user' : 'assistant'; let content = chat[j]['mes']; - // If this flag is set, completely ignore the message + // If this flag is set, completely ignore the message. + // This can be used to hide messages without affecting the number of messages in the chat. if (chat[j].extra?.[IGNORE_SYMBOL]) { j++; continue; From de091daa4021e5769f14b35bbb2fbc1ad56cc4aa Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 27 Mar 2025 20:46:27 +0200 Subject: [PATCH 8/9] Clean-up comments --- public/scripts/constants.js | 9 +++++---- public/scripts/openai.js | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/public/scripts/constants.js b/public/scripts/constants.js index e1f632905..5cd1a2ff5 100644 --- a/public/scripts/constants.js +++ b/public/scripts/constants.js @@ -15,9 +15,10 @@ export const debounce_timeout = { extended: 5000, }; -/* -Used as an ephemeral key in message extra metadata. -When set, the message will be excluded from generation prompts without affecting the number of chat messages, - which is needed to preserve world info timed effects. +/** + * Used as an ephemeral key in message extra metadata. + * When set, the message will be excluded from generation + * prompts without affecting the number of chat messages, + * which is needed to preserve world info timed effects. */ export const IGNORE_SYMBOL = Symbol.for('ignore'); diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 30efc6e03..354067011 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -528,7 +528,7 @@ function setOpenAIMessages(chat) { let role = chat[j]['is_user'] ? 'user' : 'assistant'; let content = chat[j]['mes']; - // If this flag is set, completely ignore the message. + // If this symbol flag is set, completely ignore the message. // This can be used to hide messages without affecting the number of messages in the chat. if (chat[j].extra?.[IGNORE_SYMBOL]) { j++; From e5d5f953ec3cdfb809a1ae0da73d7ab75ea482e5 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Thu, 27 Mar 2025 20:48:13 +0200 Subject: [PATCH 9/9] Add symbols to getContext --- public/scripts/st-context.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/public/scripts/st-context.js b/public/scripts/st-context.js index ae32ff3f8..0ba1104bd 100644 --- a/public/scripts/st-context.js +++ b/public/scripts/st-context.js @@ -83,6 +83,7 @@ import { convertCharacterBook, getWorldInfoPrompt, loadWorldInfo, reloadEditor, import { ChatCompletionService, TextCompletionService } from './custom-request.js'; import { ConnectionManagerRequestService } from './extensions/shared.js'; import { updateReasoningUI, parseReasoningFromString } from './reasoning.js'; +import { IGNORE_SYMBOL } from './constants.js'; export function getContext() { return { @@ -225,6 +226,9 @@ export function getContext() { parseReasoningFromString, unshallowCharacter, unshallowGroupMembers, + symbols: { + ignore: IGNORE_SYMBOL, + }, }; }