Preserve prompt cache shape in keepalives

8346f51967f3e6fdf658f3f24949220d553991a4

permissionBRICK <40219477+permissionBRICK@users.noreply.github.com>

4 files changed, +10 -4Ignore whitespace
public/script.js+3 -1
@@ -4218,6 +4218,7 @@ function removeLastMessage() {
4218 * @property {string} [quietName] Name to use for the quiet prompt (defaults to "System:")4218 * @property {string} [quietName] Name to use for the quiet prompt (defaults to "System:")
4219 * @property {number} [depth] Recursion depth for the generation. Used to prevent infinite loops in tool calls.4219 * @property {number} [depth] Recursion depth for the generation. Used to prevent infinite loops in tool calls.
4220 * @property {JsonSchema} [jsonSchema] JSON schema to use for the structured generation. Usually requires a special instruction.4220 * @property {JsonSchema} [jsonSchema] JSON schema to use for the structured generation. Usually requires a special instruction.
4221 * @property {boolean} [squashSystemMessages] Apply the configured system-message squashing during a dry run.
4221 */4222 */
42224223
4223/**4224/**
@@ -4228,7 +4229,7 @@ function removeLastMessage() {
4228 * @param {boolean} dryRun Whether to actually generate a message or just assemble the prompt4229 * @param {boolean} dryRun Whether to actually generate a message or just assemble the prompt
4229 * @returns {Promise<any>} Returns a promise that resolves when the text is done generating.4230 * @returns {Promise<any>} Returns a promise that resolves when the text is done generating.
4230 */4231 */
4231export async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName, jsonSchema = null, depth = 0 } = {}, dryRun = false) {4232export async function Generate(type, { automatic_trigger, force_name2, quiet_prompt, quietToLoud, skipWIAN, force_chid, signal, quietImage, quietName, jsonSchema = null, depth = 0, squashSystemMessages = false } = {}, dryRun = false) {
4232 console.log('Generate entered');4233 console.log('Generate entered');
4233 setGenerationProgress(0);4234 setGenerationProgress(0);
4234 generation_started = new Date();4235 generation_started = new Date();
@@ -5240,6 +5241,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
5240 jailbreakPromptOverride: jailbreak,5241 jailbreakPromptOverride: jailbreak,
5241 messages: oaiMessages,5242 messages: oaiMessages,
5242 messageExamples: oaiMessageExamples,5243 messageExamples: oaiMessageExamples,
5244 squashSystemMessages,
5243 }, dryRun);5245 }, dryRun);
5244 generate_data = { prompt: prompt };5246 generate_data = { prompt: prompt };
52455247
public/scripts/extensions/keepalive/index.js+3 -1
@@ -209,7 +209,9 @@ async function prepareKeepaliveRequest() {
209 eventSource.on(event_types.GENERATE_AFTER_DATA, captureData);209 eventSource.on(event_types.GENERATE_AFTER_DATA, captureData);
210 setExtensionPrompt(KEEPALIVE_INJECT_ID, extension_settings[MODULE].message, extension_prompt_types.IN_CHAT, 0, false, extension_prompt_roles.USER);210 setExtensionPrompt(KEEPALIVE_INJECT_ID, extension_settings[MODULE].message, extension_prompt_types.IN_CHAT, 0, false, extension_prompt_roles.USER);
211 try {211 try {
212 await Generate('quiet', { quiet_prompt: '', force_name2: true }, true);212 // A normal live request applies the user's system-message squashing after prompt assembly.
213 // Opt into the same final transform here so provider cache keys see identical boundaries.
214 await Generate('quiet', { quiet_prompt: '', force_name2: true, squashSystemMessages: true }, true);
213 } finally {215 } finally {
214 setExtensionPrompt(KEEPALIVE_INJECT_ID, '', extension_prompt_types.IN_CHAT, 0, false, extension_prompt_roles.USER);216 setExtensionPrompt(KEEPALIVE_INJECT_ID, '', extension_prompt_types.IN_CHAT, 0, false, extension_prompt_roles.USER);
215 eventSource.removeListener(event_types.GENERATE_AFTER_DATA, captureData);217 eventSource.removeListener(event_types.GENERATE_AFTER_DATA, captureData);
public/scripts/extensions/keepalive/manifest.json+1 -1
@@ -6,7 +6,7 @@
6 "js": "index.js",6 "js": "index.js",
7 "css": "",7 "css": "",
8 "author": "SillyTavern",8 "author": "SillyTavern",
9 "version": "1.1.1",9 "version": "1.1.2",
10 "homePage": "https://github.com/SillyTavern/SillyTavern",10 "homePage": "https://github.com/SillyTavern/SillyTavern",
11 "hooks": {11 "hooks": {
12 "activate": "init"12 "activate": "init"
public/scripts/openai.js+3 -1
@@ -1528,6 +1528,7 @@ async function preparePromptsForChatCompletion({ scenario, charPersonality, name
1528 * @param {object} content.extensionPrompts - An array of additional prompts.1528 * @param {object} content.extensionPrompts - An array of additional prompts.
1529 * @param {object[]} content.messages - An array of messages to be used as chat history.1529 * @param {object[]} content.messages - An array of messages to be used as chat history.
1530 * @param {string[]} content.messageExamples - An array of messages to be used as dialogue examples.1530 * @param {string[]} content.messageExamples - An array of messages to be used as dialogue examples.
1531 * @param {boolean} [content.squashSystemMessages] Apply configured system-message squashing during a dry run.
1531 * @param dryRun - Whether this is a live call or not.1532 * @param dryRun - Whether this is a live call or not.
1532 * @returns {Promise<(any[]|boolean)[]>} An array where the first element is the prepared chat and the second element is a boolean flag.1533 * @returns {Promise<(any[]|boolean)[]>} An array where the first element is the prepared chat and the second element is a boolean flag.
1533 */1534 */
@@ -1548,6 +1549,7 @@ export async function prepareOpenAIMessages({
1548 jailbreakPromptOverride,1549 jailbreakPromptOverride,
1549 messages,1550 messages,
1550 messageExamples,1551 messageExamples,
1552 squashSystemMessages = false,
1551}, dryRun) {1553}, dryRun) {
1552 // Without a character selected, there is no way to accurately calculate tokens1554 // Without a character selected, there is no way to accurately calculate tokens
1553 if (!promptManager.activeCharacter && dryRun) return [null, false];1555 if (!promptManager.activeCharacter && dryRun) return [null, false];
@@ -1597,7 +1599,7 @@ export async function prepareOpenAIMessages({
1597 // Pass chat completion to prompt manager for inspection1599 // Pass chat completion to prompt manager for inspection
1598 promptManager.setChatCompletion(chatCompletion);1600 promptManager.setChatCompletion(chatCompletion);
15991601
1600 if (oai_settings.squash_system_messages && dryRun == false) {1602 if (oai_settings.squash_system_messages && (!dryRun || squashSystemMessages)) {
1601 await chatCompletion.squashSystemMessages();1603 await chatCompletion.squashSystemMessages();
1602 }1604 }
16031605