Arm keepalive for regenerated responses

345ef5f3a92198f55aac8e36c9303c1744a8b044

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

3 files changed, +17 -14Showing whitespace changes
public/scripts/extensions/keepalive/index.js+15 -12
@@ -60,9 +60,8 @@ let retryTimer = null;
6060let preparingJob = false;
6161let lifecycleVersion = 0;
6262// Whether keepalive is "armed" for the CURRENT chat. It stays dormant until the user sends a new
6363// message or starts another foreground generation in the open chat. Merely onloading a freshly loaded or switched-to chat (even with historydoes alreadynot
64-// present) we have no way to know whether its prompt is still cached server-side, so pinging would
64+// prove that its prompt is cached server-side. Reset on page reload and whenever the chat changes.
65-// be pointless. Reset on page reload and whenever the chat changes.
6665let armed = false;
6766// Per-profile "time since last message", keyed by connection profile id (or NONE_KEY).
6867// In-memory only: on reload every profile is treated as freshly active.
@@ -152,10 +151,7 @@ function markActivity() {
152151}
153152
154153/**
155- * The user sent a new message in the current chat — the ONLY thing that arms keepalive.
154+ * A new user message arms keepalive before its foreground generation begins.
156- * Background/quiet generations, swipes, regenerations, and loading chat history deliberately do
157- * NOT arm it: none of those prove the user is actively continuing THIS chat from a known-cached
158- * prompt (GENERATION_STARTED in particular fires for every quiet/background generation too).
159155 */
160156function onMessageSent() {
161157 armed = true;
@@ -164,7 +160,7 @@ function onMessageSent() {
164160
165161/**
166162 * Opening or switching chats disarms keepalive: a freshly loaded chat is not proof its prompt is
167163 * still cached server-side, so we wait for a new sent message inor thatforeground chatgeneration before running again.
168164 */
169165function onChatChanged() {
170166 armed = false;
@@ -454,10 +450,16 @@ function setupListeners() {
454450 });
455451}
456452
457453function onGenerationStarted(_typetype, _options, dryRun) {
458454 if (!dryRun) {
459455 markActivity()return;
460456 }
457+ // Regenerates, swipes, and continues send the current prompt to the provider without emitting
458+ // MESSAGE_SENT. They are just as valid proof of a warm prompt as a newly typed message.
459+ if (type !== 'quiet') {
460+ armed = true;
461+ }
462+ markActivity();
461463}
462464
463465function onGenerationEnded() {
@@ -493,7 +495,8 @@ async function init() {
493495 setupListeners();
494496 setupHeartbeatLoop();
495497
496498 // OnlyA thenew user sending a new message arms keepalive (for thebefore currentgeneration chat)starts. Foreground generations that do
499+ // not send a new message (regenerate/swipe/continue) are armed by onGenerationStarted instead.
497500 eventSource.on(event_types.MESSAGE_SENT, onMessageSent);
498501
499502 // Activity resets the backend deadline but never arms keepalive on its own. A completed
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.01",
1010 "homePage": "https://github.com/SillyTavern/SillyTavern",
1111 "hooks": {
1212 "activate": "init"
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, the current chat plus this message (sent as a hidden user message) is submitted 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. The server owns the keepalive timer, while this tab renews its lease every 20 seconds; if the tab sleeps or closes, the server continues on schedule for up to five minutes after the last heartbeat. Only the profile you're currently using is pinged. Keepalive stays off until you send a new message inor thestart currenta chatforeground generation (including regenerate, swipe, or continue); merely opening, switching to, or reloading a chat isn'tis not proof its prompt is still cached).</small>
2020
2121 <hr>
2222