Migrate substituteParams calls to new engine (#4901)
Signed| @@ -3159,10 +3159,19 @@ export async function getExtensionPrompt(position = extension_prompt_types.IN_PR | ||
| 3159 | 3159 | return values; |
| 3160 | 3160 | } |
| 3161 | 3161 | |
| 3162 | -export function baseChatReplace(value, name1, name2) { | |
| 3162 | +/** | |
| 3163 | - if (value !== undefined && value.length > 0) { | |
| 3163 | + * Base chat replacement function for character card fields. | |
| 3164 | - const _ = undefined; | |
| 3164 | + * 1. Substitutes macros using substituteParams. | |
| 3165 | - value = substituteParams(value, name1, name2, _, _, false); | |
| 3165 | + * 2. Collapses newlines if enabled in power user settings. | |
| 3166 | + * 3. Removes carriage return characters. | |
| 3167 | + * @param {string} value Input string | |
| 3168 | + * @param {string?} name1Override Override for name1 | |
| 3169 | + * @param {string?} name2Override Override for name2 | |
| 3170 | + * @returns {string} Processed string | |
| 3171 | + */ | |
| 3172 | +export function baseChatReplace(value, name1Override = null, name2Override = null) { | |
| 3173 | + if (typeof value === 'string' && value.length > 0) { | |
| 3174 | + value = substituteParams(value, { name1Override, name2Override, replaceCharacterCard: false }); | |
| 3166 | 3175 | |
| 3167 | 3176 | if (power_user.collapse_newlines) { |
| 3168 | 3177 | value = collapseNewlines(value); |
| @@ -3229,47 +3238,47 @@ export function getCharacterCardFieldsLazy({ chid = undefined } = {}) { | ||
| 3229 | 3238 | |
| 3230 | 3239 | /** @type {Record<string, () => string>} */ |
| 3231 | 3240 | const resolvers = { |
| 3232 | 3241 | persona: () => baseChatReplace(power_user.persona_description?.trim(), name1, name2), |
| 3233 | 3242 | system: () => { |
| 3234 | 3243 | if (!character) return ''; |
| 3235 | 3244 | const systemPrompt = chat_metadata['system_prompt'] || character.data?.system_prompt || ''; |
| 3236 | 3245 | return power_user.prefer_character_prompt ? baseChatReplace(systemPrompt.trim(), name1, name2) : ''; |
| 3237 | 3246 | }, |
| 3238 | 3247 | jailbreak: () => { |
| 3239 | 3248 | if (!character) return ''; |
| 3240 | 3249 | return power_user.prefer_character_jailbreak ? baseChatReplace(character.data?.post_history_instructions?.trim(), name1, name2) : ''; |
| 3241 | 3250 | }, |
| 3242 | 3251 | version: () => character?.data?.character_version ?? '', |
| 3243 | 3252 | charDepthPrompt: () => { |
| 3244 | 3253 | if (!character) return ''; |
| 3245 | 3254 | return baseChatReplace(character.data?.extensions?.depth_prompt?.prompt?.trim(), name1, name2); |
| 3246 | 3255 | }, |
| 3247 | 3256 | creatorNotes: () => { |
| 3248 | 3257 | if (!character) return ''; |
| 3249 | 3258 | return baseChatReplace(character.data?.creator_notes?.trim(), name1, name2); |
| 3250 | 3259 | }, |
| 3251 | 3260 | // These four fields may be overridden by group cards |
| 3252 | 3261 | description: () => { |
| 3253 | 3262 | if (groupCardsLazy) return groupCardsLazy.description; |
| 3254 | 3263 | if (!character) return ''; |
| 3255 | 3264 | return baseChatReplace(character.description?.trim(), name1, name2); |
| 3256 | 3265 | }, |
| 3257 | 3266 | personality: () => { |
| 3258 | 3267 | if (groupCardsLazy) return groupCardsLazy.personality; |
| 3259 | 3268 | if (!character) return ''; |
| 3260 | 3269 | return baseChatReplace(character.personality?.trim(), name1, name2); |
| 3261 | 3270 | }, |
| 3262 | 3271 | scenario: () => { |
| 3263 | 3272 | if (groupCardsLazy) return groupCardsLazy.scenario; |
| 3264 | 3273 | if (!character) return ''; |
| 3265 | 3274 | const scenarioText = chat_metadata['scenario'] || character.scenario || ''; |
| 3266 | 3275 | return baseChatReplace(scenarioText.trim(), name1, name2); |
| 3267 | 3276 | }, |
| 3268 | 3277 | mesExamples: () => { |
| 3269 | 3278 | if (groupCardsLazy) return groupCardsLazy.mesExamples; |
| 3270 | 3279 | if (!character) return ''; |
| 3271 | 3280 | const exampleDialog = chat_metadata['mes_example'] || character.mes_example || ''; |
| 3272 | 3281 | return baseChatReplace(exampleDialog.trim(), name1, name2); |
| 3273 | 3282 | }, |
| 3274 | 3283 | }; |
| 3275 | 3284 | |
| @@ -4404,7 +4413,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 4404 | 4413 | continue; |
| 4405 | 4414 | } |
| 4406 | 4415 | |
| 4407 | 4416 | const formattedExample = baseChatReplace(exampleMessage, name1, name2); |
| 4408 | 4417 | const cleanedExample = parseMesExamples(formattedExample, isInstruct); |
| 4409 | 4418 | |
| 4410 | 4419 | // Insert depending on before or after position |
| @@ -4448,9 +4457,9 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 4448 | 4457 | if (main_api !== 'openai') { |
| 4449 | 4458 | if (power_user.sysprompt.enabled) { |
| 4450 | 4459 | system = power_user.prefer_character_prompt && system |
| 4451 | 4460 | ? substituteParams(system, name1,{ name2,original: (power_user.sysprompt.content ?? '') }) |
| 4452 | 4461 | : baseChatReplace(power_user.sysprompt.content, name1, name2); |
| 4453 | 4462 | system = isInstruct ? substituteParams(system, name1,{ name2,original: power_user.sysprompt.content ?? '' }) : system; |
| 4454 | 4463 | } else { |
| 4455 | 4464 | // Nullify if it's not enabled |
| 4456 | 4465 | system = ''; |
| @@ -4508,8 +4517,8 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | ||
| 4508 | 4517 | |
| 4509 | 4518 | if (main_api !== 'openai' && power_user.sysprompt.enabled) { |
| 4510 | 4519 | jailbreak = power_user.prefer_character_jailbreak && jailbreak |
| 4511 | 4520 | ? substituteParams(jailbreak, name1,{ name2,original: (power_user.sysprompt.post_history ?? '') }) |
| 4512 | 4521 | : baseChatReplace(power_user.sysprompt.post_history, name1, name2); |
| 4513 | 4522 | |
| 4514 | 4523 | // Only inject the jb if there is one |
| 4515 | 4524 | if (jailbreak) { |
| @@ -1280,10 +1280,10 @@ class PromptManager { | ||
| 1280 | 1280 | const preparedPrompt = new Prompt(prompt); |
| 1281 | 1281 | |
| 1282 | 1282 | if (typeof original === 'string') { |
| 1283 | 1283 | if (0 < groupMembers.length) preparedPrompt.content = substituteParams(prompt.content ?? '', null, null,{ original, groupOverride: groupMembers.join(', ') }); |
| 1284 | 1284 | else preparedPrompt.content = substituteParams(prompt.content, null, null,{ original }); |
| 1285 | 1285 | } else { |
| 1286 | 1286 | if (0 < groupMembers.length) preparedPrompt.content = substituteParams(prompt.content ?? '', null, null,{ null,groupOverride: groupMembers.join(', ') }); |
| 1287 | 1287 | else preparedPrompt.content = substituteParams(prompt.content); |
| 1288 | 1288 | } |
| 1289 | 1289 | |
| @@ -457,7 +457,7 @@ export function runRegexScript(regexScript, rawString, { characterOverride } = { | ||
| 457 | 457 | function filterString(rawString, trimStrings, { characterOverride } = {}) { |
| 458 | 458 | let finalString = rawString; |
| 459 | 459 | trimStrings.forEach((trimString) => { |
| 460 | 460 | const subTrimString = substituteParams(trimString, undefined,{ name2Override: characterOverride }); |
| 461 | 461 | finalString = finalString.replaceAll(subTrimString, ''); |
| 462 | 462 | }); |
| 463 | 463 | |
| @@ -210,7 +210,7 @@ async function translateIncomingMessage(messageId) { | ||
| 210 | 210 | return; |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | const textToTranslate = substituteParams(message.mes, context.name1,{ name2Override: message.name }); |
| 214 | 214 | const translation = await translate(textToTranslate, extension_settings.translate.target_language); |
| 215 | 215 | message.extra.display_text = translation; |
| 216 | 216 | |
| @@ -238,7 +238,7 @@ async function translateIncomingMessageReasoning(messageId) { | ||
| 238 | 238 | return false; |
| 239 | 239 | } |
| 240 | 240 | |
| 241 | 241 | const textToTranslate = substituteParams(message.extra.reasoning, context.name1,{ name2Override: message.name }); |
| 242 | 242 | const translation = await translate(textToTranslate, extension_settings.translate.target_language); |
| 243 | 243 | message.extra.reasoning_display_text = translation; |
| 244 | 244 | |
| @@ -39,8 +39,6 @@ import { | ||
| 39 | 39 | setCharacterName, |
| 40 | 40 | setEditedMessageId, |
| 41 | 41 | is_send_press, |
| 42 | - name1, | |
| 43 | - name2, | |
| 44 | 42 | resetChatState, |
| 45 | 43 | setSendButtonState, |
| 46 | 44 | getCharacters, |
| @@ -459,7 +457,7 @@ export function getGroupDepthPrompts(groupId, characterId) { | ||
| 459 | 457 | continue; |
| 460 | 458 | } |
| 461 | 459 | |
| 462 | 460 | const depthPromptText = baseChatReplace(character.data?.extensions?.depth_prompt?.prompt?.trim(), name1null, character.name) || ''; |
| 463 | 461 | const depthPromptDepth = character.data?.extensions?.depth_prompt?.depth ?? depth_prompt_depth_default; |
| 464 | 462 | const depthPromptRole = character.data?.extensions?.depth_prompt?.role ?? depth_prompt_role_default; |
| 465 | 463 | |
| @@ -517,7 +515,7 @@ export function getGroupCharacterCardsLazy(groupId, characterId) { | ||
| 517 | 515 | if (!value) return ''; |
| 518 | 516 | value = value.replace(/<FIELDNAME>/gi, fieldName); |
| 519 | 517 | value = trim ? value.trim() : value; |
| 520 | 518 | return baseChatReplace(value, name1null, characterName); |
| 521 | 519 | } |
| 522 | 520 | |
| 523 | 521 | /** |
| @@ -543,7 +541,7 @@ export function getGroupCharacterCardsLazy(groupId, characterId) { | ||
| 543 | 541 | /** |
| 544 | 542 | * Collects and joins field values from all group members |
| 545 | 543 | * @param {string} fieldName Display name of the field |
| 546 | 544 | * @param {function(import('../script.js').Character): string} getter Function to get field value from character |
| 547 | 545 | * @param {function(string): string} [preprocess] Optional preprocess function |
| 548 | 546 | * @returns {string} Combined field values |
| 549 | 547 | */ |
| @@ -567,8 +565,8 @@ export function getGroupCharacterCardsLazy(groupId, characterId) { | ||
| 567 | 565 | return createLazyFields({ |
| 568 | 566 | description: () => collectField('Description', c => c.description), |
| 569 | 567 | personality: () => collectField('Personality', c => c.personality), |
| 570 | 568 | scenario: () => baseChatReplace(scenarioOverride?.trim(), name1, name2) || collectField('Scenario', c => c.scenario), |
| 571 | 569 | mesExamples: () => baseChatReplace(mesExamplesOverride?.trim(), name1, name2) || |
| 572 | 570 | collectField('Example Messages', c => c.mes_example, x => !x.startsWith('<START>') ? `<START>\n${x}` : x), |
| 573 | 571 | }); |
| 574 | 572 | } |
| @@ -602,7 +600,7 @@ async function getFirstCharacterMessage(character) { | ||
| 602 | 600 | mes['original_avatar'] = character.avatar; |
| 603 | 601 | mes['extra'] = { 'gen_id': Date.now() * Math.random() * 1000000 }; |
| 604 | 602 | mes['mes'] = messageText |
| 605 | 603 | ? substituteParams(messageText.trim(), name1,{ name2Override: character.name }) |
| 606 | 604 | : ''; |
| 607 | 605 | mes['force_avatar'] = |
| 608 | 606 | character.avatar != 'none' |
| @@ -436,10 +436,10 @@ export function formatInstructModeChat(name, mes, isUser, isNarrator, forceAvata | ||
| 436 | 436 | let suffix = getSuffix() || ''; |
| 437 | 437 | |
| 438 | 438 | if (instruct.macro) { |
| 439 | 439 | prefix = substituteParams(prefix, { name1Override: name1, name2Override: name2 }); |
| 440 | 440 | prefix = prefix.replace(/{{name}}/gi, name || 'System'); |
| 441 | 441 | |
| 442 | 442 | suffix = substituteParams(suffix, { name1Override: name1, name2Override: name2 }); |
| 443 | 443 | suffix = suffix.replace(/{{name}}/gi, name || 'System'); |
| 444 | 444 | } |
| 445 | 445 | |
| @@ -524,10 +524,10 @@ export function formatInstructModeExamples(mesExamplesArray, name1, name2) { | ||
| 524 | 524 | let outputSuffix = power_user.instruct.output_suffix || ''; |
| 525 | 525 | |
| 526 | 526 | if (power_user.instruct.macro) { |
| 527 | 527 | inputPrefix = substituteParams(inputPrefix, { name1Override: name1, name2Override: name2 }); |
| 528 | 528 | outputPrefix = substituteParams(outputPrefix, { name1Override: name1, name2Override: name2 }); |
| 529 | 529 | inputSuffix = substituteParams(inputSuffix, { name1Override: name1, name2Override: name2 }); |
| 530 | 530 | outputSuffix = substituteParams(outputSuffix, { name1Override: name1, name2Override: name2 }); |
| 531 | 531 | |
| 532 | 532 | inputPrefix = inputPrefix.replace(/{{name}}/gi, name1); |
| 533 | 533 | outputPrefix = outputPrefix.replace(/{{name}}/gi, name2); |
| @@ -631,7 +631,7 @@ export function formatInstructModePrompt(name, isImpersonate, promptBias, name1, | ||
| 631 | 631 | } |
| 632 | 632 | |
| 633 | 633 | if (instruct.macro) { |
| 634 | 634 | sequence = substituteParams(sequence, { name1Override: name1, name2Override: name2 }); |
| 635 | 635 | sequence = sequence.replace(/{{name}}/gi, name || 'System'); |
| 636 | 636 | } |
| 637 | 637 | |