forked from aikohanasaki/SillyTavern-MemoryBooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoSettings.js
More file actions
365 lines (343 loc) · 14.6 KB
/
Copy pathautoSettings.js
File metadata and controls
365 lines (343 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// Copyright (C) 2024–2026 Aiko Hanasaki
// SPDX-License-Identifier: AGPL-3.0-only
//
// autoSettings.js — Phase 2 (P2.2): settings storage for the Auto subsystem.
//
// Two scopes:
//
// Global → extension_settings.STMemoryBooks.autoModule
// (sentinel on/off, cadence, window size, truncation,
// guard, detection profile, detection prompt, debug logging)
//
// Per-chat → chat_metadata.stmbc
// (enabled, watermark fallback, structure-hint regex,
// prompt override)
//
// Why a separate module (plan §4.5): the Auto subsystem adds ~10 fields to
// both global and per-chat settings. Keeping them in their own module makes
// the merge map clean (single-file addition, additive only) and isolates the
// migration story (one-shot backfill of defaults).
//
// Why the read APIs merge defaults onto whatever's stored: in production we
// see settings objects with partial Auto data (older versions, manual edits,
// half-migrated state). The merger is the single point that decides what a
// field means when it's missing.
// `chat_metadata` is the SillyTavern runtime global. We resolve it lazily via
// a tiny helper so the unit tests can run in a Node-only environment (where
// the ST global isn't on `globalThis`). The public APIs accept an explicit
// `chatMeta` argument; when none is passed, the helper returns `null`, which
// downstream functions treat as "no chat metadata available" (uses defaults).
function getDefaultChatMeta() {
try {
if (typeof globalThis !== 'undefined' && globalThis.chat_metadata != null) {
return globalThis.chat_metadata;
}
} catch (_e) { /* no globalThis */ }
return null;
}
// ----------------------------------------------------------------------------
// Defaults
// ----------------------------------------------------------------------------
/** @type {Readonly<{sentinelEnabled: boolean, cadenceMessages: number, windowSize: number, windowOverlap: number, truncateChars: number, guardSize: number, detectionProfileIndex: number|null, detectionPrompt: string, debugLogging: boolean, auditorOfferEnabled: boolean, auditorEveryNScenes: number, autoSummaryAsSentinelSignal: boolean}>} */
export const AUTO_MODULE_DEFAULTS = Object.freeze({
sentinelEnabled: false,
cadenceMessages: 8,
windowSize: 26,
windowOverlap: 8,
truncateChars: 500,
guardSize: 4,
detectionProfileIndex: null, // null = use the default STMB profile
detectionPrompt: '', // empty = use the bundled baseline prompt
debugLogging: false,
auditorOfferEnabled: true,
auditorEveryNScenes: 15,
// PHA-1664: when true, Sentinel reads upstream autosummary.js scene
// markers (via getSceneMarkers().sceneEnd + 1, filtered to the current
// detection window) and treats them as a peer signal alongside its own
// LLM-detected boundaries. Disable this flag if autosummary's cadence
// detection starts to conflict with Sentinel's LLM path in practice
// (acceptance criterion 3 of PHA-1664).
autoSummaryAsSentinelSignal: true,
});
/** @type {Readonly<{enabled: boolean|null, watermarkFallback: number|null, structureHintRegex: string, promptOverride: string}>} */
export const CHAT_AUTO_DEFAULTS = Object.freeze({
enabled: null, // null = inherit from global sentinelEnabled
watermarkFallback: null, // null = no fallback; integer = override message index
structureHintRegex: '', // empty = no hint
promptOverride: '', // empty = no override
});
// ----------------------------------------------------------------------------
// Numeric clamps (plan §3.3 + §4.1)
// ----------------------------------------------------------------------------
const CLAMPS = Object.freeze({
cadenceMessages: { min: 1, max: 200, def: AUTO_MODULE_DEFAULTS.cadenceMessages },
windowSize: { min: 4, max: 200, def: AUTO_MODULE_DEFAULTS.windowSize },
windowOverlap: { min: 0, max: 100, def: AUTO_MODULE_DEFAULTS.windowOverlap },
truncateChars: { min: 50, max: 5000, def: AUTO_MODULE_DEFAULTS.truncateChars },
guardSize: { min: 0, max: 50, def: AUTO_MODULE_DEFAULTS.guardSize },
watermarkFallback: { min: 0, max: 1_000_000, def: 0 },
auditorEveryNScenes: { min: 1, max: 1000, def: AUTO_MODULE_DEFAULTS.auditorEveryNScenes },
});
function clampInt(value, { min, max, def }) {
if (value == null || value === '') return def;
const n = Number.isInteger(value) ? value : parseInt(value, 10);
if (!Number.isFinite(n)) return def;
if (n < min) return min;
if (n > max) return max;
return n;
}
// ----------------------------------------------------------------------------
// Validation
// ----------------------------------------------------------------------------
/**
* Validate a partial Auto-module patch and return a sanitized version.
* Unknown fields are dropped. Known fields are coerced/clamped.
*
* @param {object} patch
* @returns {object} sanitized patch (may be empty)
*/
export function validateAutoPatch(patch) {
if (!patch || typeof patch !== 'object') return {};
const out = {};
if ('sentinelEnabled' in patch) {
out.sentinelEnabled = !!patch.sentinelEnabled;
}
if ('cadenceMessages' in patch) {
out.cadenceMessages = clampInt(patch.cadenceMessages, CLAMPS.cadenceMessages);
}
if ('windowSize' in patch) {
out.windowSize = clampInt(patch.windowSize, CLAMPS.windowSize);
}
if ('windowOverlap' in patch) {
out.windowOverlap = clampInt(patch.windowOverlap, CLAMPS.windowOverlap);
}
if ('truncateChars' in patch) {
out.truncateChars = clampInt(patch.truncateChars, CLAMPS.truncateChars);
}
if ('guardSize' in patch) {
out.guardSize = clampInt(patch.guardSize, CLAMPS.guardSize);
}
if ('detectionProfileIndex' in patch) {
const v = patch.detectionProfileIndex;
out.detectionProfileIndex = (v == null || v === '' || v === 'null') ? null : clampInt(v, { min: 0, max: 1_000, def: 0 });
}
if ('detectionPrompt' in patch) {
const v = patch.detectionPrompt;
out.detectionPrompt = typeof v === 'string' ? v : '';
}
if ('debugLogging' in patch) {
out.debugLogging = !!patch.debugLogging;
}
if ('auditorOfferEnabled' in patch) {
out.auditorOfferEnabled = !!patch.auditorOfferEnabled;
}
if ('auditorEveryNScenes' in patch) {
out.auditorEveryNScenes = clampInt(patch.auditorEveryNScenes, CLAMPS.auditorEveryNScenes);
}
return out;
}
/**
* Validate a partial per-chat Auto patch. Similar semantics to validateAutoPatch.
* `enabled` accepts null|true|false; null means "inherit from global".
*
* @param {object} patch
* @returns {object} sanitized patch
*/
export function validateChatAutoPatch(patch) {
if (!patch || typeof patch !== 'object') return {};
const out = {};
if ('enabled' in patch) {
const v = patch.enabled;
out.enabled = (v == null || v === '' || v === 'null') ? null : !!v;
}
if ('watermarkFallback' in patch) {
const v = patch.watermarkFallback;
if (v == null || v === '' || v === 'null') {
out.watermarkFallback = null;
} else {
out.watermarkFallback = clampInt(v, CLAMPS.watermarkFallback);
}
}
if ('structureHintRegex' in patch) {
const v = patch.structureHintRegex;
if (typeof v !== 'string') {
out.structureHintRegex = '';
} else {
// Validate the regex compiles; on failure, drop to empty (don't throw).
try {
if (v.length > 0) new RegExp(v);
out.structureHintRegex = v;
} catch (_e) {
out.structureHintRegex = '';
}
}
}
if ('promptOverride' in patch) {
const v = patch.promptOverride;
out.promptOverride = typeof v === 'string' ? v : '';
}
return out;
}
// ----------------------------------------------------------------------------
// Global auto-module read/write
// ----------------------------------------------------------------------------
/**
* Read the global Auto-module settings, merged with defaults.
* Does not mutate the input.
*
* @param {object} settings - the full extension_settings.STMemoryBooks object
* @returns {object} fully-populated autoModule object
*/
export function getAutoSettings(settings) {
const stored = (settings && settings.autoModule && typeof settings.autoModule === 'object')
? settings.autoModule
: {};
return { ...AUTO_MODULE_DEFAULTS, ...stored };
}
/**
* Apply a sanitized patch to the global auto-module settings (mutates in place).
* Creates the autoModule container if missing.
*
* @param {object} settings - the full extension_settings.STMemoryBooks object
* @param {object} patch - sanitized patch from validateAutoPatch
* @returns {object} the resulting autoModule object
*/
export function setAutoSettings(settings, patch) {
if (!settings || typeof settings !== 'object') {
throw new TypeError('setAutoSettings: settings must be an object');
}
if (!settings.autoModule || typeof settings.autoModule !== 'object') {
settings.autoModule = {};
}
const merged = { ...getAutoSettings(settings), ...patch };
Object.assign(settings.autoModule, patch);
return merged;
}
/**
* Backfill missing fields onto the stored autoModule so subsequent reads are stable.
* Migration-safe: only writes if a field is absent.
*
* @param {object} settings
*/
export function initializeAutoSettings(settings) {
if (!settings || typeof settings !== 'object') return;
if (!settings.autoModule || typeof settings.autoModule !== 'object') {
settings.autoModule = { ...AUTO_MODULE_DEFAULTS };
return;
}
for (const [k, v] of Object.entries(AUTO_MODULE_DEFAULTS)) {
if (!(k in settings.autoModule)) settings.autoModule[k] = v;
}
}
// ----------------------------------------------------------------------------
// Per-chat auto settings (chat_metadata.stmbc)
// ----------------------------------------------------------------------------
/**
* Read per-chat Auto settings, merged with defaults. The `enabled` field
* resolves null → the global sentinelEnabled value at read time so callers
* see a concrete boolean.
*
* @param {object} chatMeta - chat_metadata object (defaults to current chat)
* @param {object} [opts]
* @param {boolean} [opts.globalSentinelEnabled] - global sentinel on/off (used to resolve null)
* @returns {object} fully-populated per-chat Auto settings (always concrete enabled)
*/
export function getChatAutoSettings(chatMeta = getDefaultChatMeta(), opts = {}) {
const stored = (chatMeta && chatMeta.stmbc && typeof chatMeta.stmbc === 'object')
? chatMeta.stmbc
: {};
const merged = { ...CHAT_AUTO_DEFAULTS, ...stored };
if (merged.enabled == null) {
merged.enabled = opts.globalSentinelEnabled === true;
}
return merged;
}
/**
* Apply a sanitized patch to the per-chat Auto settings (mutates chatMeta in place).
*
* @param {object} chatMeta
* @param {object} patch - sanitized patch from validateChatAutoPatch
*/
export function setChatAutoSettings(chatMeta, patch) {
if (!chatMeta || typeof chatMeta !== 'object') {
throw new TypeError('setChatAutoSettings: chatMeta must be an object');
}
if (!chatMeta.stmbc || typeof chatMeta.stmbc !== 'object') {
chatMeta.stmbc = {};
}
Object.assign(chatMeta.stmbc, patch);
}
/**
* Backfill per-chat defaults if missing.
*
* @param {object} chatMeta
*/
export function initializeChatAutoSettings(chatMeta) {
if (!chatMeta || typeof chatMeta !== 'object') return;
if (!chatMeta.stmbc || typeof chatMeta.stmbc !== 'object') {
chatMeta.stmbc = { ...CHAT_AUTO_DEFAULTS };
return;
}
for (const [k, v] of Object.entries(CHAT_AUTO_DEFAULTS)) {
if (!(k in chatMeta.stmbc)) chatMeta.stmbc[k] = v;
}
}
// ----------------------------------------------------------------------------
// Resolve "what does the user want for THIS chat right now?"
// ----------------------------------------------------------------------------
/**
* Resolve the effective sentinel on/off for the current chat.
* Per-chat `enabled` (when non-null) wins; otherwise global sentinelEnabled.
*
* @param {object} settings - global extension_settings
* @param {object} [chatMeta] - chat_metadata; defaults to current
* @returns {boolean}
*/
export function resolveSentinelEnabled(settings, chatMeta = getDefaultChatMeta()) {
const globalAuto = getAutoSettings(settings);
const chatAuto = getChatAutoSettings(chatMeta, { globalSentinelEnabled: globalAuto.sentinelEnabled });
return chatAuto.enabled;
}
/**
* Resolve the effective autoSummaryEnabled flag for the current chat.
*
* Phase 2 (P2.4) — plan §4.1 + §1.2.4: native auto-summary is force-disabled
* while sentinel is enabled for a chat. We don't mutate the stored setting —
* we just return false when sentinel is on, so all callers (runtime + UI)
* agree on the same effective value. The setting key stays unchanged
* (`extension_settings.STMemoryBooks.moduleSettings.autoSummaryEnabled`)
* for data compatibility; we just never honor a `true` value while sentinel
* is on.
*
* Note: per plan §1.2.4 we use configuration (this resolver) instead of
* modifying autosummary.js. autosummary.js stays untouched for mergeability.
*
* @param {object} settings - global extension_settings
* @param {object} [chatMeta] - chat_metadata; defaults to current
* @returns {boolean}
*/
export function resolveAutoSummaryEnabled(settings, chatMeta = getDefaultChatMeta()) {
if (resolveSentinelEnabled(settings, chatMeta)) return false;
const stored = settings?.moduleSettings?.autoSummaryEnabled;
return stored === true;
}
/**
* Resolve the effective detection prompt for the current chat.
* Per-chat promptOverride (when non-empty) wins; otherwise global detectionPrompt.
* Empty global detectionPrompt means "use bundled baseline" (signaled by returning null).
*
* @param {object} settings
* @param {object} [chatMeta]
* @returns {string|null} prompt text, or null to use the bundled baseline
*/
export function resolveDetectionPrompt(settings, chatMeta = getDefaultChatMeta()) {
const globalAuto = getAutoSettings(settings);
const chatAuto = getChatAutoSettings(chatMeta, { globalSentinelEnabled: globalAuto.sentinelEnabled });
if (chatAuto.promptOverride && chatAuto.promptOverride.trim().length > 0) {
return chatAuto.promptOverride;
}
if (globalAuto.detectionPrompt && globalAuto.detectionPrompt.trim().length > 0) {
return globalAuto.detectionPrompt;
}
return null;
}