Merge pull request #3763 from qvink/empty_message_injection Fix for generation interceptors messing with WI timed effects
Signed| @@ -174,7 +174,7 @@ import { | |||
| 174 | saveBase64AsFile, | 174 | saveBase64AsFile, |
| 175 | uuidv4, | 175 | uuidv4, |
| 176 | } from './scripts/utils.js'; | 176 | } from './scripts/utils.js'; |
| 177 | import { debounce_timeout } from './scripts/constants.js'; | 177 | import { debounce_timeout, IGNORE_SYMBOL } from './scripts/constants.js'; |
| 178 | 178 | ||
| 179 | import { doDailyExtensionUpdatesCheck, extension_settings, initExtensions, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from './scripts/extensions.js'; | 179 | import { doDailyExtensionUpdatesCheck, extension_settings, initExtensions, loadExtensionSettings, runGenerationInterceptors, saveMetadataDebounced } from './scripts/extensions.js'; |
| 180 | import { COMMENT_NAME_DEFAULT, executeSlashCommandsOnChatInput, getSlashCommandsHelp, initDefaultSlashCommands, isExecutingCommandsFromChatInput, pauseScriptExecution, processChatSlashCommands, stopScriptExecution } from './scripts/slash-commands.js'; | 180 | import { 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; |
| 5303 | 5303 | ||
| 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 empty | 5310 | // 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`; |
| 5306 | 5312 | ||
| @@ -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 | */ | ||
| 24 | export const IGNORE_SYMBOL = Symbol.for('ignore'); | ||
| @@ -75,6 +75,7 @@ import { Popup, POPUP_RESULT } from './popup.js'; | |||
| 75 | import { t } from './i18n.js'; | 75 | import { t } from './i18n.js'; |
| 76 | import { ToolManager } from './tool-calling.js'; | 76 | import { ToolManager } from './tool-calling.js'; |
| 77 | import { accountStorage } from './util/AccountStorage.js'; | 77 | import { accountStorage } from './util/AccountStorage.js'; |
| 78 | import { IGNORE_SYMBOL } from './constants.js'; | ||
| 78 | 79 | ||
| 79 | export { | 80 | export { |
| 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']; |
| 525 | 526 | ||
| 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 system | 534 | // 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'; |
| @@ -83,6 +83,7 @@ import { convertCharacterBook, getWorldInfoPrompt, loadWorldInfo, reloadEditor, | |||
| 83 | import { ChatCompletionService, TextCompletionService } from './custom-request.js'; | 83 | import { ChatCompletionService, TextCompletionService } from './custom-request.js'; |
| 84 | import { ConnectionManagerRequestService } from './extensions/shared.js'; | 84 | import { ConnectionManagerRequestService } from './extensions/shared.js'; |
| 85 | import { updateReasoningUI, parseReasoningFromString } from './reasoning.js'; | 85 | import { updateReasoningUI, parseReasoningFromString } from './reasoning.js'; |
| 86 | import { IGNORE_SYMBOL } from './constants.js'; | ||
| 86 | 87 | ||
| 87 | export function getContext() { | 88 | export 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 | } |
| 230 | 234 | ||