Merge pull request #3763 from qvink/empty_message_injection Fix for generation interceptors messing with WI timed effects

1639289b18927b47cf677363d867eb2c9903ccbe

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
4 files changed, +27 -1Ignore whitespace
public/script.js+7 -1
@@ -174,7 +174,7 @@ import {
174174 saveBase64AsFile,
175175 uuidv4,
176176} from './scripts/utils.js';
177177import { debounce_timeout, IGNORE_SYMBOL } from './scripts/constants.js';
178178
179179import { doDailyExtensionUpdatesCheck, extension_settings, initExtensions, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from './scripts/extensions.js';
180180import { COMMENT_NAME_DEFAULT, executeSlashCommandsOnChatInput, getSlashCommandsHelp, initDefaultSlashCommands, isExecutingCommandsFromChatInput, pauseScriptExecution, processChatSlashCommands, stopScriptExecution } from './scripts/slash-commands.js';
@@ -5301,6 +5301,12 @@ function formatMessageHistoryItem(chatItem, isInstruct, forceOutputSequence) {
53015301 const itemName = chatItem.is_user ? chatItem['name'] : characterName;
53025302 const shouldPrependName = !isNarratorType;
53035303
5304+ // If this symbol flag is set, completely ignore the message.
5305+ // This can be used to hide messages without affecting the number of messages in the chat.
5306+ if (chatItem.extra?.[IGNORE_SYMBOL]) {
5307+ return '';
5308+ }
5309+
53045310 // Don't include a name if it's empty
53055311 let textResult = chatItem?.name && shouldPrependName ? `${itemName}: ${chatItem.mes}\n` : `${chatItem.mes}\n`;
53065312
public/scripts/constants.js+8 -0
@@ -14,3 +14,11 @@ export const debounce_timeout = {
1414 /** [5 sec] For delayed tasks, like auto-saving or completing batch operations that need a significant pause. */
1515 extended: 5000,
1616};
17+
18+/**
19+ * Used as an ephemeral key in message extra metadata.
20+ * When set, the message will be excluded from generation
21+ * prompts without affecting the number of chat messages,
22+ * which is needed to preserve world info timed effects.
23+ */
24+export const IGNORE_SYMBOL = Symbol.for('ignore');
public/scripts/openai.js+8 -0
@@ -75,6 +75,7 @@ import { Popup, POPUP_RESULT } from './popup.js';
7575import { t } from './i18n.js';
7676import { ToolManager } from './tool-calling.js';
7777import { accountStorage } from './util/AccountStorage.js';
78+import { IGNORE_SYMBOL } from './constants.js';
7879
7980export {
8081 openai_messages_count,
@@ -523,6 +524,13 @@ function setOpenAIMessages(chat) {
523524 let role = chat[j]['is_user'] ? 'user' : 'assistant';
524525 let content = chat[j]['mes'];
525526
527+ // If this symbol flag is set, completely ignore the message.
528+ // This can be used to hide messages without affecting the number of messages in the chat.
529+ if (chat[j].extra?.[IGNORE_SYMBOL]) {
530+ j++;
531+ continue;
532+ }
533+
526534 // 100% legal way to send a message as system
527535 if (chat[j].extra?.type === system_message_types.NARRATOR) {
528536 role = 'system';
public/scripts/st-context.js+4 -0
@@ -83,6 +83,7 @@ import { convertCharacterBook, getWorldInfoPrompt, loadWorldInfo, reloadEditor,
8383import { ChatCompletionService, TextCompletionService } from './custom-request.js';
8484import { ConnectionManagerRequestService } from './extensions/shared.js';
8585import { updateReasoningUI, parseReasoningFromString } from './reasoning.js';
86+import { IGNORE_SYMBOL } from './constants.js';
8687
8788export function getContext() {
8889 return {
@@ -225,6 +226,9 @@ export function getContext() {
225226 parseReasoningFromString,
226227 unshallowCharacter,
227228 unshallowGroupMembers,
229+ symbols: {
230+ ignore: IGNORE_SYMBOL,
231+ },
228232 };
229233}
230234