dsh-model-switch 0.4.57 → 0.4.59
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +1 -1
- package/README.zh.md +1 -1
- package/cordis.patch.yml +2 -6
- package/lib/automode-compat.js +54 -3
- package/lib/client.js +29 -2
- package/lib/types/automode-compat.d.ts +23 -0
- package/lib/types/capabilities-rpc.d.ts +2 -0
- package/lib/types/client/main-row-controller.d.ts +14 -0
- package/package.json +1 -1
- package/lib/auto-review-BHMVpmsr.js +0 -1068
- package/lib/auto-review-BbfitRtw.js +0 -1067
- package/lib/auto-review-D0RrXQQb.js +0 -1068
- package/lib/auto-review-QDEFUjT2.js +0 -1067
- package/lib/auto-review-uu9lvAIZ.js +0 -1068
- package/lib/prime-memory-compat-0eGkT2SF.js +0 -652
- package/lib/prime-memory-compat-B7x3nSB2.js +0 -649
- package/lib/prime-memory-compat-C6dvrTgT.js +0 -649
- package/lib/prime-memory-compat-CCKuGrem.js +0 -647
- package/lib/prime-memory-compat-DqmcT12Z.js +0 -645
- package/lib/prime-memory-compat-MlRHdNv4.js +0 -656
|
@@ -1,652 +0,0 @@
|
|
|
1
|
-
import { r as memoryRouteOf } from "./session-route-D8izd8Zd.js";
|
|
2
|
-
//#region lib/types/prime-memory-route.js
|
|
3
|
-
/**
|
|
4
|
-
* Model Switch's adapter to `dsh-prime-memory`'s distillation route.
|
|
5
|
-
*
|
|
6
|
-
* prime-memory owns its distillation route: a `distillChain` in the volatile
|
|
7
|
-
* `live` settings section (`distillProvider`/`distillModel` before 0.x chains
|
|
8
|
-
* existed), read through its own settings handle and committed with
|
|
9
|
-
* `ctx.settings.update('dsh-memory', { live })` — a volatile-only commit that
|
|
10
|
-
* does not remount the plugin. This module only builds the write Model Switch
|
|
11
|
-
* offers; it never runs a distillation call, never rewrites the plugin, and
|
|
12
|
-
* never changes a route the user did not ask to change.
|
|
13
|
-
*
|
|
14
|
-
* All functions here are pure.
|
|
15
|
-
*/
|
|
16
|
-
/** The loader row id prime-memory is composed under (fixed by its bundle patch). */
|
|
17
|
-
const PRIME_MEMORY_ENTRY_ID = "dsh-memory";
|
|
18
|
-
/** The package name that identifies the row; the authoritative identity. */
|
|
19
|
-
const PRIME_MEMORY_PACKAGE = "dsh-prime-memory";
|
|
20
|
-
/** A non-blank string; blank values behave exactly like an absent field. */
|
|
21
|
-
function present(value) {
|
|
22
|
-
return typeof value === "string" && value.trim() !== "";
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Whether one loader entry is prime-memory.
|
|
26
|
-
*
|
|
27
|
-
* The package name wins over the row id: a profile layer may rename the row,
|
|
28
|
-
* while an unrelated row could recycle the id.
|
|
29
|
-
* @param entry - the loader entry to test.
|
|
30
|
-
* @returns True for prime-memory's row.
|
|
31
|
-
*/
|
|
32
|
-
function isPrimeMemoryRow(entry) {
|
|
33
|
-
const name = entry?.options?.name;
|
|
34
|
-
if (present(name)) return name === PRIME_MEMORY_PACKAGE;
|
|
35
|
-
return entry?.options?.id === PRIME_MEMORY_ENTRY_ID;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* The chain prime-memory should use, given the route Model Switch wants.
|
|
39
|
-
*
|
|
40
|
-
* The chosen route goes first; the entries prime-memory already had follow in
|
|
41
|
-
* their own order, minus any route that duplicates the chosen one (a duplicate
|
|
42
|
-
* is a guaranteed second failure, not a fallback). An empty chosen route leaves
|
|
43
|
-
* the existing chain untouched.
|
|
44
|
-
* @param primary - the route Model Switch wants first, if any.
|
|
45
|
-
* @param existing - prime-memory's current chain, as read from its settings.
|
|
46
|
-
* @returns The chain to write; empty when neither side has a complete route.
|
|
47
|
-
*/
|
|
48
|
-
function planDistillChain(primary, existing) {
|
|
49
|
-
const chain = [];
|
|
50
|
-
const seen = /* @__PURE__ */ new Set();
|
|
51
|
-
const push = (entry) => {
|
|
52
|
-
const key = `${entry.provider}\u0000${entry.model}`;
|
|
53
|
-
if (seen.has(key)) return;
|
|
54
|
-
seen.add(key);
|
|
55
|
-
chain.push(entry);
|
|
56
|
-
};
|
|
57
|
-
if (primary !== void 0 && present(primary.provider) && present(primary.model)) {
|
|
58
|
-
const effort = primary.reasoningEffort;
|
|
59
|
-
push(present(effort) ? {
|
|
60
|
-
provider: primary.provider,
|
|
61
|
-
model: primary.model,
|
|
62
|
-
reasoningEffort: effort
|
|
63
|
-
} : {
|
|
64
|
-
provider: primary.provider,
|
|
65
|
-
model: primary.model
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
for (const entry of existing ?? []) {
|
|
69
|
-
if (!present(entry.provider) || !present(entry.model)) continue;
|
|
70
|
-
push(present(entry.reasoningEffort) ? {
|
|
71
|
-
provider: entry.provider,
|
|
72
|
-
model: entry.model,
|
|
73
|
-
reasoningEffort: entry.reasoningEffort
|
|
74
|
-
} : {
|
|
75
|
-
provider: entry.provider,
|
|
76
|
-
model: entry.model
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
return chain;
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* The settings patch that writes one distillation chain.
|
|
83
|
-
*
|
|
84
|
-
* Only the `distillChain` key is produced: prime-memory's `live` section carries
|
|
85
|
-
* unrelated switches (capture, trace, embedding, memory mutation), and a patch
|
|
86
|
-
* naming them would rewrite the user's choices.
|
|
87
|
-
* @param chain - the chain to write.
|
|
88
|
-
* @returns The `live` patch to pass to `ctx.settings.update`.
|
|
89
|
-
*/
|
|
90
|
-
function distillChainPatch(chain) {
|
|
91
|
-
return { live: { distillChain: chain } };
|
|
92
|
-
}
|
|
93
|
-
//#endregion
|
|
94
|
-
//#region lib/types/prime-memory-compat.js
|
|
95
|
-
/**
|
|
96
|
-
* Adapt `dsh-prime-memory`'s distillation route to this plugin's route.
|
|
97
|
-
*
|
|
98
|
-
* The memory plugin resolves its distillation route from its own runtime
|
|
99
|
-
* settings section (`live.distillChain`, legacy `live.distillProvider/Model`,
|
|
100
|
-
* else the deployment default model), and it WRITES that section through the
|
|
101
|
-
* host's settings-forms service:
|
|
102
|
-
*
|
|
103
|
-
* await forms.update('dsh-memory', { live: next })
|
|
104
|
-
*
|
|
105
|
-
* Its row config is read once at plugin init, so patching
|
|
106
|
-
* `entry.options.config.llm.*` afterwards has no effect (verified on 0.2.1-rc.3:
|
|
107
|
-
* the model kept being the deployment default). This adapter therefore writes
|
|
108
|
-
* the same section the plugin itself writes, and only that key:
|
|
109
|
-
*
|
|
110
|
-
* - a chain that is already set and is not the one this adapter wrote counts as
|
|
111
|
-
* user intent and is left alone (`user-set`);
|
|
112
|
-
* - an empty Model Switch route means "leave it alone" (`no-route`);
|
|
113
|
-
* - nothing is persisted by us beyond that section, and no other `live` key
|
|
114
|
-
* (capture, trace, embedding, memory mutation) is touched.
|
|
115
|
-
*
|
|
116
|
-
* The route itself is provider-group based: the group of the deployment default
|
|
117
|
-
* provider (read from the `agent-default-model` row), falling back to the flat
|
|
118
|
-
* memory pair.
|
|
119
|
-
*/
|
|
120
|
-
/** Row name and dependencies, mirroring the sibling adapter row. */
|
|
121
|
-
const name = "model-switch-prime-memory-compat";
|
|
122
|
-
const inject = ["loader", "settings"];
|
|
123
|
-
/** The loader row id prime-memory is composed under (fixed by its bundle patch). */
|
|
124
|
-
const MEMORY_ENTRY_ID = "dsh-memory";
|
|
125
|
-
/** The state the settings page reads. */
|
|
126
|
-
const primeMemoryCompatStatus = {
|
|
127
|
-
installed: false,
|
|
128
|
-
replays: 0
|
|
129
|
-
};
|
|
130
|
-
/**
|
|
131
|
-
* Write the memory row's static route into the profile patch itself.
|
|
132
|
-
*
|
|
133
|
-
* `settings.update` only accepts the entry's *live* schema fields, so the static
|
|
134
|
-
* `llm` pin never survives that call. The pin is what `resolveModelRoute` returns
|
|
135
|
-
* first, and it is the only key every distillation layer honours — so it has to be
|
|
136
|
-
* written where the next start reads it: the row config in the profile document.
|
|
137
|
-
* @param documentPath - the profile patch file.
|
|
138
|
-
* @param provider - route provider to pin.
|
|
139
|
-
* @param model - route model to pin.
|
|
140
|
-
* @returns whether the document changed.
|
|
141
|
-
*/
|
|
142
|
-
/**
|
|
143
|
-
* Remove this plugin's own static pin from the memory row.
|
|
144
|
-
*
|
|
145
|
-
* `effectiveCfg` (dsh-prime-memory `pipeline/runner.js`) treats a non-empty
|
|
146
|
-
* `cfg.llm.provider + cfg.llm.model` pair as "pinned" and then ignores *both* the runtime
|
|
147
|
-
* chain and the legacy `distillProvider`/`distillModel` pair. A pin written earlier — for a
|
|
148
|
-
* host whose live RPC was not ready yet — is therefore not harmless: it keeps winning over
|
|
149
|
-
* every later live push, which is exactly the "the card changed, the calls did not" report.
|
|
150
|
-
* Once a live push is confirmed, the pin has to go.
|
|
151
|
-
*
|
|
152
|
-
* @param documentPath - the profile patch document that holds the memory row.
|
|
153
|
-
* @returns what happened, so the caller can log it without guessing.
|
|
154
|
-
*/
|
|
155
|
-
async function clearStaticPin(documentPath) {
|
|
156
|
-
const { readFile, writeFile } = await import("node:fs/promises");
|
|
157
|
-
let text;
|
|
158
|
-
try {
|
|
159
|
-
text = await readFile(documentPath, "utf8");
|
|
160
|
-
} catch {
|
|
161
|
-
return "missing";
|
|
162
|
-
}
|
|
163
|
-
const rows = text.split(/(?=^- id: )/m);
|
|
164
|
-
const index = rows.findIndex((row) => /^- id: dsh-memory\r?\n/.test(row));
|
|
165
|
-
if (index < 0) return "missing";
|
|
166
|
-
const row = rows[index];
|
|
167
|
-
const trimmed = row.replace(/^ {4}llm:\r?\n(?: {6}.*\r?\n)*/m, "");
|
|
168
|
-
if (trimmed === row) return "absent";
|
|
169
|
-
rows[index] = trimmed;
|
|
170
|
-
try {
|
|
171
|
-
await writeFile(documentPath, rows.join(""), "utf8");
|
|
172
|
-
} catch {
|
|
173
|
-
return "missing";
|
|
174
|
-
}
|
|
175
|
-
return "cleared";
|
|
176
|
-
}
|
|
177
|
-
async function writeStaticPin(documentPath, provider, model) {
|
|
178
|
-
const { readFile, writeFile } = await import("node:fs/promises");
|
|
179
|
-
let text;
|
|
180
|
-
try {
|
|
181
|
-
text = await readFile(documentPath, "utf8");
|
|
182
|
-
} catch {
|
|
183
|
-
return "missing";
|
|
184
|
-
}
|
|
185
|
-
const rows = text.split(/(?=^- id: )/m);
|
|
186
|
-
const index = rows.findIndex((row) => /^- id: dsh-memory\r?\n/.test(row));
|
|
187
|
-
if (index < 0) return "missing";
|
|
188
|
-
const row = rows[index];
|
|
189
|
-
const desired = ` llm:\n provider: ${provider}\n model: ${model}\n`;
|
|
190
|
-
let next;
|
|
191
|
-
if (/^ {4}llm:\r?\n(?: {6}.*\r?\n)*/m.test(row)) next = row.replace(/^ {4}llm:\r?\n(?: {6}.*\r?\n)*/m, desired);
|
|
192
|
-
else if (/^ {2}config:\r?\n/m.test(row)) next = row.replace(/^ {2}config:\r?\n/m, ` config:\n${desired}`);
|
|
193
|
-
else next = row.replace(/^- id: dsh-memory\r?\n/m, `- id: dsh-memory\n config:\n${desired}`);
|
|
194
|
-
if (next === row) return "unchanged";
|
|
195
|
-
rows[index] = next;
|
|
196
|
-
try {
|
|
197
|
-
await writeFile(documentPath, rows.join(""), "utf8");
|
|
198
|
-
} catch {
|
|
199
|
-
return "missing";
|
|
200
|
-
}
|
|
201
|
-
return "written";
|
|
202
|
-
}
|
|
203
|
-
/** A non-blank string. */
|
|
204
|
-
function text(value) {
|
|
205
|
-
return typeof value === "string" ? value.trim() : "";
|
|
206
|
-
}
|
|
207
|
-
/** Read a value as a plain record. */
|
|
208
|
-
function asRecord(value) {
|
|
209
|
-
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* The distillation chain a settings section currently carries.
|
|
213
|
-
*
|
|
214
|
-
* Accepts the chain form and the legacy provider/model pair, so a user who set
|
|
215
|
-
* the old two fields is still recognised as user intent.
|
|
216
|
-
* @param section - the `live` section (or a whole settings view) to read.
|
|
217
|
-
* @returns The chain entries; empty when nothing is set.
|
|
218
|
-
*/
|
|
219
|
-
function chainOf(section) {
|
|
220
|
-
const live = asRecord(section);
|
|
221
|
-
if (live === void 0) return [];
|
|
222
|
-
const chain = live.distillChain;
|
|
223
|
-
if (Array.isArray(chain)) {
|
|
224
|
-
const entries = [];
|
|
225
|
-
for (const item of chain) {
|
|
226
|
-
const entry = asRecord(item);
|
|
227
|
-
const provider = text(entry?.provider);
|
|
228
|
-
const model = text(entry?.model);
|
|
229
|
-
if (provider === "" || model === "") continue;
|
|
230
|
-
const effort = text(entry?.reasoningEffort);
|
|
231
|
-
entries.push(effort === "" ? {
|
|
232
|
-
provider,
|
|
233
|
-
model
|
|
234
|
-
} : {
|
|
235
|
-
provider,
|
|
236
|
-
model,
|
|
237
|
-
reasoningEffort: effort
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
return entries;
|
|
241
|
-
}
|
|
242
|
-
const provider = text(live.distillProvider);
|
|
243
|
-
const model = text(live.distillModel);
|
|
244
|
-
return provider === "" || model === "" ? [] : [{
|
|
245
|
-
provider,
|
|
246
|
-
model
|
|
247
|
-
}];
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Decide the route config one memory row would run with from its config alone.
|
|
251
|
-
*
|
|
252
|
-
* Kept for the settings-free path (a profile that pins `config.llm.*`): the
|
|
253
|
-
* runtime path writes the settings section instead, because the plugin reads its
|
|
254
|
-
* config only at init.
|
|
255
|
-
* @param config - the row's current config (any JSON-ish value).
|
|
256
|
-
* @param route - this plugin's distillation route; an incomplete pair counts as unset.
|
|
257
|
-
* @param adoptedRoute - a `provider/model` this adapter wrote on an earlier pass.
|
|
258
|
-
* @returns The config to install, whether it changed, and what was applied.
|
|
259
|
-
*/
|
|
260
|
-
function planPrimeMemoryRoute(config, route, adoptedRoute) {
|
|
261
|
-
const base = asRecord(config);
|
|
262
|
-
if (base === void 0) return {
|
|
263
|
-
config: {},
|
|
264
|
-
changed: false,
|
|
265
|
-
skipped: "no-route"
|
|
266
|
-
};
|
|
267
|
-
const llm = asRecord(base.llm) ?? {};
|
|
268
|
-
const declaredProvider = text(llm.provider);
|
|
269
|
-
const declaredModel = text(llm.model);
|
|
270
|
-
const declaredRoute = declaredProvider !== "" && declaredModel !== "" ? `${declaredProvider}/${declaredModel}` : void 0;
|
|
271
|
-
if (!(declaredRoute !== void 0 && adoptedRoute !== void 0 && declaredRoute === adoptedRoute) && (declaredProvider !== "" || declaredModel !== "")) return {
|
|
272
|
-
config: base,
|
|
273
|
-
changed: false,
|
|
274
|
-
skipped: "user-set"
|
|
275
|
-
};
|
|
276
|
-
const provider = text(route?.provider);
|
|
277
|
-
const model = text(route?.model);
|
|
278
|
-
if (provider === "" || model === "") return {
|
|
279
|
-
config: base,
|
|
280
|
-
changed: false,
|
|
281
|
-
skipped: "no-route"
|
|
282
|
-
};
|
|
283
|
-
return {
|
|
284
|
-
config: {
|
|
285
|
-
...base,
|
|
286
|
-
llm: {
|
|
287
|
-
...llm,
|
|
288
|
-
provider,
|
|
289
|
-
model
|
|
290
|
-
}
|
|
291
|
-
},
|
|
292
|
-
changed: true,
|
|
293
|
-
route: `${provider}/${model}`
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
/** The endpoint the memory plugin registers for its own runtime settings writes. */
|
|
297
|
-
const MEMORY_RPC_PATH = "/dsh-memory/rpc/settings-set";
|
|
298
|
-
/**
|
|
299
|
-
* Whether the memory row's live section already carries exactly our route.
|
|
300
|
-
*
|
|
301
|
-
* The RPC write path persists the live section to the profile document on every
|
|
302
|
-
* call, and a document change reloads the composition — so pushing an unchanged
|
|
303
|
-
* route re-triggers this adapter forever (a ~6 s recompose loop, observed with
|
|
304
|
-
* `dsh-prime-memory@0.26.10`). Comparing first is what makes the push idempotent
|
|
305
|
-
* across reloads.
|
|
306
|
-
*
|
|
307
|
-
* @param value - the memory row's live settings object, as the forms describe it.
|
|
308
|
-
* @param provider - route provider to compare against.
|
|
309
|
-
* @param model - route model to compare against.
|
|
310
|
-
* @returns true when a push would change nothing.
|
|
311
|
-
*/
|
|
312
|
-
function memoryLiveMatchesRoute(value, provider, model) {
|
|
313
|
-
const outer = value === null || typeof value !== "object" ? void 0 : value;
|
|
314
|
-
if (outer === void 0) return false;
|
|
315
|
-
const inner = outer.live;
|
|
316
|
-
const live = inner !== null && typeof inner === "object" ? inner : outer;
|
|
317
|
-
if (live.distillProvider !== provider || live.distillModel !== model) return false;
|
|
318
|
-
const chain = live.distillChain;
|
|
319
|
-
if (Array.isArray(chain) && chain.length !== 0) return false;
|
|
320
|
-
const layers = live.distillLayerChains;
|
|
321
|
-
if (layers === null || typeof layers !== "object") return false;
|
|
322
|
-
for (const key of [
|
|
323
|
-
"l1",
|
|
324
|
-
"l2",
|
|
325
|
-
"l3"
|
|
326
|
-
]) {
|
|
327
|
-
const layer = layers[key];
|
|
328
|
-
if (!Array.isArray(layer) || layer.length !== 1) return false;
|
|
329
|
-
const first = layer[0];
|
|
330
|
-
if (first === void 0 || first.provider !== provider || first.model !== model) return false;
|
|
331
|
-
}
|
|
332
|
-
return true;
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Hand the route to the memory plugin through the RPC it registers for its own
|
|
336
|
-
* settings page.
|
|
337
|
-
*
|
|
338
|
-
* Writing the config document (what `settings.update` does) only changes what the
|
|
339
|
-
* next start reads; the plugin resolves its route from the runtime settings object
|
|
340
|
-
* it built at `apply`. This endpoint is the documented runtime write path — the
|
|
341
|
-
* same one its own "distillation parameters" panel posts to — so it takes effect
|
|
342
|
-
* without a restart, which is exactly what a provider switch needs.
|
|
343
|
-
*/
|
|
344
|
-
async function pushMemoryRoute(ctx, route) {
|
|
345
|
-
const server = ctx.get("webServer", false);
|
|
346
|
-
const port = server?.port ?? server?.address?.port;
|
|
347
|
-
if (typeof port !== "number" || port <= 0) return "absent";
|
|
348
|
-
try {
|
|
349
|
-
const response = await fetch(`http://127.0.0.1:${port}${MEMORY_RPC_PATH}`, {
|
|
350
|
-
method: "POST",
|
|
351
|
-
headers: { "content-type": "application/json" },
|
|
352
|
-
body: JSON.stringify({
|
|
353
|
-
distillChain: [],
|
|
354
|
-
distillLayerChains: {
|
|
355
|
-
l1: [{
|
|
356
|
-
provider: route.provider,
|
|
357
|
-
model: route.model
|
|
358
|
-
}],
|
|
359
|
-
l2: [{
|
|
360
|
-
provider: route.provider,
|
|
361
|
-
model: route.model
|
|
362
|
-
}],
|
|
363
|
-
l3: [{
|
|
364
|
-
provider: route.provider,
|
|
365
|
-
model: route.model
|
|
366
|
-
}]
|
|
367
|
-
},
|
|
368
|
-
distillProvider: route.provider,
|
|
369
|
-
distillModel: route.model,
|
|
370
|
-
reasoningEffort: ""
|
|
371
|
-
})
|
|
372
|
-
});
|
|
373
|
-
if (response.status === 404 || response.status === 405 || response.status === 403) return "absent";
|
|
374
|
-
return response.ok ? "pushed" : "rejected";
|
|
375
|
-
} catch {
|
|
376
|
-
return "absent";
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
/** The endpoint the memory plugin registers for its own runtime settings reads. */
|
|
380
|
-
const MEMORY_RPC_GET_PATH = "/dsh-memory/rpc/settings-get";
|
|
381
|
-
/**
|
|
382
|
-
* Read the memory plugin's live settings through the RPC its panel posts to.
|
|
383
|
-
*
|
|
384
|
-
* The `settings` forms descriptors are an internal detail of the plugin: on
|
|
385
|
-
* `dsh-prime-memory@0.26.16` this row could not observe them, which skipped every
|
|
386
|
-
* push and left the plugin on its own deployment default. Its RPC is the surface
|
|
387
|
-
* its own panel uses, so reading there keeps the adapter working across plugin
|
|
388
|
-
* releases — and it is what makes the push idempotent across document reloads.
|
|
389
|
-
* @param ctx - this row's context; the host web server supplies the port.
|
|
390
|
-
* @returns The live settings object, or undefined when the seam is unreachable.
|
|
391
|
-
*/
|
|
392
|
-
async function readMemoryLive(ctx) {
|
|
393
|
-
const server = ctx.get("webServer", false);
|
|
394
|
-
const port = server?.port ?? server?.address?.port;
|
|
395
|
-
if (typeof port !== "number" || port <= 0) return void 0;
|
|
396
|
-
try {
|
|
397
|
-
const response = await fetch(`http://127.0.0.1:${port}${MEMORY_RPC_GET_PATH}`, {
|
|
398
|
-
method: "POST",
|
|
399
|
-
headers: { "content-type": "application/json" },
|
|
400
|
-
body: JSON.stringify({ method: "settings-get" })
|
|
401
|
-
});
|
|
402
|
-
if (!response.ok) return void 0;
|
|
403
|
-
return (await response.json())?.value?.settings;
|
|
404
|
-
} catch {
|
|
405
|
-
return;
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
/** The `agent-default-model` row's provider: the deployment default. */
|
|
409
|
-
function defaultProviderOf(loader) {
|
|
410
|
-
if (typeof loader?.entries !== "function") return void 0;
|
|
411
|
-
for (const entry of loader.entries()) {
|
|
412
|
-
if (entry.options?.id !== "agent-default-model") continue;
|
|
413
|
-
const provider = text(asRecord(entry.options?.config)?.provider);
|
|
414
|
-
if (provider !== "") return provider;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
/** The `settings` service the memory plugin itself writes through. */
|
|
418
|
-
function formsOf(ctx) {
|
|
419
|
-
try {
|
|
420
|
-
return ctx.get("settings", false);
|
|
421
|
-
} catch {
|
|
422
|
-
return;
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
/** Live attempts to spend before falling back to the static pin. */
|
|
426
|
-
const PIN_AFTER_ATTEMPTS = 4;
|
|
427
|
-
/**
|
|
428
|
-
* Install the adapter for the whole profile.
|
|
429
|
-
*
|
|
430
|
-
* Writes the memory plugin's own `live` section on activation, on every
|
|
431
|
-
* `loader/patch-context`, on `loader/entry-init`, once more after a short delay
|
|
432
|
-
* (a bundle-inserted row is often composed after this one), and again whenever
|
|
433
|
-
* this plugin's settings change.
|
|
434
|
-
* @param ctx - this row's context.
|
|
435
|
-
* @param loader - the loader service, when present.
|
|
436
|
-
* @param routeOf - resolves this plugin's distillation route for one settings snapshot.
|
|
437
|
-
* @returns The disposer and a replay hook.
|
|
438
|
-
*/
|
|
439
|
-
function installPrimeMemoryCompat(ctx, loader, routeOf) {
|
|
440
|
-
/** The route this adapter wrote last, so it may replace its own value only. */
|
|
441
|
-
let adoptedRoute;
|
|
442
|
-
let pending;
|
|
443
|
-
let attempt = 0;
|
|
444
|
-
const timers = [];
|
|
445
|
-
const primeMemoryEntry = () => {
|
|
446
|
-
for (const entry of loader?.entries?.() ?? []) if (isPrimeMemoryRow(entry)) return entry;
|
|
447
|
-
};
|
|
448
|
-
const memoryRowPresent = () => primeMemoryEntry() !== void 0;
|
|
449
|
-
/** The live section the memory plugin registered, read back through the host. */
|
|
450
|
-
const sectionOf = (forms) => {
|
|
451
|
-
try {
|
|
452
|
-
return (forms.describe?.({ redactSecrets: true }) ?? []).find((item) => item.ns === MEMORY_ENTRY_ID);
|
|
453
|
-
} catch {
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
};
|
|
457
|
-
const scheduleRetry = () => {
|
|
458
|
-
if (attempt >= 8) return;
|
|
459
|
-
const delay = Math.min(4e3, 200 * 2 ** attempt);
|
|
460
|
-
attempt += 1;
|
|
461
|
-
const timer = setTimeout(() => {
|
|
462
|
-
run();
|
|
463
|
-
}, delay);
|
|
464
|
-
timer.unref?.();
|
|
465
|
-
timers.push(timer);
|
|
466
|
-
};
|
|
467
|
-
/**
|
|
468
|
-
* Last resort for a host whose live seams never accept the route.
|
|
469
|
-
*
|
|
470
|
-
* Writing the static pin disables every live form (`pinned` in `effectiveCfg`), so it must
|
|
471
|
-
* not happen while the live attempts still have a chance: a pin written on a slow start is
|
|
472
|
-
* what kept overriding later pushes. It therefore waits for the retry budget to run out.
|
|
473
|
-
*
|
|
474
|
-
* @param forms - the settings service describing the memory row.
|
|
475
|
-
* @param provider - route provider to pin.
|
|
476
|
-
* @param model - route model to pin.
|
|
477
|
-
* @param wanted - `provider/model`, for the log line.
|
|
478
|
-
*/
|
|
479
|
-
const pinAsFallback = async (forms, provider, model, wanted) => {
|
|
480
|
-
if (attempt < PIN_AFTER_ATTEMPTS) {
|
|
481
|
-
scheduleRetry();
|
|
482
|
-
return;
|
|
483
|
-
}
|
|
484
|
-
const documentPath = forms.documentPath;
|
|
485
|
-
if (typeof documentPath !== "string" || documentPath === "") return;
|
|
486
|
-
try {
|
|
487
|
-
if (await writeStaticPin(documentPath, provider, model) === "written") ctx.logger.info(`model-switch: pinned the dsh-prime-memory distillation route to ${wanted} after ${attempt} live attempts (applies from the next start)`);
|
|
488
|
-
} catch (cause) {
|
|
489
|
-
ctx.logger.warn(`model-switch: could not pin the dsh-prime-memory route (${String(cause)})`);
|
|
490
|
-
}
|
|
491
|
-
};
|
|
492
|
-
/**
|
|
493
|
-
* Drop a pin left over from an earlier release.
|
|
494
|
-
*
|
|
495
|
-
* A static `llm.provider/model` pin sets `pinned`, which makes the plugin's
|
|
496
|
-
* `effectiveCfg` ignore the chain *and* the live pair, so the live route decides
|
|
497
|
-
* nothing on that host until the pin is gone.
|
|
498
|
-
* @param forms - the settings seam, when this host exposes it.
|
|
499
|
-
*/
|
|
500
|
-
const clearStalePin = async (forms) => {
|
|
501
|
-
const pinnedDocument = forms?.documentPath;
|
|
502
|
-
if (typeof pinnedDocument !== "string" || pinnedDocument === "") return;
|
|
503
|
-
try {
|
|
504
|
-
if (await clearStaticPin(pinnedDocument) === "cleared") ctx.logger.info("model-switch: removed the stale dsh-prime-memory static pin; the live route decides now");
|
|
505
|
-
} catch (cause) {
|
|
506
|
-
ctx.logger.warn(`model-switch: could not clear the dsh-prime-memory static pin (${String(cause)})`);
|
|
507
|
-
}
|
|
508
|
-
};
|
|
509
|
-
/**
|
|
510
|
-
*
|
|
511
|
-
* The plugin builds that section inside its own `apply` (`settings.register`), so
|
|
512
|
-
* a write issued before that point lands in a section nobody reads — the reason
|
|
513
|
-
* an unverified "write once at mount" pass left it on the deployment default.
|
|
514
|
-
* Writing only once the section is visible, and verifying by reading the chain
|
|
515
|
-
* back, is what makes this measurable instead of hopeful.
|
|
516
|
-
*/
|
|
517
|
-
const run = async () => {
|
|
518
|
-
if (pending !== void 0) return pending;
|
|
519
|
-
if (!memoryRowPresent()) {
|
|
520
|
-
primeMemoryCompatStatus.routeSkipped = "no-row";
|
|
521
|
-
return;
|
|
522
|
-
}
|
|
523
|
-
const route = routeOf();
|
|
524
|
-
const provider = text(route?.provider);
|
|
525
|
-
const model = text(route?.model);
|
|
526
|
-
if (provider === "" || model === "") {
|
|
527
|
-
primeMemoryCompatStatus.routeSkipped = "no-route";
|
|
528
|
-
return;
|
|
529
|
-
}
|
|
530
|
-
const wanted = `${provider}/${model}`;
|
|
531
|
-
const forms = formsOf(ctx);
|
|
532
|
-
const live = await readMemoryLive(ctx);
|
|
533
|
-
if (live === void 0) {
|
|
534
|
-
primeMemoryCompatStatus.routeSkipped = "no-route";
|
|
535
|
-
scheduleRetry();
|
|
536
|
-
return;
|
|
537
|
-
}
|
|
538
|
-
if (memoryLiveMatchesRoute(live, provider, model)) {
|
|
539
|
-
adoptedRoute = wanted;
|
|
540
|
-
primeMemoryCompatStatus.installed = true;
|
|
541
|
-
primeMemoryCompatStatus.route = wanted;
|
|
542
|
-
delete primeMemoryCompatStatus.routeSkipped;
|
|
543
|
-
await clearStalePin(forms);
|
|
544
|
-
return;
|
|
545
|
-
}
|
|
546
|
-
const declared = chainOf(live);
|
|
547
|
-
const declaredRoute = declared.length > 0 ? `${text(declared[0]?.provider)}/${text(declared[0]?.model)}` : "";
|
|
548
|
-
if (declaredRoute !== "" && declaredRoute !== wanted && declaredRoute !== adoptedRoute) {
|
|
549
|
-
primeMemoryCompatStatus.routeSkipped = "user-set";
|
|
550
|
-
primeMemoryCompatStatus.route = void 0;
|
|
551
|
-
return;
|
|
552
|
-
}
|
|
553
|
-
if (await pushMemoryRoute(ctx, {
|
|
554
|
-
provider,
|
|
555
|
-
model
|
|
556
|
-
}) === "pushed") {
|
|
557
|
-
adoptedRoute = wanted;
|
|
558
|
-
attempt = 0;
|
|
559
|
-
primeMemoryCompatStatus.installed = true;
|
|
560
|
-
primeMemoryCompatStatus.route = wanted;
|
|
561
|
-
delete primeMemoryCompatStatus.routeSkipped;
|
|
562
|
-
await clearStalePin(forms);
|
|
563
|
-
return;
|
|
564
|
-
}
|
|
565
|
-
if (forms === void 0 || forms.update === void 0 || forms.describe === void 0) {
|
|
566
|
-
primeMemoryCompatStatus.routeSkipped = "no-route";
|
|
567
|
-
scheduleRetry();
|
|
568
|
-
return;
|
|
569
|
-
}
|
|
570
|
-
const section = sectionOf(forms);
|
|
571
|
-
if (section === void 0 || asRecord(section.value) === void 0) {
|
|
572
|
-
await pinAsFallback(forms, provider, model, wanted);
|
|
573
|
-
return;
|
|
574
|
-
}
|
|
575
|
-
const chain = planDistillChain({
|
|
576
|
-
provider,
|
|
577
|
-
model
|
|
578
|
-
}, chainOf(section.value));
|
|
579
|
-
pending = forms.update(MEMORY_ENTRY_ID, distillChainPatch(chain), section.revision).then(async () => {
|
|
580
|
-
const applied = chainOf(sectionOf(forms)?.value);
|
|
581
|
-
if ((applied.length > 0 ? `${text(applied[0]?.provider)}/${text(applied[0]?.model)}` : "") !== wanted) {
|
|
582
|
-
await pinAsFallback(forms, provider, model, wanted);
|
|
583
|
-
return;
|
|
584
|
-
}
|
|
585
|
-
adoptedRoute = wanted;
|
|
586
|
-
attempt = 0;
|
|
587
|
-
primeMemoryCompatStatus.installed = true;
|
|
588
|
-
primeMemoryCompatStatus.route = wanted;
|
|
589
|
-
delete primeMemoryCompatStatus.routeSkipped;
|
|
590
|
-
}).catch(async (cause) => {
|
|
591
|
-
primeMemoryCompatStatus.routeSkipped = "no-route";
|
|
592
|
-
ctx.logger.warn(`model-switch: could not write the dsh-prime-memory distillation route (${String(cause)})`);
|
|
593
|
-
await pinAsFallback(forms, provider, model, wanted);
|
|
594
|
-
}).finally(() => {
|
|
595
|
-
pending = void 0;
|
|
596
|
-
});
|
|
597
|
-
return pending;
|
|
598
|
-
};
|
|
599
|
-
const replay = () => {
|
|
600
|
-
run();
|
|
601
|
-
return true;
|
|
602
|
-
};
|
|
603
|
-
run();
|
|
604
|
-
const offPatch = ctx.on("loader/patch-context", function(first, second) {
|
|
605
|
-
const nextFn = typeof first === "function" ? first : second;
|
|
606
|
-
if (isPrimeMemoryRow(typeof first === "function" ? this : first)) run();
|
|
607
|
-
return nextFn === void 0 ? void 0 : nextFn();
|
|
608
|
-
});
|
|
609
|
-
const offInit = ctx.on("loader/entry-init", function(first) {
|
|
610
|
-
const candidate = this ?? first;
|
|
611
|
-
if (candidate !== void 0 && isPrimeMemoryRow(candidate)) run();
|
|
612
|
-
});
|
|
613
|
-
const offService = ctx.on("internal/service", (name) => {
|
|
614
|
-
if (name === "settings") run();
|
|
615
|
-
});
|
|
616
|
-
const lateSweep = setTimeout(() => {
|
|
617
|
-
run();
|
|
618
|
-
}, 1500);
|
|
619
|
-
return {
|
|
620
|
-
dispose: () => {
|
|
621
|
-
offPatch();
|
|
622
|
-
offInit();
|
|
623
|
-
offService();
|
|
624
|
-
clearTimeout(lateSweep);
|
|
625
|
-
for (const timer of timers) clearTimeout(timer);
|
|
626
|
-
},
|
|
627
|
-
replay
|
|
628
|
-
};
|
|
629
|
-
}
|
|
630
|
-
/**
|
|
631
|
-
* Row entry point: adapt the memory plugin on activation and on remount.
|
|
632
|
-
* @param ctx - this row's context.
|
|
633
|
-
*/
|
|
634
|
-
function apply(ctx) {
|
|
635
|
-
ctx.inject(["modelSwitch"], (withProvider) => {
|
|
636
|
-
withProvider.inject(["webServer"], (scope) => scope.effect(() => {
|
|
637
|
-
const runtime = scope.modelSwitch;
|
|
638
|
-
const loader = scope.get("loader", false);
|
|
639
|
-
const routeOf = () => memoryRouteOf(runtime.currentSettings(), defaultProviderOf(loader));
|
|
640
|
-
const adapter = installPrimeMemoryCompat(scope, loader, routeOf);
|
|
641
|
-
const off = scope.on("model-switch/settings-update", () => {
|
|
642
|
-
if (adapter.replay()) scope.logger.info("model-switch: re-applied the dsh-prime-memory distillation route after a settings change");
|
|
643
|
-
});
|
|
644
|
-
return () => {
|
|
645
|
-
off();
|
|
646
|
-
adapter.dispose();
|
|
647
|
-
};
|
|
648
|
-
}, "model-switch: keep the dsh-prime-memory route aligned with the settings"));
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
//#endregion
|
|
652
|
-
export { installPrimeMemoryCompat as a, planPrimeMemoryRoute as c, readMemoryLive as d, writeStaticPin as f, inject as i, primeMemoryCompatStatus as l, chainOf as n, memoryLiveMatchesRoute as o, clearStaticPin as r, name as s, apply as t, pushMemoryRoute as u };
|