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 -1Showing whitespace changes
public/script.js+7 -1
@@ -174,7 +174,7 @@ import {
174 saveBase64AsFile,174 saveBase64AsFile,
175 uuidv4,175 uuidv4,
176} from './scripts/utils.js';176} from './scripts/utils.js';
177import { debounce_timeout } from './scripts/constants.js';177import { debounce_timeout, IGNORE_SYMBOL } from './scripts/constants.js';
178178
179import { doDailyExtensionUpdatesCheck, extension_settings, initExtensions, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from './scripts/extensions.js';179import { doDailyExtensionUpdatesCheck, extension_settings, initExtensions, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from './scripts/extensions.js';
180import { COMMENT_NAME_DEFAULT, executeSlashCommandsOnChatInput, getSlashCommandsHelp, initDefaultSlashCommands, isExecutingCommandsFromChatInput, pauseScriptExecution, processChatSlashCommands, stopScriptExecution } from './scripts/slash-commands.js';180import { COMMENT_NAME_DEFAULT, executeSlashCommandsOnChatInput, getSlashCommandsHelp, initDefaultSlashCommands, isExecutingCommandsFromChatInput, pauseScriptExecution, processChatSlashCommands, stopScriptExecution } from './scripts/slash-commands.js';
@@ -5301,6 +5301,12 @@ function formatMessageHistoryItem(chatItem, isInstruct, forceOutputSequence) {
5301 const itemName = chatItem.is_user ? chatItem['name'] : characterName;5301 const itemName = chatItem.is_user ? chatItem['name'] : characterName;
5302 const shouldPrependName = !isNarratorType;5302 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
5304 // Don't include a name if it's empty5310 // Don't include a name if it's empty
5305 let textResult = chatItem?.name && shouldPrependName ? `${itemName}: ${chatItem.mes}\n` : `${chatItem.mes}\n`;5311 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 = {
14 /** [5 sec] For delayed tasks, like auto-saving or completing batch operations that need a significant pause. */14 /** [5 sec] For delayed tasks, like auto-saving or completing batch operations that need a significant pause. */
15 extended: 5000,15 extended: 5000,
16};16};
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 */
24export const IGNORE_SYMBOL = Symbol.for('ignore');
public/scripts/openai.js+8 -0
@@ -75,6 +75,7 @@ import { Popup, POPUP_RESULT } from './popup.js';
75import { t } from './i18n.js';75import { t } from './i18n.js';
76import { ToolManager } from './tool-calling.js';76import { ToolManager } from './tool-calling.js';
77import { accountStorage } from './util/AccountStorage.js';77import { accountStorage } from './util/AccountStorage.js';
78import { IGNORE_SYMBOL } from './constants.js';
7879
79export {80export {
80 openai_messages_count,81 openai_messages_count,
@@ -523,6 +524,13 @@ function setOpenAIMessages(chat) {
523 let role = chat[j]['is_user'] ? 'user' : 'assistant';524 let role = chat[j]['is_user'] ? 'user' : 'assistant';
524 let content = chat[j]['mes'];525 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
526 // 100% legal way to send a message as system534 // 100% legal way to send a message as system
527 if (chat[j].extra?.type === system_message_types.NARRATOR) {535 if (chat[j].extra?.type === system_message_types.NARRATOR) {
528 role = 'system';536 role = 'system';
public/scripts/st-context.js+4 -0
@@ -83,6 +83,7 @@ import { convertCharacterBook, getWorldInfoPrompt, loadWorldInfo, reloadEditor,
83import { ChatCompletionService, TextCompletionService } from './custom-request.js';83import { ChatCompletionService, TextCompletionService } from './custom-request.js';
84import { ConnectionManagerRequestService } from './extensions/shared.js';84import { ConnectionManagerRequestService } from './extensions/shared.js';
85import { updateReasoningUI, parseReasoningFromString } from './reasoning.js';85import { updateReasoningUI, parseReasoningFromString } from './reasoning.js';
86import { IGNORE_SYMBOL } from './constants.js';
8687
87export function getContext() {88export function getContext() {
88 return {89 return {
@@ -225,6 +226,9 @@ export function getContext() {
225 parseReasoningFromString,226 parseReasoningFromString,
226 unshallowCharacter,227 unshallowCharacter,
227 unshallowGroupMembers,228 unshallowGroupMembers,
229 symbols: {
230 ignore: IGNORE_SYMBOL,
231 },
228 };232 };
229}233}
230234