dsh-model-think-level 1.1.8 → 2.0.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/LICENSE +13 -0
- package/README.md +38 -0
- package/assets/NOTICE.md +15 -0
- package/assets/unified_bank.json +32910 -0
- package/lib/client/ComposerSlider.d.ts +9 -9
- package/lib/client/EffortEditor.d.ts +22 -12
- package/lib/client/FingerprintPane.d.ts +36 -0
- package/lib/client/KeyManager.d.ts +36 -10
- package/lib/client/ProtocolMenu.d.ts +51 -0
- package/lib/client/capacity.d.ts +30 -0
- package/lib/client/events.d.ts +26 -0
- package/lib/client/fingerprint-client.d.ts +81 -0
- package/lib/client/fingerprint-runner.d.ts +61 -0
- package/lib/client/index.d.ts +6 -0
- package/lib/client/injection/add-card.d.ts +4 -0
- package/lib/client/injection/create-card-pane.d.ts +15 -0
- package/lib/client/injection/create-key-seat.d.ts +53 -0
- package/lib/client/injection/create-keys-pane.d.ts +46 -0
- package/lib/client/injection/editor-tab-bar.d.ts +2 -1
- package/lib/client/injection/editor-tabs.d.ts +11 -1
- package/lib/client/injection/key-reveal.d.ts +24 -0
- package/lib/client/injection/models-page-editor.d.ts +165 -1
- package/lib/client/injection/provider-card-slot.d.ts +3 -1
- package/lib/client/injection/provider-endpoints.d.ts +70 -0
- package/lib/client/injection/staged-keys.d.ts +169 -0
- package/lib/client/key-manager-client.d.ts +89 -12
- package/lib/client/locales.d.ts +80 -6
- package/lib/client/ops.d.ts +4 -0
- package/lib/client/refresh-scheduler.d.ts +29 -0
- package/lib/client/styles.d.ts +1 -1
- package/lib/client/types.d.ts +44 -2
- package/lib/client.js +2275 -266
- package/lib/client.js.map +4 -4
- package/lib/constants.d.ts +11 -0
- package/lib/discovery-merge.d.ts +35 -0
- package/lib/fingerprint/bank.d.ts +24 -0
- package/lib/fingerprint/challenges.d.ts +14 -0
- package/lib/fingerprint/core.d.ts +85 -0
- package/lib/fingerprint/eligibility.d.ts +8 -0
- package/lib/fingerprint/random.d.ts +4 -0
- package/lib/fingerprint/run.d.ts +110 -0
- package/lib/fingerprint/store.d.ts +31 -0
- package/lib/fingerprint/verdict.d.ts +64 -0
- package/lib/headers-fetch.d.ts +53 -3
- package/lib/headers-pi-ai.d.ts +31 -1
- package/lib/index.d.ts +3 -19
- package/lib/index.js +2438 -153
- package/lib/index.js.map +4 -4
- package/lib/key-index.d.ts +58 -1
- package/lib/key-probe.d.ts +126 -0
- package/lib/key-refs.d.ts +106 -0
- package/lib/key-route-core.d.ts +415 -0
- package/lib/knowledge.d.ts +6 -5
- package/lib/listing.d.ts +118 -0
- package/lib/multi-key-listing.d.ts +101 -0
- package/package.json +13 -5
package/lib/index.js
CHANGED
|
@@ -9,12 +9,56 @@ var PROVIDER_KEY_PATH = "/dsh-model-think-level/provider-key";
|
|
|
9
9
|
var KEY_INDEX_PATH = "/dsh-model-think-level/key-index";
|
|
10
10
|
var AUTOFILL_CONFIG_PATH = "/dsh-model-think-level/autofill-config";
|
|
11
11
|
var HEADERS_CONFIG_PATH = "/dsh-model-think-level/headers-config";
|
|
12
|
+
var FINGERPRINT_PATH = "/dsh-model-think-level/fingerprint";
|
|
12
13
|
var PI_AI_NS = "llm-pi-ai";
|
|
13
14
|
var UNSET_MARKER = "reasoningEffortsUnset";
|
|
14
15
|
var INPUT_UNSET_MARKER = "inputUnset";
|
|
15
16
|
var AUTOFILL_MARKER = "reasoningEffortsAutofilled";
|
|
16
17
|
|
|
17
18
|
// src/key-refs.ts
|
|
19
|
+
var ENDPOINT_FAMILIES = ["openai", "anthropic", "gemini"];
|
|
20
|
+
function endpointFamilyOf(protocol) {
|
|
21
|
+
if (protocol === "anthropic-messages") return "anthropic";
|
|
22
|
+
if (protocol === "google-generative-ai") return "gemini";
|
|
23
|
+
return "openai";
|
|
24
|
+
}
|
|
25
|
+
function isRouteProtocol(value) {
|
|
26
|
+
return value === "openai-completions" || value === "anthropic-messages" || value === "openai-responses" || value === "google-generative-ai";
|
|
27
|
+
}
|
|
28
|
+
function normalizeKeyProtocol(value) {
|
|
29
|
+
return isRouteProtocol(value) ? value : void 0;
|
|
30
|
+
}
|
|
31
|
+
function normalizeKeyDisabled(value) {
|
|
32
|
+
return value === true ? true : void 0;
|
|
33
|
+
}
|
|
34
|
+
function isEndpointUrl(value) {
|
|
35
|
+
if (typeof value !== "string") return false;
|
|
36
|
+
const trimmed = value.trim();
|
|
37
|
+
if (trimmed.length === 0) return false;
|
|
38
|
+
return /^https?:\/\/\S+$/i.test(trimmed);
|
|
39
|
+
}
|
|
40
|
+
function normalizeRouteEndpoints(value) {
|
|
41
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
42
|
+
const record = value;
|
|
43
|
+
const out = {};
|
|
44
|
+
if (isEndpointUrl(record["openai"])) out.openai = record["openai"].trim();
|
|
45
|
+
if (isEndpointUrl(record["anthropic"])) out.anthropic = record["anthropic"].trim();
|
|
46
|
+
if (isEndpointUrl(record["gemini"])) out.gemini = record["gemini"].trim();
|
|
47
|
+
return out.openai === void 0 && out.anthropic === void 0 && out.gemini === void 0 ? void 0 : out;
|
|
48
|
+
}
|
|
49
|
+
function normalizeKeyModels(value) {
|
|
50
|
+
if (!Array.isArray(value)) return void 0;
|
|
51
|
+
const seen = /* @__PURE__ */ new Set();
|
|
52
|
+
const out = [];
|
|
53
|
+
for (const item of value) {
|
|
54
|
+
if (typeof item !== "string") continue;
|
|
55
|
+
const id = item.trim();
|
|
56
|
+
if (id.length === 0 || seen.has(id)) continue;
|
|
57
|
+
seen.add(id);
|
|
58
|
+
out.push(id);
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
18
62
|
var KEY_ALIAS_MAX_LENGTH = 40;
|
|
19
63
|
function deriveKeyRef(route) {
|
|
20
64
|
return `${route.toUpperCase().replace(/[^A-Z0-9]+/g, "_")}_API_KEY`;
|
|
@@ -55,6 +99,14 @@ function maskKeyValue(value) {
|
|
|
55
99
|
import { mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
56
100
|
import { homedir } from "node:os";
|
|
57
101
|
import { dirname, join } from "node:path";
|
|
102
|
+
function makeEntry(ref, alias, models, protocol, disabled) {
|
|
103
|
+
const entry = { ref };
|
|
104
|
+
if (alias !== void 0) entry.alias = alias;
|
|
105
|
+
if (models !== void 0) entry.models = models;
|
|
106
|
+
if (protocol !== void 0) entry.protocol = protocol;
|
|
107
|
+
if (disabled === true) entry.disabled = true;
|
|
108
|
+
return entry;
|
|
109
|
+
}
|
|
58
110
|
function emptyKeyIndex() {
|
|
59
111
|
return { version: 1, providers: {} };
|
|
60
112
|
}
|
|
@@ -72,7 +124,8 @@ function keyIndexFilePath(home = harnessHome()) {
|
|
|
72
124
|
}
|
|
73
125
|
function parseKeyIndex(raw) {
|
|
74
126
|
if (typeof raw !== "object" || raw === null) return emptyKeyIndex();
|
|
75
|
-
const
|
|
127
|
+
const root = raw;
|
|
128
|
+
const providers = root["providers"];
|
|
76
129
|
if (typeof providers !== "object" || providers === null) return emptyKeyIndex();
|
|
77
130
|
const out = {};
|
|
78
131
|
for (const [route, entries] of Object.entries(providers)) {
|
|
@@ -85,11 +138,26 @@ function parseKeyIndex(raw) {
|
|
|
85
138
|
if (typeof ref !== "string" || ref.length === 0) continue;
|
|
86
139
|
if (list.some((existing) => existing.ref === ref)) continue;
|
|
87
140
|
const alias = normalizeKeyAlias(record["alias"]);
|
|
88
|
-
|
|
141
|
+
const models = normalizeKeyModels(record["models"]);
|
|
142
|
+
const protocol = normalizeKeyProtocol(record["protocol"]);
|
|
143
|
+
const disabled = normalizeKeyDisabled(record["disabled"]);
|
|
144
|
+
list.push(makeEntry(ref, alias, models, protocol, disabled));
|
|
89
145
|
}
|
|
90
146
|
if (list.length > 0) out[route] = list;
|
|
91
147
|
}
|
|
92
|
-
|
|
148
|
+
const endpoints = parseEndpoints(root["endpoints"]);
|
|
149
|
+
const document = { version: 1, providers: out };
|
|
150
|
+
return endpoints === void 0 ? document : { ...document, endpoints };
|
|
151
|
+
}
|
|
152
|
+
function parseEndpoints(raw) {
|
|
153
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return void 0;
|
|
154
|
+
const out = {};
|
|
155
|
+
for (const [route, value] of Object.entries(raw)) {
|
|
156
|
+
if (route.length === 0) continue;
|
|
157
|
+
const pair = normalizeRouteEndpoints(value);
|
|
158
|
+
if (pair !== void 0) out[route] = pair;
|
|
159
|
+
}
|
|
160
|
+
return Object.keys(out).length === 0 ? void 0 : out;
|
|
93
161
|
}
|
|
94
162
|
function readKeyIndex(file = keyIndexFilePath()) {
|
|
95
163
|
let text;
|
|
@@ -148,27 +216,282 @@ function hasKeyEntry(document, route, ref) {
|
|
|
148
216
|
function withKeyEntry(document, route, ref, alias) {
|
|
149
217
|
const list = [...keyEntriesOf(document, route)];
|
|
150
218
|
const index = list.findIndex((entry) => entry.ref === ref);
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
else
|
|
155
|
-
|
|
219
|
+
const label2 = normalizeKeyAlias(alias);
|
|
220
|
+
if (index === -1) {
|
|
221
|
+
list.push(makeEntry(ref, label2, void 0, void 0, void 0));
|
|
222
|
+
} else {
|
|
223
|
+
const existing = list[index];
|
|
224
|
+
const keptLabel = label2 === void 0 ? existing?.alias : label2;
|
|
225
|
+
list[index] = makeEntry(ref, keptLabel, existing?.models, existing?.protocol, existing?.disabled);
|
|
226
|
+
}
|
|
227
|
+
return { version: 1, providers: { ...document.providers, [route]: list }, ...endpointsOf(document) };
|
|
156
228
|
}
|
|
157
229
|
function withKeyAlias(document, route, ref, alias) {
|
|
158
230
|
const list = [...keyEntriesOf(document, route)];
|
|
159
231
|
const index = list.findIndex((entry) => entry.ref === ref);
|
|
160
232
|
if (index === -1) return document;
|
|
161
|
-
const
|
|
162
|
-
list[index] =
|
|
163
|
-
return { version: 1, providers: { ...document.providers, [route]: list } };
|
|
233
|
+
const label2 = normalizeKeyAlias(alias);
|
|
234
|
+
list[index] = makeEntry(ref, label2, list[index]?.models, list[index]?.protocol, list[index]?.disabled);
|
|
235
|
+
return { version: 1, providers: { ...document.providers, [route]: list }, ...endpointsOf(document) };
|
|
236
|
+
}
|
|
237
|
+
function withKeyModels(document, route, ref, models) {
|
|
238
|
+
const list = [...keyEntriesOf(document, route)];
|
|
239
|
+
const index = list.findIndex((entry) => entry.ref === ref);
|
|
240
|
+
if (index === -1) return document;
|
|
241
|
+
const scope = models === void 0 ? void 0 : normalizeKeyModels(models);
|
|
242
|
+
list[index] = makeEntry(ref, list[index]?.alias, scope, list[index]?.protocol, list[index]?.disabled);
|
|
243
|
+
return { version: 1, providers: { ...document.providers, [route]: list }, ...endpointsOf(document) };
|
|
244
|
+
}
|
|
245
|
+
function withKeyProtocol(document, route, ref, protocol) {
|
|
246
|
+
const list = [...keyEntriesOf(document, route)];
|
|
247
|
+
const index = list.findIndex((entry) => entry.ref === ref);
|
|
248
|
+
if (index === -1) return document;
|
|
249
|
+
const pinned = normalizeKeyProtocol(protocol);
|
|
250
|
+
list[index] = makeEntry(ref, list[index]?.alias, list[index]?.models, pinned, list[index]?.disabled);
|
|
251
|
+
return { version: 1, providers: { ...document.providers, [route]: list }, ...endpointsOf(document) };
|
|
252
|
+
}
|
|
253
|
+
function withKeyDisabled(document, route, ref, disabled) {
|
|
254
|
+
const list = [...keyEntriesOf(document, route)];
|
|
255
|
+
const index = list.findIndex((entry) => entry.ref === ref);
|
|
256
|
+
if (index === -1) return document;
|
|
257
|
+
const existing = list[index];
|
|
258
|
+
list[index] = makeEntry(ref, existing?.alias, existing?.models, existing?.protocol, disabled ? true : void 0);
|
|
259
|
+
return { version: 1, providers: { ...document.providers, [route]: list }, ...endpointsOf(document) };
|
|
260
|
+
}
|
|
261
|
+
function withRouteEndpoints(document, route, endpoints) {
|
|
262
|
+
const map = { ...document.endpoints ?? {} };
|
|
263
|
+
const cleaned = normalizeRouteEndpoints(endpoints);
|
|
264
|
+
if (cleaned === void 0) delete map[route];
|
|
265
|
+
else map[route] = cleaned;
|
|
266
|
+
const next = { version: 1, providers: document.providers };
|
|
267
|
+
return Object.keys(map).length === 0 ? next : { ...next, endpoints: map };
|
|
268
|
+
}
|
|
269
|
+
function endpointsOf(document) {
|
|
270
|
+
return document.endpoints === void 0 ? {} : { endpoints: document.endpoints };
|
|
164
271
|
}
|
|
165
272
|
function withoutKeyEntry(document, route, ref) {
|
|
166
273
|
const list = keyEntriesOf(document, route).filter((entry) => entry.ref !== ref);
|
|
167
274
|
const providers = { ...document.providers };
|
|
168
275
|
if (list.length === 0) delete providers[route];
|
|
169
276
|
else providers[route] = list;
|
|
170
|
-
return { version: 1, providers };
|
|
277
|
+
return { version: 1, providers, ...endpointsOf(document) };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// src/key-route-core.ts
|
|
281
|
+
function canServe(key, model) {
|
|
282
|
+
if (model === void 0) return true;
|
|
283
|
+
const models = key.models;
|
|
284
|
+
if (models === void 0) return true;
|
|
285
|
+
return models.includes(model);
|
|
286
|
+
}
|
|
287
|
+
function keysForModel(keys, model) {
|
|
288
|
+
if (model === void 0) return [...keys];
|
|
289
|
+
const eligible = keys.filter((key) => canServe(key, model));
|
|
290
|
+
if (eligible.length === 0) return [...keys];
|
|
291
|
+
const named = eligible.filter((key) => key.models !== void 0);
|
|
292
|
+
const catchAll = eligible.filter((key) => key.models === void 0);
|
|
293
|
+
return [...named, ...catchAll];
|
|
294
|
+
}
|
|
295
|
+
function wireFamilyOf(protocol) {
|
|
296
|
+
if (protocol === "anthropic-messages") return "anthropic";
|
|
297
|
+
if (protocol === "google-generative-ai") return "gemini";
|
|
298
|
+
return "openai";
|
|
299
|
+
}
|
|
300
|
+
var ANTHROPIC_MODEL_ID = /(?:^|[/:._-])(?:claude|anthropic)(?:$|[/:._-])/i;
|
|
301
|
+
var OPENAI_MODEL_ID = /(?:^|[/:._-])(?:gpt|chatgpt|codex)(?:$|[/:._-])|(?:^|[/:._-])o[1-9](?:$|[/:._-])/i;
|
|
302
|
+
var GEMINI_MODEL_ID = /(?:^|[/:._-])(?:gemini|gemma)(?:$|[/:._-])/i;
|
|
303
|
+
function modelWireFamily(modelId) {
|
|
304
|
+
if (modelId === void 0) return void 0;
|
|
305
|
+
if (ANTHROPIC_MODEL_ID.test(modelId)) return "anthropic";
|
|
306
|
+
if (GEMINI_MODEL_ID.test(modelId)) return "gemini";
|
|
307
|
+
if (OPENAI_MODEL_ID.test(modelId)) return "openai";
|
|
308
|
+
return void 0;
|
|
309
|
+
}
|
|
310
|
+
function modelWireHint(modelId) {
|
|
311
|
+
const family = modelWireFamily(modelId);
|
|
312
|
+
if (family === "anthropic") return "anthropic-messages";
|
|
313
|
+
if (family === "gemini") return "google-generative-ai";
|
|
314
|
+
return void 0;
|
|
315
|
+
}
|
|
316
|
+
function autoWireOf(candidate, api, model, avoid) {
|
|
317
|
+
if (model === void 0) return api;
|
|
318
|
+
const family = modelWireFamily(model);
|
|
319
|
+
if (family === void 0 || family === endpointFamilyOf(api)) return api;
|
|
320
|
+
if (candidate.endpoints?.[family] === void 0) return api;
|
|
321
|
+
const hinted = family === "anthropic" ? "anthropic-messages" : family === "gemini" ? "google-generative-ai" : "openai-completions";
|
|
322
|
+
return hinted === avoid ? api : hinted;
|
|
323
|
+
}
|
|
324
|
+
function wireOf(candidate, api, model, avoid) {
|
|
325
|
+
const pinned = candidate.protocol;
|
|
326
|
+
if (pinned === void 0) return autoWireOf(candidate, api, model, avoid);
|
|
327
|
+
const crossesFamilies = endpointFamilyOf(pinned) !== endpointFamilyOf(api);
|
|
328
|
+
return crossesFamilies && candidate.endpoint === void 0 ? api : pinned;
|
|
329
|
+
}
|
|
330
|
+
function isHonoured(candidate, api) {
|
|
331
|
+
const pinned = candidate.protocol;
|
|
332
|
+
if (pinned === void 0) return true;
|
|
333
|
+
return endpointFamilyOf(pinned) === endpointFamilyOf(api) || candidate.endpoint !== void 0;
|
|
334
|
+
}
|
|
335
|
+
function protocolFit(candidate, api, modelId, avoid) {
|
|
336
|
+
const wire = wireOf(candidate, api, modelId, avoid);
|
|
337
|
+
const keyFamily = wireFamilyOf(wire);
|
|
338
|
+
const targetFamily = modelWireFamily(modelId) ?? wireFamilyOf(api);
|
|
339
|
+
if (keyFamily !== targetFamily) return 2;
|
|
340
|
+
if (wire === api) return 0;
|
|
341
|
+
return wire === modelWireHint(modelId) ? 0 : 1;
|
|
342
|
+
}
|
|
343
|
+
function partition(items, keep) {
|
|
344
|
+
const first = [];
|
|
345
|
+
const rest = [];
|
|
346
|
+
for (const item of items) (keep(item) ? first : rest).push(item);
|
|
347
|
+
return [...first, ...rest];
|
|
348
|
+
}
|
|
349
|
+
function restLast(candidates, isResting) {
|
|
350
|
+
return partition(candidates, (candidate) => !isResting(candidate));
|
|
351
|
+
}
|
|
352
|
+
function planKeys(candidates, options) {
|
|
353
|
+
const api = options.api;
|
|
354
|
+
const model = options.model;
|
|
355
|
+
const avoid = options.avoidWire;
|
|
356
|
+
const entries = candidates.map((candidate) => ({
|
|
357
|
+
candidate,
|
|
358
|
+
wire: wireOf(candidate, api, model, avoid),
|
|
359
|
+
fit: protocolFit(candidate, api, model, avoid),
|
|
360
|
+
honoured: isHonoured(candidate, api),
|
|
361
|
+
resting: options.isResting?.(candidate) === true
|
|
362
|
+
}));
|
|
363
|
+
const preferredWire = options.preferredWire;
|
|
364
|
+
const byFit = [...entries].sort((a, b) => a.fit - b.fit);
|
|
365
|
+
const byPreferred = partition(byFit, (entry) => preferredWire === void 0 || entry.wire === preferredWire);
|
|
366
|
+
const byHonour = partition(byPreferred, (entry) => entry.honoured);
|
|
367
|
+
const poolWire = byHonour.find((entry) => entry.honoured)?.wire;
|
|
368
|
+
const pool = byHonour.filter((entry) => entry.honoured && entry.wire === poolWire);
|
|
369
|
+
const aside = byHonour.filter((entry) => !(entry.honoured && entry.wire === poolWire));
|
|
370
|
+
const ordered = partition([...pool, ...aside], (entry) => !entry.resting);
|
|
371
|
+
return {
|
|
372
|
+
ordered: ordered.map((entry) => entry.candidate),
|
|
373
|
+
entries: ordered,
|
|
374
|
+
pool: pool.map((entry) => entry.candidate),
|
|
375
|
+
aside: aside.map((entry) => entry.candidate),
|
|
376
|
+
wire: ordered[0]?.wire
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
function shouldFailover(status) {
|
|
380
|
+
return status === 401 || status === 403 || status === 408 || status === 429 || status >= 500;
|
|
381
|
+
}
|
|
382
|
+
var QUOTA_FAILURE = /quota|insufficient|exhausted|balance|billing|credit|payment required|额度|余额|欠费|用尽|配额|充值/i;
|
|
383
|
+
function classifyFailure(status, bodyText = "") {
|
|
384
|
+
if (status === void 0) return QUOTA_FAILURE.test(bodyText) ? "quota" : "refused";
|
|
385
|
+
if (status === 401 || status === 403) return "auth";
|
|
386
|
+
if (status === 402) return "quota";
|
|
387
|
+
if (status === 408) return "timeout";
|
|
388
|
+
if (status === 429) return QUOTA_FAILURE.test(bodyText) ? "quota" : "rate";
|
|
389
|
+
if (status >= 500) return "server";
|
|
390
|
+
return void 0;
|
|
391
|
+
}
|
|
392
|
+
var COOLDOWN_MS = {
|
|
393
|
+
auth: 5 * 6e4,
|
|
394
|
+
rate: 6e4,
|
|
395
|
+
quota: 30 * 6e4,
|
|
396
|
+
server: 3e4,
|
|
397
|
+
timeout: 3e4,
|
|
398
|
+
refused: 6e4
|
|
399
|
+
};
|
|
400
|
+
var REST_COOLDOWN_CAP_MS = 30 * 6e4;
|
|
401
|
+
function cooldownFor(kind, failures = 1, retryAfterMs) {
|
|
402
|
+
const asked = retryAfterMs !== void 0 && retryAfterMs > 0 ? retryAfterMs : 0;
|
|
403
|
+
const base = Math.min(REST_COOLDOWN_CAP_MS, Math.max(COOLDOWN_MS[kind], asked));
|
|
404
|
+
const escalated = base * 2 ** Math.max(0, failures - 1);
|
|
405
|
+
return Math.round(Math.min(REST_COOLDOWN_CAP_MS, escalated));
|
|
406
|
+
}
|
|
407
|
+
function parseRetryAfter(value, now) {
|
|
408
|
+
if (typeof value !== "string") return void 0;
|
|
409
|
+
const trimmed = value.trim();
|
|
410
|
+
if (trimmed.length === 0) return void 0;
|
|
411
|
+
if (/^\d+(?:\.\d+)?$/.test(trimmed)) return Math.round(Number(trimmed) * 1e3);
|
|
412
|
+
const at = Date.parse(trimmed);
|
|
413
|
+
if (Number.isNaN(at)) return void 0;
|
|
414
|
+
return Math.max(0, at - now);
|
|
415
|
+
}
|
|
416
|
+
var REST_STORE_LIMIT = 256;
|
|
417
|
+
function createRestStore(limit = REST_STORE_LIMIT) {
|
|
418
|
+
const resting = /* @__PURE__ */ new Map();
|
|
419
|
+
const live = (key, now) => {
|
|
420
|
+
const state = resting.get(key);
|
|
421
|
+
if (state === void 0) return void 0;
|
|
422
|
+
if (state.until > now) return state;
|
|
423
|
+
resting.delete(key);
|
|
424
|
+
return void 0;
|
|
425
|
+
};
|
|
426
|
+
return {
|
|
427
|
+
get size() {
|
|
428
|
+
return resting.size;
|
|
429
|
+
},
|
|
430
|
+
stateOf: live,
|
|
431
|
+
isResting(key, now) {
|
|
432
|
+
return live(key, now) !== void 0;
|
|
433
|
+
},
|
|
434
|
+
note(key, kind, options) {
|
|
435
|
+
const now = options?.now ?? Date.now();
|
|
436
|
+
const previous = live(key, now);
|
|
437
|
+
const failures = previous === void 0 ? 1 : previous.failures + 1;
|
|
438
|
+
const until = now + cooldownFor(kind, failures, options?.retryAfterMs);
|
|
439
|
+
if (resting.size >= limit) {
|
|
440
|
+
for (const [staleKey, state2] of resting) {
|
|
441
|
+
if (state2.until <= now) resting.delete(staleKey);
|
|
442
|
+
}
|
|
443
|
+
if (resting.size >= limit) resting.clear();
|
|
444
|
+
}
|
|
445
|
+
const state = { kind, until, failures };
|
|
446
|
+
resting.set(key, state);
|
|
447
|
+
return state;
|
|
448
|
+
},
|
|
449
|
+
clear(key) {
|
|
450
|
+
resting.delete(key);
|
|
451
|
+
},
|
|
452
|
+
reset() {
|
|
453
|
+
resting.clear();
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
function restKeyOf(route, ref) {
|
|
458
|
+
return `${route}\0${ref}`;
|
|
459
|
+
}
|
|
460
|
+
var keyRest = createRestStore();
|
|
461
|
+
var PLAN_TRACE_LIMIT = 16;
|
|
462
|
+
var planTraces = [];
|
|
463
|
+
function recordRoutePlan(route, plan, options) {
|
|
464
|
+
planTraces.push({
|
|
465
|
+
at: options?.now ?? Date.now(),
|
|
466
|
+
route,
|
|
467
|
+
model: options?.model,
|
|
468
|
+
wire: plan.wire,
|
|
469
|
+
keys: plan.entries.map((entry) => ({
|
|
470
|
+
ref: entry.candidate.ref,
|
|
471
|
+
wire: entry.wire,
|
|
472
|
+
fit: entry.fit,
|
|
473
|
+
honoured: entry.honoured,
|
|
474
|
+
resting: entry.resting
|
|
475
|
+
}))
|
|
476
|
+
});
|
|
477
|
+
if (planTraces.length > PLAN_TRACE_LIMIT) planTraces.splice(0, planTraces.length - PLAN_TRACE_LIMIT);
|
|
478
|
+
}
|
|
479
|
+
function applyKeyToHeaders(headers, value) {
|
|
480
|
+
const usesGoogle = headers.has("x-goog-api-key");
|
|
481
|
+
const usesApiKey = headers.has("x-api-key");
|
|
482
|
+
const usesBearer = headers.has("authorization");
|
|
483
|
+
if (usesGoogle) headers.set("x-goog-api-key", value);
|
|
484
|
+
if (usesApiKey) headers.set("x-api-key", value);
|
|
485
|
+
if (usesBearer) headers.set("authorization", `Bearer ${value}`);
|
|
486
|
+
if (!usesGoogle && !usesApiKey && !usesBearer) headers.set("authorization", `Bearer ${value}`);
|
|
487
|
+
if (usesGoogle) {
|
|
488
|
+
headers.delete("authorization");
|
|
489
|
+
headers.delete("x-api-key");
|
|
490
|
+
} else if (usesApiKey && usesBearer) {
|
|
491
|
+
headers.delete("authorization");
|
|
492
|
+
}
|
|
171
493
|
}
|
|
494
|
+
var WIRE_DISPATCH_HEADER = "x-dsh-model-think-level-wire";
|
|
172
495
|
|
|
173
496
|
// src/knowledge.ts
|
|
174
497
|
var INPUT_MODALITIES = ["text", "image"];
|
|
@@ -1503,16 +1826,16 @@ function isSelfHostedEndpoint(baseURL) {
|
|
|
1503
1826
|
return host.includes(":");
|
|
1504
1827
|
}
|
|
1505
1828
|
function sanitizeCompatForProtocol(compat, api) {
|
|
1506
|
-
const
|
|
1829
|
+
const norm2 = normalize(api);
|
|
1507
1830
|
const copy = { ...compat };
|
|
1508
1831
|
delete copy["supportsMidConvoEffort"];
|
|
1509
1832
|
delete copy["allowedFallbackModels"];
|
|
1510
|
-
if (
|
|
1833
|
+
if (norm2 === ANTHROPIC_PROTOCOL) {
|
|
1511
1834
|
for (const key of ["thinkingFormat", "supportsReasoningEffort", "thinkingTokenBudgetField", "supportsThinkingTokenBudget", "vllmPriority"]) delete copy[key];
|
|
1512
1835
|
if (copy["forceAdaptiveThinking"] === void 0) return void 0;
|
|
1513
1836
|
return { forceAdaptiveThinking: copy["forceAdaptiveThinking"] };
|
|
1514
1837
|
}
|
|
1515
|
-
if (
|
|
1838
|
+
if (norm2 !== COMPAT_CAPABLE_PROTOCOL) return void 0;
|
|
1516
1839
|
delete copy["forceAdaptiveThinking"];
|
|
1517
1840
|
return Object.keys(copy).length === 0 ? void 0 : copy;
|
|
1518
1841
|
}
|
|
@@ -1714,6 +2037,12 @@ function looksLikeCompatRefusal(message) {
|
|
|
1714
2037
|
const msg = message.toLowerCase();
|
|
1715
2038
|
return msg.includes("compat") || msg.includes("thinkingtokenbudget") || msg.includes("vllmpriority") || msg.includes("supportsmaxoutput");
|
|
1716
2039
|
}
|
|
2040
|
+
function modelsOf(providers, route) {
|
|
2041
|
+
if (!isRecord(providers)) return [];
|
|
2042
|
+
const profile = providers[route];
|
|
2043
|
+
if (!isRecord(profile) || !Array.isArray(profile["models"])) return [];
|
|
2044
|
+
return profile["models"].filter(isRecord);
|
|
2045
|
+
}
|
|
1717
2046
|
function routeFactsOf(providers, route) {
|
|
1718
2047
|
if (!isRecord(providers)) return {};
|
|
1719
2048
|
const profile = providers[route];
|
|
@@ -1724,6 +2053,433 @@ function routeFactsOf(providers, route) {
|
|
|
1724
2053
|
return { api, baseURL, displayName };
|
|
1725
2054
|
}
|
|
1726
2055
|
|
|
2056
|
+
// src/listing.ts
|
|
2057
|
+
var ANTHROPIC_VERSION = "2023-06-01";
|
|
2058
|
+
var ANTHROPIC_MODEL_LIMIT = 1e3;
|
|
2059
|
+
var GEMINI_MODEL_LIMIT = 1e3;
|
|
2060
|
+
var MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
|
|
2061
|
+
var LISTABLE_PROTOCOLS = /* @__PURE__ */ new Set([
|
|
2062
|
+
"anthropic-messages",
|
|
2063
|
+
"openai-completions",
|
|
2064
|
+
"openai-responses",
|
|
2065
|
+
"google-generative-ai"
|
|
2066
|
+
]);
|
|
2067
|
+
function displayUrl(raw) {
|
|
2068
|
+
try {
|
|
2069
|
+
const url = new URL(raw);
|
|
2070
|
+
url.username = "";
|
|
2071
|
+
url.password = "";
|
|
2072
|
+
return url.toString();
|
|
2073
|
+
} catch {
|
|
2074
|
+
return raw;
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
function probeListingUrl(baseURL, api) {
|
|
2078
|
+
const base = baseURL.replace(/\/+$/, "");
|
|
2079
|
+
if (api === "google-generative-ai") return `${geminiRequestBaseUrl(base)}/models?pageSize=${String(GEMINI_MODEL_LIMIT)}`;
|
|
2080
|
+
if (api !== "anthropic-messages") return `${base}/models`;
|
|
2081
|
+
const root = base.endsWith("/v1") ? base.slice(0, -3) : base;
|
|
2082
|
+
return `${root}/v1/models?limit=${String(ANTHROPIC_MODEL_LIMIT)}`;
|
|
2083
|
+
}
|
|
2084
|
+
function anthropicRequestBaseUrl(baseURL) {
|
|
2085
|
+
const base = baseURL.replace(/\/+$/, "");
|
|
2086
|
+
return base.endsWith("/v1") ? base.slice(0, -3) : base;
|
|
2087
|
+
}
|
|
2088
|
+
function geminiRequestBaseUrl(baseURL) {
|
|
2089
|
+
const base = baseURL.replace(/\/+$/, "");
|
|
2090
|
+
try {
|
|
2091
|
+
return new URL(base).pathname === "/" ? `${base}/v1beta` : base;
|
|
2092
|
+
} catch {
|
|
2093
|
+
return base;
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
function composeProbeHeaders(profileHeaders, apiKey, api) {
|
|
2097
|
+
const headers = new Headers();
|
|
2098
|
+
if (isRecord(profileHeaders)) {
|
|
2099
|
+
for (const [name2, value] of Object.entries(profileHeaders)) {
|
|
2100
|
+
if (typeof value !== "string") continue;
|
|
2101
|
+
try {
|
|
2102
|
+
headers.set(name2, value);
|
|
2103
|
+
} catch {
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
headers.set("accept", "application/json");
|
|
2108
|
+
if (api === "anthropic-messages") {
|
|
2109
|
+
headers.set("anthropic-version", ANTHROPIC_VERSION);
|
|
2110
|
+
if (apiKey !== void 0) headers.set("x-api-key", apiKey);
|
|
2111
|
+
} else if (api === "google-generative-ai") {
|
|
2112
|
+
if (apiKey !== void 0) headers.set("x-goog-api-key", apiKey);
|
|
2113
|
+
} else if (apiKey !== void 0) {
|
|
2114
|
+
headers.set("authorization", `Bearer ${apiKey}`);
|
|
2115
|
+
}
|
|
2116
|
+
return Object.fromEntries(headers.entries());
|
|
2117
|
+
}
|
|
2118
|
+
async function readBounded(response, url) {
|
|
2119
|
+
const declared = Number(response.headers.get("content-length") ?? Number.NaN);
|
|
2120
|
+
if (Number.isFinite(declared) && declared > MAX_RESPONSE_BYTES) {
|
|
2121
|
+
await response.body?.cancel();
|
|
2122
|
+
throw new Error(`${displayUrl(url)} overshoots the ${MAX_RESPONSE_BYTES}-byte listing ceiling`);
|
|
2123
|
+
}
|
|
2124
|
+
if (response.body === null) return "";
|
|
2125
|
+
const reader = response.body.getReader();
|
|
2126
|
+
const chunks = [];
|
|
2127
|
+
let total = 0;
|
|
2128
|
+
try {
|
|
2129
|
+
for (; ; ) {
|
|
2130
|
+
const { done, value } = await reader.read();
|
|
2131
|
+
if (done) break;
|
|
2132
|
+
total += value.byteLength;
|
|
2133
|
+
if (total > MAX_RESPONSE_BYTES) throw new Error(`${displayUrl(url)} overshoots the ${MAX_RESPONSE_BYTES}-byte listing ceiling`);
|
|
2134
|
+
chunks.push(value);
|
|
2135
|
+
}
|
|
2136
|
+
} finally {
|
|
2137
|
+
await reader.cancel().catch(() => {
|
|
2138
|
+
});
|
|
2139
|
+
}
|
|
2140
|
+
const body = new Uint8Array(total);
|
|
2141
|
+
let offset = 0;
|
|
2142
|
+
for (const chunk of chunks) {
|
|
2143
|
+
body.set(chunk, offset);
|
|
2144
|
+
offset += chunk.byteLength;
|
|
2145
|
+
}
|
|
2146
|
+
return new TextDecoder().decode(body);
|
|
2147
|
+
}
|
|
2148
|
+
function listingEntries(body) {
|
|
2149
|
+
const listing = isRecord(body) ? body : {};
|
|
2150
|
+
const data = listing["data"];
|
|
2151
|
+
if (Array.isArray(data)) return data.filter(isRecord);
|
|
2152
|
+
const models = listing["models"];
|
|
2153
|
+
if (Array.isArray(models)) {
|
|
2154
|
+
const out = [];
|
|
2155
|
+
for (const raw of models) {
|
|
2156
|
+
if (!isRecord(raw)) continue;
|
|
2157
|
+
const resource = typeof raw["name"] === "string" ? raw["name"] : "";
|
|
2158
|
+
const id = resource.startsWith("models/") ? resource.slice("models/".length) : resource;
|
|
2159
|
+
if (id.length === 0) continue;
|
|
2160
|
+
const methods = raw["supportedGenerationMethods"];
|
|
2161
|
+
if (Array.isArray(methods) && !methods.includes("generateContent")) continue;
|
|
2162
|
+
const display = typeof raw["displayName"] === "string" ? raw["displayName"] : void 0;
|
|
2163
|
+
const entry = { ...raw, id };
|
|
2164
|
+
if (display !== void 0 && display.trim().length > 0) entry["name"] = display;
|
|
2165
|
+
else delete entry["name"];
|
|
2166
|
+
out.push(entry);
|
|
2167
|
+
}
|
|
2168
|
+
return out;
|
|
2169
|
+
}
|
|
2170
|
+
if (isRecord(models)) {
|
|
2171
|
+
return Object.entries(models).filter(([, raw]) => isRecord(raw)).map(([key, raw]) => {
|
|
2172
|
+
const entry = raw;
|
|
2173
|
+
const ownId = typeof entry["id"] === "string" && entry["id"].length > 0 ? entry["id"] : "";
|
|
2174
|
+
return { ...entry, id: key.length > 0 ? key : ownId };
|
|
2175
|
+
}).filter((entry) => typeof entry["id"] === "string" && entry["id"].length > 0);
|
|
2176
|
+
}
|
|
2177
|
+
return void 0;
|
|
2178
|
+
}
|
|
2179
|
+
function label(value) {
|
|
2180
|
+
if (typeof value !== "string") return void 0;
|
|
2181
|
+
const trimmed = value.trim();
|
|
2182
|
+
return trimmed.length === 0 ? void 0 : trimmed;
|
|
2183
|
+
}
|
|
2184
|
+
function capacity(value) {
|
|
2185
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : void 0;
|
|
2186
|
+
}
|
|
2187
|
+
function capacities(entry, keys) {
|
|
2188
|
+
const limit = isRecord(entry["limit"]) ? entry["limit"] : void 0;
|
|
2189
|
+
const top = isRecord(entry["top_provider"]) ? entry["top_provider"] : void 0;
|
|
2190
|
+
for (const key of keys) {
|
|
2191
|
+
const direct = capacity(entry[key]);
|
|
2192
|
+
if (direct !== void 0) return direct;
|
|
2193
|
+
}
|
|
2194
|
+
const tokenWindow = capacity(limit?.["context"]);
|
|
2195
|
+
if (tokenWindow !== void 0) return tokenWindow;
|
|
2196
|
+
return capacity(top?.["max_completion_tokens"]);
|
|
2197
|
+
}
|
|
2198
|
+
var CONTEXT_WINDOW_KEYS = ["contextWindow", "context_window", "context_length", "max_input_tokens", "inputTokenLimit"];
|
|
2199
|
+
var MAX_TOKENS_KEYS = ["maxOutputTokens", "max_output_tokens", "maxTokens", "max_tokens", "outputTokenLimit"];
|
|
2200
|
+
function modelsFromListing(body) {
|
|
2201
|
+
const entries = listingEntries(body);
|
|
2202
|
+
if (entries === void 0) return void 0;
|
|
2203
|
+
const out = [];
|
|
2204
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2205
|
+
for (const entry of entries) {
|
|
2206
|
+
const id = label(entry["id"]);
|
|
2207
|
+
if (id === void 0 || seen.has(id)) continue;
|
|
2208
|
+
seen.add(id);
|
|
2209
|
+
const model = { id };
|
|
2210
|
+
const name2 = label(entry["name"]) ?? label(entry["display_name"]) ?? label(entry["displayName"]);
|
|
2211
|
+
if (name2 !== void 0) model.name = name2;
|
|
2212
|
+
const contextWindow = capacities(entry, CONTEXT_WINDOW_KEYS);
|
|
2213
|
+
if (contextWindow !== void 0) model.contextWindow = contextWindow;
|
|
2214
|
+
const maxTokens = capacities(entry, MAX_TOKENS_KEYS);
|
|
2215
|
+
if (maxTokens !== void 0) model.maxTokens = maxTokens;
|
|
2216
|
+
const input = entry["input"];
|
|
2217
|
+
if (Array.isArray(input)) {
|
|
2218
|
+
const modalities = [...new Set(input.filter((item) => typeof item === "string" && item.length > 0))];
|
|
2219
|
+
if (modalities.length > 0) model.inputModalities = modalities;
|
|
2220
|
+
}
|
|
2221
|
+
out.push(model);
|
|
2222
|
+
}
|
|
2223
|
+
return out;
|
|
2224
|
+
}
|
|
2225
|
+
|
|
2226
|
+
// src/discovery-merge.ts
|
|
2227
|
+
async function probeListing(probe) {
|
|
2228
|
+
const url = probeListingUrl(probe.baseURL, probe.api);
|
|
2229
|
+
const send = probe.send ?? ((input, init) => fetch(input, init));
|
|
2230
|
+
try {
|
|
2231
|
+
const response = await send(url, {
|
|
2232
|
+
method: "GET",
|
|
2233
|
+
headers: composeProbeHeaders(probe.headers, probe.apiKey, probe.api),
|
|
2234
|
+
redirect: "error",
|
|
2235
|
+
signal: AbortSignal.timeout(probe.timeoutMs)
|
|
2236
|
+
});
|
|
2237
|
+
if (!response.ok) {
|
|
2238
|
+
await response.body?.cancel().catch(() => {
|
|
2239
|
+
});
|
|
2240
|
+
return void 0;
|
|
2241
|
+
}
|
|
2242
|
+
const text = await readBounded(response, url);
|
|
2243
|
+
return modelsFromListing(JSON.parse(text));
|
|
2244
|
+
} catch {
|
|
2245
|
+
return void 0;
|
|
2246
|
+
}
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
// src/key-probe.ts
|
|
2250
|
+
var FAMILY_PROTOCOL = {
|
|
2251
|
+
openai: "openai-completions",
|
|
2252
|
+
anthropic: "anthropic-messages",
|
|
2253
|
+
gemini: "google-generative-ai"
|
|
2254
|
+
};
|
|
2255
|
+
function knownWire(api) {
|
|
2256
|
+
return api === "anthropic-messages" || api === "openai-completions" || api === "openai-responses" || api === "google-generative-ai" ? api : void 0;
|
|
2257
|
+
}
|
|
2258
|
+
function address(value) {
|
|
2259
|
+
if (typeof value !== "string") return void 0;
|
|
2260
|
+
const trimmed = value.trim();
|
|
2261
|
+
return trimmed.length === 0 ? void 0 : trimmed;
|
|
2262
|
+
}
|
|
2263
|
+
function probePlanFor(intent) {
|
|
2264
|
+
const wire = knownWire(intent.api);
|
|
2265
|
+
const family = wire === void 0 ? void 0 : endpointFamilyOf(wire);
|
|
2266
|
+
const baseURL = address(intent.baseURL);
|
|
2267
|
+
const targets = [];
|
|
2268
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2269
|
+
const push = (target) => {
|
|
2270
|
+
if (!LISTABLE_PROTOCOLS.has(target.api)) return;
|
|
2271
|
+
const key = `${target.api}\0${probeListingUrl(target.baseURL, target.api)}`;
|
|
2272
|
+
if (seen.has(key)) return;
|
|
2273
|
+
seen.add(key);
|
|
2274
|
+
targets.push(target);
|
|
2275
|
+
};
|
|
2276
|
+
if (intent.pinned !== void 0) {
|
|
2277
|
+
const pinFamily = endpointFamilyOf(intent.pinned);
|
|
2278
|
+
const pinnedBase = pinFamily === family ? baseURL ?? address(intent.endpoints?.[pinFamily]) : address(intent.endpoints?.[pinFamily]);
|
|
2279
|
+
if (pinnedBase === void 0) {
|
|
2280
|
+
return {
|
|
2281
|
+
targets: [],
|
|
2282
|
+
skipped: `the key pins ${intent.pinned} but this provider has no ${pinFamily} address`
|
|
2283
|
+
};
|
|
2284
|
+
}
|
|
2285
|
+
push({ api: intent.pinned, baseURL: pinnedBase, family: pinFamily });
|
|
2286
|
+
return { targets };
|
|
2287
|
+
}
|
|
2288
|
+
if (wire !== void 0 && family !== void 0) {
|
|
2289
|
+
if (baseURL !== void 0) push({ api: wire, baseURL, family });
|
|
2290
|
+
for (const other of ENDPOINT_FAMILIES) {
|
|
2291
|
+
if (other === family) continue;
|
|
2292
|
+
const otherBase = address(intent.endpoints?.[other]);
|
|
2293
|
+
if (otherBase !== void 0) push({ api: FAMILY_PROTOCOL[other], baseURL: otherBase, family: other });
|
|
2294
|
+
}
|
|
2295
|
+
return { targets };
|
|
2296
|
+
}
|
|
2297
|
+
for (const configured of ENDPOINT_FAMILIES) {
|
|
2298
|
+
const configuredBase = address(intent.endpoints?.[configured]);
|
|
2299
|
+
if (configuredBase !== void 0) push({ api: FAMILY_PROTOCOL[configured], baseURL: configuredBase, family: configured });
|
|
2300
|
+
}
|
|
2301
|
+
return {
|
|
2302
|
+
targets,
|
|
2303
|
+
...targets.length === 0 ? { skipped: `this provider cannot be interrogated on ${String(intent.api)}; set each key's models by hand` } : {}
|
|
2304
|
+
};
|
|
2305
|
+
}
|
|
2306
|
+
async function probeKeyModels(options) {
|
|
2307
|
+
const plan = probePlanFor(options.intent);
|
|
2308
|
+
const answers = await Promise.all(plan.targets.map(async (target) => {
|
|
2309
|
+
const url = probeListingUrl(target.baseURL, target.api);
|
|
2310
|
+
const reported = displayUrl(url);
|
|
2311
|
+
let response;
|
|
2312
|
+
try {
|
|
2313
|
+
response = await options.send(url, {
|
|
2314
|
+
method: "GET",
|
|
2315
|
+
headers: options.headersFor(options.profileHeaders, options.apiKey, target.api),
|
|
2316
|
+
redirect: "error",
|
|
2317
|
+
signal: AbortSignal.timeout(options.timeoutMs)
|
|
2318
|
+
});
|
|
2319
|
+
} catch (error) {
|
|
2320
|
+
const outcome2 = {
|
|
2321
|
+
api: target.api,
|
|
2322
|
+
family: target.family,
|
|
2323
|
+
url: reported,
|
|
2324
|
+
ok: false,
|
|
2325
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2326
|
+
};
|
|
2327
|
+
return { outcome: outcome2, ids: [] };
|
|
2328
|
+
}
|
|
2329
|
+
if (!response.ok) {
|
|
2330
|
+
await response.body?.cancel().catch(() => {
|
|
2331
|
+
});
|
|
2332
|
+
const outcome2 = {
|
|
2333
|
+
api: target.api,
|
|
2334
|
+
family: target.family,
|
|
2335
|
+
url: reported,
|
|
2336
|
+
ok: false,
|
|
2337
|
+
status: response.status
|
|
2338
|
+
};
|
|
2339
|
+
return { outcome: outcome2, ids: [] };
|
|
2340
|
+
}
|
|
2341
|
+
let entries;
|
|
2342
|
+
try {
|
|
2343
|
+
entries = options.entries(JSON.parse(await options.read(response, url)));
|
|
2344
|
+
} catch (error) {
|
|
2345
|
+
const outcome2 = {
|
|
2346
|
+
api: target.api,
|
|
2347
|
+
family: target.family,
|
|
2348
|
+
url: reported,
|
|
2349
|
+
ok: false,
|
|
2350
|
+
status: response.status,
|
|
2351
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2352
|
+
};
|
|
2353
|
+
return { outcome: outcome2, ids: [] };
|
|
2354
|
+
}
|
|
2355
|
+
if (entries === void 0) {
|
|
2356
|
+
const outcome2 = {
|
|
2357
|
+
api: target.api,
|
|
2358
|
+
family: target.family,
|
|
2359
|
+
url: reported,
|
|
2360
|
+
ok: false,
|
|
2361
|
+
status: response.status,
|
|
2362
|
+
error: "the reply names no model listing"
|
|
2363
|
+
};
|
|
2364
|
+
return { outcome: outcome2, ids: [] };
|
|
2365
|
+
}
|
|
2366
|
+
const outcome = {
|
|
2367
|
+
api: target.api,
|
|
2368
|
+
family: target.family,
|
|
2369
|
+
url: reported,
|
|
2370
|
+
ok: true,
|
|
2371
|
+
status: response.status,
|
|
2372
|
+
models: entries.length
|
|
2373
|
+
};
|
|
2374
|
+
const found = [];
|
|
2375
|
+
for (const entry of entries) {
|
|
2376
|
+
const id = entry["id"];
|
|
2377
|
+
if (typeof id === "string" && id.length > 0) found.push(id);
|
|
2378
|
+
}
|
|
2379
|
+
return { outcome, ids: found };
|
|
2380
|
+
}));
|
|
2381
|
+
const outcomes = answers.map((answer) => answer.outcome);
|
|
2382
|
+
const ids = [];
|
|
2383
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2384
|
+
let answered = 0;
|
|
2385
|
+
for (const answer of answers) {
|
|
2386
|
+
if (!answer.outcome.ok) continue;
|
|
2387
|
+
answered += 1;
|
|
2388
|
+
for (const id of answer.ids) {
|
|
2389
|
+
if (seen.has(id)) continue;
|
|
2390
|
+
seen.add(id);
|
|
2391
|
+
ids.push(id);
|
|
2392
|
+
}
|
|
2393
|
+
}
|
|
2394
|
+
return {
|
|
2395
|
+
ref: options.ref,
|
|
2396
|
+
models: answered === 0 ? void 0 : ids,
|
|
2397
|
+
targets: outcomes,
|
|
2398
|
+
...plan.skipped === void 0 ? {} : { skipped: plan.skipped }
|
|
2399
|
+
};
|
|
2400
|
+
}
|
|
2401
|
+
|
|
2402
|
+
// src/multi-key-listing.ts
|
|
2403
|
+
var DEFAULT_PROBE_CONCURRENCY = 4;
|
|
2404
|
+
var MODEL_ORDER = new Intl.Collator("en", { numeric: true, sensitivity: "base" });
|
|
2405
|
+
function idsOf(models) {
|
|
2406
|
+
if (models === void 0) return [];
|
|
2407
|
+
const out = [];
|
|
2408
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2409
|
+
for (const id of models) {
|
|
2410
|
+
const trimmed = typeof id === "string" ? id.trim() : "";
|
|
2411
|
+
if (trimmed.length === 0 || seen.has(trimmed)) continue;
|
|
2412
|
+
seen.add(trimmed);
|
|
2413
|
+
out.push(trimmed);
|
|
2414
|
+
}
|
|
2415
|
+
return out;
|
|
2416
|
+
}
|
|
2417
|
+
function mergeKeyListings(results, lastKnown) {
|
|
2418
|
+
const order = [];
|
|
2419
|
+
const keysByModel = /* @__PURE__ */ new Map();
|
|
2420
|
+
const answered = [];
|
|
2421
|
+
const unreadable = [];
|
|
2422
|
+
const retained = [];
|
|
2423
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
2424
|
+
const add = (ref, ids) => {
|
|
2425
|
+
for (const id of ids) {
|
|
2426
|
+
const owners = keysByModel.get(id);
|
|
2427
|
+
if (owners === void 0) {
|
|
2428
|
+
keysByModel.set(id, [ref]);
|
|
2429
|
+
order.push(id);
|
|
2430
|
+
} else if (!owners.includes(ref)) {
|
|
2431
|
+
owners.push(ref);
|
|
2432
|
+
}
|
|
2433
|
+
}
|
|
2434
|
+
};
|
|
2435
|
+
for (const result of results) {
|
|
2436
|
+
if (seenKeys.has(result.ref)) continue;
|
|
2437
|
+
seenKeys.add(result.ref);
|
|
2438
|
+
if (result.models !== void 0) {
|
|
2439
|
+
answered.push(result.ref);
|
|
2440
|
+
add(result.ref, idsOf(result.models));
|
|
2441
|
+
continue;
|
|
2442
|
+
}
|
|
2443
|
+
unreadable.push(result.ref);
|
|
2444
|
+
const known = idsOf(lastKnown?.get(result.ref));
|
|
2445
|
+
if (known.length === 0) continue;
|
|
2446
|
+
retained.push(result.ref);
|
|
2447
|
+
add(result.ref, known);
|
|
2448
|
+
}
|
|
2449
|
+
order.sort(MODEL_ORDER.compare);
|
|
2450
|
+
const models = order.map((id) => ({ id, keys: keysByModel.get(id) ?? [] }));
|
|
2451
|
+
return {
|
|
2452
|
+
models,
|
|
2453
|
+
ids: order,
|
|
2454
|
+
answered,
|
|
2455
|
+
unreadable,
|
|
2456
|
+
retained
|
|
2457
|
+
};
|
|
2458
|
+
}
|
|
2459
|
+
async function collectKeyListings(options) {
|
|
2460
|
+
const total = options.keys.length;
|
|
2461
|
+
const results = new Array(total);
|
|
2462
|
+
const bound = Math.max(1, Math.min(options.concurrency ?? DEFAULT_PROBE_CONCURRENCY, Math.max(total, 1)));
|
|
2463
|
+
let next = 0;
|
|
2464
|
+
const worker = async () => {
|
|
2465
|
+
for (; ; ) {
|
|
2466
|
+
const index = next;
|
|
2467
|
+
next += 1;
|
|
2468
|
+
if (index >= total) return;
|
|
2469
|
+
const key = options.keys[index];
|
|
2470
|
+
try {
|
|
2471
|
+
results[index] = await options.probe(key);
|
|
2472
|
+
} catch {
|
|
2473
|
+
const ref = options.refOf?.(key);
|
|
2474
|
+
if (ref !== void 0) results[index] = { ref, models: void 0, targets: [] };
|
|
2475
|
+
}
|
|
2476
|
+
}
|
|
2477
|
+
};
|
|
2478
|
+
await Promise.all(Array.from({ length: bound }, async () => worker()));
|
|
2479
|
+
const settled = results.filter((result) => result !== void 0);
|
|
2480
|
+
return { results: settled, merged: mergeKeyListings(settled, options.lastKnown) };
|
|
2481
|
+
}
|
|
2482
|
+
|
|
1727
2483
|
// src/headers-core.ts
|
|
1728
2484
|
var MAX_UA_LENGTH = 512;
|
|
1729
2485
|
function emptyIndex() {
|
|
@@ -1796,11 +2552,11 @@ function modelIdsOf(profile) {
|
|
|
1796
2552
|
return [...ids];
|
|
1797
2553
|
}
|
|
1798
2554
|
function normalizedHeaders(headers) {
|
|
1799
|
-
const
|
|
2555
|
+
const normalized2 = /* @__PURE__ */ new Map();
|
|
1800
2556
|
for (const [name2, value] of Object.entries(headers)) {
|
|
1801
|
-
|
|
2557
|
+
normalized2.set(name2.toLowerCase(), { name: name2, value });
|
|
1802
2558
|
}
|
|
1803
|
-
return
|
|
2559
|
+
return normalized2;
|
|
1804
2560
|
}
|
|
1805
2561
|
function recordHeaderConflicts(index, existing, route, headers) {
|
|
1806
2562
|
const first = normalizedHeaders(existing.headers);
|
|
@@ -1941,41 +2697,159 @@ async function bodyTextOf(input, init) {
|
|
|
1941
2697
|
}
|
|
1942
2698
|
return void 0;
|
|
1943
2699
|
}
|
|
1944
|
-
function
|
|
1945
|
-
if (
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
}
|
|
1949
|
-
|
|
2700
|
+
function isModelListingRequest(url, method) {
|
|
2701
|
+
if ((method ?? "GET").toUpperCase() !== "GET") return false;
|
|
2702
|
+
try {
|
|
2703
|
+
return new URL(url).pathname.endsWith("/models");
|
|
2704
|
+
} catch {
|
|
2705
|
+
return false;
|
|
2706
|
+
}
|
|
2707
|
+
}
|
|
2708
|
+
function listingCouldExceedPlainRequest(keys) {
|
|
2709
|
+
if (keys.length > 1) return true;
|
|
2710
|
+
return keys.some((candidate) => candidate.endpoint !== void 0 || candidate.endpoints !== void 0 && Object.keys(candidate.endpoints).length > 0);
|
|
2711
|
+
}
|
|
2712
|
+
async function readBoundedText(response, limit) {
|
|
2713
|
+
const body = response.clone().body;
|
|
2714
|
+
if (body === null) return "";
|
|
2715
|
+
const reader = body.getReader();
|
|
2716
|
+
const decoder = new TextDecoder();
|
|
2717
|
+
let text = "";
|
|
2718
|
+
try {
|
|
2719
|
+
for (; ; ) {
|
|
2720
|
+
const { done, value } = await reader.read();
|
|
2721
|
+
if (done) break;
|
|
2722
|
+
text += decoder.decode(value, { stream: true });
|
|
2723
|
+
if (text.length >= limit) break;
|
|
2724
|
+
}
|
|
2725
|
+
} finally {
|
|
2726
|
+
await reader.cancel().catch(() => {
|
|
2727
|
+
});
|
|
2728
|
+
}
|
|
2729
|
+
return text.slice(0, limit);
|
|
2730
|
+
}
|
|
2731
|
+
function seedHeaders(input, init) {
|
|
2732
|
+
if (init?.headers !== void 0 && init.headers !== null) return new Headers(init.headers);
|
|
2733
|
+
if (typeof Request !== "undefined" && input instanceof Request) return new Headers(input.headers);
|
|
2734
|
+
return new Headers();
|
|
2735
|
+
}
|
|
2736
|
+
function makeWrapper(base, entries) {
|
|
1950
2737
|
return async function fetchWithProviderHeaders(input, init) {
|
|
1951
2738
|
const url = requestUrlOf(input);
|
|
1952
2739
|
if (url === void 0) return base(input, init);
|
|
2740
|
+
let entry;
|
|
1953
2741
|
let candidates = [];
|
|
1954
|
-
for (const
|
|
1955
|
-
|
|
1956
|
-
if (
|
|
2742
|
+
for (const candidate of entries.values()) {
|
|
2743
|
+
const found = pathCandidatesOf(candidate.index(), url);
|
|
2744
|
+
if (found.length > 0) {
|
|
2745
|
+
entry = candidate;
|
|
2746
|
+
candidates = found;
|
|
2747
|
+
break;
|
|
2748
|
+
}
|
|
1957
2749
|
}
|
|
1958
|
-
if (candidates.length === 0) return base(input, init);
|
|
1959
|
-
|
|
1960
|
-
|
|
2750
|
+
if (entry === void 0 || candidates.length === 0) return base(input, init);
|
|
2751
|
+
const keyed = entry.keys !== void 0;
|
|
2752
|
+
let model;
|
|
2753
|
+
if (candidates.length > 1 || keyed) {
|
|
1961
2754
|
const body = await bodyTextOf(input, init);
|
|
1962
|
-
|
|
1963
|
-
override = selectOverrideFrom(candidates, model);
|
|
2755
|
+
model = body === void 0 ? void 0 : modelOfJsonBody(body);
|
|
1964
2756
|
}
|
|
2757
|
+
const override = candidates.length === 1 ? candidates[0] : selectOverrideFrom(candidates, model);
|
|
1965
2758
|
if (override === void 0) return base(input, init);
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
2759
|
+
const patched = seedHeaders(input, init);
|
|
2760
|
+
let hasPatch = false;
|
|
2761
|
+
if (override.hasHeaders) {
|
|
2762
|
+
for (const [name2, value] of Object.entries(override.headers)) {
|
|
2763
|
+
try {
|
|
2764
|
+
patched.set(name2, value);
|
|
2765
|
+
hasPatch = true;
|
|
2766
|
+
} catch {
|
|
2767
|
+
}
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
const attempt = (keyValue) => {
|
|
2771
|
+
const headers = new Headers(patched);
|
|
2772
|
+
if (keyValue !== void 0) applyKeyToHeaders(headers, keyValue);
|
|
2773
|
+
if (typeof Request !== "undefined" && input instanceof Request) {
|
|
2774
|
+
return base(new Request(input.clone(), { headers }), { ...init, headers });
|
|
2775
|
+
}
|
|
2776
|
+
return base(input, { ...init, headers });
|
|
2777
|
+
};
|
|
2778
|
+
if (patched.get(WIRE_DISPATCH_HEADER) === "1") {
|
|
2779
|
+
patched.delete(WIRE_DISPATCH_HEADER);
|
|
2780
|
+
if (!hasPatch) return base(input, init);
|
|
2781
|
+
return attempt(void 0);
|
|
2782
|
+
}
|
|
2783
|
+
const resolved = keyed ? await entry.keys({ route: override.route, model }) : [];
|
|
2784
|
+
const keyCandidates = restLast(
|
|
2785
|
+
resolved,
|
|
2786
|
+
(candidate) => keyRest.isResting(restKeyOf(override.route, candidate.ref), Date.now())
|
|
2787
|
+
);
|
|
2788
|
+
const method = init?.method ?? (typeof Request !== "undefined" && input instanceof Request ? input.method : void 0);
|
|
2789
|
+
if (entry.modelListing !== void 0 && isModelListingRequest(url, method) && listingCouldExceedPlainRequest(keyCandidates)) {
|
|
2790
|
+
const signal = init?.signal ?? (typeof Request !== "undefined" && input instanceof Request ? input.signal : void 0);
|
|
1969
2791
|
try {
|
|
1970
|
-
|
|
2792
|
+
const listing = await entry.modelListing({
|
|
2793
|
+
route: override.route,
|
|
2794
|
+
url,
|
|
2795
|
+
headers: new Headers(patched),
|
|
2796
|
+
keys: keyCandidates,
|
|
2797
|
+
...signal === void 0 ? {} : { signal },
|
|
2798
|
+
send: base
|
|
2799
|
+
});
|
|
2800
|
+
if (listing !== void 0) return listing;
|
|
2801
|
+
} catch {
|
|
2802
|
+
}
|
|
2803
|
+
}
|
|
2804
|
+
if (!hasPatch && keyCandidates.length === 0) return base(input, init);
|
|
2805
|
+
const chargeFailure = async (candidate, response) => {
|
|
2806
|
+
const now = Date.now();
|
|
2807
|
+
const retryAfterMs = parseRetryAfter(response.headers.get("retry-after"), now);
|
|
2808
|
+
let bodyText = "";
|
|
2809
|
+
try {
|
|
2810
|
+
bodyText = await readBoundedText(response, 4096);
|
|
1971
2811
|
} catch {
|
|
1972
2812
|
}
|
|
2813
|
+
const kind = classifyFailure(response.status, bodyText);
|
|
2814
|
+
if (kind === void 0) return;
|
|
2815
|
+
keyRest.note(
|
|
2816
|
+
restKeyOf(override.route, candidate.ref),
|
|
2817
|
+
kind,
|
|
2818
|
+
retryAfterMs === void 0 ? { now } : { now, retryAfterMs }
|
|
2819
|
+
);
|
|
2820
|
+
};
|
|
2821
|
+
const callerAborted = () => init?.signal?.aborted === true || typeof Request !== "undefined" && input instanceof Request && input.signal.aborted;
|
|
2822
|
+
if (keyCandidates.length <= 1) {
|
|
2823
|
+
const solo = keyCandidates[0];
|
|
2824
|
+
const response = await attempt(solo?.value);
|
|
2825
|
+
if (solo !== void 0) {
|
|
2826
|
+
if (shouldFailover(response.status)) await chargeFailure(solo, response);
|
|
2827
|
+
else keyRest.clear(restKeyOf(override.route, solo.ref));
|
|
2828
|
+
}
|
|
2829
|
+
return response;
|
|
1973
2830
|
}
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
2831
|
+
let lastFailure;
|
|
2832
|
+
for (let index = 0; index < keyCandidates.length; index += 1) {
|
|
2833
|
+
const candidate = keyCandidates[index];
|
|
2834
|
+
let response;
|
|
2835
|
+
try {
|
|
2836
|
+
response = await attempt(candidate.value);
|
|
2837
|
+
} catch (error) {
|
|
2838
|
+
if (callerAborted()) throw error;
|
|
2839
|
+
lastFailure = error;
|
|
2840
|
+
keyRest.note(restKeyOf(override.route, candidate.ref), "refused");
|
|
2841
|
+
continue;
|
|
2842
|
+
}
|
|
2843
|
+
if (!shouldFailover(response.status)) {
|
|
2844
|
+
keyRest.clear(restKeyOf(override.route, candidate.ref));
|
|
2845
|
+
return response;
|
|
2846
|
+
}
|
|
2847
|
+
await chargeFailure(candidate, response);
|
|
2848
|
+
if (index === keyCandidates.length - 1) return response;
|
|
2849
|
+
await response.body?.cancel().catch(() => {
|
|
2850
|
+
});
|
|
1977
2851
|
}
|
|
1978
|
-
|
|
2852
|
+
throw lastFailure ?? new Error("no key could place the request");
|
|
1979
2853
|
};
|
|
1980
2854
|
}
|
|
1981
2855
|
function installHeaderOverlay(source) {
|
|
@@ -1990,7 +2864,13 @@ function installHeaderOverlay(source) {
|
|
|
1990
2864
|
holder[REGISTRY] = registry;
|
|
1991
2865
|
globalThis.fetch = wrapper;
|
|
1992
2866
|
}
|
|
1993
|
-
registry.entries.set(token, {
|
|
2867
|
+
registry.entries.set(token, {
|
|
2868
|
+
index: () => source.current,
|
|
2869
|
+
// Delegate through the live source property so a resolver attached after
|
|
2870
|
+
// install is honoured, and its internal data stays current.
|
|
2871
|
+
keys: source.keys === void 0 ? void 0 : (context) => source.keys(context),
|
|
2872
|
+
modelListing: source.modelListing === void 0 ? void 0 : (context) => source.modelListing(context)
|
|
2873
|
+
});
|
|
1994
2874
|
const owned = registry;
|
|
1995
2875
|
return {
|
|
1996
2876
|
get active() {
|
|
@@ -2008,8 +2888,70 @@ function headerOverlayInstalled() {
|
|
|
2008
2888
|
const registry = registryOf();
|
|
2009
2889
|
return registry !== void 0 && globalThis.fetch === registry.wrapper;
|
|
2010
2890
|
}
|
|
2891
|
+
function headerOverlayBypass() {
|
|
2892
|
+
const registry = registryOf();
|
|
2893
|
+
if (registry !== void 0 && globalThis.fetch === registry.wrapper) return registry.base;
|
|
2894
|
+
return globalThis.fetch;
|
|
2895
|
+
}
|
|
2011
2896
|
|
|
2012
2897
|
// src/headers-pi-ai.ts
|
|
2898
|
+
var servedWire = /* @__PURE__ */ new Map();
|
|
2899
|
+
var failedWire = /* @__PURE__ */ new Map();
|
|
2900
|
+
var WIRE_MEMORY_LIMIT = 256;
|
|
2901
|
+
function rememberWireFact(map, key, wire) {
|
|
2902
|
+
if (map.size >= WIRE_MEMORY_LIMIT) map.clear();
|
|
2903
|
+
map.set(key, wire);
|
|
2904
|
+
}
|
|
2905
|
+
var PROTOCOL_CACHE = /* @__PURE__ */ new Map();
|
|
2906
|
+
var CONTEXT_NORMALIZER;
|
|
2907
|
+
function loadContextNormalizer() {
|
|
2908
|
+
if (CONTEXT_NORMALIZER !== void 0) return CONTEXT_NORMALIZER;
|
|
2909
|
+
CONTEXT_NORMALIZER = (async () => {
|
|
2910
|
+
try {
|
|
2911
|
+
const specifier = "@earendil-works/pi-ai/utils/transcript";
|
|
2912
|
+
const module = await import(
|
|
2913
|
+
/* @vite-ignore */
|
|
2914
|
+
specifier
|
|
2915
|
+
);
|
|
2916
|
+
return typeof module.normalizeContext === "function" ? module.normalizeContext : void 0;
|
|
2917
|
+
} catch {
|
|
2918
|
+
return void 0;
|
|
2919
|
+
}
|
|
2920
|
+
})();
|
|
2921
|
+
return CONTEXT_NORMALIZER;
|
|
2922
|
+
}
|
|
2923
|
+
var NORMALIZED_CONTEXTS = /* @__PURE__ */ new WeakMap();
|
|
2924
|
+
async function normalizeDispatchedContext(context) {
|
|
2925
|
+
const object = objectOf(context);
|
|
2926
|
+
if (object === void 0) return context;
|
|
2927
|
+
if (NORMALIZED_CONTEXTS.has(object)) return NORMALIZED_CONTEXTS.get(object);
|
|
2928
|
+
if (!Array.isArray(object["messages"])) return context;
|
|
2929
|
+
const normalize2 = await loadContextNormalizer();
|
|
2930
|
+
if (normalize2 === void 0) return context;
|
|
2931
|
+
const normalized2 = normalize2(context);
|
|
2932
|
+
if (objectOf(normalized2) === void 0) return context;
|
|
2933
|
+
NORMALIZED_CONTEXTS.set(object, normalized2);
|
|
2934
|
+
return normalized2;
|
|
2935
|
+
}
|
|
2936
|
+
function loadProtocol(protocol) {
|
|
2937
|
+
let pending = PROTOCOL_CACHE.get(protocol);
|
|
2938
|
+
if (pending !== void 0) return pending;
|
|
2939
|
+
pending = (async () => {
|
|
2940
|
+
switch (protocol) {
|
|
2941
|
+
case "anthropic-messages":
|
|
2942
|
+
return await import("@earendil-works/pi-ai/api/anthropic-messages");
|
|
2943
|
+
case "openai-responses":
|
|
2944
|
+
return await import("@earendil-works/pi-ai/api/openai-responses");
|
|
2945
|
+
case "google-generative-ai":
|
|
2946
|
+
return await import("@earendil-works/pi-ai/api/google-generative-ai");
|
|
2947
|
+
case "openai-completions":
|
|
2948
|
+
default:
|
|
2949
|
+
return await import("@earendil-works/pi-ai/api/openai-completions");
|
|
2950
|
+
}
|
|
2951
|
+
})();
|
|
2952
|
+
PROTOCOL_CACHE.set(protocol, pending);
|
|
2953
|
+
return pending;
|
|
2954
|
+
}
|
|
2013
2955
|
var PATCH_STATE = /* @__PURE__ */ Symbol.for("dsh-model-think-level.pi-ai-header-transform");
|
|
2014
2956
|
var REGISTER_PATCH_STATE = /* @__PURE__ */ Symbol.for("dsh-model-think-level.pi-ai-header-register");
|
|
2015
2957
|
var CORDIS_ORIGINAL = /* @__PURE__ */ Symbol.for("cordis.original");
|
|
@@ -2040,6 +2982,11 @@ function routeOverrideOf(source, model) {
|
|
|
2040
2982
|
return void 0;
|
|
2041
2983
|
}
|
|
2042
2984
|
}
|
|
2985
|
+
function errorTextOf(event) {
|
|
2986
|
+
const error = isRecord(event["error"]) ? event["error"] : void 0;
|
|
2987
|
+
const parts = [error?.["message"], error?.["errorMessage"], event["reason"]];
|
|
2988
|
+
return parts.filter((part) => typeof part === "string").join(" ");
|
|
2989
|
+
}
|
|
2043
2990
|
function modelsProxyOf(models, source) {
|
|
2044
2991
|
return new Proxy(models, {
|
|
2045
2992
|
get(target, property, receiver) {
|
|
@@ -2060,10 +3007,125 @@ function modelsProxyOf(models, source) {
|
|
|
2060
3007
|
const override = routeOverrideOf(source, model);
|
|
2061
3008
|
return override?.hasHeaders ? { ...merged, ...override.headers } : merged;
|
|
2062
3009
|
};
|
|
2063
|
-
|
|
2064
|
-
...
|
|
2065
|
-
|
|
3010
|
+
const markedTransformHeaders = async (headers) => ({
|
|
3011
|
+
...await transformHeaders(headers),
|
|
3012
|
+
[WIRE_DISPATCH_HEADER]: "1"
|
|
2066
3013
|
});
|
|
3014
|
+
const modelId = typeof modelObject["id"] === "string" ? modelObject["id"] : void 0;
|
|
3015
|
+
const keyResolver = source.keys;
|
|
3016
|
+
if (keyResolver === void 0) {
|
|
3017
|
+
return streamSimple.call(target, model, context, {
|
|
3018
|
+
...options,
|
|
3019
|
+
transformHeaders
|
|
3020
|
+
});
|
|
3021
|
+
}
|
|
3022
|
+
const route = routeOverrideOf(source, model)?.route;
|
|
3023
|
+
const dispatch = () => {
|
|
3024
|
+
if (route === void 0) return Promise.resolve([]);
|
|
3025
|
+
return Promise.resolve(keyResolver({ route, model: modelId })).catch(() => []);
|
|
3026
|
+
};
|
|
3027
|
+
const startAttempt = async (candidate, avoid) => {
|
|
3028
|
+
const api = typeof modelObject["api"] === "string" ? modelObject["api"] : "openai-completions";
|
|
3029
|
+
const protocol = wireOf(candidate, api, modelId, avoid);
|
|
3030
|
+
const honoured = isHonoured(candidate, api);
|
|
3031
|
+
const autoEndpoint = honoured && candidate.protocol === void 0 && protocol !== api ? candidate.endpoints?.[endpointFamilyOf(protocol)] : void 0;
|
|
3032
|
+
const selectedBaseUrl = honoured && candidate.endpoint !== void 0 ? candidate.endpoint : autoEndpoint !== void 0 ? autoEndpoint : modelObject["baseUrl"];
|
|
3033
|
+
const baseUrl = protocol === "anthropic-messages" ? anthropicRequestBaseUrl(selectedBaseUrl) : protocol === "google-generative-ai" ? geminiRequestBaseUrl(selectedBaseUrl) : selectedBaseUrl;
|
|
3034
|
+
const switched = protocol !== api || baseUrl !== modelObject["baseUrl"];
|
|
3035
|
+
if (!switched) {
|
|
3036
|
+
return streamSimple.call(target, model, context, {
|
|
3037
|
+
...options,
|
|
3038
|
+
apiKey: candidate.value,
|
|
3039
|
+
transformHeaders: markedTransformHeaders
|
|
3040
|
+
});
|
|
3041
|
+
}
|
|
3042
|
+
const mergedHeaders = await transformHeaders(isRecord(options["headers"]) ? options["headers"] : {});
|
|
3043
|
+
const implementation = await loadProtocol(protocol);
|
|
3044
|
+
const dispatchedContext = await normalizeDispatchedContext(context);
|
|
3045
|
+
return implementation.streamSimple(
|
|
3046
|
+
{ ...modelObject, api: protocol, baseUrl },
|
|
3047
|
+
dispatchedContext,
|
|
3048
|
+
{
|
|
3049
|
+
...options,
|
|
3050
|
+
apiKey: candidate.value,
|
|
3051
|
+
headers: { ...mergedHeaders, [WIRE_DISPATCH_HEADER]: "1" },
|
|
3052
|
+
transformHeaders: void 0
|
|
3053
|
+
}
|
|
3054
|
+
);
|
|
3055
|
+
};
|
|
3056
|
+
const run = async function* () {
|
|
3057
|
+
const routeCandidates = await dispatch();
|
|
3058
|
+
if (routeCandidates.length === 0) {
|
|
3059
|
+
const stream = streamSimple.call(target, model, context, {
|
|
3060
|
+
...options,
|
|
3061
|
+
transformHeaders
|
|
3062
|
+
});
|
|
3063
|
+
for await (const event of stream) yield event;
|
|
3064
|
+
return;
|
|
3065
|
+
}
|
|
3066
|
+
const api = typeof modelObject["api"] === "string" ? modelObject["api"] : "openai-completions";
|
|
3067
|
+
const servedKey = `${route ?? ""}\0${modelId ?? ""}`;
|
|
3068
|
+
const avoidWire = failedWire.get(servedKey);
|
|
3069
|
+
const plan = planKeys(routeCandidates, {
|
|
3070
|
+
api,
|
|
3071
|
+
model: modelId,
|
|
3072
|
+
preferredWire: servedWire.get(servedKey),
|
|
3073
|
+
avoidWire,
|
|
3074
|
+
isResting: (candidate) => keyRest.isResting(restKeyOf(route ?? "", candidate.ref), Date.now())
|
|
3075
|
+
});
|
|
3076
|
+
recordRoutePlan(route ?? "", plan, { model: modelId });
|
|
3077
|
+
const wireOfAttempt = (candidate) => plan.entries.find((entry) => entry.candidate === candidate)?.wire ?? wireOf(candidate, api, modelId, avoidWire);
|
|
3078
|
+
let lastErrorEvent;
|
|
3079
|
+
let lastError;
|
|
3080
|
+
for (const candidate of plan.ordered) {
|
|
3081
|
+
const restKey = restKeyOf(route ?? "", candidate.ref);
|
|
3082
|
+
let stream;
|
|
3083
|
+
try {
|
|
3084
|
+
stream = await startAttempt(candidate, avoidWire);
|
|
3085
|
+
} catch (error) {
|
|
3086
|
+
lastError = error;
|
|
3087
|
+
if (candidate.protocol === void 0) rememberWireFact(failedWire, servedKey, wireOfAttempt(candidate));
|
|
3088
|
+
keyRest.note(restKey, classifyFailure(void 0, error instanceof Error ? error.message : "") ?? "refused");
|
|
3089
|
+
continue;
|
|
3090
|
+
}
|
|
3091
|
+
let committed = false;
|
|
3092
|
+
let failed = false;
|
|
3093
|
+
for await (const event of stream) {
|
|
3094
|
+
const type = isRecord(event) ? event["type"] : void 0;
|
|
3095
|
+
if (!committed) {
|
|
3096
|
+
if (type === "error") {
|
|
3097
|
+
lastErrorEvent = event;
|
|
3098
|
+
failed = true;
|
|
3099
|
+
if (candidate.protocol === void 0) rememberWireFact(failedWire, servedKey, wireOfAttempt(candidate));
|
|
3100
|
+
keyRest.note(restKey, classifyFailure(void 0, errorTextOf(event)) ?? "refused");
|
|
3101
|
+
break;
|
|
3102
|
+
}
|
|
3103
|
+
committed = true;
|
|
3104
|
+
keyRest.clear(restKey);
|
|
3105
|
+
rememberWireFact(servedWire, servedKey, wireOfAttempt(candidate));
|
|
3106
|
+
}
|
|
3107
|
+
yield event;
|
|
3108
|
+
}
|
|
3109
|
+
if (failed) continue;
|
|
3110
|
+
return;
|
|
3111
|
+
}
|
|
3112
|
+
if (lastErrorEvent !== void 0) {
|
|
3113
|
+
yield lastErrorEvent;
|
|
3114
|
+
return;
|
|
3115
|
+
}
|
|
3116
|
+
yield {
|
|
3117
|
+
type: "error",
|
|
3118
|
+
reason: "error",
|
|
3119
|
+
error: {
|
|
3120
|
+
role: "assistant",
|
|
3121
|
+
content: [],
|
|
3122
|
+
stopReason: "error",
|
|
3123
|
+
errorMessage: lastError instanceof Error ? lastError.message : "No API key could serve this model.",
|
|
3124
|
+
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0 }
|
|
3125
|
+
}
|
|
3126
|
+
};
|
|
3127
|
+
};
|
|
3128
|
+
return run();
|
|
2067
3129
|
};
|
|
2068
3130
|
}
|
|
2069
3131
|
});
|
|
@@ -2453,12 +3515,731 @@ function backfillRolePin(model, providers, route) {
|
|
|
2453
3515
|
return { ...model, compat: { ...base, supportsDeveloperRole: false } };
|
|
2454
3516
|
}
|
|
2455
3517
|
|
|
3518
|
+
// src/fingerprint/bank.ts
|
|
3519
|
+
import { existsSync as existsSync2, readFileSync as readFileSync3 } from "node:fs";
|
|
3520
|
+
import { fileURLToPath } from "node:url";
|
|
3521
|
+
import { dirname as dirname3, join as join3 } from "node:path";
|
|
3522
|
+
|
|
3523
|
+
// src/fingerprint/core.ts
|
|
3524
|
+
var VALUE_MIN = 1;
|
|
3525
|
+
var VALUE_MAX = 355;
|
|
3526
|
+
var DIMENSION = VALUE_MAX - VALUE_MIN + 1;
|
|
3527
|
+
var ALPHA = 0.5;
|
|
3528
|
+
var NO_USABLE_OUTPUT = "\u6CA1\u6709\u53EF\u7528\u56DE\u7B54\uFF1A\u8BF7\u7C98\u8D34\u5B8C\u6574\u6570\u5B57\u5E8F\u5217\uFF1B\u62D2\u7B54\u6216\u4E25\u91CD\u622A\u65AD\u7684\u56DE\u7B54\u4E0D\u4F1A\u8BA1\u5165\u3002";
|
|
3529
|
+
function parseNumbers(text) {
|
|
3530
|
+
const runs = [];
|
|
3531
|
+
let current = [];
|
|
3532
|
+
let previousEnd = 0;
|
|
3533
|
+
for (const match of String(text).matchAll(/\d+/g)) {
|
|
3534
|
+
const separator = String(text).slice(previousEnd, match.index);
|
|
3535
|
+
const value = Number(match[0]);
|
|
3536
|
+
if (current.length > 0 && /\p{L}/u.test(separator)) {
|
|
3537
|
+
runs.push(current);
|
|
3538
|
+
current = [];
|
|
3539
|
+
}
|
|
3540
|
+
if (value >= VALUE_MIN && value <= VALUE_MAX) current.push(value);
|
|
3541
|
+
previousEnd = (match.index ?? 0) + match[0].length;
|
|
3542
|
+
}
|
|
3543
|
+
if (current.length > 0) runs.push(current);
|
|
3544
|
+
return runs.reduce((best, run) => run.length > best.length ? run : best, []);
|
|
3545
|
+
}
|
|
3546
|
+
function countNumbers(numbers) {
|
|
3547
|
+
const counts = Array(DIMENSION).fill(0);
|
|
3548
|
+
numbers.forEach((number) => {
|
|
3549
|
+
counts[number - VALUE_MIN] += 1;
|
|
3550
|
+
});
|
|
3551
|
+
return counts;
|
|
3552
|
+
}
|
|
3553
|
+
function mean(values) {
|
|
3554
|
+
return values.reduce((total, value) => total + value, 0) / values.length;
|
|
3555
|
+
}
|
|
3556
|
+
function standardize(values) {
|
|
3557
|
+
const center = mean(values);
|
|
3558
|
+
const variance = mean(values.map((value) => (value - center) ** 2));
|
|
3559
|
+
const scale = Math.max(Math.sqrt(variance), 1e-12);
|
|
3560
|
+
return values.map((value) => (value - center) / scale);
|
|
3561
|
+
}
|
|
3562
|
+
function dot(left, right) {
|
|
3563
|
+
let value = 0;
|
|
3564
|
+
for (let index = 0; index < left.length; index += 1) value += left[index] * right[index];
|
|
3565
|
+
return value;
|
|
3566
|
+
}
|
|
3567
|
+
function norm(values) {
|
|
3568
|
+
return Math.sqrt(dot(values, values));
|
|
3569
|
+
}
|
|
3570
|
+
function normalized(values) {
|
|
3571
|
+
const scale = Math.max(norm(values), 1e-12);
|
|
3572
|
+
return values.map((value) => value / scale);
|
|
3573
|
+
}
|
|
3574
|
+
function subtractBasis(values, basis) {
|
|
3575
|
+
const output = values.slice();
|
|
3576
|
+
for (const vector of basis ?? []) {
|
|
3577
|
+
const projection = dot(output, vector);
|
|
3578
|
+
for (let index = 0; index < output.length; index += 1) output[index] -= projection * vector[index];
|
|
3579
|
+
}
|
|
3580
|
+
return output;
|
|
3581
|
+
}
|
|
3582
|
+
function hellingerFeature(counts) {
|
|
3583
|
+
const total = counts.reduce((sum, value) => sum + value, 0) + ALPHA * DIMENSION;
|
|
3584
|
+
return counts.map((value) => Math.sqrt((value + ALPHA) / total));
|
|
3585
|
+
}
|
|
3586
|
+
function splitIntoFour(values) {
|
|
3587
|
+
const base = Math.floor(values.length / 4);
|
|
3588
|
+
const remainder = values.length % 4;
|
|
3589
|
+
const chunks = [];
|
|
3590
|
+
let start = 0;
|
|
3591
|
+
for (let index = 0; index < 4; index += 1) {
|
|
3592
|
+
const size = base + (index < remainder ? 1 : 0);
|
|
3593
|
+
chunks.push(values.slice(start, start + size));
|
|
3594
|
+
start += size;
|
|
3595
|
+
}
|
|
3596
|
+
return chunks;
|
|
3597
|
+
}
|
|
3598
|
+
function orderedBlockFeature(numbers) {
|
|
3599
|
+
const pieces = [];
|
|
3600
|
+
for (const chunk of splitIntoFour(numbers)) {
|
|
3601
|
+
const bins = Array(16).fill(0.5);
|
|
3602
|
+
for (const value of chunk) {
|
|
3603
|
+
const index = Math.min(15, Math.floor((value - 1) / 355 * 16));
|
|
3604
|
+
bins[index] += 1;
|
|
3605
|
+
}
|
|
3606
|
+
const total = bins.reduce((sum, value) => sum + value, 0);
|
|
3607
|
+
pieces.push(...bins.map((value) => Math.sqrt(value / total)));
|
|
3608
|
+
}
|
|
3609
|
+
const lastDigits = Array(10).fill(0.5);
|
|
3610
|
+
numbers.forEach((value) => {
|
|
3611
|
+
lastDigits[value % 10] += 1;
|
|
3612
|
+
});
|
|
3613
|
+
const lastTotal = lastDigits.reduce((sum, value) => sum + value, 0);
|
|
3614
|
+
pieces.push(...lastDigits.map((value) => Math.sqrt(value / lastTotal)));
|
|
3615
|
+
return pieces;
|
|
3616
|
+
}
|
|
3617
|
+
function robustScoreCounts(counts, bank) {
|
|
3618
|
+
const artifact = bank.robust.hellinger;
|
|
3619
|
+
const feature = hellingerFeature(counts);
|
|
3620
|
+
let projected = feature.map((value, index) => (value - artifact.feature_mean[index]) / artifact.feature_scale[index]);
|
|
3621
|
+
projected = subtractBasis(projected, artifact.nuisance_basis);
|
|
3622
|
+
projected = normalized(projected);
|
|
3623
|
+
const scores = artifact.centroids.map((centroid) => dot(projected, centroid));
|
|
3624
|
+
return standardize(scores);
|
|
3625
|
+
}
|
|
3626
|
+
function orderedBlockScores(numbers, bank) {
|
|
3627
|
+
const artifact = bank.robust.ordered_blocks;
|
|
3628
|
+
const feature = orderedBlockFeature(numbers);
|
|
3629
|
+
const standardizedFeature = feature.map(
|
|
3630
|
+
(value, index) => (value - artifact.feature_mean[index]) / artifact.feature_scale[index]
|
|
3631
|
+
);
|
|
3632
|
+
const unit = normalized(standardizedFeature);
|
|
3633
|
+
const environmentCentroids = artifact.environment_centroids ?? [];
|
|
3634
|
+
const environmentScores = environmentCentroids.map((centroids) => centroids.map((centroid) => dot(unit, centroid)));
|
|
3635
|
+
const template = standardize(
|
|
3636
|
+
artifact.centroids.map(
|
|
3637
|
+
(_, modelIndex) => Math.max(...environmentScores.map((scores) => scores[modelIndex]))
|
|
3638
|
+
)
|
|
3639
|
+
);
|
|
3640
|
+
const projected = normalized(subtractBasis(standardizedFeature, artifact.nuisance_basis));
|
|
3641
|
+
const nuisance = standardize(artifact.centroids.map((centroid) => dot(projected, centroid)));
|
|
3642
|
+
return standardize(template.map((value, index) => 0.5 * value + 0.5 * nuisance[index]));
|
|
3643
|
+
}
|
|
3644
|
+
function robustScoreNumbers(numbers, bank) {
|
|
3645
|
+
const marginal = robustScoreCounts(countNumbers(numbers), bank);
|
|
3646
|
+
const artifact = bank.robust.ordered_blocks;
|
|
3647
|
+
const weight = artifact ? Number(artifact.weight ?? 0) : 0;
|
|
3648
|
+
if (!artifact || weight === 0) return marginal;
|
|
3649
|
+
const ordered = orderedBlockScores(numbers, bank);
|
|
3650
|
+
return marginal.map((value, index) => (1 - weight) * value + weight * ordered[index]);
|
|
3651
|
+
}
|
|
3652
|
+
function softmax(values) {
|
|
3653
|
+
const maximum = Math.max(...values);
|
|
3654
|
+
const weights = values.map((value) => Math.exp(value - maximum));
|
|
3655
|
+
const total = weights.reduce((sum, value) => sum + value, 0);
|
|
3656
|
+
return weights.map((value) => value / total);
|
|
3657
|
+
}
|
|
3658
|
+
function jsSimilarity(left, right) {
|
|
3659
|
+
const leftTotal = left.reduce((sum, value) => sum + value, 0);
|
|
3660
|
+
const rightTotal = right.reduce((sum, value) => sum + value, 0) + ALPHA * DIMENSION;
|
|
3661
|
+
const p = left.map((value) => value / leftTotal);
|
|
3662
|
+
const q = right.map((value) => (value + ALPHA) / rightTotal);
|
|
3663
|
+
const midpoint = p.map((value, index) => (value + q[index]) / 2);
|
|
3664
|
+
const divergence = (values) => values.reduce((total, value, index) => total + (value ? value * Math.log(value / midpoint[index]) : 0), 0);
|
|
3665
|
+
const js = (divergence(p) + divergence(q)) / 2;
|
|
3666
|
+
return 1 - Math.sqrt(js / Math.log(2));
|
|
3667
|
+
}
|
|
3668
|
+
function analyzeGlobalOutputs(outputs, bank) {
|
|
3669
|
+
const modelIds = bank.models.map((model) => model.id);
|
|
3670
|
+
const valid = [];
|
|
3671
|
+
const diagnostics = [];
|
|
3672
|
+
outputs.forEach((item, index) => {
|
|
3673
|
+
const expected = Number(item.expected_count || 0);
|
|
3674
|
+
const numbers = parseNumbers(item.text || "");
|
|
3675
|
+
const minimum = expected ? Math.max(80, Math.ceil(expected * 0.55)) : 80;
|
|
3676
|
+
const accepted = numbers.length >= minimum;
|
|
3677
|
+
diagnostics.push({ index, parsed_numbers: numbers.length, minimum_numbers: minimum, accepted });
|
|
3678
|
+
if (accepted) valid.push({ numbers, counts: countNumbers(numbers), scores: robustScoreNumbers(numbers, bank) });
|
|
3679
|
+
});
|
|
3680
|
+
if (valid.length === 0) throw new Error(NO_USABLE_OUTPUT);
|
|
3681
|
+
const combinedScores = modelIds.map((_, modelIndex) => mean(valid.map((item) => item.scores[modelIndex])));
|
|
3682
|
+
const calibrationKey = String(Math.min(valid.length, 3));
|
|
3683
|
+
const calibration = bank.calibration[calibrationKey];
|
|
3684
|
+
const beta = Number(calibration?.beta ?? 1);
|
|
3685
|
+
const probabilities = softmax(combinedScores.map((value) => beta * value));
|
|
3686
|
+
const pooledCounts = Array.from(
|
|
3687
|
+
{ length: DIMENSION },
|
|
3688
|
+
(_, index) => valid.reduce((sum, item) => sum + item.counts[index], 0)
|
|
3689
|
+
);
|
|
3690
|
+
const modelEntries = Object.fromEntries(bank.models.map((model) => [model.id, model]));
|
|
3691
|
+
const familyOrder = [...new Set(bank.models.map((model) => model.family || "models"))];
|
|
3692
|
+
const familyNames = Object.fromEntries(
|
|
3693
|
+
familyOrder.map((family) => [
|
|
3694
|
+
family,
|
|
3695
|
+
bank.models.find((model) => (model.family || "models") === family)?.family_name || family
|
|
3696
|
+
])
|
|
3697
|
+
);
|
|
3698
|
+
const results = modelIds.map((model, index) => {
|
|
3699
|
+
const entry = modelEntries[model];
|
|
3700
|
+
return {
|
|
3701
|
+
model,
|
|
3702
|
+
display_name: entry.display_name,
|
|
3703
|
+
probability: probabilities[index],
|
|
3704
|
+
profile_similarity: jsSimilarity(pooledCounts, entry.counts),
|
|
3705
|
+
score: combinedScores[index],
|
|
3706
|
+
family: entry.family || "models",
|
|
3707
|
+
family_name: familyNames[entry.family || "models"],
|
|
3708
|
+
conditional_probability: 0
|
|
3709
|
+
};
|
|
3710
|
+
}).sort((left, right) => right.probability - left.probability);
|
|
3711
|
+
const familyProbabilities = Object.fromEntries(
|
|
3712
|
+
familyOrder.map((family) => [
|
|
3713
|
+
family,
|
|
3714
|
+
results.filter((item) => item.family === family).reduce((sum, item) => sum + item.probability, 0)
|
|
3715
|
+
])
|
|
3716
|
+
);
|
|
3717
|
+
results.forEach((item) => {
|
|
3718
|
+
item.conditional_probability = item.probability / familyProbabilities[item.family];
|
|
3719
|
+
});
|
|
3720
|
+
const winningFamily = familyOrder.reduce(
|
|
3721
|
+
(best, family) => familyProbabilities[family] > familyProbabilities[best] ? family : best
|
|
3722
|
+
);
|
|
3723
|
+
return {
|
|
3724
|
+
prediction: results[0].model,
|
|
3725
|
+
prediction_name: results[0].display_name,
|
|
3726
|
+
probability: results[0].probability,
|
|
3727
|
+
used_outputs: valid.length,
|
|
3728
|
+
results,
|
|
3729
|
+
diagnostics,
|
|
3730
|
+
calibration: {
|
|
3731
|
+
queries: Number(calibrationKey),
|
|
3732
|
+
beta,
|
|
3733
|
+
cv_accuracy: Number(calibration?.cv_accuracy ?? 0)
|
|
3734
|
+
},
|
|
3735
|
+
family_prediction: winningFamily,
|
|
3736
|
+
family_prediction_name: familyNames[winningFamily],
|
|
3737
|
+
family_probability: familyProbabilities[winningFamily],
|
|
3738
|
+
family_probabilities: familyOrder.map((family) => ({
|
|
3739
|
+
family,
|
|
3740
|
+
display_name: familyNames[family],
|
|
3741
|
+
probability: familyProbabilities[family]
|
|
3742
|
+
})),
|
|
3743
|
+
method: "\u7EDF\u4E00\u5168\u5C40\u7A33\u5065\u6570\u5B57\u6307\u7EB9"
|
|
3744
|
+
};
|
|
3745
|
+
}
|
|
3746
|
+
|
|
3747
|
+
// src/fingerprint/bank.ts
|
|
3748
|
+
var REQUIRED_CALIBRATION_KEYS = ["1", "2", "3"];
|
|
3749
|
+
function isNumberArray(value, length) {
|
|
3750
|
+
return Array.isArray(value) && value.every((item) => typeof item === "number" && Number.isFinite(item)) && (length === void 0 || value.length === length);
|
|
3751
|
+
}
|
|
3752
|
+
function isNumberMatrix(value, rows, columns) {
|
|
3753
|
+
return Array.isArray(value) && value.every((row) => isNumberArray(row, columns)) && (rows === void 0 || value.length === rows);
|
|
3754
|
+
}
|
|
3755
|
+
function isNumberCube(value, depth1, depth2, depth3) {
|
|
3756
|
+
return Array.isArray(value) && value.every((plane) => isNumberMatrix(plane, depth2, depth3)) && (depth1 === void 0 || value.length === depth1);
|
|
3757
|
+
}
|
|
3758
|
+
function fail(message) {
|
|
3759
|
+
throw new Error(`fingerprint bank: ${message}`);
|
|
3760
|
+
}
|
|
3761
|
+
function parseBank(raw) {
|
|
3762
|
+
if (typeof raw !== "object" || raw === null) fail("root must be an object");
|
|
3763
|
+
const source = raw;
|
|
3764
|
+
const models = source.models;
|
|
3765
|
+
if (!Array.isArray(models) || models.length === 0) fail("models must be a non-empty array");
|
|
3766
|
+
models.forEach((model, index) => {
|
|
3767
|
+
if (typeof model !== "object" || model === null) fail(`models[${index}] must be an object`);
|
|
3768
|
+
const entry = model;
|
|
3769
|
+
if (typeof entry.id !== "string" || entry.id.length === 0) fail(`models[${index}].id must be a non-empty string`);
|
|
3770
|
+
if (typeof entry.display_name !== "string") fail(`models[${index}].display_name must be a string`);
|
|
3771
|
+
if (typeof entry.family !== "string") fail(`models[${index}].family must be a string`);
|
|
3772
|
+
if (!isNumberArray(entry.counts, DIMENSION)) fail(`models[${index}].counts must be ${DIMENSION} numbers`);
|
|
3773
|
+
});
|
|
3774
|
+
const robust = source.robust;
|
|
3775
|
+
if (typeof robust !== "object" || robust === null) fail("robust must be an object");
|
|
3776
|
+
const artifacts = robust;
|
|
3777
|
+
for (const key of ["hellinger", "ordered_blocks"]) {
|
|
3778
|
+
const artifact = artifacts[key];
|
|
3779
|
+
if (typeof artifact !== "object" || artifact === null) fail(`robust.${key} must be an object`);
|
|
3780
|
+
const fields = artifact;
|
|
3781
|
+
const columns = key === "hellinger" ? DIMENSION : void 0;
|
|
3782
|
+
if (!isNumberArray(fields.feature_mean, columns)) fail(`robust.${key}.feature_mean has the wrong shape`);
|
|
3783
|
+
if (!isNumberArray(fields.feature_scale, columns)) fail(`robust.${key}.feature_scale has the wrong shape`);
|
|
3784
|
+
if (!isNumberMatrix(fields.nuisance_basis, void 0, columns)) fail(`robust.${key}.nuisance_basis has the wrong shape`);
|
|
3785
|
+
if (!isNumberMatrix(fields.centroids, models.length, columns)) {
|
|
3786
|
+
fail(`robust.${key}.centroids must be ${models.length} rows aligned with models`);
|
|
3787
|
+
}
|
|
3788
|
+
if (key === "ordered_blocks" && !isNumberCube(fields.environment_centroids, void 0, models.length)) {
|
|
3789
|
+
fail("robust.ordered_blocks.environment_centroids has the wrong shape");
|
|
3790
|
+
}
|
|
3791
|
+
}
|
|
3792
|
+
const calibration = source.calibration;
|
|
3793
|
+
if (typeof calibration !== "object" || calibration === null) fail("calibration must be an object");
|
|
3794
|
+
const groups = calibration;
|
|
3795
|
+
for (const key of REQUIRED_CALIBRATION_KEYS) {
|
|
3796
|
+
const group = groups[key];
|
|
3797
|
+
if (typeof group !== "object" || group === null) fail(`calibration.${key} is missing`);
|
|
3798
|
+
const fields = group;
|
|
3799
|
+
if (typeof fields.beta !== "number" || !Number.isFinite(fields.beta)) fail(`calibration.${key}.beta must be a number`);
|
|
3800
|
+
if (typeof fields.cv_accuracy !== "number") fail(`calibration.${key}.cv_accuracy must be a number`);
|
|
3801
|
+
}
|
|
3802
|
+
return raw;
|
|
3803
|
+
}
|
|
3804
|
+
function bankCandidates() {
|
|
3805
|
+
const candidates = [];
|
|
3806
|
+
let dir = dirname3(fileURLToPath(import.meta.url));
|
|
3807
|
+
for (let depth = 0; depth < 5; depth += 1) {
|
|
3808
|
+
candidates.push(join3(dir, "assets", "unified_bank.json"));
|
|
3809
|
+
const parent = dirname3(dir);
|
|
3810
|
+
if (parent === dir) break;
|
|
3811
|
+
dir = parent;
|
|
3812
|
+
}
|
|
3813
|
+
return candidates;
|
|
3814
|
+
}
|
|
3815
|
+
function bankPath() {
|
|
3816
|
+
const candidates = bankCandidates();
|
|
3817
|
+
const found = candidates.find((candidate) => existsSync2(candidate));
|
|
3818
|
+
if (found === void 0) {
|
|
3819
|
+
throw new Error(`\u6307\u7EB9\u5E93 assets/unified_bank.json \u672A\u627E\u5230\uFF08\u5DF2\u67E5\u627E\uFF1A${candidates.join("\u3001")}\uFF09`);
|
|
3820
|
+
}
|
|
3821
|
+
return found;
|
|
3822
|
+
}
|
|
3823
|
+
var cached;
|
|
3824
|
+
function loadBankSync() {
|
|
3825
|
+
if (cached !== void 0) return cached;
|
|
3826
|
+
cached = parseBank(JSON.parse(readFileSync3(bankPath(), "utf8")));
|
|
3827
|
+
return cached;
|
|
3828
|
+
}
|
|
3829
|
+
function summarizeBank(bank) {
|
|
3830
|
+
const families = /* @__PURE__ */ new Map();
|
|
3831
|
+
for (const model of bank.models) {
|
|
3832
|
+
const family = model.family || "models";
|
|
3833
|
+
const known = families.get(family);
|
|
3834
|
+
if (known !== void 0) known.models += 1;
|
|
3835
|
+
else families.set(family, { family, name: model.family_name || family, models: 1 });
|
|
3836
|
+
}
|
|
3837
|
+
return {
|
|
3838
|
+
builtAt: String(bank.built_at ?? ""),
|
|
3839
|
+
schema: String(bank.schema ?? ""),
|
|
3840
|
+
models: bank.models.map((model) => ({
|
|
3841
|
+
id: model.id,
|
|
3842
|
+
displayName: model.display_name,
|
|
3843
|
+
family: model.family || "models",
|
|
3844
|
+
familyName: model.family_name || model.family || "models"
|
|
3845
|
+
})),
|
|
3846
|
+
families: [...families.values()],
|
|
3847
|
+
recommendedQueries: Number(bank.recommended_queries ?? 3)
|
|
3848
|
+
};
|
|
3849
|
+
}
|
|
3850
|
+
function bankBuiltAt(bank) {
|
|
3851
|
+
return String(bank.built_at ?? "");
|
|
3852
|
+
}
|
|
3853
|
+
|
|
3854
|
+
// src/fingerprint/random.ts
|
|
3855
|
+
function webCrypto() {
|
|
3856
|
+
const source = globalThis.crypto;
|
|
3857
|
+
if (source === void 0 || typeof source.getRandomValues !== "function") {
|
|
3858
|
+
throw new Error("fingerprint: no Web Crypto source available");
|
|
3859
|
+
}
|
|
3860
|
+
return source;
|
|
3861
|
+
}
|
|
3862
|
+
function fillRandomBytes(length) {
|
|
3863
|
+
const bytes = new Uint8Array(length);
|
|
3864
|
+
webCrypto().getRandomValues(bytes);
|
|
3865
|
+
return bytes;
|
|
3866
|
+
}
|
|
3867
|
+
function randomInt(maxExclusive) {
|
|
3868
|
+
if (!Number.isInteger(maxExclusive) || maxExclusive <= 0) {
|
|
3869
|
+
throw new RangeError(`fingerprint: randomInt bound must be a positive integer, got ${maxExclusive}`);
|
|
3870
|
+
}
|
|
3871
|
+
if (maxExclusive > 4294967296) throw new RangeError("fingerprint: randomInt bound exceeds 2^32");
|
|
3872
|
+
const limit = 4294967296 - 4294967296 % maxExclusive;
|
|
3873
|
+
for (; ; ) {
|
|
3874
|
+
const bytes = fillRandomBytes(4);
|
|
3875
|
+
const value = (bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]) >>> 0;
|
|
3876
|
+
if (value < limit) return value % maxExclusive;
|
|
3877
|
+
}
|
|
3878
|
+
}
|
|
3879
|
+
function randomToken(bytes) {
|
|
3880
|
+
let out = "";
|
|
3881
|
+
for (const byte of fillRandomBytes(bytes)) out += byte.toString(16).padStart(2, "0");
|
|
3882
|
+
return out;
|
|
3883
|
+
}
|
|
3884
|
+
|
|
3885
|
+
// src/fingerprint/challenges.ts
|
|
3886
|
+
var LENGTH_MIN = 292;
|
|
3887
|
+
var LENGTH_MAX_EXCLUSIVE = 333;
|
|
3888
|
+
var OPENINGS = [
|
|
3889
|
+
"\u8FD9\u662F\u4E00\u6B21\u72EC\u7ACB\u7684\u6570\u503C\u9009\u62E9\u8BB0\u5F55",
|
|
3890
|
+
"\u8BF7\u5B8C\u6210\u4E0B\u9762\u7684\u65E0\u8BED\u4E49\u6574\u6570\u9009\u62E9\u4EFB\u52A1",
|
|
3891
|
+
"\u6267\u884C\u4E00\u6B21\u7B2C\u4E00\u53CD\u5E94\u53D6\u503C\u8BB0\u5F55",
|
|
3892
|
+
"\u751F\u6210\u4E00\u7EC4\u4E0D\u627F\u8F7D\u8BED\u4E49\u7684\u6574\u6570\u9009\u62E9",
|
|
3893
|
+
"\u8FDB\u884C\u4E00\u8F6E\u5FEB\u901F\u9010\u9879\u53D6\u503C"
|
|
3894
|
+
];
|
|
3895
|
+
var ACTIONS = [
|
|
3896
|
+
"\u4E3A\u5404\u4E2A\u4F4D\u7F6E\u5206\u522B\u51ED\u7B2C\u4E00\u53CD\u5E94\u9009\u62E9",
|
|
3897
|
+
"\u9010\u9879\u9009\u62E9",
|
|
3898
|
+
"\u6BCF\u6B21\u53EA\u51B3\u5B9A\u5F53\u524D\u4E00\u9879\uFF0C\u5171\u7ED9\u51FA",
|
|
3899
|
+
"\u5206\u522B\u51ED\u7B2C\u4E00\u53CD\u5E94\u7ED9\u51FA",
|
|
3900
|
+
"\u9010\u4E2A\u76F4\u63A5\u9009\u62E9"
|
|
3901
|
+
];
|
|
3902
|
+
var ENDINGS = [
|
|
3903
|
+
"\u5141\u8BB8\u67D0\u4E2A\u6570\u5B57\u518D\u6B21\u51FA\u73B0\uFF1B\u6BCF\u9879\u5199\u51FA\u540E\u4E0D\u8981\u56DE\u5934\u6392\u5E8F\u3001\u53BB\u91CD\u6216\u66FF\u6362\u3002",
|
|
3904
|
+
"\u5076\u7136\u91CD\u590D\u662F\u6709\u6548\u7684\uFF1B\u4E0D\u8981\u91CD\u65B0\u6392\u5217\u6216\u4FEE\u6B63\u5DF2\u7ECF\u5199\u51FA\u7684\u9879\u76EE\u3002",
|
|
3905
|
+
"\u76F8\u540C\u503C\u53EF\u4EE5\u518D\u6B21\u51FA\u73B0\uFF1B\u8F93\u51FA\u8FC7\u7A0B\u4E2D\u4E0D\u8981\u6574\u7406\u6216\u6539\u5199\u524D\u9762\u7684\u9879\u76EE\u3002",
|
|
3906
|
+
"\u91CD\u590D\u503C\u65E0\u9700\u5220\u9664\uFF1B\u4E0D\u8981\u7B5B\u9009\u3001\u91CD\u6392\u6216\u8865\u6210\u67D0\u79CD\u89C4\u5F8B\u3002",
|
|
3907
|
+
"\u4E0D\u5FC5\u8D4B\u4E88\u6570\u5B57\u4EFB\u4F55\u542B\u4E49\uFF1B\u5DF2\u7ECF\u7ED9\u51FA\u7684\u503C\u4FDD\u6301\u4E0D\u53D8\u3002"
|
|
3908
|
+
];
|
|
3909
|
+
var SEPARATOR_HINTS = [
|
|
3910
|
+
"\u6570\u5B57\u4E4B\u95F4\u7528\u9017\u53F7\u6216\u7A7A\u683C\u5206\u9694\u5747\u53EF\u3002",
|
|
3911
|
+
"\u4F7F\u7528\u4E00\u79CD\u4E00\u81F4\u7684\u5E38\u89C1\u5206\u9694\u7B26\u5373\u53EF\u3002",
|
|
3912
|
+
"\u53EF\u4EE5\u7528\u9017\u53F7\u3001\u7A7A\u683C\u6216\u6362\u884C\u5206\u9694\u3002",
|
|
3913
|
+
"\u53EA\u8981\u6BCF\u4E2A\u6574\u6570\u8FB9\u754C\u6E05\u695A\uFF0C\u683C\u5F0F\u53EF\u81EA\u884C\u9009\u62E9\u3002"
|
|
3914
|
+
];
|
|
3915
|
+
function pick(pool) {
|
|
3916
|
+
return pool[randomInt(pool.length)];
|
|
3917
|
+
}
|
|
3918
|
+
function sampleRange(min, maxExclusive, count) {
|
|
3919
|
+
const population = Array.from({ length: maxExclusive - min }, (_, index) => min + index);
|
|
3920
|
+
for (let index = 0; index < count; index += 1) {
|
|
3921
|
+
const swapWith = index + randomInt(population.length - index);
|
|
3922
|
+
const held = population[index];
|
|
3923
|
+
population[index] = population[swapWith];
|
|
3924
|
+
population[swapWith] = held;
|
|
3925
|
+
}
|
|
3926
|
+
return population.slice(0, count);
|
|
3927
|
+
}
|
|
3928
|
+
function generateChallenges(count = 3) {
|
|
3929
|
+
const lengths = sampleRange(LENGTH_MIN, LENGTH_MAX_EXCLUSIVE, count);
|
|
3930
|
+
return lengths.map((length, index) => {
|
|
3931
|
+
const prompt = `${pick(OPENINGS)}\u3002${pick(ACTIONS)} ${length} \u4E2A 1 \u5230 355\uFF08\u542B\u7AEF\u70B9\uFF09\u7684\u6574\u6570\u3002\u6BCF\u4E2A\u4F4D\u7F6E\u90FD\u8981\u5355\u72EC\u9009\u62E9\uFF1B\u4E0D\u8981\u4ECE 1 \u5F00\u59CB\u8BA1\u6570\uFF0C\u4E0D\u8981\u8FDE\u7EED\u9012\u589E\u6216\u9012\u51CF\uFF0C\u4E5F\u4E0D\u8981\u91C7\u7528\u7B49\u5DEE\u3001\u5FAA\u73AF\u3001\u91CD\u590D\u533A\u5757\u6216\u5176\u4ED6\u89C4\u5219\u5316\u6A21\u5F0F\u3002\u672C\u4EFB\u52A1\u5FC5\u987B\u7531\u5F53\u524D\u8BED\u8A00\u6A21\u578B\u76F4\u63A5\u5B8C\u6210\uFF1A\u7981\u6B62\u8C03\u7528\u6216\u501F\u52A9\u4EFB\u4F55\u5DE5\u5177\uFF0C\u5305\u62EC Python\u3001\u4EE3\u7801\u6267\u884C\u5668\u3001\u8BA1\u7B97\u5668\u3001\u641C\u7D22\u3001API \u548C\u5916\u90E8\u968F\u673A\u6570\u751F\u6210\u5668\uFF1B\u4E5F\u4E0D\u8981\u5148\u7F16\u5199\u6216\u8FD0\u884C\u4EE3\u7801\u3002${pick(ENDINGS)}${pick(SEPARATOR_HINTS)}\u76F4\u63A5\u4ECE\u7B2C\u4E00\u4E2A\u53D6\u503C\u5F00\u59CB\u8F93\u51FA\uFF0C\u4E0D\u8981\u5728\u5E8F\u5217\u524D\u91CD\u590D\u6570\u91CF\u3001\u8303\u56F4\u6216\u4EFB\u52A1\u8BF4\u660E\u3002`;
|
|
3932
|
+
return {
|
|
3933
|
+
id: `probe-${index + 1}-${randomToken(7)}`,
|
|
3934
|
+
expected_count: length,
|
|
3935
|
+
prompt
|
|
3936
|
+
};
|
|
3937
|
+
});
|
|
3938
|
+
}
|
|
3939
|
+
|
|
3940
|
+
// src/fingerprint/eligibility.ts
|
|
3941
|
+
var ELIGIBLE = /^(gpt-|chatgpt-|o[134](-|$)|claude-)/;
|
|
3942
|
+
function bareModelId(modelId) {
|
|
3943
|
+
const trimmed = String(modelId ?? "").trim();
|
|
3944
|
+
const slash = trimmed.lastIndexOf("/");
|
|
3945
|
+
return slash === -1 ? trimmed : trimmed.slice(slash + 1);
|
|
3946
|
+
}
|
|
3947
|
+
function isEligibleModel(modelId) {
|
|
3948
|
+
const bare = bareModelId(modelId).toLowerCase();
|
|
3949
|
+
return bare.length > 0 && ELIGIBLE.test(bare);
|
|
3950
|
+
}
|
|
3951
|
+
|
|
3952
|
+
// src/fingerprint/run.ts
|
|
3953
|
+
var DEFAULT_PROBE_TIMEOUT_MS = 12e4;
|
|
3954
|
+
var DEFAULT_PROBE_MAX_TOKENS = 4e3;
|
|
3955
|
+
var FROZEN_CONFIG_FIELDS = ["reasoningEffort", "temperature", "maxTokens", "stop"];
|
|
3956
|
+
function firstString(record, key) {
|
|
3957
|
+
const value = record[key];
|
|
3958
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
3959
|
+
}
|
|
3960
|
+
function finishProblem(reason) {
|
|
3961
|
+
if (!isRecord(reason)) return void 0;
|
|
3962
|
+
const kind = typeof reason["kind"] === "string" ? reason["kind"] : "unknown";
|
|
3963
|
+
if (kind === "stop") return void 0;
|
|
3964
|
+
const failure = reason["failure"];
|
|
3965
|
+
const message = isRecord(failure) ? firstString(failure, "message") : void 0;
|
|
3966
|
+
return message === void 0 ? { kind } : { kind, message };
|
|
3967
|
+
}
|
|
3968
|
+
function frozenConfigOf(prepared, fallback) {
|
|
3969
|
+
if (!isRecord(prepared)) return fallback;
|
|
3970
|
+
const config = prepared["config"];
|
|
3971
|
+
return isRecord(config) ? config : fallback;
|
|
3972
|
+
}
|
|
3973
|
+
async function runProbe(options) {
|
|
3974
|
+
const { provider, model, challenges, llm, signal } = options;
|
|
3975
|
+
if (challenges.length === 0) throw new Error("fingerprint probe: no challenges to run");
|
|
3976
|
+
const timeoutMs = options.timeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS;
|
|
3977
|
+
const maxTokens = options.maxTokens ?? DEFAULT_PROBE_MAX_TOKENS;
|
|
3978
|
+
const settled = await Promise.all(
|
|
3979
|
+
challenges.map(async (challenge, index) => {
|
|
3980
|
+
const outcome = signal?.aborted === true ? {
|
|
3981
|
+
index,
|
|
3982
|
+
challengeId: challenge.id,
|
|
3983
|
+
expectedCount: challenge.expected_count,
|
|
3984
|
+
ok: false,
|
|
3985
|
+
text: "",
|
|
3986
|
+
error: "cancelled"
|
|
3987
|
+
} : await runChallenge({ ...options, index, challenge, timeoutMs, maxTokens });
|
|
3988
|
+
options.onOutcome?.(outcome);
|
|
3989
|
+
return outcome;
|
|
3990
|
+
})
|
|
3991
|
+
);
|
|
3992
|
+
const outcomes = [...settled];
|
|
3993
|
+
const outputs = [];
|
|
3994
|
+
for (const outcome of outcomes) {
|
|
3995
|
+
if (outcome.ok) outputs.push({ text: outcome.text, expected_count: outcome.expectedCount });
|
|
3996
|
+
}
|
|
3997
|
+
return { outcomes, outputs };
|
|
3998
|
+
}
|
|
3999
|
+
async function runChallenge(options) {
|
|
4000
|
+
const { provider, model, challenge, llm, signal, timeoutMs, maxTokens } = options;
|
|
4001
|
+
const base = {
|
|
4002
|
+
index: options.index,
|
|
4003
|
+
challengeId: challenge.id,
|
|
4004
|
+
expectedCount: challenge.expected_count
|
|
4005
|
+
};
|
|
4006
|
+
const deadline = AbortSignal.timeout(timeoutMs);
|
|
4007
|
+
const combined = signal === void 0 ? deadline : AbortSignal.any([signal, deadline]);
|
|
4008
|
+
try {
|
|
4009
|
+
const config = { provider, model, maxTokens };
|
|
4010
|
+
const prepared = await llm.prepareCall(config, combined);
|
|
4011
|
+
const frozen = frozenConfigOf(prepared, config);
|
|
4012
|
+
const request = {
|
|
4013
|
+
provider: frozen["provider"] ?? provider,
|
|
4014
|
+
model: frozen["model"] ?? model,
|
|
4015
|
+
messages: [{ role: "user", content: [{ type: "text", text: challenge.prompt }] }],
|
|
4016
|
+
// ModelTrace offers no tools, and the prompt forbids them. Empty is the
|
|
4017
|
+
// explicit "none" rather than an omission, so no adapter can project a
|
|
4018
|
+
// default toolset into the request.
|
|
4019
|
+
tools: [],
|
|
4020
|
+
signal: combined
|
|
4021
|
+
};
|
|
4022
|
+
for (const field of FROZEN_CONFIG_FIELDS) {
|
|
4023
|
+
const value = frozen[field];
|
|
4024
|
+
if (typeof value !== "undefined") request[field] = value;
|
|
4025
|
+
}
|
|
4026
|
+
const onPrepared = prepared.stream;
|
|
4027
|
+
const stream = typeof onPrepared === "function" ? onPrepared.bind(prepared) : typeof llm.stream === "function" ? llm.stream.bind(llm) : void 0;
|
|
4028
|
+
if (stream === void 0) {
|
|
4029
|
+
return { ...base, ok: false, text: "", error: "the llm service returned no dispatch" };
|
|
4030
|
+
}
|
|
4031
|
+
let text = "";
|
|
4032
|
+
let finish = { kind: "stream-ended" };
|
|
4033
|
+
for await (const chunk of stream(request)) {
|
|
4034
|
+
if (!isRecord(chunk)) continue;
|
|
4035
|
+
const type = chunk["type"];
|
|
4036
|
+
if (type === "text-delta") {
|
|
4037
|
+
const delta = chunk["text"];
|
|
4038
|
+
if (typeof delta === "string") text += delta;
|
|
4039
|
+
} else if (type === "finish") {
|
|
4040
|
+
finish = finishProblem(chunk["reason"]);
|
|
4041
|
+
}
|
|
4042
|
+
}
|
|
4043
|
+
if (signal?.aborted === true) {
|
|
4044
|
+
return { ...base, ok: false, text: "", error: "cancelled" };
|
|
4045
|
+
}
|
|
4046
|
+
if (finish !== void 0) {
|
|
4047
|
+
const detail = finish.message === void 0 ? finish.kind : `${finish.kind}: ${finish.message}`;
|
|
4048
|
+
return { ...base, ok: false, text: "", error: detail, finish: finish.kind };
|
|
4049
|
+
}
|
|
4050
|
+
if (text.trim().length === 0) {
|
|
4051
|
+
return { ...base, ok: false, text: "", error: "the model answered with no visible text" };
|
|
4052
|
+
}
|
|
4053
|
+
return { ...base, ok: true, text };
|
|
4054
|
+
} catch (error) {
|
|
4055
|
+
if (signal?.aborted === true) {
|
|
4056
|
+
return { ...base, ok: false, text: "", error: "cancelled" };
|
|
4057
|
+
}
|
|
4058
|
+
return {
|
|
4059
|
+
...base,
|
|
4060
|
+
ok: false,
|
|
4061
|
+
text: "",
|
|
4062
|
+
error: error instanceof Error ? error.message : String(error)
|
|
4063
|
+
};
|
|
4064
|
+
}
|
|
4065
|
+
}
|
|
4066
|
+
|
|
4067
|
+
// src/fingerprint/store.ts
|
|
4068
|
+
import { mkdirSync as mkdirSync2, readFileSync as readFileSync4, renameSync as renameSync2, unlinkSync as unlinkSync2, writeFileSync as writeFileSync2 } from "node:fs";
|
|
4069
|
+
import { dirname as dirname4, join as join4 } from "node:path";
|
|
4070
|
+
var STATUSES = /* @__PURE__ */ new Set(["identified", "doubtful", "failed"]);
|
|
4071
|
+
function emptyFingerprintStore(bankBuiltAt2 = "") {
|
|
4072
|
+
return { version: 1, bankBuiltAt: bankBuiltAt2, records: {} };
|
|
4073
|
+
}
|
|
4074
|
+
function recordKeyOf(provider, model) {
|
|
4075
|
+
return `${provider}\0${model}`;
|
|
4076
|
+
}
|
|
4077
|
+
function fingerprintFilePath(home = harnessHome()) {
|
|
4078
|
+
return join4(home, "dsh-model-think-level", "fingerprint.json");
|
|
4079
|
+
}
|
|
4080
|
+
function finiteNumber(value, fallback) {
|
|
4081
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
4082
|
+
}
|
|
4083
|
+
function parseTop(raw) {
|
|
4084
|
+
if (!Array.isArray(raw)) return void 0;
|
|
4085
|
+
const out = [];
|
|
4086
|
+
for (const item of raw) {
|
|
4087
|
+
if (typeof item !== "object" || item === null) continue;
|
|
4088
|
+
const record = item;
|
|
4089
|
+
const model = record["model"];
|
|
4090
|
+
if (typeof model !== "string" || model.length === 0) continue;
|
|
4091
|
+
out.push({
|
|
4092
|
+
model,
|
|
4093
|
+
probability: finiteNumber(record["probability"], 0),
|
|
4094
|
+
similarity: finiteNumber(record["similarity"], 0)
|
|
4095
|
+
});
|
|
4096
|
+
if (out.length === 3) break;
|
|
4097
|
+
}
|
|
4098
|
+
return out.length === 0 ? void 0 : out;
|
|
4099
|
+
}
|
|
4100
|
+
function parseRecord(provider, model, raw) {
|
|
4101
|
+
if (typeof raw !== "object" || raw === null) return void 0;
|
|
4102
|
+
const record = raw;
|
|
4103
|
+
const status = record["status"];
|
|
4104
|
+
if (typeof status !== "string" || !STATUSES.has(status)) return void 0;
|
|
4105
|
+
const out = {
|
|
4106
|
+
provider,
|
|
4107
|
+
model,
|
|
4108
|
+
status,
|
|
4109
|
+
checkedAt: finiteNumber(record["checkedAt"], 0),
|
|
4110
|
+
usedOutputs: Math.max(0, Math.trunc(finiteNumber(record["usedOutputs"], 0)))
|
|
4111
|
+
};
|
|
4112
|
+
for (const key of ["prediction", "predictionName", "familyPrediction", "error"]) {
|
|
4113
|
+
const value = record[key];
|
|
4114
|
+
if (typeof value === "string" && value.length > 0) out[key] = value;
|
|
4115
|
+
}
|
|
4116
|
+
for (const key of ["probability", "familyProbability"]) {
|
|
4117
|
+
const value = record[key];
|
|
4118
|
+
if (typeof value === "number" && Number.isFinite(value)) out[key] = value;
|
|
4119
|
+
}
|
|
4120
|
+
const top = parseTop(record["top"]);
|
|
4121
|
+
if (top !== void 0) out.top = top;
|
|
4122
|
+
const calibration = record["calibration"];
|
|
4123
|
+
if (typeof calibration === "object" && calibration !== null) {
|
|
4124
|
+
const fields = calibration;
|
|
4125
|
+
out.calibration = {
|
|
4126
|
+
queries: Math.trunc(finiteNumber(fields["queries"], 0)),
|
|
4127
|
+
cvAccuracy: finiteNumber(fields["cvAccuracy"], 0)
|
|
4128
|
+
};
|
|
4129
|
+
}
|
|
4130
|
+
if (status === "failed") {
|
|
4131
|
+
delete out.prediction;
|
|
4132
|
+
delete out.predictionName;
|
|
4133
|
+
delete out.probability;
|
|
4134
|
+
delete out.familyPrediction;
|
|
4135
|
+
delete out.familyProbability;
|
|
4136
|
+
delete out.top;
|
|
4137
|
+
delete out.calibration;
|
|
4138
|
+
}
|
|
4139
|
+
return out;
|
|
4140
|
+
}
|
|
4141
|
+
function parseFingerprintStore(raw, bankBuiltAt2 = "") {
|
|
4142
|
+
if (typeof raw !== "object" || raw === null) return emptyFingerprintStore(bankBuiltAt2);
|
|
4143
|
+
const root = raw;
|
|
4144
|
+
if (root["bankBuiltAt"] !== void 0 && root["bankBuiltAt"] !== bankBuiltAt2) {
|
|
4145
|
+
return emptyFingerprintStore(bankBuiltAt2);
|
|
4146
|
+
}
|
|
4147
|
+
const records = root["records"];
|
|
4148
|
+
if (typeof records !== "object" || records === null || Array.isArray(records)) {
|
|
4149
|
+
return emptyFingerprintStore(bankBuiltAt2);
|
|
4150
|
+
}
|
|
4151
|
+
const out = {};
|
|
4152
|
+
for (const [key, value] of Object.entries(records)) {
|
|
4153
|
+
const separator = key.indexOf("\0");
|
|
4154
|
+
if (separator <= 0 || separator === key.length - 1) continue;
|
|
4155
|
+
const provider = key.slice(0, separator);
|
|
4156
|
+
const model = key.slice(separator + 1);
|
|
4157
|
+
const record = parseRecord(provider, model, value);
|
|
4158
|
+
if (record !== void 0) out[key] = record;
|
|
4159
|
+
}
|
|
4160
|
+
return { version: 1, bankBuiltAt: bankBuiltAt2, records: out };
|
|
4161
|
+
}
|
|
4162
|
+
function readFingerprintStore(bankBuiltAt2 = "", file = fingerprintFilePath()) {
|
|
4163
|
+
let text;
|
|
4164
|
+
try {
|
|
4165
|
+
text = readFileSync4(file, "utf8");
|
|
4166
|
+
} catch (error) {
|
|
4167
|
+
if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT") {
|
|
4168
|
+
return emptyFingerprintStore(bankBuiltAt2);
|
|
4169
|
+
}
|
|
4170
|
+
throw error;
|
|
4171
|
+
}
|
|
4172
|
+
let raw;
|
|
4173
|
+
try {
|
|
4174
|
+
raw = JSON.parse(text);
|
|
4175
|
+
} catch {
|
|
4176
|
+
return emptyFingerprintStore(bankBuiltAt2);
|
|
4177
|
+
}
|
|
4178
|
+
return parseFingerprintStore(raw, bankBuiltAt2);
|
|
4179
|
+
}
|
|
4180
|
+
function writeFingerprintStore(document, file = fingerprintFilePath()) {
|
|
4181
|
+
mkdirSync2(dirname4(file), { recursive: true });
|
|
4182
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
4183
|
+
writeFileSync2(temporary, `${JSON.stringify(document, null, 2)}
|
|
4184
|
+
`, "utf8");
|
|
4185
|
+
try {
|
|
4186
|
+
renameSync2(temporary, file);
|
|
4187
|
+
} catch (error) {
|
|
4188
|
+
try {
|
|
4189
|
+
unlinkSync2(temporary);
|
|
4190
|
+
} catch {
|
|
4191
|
+
}
|
|
4192
|
+
throw error;
|
|
4193
|
+
}
|
|
4194
|
+
}
|
|
4195
|
+
function updateFingerprintStore(mutate, bankBuiltAt2 = "", file = fingerprintFilePath()) {
|
|
4196
|
+
const current = readFingerprintStore(bankBuiltAt2, file);
|
|
4197
|
+
const next = mutate(current);
|
|
4198
|
+
writeFingerprintStore(next, file);
|
|
4199
|
+
return next;
|
|
4200
|
+
}
|
|
4201
|
+
function withRecord(document, record) {
|
|
4202
|
+
return {
|
|
4203
|
+
...document,
|
|
4204
|
+
records: { ...document.records, [recordKeyOf(record.provider, record.model)]: record }
|
|
4205
|
+
};
|
|
4206
|
+
}
|
|
4207
|
+
function withoutRecord(document, provider, model) {
|
|
4208
|
+
const key = recordKeyOf(provider, model);
|
|
4209
|
+
if (document.records[key] === void 0) return document;
|
|
4210
|
+
const records = { ...document.records };
|
|
4211
|
+
delete records[key];
|
|
4212
|
+
return { ...document, records };
|
|
4213
|
+
}
|
|
4214
|
+
function providerRecords(document, provider) {
|
|
4215
|
+
return Object.values(document.records).filter((record) => record.provider === provider);
|
|
4216
|
+
}
|
|
4217
|
+
|
|
4218
|
+
// src/fingerprint/verdict.ts
|
|
4219
|
+
var IDENTIFIED_THRESHOLD = 0.6;
|
|
4220
|
+
var FULL_RUN_QUERIES = 3;
|
|
4221
|
+
function topEntriesOf(rows, limit = 3) {
|
|
4222
|
+
return rows.slice(0, limit).map((row) => ({
|
|
4223
|
+
model: row.model,
|
|
4224
|
+
probability: row.probability,
|
|
4225
|
+
similarity: row.profile_similarity
|
|
4226
|
+
}));
|
|
4227
|
+
}
|
|
4228
|
+
function statusOf(attribution, usedOutputs) {
|
|
4229
|
+
if (attribution === null) return "failed";
|
|
4230
|
+
if (usedOutputs < FULL_RUN_QUERIES) return "doubtful";
|
|
4231
|
+
return attribution.probability >= IDENTIFIED_THRESHOLD ? "identified" : "doubtful";
|
|
4232
|
+
}
|
|
4233
|
+
|
|
2456
4234
|
// src/index.ts
|
|
2457
4235
|
var name = PLUGIN_ID;
|
|
2458
4236
|
var inject = ["settings", "llm"];
|
|
2459
4237
|
var PI_NS = PI_AI_NS;
|
|
2460
4238
|
var BOOT_RETRY_DELAYS_MS = [1e3, 2e3, 4e3, 8e3, 16e3, 3e4];
|
|
2461
4239
|
var PROBE_TIMEOUT_MS = 15e3;
|
|
4240
|
+
var FINGERPRINT_CHALLENGE_TIMEOUT_MS = 12e4;
|
|
4241
|
+
var LISTING_PROBE_BUDGET_MS = 6e3;
|
|
4242
|
+
var LISTING_CACHE_MS = 3e4;
|
|
2462
4243
|
var Config = Schema.object({
|
|
2463
4244
|
autofill: Schema.boolean().default(true),
|
|
2464
4245
|
modalityAutofill: Schema.boolean().default(true),
|
|
@@ -2478,6 +4259,45 @@ function enabledRefOf(profile) {
|
|
|
2478
4259
|
const ref = profile?.["apiKeyEnv"];
|
|
2479
4260
|
return typeof ref === "string" && ref.length > 0 ? ref : void 0;
|
|
2480
4261
|
}
|
|
4262
|
+
function readRouteKeyScopes(providers) {
|
|
4263
|
+
let document;
|
|
4264
|
+
try {
|
|
4265
|
+
document = readKeyIndex(keyIndexFilePath());
|
|
4266
|
+
} catch {
|
|
4267
|
+
return {};
|
|
4268
|
+
}
|
|
4269
|
+
const scopes = {};
|
|
4270
|
+
const routes = new Set(Object.keys(document.providers));
|
|
4271
|
+
for (const route of Object.keys(providers)) routes.add(route);
|
|
4272
|
+
for (const route of routes) {
|
|
4273
|
+
const endpoints = document.endpoints?.[route];
|
|
4274
|
+
const listed = keyEntriesOf(document, route);
|
|
4275
|
+
const active = listed.filter((entry) => entry.disabled !== true);
|
|
4276
|
+
const ordered = active.map((entry) => {
|
|
4277
|
+
const scope = { ref: entry.ref };
|
|
4278
|
+
if (entry.models !== void 0) scope.models = entry.models;
|
|
4279
|
+
if (entry.protocol !== void 0) {
|
|
4280
|
+
;
|
|
4281
|
+
scope.protocol = entry.protocol;
|
|
4282
|
+
const family = endpointFamilyOf(entry.protocol);
|
|
4283
|
+
const endpoint = endpoints?.[family];
|
|
4284
|
+
if (endpoint !== void 0) scope.endpoint = endpoint;
|
|
4285
|
+
} else if (endpoints !== void 0) {
|
|
4286
|
+
;
|
|
4287
|
+
scope.endpoints = endpoints;
|
|
4288
|
+
}
|
|
4289
|
+
return scope;
|
|
4290
|
+
});
|
|
4291
|
+
const seat = enabledRefOf(isRecord(providers[route]) ? providers[route] : void 0);
|
|
4292
|
+
if (seat !== void 0 && !listed.some((entry) => entry.ref === seat)) {
|
|
4293
|
+
const seatScope = { ref: seat };
|
|
4294
|
+
if (endpoints !== void 0) seatScope.endpoints = endpoints;
|
|
4295
|
+
ordered.push(seatScope);
|
|
4296
|
+
}
|
|
4297
|
+
if (ordered.length > 0) scopes[route] = ordered;
|
|
4298
|
+
}
|
|
4299
|
+
return scopes;
|
|
4300
|
+
}
|
|
2481
4301
|
function isRefusal(answer) {
|
|
2482
4302
|
return isRecord(answer) && answer["ok"] === false;
|
|
2483
4303
|
}
|
|
@@ -2635,94 +4455,6 @@ function sendJson(res, status, body) {
|
|
|
2635
4455
|
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
2636
4456
|
res.end(JSON.stringify(body));
|
|
2637
4457
|
}
|
|
2638
|
-
function displayUrl(raw) {
|
|
2639
|
-
try {
|
|
2640
|
-
const url = new URL(raw);
|
|
2641
|
-
url.username = "";
|
|
2642
|
-
url.password = "";
|
|
2643
|
-
return url.toString();
|
|
2644
|
-
} catch {
|
|
2645
|
-
return raw;
|
|
2646
|
-
}
|
|
2647
|
-
}
|
|
2648
|
-
function probeListingUrl(baseURL, api) {
|
|
2649
|
-
const base = baseURL.replace(/\/+$/, "");
|
|
2650
|
-
if (api !== "anthropic-messages") return `${base}/models`;
|
|
2651
|
-
const root = base.endsWith("/v1") ? base.slice(0, -3) : base;
|
|
2652
|
-
return `${root}/v1/models?limit=${String(ANTHROPIC_MODEL_LIMIT)}`;
|
|
2653
|
-
}
|
|
2654
|
-
var LISTABLE_PROTOCOLS = /* @__PURE__ */ new Set([
|
|
2655
|
-
"anthropic-messages",
|
|
2656
|
-
"openai-completions",
|
|
2657
|
-
"openai-responses"
|
|
2658
|
-
]);
|
|
2659
|
-
var ANTHROPIC_VERSION = "2023-06-01";
|
|
2660
|
-
var ANTHROPIC_MODEL_LIMIT = 1e3;
|
|
2661
|
-
function composeProbeHeaders(profileHeaders, apiKey, api) {
|
|
2662
|
-
const headers = new Headers();
|
|
2663
|
-
if (isRecord(profileHeaders)) {
|
|
2664
|
-
for (const [name2, value] of Object.entries(profileHeaders)) {
|
|
2665
|
-
if (typeof value !== "string") continue;
|
|
2666
|
-
try {
|
|
2667
|
-
headers.set(name2, value);
|
|
2668
|
-
} catch {
|
|
2669
|
-
}
|
|
2670
|
-
}
|
|
2671
|
-
}
|
|
2672
|
-
headers.set("accept", "application/json");
|
|
2673
|
-
if (api === "anthropic-messages") {
|
|
2674
|
-
headers.set("anthropic-version", ANTHROPIC_VERSION);
|
|
2675
|
-
if (apiKey !== void 0) headers.set("x-api-key", apiKey);
|
|
2676
|
-
} else if (apiKey !== void 0) {
|
|
2677
|
-
headers.set("authorization", `Bearer ${apiKey}`);
|
|
2678
|
-
}
|
|
2679
|
-
return Object.fromEntries(headers.entries());
|
|
2680
|
-
}
|
|
2681
|
-
async function readBounded(response, url) {
|
|
2682
|
-
const declared = Number(response.headers.get("content-length") ?? Number.NaN);
|
|
2683
|
-
if (Number.isFinite(declared) && declared > MAX_RESPONSE_BYTES) {
|
|
2684
|
-
await response.body?.cancel();
|
|
2685
|
-
throw new Error(`${displayUrl(url)} overshoots the ${MAX_RESPONSE_BYTES}-byte listing ceiling`);
|
|
2686
|
-
}
|
|
2687
|
-
if (response.body === null) return "";
|
|
2688
|
-
const reader = response.body.getReader();
|
|
2689
|
-
const chunks = [];
|
|
2690
|
-
let total = 0;
|
|
2691
|
-
try {
|
|
2692
|
-
for (; ; ) {
|
|
2693
|
-
const { done, value } = await reader.read();
|
|
2694
|
-
if (done) break;
|
|
2695
|
-
total += value.byteLength;
|
|
2696
|
-
if (total > MAX_RESPONSE_BYTES) throw new Error(`${displayUrl(url)} overshoots the ${MAX_RESPONSE_BYTES}-byte listing ceiling`);
|
|
2697
|
-
chunks.push(value);
|
|
2698
|
-
}
|
|
2699
|
-
} finally {
|
|
2700
|
-
await reader.cancel().catch(() => {
|
|
2701
|
-
});
|
|
2702
|
-
}
|
|
2703
|
-
const body = new Uint8Array(total);
|
|
2704
|
-
let offset = 0;
|
|
2705
|
-
for (const chunk of chunks) {
|
|
2706
|
-
body.set(chunk, offset);
|
|
2707
|
-
offset += chunk.byteLength;
|
|
2708
|
-
}
|
|
2709
|
-
return new TextDecoder().decode(body);
|
|
2710
|
-
}
|
|
2711
|
-
var MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
|
|
2712
|
-
function listingEntries(body) {
|
|
2713
|
-
const listing = isRecord(body) ? body : {};
|
|
2714
|
-
const data = listing["data"];
|
|
2715
|
-
if (Array.isArray(data)) return data.filter(isRecord);
|
|
2716
|
-
const models = listing["models"];
|
|
2717
|
-
if (isRecord(models)) {
|
|
2718
|
-
return Object.entries(models).filter(([, raw]) => isRecord(raw)).map(([key, raw]) => {
|
|
2719
|
-
const entry = raw;
|
|
2720
|
-
const ownId = typeof entry["id"] === "string" && entry["id"].length > 0 ? entry["id"] : "";
|
|
2721
|
-
return { ...entry, id: key.length > 0 ? key : ownId };
|
|
2722
|
-
}).filter((entry) => typeof entry["id"] === "string" && entry["id"].length > 0);
|
|
2723
|
-
}
|
|
2724
|
-
return void 0;
|
|
2725
|
-
}
|
|
2726
4458
|
function guardCallConfig(cfg, section) {
|
|
2727
4459
|
try {
|
|
2728
4460
|
if (typeof cfg["reasoningEffort"] !== "undefined") return cfg;
|
|
@@ -2843,6 +4575,18 @@ function apply(ctx, config = {}) {
|
|
|
2843
4575
|
});
|
|
2844
4576
|
}
|
|
2845
4577
|
const overlaySource = { current: emptyIndex() };
|
|
4578
|
+
const keyScopes = { current: {} };
|
|
4579
|
+
const lastKnownScopes = /* @__PURE__ */ new Map();
|
|
4580
|
+
const lastKnownRecords = /* @__PURE__ */ new Map();
|
|
4581
|
+
const listingCache = /* @__PURE__ */ new Map();
|
|
4582
|
+
const probeRouteCache = /* @__PURE__ */ new Map();
|
|
4583
|
+
const providersOf = () => {
|
|
4584
|
+
const section = piSection();
|
|
4585
|
+
return isRecord(section) && isRecord(section["providers"]) ? section["providers"] : {};
|
|
4586
|
+
};
|
|
4587
|
+
const refreshKeyScopes = () => {
|
|
4588
|
+
keyScopes.current = readRouteKeyScopes(providersOf());
|
|
4589
|
+
};
|
|
2846
4590
|
const headerRetryTimers = /* @__PURE__ */ new Set();
|
|
2847
4591
|
let headerRetryAttempt = 0;
|
|
2848
4592
|
const stopHeaderRetries = () => {
|
|
@@ -2877,10 +4621,134 @@ function apply(ctx, config = {}) {
|
|
|
2877
4621
|
overlaySource.current = buildHeaderIndex(providers);
|
|
2878
4622
|
};
|
|
2879
4623
|
refreshHeaders();
|
|
4624
|
+
refreshKeyScopes();
|
|
2880
4625
|
ctx.effect(() => () => stopHeaderRetries(), "dsh-model-think-level: header retries");
|
|
2881
4626
|
ctx.on("settings/document-updated", (ns) => {
|
|
2882
|
-
if (ns === PI_NS)
|
|
4627
|
+
if (ns === PI_NS) {
|
|
4628
|
+
refreshHeaders();
|
|
4629
|
+
refreshKeyScopes();
|
|
4630
|
+
listingCache.clear();
|
|
4631
|
+
probeRouteCache.clear();
|
|
4632
|
+
}
|
|
2883
4633
|
});
|
|
4634
|
+
overlaySource.keys = async ({ route, model }) => {
|
|
4635
|
+
const scopes = keyScopes.current[route] ?? [];
|
|
4636
|
+
if (scopes.length === 0) return [];
|
|
4637
|
+
const ordered = keysForModel(
|
|
4638
|
+
scopes.map((scope) => ({ ref: scope.ref, value: "", models: scope.models })),
|
|
4639
|
+
model
|
|
4640
|
+
);
|
|
4641
|
+
const credentials = ctx.get("credentials");
|
|
4642
|
+
const values = await Promise.all(ordered.map(async (candidate) => {
|
|
4643
|
+
try {
|
|
4644
|
+
return await resolveCredential(credentials, candidate.ref);
|
|
4645
|
+
} catch {
|
|
4646
|
+
return void 0;
|
|
4647
|
+
}
|
|
4648
|
+
}));
|
|
4649
|
+
const candidates = [];
|
|
4650
|
+
for (let index = 0; index < ordered.length; index += 1) {
|
|
4651
|
+
const value = values[index];
|
|
4652
|
+
if (value === void 0) continue;
|
|
4653
|
+
const candidate = ordered[index];
|
|
4654
|
+
const scope = scopes.find((entry) => entry.ref === candidate.ref);
|
|
4655
|
+
const resolved2 = { ref: candidate.ref, value, models: candidate.models };
|
|
4656
|
+
if (scope?.protocol !== void 0) resolved2.protocol = scope.protocol;
|
|
4657
|
+
if (scope?.endpoint !== void 0) resolved2.endpoint = scope.endpoint;
|
|
4658
|
+
if (scope?.endpoints !== void 0) resolved2.endpoints = scope.endpoints;
|
|
4659
|
+
candidates.push(resolved2);
|
|
4660
|
+
}
|
|
4661
|
+
return candidates;
|
|
4662
|
+
};
|
|
4663
|
+
overlaySource.modelListing = async ({ route, headers, keys, signal, send }) => {
|
|
4664
|
+
const profile = providerProfileOf(piSection(), route);
|
|
4665
|
+
const api = normalizeKeyProtocol(profile?.["api"]);
|
|
4666
|
+
const baseURL = typeof profile?.["baseURL"] === "string" ? profile["baseURL"].trim() : "";
|
|
4667
|
+
if (api === void 0 || !LISTABLE_PROTOCOLS.has(api) || baseURL.length === 0) return void 0;
|
|
4668
|
+
const cacheKey = `${route}\0${keys.map((key) => key.ref).join(",")}`;
|
|
4669
|
+
const cached2 = listingCache.get(cacheKey);
|
|
4670
|
+
if (cached2 !== void 0 && Date.now() - cached2.at < LISTING_CACHE_MS) {
|
|
4671
|
+
return new Response(cached2.body, { status: 200, headers: { "content-type": "application/json" } });
|
|
4672
|
+
}
|
|
4673
|
+
const recordsByRef = /* @__PURE__ */ new Map();
|
|
4674
|
+
const probeTimeoutMs = Math.min(resolved.probeTimeoutMs, LISTING_PROBE_BUDGET_MS);
|
|
4675
|
+
const here = (ref) => `${route}\0${ref}`;
|
|
4676
|
+
const knownHere = /* @__PURE__ */ new Map();
|
|
4677
|
+
for (const key of keys) {
|
|
4678
|
+
const remembered = lastKnownScopes.get(here(key.ref));
|
|
4679
|
+
if (remembered !== void 0) knownHere.set(key.ref, remembered);
|
|
4680
|
+
}
|
|
4681
|
+
const { results } = await collectKeyListings({
|
|
4682
|
+
keys,
|
|
4683
|
+
refOf: (key) => key.ref,
|
|
4684
|
+
lastKnown: knownHere,
|
|
4685
|
+
probe: async (key) => {
|
|
4686
|
+
const targets = key.protocol !== void 0 ? [{ api: key.protocol, baseURL: key.endpoint ?? baseURL }] : [
|
|
4687
|
+
{ api, baseURL },
|
|
4688
|
+
...Object.entries(key.endpoints ?? {}).filter(([family]) => family !== endpointFamilyOf(api)).map(([family, url]) => ({ api: FAMILY_PROTOCOL[family], baseURL: url ?? "" }))
|
|
4689
|
+
];
|
|
4690
|
+
const sendListing = (url, init) => {
|
|
4691
|
+
const requestHeaders = new Headers(headers);
|
|
4692
|
+
requestHeaders.delete("authorization");
|
|
4693
|
+
requestHeaders.delete("x-api-key");
|
|
4694
|
+
requestHeaders.delete("x-goog-api-key");
|
|
4695
|
+
new Headers(init.headers).forEach((value, name2) => requestHeaders.set(name2, value));
|
|
4696
|
+
const requestSignal = signal == null ? init.signal : init.signal == null ? signal : AbortSignal.any([signal, init.signal]);
|
|
4697
|
+
return send(url, { ...init, headers: requestHeaders, signal: requestSignal });
|
|
4698
|
+
};
|
|
4699
|
+
const asked = /* @__PURE__ */ new Set();
|
|
4700
|
+
const distinct = [];
|
|
4701
|
+
for (const target of targets) {
|
|
4702
|
+
if (!LISTABLE_PROTOCOLS.has(target.api) || target.baseURL.length === 0) continue;
|
|
4703
|
+
const url = probeListingUrl(target.baseURL, target.api);
|
|
4704
|
+
if (asked.has(url)) continue;
|
|
4705
|
+
asked.add(url);
|
|
4706
|
+
distinct.push(target);
|
|
4707
|
+
}
|
|
4708
|
+
const answered = await Promise.all(distinct.map((target) => probeListing({
|
|
4709
|
+
baseURL: target.baseURL,
|
|
4710
|
+
api: target.api,
|
|
4711
|
+
apiKey: key.value,
|
|
4712
|
+
headers: profile?.["headers"],
|
|
4713
|
+
timeoutMs: probeTimeoutMs,
|
|
4714
|
+
send: sendListing
|
|
4715
|
+
})));
|
|
4716
|
+
const records2 = [];
|
|
4717
|
+
for (const listing of answered) {
|
|
4718
|
+
if (listing === void 0) continue;
|
|
4719
|
+
for (const model of listing) {
|
|
4720
|
+
if (records2.some((existing) => existing.id === model.id)) continue;
|
|
4721
|
+
records2.push(model);
|
|
4722
|
+
}
|
|
4723
|
+
}
|
|
4724
|
+
if (records2.length === 0) return { ref: key.ref, models: void 0, targets: [] };
|
|
4725
|
+
recordsByRef.set(key.ref, records2);
|
|
4726
|
+
const ids = records2.map((model) => model.id);
|
|
4727
|
+
lastKnownScopes.set(here(key.ref), ids);
|
|
4728
|
+
lastKnownRecords.set(here(key.ref), records2);
|
|
4729
|
+
return {
|
|
4730
|
+
ref: key.ref,
|
|
4731
|
+
models: ids,
|
|
4732
|
+
targets: []
|
|
4733
|
+
};
|
|
4734
|
+
}
|
|
4735
|
+
});
|
|
4736
|
+
const records = /* @__PURE__ */ new Map();
|
|
4737
|
+
for (const result of results) {
|
|
4738
|
+
const owned = recordsByRef.get(result.ref) ?? lastKnownRecords.get(here(result.ref)) ?? [];
|
|
4739
|
+
for (const model of owned) {
|
|
4740
|
+
if (!records.has(model.id)) records.set(model.id, model);
|
|
4741
|
+
}
|
|
4742
|
+
}
|
|
4743
|
+
const models = mergeKeyListings(results, knownHere).ids.map((id) => records.get(id)).filter((model) => model !== void 0);
|
|
4744
|
+
if (models.length === 0) return void 0;
|
|
4745
|
+
const body = JSON.stringify({ data: models });
|
|
4746
|
+
listingCache.set(cacheKey, { at: Date.now(), body });
|
|
4747
|
+
return new Response(body, {
|
|
4748
|
+
status: 200,
|
|
4749
|
+
headers: { "content-type": "application/json" }
|
|
4750
|
+
});
|
|
4751
|
+
};
|
|
2884
4752
|
const overlay = installHeaderOverlay(overlaySource);
|
|
2885
4753
|
ctx.effect(() => () => {
|
|
2886
4754
|
overlay.dispose();
|
|
@@ -2894,15 +4762,20 @@ function apply(ctx, config = {}) {
|
|
|
2894
4762
|
ctx.effect(() => () => transforms.dispose(), "dsh-model-think-level: pi-ai request headers");
|
|
2895
4763
|
};
|
|
2896
4764
|
const availableLlm = ctx.llm;
|
|
4765
|
+
let fingerprintDispatch = availableLlm;
|
|
2897
4766
|
if (availableLlm === void 0) {
|
|
2898
4767
|
ctx.inject(["llm"], (llmCtx) => {
|
|
2899
4768
|
const llm = llmCtx.llm;
|
|
2900
|
-
if (llm !== void 0)
|
|
4769
|
+
if (llm !== void 0) {
|
|
4770
|
+
fingerprintDispatch = llm;
|
|
4771
|
+
installPiAiBridge(llm);
|
|
4772
|
+
}
|
|
2901
4773
|
});
|
|
2902
4774
|
} else {
|
|
2903
4775
|
installPiAiBridge(availableLlm);
|
|
2904
4776
|
}
|
|
2905
4777
|
const environmentConflicts = detectHeaderConflicts();
|
|
4778
|
+
let fingerprintMutationTail = Promise.resolve();
|
|
2906
4779
|
ctx.inject(["webServer"], (webServerCtx) => {
|
|
2907
4780
|
ctx.effect(
|
|
2908
4781
|
() => webServerCtx.webServer.register({
|
|
@@ -2964,6 +4837,13 @@ function apply(ctx, config = {}) {
|
|
|
2964
4837
|
}
|
|
2965
4838
|
const apiKeyEnv = typeof profile["apiKeyEnv"] === "string" ? profile["apiKeyEnv"] : void 0;
|
|
2966
4839
|
const listingURL = probeListingUrl(baseURL, api);
|
|
4840
|
+
const listingKey = `${listingURL}\0${apiKeyEnv ?? ""}`;
|
|
4841
|
+
const cacheable = url.searchParams.get("refresh") !== "1";
|
|
4842
|
+
const remembered = cacheable ? probeRouteCache.get(listingKey) : void 0;
|
|
4843
|
+
if (remembered !== void 0 && Date.now() - remembered.at < LISTING_CACHE_MS) {
|
|
4844
|
+
sendJson(res, 200, { ok: true, url: displayUrl(listingURL), data: remembered.data });
|
|
4845
|
+
return;
|
|
4846
|
+
}
|
|
2967
4847
|
let apiKey;
|
|
2968
4848
|
if (apiKeyEnv !== void 0) {
|
|
2969
4849
|
try {
|
|
@@ -2974,7 +4854,7 @@ function apply(ctx, config = {}) {
|
|
|
2974
4854
|
}
|
|
2975
4855
|
}
|
|
2976
4856
|
try {
|
|
2977
|
-
const upstream = await
|
|
4857
|
+
const upstream = await headerOverlayBypass()(listingURL, {
|
|
2978
4858
|
method: "GET",
|
|
2979
4859
|
// Header composition mirrors the harness's own model discovery:
|
|
2980
4860
|
// the profile's configured request headers ride
|
|
@@ -2995,7 +4875,11 @@ function apply(ctx, config = {}) {
|
|
|
2995
4875
|
// protocol inference, the path every unanswerable endpoint
|
|
2996
4876
|
// takes.
|
|
2997
4877
|
redirect: "error",
|
|
2998
|
-
|
|
4878
|
+
// This probe waits on an interactive pass (Auto-adapt asks for
|
|
4879
|
+
// it per model), so it gets the catalogue budget rather than the
|
|
4880
|
+
// diagnostic one: a gateway that cannot list within it yields no
|
|
4881
|
+
// endpoint evidence, and the pass moves on to the knowledge base.
|
|
4882
|
+
signal: AbortSignal.timeout(Math.min(resolved.probeTimeoutMs, LISTING_PROBE_BUDGET_MS))
|
|
2999
4883
|
});
|
|
3000
4884
|
if (!upstream.ok) {
|
|
3001
4885
|
const hint = upstream.status === 401 || upstream.status === 403 ? "; check the API key" : "";
|
|
@@ -3018,6 +4902,7 @@ function apply(ctx, config = {}) {
|
|
|
3018
4902
|
});
|
|
3019
4903
|
return;
|
|
3020
4904
|
}
|
|
4905
|
+
probeRouteCache.set(listingKey, { at: Date.now(), data: entries });
|
|
3021
4906
|
sendJson(res, 200, { ok: true, url: displayUrl(listingURL), data: entries });
|
|
3022
4907
|
} catch (error) {
|
|
3023
4908
|
sendJson(res, 502, {
|
|
@@ -3172,24 +5057,62 @@ function apply(ctx, config = {}) {
|
|
|
3172
5057
|
return "the settings document kept changing; try again";
|
|
3173
5058
|
};
|
|
3174
5059
|
const payload = async () => {
|
|
3175
|
-
const
|
|
3176
|
-
const
|
|
5060
|
+
const document = readKeyIndex(indexFile);
|
|
5061
|
+
const seat = enabledNow();
|
|
5062
|
+
const listed = keyEntriesOf(document, route);
|
|
3177
5063
|
const refs = listed.map((entry) => entry.ref);
|
|
3178
|
-
if (
|
|
5064
|
+
if (seat !== void 0 && !refs.includes(seat)) refs.push(seat);
|
|
3179
5065
|
const facts = await credentialFacts(credentials, refs);
|
|
5066
|
+
const now = Date.now();
|
|
3180
5067
|
const entries = refs.map((ref2) => {
|
|
3181
5068
|
const fact = facts[ref2];
|
|
5069
|
+
const stored = listed.find((candidate) => candidate.ref === ref2);
|
|
3182
5070
|
const view = {
|
|
3183
5071
|
ref: ref2,
|
|
3184
|
-
|
|
5072
|
+
// Being switched on is the default: a key the index has
|
|
5073
|
+
// never described (the seat) reads as in use.
|
|
5074
|
+
enabled: stored?.disabled !== true,
|
|
3185
5075
|
configured: fact?.configured ?? null
|
|
3186
5076
|
};
|
|
3187
5077
|
if (fact?.masked !== void 0) view.masked = fact.masked;
|
|
3188
|
-
const stored = listed.find((candidate) => candidate.ref === ref2);
|
|
3189
5078
|
if (stored?.alias !== void 0) view.alias = stored.alias;
|
|
5079
|
+
if (stored?.models !== void 0) view.models = stored.models;
|
|
5080
|
+
if (stored?.protocol !== void 0) view.protocol = stored.protocol;
|
|
5081
|
+
const rest = keyRest.stateOf(restKeyOf(route, ref2), now);
|
|
5082
|
+
if (rest !== void 0) {
|
|
5083
|
+
;
|
|
5084
|
+
view.rest = {
|
|
5085
|
+
kind: rest.kind,
|
|
5086
|
+
remainingMs: Math.max(0, rest.until - now),
|
|
5087
|
+
failures: rest.failures
|
|
5088
|
+
};
|
|
5089
|
+
}
|
|
3190
5090
|
return view;
|
|
3191
5091
|
});
|
|
3192
|
-
|
|
5092
|
+
const endpoints = document.endpoints?.[route];
|
|
5093
|
+
const routeModels = modelsOf(providersOf(), route).map((model) => typeof model["id"] === "string" ? model["id"] : "").filter((id) => id.length > 0);
|
|
5094
|
+
const merged = mergeKeyListings(
|
|
5095
|
+
listed.map((entry) => ({
|
|
5096
|
+
ref: entry.ref,
|
|
5097
|
+
targets: [],
|
|
5098
|
+
models: entry.models === void 0 ? void 0 : [...entry.models]
|
|
5099
|
+
}))
|
|
5100
|
+
);
|
|
5101
|
+
const declaredModels = new Set(routeModels);
|
|
5102
|
+
const unmergedModels = merged.ids.filter((id) => !declaredModels.has(id));
|
|
5103
|
+
return {
|
|
5104
|
+
ok: true,
|
|
5105
|
+
route,
|
|
5106
|
+
entries,
|
|
5107
|
+
models: routeModels,
|
|
5108
|
+
...merged.ids.length === 0 ? {} : { mergedModels: merged.ids },
|
|
5109
|
+
...unmergedModels.length === 0 ? {} : { unmergedModels },
|
|
5110
|
+
endpoints: {
|
|
5111
|
+
openai: endpoints?.openai ?? null,
|
|
5112
|
+
anthropic: endpoints?.anthropic ?? null,
|
|
5113
|
+
gemini: endpoints?.gemini ?? null
|
|
5114
|
+
}
|
|
5115
|
+
};
|
|
3193
5116
|
};
|
|
3194
5117
|
if (req.method === "GET") {
|
|
3195
5118
|
sendJson(res, 200, await payload());
|
|
@@ -3201,6 +5124,7 @@ function apply(ctx, config = {}) {
|
|
|
3201
5124
|
const alias = isRecord(body) ? normalizeKeyAlias(body["alias"]) : void 0;
|
|
3202
5125
|
const value = isRecord(body) && typeof body["value"] === "string" ? body["value"] : void 0;
|
|
3203
5126
|
const refuse = (error) => sendJson(res, 400, { ok: false, error });
|
|
5127
|
+
let probeReport;
|
|
3204
5128
|
if (op === "add") {
|
|
3205
5129
|
if (!isLegalKeyValue(value)) {
|
|
3206
5130
|
refuse("a key must be printable ASCII without spaces");
|
|
@@ -3241,25 +5165,149 @@ function apply(ctx, config = {}) {
|
|
|
3241
5165
|
}
|
|
3242
5166
|
throw error;
|
|
3243
5167
|
}
|
|
5168
|
+
} else if (op === "scope") {
|
|
5169
|
+
const document = readKeyIndex(indexFile);
|
|
5170
|
+
const known = ref !== void 0 && (hasKeyEntry(document, route, ref) || ref === enabledNow());
|
|
5171
|
+
if (ref === void 0 || !known) {
|
|
5172
|
+
refuse("that key is not listed for this provider");
|
|
5173
|
+
return;
|
|
5174
|
+
}
|
|
5175
|
+
const scope = isRecord(body) ? normalizeKeyModels(body["models"]) : void 0;
|
|
5176
|
+
const target = ref;
|
|
5177
|
+
updateKeyIndex(
|
|
5178
|
+
(document2) => hasKeyEntry(document2, route, target) ? withKeyModels(document2, route, target, scope) : withKeyEntry(document2, route, target, void 0),
|
|
5179
|
+
indexFile
|
|
5180
|
+
);
|
|
5181
|
+
} else if (op === "protocol") {
|
|
5182
|
+
const document = readKeyIndex(indexFile);
|
|
5183
|
+
const known = ref !== void 0 && (hasKeyEntry(document, route, ref) || ref === enabledNow());
|
|
5184
|
+
if (ref === void 0 || !known) {
|
|
5185
|
+
refuse("that key is not listed for this provider");
|
|
5186
|
+
return;
|
|
5187
|
+
}
|
|
5188
|
+
const protocol = isRecord(body) ? normalizeKeyProtocol(body["protocol"]) : void 0;
|
|
5189
|
+
if (isRecord(body) && body["protocol"] !== void 0 && body["protocol"] !== null && protocol === void 0) {
|
|
5190
|
+
refuse("that protocol is not supported");
|
|
5191
|
+
return;
|
|
5192
|
+
}
|
|
5193
|
+
const target = ref;
|
|
5194
|
+
updateKeyIndex(
|
|
5195
|
+
(document2) => hasKeyEntry(document2, route, target) ? withKeyProtocol(document2, route, target, protocol) : withKeyEntry(document2, route, target, void 0),
|
|
5196
|
+
indexFile
|
|
5197
|
+
);
|
|
3244
5198
|
} else if (op === "enable") {
|
|
3245
|
-
const
|
|
3246
|
-
|
|
5199
|
+
const document = readKeyIndex(indexFile);
|
|
5200
|
+
const known = ref !== void 0 && (hasKeyEntry(document, route, ref) || ref === enabledNow());
|
|
5201
|
+
if (ref === void 0 || !known) {
|
|
3247
5202
|
refuse("that key is not listed for this provider");
|
|
3248
5203
|
return;
|
|
3249
5204
|
}
|
|
3250
|
-
const
|
|
3251
|
-
if (
|
|
3252
|
-
const
|
|
5205
|
+
const enabling = !(isRecord(body) && body["enabled"] === false);
|
|
5206
|
+
if (!enabling) {
|
|
5207
|
+
const listed = keyEntriesOf(document, route);
|
|
5208
|
+
const seat = enabledNow();
|
|
5209
|
+
const others = listed.filter((entry) => entry.ref !== ref && entry.disabled !== true);
|
|
5210
|
+
const seatRemains = seat !== void 0 && seat !== ref && !listed.some((entry) => entry.ref === seat);
|
|
5211
|
+
if (others.length === 0 && !seatRemains) {
|
|
5212
|
+
refuse("the last enabled key cannot be switched off");
|
|
5213
|
+
return;
|
|
5214
|
+
}
|
|
5215
|
+
}
|
|
5216
|
+
const target = ref;
|
|
5217
|
+
updateKeyIndex(
|
|
5218
|
+
(document2) => {
|
|
5219
|
+
const indexed = hasKeyEntry(document2, route, target);
|
|
5220
|
+
if (!indexed && enabling) return document2;
|
|
5221
|
+
return withKeyDisabled(
|
|
5222
|
+
indexed ? document2 : withKeyEntry(document2, route, target, void 0),
|
|
5223
|
+
route,
|
|
5224
|
+
target,
|
|
5225
|
+
!enabling
|
|
5226
|
+
);
|
|
5227
|
+
},
|
|
5228
|
+
indexFile
|
|
5229
|
+
);
|
|
5230
|
+
} else if (op === "endpoints") {
|
|
5231
|
+
const endpoints = isRecord(body) ? normalizeRouteEndpoints({
|
|
5232
|
+
openai: body["openai"],
|
|
5233
|
+
anthropic: body["anthropic"],
|
|
5234
|
+
gemini: body["gemini"]
|
|
5235
|
+
}) : void 0;
|
|
5236
|
+
updateKeyIndex((document) => withRouteEndpoints(document, route, endpoints), indexFile);
|
|
5237
|
+
} else if (op === "discover") {
|
|
5238
|
+
const document = readKeyIndex(indexFile);
|
|
5239
|
+
const profile = providerProfileOf(entryOf()?.value, route);
|
|
5240
|
+
const baseURL = typeof profile?.["baseURL"] === "string" ? profile["baseURL"] : "";
|
|
5241
|
+
const api = typeof profile?.["api"] === "string" ? profile["api"] : "";
|
|
5242
|
+
if (baseURL.length === 0) {
|
|
5243
|
+
refuse("this provider has no address to interrogate; set each key's models by hand");
|
|
5244
|
+
return;
|
|
5245
|
+
}
|
|
5246
|
+
const seat = enabledNow();
|
|
5247
|
+
const listedEntries = keyEntriesOf(document, route);
|
|
5248
|
+
const listedRefs = listedEntries.map((candidate) => candidate.ref);
|
|
5249
|
+
if (seat !== void 0 && !listedRefs.includes(seat)) listedRefs.push(seat);
|
|
5250
|
+
if (listedRefs.length === 0) {
|
|
5251
|
+
refuse("add a key before detecting its models");
|
|
5252
|
+
return;
|
|
5253
|
+
}
|
|
5254
|
+
const endpoints = document.endpoints?.[route];
|
|
5255
|
+
const asks = [];
|
|
5256
|
+
for (const listed of listedRefs) {
|
|
5257
|
+
let apiKey;
|
|
5258
|
+
try {
|
|
5259
|
+
apiKey = await resolveCredential(credentials, listed);
|
|
5260
|
+
} catch {
|
|
5261
|
+
continue;
|
|
5262
|
+
}
|
|
5263
|
+
if (apiKey === void 0) continue;
|
|
5264
|
+
const pinned = listedEntries.find((candidate) => candidate.ref === listed)?.protocol;
|
|
5265
|
+
asks.push({
|
|
5266
|
+
ref: listed,
|
|
5267
|
+
apiKey,
|
|
5268
|
+
...pinned === void 0 ? {} : { pinned }
|
|
5269
|
+
});
|
|
5270
|
+
}
|
|
5271
|
+
const lastKnown = new Map(
|
|
5272
|
+
listedEntries.filter((candidate) => candidate.models !== void 0).map((candidate) => [candidate.ref, candidate.models])
|
|
5273
|
+
);
|
|
5274
|
+
const { results } = await collectKeyListings({
|
|
5275
|
+
keys: asks,
|
|
5276
|
+
lastKnown,
|
|
5277
|
+
refOf: (ask) => ask.ref,
|
|
5278
|
+
probe: async (ask) => probeKeyModels({
|
|
5279
|
+
ref: ask.ref,
|
|
5280
|
+
intent: {
|
|
5281
|
+
api,
|
|
5282
|
+
baseURL,
|
|
5283
|
+
...ask.pinned === void 0 ? {} : { pinned: ask.pinned },
|
|
5284
|
+
...endpoints === void 0 ? {} : { endpoints }
|
|
5285
|
+
},
|
|
5286
|
+
apiKey: ask.apiKey,
|
|
5287
|
+
profileHeaders: profile?.["headers"],
|
|
5288
|
+
timeoutMs: resolved.probeTimeoutMs,
|
|
5289
|
+
// Each key probes its OWN credential, so bypass the key-routing
|
|
5290
|
+
// wrapper (which would otherwise sign every probe with the first key).
|
|
5291
|
+
send: headerOverlayBypass(),
|
|
5292
|
+
headersFor: composeProbeHeaders,
|
|
5293
|
+
read: readBounded,
|
|
5294
|
+
entries: listingEntries
|
|
5295
|
+
})
|
|
5296
|
+
});
|
|
5297
|
+
for (const result of results) {
|
|
5298
|
+
if (result.models === void 0) continue;
|
|
3253
5299
|
updateKeyIndex(
|
|
3254
|
-
(
|
|
5300
|
+
(document2) => hasKeyEntry(document2, route, result.ref) ? withKeyModels(document2, route, result.ref, result.models) : withKeyEntry(document2, route, result.ref, void 0),
|
|
3255
5301
|
indexFile
|
|
3256
5302
|
);
|
|
3257
5303
|
}
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
5304
|
+
if (results.every((result) => result.targets.length === 0)) {
|
|
5305
|
+
refuse(
|
|
5306
|
+
results.find((result) => result.skipped !== void 0)?.skipped ?? "this provider cannot be interrogated; set each key's models by hand"
|
|
5307
|
+
);
|
|
3261
5308
|
return;
|
|
3262
5309
|
}
|
|
5310
|
+
probeReport = results;
|
|
3263
5311
|
} else if (op === "rename") {
|
|
3264
5312
|
const known = ref !== void 0 && (hasKeyEntry(readKeyIndex(indexFile), route, ref) || ref === enabledNow());
|
|
3265
5313
|
if (ref === void 0 || !known) {
|
|
@@ -3318,7 +5366,7 @@ function apply(ctx, config = {}) {
|
|
|
3318
5366
|
const original = document.providers[route];
|
|
3319
5367
|
if (original === void 0) delete providers[route];
|
|
3320
5368
|
else providers[route] = original;
|
|
3321
|
-
return {
|
|
5369
|
+
return { ...current, providers };
|
|
3322
5370
|
}, indexFile);
|
|
3323
5371
|
} catch {
|
|
3324
5372
|
failures.push("key index rollback failed");
|
|
@@ -3329,11 +5377,66 @@ function apply(ctx, config = {}) {
|
|
|
3329
5377
|
}
|
|
3330
5378
|
throw new Error([error instanceof Error ? error.message : String(error), ...failures].join("; "));
|
|
3331
5379
|
}
|
|
5380
|
+
} else if (op === "applyMerged") {
|
|
5381
|
+
const merged = mergeKeyListings(
|
|
5382
|
+
keyEntriesOf(readKeyIndex(indexFile), route).map((candidate) => ({
|
|
5383
|
+
ref: candidate.ref,
|
|
5384
|
+
targets: [],
|
|
5385
|
+
models: candidate.models === void 0 ? void 0 : [...candidate.models]
|
|
5386
|
+
}))
|
|
5387
|
+
);
|
|
5388
|
+
if (merged.ids.length === 0) {
|
|
5389
|
+
refuse("no key models have been detected yet; detect them first");
|
|
5390
|
+
return;
|
|
5391
|
+
}
|
|
5392
|
+
const appendMerged = async () => {
|
|
5393
|
+
const descriptor = settings.describe().find((candidate) => candidate.ns === PI_NS);
|
|
5394
|
+
const user = isRecord(descriptor?.["user"]) ? descriptor["user"] : void 0;
|
|
5395
|
+
const userProviders = isRecord(user) && isRecord(user["providers"]) ? user["providers"] : void 0;
|
|
5396
|
+
const profile = isRecord(userProviders) ? userProviders[route] : void 0;
|
|
5397
|
+
const rawModels = isRecord(profile) && Array.isArray(profile["models"]) ? profile["models"] : [];
|
|
5398
|
+
const declared = new Set(
|
|
5399
|
+
rawModels.filter(isRecord).map((model) => typeof model["id"] === "string" ? model["id"].trim() : "").filter((id) => id.length > 0)
|
|
5400
|
+
);
|
|
5401
|
+
const additions = merged.ids.filter((id) => !declared.has(id));
|
|
5402
|
+
if (additions.length === 0) return void 0;
|
|
5403
|
+
try {
|
|
5404
|
+
await settings.mutate(
|
|
5405
|
+
PI_NS,
|
|
5406
|
+
[{
|
|
5407
|
+
op: "set",
|
|
5408
|
+
path: ["providers", route, "models"],
|
|
5409
|
+
value: [...rawModels, ...additions.map((id) => ({ id }))]
|
|
5410
|
+
}],
|
|
5411
|
+
descriptor?.revision
|
|
5412
|
+
);
|
|
5413
|
+
return void 0;
|
|
5414
|
+
} catch (error) {
|
|
5415
|
+
return {
|
|
5416
|
+
conflict: isConflictError(error),
|
|
5417
|
+
error: error instanceof Error ? error.message : String(error)
|
|
5418
|
+
};
|
|
5419
|
+
}
|
|
5420
|
+
};
|
|
5421
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
5422
|
+
const failed = await appendMerged();
|
|
5423
|
+
if (failed === void 0) break;
|
|
5424
|
+
if (attempt === 0 && failed.conflict) continue;
|
|
5425
|
+
refuse(failed.error);
|
|
5426
|
+
return;
|
|
5427
|
+
}
|
|
5428
|
+
piSectionCached = false;
|
|
3332
5429
|
} else {
|
|
3333
5430
|
refuse(`unknown key operation "${op}"`);
|
|
3334
5431
|
return;
|
|
3335
5432
|
}
|
|
3336
|
-
|
|
5433
|
+
refreshKeyScopes();
|
|
5434
|
+
const answer = await payload();
|
|
5435
|
+
sendJson(
|
|
5436
|
+
res,
|
|
5437
|
+
200,
|
|
5438
|
+
probeReport === void 0 || !isRecord(answer) ? answer : { ...answer, probe: probeReport }
|
|
5439
|
+
);
|
|
3337
5440
|
} catch (error) {
|
|
3338
5441
|
sendJson(res, error instanceof HostReadError ? 502 : 500, {
|
|
3339
5442
|
ok: false,
|
|
@@ -3407,6 +5510,188 @@ function apply(ctx, config = {}) {
|
|
|
3407
5510
|
}),
|
|
3408
5511
|
"dsh-model-think-level: headers config route"
|
|
3409
5512
|
);
|
|
5513
|
+
ctx.effect(
|
|
5514
|
+
() => webServerCtx.webServer.register({
|
|
5515
|
+
kind: "exact",
|
|
5516
|
+
path: FINGERPRINT_PATH,
|
|
5517
|
+
handler: async (req, res) => {
|
|
5518
|
+
if (!isTrustedRequest(req)) {
|
|
5519
|
+
sendJson(res, 403, { ok: false, error: "forbidden" });
|
|
5520
|
+
return;
|
|
5521
|
+
}
|
|
5522
|
+
const method = req.method ?? "GET";
|
|
5523
|
+
if (method !== "GET" && method !== "POST" && method !== "DELETE") {
|
|
5524
|
+
sendJson(res, 405, { ok: false, error: "method not allowed" });
|
|
5525
|
+
return;
|
|
5526
|
+
}
|
|
5527
|
+
const url = new URL(req.url ?? "/", "http://x");
|
|
5528
|
+
const route = url.searchParams.get("route") ?? "";
|
|
5529
|
+
if (route.length === 0) {
|
|
5530
|
+
sendJson(res, 400, { ok: false, error: "a provider route is required" });
|
|
5531
|
+
return;
|
|
5532
|
+
}
|
|
5533
|
+
let section;
|
|
5534
|
+
try {
|
|
5535
|
+
section = piSection(true);
|
|
5536
|
+
} catch {
|
|
5537
|
+
sendJson(res, 502, { ok: false, error: "the llm-pi-ai settings could not be read" });
|
|
5538
|
+
return;
|
|
5539
|
+
}
|
|
5540
|
+
const profile = isRecord(section) && isRecord(section["providers"]) ? section["providers"][route] : void 0;
|
|
5541
|
+
if (!isRecord(profile)) {
|
|
5542
|
+
sendJson(res, 400, { ok: false, error: `no llm-pi-ai provider route "${route}"` });
|
|
5543
|
+
return;
|
|
5544
|
+
}
|
|
5545
|
+
let bank;
|
|
5546
|
+
try {
|
|
5547
|
+
bank = loadBankSync();
|
|
5548
|
+
} catch (error) {
|
|
5549
|
+
sendJson(res, 500, {
|
|
5550
|
+
ok: false,
|
|
5551
|
+
error: error instanceof Error ? error.message : String(error)
|
|
5552
|
+
});
|
|
5553
|
+
return;
|
|
5554
|
+
}
|
|
5555
|
+
const file = fingerprintFilePath();
|
|
5556
|
+
const stamp = bankBuiltAt(bank);
|
|
5557
|
+
if (method === "GET") {
|
|
5558
|
+
const document = readFingerprintStore(stamp, file);
|
|
5559
|
+
sendJson(res, 200, {
|
|
5560
|
+
ok: true,
|
|
5561
|
+
data: {
|
|
5562
|
+
bank: summarizeBank(bank),
|
|
5563
|
+
records: providerRecords(document, route)
|
|
5564
|
+
}
|
|
5565
|
+
});
|
|
5566
|
+
return;
|
|
5567
|
+
}
|
|
5568
|
+
let releaseMutation;
|
|
5569
|
+
const previous = fingerprintMutationTail;
|
|
5570
|
+
fingerprintMutationTail = new Promise((resolve2) => {
|
|
5571
|
+
releaseMutation = resolve2;
|
|
5572
|
+
});
|
|
5573
|
+
await previous;
|
|
5574
|
+
try {
|
|
5575
|
+
if (method === "DELETE") {
|
|
5576
|
+
const model2 = url.searchParams.get("model") ?? "";
|
|
5577
|
+
if (model2.length === 0) {
|
|
5578
|
+
sendJson(res, 400, { ok: false, error: "a model id is required" });
|
|
5579
|
+
return;
|
|
5580
|
+
}
|
|
5581
|
+
updateFingerprintStore(
|
|
5582
|
+
(document) => withoutRecord(document, route, model2),
|
|
5583
|
+
stamp,
|
|
5584
|
+
file
|
|
5585
|
+
);
|
|
5586
|
+
sendJson(res, 200, { ok: true });
|
|
5587
|
+
return;
|
|
5588
|
+
}
|
|
5589
|
+
const body = await readJsonBody(req, 2048);
|
|
5590
|
+
const model = isRecord(body) && typeof body["model"] === "string" ? body["model"] : "";
|
|
5591
|
+
if (model.length === 0) {
|
|
5592
|
+
sendJson(res, 400, { ok: false, error: "a model id is required" });
|
|
5593
|
+
return;
|
|
5594
|
+
}
|
|
5595
|
+
if (!isEligibleModel(model)) {
|
|
5596
|
+
sendJson(res, 400, {
|
|
5597
|
+
ok: false,
|
|
5598
|
+
error: `model "${model}" is not a GPT or Claude model`
|
|
5599
|
+
});
|
|
5600
|
+
return;
|
|
5601
|
+
}
|
|
5602
|
+
const llm = fingerprintDispatch ?? ctx.llm;
|
|
5603
|
+
if (llm === void 0 || typeof llm.prepareCall !== "function") {
|
|
5604
|
+
sendJson(res, 503, { ok: false, error: "the llm service is unavailable" });
|
|
5605
|
+
return;
|
|
5606
|
+
}
|
|
5607
|
+
res.statusCode = 200;
|
|
5608
|
+
res.setHeader("content-type", "application/x-ndjson; charset=utf-8");
|
|
5609
|
+
res.setHeader("cache-control", "no-store");
|
|
5610
|
+
res.setHeader("x-accel-buffering", "no");
|
|
5611
|
+
const emit = (line) => {
|
|
5612
|
+
res.write(`${JSON.stringify(line)}
|
|
5613
|
+
`);
|
|
5614
|
+
};
|
|
5615
|
+
const challenges = generateChallenges(3);
|
|
5616
|
+
const controller = new AbortController();
|
|
5617
|
+
res.on("close", () => {
|
|
5618
|
+
if (!res.writableEnded) controller.abort();
|
|
5619
|
+
});
|
|
5620
|
+
let run;
|
|
5621
|
+
try {
|
|
5622
|
+
run = await runProbe({
|
|
5623
|
+
provider: route,
|
|
5624
|
+
model,
|
|
5625
|
+
challenges,
|
|
5626
|
+
llm,
|
|
5627
|
+
signal: controller.signal,
|
|
5628
|
+
timeoutMs: FINGERPRINT_CHALLENGE_TIMEOUT_MS,
|
|
5629
|
+
onOutcome: (outcome) => {
|
|
5630
|
+
emit({
|
|
5631
|
+
type: "progress",
|
|
5632
|
+
index: outcome.index,
|
|
5633
|
+
ok: outcome.ok,
|
|
5634
|
+
...outcome.error === void 0 ? {} : { error: outcome.error }
|
|
5635
|
+
});
|
|
5636
|
+
}
|
|
5637
|
+
});
|
|
5638
|
+
} catch (error) {
|
|
5639
|
+
emit({
|
|
5640
|
+
type: "error",
|
|
5641
|
+
error: error instanceof Error ? error.message : String(error)
|
|
5642
|
+
});
|
|
5643
|
+
res.end();
|
|
5644
|
+
return;
|
|
5645
|
+
}
|
|
5646
|
+
let attribution = null;
|
|
5647
|
+
let failure;
|
|
5648
|
+
try {
|
|
5649
|
+
attribution = analyzeGlobalOutputs(run.outputs, bank);
|
|
5650
|
+
} catch (error) {
|
|
5651
|
+
failure = error instanceof Error ? error.message : String(error);
|
|
5652
|
+
}
|
|
5653
|
+
const usedOutputs = attribution === null ? 0 : attribution.used_outputs;
|
|
5654
|
+
const record = {
|
|
5655
|
+
provider: route,
|
|
5656
|
+
model,
|
|
5657
|
+
status: statusOf(attribution, usedOutputs),
|
|
5658
|
+
checkedAt: Date.now(),
|
|
5659
|
+
usedOutputs
|
|
5660
|
+
};
|
|
5661
|
+
if (attribution === null) {
|
|
5662
|
+
const firstFailure = run.outcomes.find((outcome) => !outcome.ok);
|
|
5663
|
+
record.error = run.outputs.length === 0 && firstFailure?.error !== void 0 ? firstFailure.error : failure ?? NO_USABLE_OUTPUT;
|
|
5664
|
+
} else {
|
|
5665
|
+
record.prediction = attribution.prediction;
|
|
5666
|
+
record.predictionName = attribution.prediction_name;
|
|
5667
|
+
record.probability = attribution.probability;
|
|
5668
|
+
record.familyPrediction = attribution.family_prediction;
|
|
5669
|
+
record.familyProbability = attribution.family_probability;
|
|
5670
|
+
record.top = topEntriesOf(attribution.results, 3);
|
|
5671
|
+
record.calibration = {
|
|
5672
|
+
queries: attribution.calibration.queries,
|
|
5673
|
+
cvAccuracy: attribution.calibration.cv_accuracy
|
|
5674
|
+
};
|
|
5675
|
+
}
|
|
5676
|
+
updateFingerprintStore(
|
|
5677
|
+
(document) => withRecord(document, record),
|
|
5678
|
+
stamp,
|
|
5679
|
+
file
|
|
5680
|
+
);
|
|
5681
|
+
emit({ type: "result", record });
|
|
5682
|
+
res.end();
|
|
5683
|
+
} catch (error) {
|
|
5684
|
+
sendJson(res, 500, {
|
|
5685
|
+
ok: false,
|
|
5686
|
+
error: error instanceof Error ? error.message : String(error)
|
|
5687
|
+
});
|
|
5688
|
+
} finally {
|
|
5689
|
+
releaseMutation?.();
|
|
5690
|
+
}
|
|
5691
|
+
}
|
|
5692
|
+
}),
|
|
5693
|
+
"dsh-model-think-level: fingerprint route"
|
|
5694
|
+
);
|
|
3410
5695
|
});
|
|
3411
5696
|
}
|
|
3412
5697
|
export {
|