Cache Keepalive: send ping as a user message (depth-0 injection) and update default message

6803ba16e2fd0e1a5c64a2d836107d430410fcda

permissionBRICK <permissionBRICK@gmail.com>

2 files changed, +25 -3Showing whitespace changes
public/scripts/extensions/keepalive/index.js+24 -2
@@ -4,9 +4,12 @@ import {
44 deactivateSendButtons,
55 eventSource,
66 event_types,
7+ extension_prompt_roles,
8+ extension_prompt_types,
79 generateQuietPrompt,
810 isGenerating,
911 saveSettingsDebounced,
12+ setExtensionPrompt,
1013 setSendButtonState,
1114 streamingProcessor,
1215} from '../../../script.js';
@@ -20,6 +23,12 @@ const MODULE = 'keepalive';
2023// cache), but we never pay for a full completion.
2124const KEEPALIVE_RESPONSE_LENGTH = 1;
2225
26+// Extension-prompt key used to inject the keepalive message as a USER message at depth 0.
27+const KEEPALIVE_INJECT_ID = 'keepalive_ping';
28+
29+// Original default message; used to migrate untouched installs to the current default wording.
30+const LEGACY_DEFAULT_MESSAGE = 'Keep the cache warm.';
31+
2332// Idle-tracking key used when no connection profile is active (manual API panel).
2433const NONE_KEY = '__none__';
2534
@@ -31,7 +40,7 @@ const EXPIRED_GRACE_MS = 3000;
3140const defaultSettings = {
3241 enabled: false,
3342 timeoutSeconds: 295,
34- message: 'Keep the cache warm.',
43+ message: '[Note: just reply with an empty response to keep the session alive]',
3544 // Per-profile enable map: { [profileId]: boolean }. Unlisted profiles default to enabled.
3645 profiles: {},
3746};
@@ -139,7 +148,12 @@ async function fireKeepalive() {
139148 deactivateSendButtons();
140149
141150 try {
142- await generateQuietPrompt({ quietPrompt: extension_settings[MODULE].message, responseLength: KEEPALIVE_RESPONSE_LENGTH });
151+ // Deliver the keepalive as a USER message at depth 0 (the quiet-prompt path forces a
152+ // system role — openai.js hardcodes role:'system' for it), using the same depth+role
153+ // injection Author's Note uses. The quiet prompt itself stays empty so the full chat
154+ // context is still assembled (which is what keeps the cached prefix warm).
155+ setExtensionPrompt(KEEPALIVE_INJECT_ID, extension_settings[MODULE].message, extension_prompt_types.IN_CHAT, 0, false, extension_prompt_roles.USER);
156+ await generateQuietPrompt({ quietPrompt: '', responseLength: KEEPALIVE_RESPONSE_LENGTH });
143157 // The ping kept this profile warm; treat it as fresh activity so the next
144158 // keepalive for it is a full timeout away.
145159 lastActivity[getActiveProfileKey()] = Date.now();
@@ -147,6 +161,7 @@ async function fireKeepalive() {
147161 // Best-effort: any failure here is harmless and intentionally swallowed.
148162 console.debug('[Keepalive] Background generation ended:', error);
149163 } finally {
164+ setExtensionPrompt(KEEPALIVE_INJECT_ID, '', extension_prompt_types.IN_CHAT, 0, false, extension_prompt_roles.USER);
150165 setSendButtonState(false);
151166 activateSendButtons();
152167 keepaliveActive = false;
@@ -209,6 +224,13 @@ function loadSettings() {
209224 extension_settings[MODULE].profiles = {};
210225 }
211226
227+ // Migrate the original default message to the current default so installs that never
228+ // customized it pick up the improved wording (custom messages are left untouched).
229+ if (extension_settings[MODULE].message === LEGACY_DEFAULT_MESSAGE) {
230+ extension_settings[MODULE].message = defaultSettings.message;
231+ saveSettingsDebounced();
232+ }
233+
212234 $('#keepalive_enabled').prop('checked', extension_settings[MODULE].enabled);
213235 $('#keepalive_timeout').val(extension_settings[MODULE].timeoutSeconds);
214236 $('#keepalive_message').val(extension_settings[MODULE].message);
public/scripts/extensions/keepalive/settings.html+1 -1
@@ -16,7 +16,7 @@
1616 <label for="keepalive_message" data-i18n="ext_keepalive_message">Keepalive message</label>
1717 <input id="keepalive_message" class="text_pole" type="text" />
1818
1919 <small data-i18n="ext_keepalive_help">After this many seconds without activity on the active connection profile, a hidden background prompt (the current chat plus this message (sent as a hidden user message) is sentsubmitted to that profile to keep its server-side prompt cache warm. The completion is capped to a single token and is never saved to the chat. Sending is briefly locked while it runs. Only the profile you're currently using is pinged; each profile has its own idle timer.</small>
2020
2121 <hr>
2222