dsh-model-think-level 2.3.0 → 2.3.2
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/README.md +12 -1
- package/lib/client/ConfigPane.d.ts +16 -7
- package/lib/client/KeyManager.d.ts +3 -0
- package/lib/client/auto-continue-pref.d.ts +33 -0
- package/lib/client/auto-continue-rules.d.ts +96 -0
- package/lib/client/image-config-client.d.ts +10 -1
- package/lib/client/index.d.ts +1 -0
- package/lib/client/injection/auto-continue-run.d.ts +53 -0
- package/lib/client/injection/config-button-seat.d.ts +2 -2
- package/lib/client/injection/session-directory.d.ts +44 -0
- package/lib/client/locales.d.ts +17 -0
- package/lib/client/styles.d.ts +1 -1
- package/lib/client.js +153 -14
- package/lib/client.js.map +4 -4
- package/lib/image/config.d.ts +6 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +281 -208
- package/lib/index.js.map +4 -4
- package/lib/key-index.d.ts +14 -5
- package/lib/key-mutation-queue.d.ts +2 -0
- package/package.json +5 -3
package/lib/index.js
CHANGED
|
@@ -107,7 +107,8 @@ function maskKeyValue(value) {
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
// src/key-index.ts
|
|
110
|
-
import { mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
110
|
+
import { mkdirSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
111
|
+
import { randomUUID } from "node:crypto";
|
|
111
112
|
import { homedir } from "node:os";
|
|
112
113
|
import { dirname, join } from "node:path";
|
|
113
114
|
function makeEntry(ref, alias, models, protocol, disabled, auto) {
|
|
@@ -119,6 +120,12 @@ function makeEntry(ref, alias, models, protocol, disabled, auto) {
|
|
|
119
120
|
if (auto === true) entry.auto = true;
|
|
120
121
|
return entry;
|
|
121
122
|
}
|
|
123
|
+
function setOwnRouteValue(map, route, value) {
|
|
124
|
+
Object.defineProperty(map, route, { value, enumerable: true, configurable: true, writable: true });
|
|
125
|
+
}
|
|
126
|
+
function ownRouteValue(map, route) {
|
|
127
|
+
return map !== void 0 && Object.hasOwn(map, route) ? map[route] : void 0;
|
|
128
|
+
}
|
|
122
129
|
function emptyKeyIndex() {
|
|
123
130
|
return { version: 1, providers: {} };
|
|
124
131
|
}
|
|
@@ -156,7 +163,7 @@ function parseKeyIndex(raw) {
|
|
|
156
163
|
const auto = normalizeKeyAuto(record["auto"]);
|
|
157
164
|
list.push(makeEntry(ref, alias, models, protocol, disabled, auto));
|
|
158
165
|
}
|
|
159
|
-
if (list.length > 0) out
|
|
166
|
+
if (list.length > 0) setOwnRouteValue(out, route, list);
|
|
160
167
|
}
|
|
161
168
|
const endpoints = parseEndpoints(root["endpoints"]);
|
|
162
169
|
const balances = parseBalances(root["balances"]);
|
|
@@ -171,7 +178,7 @@ function parseBalances(raw) {
|
|
|
171
178
|
for (const [route, value] of Object.entries(raw)) {
|
|
172
179
|
if (route.length === 0) continue;
|
|
173
180
|
const endpoint = normalizeBalanceEndpoint(value);
|
|
174
|
-
if (endpoint !== void 0) out
|
|
181
|
+
if (endpoint !== void 0) setOwnRouteValue(out, route, endpoint);
|
|
175
182
|
}
|
|
176
183
|
return Object.keys(out).length === 0 ? void 0 : out;
|
|
177
184
|
}
|
|
@@ -181,7 +188,7 @@ function parseEndpoints(raw) {
|
|
|
181
188
|
for (const [route, value] of Object.entries(raw)) {
|
|
182
189
|
if (route.length === 0) continue;
|
|
183
190
|
const pair = normalizeRouteEndpoints(value);
|
|
184
|
-
if (pair !== void 0) out
|
|
191
|
+
if (pair !== void 0) setOwnRouteValue(out, route, pair);
|
|
185
192
|
}
|
|
186
193
|
return Object.keys(out).length === 0 ? void 0 : out;
|
|
187
194
|
}
|
|
@@ -217,12 +224,44 @@ function readKeyIndex(file = keyIndexFilePath()) {
|
|
|
217
224
|
}
|
|
218
225
|
return parseKeyIndex(raw);
|
|
219
226
|
}
|
|
227
|
+
var KEY_INDEX_HISTORY_LIMIT = 64;
|
|
228
|
+
function keyIndexHistoryPath(file = keyIndexFilePath()) {
|
|
229
|
+
return `${file}.history`;
|
|
230
|
+
}
|
|
231
|
+
function trimKeyIndexHistory(file) {
|
|
232
|
+
try {
|
|
233
|
+
const directory = keyIndexHistoryPath(file);
|
|
234
|
+
const snapshots = readdirSync(directory, { withFileTypes: true }).filter((entry) => entry.isFile() && /^\d{13}-[0-9a-f-]{36}\.json$/.test(entry.name)).map((entry) => entry.name).sort();
|
|
235
|
+
for (const name2 of snapshots.slice(0, Math.max(0, snapshots.length - KEY_INDEX_HISTORY_LIMIT))) {
|
|
236
|
+
unlinkSync(join(directory, name2));
|
|
237
|
+
}
|
|
238
|
+
} catch {
|
|
239
|
+
}
|
|
240
|
+
}
|
|
220
241
|
function writeKeyIndex(document, file = keyIndexFilePath()) {
|
|
242
|
+
const text = `${JSON.stringify(document, null, 2)}
|
|
243
|
+
`;
|
|
244
|
+
let previous;
|
|
245
|
+
try {
|
|
246
|
+
previous = readFileSync(file, "utf8");
|
|
247
|
+
} catch (error) {
|
|
248
|
+
if (!(typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT")) throw error;
|
|
249
|
+
}
|
|
250
|
+
if (previous === text) return;
|
|
221
251
|
mkdirSync(dirname(file), { recursive: true });
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
252
|
+
if (previous !== void 0) {
|
|
253
|
+
readKeyIndex(file);
|
|
254
|
+
const directory = keyIndexHistoryPath(file);
|
|
255
|
+
mkdirSync(directory, { recursive: true, mode: 448 });
|
|
256
|
+
writeFileSync(
|
|
257
|
+
join(directory, `${Date.now()}-${randomUUID()}.json`),
|
|
258
|
+
previous,
|
|
259
|
+
{ encoding: "utf8", flag: "wx", mode: 384 }
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
const temporary = `${file}.${process.pid}.${randomUUID()}.tmp`;
|
|
225
263
|
try {
|
|
264
|
+
writeFileSync(temporary, text, { encoding: "utf8", flag: "wx", mode: 384 });
|
|
226
265
|
renameSync(temporary, file);
|
|
227
266
|
} catch (error) {
|
|
228
267
|
try {
|
|
@@ -231,16 +270,27 @@ function writeKeyIndex(document, file = keyIndexFilePath()) {
|
|
|
231
270
|
}
|
|
232
271
|
throw error;
|
|
233
272
|
}
|
|
273
|
+
if (previous !== void 0) trimKeyIndexHistory(file);
|
|
234
274
|
}
|
|
235
275
|
function updateKeyIndex(mutate, file = keyIndexFilePath()) {
|
|
236
|
-
const
|
|
276
|
+
const current = readKeyIndex(file);
|
|
277
|
+
const next = mutate(current);
|
|
278
|
+
if (JSON.stringify(readKeyIndex(file)) !== JSON.stringify(current)) {
|
|
279
|
+
throw new Error("the key index changed while updating; retry the operation");
|
|
280
|
+
}
|
|
237
281
|
writeKeyIndex(next, file);
|
|
238
282
|
return next;
|
|
239
283
|
}
|
|
240
284
|
function keyEntriesOf(document, route) {
|
|
241
|
-
const entries = document.providers
|
|
285
|
+
const entries = ownRouteValue(document.providers, route);
|
|
242
286
|
return Array.isArray(entries) ? entries : [];
|
|
243
287
|
}
|
|
288
|
+
function routeEndpointsOf(document, route) {
|
|
289
|
+
return ownRouteValue(document.endpoints, route);
|
|
290
|
+
}
|
|
291
|
+
function routeBalanceOf(document, route) {
|
|
292
|
+
return ownRouteValue(document.balances, route);
|
|
293
|
+
}
|
|
244
294
|
function hasKeyEntry(document, route, ref) {
|
|
245
295
|
return keyEntriesOf(document, route).some((entry) => entry.ref === ref);
|
|
246
296
|
}
|
|
@@ -301,7 +351,7 @@ function withRouteEndpoints(document, route, endpoints) {
|
|
|
301
351
|
const map = { ...document.endpoints ?? {} };
|
|
302
352
|
const cleaned = normalizeRouteEndpoints(endpoints);
|
|
303
353
|
if (cleaned === void 0) delete map[route];
|
|
304
|
-
else map
|
|
354
|
+
else setOwnRouteValue(map, route, cleaned);
|
|
305
355
|
const next = { version: 1, providers: document.providers, ...balancesOf(document) };
|
|
306
356
|
return Object.keys(map).length === 0 ? next : { ...next, endpoints: map };
|
|
307
357
|
}
|
|
@@ -309,7 +359,7 @@ function withRouteBalance(document, route, endpoint) {
|
|
|
309
359
|
const map = { ...document.balances ?? {} };
|
|
310
360
|
const cleaned = normalizeBalanceEndpoint(endpoint);
|
|
311
361
|
if (cleaned === void 0) delete map[route];
|
|
312
|
-
else map
|
|
362
|
+
else setOwnRouteValue(map, route, cleaned);
|
|
313
363
|
const next = { version: 1, providers: document.providers, ...endpointsOf(document) };
|
|
314
364
|
return Object.keys(map).length === 0 ? next : { ...next, balances: map };
|
|
315
365
|
}
|
|
@@ -323,20 +373,33 @@ function withoutKeyEntry(document, route, ref) {
|
|
|
323
373
|
const list = keyEntriesOf(document, route).filter((entry) => entry.ref !== ref);
|
|
324
374
|
const providers = { ...document.providers };
|
|
325
375
|
if (list.length === 0) delete providers[route];
|
|
326
|
-
else providers
|
|
376
|
+
else setOwnRouteValue(providers, route, list);
|
|
327
377
|
return { version: 1, providers, ...endpointsOf(document), ...balancesOf(document) };
|
|
328
378
|
}
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
379
|
+
|
|
380
|
+
// src/key-mutation-queue.ts
|
|
381
|
+
import { resolve } from "node:path";
|
|
382
|
+
var KEY_MUTATION_QUEUES = /* @__PURE__ */ Symbol.for("dsh-model-think-level.key-mutation-queues");
|
|
383
|
+
var registry = globalThis;
|
|
384
|
+
var queues = registry[KEY_MUTATION_QUEUES] ??= /* @__PURE__ */ new Map();
|
|
385
|
+
async function acquireKeyMutation(file = keyIndexFilePath()) {
|
|
386
|
+
const absolute = resolve(file);
|
|
387
|
+
const key = process.platform === "win32" ? absolute.toLowerCase() : absolute;
|
|
388
|
+
const previous = queues.get(key) ?? Promise.resolve();
|
|
389
|
+
let releaseTurn = () => {
|
|
390
|
+
};
|
|
391
|
+
const turn = new Promise((done) => {
|
|
392
|
+
releaseTurn = done;
|
|
393
|
+
});
|
|
394
|
+
queues.set(key, turn);
|
|
395
|
+
await previous;
|
|
396
|
+
let released = false;
|
|
397
|
+
return () => {
|
|
398
|
+
if (released) return;
|
|
399
|
+
released = true;
|
|
400
|
+
if (queues.get(key) === turn) queues.delete(key);
|
|
401
|
+
releaseTurn();
|
|
402
|
+
};
|
|
340
403
|
}
|
|
341
404
|
|
|
342
405
|
// src/key-route-core.ts
|
|
@@ -3127,12 +3190,12 @@ var REGISTRY = /* @__PURE__ */ Symbol.for("dsh-model-think-level.fetch-overrides
|
|
|
3127
3190
|
var DISPATCH_REGISTRY = /* @__PURE__ */ Symbol.for("dsh-model-think-level.dispatch-overrides");
|
|
3128
3191
|
function dispatchRegistryOf() {
|
|
3129
3192
|
const holder = globalThis;
|
|
3130
|
-
let
|
|
3131
|
-
if (
|
|
3132
|
-
|
|
3133
|
-
holder[DISPATCH_REGISTRY] =
|
|
3193
|
+
let registry2 = holder[DISPATCH_REGISTRY];
|
|
3194
|
+
if (registry2 === void 0) {
|
|
3195
|
+
registry2 = { hooks: [] };
|
|
3196
|
+
holder[DISPATCH_REGISTRY] = registry2;
|
|
3134
3197
|
}
|
|
3135
|
-
return
|
|
3198
|
+
return registry2;
|
|
3136
3199
|
}
|
|
3137
3200
|
function methodOf(input, init) {
|
|
3138
3201
|
if (init?.method !== void 0 && init.method.length > 0) return init.method.toUpperCase();
|
|
@@ -3407,24 +3470,24 @@ async function withUnservedNote(response, bodyText, model, tried) {
|
|
|
3407
3470
|
}
|
|
3408
3471
|
function installHeaderOverlay(source) {
|
|
3409
3472
|
const holder = globalThis;
|
|
3410
|
-
let
|
|
3473
|
+
let registry2 = holder[REGISTRY];
|
|
3411
3474
|
const token = /* @__PURE__ */ Symbol("bre-header-overlay");
|
|
3412
|
-
if (
|
|
3475
|
+
if (registry2 === void 0) {
|
|
3413
3476
|
const base = globalThis.fetch;
|
|
3414
3477
|
const entries = /* @__PURE__ */ new Map();
|
|
3415
3478
|
const wrapper = makeWrapper(base, entries);
|
|
3416
|
-
|
|
3417
|
-
holder[REGISTRY] =
|
|
3479
|
+
registry2 = { base, wrapper, entries };
|
|
3480
|
+
holder[REGISTRY] = registry2;
|
|
3418
3481
|
globalThis.fetch = wrapper;
|
|
3419
3482
|
}
|
|
3420
|
-
|
|
3483
|
+
registry2.entries.set(token, {
|
|
3421
3484
|
index: () => source.current,
|
|
3422
3485
|
// Delegate through the live source property so a resolver attached after
|
|
3423
3486
|
// install is honoured, and its internal data stays current.
|
|
3424
3487
|
keys: source.keys === void 0 ? void 0 : (context) => source.keys(context),
|
|
3425
3488
|
modelListing: source.modelListing === void 0 ? void 0 : (context) => source.modelListing(context)
|
|
3426
3489
|
});
|
|
3427
|
-
const owned =
|
|
3490
|
+
const owned = registry2;
|
|
3428
3491
|
return {
|
|
3429
3492
|
get active() {
|
|
3430
3493
|
return owned.entries.has(token);
|
|
@@ -3438,12 +3501,12 @@ function installHeaderOverlay(source) {
|
|
|
3438
3501
|
};
|
|
3439
3502
|
}
|
|
3440
3503
|
function headerOverlayInstalled() {
|
|
3441
|
-
const
|
|
3442
|
-
return
|
|
3504
|
+
const registry2 = registryOf();
|
|
3505
|
+
return registry2 !== void 0 && globalThis.fetch === registry2.wrapper;
|
|
3443
3506
|
}
|
|
3444
3507
|
function headerOverlayBypass() {
|
|
3445
|
-
const
|
|
3446
|
-
if (
|
|
3508
|
+
const registry2 = registryOf();
|
|
3509
|
+
if (registry2 !== void 0 && globalThis.fetch === registry2.wrapper) return registry2.base;
|
|
3447
3510
|
return globalThis.fetch;
|
|
3448
3511
|
}
|
|
3449
3512
|
|
|
@@ -3865,7 +3928,7 @@ function installPiAiHeaderTransforms(llm, source) {
|
|
|
3865
3928
|
// src/headers-conflict.ts
|
|
3866
3929
|
import { existsSync, readFileSync as readFileSync4 } from "node:fs";
|
|
3867
3930
|
import { createRequire } from "node:module";
|
|
3868
|
-
import { dirname as dirname4, join as join4, resolve } from "node:path";
|
|
3931
|
+
import { dirname as dirname4, join as join4, resolve as resolve2 } from "node:path";
|
|
3869
3932
|
var SIBLING_PACKAGES = [
|
|
3870
3933
|
"dsh-client-masquerade",
|
|
3871
3934
|
"dsh-llm-headers",
|
|
@@ -3877,7 +3940,7 @@ function searchRoots() {
|
|
|
3877
3940
|
const roots = /* @__PURE__ */ new Set();
|
|
3878
3941
|
const entry = process.argv[1];
|
|
3879
3942
|
if (typeof entry === "string" && entry.length > 0) {
|
|
3880
|
-
let dir = dirname4(
|
|
3943
|
+
let dir = dirname4(resolve2(entry));
|
|
3881
3944
|
for (let depth = 0; depth < 8; depth += 1) {
|
|
3882
3945
|
roots.add(join4(dir, "node_modules"));
|
|
3883
3946
|
const parent = dirname4(dir);
|
|
@@ -3984,6 +4047,7 @@ function imageConfigFilePath(home = harnessHome()) {
|
|
|
3984
4047
|
function emptyImageConfig(now = () => /* @__PURE__ */ new Date()) {
|
|
3985
4048
|
return {
|
|
3986
4049
|
version: 2,
|
|
4050
|
+
enabled: true,
|
|
3987
4051
|
syncedAt: now().toISOString(),
|
|
3988
4052
|
defaultModel: null,
|
|
3989
4053
|
defaults: { size: "1024x1024", quality: "high", n: 1, timeoutMs: 3e5 },
|
|
@@ -4039,6 +4103,9 @@ function parseImageConfig(raw, now = () => /* @__PURE__ */ new Date()) {
|
|
|
4039
4103
|
}
|
|
4040
4104
|
return {
|
|
4041
4105
|
version: 2,
|
|
4106
|
+
// A config written before the switch existed has no opinion on it: the
|
|
4107
|
+
// shipped behaviour was "on", so only an explicit false turns it off.
|
|
4108
|
+
enabled: raw["enabled"] !== false,
|
|
4042
4109
|
syncedAt: typeof raw["syncedAt"] === "string" ? raw["syncedAt"] : base.syncedAt,
|
|
4043
4110
|
defaultModel: typeof raw["defaultModel"] === "string" && raw["defaultModel"].length > 0 ? raw["defaultModel"] : null,
|
|
4044
4111
|
defaults,
|
|
@@ -4184,7 +4251,7 @@ function updateImageConfig(mutate, file = imageConfigFilePath()) {
|
|
|
4184
4251
|
|
|
4185
4252
|
// src/image/generate.ts
|
|
4186
4253
|
import { mkdirSync as mkdirSync5, readFileSync as readFileSync6, writeFileSync as writeFileSync5, existsSync as existsSync2, statSync } from "node:fs";
|
|
4187
|
-
import { dirname as dirname6, join as join6, resolve as
|
|
4254
|
+
import { dirname as dirname6, join as join6, resolve as resolve3 } from "node:path";
|
|
4188
4255
|
|
|
4189
4256
|
// src/image/deadline.ts
|
|
4190
4257
|
function createImageDeadline(timeoutMs, caller, errorFor) {
|
|
@@ -4215,7 +4282,7 @@ function createImageDeadline(timeoutMs, caller, errorFor) {
|
|
|
4215
4282
|
return {
|
|
4216
4283
|
signal: controller.signal,
|
|
4217
4284
|
check,
|
|
4218
|
-
run: (work) => new Promise((
|
|
4285
|
+
run: (work) => new Promise((resolve4, reject) => {
|
|
4219
4286
|
let settled = false;
|
|
4220
4287
|
const finish = (done) => {
|
|
4221
4288
|
if (settled) return;
|
|
@@ -4237,7 +4304,7 @@ function createImageDeadline(timeoutMs, caller, errorFor) {
|
|
|
4237
4304
|
Promise.resolve(work()).then((value) => {
|
|
4238
4305
|
try {
|
|
4239
4306
|
check();
|
|
4240
|
-
finish(() =>
|
|
4307
|
+
finish(() => resolve4(value));
|
|
4241
4308
|
} catch (error) {
|
|
4242
4309
|
finish(() => reject(error));
|
|
4243
4310
|
}
|
|
@@ -4439,7 +4506,7 @@ function resolveDestination(requested, fallbackDir, slug, now) {
|
|
|
4439
4506
|
mkdirSync5(fallbackDir, { recursive: true });
|
|
4440
4507
|
return destinationOf(fallbackDir, slug, now);
|
|
4441
4508
|
}
|
|
4442
|
-
const target =
|
|
4509
|
+
const target = resolve3(requested.trim());
|
|
4443
4510
|
let isDir = false;
|
|
4444
4511
|
try {
|
|
4445
4512
|
isDir = statSync(target).isDirectory();
|
|
@@ -4512,6 +4579,7 @@ async function generateImage(deps, request, sharedDeadline) {
|
|
|
4512
4579
|
}
|
|
4513
4580
|
}
|
|
4514
4581
|
async function generateWithinDeadline(deps, request, config, deadline) {
|
|
4582
|
+
if (!config.enabled) throw new GenerateError("config", "\u751F\u56FE\u670D\u52A1\u5DF2\u5173\u95ED\u2014\u2014\u53EF\u5728\u300C\u9AD8\u7EA7\u8BBE\u7F6E \u2192 \u751F\u56FE\u300D\u91CC\u91CD\u65B0\u5F00\u542F");
|
|
4515
4583
|
const prompt = request.prompt.trim();
|
|
4516
4584
|
if (prompt.length === 0) throw new GenerateError("param", "\u63D0\u793A\u8BCD\u4E3A\u7A7A");
|
|
4517
4585
|
const model = resolveImageModel(config, request.model);
|
|
@@ -4535,7 +4603,7 @@ async function generateWithinDeadline(deps, request, config, deadline) {
|
|
|
4535
4603
|
...(request.referenceFiles ?? []).map((file) => ({ name: file.name, bytes: file.bytes, mime: file.mime })),
|
|
4536
4604
|
...(request.referenceImages ?? []).map((path) => {
|
|
4537
4605
|
deadline.check();
|
|
4538
|
-
const absolute =
|
|
4606
|
+
const absolute = resolve3(path);
|
|
4539
4607
|
if (!existsSync2(absolute)) throw new GenerateError("reference", `\u53C2\u8003\u56FE\u4E0D\u5B58\u5728\u6216\u4E0D\u53EF\u8BFB\uFF1A${path}`);
|
|
4540
4608
|
try {
|
|
4541
4609
|
return { name: absolute.split(/[\\/]/).pop() ?? "image", bytes: readFileSync6(absolute), mime: mimeOfPath(absolute) };
|
|
@@ -4940,6 +5008,7 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
4940
5008
|
const parsed = parseArgs(rawArgs);
|
|
4941
5009
|
if (typeof parsed === "string") throw new Error(parsed);
|
|
4942
5010
|
const config = deps.generateDeps.config?.() ?? deps.readConfig();
|
|
5011
|
+
if (!config.enabled) throw new Error("\u751F\u56FE\u670D\u52A1\u5DF2\u5173\u95ED\u2014\u2014\u53EF\u5728\u300C\u9AD8\u7EA7\u8BBE\u7F6E \u2192 \u751F\u56FE\u300D\u91CC\u91CD\u65B0\u5F00\u542F");
|
|
4943
5012
|
const deadline = createGenerateDeadline(deps.generateDeps.timeoutMs ?? config.defaults.timeoutMs, exec.signal);
|
|
4944
5013
|
try {
|
|
4945
5014
|
deadline.check();
|
|
@@ -5029,6 +5098,7 @@ function registerGenerateImageTool(ctx, deps) {
|
|
|
5029
5098
|
};
|
|
5030
5099
|
}
|
|
5031
5100
|
function imageToolHint(config) {
|
|
5101
|
+
if (!config.enabled) return "";
|
|
5032
5102
|
if (config.models.length === 0) return "";
|
|
5033
5103
|
const defaults = `size ${config.defaults.size}\uFF0Cquality ${config.defaults.quality}\uFF0Cn ${config.defaults.n}`;
|
|
5034
5104
|
const resolved = resolveImageModel(config);
|
|
@@ -5043,7 +5113,7 @@ function imageToolHint(config) {
|
|
|
5043
5113
|
}
|
|
5044
5114
|
|
|
5045
5115
|
// src/image/legacy-cleanup.ts
|
|
5046
|
-
import { copyFileSync, existsSync as existsSync3, mkdirSync as mkdirSync6, readFileSync as readFileSync7, readdirSync, statSync as statSync2, writeFileSync as writeFileSync6 } from "node:fs";
|
|
5116
|
+
import { copyFileSync, existsSync as existsSync3, mkdirSync as mkdirSync6, readFileSync as readFileSync7, readdirSync as readdirSync2, statSync as statSync2, writeFileSync as writeFileSync6 } from "node:fs";
|
|
5047
5117
|
import { dirname as dirname7, join as join7 } from "node:path";
|
|
5048
5118
|
var MARK_BEGIN = `# >>> ${IMAGE_MCP_PATCH_MARK}:begin`;
|
|
5049
5119
|
var MARK_END = `# <<< ${IMAGE_MCP_PATCH_MARK}:end`;
|
|
@@ -5054,7 +5124,7 @@ function resolveLegacyPatchFile(home = harnessHome()) {
|
|
|
5054
5124
|
try {
|
|
5055
5125
|
if (existsSync3(profilesDir)) {
|
|
5056
5126
|
const candidates = [];
|
|
5057
|
-
for (const name2 of
|
|
5127
|
+
for (const name2 of readdirSync2(profilesDir)) {
|
|
5058
5128
|
const file = join7(profilesDir, name2, "cordis.patch.yml");
|
|
5059
5129
|
if (!existsSync3(file)) continue;
|
|
5060
5130
|
candidates.push({ file, mtime: statSync2(file).mtimeMs });
|
|
@@ -6112,6 +6182,7 @@ var name = PLUGIN_ID;
|
|
|
6112
6182
|
var inject = ["settings", "llm"];
|
|
6113
6183
|
var PI_NS = PI_AI_NS;
|
|
6114
6184
|
var BOOT_RETRY_DELAYS_MS = [1e3, 2e3, 4e3, 8e3, 16e3, 3e4];
|
|
6185
|
+
var ROUTE_SWEEP_GRACE_MS = 3e4;
|
|
6115
6186
|
var PROBE_TIMEOUT_MS = 15e3;
|
|
6116
6187
|
var BALANCE_TIMEOUT_MS = 8e3;
|
|
6117
6188
|
var CREDENTIAL_IPC_BUDGET_MS = 1e4;
|
|
@@ -6161,6 +6232,7 @@ var Config = Schema.object({
|
|
|
6161
6232
|
modalityAutofill: Schema.boolean().default(true),
|
|
6162
6233
|
probeTimeoutMs: Schema.natural().min(1).default(PROBE_TIMEOUT_MS),
|
|
6163
6234
|
bootRetryDelaysMs: Schema.array(Schema.natural().min(1)).default([...BOOT_RETRY_DELAYS_MS]),
|
|
6235
|
+
routeSweepGraceMs: Schema.natural().default(ROUTE_SWEEP_GRACE_MS),
|
|
6164
6236
|
defaultGuard: Schema.boolean().default(true),
|
|
6165
6237
|
uaOverride: Schema.boolean().default(true)
|
|
6166
6238
|
});
|
|
@@ -6168,7 +6240,7 @@ function providerProfileOf(section, route) {
|
|
|
6168
6240
|
if (!isRecord(section)) return void 0;
|
|
6169
6241
|
const providers = section["providers"];
|
|
6170
6242
|
if (!isRecord(providers)) return void 0;
|
|
6171
|
-
const profile = providers[route];
|
|
6243
|
+
const profile = Object.hasOwn(providers, route) ? providers[route] : void 0;
|
|
6172
6244
|
return isRecord(profile) ? profile : void 0;
|
|
6173
6245
|
}
|
|
6174
6246
|
function enabledRefOf(profile) {
|
|
@@ -6176,17 +6248,17 @@ function enabledRefOf(profile) {
|
|
|
6176
6248
|
return typeof ref === "string" && ref.length > 0 ? ref : void 0;
|
|
6177
6249
|
}
|
|
6178
6250
|
function readRouteKeyScopes(providers) {
|
|
6251
|
+
const scopes = /* @__PURE__ */ Object.create(null);
|
|
6179
6252
|
let document;
|
|
6180
6253
|
try {
|
|
6181
6254
|
document = readKeyIndex(keyIndexFilePath());
|
|
6182
6255
|
} catch {
|
|
6183
|
-
return
|
|
6256
|
+
return scopes;
|
|
6184
6257
|
}
|
|
6185
|
-
const scopes = {};
|
|
6186
6258
|
const routes = new Set(Object.keys(document.providers));
|
|
6187
6259
|
for (const route of Object.keys(providers)) routes.add(route);
|
|
6188
6260
|
for (const route of routes) {
|
|
6189
|
-
const endpoints = document
|
|
6261
|
+
const endpoints = routeEndpointsOf(document, route);
|
|
6190
6262
|
const listed = keyEntriesOf(document, route);
|
|
6191
6263
|
const active = listed.filter((entry) => entry.disabled !== true);
|
|
6192
6264
|
const ordered = active.map((entry) => {
|
|
@@ -6228,14 +6300,52 @@ function describeRecords(answer) {
|
|
|
6228
6300
|
const candidate = isRecord(answer) && answer["value"] !== void 0 ? answer["value"] : answer;
|
|
6229
6301
|
return isRecord(candidate) ? candidate : void 0;
|
|
6230
6302
|
}
|
|
6231
|
-
|
|
6303
|
+
var PendingCredentialMutations = class {
|
|
6304
|
+
pending = /* @__PURE__ */ new Set();
|
|
6305
|
+
track(operation) {
|
|
6306
|
+
const settled = operation.then(
|
|
6307
|
+
() => {
|
|
6308
|
+
this.pending.delete(settled);
|
|
6309
|
+
},
|
|
6310
|
+
() => {
|
|
6311
|
+
this.pending.delete(settled);
|
|
6312
|
+
}
|
|
6313
|
+
);
|
|
6314
|
+
this.pending.add(settled);
|
|
6315
|
+
return operation;
|
|
6316
|
+
}
|
|
6317
|
+
releaseAfterSettled(release) {
|
|
6318
|
+
if (this.pending.size === 0) release();
|
|
6319
|
+
else void Promise.all([...this.pending]).then(release, release);
|
|
6320
|
+
}
|
|
6321
|
+
};
|
|
6322
|
+
async function awaitCredentialMutation(operation, pending, budgetMs) {
|
|
6323
|
+
pending.track(operation);
|
|
6324
|
+
let timer;
|
|
6325
|
+
try {
|
|
6326
|
+
return await Promise.race([
|
|
6327
|
+
operation,
|
|
6328
|
+
new Promise((_, reject) => {
|
|
6329
|
+
timer = setTimeout(() => reject(new Error(
|
|
6330
|
+
`the credential store did not answer within ${Math.round(budgetMs / 1e3)}s; the write result is unknown until it settles`
|
|
6331
|
+
)), budgetMs);
|
|
6332
|
+
})
|
|
6333
|
+
]);
|
|
6334
|
+
} finally {
|
|
6335
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
6336
|
+
}
|
|
6337
|
+
}
|
|
6338
|
+
async function storeCredential(credentials, ref, value, pending) {
|
|
6232
6339
|
const set = credentials?.set;
|
|
6233
6340
|
if (credentials === void 0 || typeof set !== "function") {
|
|
6234
6341
|
return "this deployment cannot write the credential store";
|
|
6235
6342
|
}
|
|
6236
6343
|
invalidateCredentialDisplay(credentials, ref);
|
|
6237
6344
|
try {
|
|
6238
|
-
const
|
|
6345
|
+
const operation = Promise.resolve().then(() => set.call(credentials, ref, value)).finally(() => {
|
|
6346
|
+
invalidateCredentialDisplay(credentials, ref);
|
|
6347
|
+
});
|
|
6348
|
+
const answer = await awaitCredentialMutation(operation, pending, CREDENTIAL_IPC_BUDGET_MS);
|
|
6239
6349
|
if (!isRefusal(answer)) return void 0;
|
|
6240
6350
|
return refusalMessage(answer) ?? `the credential store refused ${ref}`;
|
|
6241
6351
|
} catch (error) {
|
|
@@ -6244,30 +6354,22 @@ async function storeCredential(credentials, ref, value) {
|
|
|
6244
6354
|
invalidateCredentialDisplay(credentials, ref);
|
|
6245
6355
|
}
|
|
6246
6356
|
}
|
|
6247
|
-
async function forgetCredential(credentials, ref, budgetMs) {
|
|
6357
|
+
async function forgetCredential(credentials, ref, pending, budgetMs = CREDENTIAL_IPC_BUDGET_MS) {
|
|
6248
6358
|
const unset = credentials?.unset;
|
|
6249
6359
|
if (credentials === void 0 || typeof unset !== "function") {
|
|
6250
6360
|
return "this deployment cannot write the credential store";
|
|
6251
6361
|
}
|
|
6252
6362
|
invalidateCredentialDisplay(credentials, ref);
|
|
6253
|
-
let timer;
|
|
6254
6363
|
try {
|
|
6255
|
-
const
|
|
6256
|
-
|
|
6257
|
-
|
|
6258
|
-
|
|
6259
|
-
timer = setTimeout(
|
|
6260
|
-
() => reject(new Error(`the credential store did not answer within ${Math.round(budgetMs / 1e3)}s`)),
|
|
6261
|
-
budgetMs
|
|
6262
|
-
);
|
|
6263
|
-
})
|
|
6264
|
-
]);
|
|
6364
|
+
const operation = Promise.resolve().then(() => unset.call(credentials, ref)).finally(() => {
|
|
6365
|
+
invalidateCredentialDisplay(credentials, ref);
|
|
6366
|
+
});
|
|
6367
|
+
const answer = await awaitCredentialMutation(operation, pending, budgetMs);
|
|
6265
6368
|
if (!isRefusal(answer)) return void 0;
|
|
6266
6369
|
return refusalMessage(answer) ?? `the credential store refused to forget ${ref}`;
|
|
6267
6370
|
} catch (error) {
|
|
6268
6371
|
return error instanceof Error ? error.message : String(error);
|
|
6269
6372
|
} finally {
|
|
6270
|
-
if (timer !== void 0) clearTimeout(timer);
|
|
6271
6373
|
invalidateCredentialDisplay(credentials, ref);
|
|
6272
6374
|
}
|
|
6273
6375
|
}
|
|
@@ -6361,8 +6463,8 @@ async function displayCredentialFacts(credentials, refs, budgetMs) {
|
|
|
6361
6463
|
try {
|
|
6362
6464
|
await Promise.race([
|
|
6363
6465
|
Promise.all([...selected.values()].map((read) => read.settled)),
|
|
6364
|
-
new Promise((
|
|
6365
|
-
timer = setTimeout(
|
|
6466
|
+
new Promise((resolve4) => {
|
|
6467
|
+
timer = setTimeout(resolve4, budgetMs);
|
|
6366
6468
|
})
|
|
6367
6469
|
]);
|
|
6368
6470
|
} finally {
|
|
@@ -6406,8 +6508,8 @@ async function credentialFacts(credentials, refs, budgetMs) {
|
|
|
6406
6508
|
let timer;
|
|
6407
6509
|
try {
|
|
6408
6510
|
if (budgetMs === void 0) await settled;
|
|
6409
|
-
else await Promise.race([settled, new Promise((
|
|
6410
|
-
timer = setTimeout(
|
|
6511
|
+
else await Promise.race([settled, new Promise((resolve4) => {
|
|
6512
|
+
timer = setTimeout(resolve4, budgetMs);
|
|
6411
6513
|
})]);
|
|
6412
6514
|
} finally {
|
|
6413
6515
|
if (timer !== void 0) clearTimeout(timer);
|
|
@@ -6716,61 +6818,14 @@ function apply(ctx, config = {}) {
|
|
|
6716
6818
|
if (key.startsWith(prefix)) listingCache.delete(key);
|
|
6717
6819
|
}
|
|
6718
6820
|
};
|
|
6719
|
-
const sweepVanishedRoutes = () => {
|
|
6720
|
-
if (piSection() === void 0) return false;
|
|
6721
|
-
const live = providersOf();
|
|
6722
|
-
const indexFile = keyIndexFilePath();
|
|
6723
|
-
let index;
|
|
6724
|
-
try {
|
|
6725
|
-
index = readKeyIndex(indexFile);
|
|
6726
|
-
} catch {
|
|
6727
|
-
return true;
|
|
6728
|
-
}
|
|
6729
|
-
const stale = /* @__PURE__ */ new Set();
|
|
6730
|
-
for (const route of Object.keys(index.providers)) if (!(route in live)) stale.add(route);
|
|
6731
|
-
for (const route of Object.keys(index.endpoints ?? {})) if (!(route in live)) stale.add(route);
|
|
6732
|
-
for (const route of Object.keys(index.balances ?? {})) if (!(route in live)) stale.add(route);
|
|
6733
|
-
if (stale.size === 0) return true;
|
|
6734
|
-
const survivors = /* @__PURE__ */ new Set();
|
|
6735
|
-
for (const [route, entries] of Object.entries(index.providers)) {
|
|
6736
|
-
if (stale.has(route)) continue;
|
|
6737
|
-
for (const entry of entries) survivors.add(entry.ref);
|
|
6738
|
-
}
|
|
6739
|
-
for (const profile of Object.values(live)) {
|
|
6740
|
-
if (!isRecord(profile)) continue;
|
|
6741
|
-
const seat = enabledRefOf(profile);
|
|
6742
|
-
if (seat !== void 0) survivors.add(seat);
|
|
6743
|
-
}
|
|
6744
|
-
const credentials = ctx.get("credentials");
|
|
6745
|
-
let swept = false;
|
|
6746
|
-
for (const route of stale) {
|
|
6747
|
-
const refs = keyEntriesOf(index, route).map((entry) => entry.ref);
|
|
6748
|
-
try {
|
|
6749
|
-
updateKeyIndex((current) => withoutRoute(current, route), indexFile);
|
|
6750
|
-
} catch (error) {
|
|
6751
|
-
console.error(`[dsh-model-think-level] key route cleanup for "${route}" failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
6752
|
-
continue;
|
|
6753
|
-
}
|
|
6754
|
-
swept = true;
|
|
6755
|
-
for (const ref of refs) {
|
|
6756
|
-
forgetServes(route, ref);
|
|
6757
|
-
const here = `${route}\0${ref}`;
|
|
6758
|
-
lastKnownScopes.delete(here);
|
|
6759
|
-
lastKnownRecords.delete(here);
|
|
6760
|
-
if (survivors.has(ref)) continue;
|
|
6761
|
-
void forgetCredential(credentials, ref, CREDENTIAL_IPC_BUDGET_MS).then((refused) => {
|
|
6762
|
-
if (refused !== void 0) {
|
|
6763
|
-
console.error(`[dsh-model-think-level] credential cleanup for "${ref}" failed: ${refused}`);
|
|
6764
|
-
}
|
|
6765
|
-
});
|
|
6766
|
-
}
|
|
6767
|
-
invalidateListings(route);
|
|
6768
|
-
}
|
|
6769
|
-
if (swept) refreshKeyScopes();
|
|
6770
|
-
return true;
|
|
6771
|
-
};
|
|
6772
6821
|
const keyProbeRuns = /* @__PURE__ */ new Map();
|
|
6822
|
+
let probeStopped = false;
|
|
6823
|
+
ctx.effect(() => () => {
|
|
6824
|
+
probeStopped = true;
|
|
6825
|
+
});
|
|
6773
6826
|
const probeRouteKeys = async (route) => {
|
|
6827
|
+
if (probeStopped) return;
|
|
6828
|
+
const indexFile = keyIndexFilePath();
|
|
6774
6829
|
let profile;
|
|
6775
6830
|
try {
|
|
6776
6831
|
profile = providerProfileOf(piSection(true), route);
|
|
@@ -6783,11 +6838,11 @@ function apply(ctx, config = {}) {
|
|
|
6783
6838
|
if (api === void 0 || !LISTABLE_PROTOCOLS.has(api) || baseURL.length === 0) return;
|
|
6784
6839
|
let document;
|
|
6785
6840
|
try {
|
|
6786
|
-
document = readKeyIndex(
|
|
6841
|
+
document = readKeyIndex(indexFile);
|
|
6787
6842
|
} catch {
|
|
6788
6843
|
return;
|
|
6789
6844
|
}
|
|
6790
|
-
const endpoints = document
|
|
6845
|
+
const endpoints = routeEndpointsOf(document, route);
|
|
6791
6846
|
const listed = keyEntriesOf(document, route);
|
|
6792
6847
|
const seat = enabledRefOf(profile);
|
|
6793
6848
|
const keys = listed.map((entry) => ({ ref: entry.ref, entry }));
|
|
@@ -6801,6 +6856,7 @@ function apply(ctx, config = {}) {
|
|
|
6801
6856
|
return void 0;
|
|
6802
6857
|
}
|
|
6803
6858
|
}));
|
|
6859
|
+
if (probeStopped) return;
|
|
6804
6860
|
const askable = keys.flatMap((key, index) => {
|
|
6805
6861
|
const value = values[index];
|
|
6806
6862
|
return value === void 0 || value.length === 0 ? [] : [{ ...key, value }];
|
|
@@ -6814,6 +6870,7 @@ function apply(ctx, config = {}) {
|
|
|
6814
6870
|
keys: askable,
|
|
6815
6871
|
refOf: (key) => key.ref,
|
|
6816
6872
|
probe: async (key) => {
|
|
6873
|
+
if (probeStopped) return { ref: key.ref, models: void 0 };
|
|
6817
6874
|
const listings = await Promise.all(targetsOf(key.entry).map((target) => probeListing({
|
|
6818
6875
|
baseURL: target.baseURL,
|
|
6819
6876
|
api: target.api,
|
|
@@ -6822,6 +6879,7 @@ function apply(ctx, config = {}) {
|
|
|
6822
6879
|
timeoutMs,
|
|
6823
6880
|
send
|
|
6824
6881
|
})));
|
|
6882
|
+
if (probeStopped) return { ref: key.ref, models: void 0 };
|
|
6825
6883
|
const ids = [];
|
|
6826
6884
|
for (const listing of listings) {
|
|
6827
6885
|
if (listing === void 0) continue;
|
|
@@ -6831,38 +6889,47 @@ function apply(ctx, config = {}) {
|
|
|
6831
6889
|
}
|
|
6832
6890
|
if (ids.length === 0) return { ref: key.ref, models: void 0 };
|
|
6833
6891
|
served.set(key.ref, ids);
|
|
6834
|
-
lastKnownScopes.set(`${route}\0${key.ref}`, ids);
|
|
6835
|
-
recordServes(route, key.ref, ids);
|
|
6836
6892
|
return { ref: key.ref, models: ids };
|
|
6837
6893
|
}
|
|
6838
6894
|
});
|
|
6839
|
-
if (served.size === 0) return;
|
|
6840
|
-
const
|
|
6841
|
-
if (declared.length === 0) return;
|
|
6842
|
-
let next = readKeyIndex(keyIndexFilePath());
|
|
6843
|
-
let mutated = false;
|
|
6844
|
-
for (const [ref, ids] of served) {
|
|
6845
|
-
const entry = keyEntriesOf(next, route).find((candidate) => candidate.ref === ref);
|
|
6846
|
-
if (entry === void 0) continue;
|
|
6847
|
-
if (entry.models !== void 0 && entry.auto !== true) continue;
|
|
6848
|
-
const target = declared.filter((id) => ids.includes(id));
|
|
6849
|
-
const scope = target.length === declared.length ? void 0 : target;
|
|
6850
|
-
const unchanged = scope === void 0 ? entry.models === void 0 : entry.models !== void 0 && entry.models.length === scope.length && scope.every((id) => entry.models?.includes(id));
|
|
6851
|
-
if (unchanged) continue;
|
|
6852
|
-
next = withKeyAutoScope(next, route, ref, scope);
|
|
6853
|
-
mutated = true;
|
|
6854
|
-
}
|
|
6855
|
-
if (!mutated) return;
|
|
6895
|
+
if (probeStopped || served.size === 0) return;
|
|
6896
|
+
const releaseMutation = await acquireKeyMutation(indexFile);
|
|
6856
6897
|
try {
|
|
6857
|
-
|
|
6858
|
-
|
|
6859
|
-
return;
|
|
6898
|
+
if (probeStopped) return;
|
|
6899
|
+
const currentProfile = providerProfileOf(settings.describe().find((entry) => entry.ns === PI_NS)?.value, route);
|
|
6900
|
+
if (currentProfile === void 0) return;
|
|
6901
|
+
const declared = Array.isArray(currentProfile["models"]) ? currentProfile["models"].filter(isRecord).map((model) => typeof model["id"] === "string" ? model["id"].trim() : "").filter(Boolean) : [];
|
|
6902
|
+
if (declared.length === 0) return;
|
|
6903
|
+
let next = readKeyIndex(indexFile);
|
|
6904
|
+
let mutated = false;
|
|
6905
|
+
for (const [ref, ids] of served) {
|
|
6906
|
+
lastKnownScopes.set(`${route}\0${ref}`, ids);
|
|
6907
|
+
recordServes(route, ref, ids);
|
|
6908
|
+
const entry = keyEntriesOf(next, route).find((candidate) => candidate.ref === ref);
|
|
6909
|
+
if (entry === void 0) continue;
|
|
6910
|
+
if (entry.models !== void 0 && entry.auto !== true) continue;
|
|
6911
|
+
const target = declared.filter((id) => ids.includes(id));
|
|
6912
|
+
const scope = target.length === declared.length ? void 0 : target;
|
|
6913
|
+
const unchanged = scope === void 0 ? entry.models === void 0 : entry.models !== void 0 && entry.models.length === scope.length && scope.every((id) => entry.models?.includes(id));
|
|
6914
|
+
if (unchanged) continue;
|
|
6915
|
+
next = withKeyAutoScope(next, route, ref, scope);
|
|
6916
|
+
mutated = true;
|
|
6917
|
+
}
|
|
6918
|
+
if (!mutated) return;
|
|
6919
|
+
try {
|
|
6920
|
+
writeKeyIndex(next, indexFile);
|
|
6921
|
+
} catch {
|
|
6922
|
+
return;
|
|
6923
|
+
}
|
|
6924
|
+
refreshKeyScopes();
|
|
6925
|
+
} finally {
|
|
6926
|
+
releaseMutation();
|
|
6860
6927
|
}
|
|
6861
|
-
refreshKeyScopes();
|
|
6862
6928
|
};
|
|
6863
6929
|
const runKeyProbe = (route) => {
|
|
6930
|
+
if (probeStopped) return Promise.resolve();
|
|
6864
6931
|
const previous = keyProbeRuns.get(route) ?? Promise.resolve();
|
|
6865
|
-
const run = previous.then(() => probeRouteKeys(route)).catch(() => {
|
|
6932
|
+
const run = previous.then(() => probeStopped ? void 0 : probeRouteKeys(route)).catch(() => {
|
|
6866
6933
|
}).finally(() => {
|
|
6867
6934
|
if (keyProbeRuns.get(route) === run) keyProbeRuns.delete(route);
|
|
6868
6935
|
});
|
|
@@ -6909,7 +6976,6 @@ function apply(ctx, config = {}) {
|
|
|
6909
6976
|
if (ns === PI_NS) {
|
|
6910
6977
|
refreshHeaders();
|
|
6911
6978
|
refreshKeyScopes();
|
|
6912
|
-
sweepVanishedRoutes();
|
|
6913
6979
|
syncImageConfigNow();
|
|
6914
6980
|
const source = catalogueSource();
|
|
6915
6981
|
if (source !== catalogueSourceStamp) {
|
|
@@ -6918,26 +6984,6 @@ function apply(ctx, config = {}) {
|
|
|
6918
6984
|
}
|
|
6919
6985
|
}
|
|
6920
6986
|
});
|
|
6921
|
-
{
|
|
6922
|
-
const sweepTimers = /* @__PURE__ */ new Set();
|
|
6923
|
-
ctx.effect(() => () => {
|
|
6924
|
-
for (const timer of sweepTimers) clearTimeout(timer);
|
|
6925
|
-
sweepTimers.clear();
|
|
6926
|
-
}, "dsh-model-think-level: route-sweep boot retries");
|
|
6927
|
-
const bootRouteSweep = (attempt) => {
|
|
6928
|
-
void Promise.resolve().then(() => sweepVanishedRoutes()).then((ready) => {
|
|
6929
|
-
if (ready || attempt >= resolved.bootRetryDelaysMs.length) return;
|
|
6930
|
-
const timer = setTimeout(() => {
|
|
6931
|
-
sweepTimers.delete(timer);
|
|
6932
|
-
bootRouteSweep(attempt + 1);
|
|
6933
|
-
}, resolved.bootRetryDelaysMs[attempt]);
|
|
6934
|
-
sweepTimers.add(timer);
|
|
6935
|
-
}, (error) => {
|
|
6936
|
-
console.error(`[dsh-model-think-level] route sweep failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
6937
|
-
});
|
|
6938
|
-
};
|
|
6939
|
-
bootRouteSweep(0);
|
|
6940
|
-
}
|
|
6941
6987
|
overlaySource.keys = async ({ route, model }) => {
|
|
6942
6988
|
const scopes = keyScopes.current[route] ?? [];
|
|
6943
6989
|
if (scopes.length === 0) return [];
|
|
@@ -7112,28 +7158,46 @@ function apply(ctx, config = {}) {
|
|
|
7112
7158
|
}
|
|
7113
7159
|
);
|
|
7114
7160
|
const imageToolState = { name: void 0, error: void 0 };
|
|
7161
|
+
let reconcileImageTool;
|
|
7115
7162
|
ctx.inject(["tools", "attachments"], (toolCtx) => {
|
|
7163
|
+
let registered;
|
|
7164
|
+
const release = () => {
|
|
7165
|
+
const current = registered;
|
|
7166
|
+
registered = void 0;
|
|
7167
|
+
imageToolState.name = void 0;
|
|
7168
|
+
if (current === void 0) return;
|
|
7169
|
+
try {
|
|
7170
|
+
current.dispose();
|
|
7171
|
+
} catch {
|
|
7172
|
+
}
|
|
7173
|
+
};
|
|
7174
|
+
const acquire = () => {
|
|
7175
|
+
if (registered !== void 0) return;
|
|
7176
|
+
try {
|
|
7177
|
+
registered = registerGenerateImageTool(toolCtx, {
|
|
7178
|
+
generateDeps: imageGenerateDeps,
|
|
7179
|
+
attachments: toolCtx.attachments,
|
|
7180
|
+
readConfig: () => readImageConfig()
|
|
7181
|
+
});
|
|
7182
|
+
imageToolState.name = registered.name;
|
|
7183
|
+
imageToolState.error = void 0;
|
|
7184
|
+
} catch (error) {
|
|
7185
|
+
imageToolState.name = void 0;
|
|
7186
|
+
imageToolState.error = error instanceof Error ? error.message : String(error);
|
|
7187
|
+
console.warn(`[dsh-model-think-level] \u5185\u5EFA\u751F\u56FE\u5DE5\u5177\u6CE8\u518C\u5931\u8D25\uFF08\u8BCA\u65AD\u8DEF\u7531 ${IMAGE_CONFIG_PATH} \u7684 tool.error \u4F1A\u5E26\u56DE\u8FD9\u6761\u539F\u56E0\uFF09\uFF1A${imageToolState.error}`);
|
|
7188
|
+
}
|
|
7189
|
+
};
|
|
7190
|
+
const reconcile = () => {
|
|
7191
|
+
if (readImageConfig().enabled) acquire();
|
|
7192
|
+
else release();
|
|
7193
|
+
};
|
|
7194
|
+
reconcileImageTool = reconcile;
|
|
7116
7195
|
toolCtx.effect(
|
|
7117
7196
|
() => {
|
|
7118
|
-
|
|
7119
|
-
try {
|
|
7120
|
-
registered = registerGenerateImageTool(toolCtx, {
|
|
7121
|
-
generateDeps: imageGenerateDeps,
|
|
7122
|
-
attachments: toolCtx.attachments,
|
|
7123
|
-
readConfig: () => readImageConfig()
|
|
7124
|
-
});
|
|
7125
|
-
} catch (error) {
|
|
7126
|
-
imageToolState.name = void 0;
|
|
7127
|
-
imageToolState.error = error instanceof Error ? error.message : String(error);
|
|
7128
|
-
console.warn(`[dsh-model-think-level] \u5185\u5EFA\u751F\u56FE\u5DE5\u5177\u6CE8\u518C\u5931\u8D25\uFF08\u8BCA\u65AD\u8DEF\u7531 ${IMAGE_CONFIG_PATH} \u7684 tool.error \u4F1A\u5E26\u56DE\u8FD9\u6761\u539F\u56E0\uFF09\uFF1A${imageToolState.error}`);
|
|
7129
|
-
throw error;
|
|
7130
|
-
}
|
|
7131
|
-
const { name: name2, dispose } = registered;
|
|
7132
|
-
imageToolState.name = name2;
|
|
7133
|
-
imageToolState.error = void 0;
|
|
7197
|
+
reconcile();
|
|
7134
7198
|
return () => {
|
|
7135
|
-
|
|
7136
|
-
|
|
7199
|
+
reconcileImageTool = void 0;
|
|
7200
|
+
release();
|
|
7137
7201
|
};
|
|
7138
7202
|
},
|
|
7139
7203
|
"dsh-model-think-level: generate_image tool"
|
|
@@ -7396,13 +7460,13 @@ function apply(ctx, config = {}) {
|
|
|
7396
7460
|
}),
|
|
7397
7461
|
"dsh-model-think-level: provider-key route"
|
|
7398
7462
|
);
|
|
7399
|
-
let keyMutationTail = Promise.resolve();
|
|
7400
7463
|
ctx.effect(
|
|
7401
7464
|
() => webServerCtx.webServer.register({
|
|
7402
7465
|
kind: "exact",
|
|
7403
7466
|
path: KEY_INDEX_PATH,
|
|
7404
7467
|
handler: async (req, res) => {
|
|
7405
7468
|
res.setHeader("cache-control", "no-store");
|
|
7469
|
+
res.setHeader("x-dsh-key-integrity", "20261101-explicit-delete+history64+shared-lease-drain");
|
|
7406
7470
|
if (!isTrustedRequest(req)) {
|
|
7407
7471
|
sendJson(res, 403, { ok: false, error: "forbidden" });
|
|
7408
7472
|
return;
|
|
@@ -7413,21 +7477,25 @@ function apply(ctx, config = {}) {
|
|
|
7413
7477
|
}
|
|
7414
7478
|
const url = new URL(req.url ?? "/", "http://x");
|
|
7415
7479
|
const route = url.searchParams.get("route") ?? "";
|
|
7480
|
+
const indexFile = keyIndexFilePath();
|
|
7481
|
+
const pendingMutations = new PendingCredentialMutations();
|
|
7416
7482
|
let releaseMutation;
|
|
7417
7483
|
try {
|
|
7418
7484
|
if (req.method === "POST") {
|
|
7419
|
-
const
|
|
7420
|
-
|
|
7421
|
-
|
|
7422
|
-
|
|
7423
|
-
|
|
7485
|
+
const release = await acquireKeyMutation(indexFile);
|
|
7486
|
+
let finished = false;
|
|
7487
|
+
releaseMutation = () => {
|
|
7488
|
+
if (finished) return;
|
|
7489
|
+
finished = true;
|
|
7490
|
+
pendingMutations.releaseAfterSettled(release);
|
|
7491
|
+
};
|
|
7424
7492
|
}
|
|
7425
7493
|
if (req.method === "GET" && url.searchParams.get("op") === "endpoints") {
|
|
7426
7494
|
if (route.length === 0) {
|
|
7427
7495
|
sendJson(res, 400, { ok: false, error: "a route is required" });
|
|
7428
7496
|
return;
|
|
7429
7497
|
}
|
|
7430
|
-
const endpoints = readKeyIndex(
|
|
7498
|
+
const endpoints = routeEndpointsOf(readKeyIndex(indexFile), route);
|
|
7431
7499
|
sendJson(res, 200, {
|
|
7432
7500
|
ok: true,
|
|
7433
7501
|
route,
|
|
@@ -7443,7 +7511,7 @@ function apply(ctx, config = {}) {
|
|
|
7443
7511
|
sendJson(res, 400, { ok: false, error: "a route is required" });
|
|
7444
7512
|
return;
|
|
7445
7513
|
}
|
|
7446
|
-
const endpoint = readKeyIndex(
|
|
7514
|
+
const endpoint = routeBalanceOf(readKeyIndex(indexFile), route);
|
|
7447
7515
|
sendJson(res, 200, { ok: true, route, endpoint: endpoint ?? null });
|
|
7448
7516
|
return;
|
|
7449
7517
|
}
|
|
@@ -7464,7 +7532,6 @@ function apply(ctx, config = {}) {
|
|
|
7464
7532
|
return;
|
|
7465
7533
|
}
|
|
7466
7534
|
const credentials = ctx.get("credentials");
|
|
7467
|
-
const indexFile = keyIndexFilePath();
|
|
7468
7535
|
const enabledNow = () => enabledRefOf(providerProfileOf(entryOf()?.value, route));
|
|
7469
7536
|
const setEnabledKey = async (ref2) => {
|
|
7470
7537
|
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
@@ -7526,7 +7593,7 @@ function apply(ctx, config = {}) {
|
|
|
7526
7593
|
}
|
|
7527
7594
|
return view;
|
|
7528
7595
|
});
|
|
7529
|
-
const endpoints = document
|
|
7596
|
+
const endpoints = routeEndpointsOf(document, route);
|
|
7530
7597
|
const probing = keyProbeRuns.has(route);
|
|
7531
7598
|
return {
|
|
7532
7599
|
ok: true,
|
|
@@ -7615,7 +7682,7 @@ function apply(ctx, config = {}) {
|
|
|
7615
7682
|
}
|
|
7616
7683
|
const seatBefore = active;
|
|
7617
7684
|
const seatIsEmpty = seatBefore !== void 0 && seatBefore !== created && holdsNothing((await credentialFacts(credentials, [seatBefore], CREDENTIAL_IPC_BUDGET_MS))[seatBefore]);
|
|
7618
|
-
const refused = await storeCredential(credentials, created, value);
|
|
7685
|
+
const refused = await storeCredential(credentials, created, value, pendingMutations);
|
|
7619
7686
|
if (refused !== void 0) {
|
|
7620
7687
|
refuse(refused);
|
|
7621
7688
|
return;
|
|
@@ -7629,7 +7696,7 @@ function apply(ctx, config = {}) {
|
|
|
7629
7696
|
if (failed !== void 0) throw new Error(failed);
|
|
7630
7697
|
}
|
|
7631
7698
|
} catch (error) {
|
|
7632
|
-
const rollback = await forgetCredential(credentials, created, CREDENTIAL_IPC_BUDGET_MS);
|
|
7699
|
+
const rollback = await forgetCredential(credentials, created, pendingMutations, CREDENTIAL_IPC_BUDGET_MS);
|
|
7633
7700
|
if (rollback === void 0 && indexed) {
|
|
7634
7701
|
updateKeyIndex((document) => withoutKeyEntry(document, route, created), indexFile);
|
|
7635
7702
|
}
|
|
@@ -7751,7 +7818,7 @@ function apply(ctx, config = {}) {
|
|
|
7751
7818
|
refuse("a key must be printable ASCII without spaces");
|
|
7752
7819
|
return;
|
|
7753
7820
|
}
|
|
7754
|
-
const refused = await storeCredential(credentials, ref, value);
|
|
7821
|
+
const refused = await storeCredential(credentials, ref, value, pendingMutations);
|
|
7755
7822
|
if (refused !== void 0) {
|
|
7756
7823
|
refuse(refused);
|
|
7757
7824
|
return;
|
|
@@ -7781,17 +7848,17 @@ function apply(ctx, config = {}) {
|
|
|
7781
7848
|
forgetServes(route, ref);
|
|
7782
7849
|
invalidateCredentialDisplay(credentials, ref);
|
|
7783
7850
|
if (configured) {
|
|
7784
|
-
const refused = await forgetCredential(credentials, ref, CREDENTIAL_IPC_BUDGET_MS);
|
|
7851
|
+
const refused = await forgetCredential(credentials, ref, pendingMutations, CREDENTIAL_IPC_BUDGET_MS);
|
|
7785
7852
|
if (refused !== void 0) throw new Error(refused);
|
|
7786
7853
|
}
|
|
7787
7854
|
} catch (error) {
|
|
7788
7855
|
const failures = [];
|
|
7789
7856
|
try {
|
|
7790
7857
|
updateKeyIndex((current) => {
|
|
7858
|
+
const original = Object.hasOwn(document.providers, route) ? document.providers[route] : void 0;
|
|
7859
|
+
if (original !== void 0) return { ...current, providers: { ...current.providers, [route]: original } };
|
|
7791
7860
|
const providers = { ...current.providers };
|
|
7792
|
-
|
|
7793
|
-
if (original === void 0) delete providers[route];
|
|
7794
|
-
else providers[route] = original;
|
|
7861
|
+
delete providers[route];
|
|
7795
7862
|
return { ...current, providers };
|
|
7796
7863
|
}, indexFile);
|
|
7797
7864
|
} catch {
|
|
@@ -7864,7 +7931,7 @@ function apply(ctx, config = {}) {
|
|
|
7864
7931
|
sendJson(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) });
|
|
7865
7932
|
return;
|
|
7866
7933
|
}
|
|
7867
|
-
const endpoint = document
|
|
7934
|
+
const endpoint = routeBalanceOf(document, route);
|
|
7868
7935
|
if (endpoint === void 0) {
|
|
7869
7936
|
sendJson(res, 200, { ok: true, route, configured: false });
|
|
7870
7937
|
return;
|
|
@@ -8113,8 +8180,8 @@ function apply(ctx, config = {}) {
|
|
|
8113
8180
|
}
|
|
8114
8181
|
let releaseMutation;
|
|
8115
8182
|
const previous = fingerprintMutationTail;
|
|
8116
|
-
fingerprintMutationTail = new Promise((
|
|
8117
|
-
releaseMutation =
|
|
8183
|
+
fingerprintMutationTail = new Promise((resolve4) => {
|
|
8184
|
+
releaseMutation = resolve4;
|
|
8118
8185
|
});
|
|
8119
8186
|
await previous;
|
|
8120
8187
|
try {
|
|
@@ -8284,6 +8351,7 @@ function apply(ctx, config = {}) {
|
|
|
8284
8351
|
const config2 = readImageConfig();
|
|
8285
8352
|
sendJson(res, 200, {
|
|
8286
8353
|
ok: true,
|
|
8354
|
+
enabled: config2.enabled,
|
|
8287
8355
|
models: config2.models,
|
|
8288
8356
|
defaultModel: config2.defaultModel,
|
|
8289
8357
|
defaults: config2.defaults,
|
|
@@ -8307,6 +8375,7 @@ function apply(ctx, config = {}) {
|
|
|
8307
8375
|
try {
|
|
8308
8376
|
updateImageConfig((document) => {
|
|
8309
8377
|
let next = document;
|
|
8378
|
+
if (typeof body["enabled"] === "boolean") next = { ...next, enabled: body["enabled"] };
|
|
8310
8379
|
if (typeof body["defaultModel"] === "string" && body["defaultModel"].length > 0) {
|
|
8311
8380
|
next = { ...next, defaultModel: body["defaultModel"] };
|
|
8312
8381
|
}
|
|
@@ -8329,6 +8398,10 @@ function apply(ctx, config = {}) {
|
|
|
8329
8398
|
return next;
|
|
8330
8399
|
});
|
|
8331
8400
|
syncImageConfigNow();
|
|
8401
|
+
try {
|
|
8402
|
+
reconcileImageTool?.();
|
|
8403
|
+
} catch {
|
|
8404
|
+
}
|
|
8332
8405
|
sendJson(res, 200, { ok: true });
|
|
8333
8406
|
} catch (error) {
|
|
8334
8407
|
sendJson(res, 500, { ok: false, error: error instanceof Error ? error.message : String(error) });
|