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() {
42184218 * @property {string} [quietName] Name to use for the quiet prompt (defaults to "System:")
42194219 * @property {number} [depth] Recursion depth for the generation. Used to prevent infinite loops in tool calls.
42204220 * @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.
42214222 */
42224223
42234224/**
@@ -4228,7 +4229,7 @@ function removeLastMessage() {
42284229 * @param {boolean} dryRun Whether to actually generate a message or just assemble the prompt
42294230 * @returns {Promise<any>} Returns a promise that resolves when the text is done generating.
42304231 */
42314232export 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) {
42324233 console.log('Generate entered');
42334234 setGenerationProgress(0);
42344235 generation_started = new Date();
@@ -5240,6 +5241,7 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro
52405241 jailbreakPromptOverride: jailbreak,
52415242 messages: oaiMessages,
52425243 messageExamples: oaiMessageExamples,
5244+ squashSystemMessages,
52435245 }, dryRun);
52445246 generate_data = { prompt: prompt };
52455247
public/scripts/extensions/keepalive/index.js+3 -1
@@ -209,7 +209,9 @@ async function prepareKeepaliveRequest() {
209209 eventSource.on(event_types.GENERATE_AFTER_DATA, captureData);
210210 setExtensionPrompt(KEEPALIVE_INJECT_ID, extension_settings[MODULE].message, extension_prompt_types.IN_CHAT, 0, false, extension_prompt_roles.USER);
211211 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);
213215 } finally {
214216 setExtensionPrompt(KEEPALIVE_INJECT_ID, '', extension_prompt_types.IN_CHAT, 0, false, extension_prompt_roles.USER);
215217 eventSource.removeListener(event_types.GENERATE_AFTER_DATA, captureData);
public/scripts/extensions/keepalive/manifest.json+1 -1
@@ -6,7 +6,7 @@
66 "js": "index.js",
77 "css": "",
88 "author": "SillyTavern",
99 "version": "1.1.12",
1010 "homePage": "https://github.com/SillyTavern/SillyTavern",
1111 "hooks": {
1212 "activate": "init"
public/scripts/openai.js+3 -1
@@ -1528,6 +1528,7 @@ async function preparePromptsForChatCompletion({ scenario, charPersonality, name
15281528 * @param {object} content.extensionPrompts - An array of additional prompts.
15291529 * @param {object[]} content.messages - An array of messages to be used as chat history.
15301530 * @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.
15311532 * @param dryRun - Whether this is a live call or not.
15321533 * @returns {Promise<(any[]|boolean)[]>} An array where the first element is the prepared chat and the second element is a boolean flag.
15331534 */
@@ -1548,6 +1549,7 @@ export async function prepareOpenAIMessages({
15481549 jailbreakPromptOverride,
15491550 messages,
15501551 messageExamples,
1552+ squashSystemMessages = false,
15511553}, dryRun) {
15521554 // Without a character selected, there is no way to accurately calculate tokens
15531555 if (!promptManager.activeCharacter && dryRun) return [null, false];
@@ -1597,7 +1599,7 @@ export async function prepareOpenAIMessages({
15971599 // Pass chat completion to prompt manager for inspection
15981600 promptManager.setChatCompletion(chatCompletion);
15991601
16001602 if (oai_settings.squash_system_messages && (!dryRun ==|| falsesquashSystemMessages)) {
16011603 await chatCompletion.squashSystemMessages();
16021604 }
16031605