dsh-model-think-level 1.1.8 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -0
- package/lib/client/ComposerSlider.d.ts +9 -9
- package/lib/client/EffortEditor.d.ts +9 -6
- package/lib/client/KeyManager.d.ts +36 -10
- package/lib/client/ProtocolMenu.d.ts +51 -0
- package/lib/client/events.d.ts +26 -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 +54 -0
- package/lib/client/injection/create-keys-pane.d.ts +46 -0
- package/lib/client/injection/key-reveal.d.ts +12 -0
- package/lib/client/injection/models-page-editor.d.ts +83 -0
- 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 +48 -6
- package/lib/client/styles.d.ts +1 -1
- package/lib/client/types.d.ts +35 -2
- package/lib/client.js +1773 -256
- package/lib/client.js.map +4 -4
- package/lib/discovery-merge.d.ts +35 -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 +1462 -139
- 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 +1 -1
package/lib/index.js
CHANGED
|
@@ -15,6 +15,49 @@ var INPUT_UNSET_MARKER = "inputUnset";
|
|
|
15
15
|
var AUTOFILL_MARKER = "reasoningEffortsAutofilled";
|
|
16
16
|
|
|
17
17
|
// src/key-refs.ts
|
|
18
|
+
var ENDPOINT_FAMILIES = ["openai", "anthropic", "gemini"];
|
|
19
|
+
function endpointFamilyOf(protocol) {
|
|
20
|
+
if (protocol === "anthropic-messages") return "anthropic";
|
|
21
|
+
if (protocol === "google-generative-ai") return "gemini";
|
|
22
|
+
return "openai";
|
|
23
|
+
}
|
|
24
|
+
function isRouteProtocol(value) {
|
|
25
|
+
return value === "openai-completions" || value === "anthropic-messages" || value === "openai-responses" || value === "google-generative-ai";
|
|
26
|
+
}
|
|
27
|
+
function normalizeKeyProtocol(value) {
|
|
28
|
+
return isRouteProtocol(value) ? value : void 0;
|
|
29
|
+
}
|
|
30
|
+
function normalizeKeyDisabled(value) {
|
|
31
|
+
return value === true ? true : void 0;
|
|
32
|
+
}
|
|
33
|
+
function isEndpointUrl(value) {
|
|
34
|
+
if (typeof value !== "string") return false;
|
|
35
|
+
const trimmed = value.trim();
|
|
36
|
+
if (trimmed.length === 0) return false;
|
|
37
|
+
return /^https?:\/\/\S+$/i.test(trimmed);
|
|
38
|
+
}
|
|
39
|
+
function normalizeRouteEndpoints(value) {
|
|
40
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
41
|
+
const record = value;
|
|
42
|
+
const out = {};
|
|
43
|
+
if (isEndpointUrl(record["openai"])) out.openai = record["openai"].trim();
|
|
44
|
+
if (isEndpointUrl(record["anthropic"])) out.anthropic = record["anthropic"].trim();
|
|
45
|
+
if (isEndpointUrl(record["gemini"])) out.gemini = record["gemini"].trim();
|
|
46
|
+
return out.openai === void 0 && out.anthropic === void 0 && out.gemini === void 0 ? void 0 : out;
|
|
47
|
+
}
|
|
48
|
+
function normalizeKeyModels(value) {
|
|
49
|
+
if (!Array.isArray(value)) return void 0;
|
|
50
|
+
const seen = /* @__PURE__ */ new Set();
|
|
51
|
+
const out = [];
|
|
52
|
+
for (const item of value) {
|
|
53
|
+
if (typeof item !== "string") continue;
|
|
54
|
+
const id = item.trim();
|
|
55
|
+
if (id.length === 0 || seen.has(id)) continue;
|
|
56
|
+
seen.add(id);
|
|
57
|
+
out.push(id);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
18
61
|
var KEY_ALIAS_MAX_LENGTH = 40;
|
|
19
62
|
function deriveKeyRef(route) {
|
|
20
63
|
return `${route.toUpperCase().replace(/[^A-Z0-9]+/g, "_")}_API_KEY`;
|
|
@@ -55,6 +98,14 @@ function maskKeyValue(value) {
|
|
|
55
98
|
import { mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
56
99
|
import { homedir } from "node:os";
|
|
57
100
|
import { dirname, join } from "node:path";
|
|
101
|
+
function makeEntry(ref, alias, models, protocol, disabled) {
|
|
102
|
+
const entry = { ref };
|
|
103
|
+
if (alias !== void 0) entry.alias = alias;
|
|
104
|
+
if (models !== void 0) entry.models = models;
|
|
105
|
+
if (protocol !== void 0) entry.protocol = protocol;
|
|
106
|
+
if (disabled === true) entry.disabled = true;
|
|
107
|
+
return entry;
|
|
108
|
+
}
|
|
58
109
|
function emptyKeyIndex() {
|
|
59
110
|
return { version: 1, providers: {} };
|
|
60
111
|
}
|
|
@@ -72,7 +123,8 @@ function keyIndexFilePath(home = harnessHome()) {
|
|
|
72
123
|
}
|
|
73
124
|
function parseKeyIndex(raw) {
|
|
74
125
|
if (typeof raw !== "object" || raw === null) return emptyKeyIndex();
|
|
75
|
-
const
|
|
126
|
+
const root = raw;
|
|
127
|
+
const providers = root["providers"];
|
|
76
128
|
if (typeof providers !== "object" || providers === null) return emptyKeyIndex();
|
|
77
129
|
const out = {};
|
|
78
130
|
for (const [route, entries] of Object.entries(providers)) {
|
|
@@ -85,11 +137,26 @@ function parseKeyIndex(raw) {
|
|
|
85
137
|
if (typeof ref !== "string" || ref.length === 0) continue;
|
|
86
138
|
if (list.some((existing) => existing.ref === ref)) continue;
|
|
87
139
|
const alias = normalizeKeyAlias(record["alias"]);
|
|
88
|
-
|
|
140
|
+
const models = normalizeKeyModels(record["models"]);
|
|
141
|
+
const protocol = normalizeKeyProtocol(record["protocol"]);
|
|
142
|
+
const disabled = normalizeKeyDisabled(record["disabled"]);
|
|
143
|
+
list.push(makeEntry(ref, alias, models, protocol, disabled));
|
|
89
144
|
}
|
|
90
145
|
if (list.length > 0) out[route] = list;
|
|
91
146
|
}
|
|
92
|
-
|
|
147
|
+
const endpoints = parseEndpoints(root["endpoints"]);
|
|
148
|
+
const document = { version: 1, providers: out };
|
|
149
|
+
return endpoints === void 0 ? document : { ...document, endpoints };
|
|
150
|
+
}
|
|
151
|
+
function parseEndpoints(raw) {
|
|
152
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) return void 0;
|
|
153
|
+
const out = {};
|
|
154
|
+
for (const [route, value] of Object.entries(raw)) {
|
|
155
|
+
if (route.length === 0) continue;
|
|
156
|
+
const pair = normalizeRouteEndpoints(value);
|
|
157
|
+
if (pair !== void 0) out[route] = pair;
|
|
158
|
+
}
|
|
159
|
+
return Object.keys(out).length === 0 ? void 0 : out;
|
|
93
160
|
}
|
|
94
161
|
function readKeyIndex(file = keyIndexFilePath()) {
|
|
95
162
|
let text;
|
|
@@ -148,28 +215,283 @@ function hasKeyEntry(document, route, ref) {
|
|
|
148
215
|
function withKeyEntry(document, route, ref, alias) {
|
|
149
216
|
const list = [...keyEntriesOf(document, route)];
|
|
150
217
|
const index = list.findIndex((entry) => entry.ref === ref);
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
else
|
|
155
|
-
|
|
218
|
+
const label2 = normalizeKeyAlias(alias);
|
|
219
|
+
if (index === -1) {
|
|
220
|
+
list.push(makeEntry(ref, label2, void 0, void 0, void 0));
|
|
221
|
+
} else {
|
|
222
|
+
const existing = list[index];
|
|
223
|
+
const keptLabel = label2 === void 0 ? existing?.alias : label2;
|
|
224
|
+
list[index] = makeEntry(ref, keptLabel, existing?.models, existing?.protocol, existing?.disabled);
|
|
225
|
+
}
|
|
226
|
+
return { version: 1, providers: { ...document.providers, [route]: list }, ...endpointsOf(document) };
|
|
156
227
|
}
|
|
157
228
|
function withKeyAlias(document, route, ref, alias) {
|
|
158
229
|
const list = [...keyEntriesOf(document, route)];
|
|
159
230
|
const index = list.findIndex((entry) => entry.ref === ref);
|
|
160
231
|
if (index === -1) return document;
|
|
161
|
-
const
|
|
162
|
-
list[index] =
|
|
163
|
-
return { version: 1, providers: { ...document.providers, [route]: list } };
|
|
232
|
+
const label2 = normalizeKeyAlias(alias);
|
|
233
|
+
list[index] = makeEntry(ref, label2, list[index]?.models, list[index]?.protocol, list[index]?.disabled);
|
|
234
|
+
return { version: 1, providers: { ...document.providers, [route]: list }, ...endpointsOf(document) };
|
|
235
|
+
}
|
|
236
|
+
function withKeyModels(document, route, ref, models) {
|
|
237
|
+
const list = [...keyEntriesOf(document, route)];
|
|
238
|
+
const index = list.findIndex((entry) => entry.ref === ref);
|
|
239
|
+
if (index === -1) return document;
|
|
240
|
+
const scope = models === void 0 ? void 0 : normalizeKeyModels(models);
|
|
241
|
+
list[index] = makeEntry(ref, list[index]?.alias, scope, list[index]?.protocol, list[index]?.disabled);
|
|
242
|
+
return { version: 1, providers: { ...document.providers, [route]: list }, ...endpointsOf(document) };
|
|
243
|
+
}
|
|
244
|
+
function withKeyProtocol(document, route, ref, protocol) {
|
|
245
|
+
const list = [...keyEntriesOf(document, route)];
|
|
246
|
+
const index = list.findIndex((entry) => entry.ref === ref);
|
|
247
|
+
if (index === -1) return document;
|
|
248
|
+
const pinned = normalizeKeyProtocol(protocol);
|
|
249
|
+
list[index] = makeEntry(ref, list[index]?.alias, list[index]?.models, pinned, list[index]?.disabled);
|
|
250
|
+
return { version: 1, providers: { ...document.providers, [route]: list }, ...endpointsOf(document) };
|
|
251
|
+
}
|
|
252
|
+
function withKeyDisabled(document, route, ref, disabled) {
|
|
253
|
+
const list = [...keyEntriesOf(document, route)];
|
|
254
|
+
const index = list.findIndex((entry) => entry.ref === ref);
|
|
255
|
+
if (index === -1) return document;
|
|
256
|
+
const existing = list[index];
|
|
257
|
+
list[index] = makeEntry(ref, existing?.alias, existing?.models, existing?.protocol, disabled ? true : void 0);
|
|
258
|
+
return { version: 1, providers: { ...document.providers, [route]: list }, ...endpointsOf(document) };
|
|
259
|
+
}
|
|
260
|
+
function withRouteEndpoints(document, route, endpoints) {
|
|
261
|
+
const map = { ...document.endpoints ?? {} };
|
|
262
|
+
const cleaned = normalizeRouteEndpoints(endpoints);
|
|
263
|
+
if (cleaned === void 0) delete map[route];
|
|
264
|
+
else map[route] = cleaned;
|
|
265
|
+
const next = { version: 1, providers: document.providers };
|
|
266
|
+
return Object.keys(map).length === 0 ? next : { ...next, endpoints: map };
|
|
267
|
+
}
|
|
268
|
+
function endpointsOf(document) {
|
|
269
|
+
return document.endpoints === void 0 ? {} : { endpoints: document.endpoints };
|
|
164
270
|
}
|
|
165
271
|
function withoutKeyEntry(document, route, ref) {
|
|
166
272
|
const list = keyEntriesOf(document, route).filter((entry) => entry.ref !== ref);
|
|
167
273
|
const providers = { ...document.providers };
|
|
168
274
|
if (list.length === 0) delete providers[route];
|
|
169
275
|
else providers[route] = list;
|
|
170
|
-
return { version: 1, providers };
|
|
276
|
+
return { version: 1, providers, ...endpointsOf(document) };
|
|
171
277
|
}
|
|
172
278
|
|
|
279
|
+
// src/key-route-core.ts
|
|
280
|
+
function canServe(key, model) {
|
|
281
|
+
if (model === void 0) return true;
|
|
282
|
+
const models = key.models;
|
|
283
|
+
if (models === void 0) return true;
|
|
284
|
+
return models.includes(model);
|
|
285
|
+
}
|
|
286
|
+
function keysForModel(keys, model) {
|
|
287
|
+
if (model === void 0) return [...keys];
|
|
288
|
+
const eligible = keys.filter((key) => canServe(key, model));
|
|
289
|
+
if (eligible.length === 0) return [...keys];
|
|
290
|
+
const named = eligible.filter((key) => key.models !== void 0);
|
|
291
|
+
const catchAll = eligible.filter((key) => key.models === void 0);
|
|
292
|
+
return [...named, ...catchAll];
|
|
293
|
+
}
|
|
294
|
+
function wireFamilyOf(protocol) {
|
|
295
|
+
if (protocol === "anthropic-messages") return "anthropic";
|
|
296
|
+
if (protocol === "google-generative-ai") return "gemini";
|
|
297
|
+
return "openai";
|
|
298
|
+
}
|
|
299
|
+
var ANTHROPIC_MODEL_ID = /(?:^|[/:._-])(?:claude|anthropic)(?:$|[/:._-])/i;
|
|
300
|
+
var OPENAI_MODEL_ID = /(?:^|[/:._-])(?:gpt|chatgpt|codex)(?:$|[/:._-])|(?:^|[/:._-])o[1-9](?:$|[/:._-])/i;
|
|
301
|
+
var GEMINI_MODEL_ID = /(?:^|[/:._-])(?:gemini|gemma)(?:$|[/:._-])/i;
|
|
302
|
+
function modelWireFamily(modelId) {
|
|
303
|
+
if (modelId === void 0) return void 0;
|
|
304
|
+
if (ANTHROPIC_MODEL_ID.test(modelId)) return "anthropic";
|
|
305
|
+
if (GEMINI_MODEL_ID.test(modelId)) return "gemini";
|
|
306
|
+
if (OPENAI_MODEL_ID.test(modelId)) return "openai";
|
|
307
|
+
return void 0;
|
|
308
|
+
}
|
|
309
|
+
function modelWireHint(modelId) {
|
|
310
|
+
const family = modelWireFamily(modelId);
|
|
311
|
+
if (family === "anthropic") return "anthropic-messages";
|
|
312
|
+
if (family === "gemini") return "google-generative-ai";
|
|
313
|
+
return void 0;
|
|
314
|
+
}
|
|
315
|
+
function autoWireOf(candidate, api, model, avoid) {
|
|
316
|
+
if (model === void 0) return api;
|
|
317
|
+
const family = modelWireFamily(model);
|
|
318
|
+
if (family === void 0 || family === endpointFamilyOf(api)) return api;
|
|
319
|
+
if (candidate.endpoints?.[family] === void 0) return api;
|
|
320
|
+
const hinted = family === "anthropic" ? "anthropic-messages" : family === "gemini" ? "google-generative-ai" : "openai-completions";
|
|
321
|
+
return hinted === avoid ? api : hinted;
|
|
322
|
+
}
|
|
323
|
+
function wireOf(candidate, api, model, avoid) {
|
|
324
|
+
const pinned = candidate.protocol;
|
|
325
|
+
if (pinned === void 0) return autoWireOf(candidate, api, model, avoid);
|
|
326
|
+
const crossesFamilies = endpointFamilyOf(pinned) !== endpointFamilyOf(api);
|
|
327
|
+
return crossesFamilies && candidate.endpoint === void 0 ? api : pinned;
|
|
328
|
+
}
|
|
329
|
+
function isHonoured(candidate, api) {
|
|
330
|
+
const pinned = candidate.protocol;
|
|
331
|
+
if (pinned === void 0) return true;
|
|
332
|
+
return endpointFamilyOf(pinned) === endpointFamilyOf(api) || candidate.endpoint !== void 0;
|
|
333
|
+
}
|
|
334
|
+
function protocolFit(candidate, api, modelId, avoid) {
|
|
335
|
+
const wire = wireOf(candidate, api, modelId, avoid);
|
|
336
|
+
const keyFamily = wireFamilyOf(wire);
|
|
337
|
+
const targetFamily = modelWireFamily(modelId) ?? wireFamilyOf(api);
|
|
338
|
+
if (keyFamily !== targetFamily) return 2;
|
|
339
|
+
if (wire === api) return 0;
|
|
340
|
+
return wire === modelWireHint(modelId) ? 0 : 1;
|
|
341
|
+
}
|
|
342
|
+
function partition(items, keep) {
|
|
343
|
+
const first = [];
|
|
344
|
+
const rest = [];
|
|
345
|
+
for (const item of items) (keep(item) ? first : rest).push(item);
|
|
346
|
+
return [...first, ...rest];
|
|
347
|
+
}
|
|
348
|
+
function restLast(candidates, isResting) {
|
|
349
|
+
return partition(candidates, (candidate) => !isResting(candidate));
|
|
350
|
+
}
|
|
351
|
+
function planKeys(candidates, options) {
|
|
352
|
+
const api = options.api;
|
|
353
|
+
const model = options.model;
|
|
354
|
+
const avoid = options.avoidWire;
|
|
355
|
+
const entries = candidates.map((candidate) => ({
|
|
356
|
+
candidate,
|
|
357
|
+
wire: wireOf(candidate, api, model, avoid),
|
|
358
|
+
fit: protocolFit(candidate, api, model, avoid),
|
|
359
|
+
honoured: isHonoured(candidate, api),
|
|
360
|
+
resting: options.isResting?.(candidate) === true
|
|
361
|
+
}));
|
|
362
|
+
const preferredWire = options.preferredWire;
|
|
363
|
+
const byFit = [...entries].sort((a, b) => a.fit - b.fit);
|
|
364
|
+
const byPreferred = partition(byFit, (entry) => preferredWire === void 0 || entry.wire === preferredWire);
|
|
365
|
+
const byHonour = partition(byPreferred, (entry) => entry.honoured);
|
|
366
|
+
const poolWire = byHonour.find((entry) => entry.honoured)?.wire;
|
|
367
|
+
const pool = byHonour.filter((entry) => entry.honoured && entry.wire === poolWire);
|
|
368
|
+
const aside = byHonour.filter((entry) => !(entry.honoured && entry.wire === poolWire));
|
|
369
|
+
const ordered = partition([...pool, ...aside], (entry) => !entry.resting);
|
|
370
|
+
return {
|
|
371
|
+
ordered: ordered.map((entry) => entry.candidate),
|
|
372
|
+
entries: ordered,
|
|
373
|
+
pool: pool.map((entry) => entry.candidate),
|
|
374
|
+
aside: aside.map((entry) => entry.candidate),
|
|
375
|
+
wire: ordered[0]?.wire
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
function shouldFailover(status) {
|
|
379
|
+
return status === 401 || status === 403 || status === 408 || status === 429 || status >= 500;
|
|
380
|
+
}
|
|
381
|
+
var QUOTA_FAILURE = /quota|insufficient|exhausted|balance|billing|credit|payment required|额度|余额|欠费|用尽|配额|充值/i;
|
|
382
|
+
function classifyFailure(status, bodyText = "") {
|
|
383
|
+
if (status === void 0) return QUOTA_FAILURE.test(bodyText) ? "quota" : "refused";
|
|
384
|
+
if (status === 401 || status === 403) return "auth";
|
|
385
|
+
if (status === 402) return "quota";
|
|
386
|
+
if (status === 408) return "timeout";
|
|
387
|
+
if (status === 429) return QUOTA_FAILURE.test(bodyText) ? "quota" : "rate";
|
|
388
|
+
if (status >= 500) return "server";
|
|
389
|
+
return void 0;
|
|
390
|
+
}
|
|
391
|
+
var COOLDOWN_MS = {
|
|
392
|
+
auth: 5 * 6e4,
|
|
393
|
+
rate: 6e4,
|
|
394
|
+
quota: 30 * 6e4,
|
|
395
|
+
server: 3e4,
|
|
396
|
+
timeout: 3e4,
|
|
397
|
+
refused: 6e4
|
|
398
|
+
};
|
|
399
|
+
var REST_COOLDOWN_CAP_MS = 30 * 6e4;
|
|
400
|
+
function cooldownFor(kind, failures = 1, retryAfterMs) {
|
|
401
|
+
const asked = retryAfterMs !== void 0 && retryAfterMs > 0 ? retryAfterMs : 0;
|
|
402
|
+
const base = Math.min(REST_COOLDOWN_CAP_MS, Math.max(COOLDOWN_MS[kind], asked));
|
|
403
|
+
const escalated = base * 2 ** Math.max(0, failures - 1);
|
|
404
|
+
return Math.round(Math.min(REST_COOLDOWN_CAP_MS, escalated));
|
|
405
|
+
}
|
|
406
|
+
function parseRetryAfter(value, now) {
|
|
407
|
+
if (typeof value !== "string") return void 0;
|
|
408
|
+
const trimmed = value.trim();
|
|
409
|
+
if (trimmed.length === 0) return void 0;
|
|
410
|
+
if (/^\d+(?:\.\d+)?$/.test(trimmed)) return Math.round(Number(trimmed) * 1e3);
|
|
411
|
+
const at = Date.parse(trimmed);
|
|
412
|
+
if (Number.isNaN(at)) return void 0;
|
|
413
|
+
return Math.max(0, at - now);
|
|
414
|
+
}
|
|
415
|
+
var REST_STORE_LIMIT = 256;
|
|
416
|
+
function createRestStore(limit = REST_STORE_LIMIT) {
|
|
417
|
+
const resting = /* @__PURE__ */ new Map();
|
|
418
|
+
const live = (key, now) => {
|
|
419
|
+
const state = resting.get(key);
|
|
420
|
+
if (state === void 0) return void 0;
|
|
421
|
+
if (state.until > now) return state;
|
|
422
|
+
resting.delete(key);
|
|
423
|
+
return void 0;
|
|
424
|
+
};
|
|
425
|
+
return {
|
|
426
|
+
get size() {
|
|
427
|
+
return resting.size;
|
|
428
|
+
},
|
|
429
|
+
stateOf: live,
|
|
430
|
+
isResting(key, now) {
|
|
431
|
+
return live(key, now) !== void 0;
|
|
432
|
+
},
|
|
433
|
+
note(key, kind, options) {
|
|
434
|
+
const now = options?.now ?? Date.now();
|
|
435
|
+
const previous = live(key, now);
|
|
436
|
+
const failures = previous === void 0 ? 1 : previous.failures + 1;
|
|
437
|
+
const until = now + cooldownFor(kind, failures, options?.retryAfterMs);
|
|
438
|
+
if (resting.size >= limit) {
|
|
439
|
+
for (const [staleKey, state2] of resting) {
|
|
440
|
+
if (state2.until <= now) resting.delete(staleKey);
|
|
441
|
+
}
|
|
442
|
+
if (resting.size >= limit) resting.clear();
|
|
443
|
+
}
|
|
444
|
+
const state = { kind, until, failures };
|
|
445
|
+
resting.set(key, state);
|
|
446
|
+
return state;
|
|
447
|
+
},
|
|
448
|
+
clear(key) {
|
|
449
|
+
resting.delete(key);
|
|
450
|
+
},
|
|
451
|
+
reset() {
|
|
452
|
+
resting.clear();
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
function restKeyOf(route, ref) {
|
|
457
|
+
return `${route}\0${ref}`;
|
|
458
|
+
}
|
|
459
|
+
var keyRest = createRestStore();
|
|
460
|
+
var PLAN_TRACE_LIMIT = 16;
|
|
461
|
+
var planTraces = [];
|
|
462
|
+
function recordRoutePlan(route, plan, options) {
|
|
463
|
+
planTraces.push({
|
|
464
|
+
at: options?.now ?? Date.now(),
|
|
465
|
+
route,
|
|
466
|
+
model: options?.model,
|
|
467
|
+
wire: plan.wire,
|
|
468
|
+
keys: plan.entries.map((entry) => ({
|
|
469
|
+
ref: entry.candidate.ref,
|
|
470
|
+
wire: entry.wire,
|
|
471
|
+
fit: entry.fit,
|
|
472
|
+
honoured: entry.honoured,
|
|
473
|
+
resting: entry.resting
|
|
474
|
+
}))
|
|
475
|
+
});
|
|
476
|
+
if (planTraces.length > PLAN_TRACE_LIMIT) planTraces.splice(0, planTraces.length - PLAN_TRACE_LIMIT);
|
|
477
|
+
}
|
|
478
|
+
function applyKeyToHeaders(headers, value) {
|
|
479
|
+
const usesGoogle = headers.has("x-goog-api-key");
|
|
480
|
+
const usesApiKey = headers.has("x-api-key");
|
|
481
|
+
const usesBearer = headers.has("authorization");
|
|
482
|
+
if (usesGoogle) headers.set("x-goog-api-key", value);
|
|
483
|
+
if (usesApiKey) headers.set("x-api-key", value);
|
|
484
|
+
if (usesBearer) headers.set("authorization", `Bearer ${value}`);
|
|
485
|
+
if (!usesGoogle && !usesApiKey && !usesBearer) headers.set("authorization", `Bearer ${value}`);
|
|
486
|
+
if (usesGoogle) {
|
|
487
|
+
headers.delete("authorization");
|
|
488
|
+
headers.delete("x-api-key");
|
|
489
|
+
} else if (usesApiKey && usesBearer) {
|
|
490
|
+
headers.delete("authorization");
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
var WIRE_DISPATCH_HEADER = "x-dsh-model-think-level-wire";
|
|
494
|
+
|
|
173
495
|
// src/knowledge.ts
|
|
174
496
|
var INPUT_MODALITIES = ["text", "image"];
|
|
175
497
|
var NEW_COMPAT_KEYS = ["thinkingTokenBudgetField", "vllmPriority", "supportsMaxOutputTokens"];
|
|
@@ -1714,6 +2036,12 @@ function looksLikeCompatRefusal(message) {
|
|
|
1714
2036
|
const msg = message.toLowerCase();
|
|
1715
2037
|
return msg.includes("compat") || msg.includes("thinkingtokenbudget") || msg.includes("vllmpriority") || msg.includes("supportsmaxoutput");
|
|
1716
2038
|
}
|
|
2039
|
+
function modelsOf(providers, route) {
|
|
2040
|
+
if (!isRecord(providers)) return [];
|
|
2041
|
+
const profile = providers[route];
|
|
2042
|
+
if (!isRecord(profile) || !Array.isArray(profile["models"])) return [];
|
|
2043
|
+
return profile["models"].filter(isRecord);
|
|
2044
|
+
}
|
|
1717
2045
|
function routeFactsOf(providers, route) {
|
|
1718
2046
|
if (!isRecord(providers)) return {};
|
|
1719
2047
|
const profile = providers[route];
|
|
@@ -1724,6 +2052,419 @@ function routeFactsOf(providers, route) {
|
|
|
1724
2052
|
return { api, baseURL, displayName };
|
|
1725
2053
|
}
|
|
1726
2054
|
|
|
2055
|
+
// src/listing.ts
|
|
2056
|
+
var ANTHROPIC_VERSION = "2023-06-01";
|
|
2057
|
+
var ANTHROPIC_MODEL_LIMIT = 1e3;
|
|
2058
|
+
var GEMINI_MODEL_LIMIT = 1e3;
|
|
2059
|
+
var MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
|
|
2060
|
+
var LISTABLE_PROTOCOLS = /* @__PURE__ */ new Set([
|
|
2061
|
+
"anthropic-messages",
|
|
2062
|
+
"openai-completions",
|
|
2063
|
+
"openai-responses",
|
|
2064
|
+
"google-generative-ai"
|
|
2065
|
+
]);
|
|
2066
|
+
function displayUrl(raw) {
|
|
2067
|
+
try {
|
|
2068
|
+
const url = new URL(raw);
|
|
2069
|
+
url.username = "";
|
|
2070
|
+
url.password = "";
|
|
2071
|
+
return url.toString();
|
|
2072
|
+
} catch {
|
|
2073
|
+
return raw;
|
|
2074
|
+
}
|
|
2075
|
+
}
|
|
2076
|
+
function probeListingUrl(baseURL, api) {
|
|
2077
|
+
const base = baseURL.replace(/\/+$/, "");
|
|
2078
|
+
if (api === "google-generative-ai") return `${geminiRequestBaseUrl(base)}/models?pageSize=${String(GEMINI_MODEL_LIMIT)}`;
|
|
2079
|
+
if (api !== "anthropic-messages") return `${base}/models`;
|
|
2080
|
+
const root = base.endsWith("/v1") ? base.slice(0, -3) : base;
|
|
2081
|
+
return `${root}/v1/models?limit=${String(ANTHROPIC_MODEL_LIMIT)}`;
|
|
2082
|
+
}
|
|
2083
|
+
function anthropicRequestBaseUrl(baseURL) {
|
|
2084
|
+
const base = baseURL.replace(/\/+$/, "");
|
|
2085
|
+
return base.endsWith("/v1") ? base.slice(0, -3) : base;
|
|
2086
|
+
}
|
|
2087
|
+
function geminiRequestBaseUrl(baseURL) {
|
|
2088
|
+
const base = baseURL.replace(/\/+$/, "");
|
|
2089
|
+
try {
|
|
2090
|
+
return new URL(base).pathname === "/" ? `${base}/v1beta` : base;
|
|
2091
|
+
} catch {
|
|
2092
|
+
return base;
|
|
2093
|
+
}
|
|
2094
|
+
}
|
|
2095
|
+
function composeProbeHeaders(profileHeaders, apiKey, api) {
|
|
2096
|
+
const headers = new Headers();
|
|
2097
|
+
if (isRecord(profileHeaders)) {
|
|
2098
|
+
for (const [name2, value] of Object.entries(profileHeaders)) {
|
|
2099
|
+
if (typeof value !== "string") continue;
|
|
2100
|
+
try {
|
|
2101
|
+
headers.set(name2, value);
|
|
2102
|
+
} catch {
|
|
2103
|
+
}
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
headers.set("accept", "application/json");
|
|
2107
|
+
if (api === "anthropic-messages") {
|
|
2108
|
+
headers.set("anthropic-version", ANTHROPIC_VERSION);
|
|
2109
|
+
if (apiKey !== void 0) headers.set("x-api-key", apiKey);
|
|
2110
|
+
} else if (api === "google-generative-ai") {
|
|
2111
|
+
if (apiKey !== void 0) headers.set("x-goog-api-key", apiKey);
|
|
2112
|
+
} else if (apiKey !== void 0) {
|
|
2113
|
+
headers.set("authorization", `Bearer ${apiKey}`);
|
|
2114
|
+
}
|
|
2115
|
+
return Object.fromEntries(headers.entries());
|
|
2116
|
+
}
|
|
2117
|
+
async function readBounded(response, url) {
|
|
2118
|
+
const declared = Number(response.headers.get("content-length") ?? Number.NaN);
|
|
2119
|
+
if (Number.isFinite(declared) && declared > MAX_RESPONSE_BYTES) {
|
|
2120
|
+
await response.body?.cancel();
|
|
2121
|
+
throw new Error(`${displayUrl(url)} overshoots the ${MAX_RESPONSE_BYTES}-byte listing ceiling`);
|
|
2122
|
+
}
|
|
2123
|
+
if (response.body === null) return "";
|
|
2124
|
+
const reader = response.body.getReader();
|
|
2125
|
+
const chunks = [];
|
|
2126
|
+
let total = 0;
|
|
2127
|
+
try {
|
|
2128
|
+
for (; ; ) {
|
|
2129
|
+
const { done, value } = await reader.read();
|
|
2130
|
+
if (done) break;
|
|
2131
|
+
total += value.byteLength;
|
|
2132
|
+
if (total > MAX_RESPONSE_BYTES) throw new Error(`${displayUrl(url)} overshoots the ${MAX_RESPONSE_BYTES}-byte listing ceiling`);
|
|
2133
|
+
chunks.push(value);
|
|
2134
|
+
}
|
|
2135
|
+
} finally {
|
|
2136
|
+
await reader.cancel().catch(() => {
|
|
2137
|
+
});
|
|
2138
|
+
}
|
|
2139
|
+
const body = new Uint8Array(total);
|
|
2140
|
+
let offset = 0;
|
|
2141
|
+
for (const chunk of chunks) {
|
|
2142
|
+
body.set(chunk, offset);
|
|
2143
|
+
offset += chunk.byteLength;
|
|
2144
|
+
}
|
|
2145
|
+
return new TextDecoder().decode(body);
|
|
2146
|
+
}
|
|
2147
|
+
function listingEntries(body) {
|
|
2148
|
+
const listing = isRecord(body) ? body : {};
|
|
2149
|
+
const data = listing["data"];
|
|
2150
|
+
if (Array.isArray(data)) return data.filter(isRecord);
|
|
2151
|
+
const models = listing["models"];
|
|
2152
|
+
if (Array.isArray(models)) {
|
|
2153
|
+
const out = [];
|
|
2154
|
+
for (const raw of models) {
|
|
2155
|
+
if (!isRecord(raw)) continue;
|
|
2156
|
+
const resource = typeof raw["name"] === "string" ? raw["name"] : "";
|
|
2157
|
+
const id = resource.startsWith("models/") ? resource.slice("models/".length) : resource;
|
|
2158
|
+
if (id.length === 0) continue;
|
|
2159
|
+
const methods = raw["supportedGenerationMethods"];
|
|
2160
|
+
if (Array.isArray(methods) && !methods.includes("generateContent")) continue;
|
|
2161
|
+
const display = typeof raw["displayName"] === "string" ? raw["displayName"] : void 0;
|
|
2162
|
+
const entry = { ...raw, id };
|
|
2163
|
+
if (display !== void 0 && display.trim().length > 0) entry["name"] = display;
|
|
2164
|
+
else delete entry["name"];
|
|
2165
|
+
out.push(entry);
|
|
2166
|
+
}
|
|
2167
|
+
return out;
|
|
2168
|
+
}
|
|
2169
|
+
if (isRecord(models)) {
|
|
2170
|
+
return Object.entries(models).filter(([, raw]) => isRecord(raw)).map(([key, raw]) => {
|
|
2171
|
+
const entry = raw;
|
|
2172
|
+
const ownId = typeof entry["id"] === "string" && entry["id"].length > 0 ? entry["id"] : "";
|
|
2173
|
+
return { ...entry, id: key.length > 0 ? key : ownId };
|
|
2174
|
+
}).filter((entry) => typeof entry["id"] === "string" && entry["id"].length > 0);
|
|
2175
|
+
}
|
|
2176
|
+
return void 0;
|
|
2177
|
+
}
|
|
2178
|
+
function label(value) {
|
|
2179
|
+
if (typeof value !== "string") return void 0;
|
|
2180
|
+
const trimmed = value.trim();
|
|
2181
|
+
return trimmed.length === 0 ? void 0 : trimmed;
|
|
2182
|
+
}
|
|
2183
|
+
function capacity(value) {
|
|
2184
|
+
return typeof value === "number" && Number.isFinite(value) && value > 0 ? Math.floor(value) : void 0;
|
|
2185
|
+
}
|
|
2186
|
+
function capacities(entry, keys) {
|
|
2187
|
+
const limit = isRecord(entry["limit"]) ? entry["limit"] : void 0;
|
|
2188
|
+
const top = isRecord(entry["top_provider"]) ? entry["top_provider"] : void 0;
|
|
2189
|
+
for (const key of keys) {
|
|
2190
|
+
const direct = capacity(entry[key]);
|
|
2191
|
+
if (direct !== void 0) return direct;
|
|
2192
|
+
}
|
|
2193
|
+
const tokenWindow = capacity(limit?.["context"]);
|
|
2194
|
+
if (tokenWindow !== void 0) return tokenWindow;
|
|
2195
|
+
return capacity(top?.["max_completion_tokens"]);
|
|
2196
|
+
}
|
|
2197
|
+
var CONTEXT_WINDOW_KEYS = ["contextWindow", "context_window", "context_length", "max_input_tokens", "inputTokenLimit"];
|
|
2198
|
+
var MAX_TOKENS_KEYS = ["maxOutputTokens", "max_output_tokens", "maxTokens", "max_tokens", "outputTokenLimit"];
|
|
2199
|
+
function modelsFromListing(body) {
|
|
2200
|
+
const entries = listingEntries(body);
|
|
2201
|
+
if (entries === void 0) return void 0;
|
|
2202
|
+
const out = [];
|
|
2203
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2204
|
+
for (const entry of entries) {
|
|
2205
|
+
const id = label(entry["id"]);
|
|
2206
|
+
if (id === void 0 || seen.has(id)) continue;
|
|
2207
|
+
seen.add(id);
|
|
2208
|
+
const model = { id };
|
|
2209
|
+
const name2 = label(entry["name"]) ?? label(entry["display_name"]) ?? label(entry["displayName"]);
|
|
2210
|
+
if (name2 !== void 0) model.name = name2;
|
|
2211
|
+
const contextWindow = capacities(entry, CONTEXT_WINDOW_KEYS);
|
|
2212
|
+
if (contextWindow !== void 0) model.contextWindow = contextWindow;
|
|
2213
|
+
const maxTokens = capacities(entry, MAX_TOKENS_KEYS);
|
|
2214
|
+
if (maxTokens !== void 0) model.maxTokens = maxTokens;
|
|
2215
|
+
const input = entry["input"];
|
|
2216
|
+
if (Array.isArray(input)) {
|
|
2217
|
+
const modalities = [...new Set(input.filter((item) => typeof item === "string" && item.length > 0))];
|
|
2218
|
+
if (modalities.length > 0) model.inputModalities = modalities;
|
|
2219
|
+
}
|
|
2220
|
+
out.push(model);
|
|
2221
|
+
}
|
|
2222
|
+
return out;
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2225
|
+
// src/discovery-merge.ts
|
|
2226
|
+
async function probeListing(probe) {
|
|
2227
|
+
const url = probeListingUrl(probe.baseURL, probe.api);
|
|
2228
|
+
const send = probe.send ?? ((input, init) => fetch(input, init));
|
|
2229
|
+
try {
|
|
2230
|
+
const response = await send(url, {
|
|
2231
|
+
method: "GET",
|
|
2232
|
+
headers: composeProbeHeaders(probe.headers, probe.apiKey, probe.api),
|
|
2233
|
+
redirect: "error",
|
|
2234
|
+
signal: AbortSignal.timeout(probe.timeoutMs)
|
|
2235
|
+
});
|
|
2236
|
+
if (!response.ok) {
|
|
2237
|
+
await response.body?.cancel().catch(() => {
|
|
2238
|
+
});
|
|
2239
|
+
return void 0;
|
|
2240
|
+
}
|
|
2241
|
+
const text = await readBounded(response, url);
|
|
2242
|
+
return modelsFromListing(JSON.parse(text));
|
|
2243
|
+
} catch {
|
|
2244
|
+
return void 0;
|
|
2245
|
+
}
|
|
2246
|
+
}
|
|
2247
|
+
|
|
2248
|
+
// src/key-probe.ts
|
|
2249
|
+
var FAMILY_PROTOCOL = {
|
|
2250
|
+
openai: "openai-completions",
|
|
2251
|
+
anthropic: "anthropic-messages",
|
|
2252
|
+
gemini: "google-generative-ai"
|
|
2253
|
+
};
|
|
2254
|
+
function knownWire(api) {
|
|
2255
|
+
return api === "anthropic-messages" || api === "openai-completions" || api === "openai-responses" || api === "google-generative-ai" ? api : void 0;
|
|
2256
|
+
}
|
|
2257
|
+
function address(value) {
|
|
2258
|
+
if (typeof value !== "string") return void 0;
|
|
2259
|
+
const trimmed = value.trim();
|
|
2260
|
+
return trimmed.length === 0 ? void 0 : trimmed;
|
|
2261
|
+
}
|
|
2262
|
+
function probePlanFor(intent) {
|
|
2263
|
+
const wire = knownWire(intent.api);
|
|
2264
|
+
const family = wire === void 0 ? void 0 : endpointFamilyOf(wire);
|
|
2265
|
+
const baseURL = address(intent.baseURL);
|
|
2266
|
+
const targets = [];
|
|
2267
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2268
|
+
const push = (target) => {
|
|
2269
|
+
if (!LISTABLE_PROTOCOLS.has(target.api)) return;
|
|
2270
|
+
const key = `${target.api}\0${probeListingUrl(target.baseURL, target.api)}`;
|
|
2271
|
+
if (seen.has(key)) return;
|
|
2272
|
+
seen.add(key);
|
|
2273
|
+
targets.push(target);
|
|
2274
|
+
};
|
|
2275
|
+
if (intent.pinned !== void 0) {
|
|
2276
|
+
const pinFamily = endpointFamilyOf(intent.pinned);
|
|
2277
|
+
const pinnedBase = pinFamily === family ? baseURL ?? address(intent.endpoints?.[pinFamily]) : address(intent.endpoints?.[pinFamily]);
|
|
2278
|
+
if (pinnedBase === void 0) {
|
|
2279
|
+
return {
|
|
2280
|
+
targets: [],
|
|
2281
|
+
skipped: `the key pins ${intent.pinned} but this provider has no ${pinFamily} address`
|
|
2282
|
+
};
|
|
2283
|
+
}
|
|
2284
|
+
push({ api: intent.pinned, baseURL: pinnedBase, family: pinFamily });
|
|
2285
|
+
return { targets };
|
|
2286
|
+
}
|
|
2287
|
+
if (wire !== void 0 && family !== void 0) {
|
|
2288
|
+
if (baseURL !== void 0) push({ api: wire, baseURL, family });
|
|
2289
|
+
for (const other of ENDPOINT_FAMILIES) {
|
|
2290
|
+
if (other === family) continue;
|
|
2291
|
+
const otherBase = address(intent.endpoints?.[other]);
|
|
2292
|
+
if (otherBase !== void 0) push({ api: FAMILY_PROTOCOL[other], baseURL: otherBase, family: other });
|
|
2293
|
+
}
|
|
2294
|
+
return { targets };
|
|
2295
|
+
}
|
|
2296
|
+
for (const configured of ENDPOINT_FAMILIES) {
|
|
2297
|
+
const configuredBase = address(intent.endpoints?.[configured]);
|
|
2298
|
+
if (configuredBase !== void 0) push({ api: FAMILY_PROTOCOL[configured], baseURL: configuredBase, family: configured });
|
|
2299
|
+
}
|
|
2300
|
+
return {
|
|
2301
|
+
targets,
|
|
2302
|
+
...targets.length === 0 ? { skipped: `this provider cannot be interrogated on ${String(intent.api)}; set each key's models by hand` } : {}
|
|
2303
|
+
};
|
|
2304
|
+
}
|
|
2305
|
+
async function probeKeyModels(options) {
|
|
2306
|
+
const plan = probePlanFor(options.intent);
|
|
2307
|
+
const outcomes = [];
|
|
2308
|
+
const ids = [];
|
|
2309
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2310
|
+
let answered = 0;
|
|
2311
|
+
for (const target of plan.targets) {
|
|
2312
|
+
const url = probeListingUrl(target.baseURL, target.api);
|
|
2313
|
+
const reported = displayUrl(url);
|
|
2314
|
+
let response;
|
|
2315
|
+
try {
|
|
2316
|
+
response = await options.send(url, {
|
|
2317
|
+
method: "GET",
|
|
2318
|
+
headers: options.headersFor(options.profileHeaders, options.apiKey, target.api),
|
|
2319
|
+
redirect: "error",
|
|
2320
|
+
signal: AbortSignal.timeout(options.timeoutMs)
|
|
2321
|
+
});
|
|
2322
|
+
} catch (error) {
|
|
2323
|
+
outcomes.push({
|
|
2324
|
+
api: target.api,
|
|
2325
|
+
family: target.family,
|
|
2326
|
+
url: reported,
|
|
2327
|
+
ok: false,
|
|
2328
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2329
|
+
});
|
|
2330
|
+
continue;
|
|
2331
|
+
}
|
|
2332
|
+
if (!response.ok) {
|
|
2333
|
+
await response.body?.cancel().catch(() => {
|
|
2334
|
+
});
|
|
2335
|
+
outcomes.push({ api: target.api, family: target.family, url: reported, ok: false, status: response.status });
|
|
2336
|
+
continue;
|
|
2337
|
+
}
|
|
2338
|
+
let entries;
|
|
2339
|
+
try {
|
|
2340
|
+
entries = options.entries(JSON.parse(await options.read(response, url)));
|
|
2341
|
+
} catch (error) {
|
|
2342
|
+
outcomes.push({
|
|
2343
|
+
api: target.api,
|
|
2344
|
+
family: target.family,
|
|
2345
|
+
url: reported,
|
|
2346
|
+
ok: false,
|
|
2347
|
+
status: response.status,
|
|
2348
|
+
error: error instanceof Error ? error.message : String(error)
|
|
2349
|
+
});
|
|
2350
|
+
continue;
|
|
2351
|
+
}
|
|
2352
|
+
if (entries === void 0) {
|
|
2353
|
+
outcomes.push({
|
|
2354
|
+
api: target.api,
|
|
2355
|
+
family: target.family,
|
|
2356
|
+
url: reported,
|
|
2357
|
+
ok: false,
|
|
2358
|
+
status: response.status,
|
|
2359
|
+
error: "the reply names no model listing"
|
|
2360
|
+
});
|
|
2361
|
+
continue;
|
|
2362
|
+
}
|
|
2363
|
+
answered += 1;
|
|
2364
|
+
for (const entry of entries) {
|
|
2365
|
+
const id = entry["id"];
|
|
2366
|
+
if (typeof id !== "string" || id.length === 0 || seen.has(id)) continue;
|
|
2367
|
+
seen.add(id);
|
|
2368
|
+
ids.push(id);
|
|
2369
|
+
}
|
|
2370
|
+
outcomes.push({
|
|
2371
|
+
api: target.api,
|
|
2372
|
+
family: target.family,
|
|
2373
|
+
url: reported,
|
|
2374
|
+
ok: true,
|
|
2375
|
+
status: response.status,
|
|
2376
|
+
models: entries.length
|
|
2377
|
+
});
|
|
2378
|
+
}
|
|
2379
|
+
return {
|
|
2380
|
+
ref: options.ref,
|
|
2381
|
+
models: answered === 0 ? void 0 : ids,
|
|
2382
|
+
targets: outcomes,
|
|
2383
|
+
...plan.skipped === void 0 ? {} : { skipped: plan.skipped }
|
|
2384
|
+
};
|
|
2385
|
+
}
|
|
2386
|
+
|
|
2387
|
+
// src/multi-key-listing.ts
|
|
2388
|
+
var DEFAULT_PROBE_CONCURRENCY = 4;
|
|
2389
|
+
var MODEL_ORDER = new Intl.Collator("en", { numeric: true, sensitivity: "base" });
|
|
2390
|
+
function idsOf(models) {
|
|
2391
|
+
if (models === void 0) return [];
|
|
2392
|
+
const out = [];
|
|
2393
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2394
|
+
for (const id of models) {
|
|
2395
|
+
const trimmed = typeof id === "string" ? id.trim() : "";
|
|
2396
|
+
if (trimmed.length === 0 || seen.has(trimmed)) continue;
|
|
2397
|
+
seen.add(trimmed);
|
|
2398
|
+
out.push(trimmed);
|
|
2399
|
+
}
|
|
2400
|
+
return out;
|
|
2401
|
+
}
|
|
2402
|
+
function mergeKeyListings(results, lastKnown) {
|
|
2403
|
+
const order = [];
|
|
2404
|
+
const keysByModel = /* @__PURE__ */ new Map();
|
|
2405
|
+
const answered = [];
|
|
2406
|
+
const unreadable = [];
|
|
2407
|
+
const retained = [];
|
|
2408
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
2409
|
+
const add = (ref, ids) => {
|
|
2410
|
+
for (const id of ids) {
|
|
2411
|
+
const owners = keysByModel.get(id);
|
|
2412
|
+
if (owners === void 0) {
|
|
2413
|
+
keysByModel.set(id, [ref]);
|
|
2414
|
+
order.push(id);
|
|
2415
|
+
} else if (!owners.includes(ref)) {
|
|
2416
|
+
owners.push(ref);
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
};
|
|
2420
|
+
for (const result of results) {
|
|
2421
|
+
if (seenKeys.has(result.ref)) continue;
|
|
2422
|
+
seenKeys.add(result.ref);
|
|
2423
|
+
if (result.models !== void 0) {
|
|
2424
|
+
answered.push(result.ref);
|
|
2425
|
+
add(result.ref, idsOf(result.models));
|
|
2426
|
+
continue;
|
|
2427
|
+
}
|
|
2428
|
+
unreadable.push(result.ref);
|
|
2429
|
+
const known = idsOf(lastKnown?.get(result.ref));
|
|
2430
|
+
if (known.length === 0) continue;
|
|
2431
|
+
retained.push(result.ref);
|
|
2432
|
+
add(result.ref, known);
|
|
2433
|
+
}
|
|
2434
|
+
order.sort(MODEL_ORDER.compare);
|
|
2435
|
+
const models = order.map((id) => ({ id, keys: keysByModel.get(id) ?? [] }));
|
|
2436
|
+
return {
|
|
2437
|
+
models,
|
|
2438
|
+
ids: order,
|
|
2439
|
+
answered,
|
|
2440
|
+
unreadable,
|
|
2441
|
+
retained
|
|
2442
|
+
};
|
|
2443
|
+
}
|
|
2444
|
+
async function collectKeyListings(options) {
|
|
2445
|
+
const total = options.keys.length;
|
|
2446
|
+
const results = new Array(total);
|
|
2447
|
+
const bound = Math.max(1, Math.min(options.concurrency ?? DEFAULT_PROBE_CONCURRENCY, Math.max(total, 1)));
|
|
2448
|
+
let next = 0;
|
|
2449
|
+
const worker = async () => {
|
|
2450
|
+
for (; ; ) {
|
|
2451
|
+
const index = next;
|
|
2452
|
+
next += 1;
|
|
2453
|
+
if (index >= total) return;
|
|
2454
|
+
const key = options.keys[index];
|
|
2455
|
+
try {
|
|
2456
|
+
results[index] = await options.probe(key);
|
|
2457
|
+
} catch {
|
|
2458
|
+
const ref = options.refOf?.(key);
|
|
2459
|
+
if (ref !== void 0) results[index] = { ref, models: void 0, targets: [] };
|
|
2460
|
+
}
|
|
2461
|
+
}
|
|
2462
|
+
};
|
|
2463
|
+
await Promise.all(Array.from({ length: bound }, async () => worker()));
|
|
2464
|
+
const settled = results.filter((result) => result !== void 0);
|
|
2465
|
+
return { results: settled, merged: mergeKeyListings(settled, options.lastKnown) };
|
|
2466
|
+
}
|
|
2467
|
+
|
|
1727
2468
|
// src/headers-core.ts
|
|
1728
2469
|
var MAX_UA_LENGTH = 512;
|
|
1729
2470
|
function emptyIndex() {
|
|
@@ -1941,6 +2682,37 @@ async function bodyTextOf(input, init) {
|
|
|
1941
2682
|
}
|
|
1942
2683
|
return void 0;
|
|
1943
2684
|
}
|
|
2685
|
+
function isModelListingRequest(url, method) {
|
|
2686
|
+
if ((method ?? "GET").toUpperCase() !== "GET") return false;
|
|
2687
|
+
try {
|
|
2688
|
+
return new URL(url).pathname.endsWith("/models");
|
|
2689
|
+
} catch {
|
|
2690
|
+
return false;
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
function listingCouldExceedPlainRequest(keys) {
|
|
2694
|
+
if (keys.length > 1) return true;
|
|
2695
|
+
return keys.some((candidate) => candidate.endpoint !== void 0 || candidate.endpoints !== void 0 && Object.keys(candidate.endpoints).length > 0);
|
|
2696
|
+
}
|
|
2697
|
+
async function readBoundedText(response, limit) {
|
|
2698
|
+
const body = response.clone().body;
|
|
2699
|
+
if (body === null) return "";
|
|
2700
|
+
const reader = body.getReader();
|
|
2701
|
+
const decoder = new TextDecoder();
|
|
2702
|
+
let text = "";
|
|
2703
|
+
try {
|
|
2704
|
+
for (; ; ) {
|
|
2705
|
+
const { done, value } = await reader.read();
|
|
2706
|
+
if (done) break;
|
|
2707
|
+
text += decoder.decode(value, { stream: true });
|
|
2708
|
+
if (text.length >= limit) break;
|
|
2709
|
+
}
|
|
2710
|
+
} finally {
|
|
2711
|
+
await reader.cancel().catch(() => {
|
|
2712
|
+
});
|
|
2713
|
+
}
|
|
2714
|
+
return text.slice(0, limit);
|
|
2715
|
+
}
|
|
1944
2716
|
function seedHeaders(input, init) {
|
|
1945
2717
|
if (init?.headers !== void 0 && init.headers !== null) return new Headers(init.headers);
|
|
1946
2718
|
if (typeof Request !== "undefined" && input instanceof Request) return new Headers(input.headers);
|
|
@@ -1950,32 +2722,119 @@ function makeWrapper(base, entries) {
|
|
|
1950
2722
|
return async function fetchWithProviderHeaders(input, init) {
|
|
1951
2723
|
const url = requestUrlOf(input);
|
|
1952
2724
|
if (url === void 0) return base(input, init);
|
|
2725
|
+
let entry;
|
|
1953
2726
|
let candidates = [];
|
|
1954
|
-
for (const
|
|
1955
|
-
|
|
1956
|
-
if (
|
|
2727
|
+
for (const candidate of entries.values()) {
|
|
2728
|
+
const found = pathCandidatesOf(candidate.index(), url);
|
|
2729
|
+
if (found.length > 0) {
|
|
2730
|
+
entry = candidate;
|
|
2731
|
+
candidates = found;
|
|
2732
|
+
break;
|
|
2733
|
+
}
|
|
1957
2734
|
}
|
|
1958
|
-
if (candidates.length === 0) return base(input, init);
|
|
1959
|
-
|
|
1960
|
-
|
|
2735
|
+
if (entry === void 0 || candidates.length === 0) return base(input, init);
|
|
2736
|
+
const keyed = entry.keys !== void 0;
|
|
2737
|
+
let model;
|
|
2738
|
+
if (candidates.length > 1 || keyed) {
|
|
1961
2739
|
const body = await bodyTextOf(input, init);
|
|
1962
|
-
|
|
1963
|
-
override = selectOverrideFrom(candidates, model);
|
|
2740
|
+
model = body === void 0 ? void 0 : modelOfJsonBody(body);
|
|
1964
2741
|
}
|
|
2742
|
+
const override = candidates.length === 1 ? candidates[0] : selectOverrideFrom(candidates, model);
|
|
1965
2743
|
if (override === void 0) return base(input, init);
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
2744
|
+
const patched = seedHeaders(input, init);
|
|
2745
|
+
let hasPatch = false;
|
|
2746
|
+
if (override.hasHeaders) {
|
|
2747
|
+
for (const [name2, value] of Object.entries(override.headers)) {
|
|
2748
|
+
try {
|
|
2749
|
+
patched.set(name2, value);
|
|
2750
|
+
hasPatch = true;
|
|
2751
|
+
} catch {
|
|
2752
|
+
}
|
|
2753
|
+
}
|
|
2754
|
+
}
|
|
2755
|
+
const attempt = (keyValue) => {
|
|
2756
|
+
const headers = new Headers(patched);
|
|
2757
|
+
if (keyValue !== void 0) applyKeyToHeaders(headers, keyValue);
|
|
2758
|
+
if (typeof Request !== "undefined" && input instanceof Request) {
|
|
2759
|
+
return base(new Request(input.clone(), { headers }), { ...init, headers });
|
|
2760
|
+
}
|
|
2761
|
+
return base(input, { ...init, headers });
|
|
2762
|
+
};
|
|
2763
|
+
if (patched.get(WIRE_DISPATCH_HEADER) === "1") {
|
|
2764
|
+
patched.delete(WIRE_DISPATCH_HEADER);
|
|
2765
|
+
if (!hasPatch) return base(input, init);
|
|
2766
|
+
return attempt(void 0);
|
|
2767
|
+
}
|
|
2768
|
+
const resolved = keyed ? await entry.keys({ route: override.route, model }) : [];
|
|
2769
|
+
const keyCandidates = restLast(
|
|
2770
|
+
resolved,
|
|
2771
|
+
(candidate) => keyRest.isResting(restKeyOf(override.route, candidate.ref), Date.now())
|
|
2772
|
+
);
|
|
2773
|
+
const method = init?.method ?? (typeof Request !== "undefined" && input instanceof Request ? input.method : void 0);
|
|
2774
|
+
if (entry.modelListing !== void 0 && isModelListingRequest(url, method) && listingCouldExceedPlainRequest(keyCandidates)) {
|
|
2775
|
+
const signal = init?.signal ?? (typeof Request !== "undefined" && input instanceof Request ? input.signal : void 0);
|
|
1969
2776
|
try {
|
|
1970
|
-
|
|
2777
|
+
const listing = await entry.modelListing({
|
|
2778
|
+
route: override.route,
|
|
2779
|
+
url,
|
|
2780
|
+
headers: new Headers(patched),
|
|
2781
|
+
keys: keyCandidates,
|
|
2782
|
+
...signal === void 0 ? {} : { signal },
|
|
2783
|
+
send: base
|
|
2784
|
+
});
|
|
2785
|
+
if (listing !== void 0) return listing;
|
|
2786
|
+
} catch {
|
|
2787
|
+
}
|
|
2788
|
+
}
|
|
2789
|
+
if (!hasPatch && keyCandidates.length === 0) return base(input, init);
|
|
2790
|
+
const chargeFailure = async (candidate, response) => {
|
|
2791
|
+
const now = Date.now();
|
|
2792
|
+
const retryAfterMs = parseRetryAfter(response.headers.get("retry-after"), now);
|
|
2793
|
+
let bodyText = "";
|
|
2794
|
+
try {
|
|
2795
|
+
bodyText = await readBoundedText(response, 4096);
|
|
1971
2796
|
} catch {
|
|
1972
2797
|
}
|
|
2798
|
+
const kind = classifyFailure(response.status, bodyText);
|
|
2799
|
+
if (kind === void 0) return;
|
|
2800
|
+
keyRest.note(
|
|
2801
|
+
restKeyOf(override.route, candidate.ref),
|
|
2802
|
+
kind,
|
|
2803
|
+
retryAfterMs === void 0 ? { now } : { now, retryAfterMs }
|
|
2804
|
+
);
|
|
2805
|
+
};
|
|
2806
|
+
const callerAborted = () => init?.signal?.aborted === true || typeof Request !== "undefined" && input instanceof Request && input.signal.aborted;
|
|
2807
|
+
if (keyCandidates.length <= 1) {
|
|
2808
|
+
const solo = keyCandidates[0];
|
|
2809
|
+
const response = await attempt(solo?.value);
|
|
2810
|
+
if (solo !== void 0) {
|
|
2811
|
+
if (shouldFailover(response.status)) await chargeFailure(solo, response);
|
|
2812
|
+
else keyRest.clear(restKeyOf(override.route, solo.ref));
|
|
2813
|
+
}
|
|
2814
|
+
return response;
|
|
1973
2815
|
}
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
2816
|
+
let lastFailure;
|
|
2817
|
+
for (let index = 0; index < keyCandidates.length; index += 1) {
|
|
2818
|
+
const candidate = keyCandidates[index];
|
|
2819
|
+
let response;
|
|
2820
|
+
try {
|
|
2821
|
+
response = await attempt(candidate.value);
|
|
2822
|
+
} catch (error) {
|
|
2823
|
+
if (callerAborted()) throw error;
|
|
2824
|
+
lastFailure = error;
|
|
2825
|
+
keyRest.note(restKeyOf(override.route, candidate.ref), "refused");
|
|
2826
|
+
continue;
|
|
2827
|
+
}
|
|
2828
|
+
if (!shouldFailover(response.status)) {
|
|
2829
|
+
keyRest.clear(restKeyOf(override.route, candidate.ref));
|
|
2830
|
+
return response;
|
|
2831
|
+
}
|
|
2832
|
+
await chargeFailure(candidate, response);
|
|
2833
|
+
if (index === keyCandidates.length - 1) return response;
|
|
2834
|
+
await response.body?.cancel().catch(() => {
|
|
2835
|
+
});
|
|
1977
2836
|
}
|
|
1978
|
-
|
|
2837
|
+
throw lastFailure ?? new Error("no key could place the request");
|
|
1979
2838
|
};
|
|
1980
2839
|
}
|
|
1981
2840
|
function installHeaderOverlay(source) {
|
|
@@ -1990,7 +2849,13 @@ function installHeaderOverlay(source) {
|
|
|
1990
2849
|
holder[REGISTRY] = registry;
|
|
1991
2850
|
globalThis.fetch = wrapper;
|
|
1992
2851
|
}
|
|
1993
|
-
registry.entries.set(token, {
|
|
2852
|
+
registry.entries.set(token, {
|
|
2853
|
+
index: () => source.current,
|
|
2854
|
+
// Delegate through the live source property so a resolver attached after
|
|
2855
|
+
// install is honoured, and its internal data stays current.
|
|
2856
|
+
keys: source.keys === void 0 ? void 0 : (context) => source.keys(context),
|
|
2857
|
+
modelListing: source.modelListing === void 0 ? void 0 : (context) => source.modelListing(context)
|
|
2858
|
+
});
|
|
1994
2859
|
const owned = registry;
|
|
1995
2860
|
return {
|
|
1996
2861
|
get active() {
|
|
@@ -2008,8 +2873,70 @@ function headerOverlayInstalled() {
|
|
|
2008
2873
|
const registry = registryOf();
|
|
2009
2874
|
return registry !== void 0 && globalThis.fetch === registry.wrapper;
|
|
2010
2875
|
}
|
|
2876
|
+
function headerOverlayBypass() {
|
|
2877
|
+
const registry = registryOf();
|
|
2878
|
+
if (registry !== void 0 && globalThis.fetch === registry.wrapper) return registry.base;
|
|
2879
|
+
return globalThis.fetch;
|
|
2880
|
+
}
|
|
2011
2881
|
|
|
2012
2882
|
// src/headers-pi-ai.ts
|
|
2883
|
+
var servedWire = /* @__PURE__ */ new Map();
|
|
2884
|
+
var failedWire = /* @__PURE__ */ new Map();
|
|
2885
|
+
var WIRE_MEMORY_LIMIT = 256;
|
|
2886
|
+
function rememberWireFact(map, key, wire) {
|
|
2887
|
+
if (map.size >= WIRE_MEMORY_LIMIT) map.clear();
|
|
2888
|
+
map.set(key, wire);
|
|
2889
|
+
}
|
|
2890
|
+
var PROTOCOL_CACHE = /* @__PURE__ */ new Map();
|
|
2891
|
+
var CONTEXT_NORMALIZER;
|
|
2892
|
+
function loadContextNormalizer() {
|
|
2893
|
+
if (CONTEXT_NORMALIZER !== void 0) return CONTEXT_NORMALIZER;
|
|
2894
|
+
CONTEXT_NORMALIZER = (async () => {
|
|
2895
|
+
try {
|
|
2896
|
+
const specifier = "@earendil-works/pi-ai/utils/transcript";
|
|
2897
|
+
const module = await import(
|
|
2898
|
+
/* @vite-ignore */
|
|
2899
|
+
specifier
|
|
2900
|
+
);
|
|
2901
|
+
return typeof module.normalizeContext === "function" ? module.normalizeContext : void 0;
|
|
2902
|
+
} catch {
|
|
2903
|
+
return void 0;
|
|
2904
|
+
}
|
|
2905
|
+
})();
|
|
2906
|
+
return CONTEXT_NORMALIZER;
|
|
2907
|
+
}
|
|
2908
|
+
var NORMALIZED_CONTEXTS = /* @__PURE__ */ new WeakMap();
|
|
2909
|
+
async function normalizeDispatchedContext(context) {
|
|
2910
|
+
const object = objectOf(context);
|
|
2911
|
+
if (object === void 0) return context;
|
|
2912
|
+
if (NORMALIZED_CONTEXTS.has(object)) return NORMALIZED_CONTEXTS.get(object);
|
|
2913
|
+
if (!Array.isArray(object["messages"])) return context;
|
|
2914
|
+
const normalize2 = await loadContextNormalizer();
|
|
2915
|
+
if (normalize2 === void 0) return context;
|
|
2916
|
+
const normalized = normalize2(context);
|
|
2917
|
+
if (objectOf(normalized) === void 0) return context;
|
|
2918
|
+
NORMALIZED_CONTEXTS.set(object, normalized);
|
|
2919
|
+
return normalized;
|
|
2920
|
+
}
|
|
2921
|
+
function loadProtocol(protocol) {
|
|
2922
|
+
let pending = PROTOCOL_CACHE.get(protocol);
|
|
2923
|
+
if (pending !== void 0) return pending;
|
|
2924
|
+
pending = (async () => {
|
|
2925
|
+
switch (protocol) {
|
|
2926
|
+
case "anthropic-messages":
|
|
2927
|
+
return await import("@earendil-works/pi-ai/api/anthropic-messages");
|
|
2928
|
+
case "openai-responses":
|
|
2929
|
+
return await import("@earendil-works/pi-ai/api/openai-responses");
|
|
2930
|
+
case "google-generative-ai":
|
|
2931
|
+
return await import("@earendil-works/pi-ai/api/google-generative-ai");
|
|
2932
|
+
case "openai-completions":
|
|
2933
|
+
default:
|
|
2934
|
+
return await import("@earendil-works/pi-ai/api/openai-completions");
|
|
2935
|
+
}
|
|
2936
|
+
})();
|
|
2937
|
+
PROTOCOL_CACHE.set(protocol, pending);
|
|
2938
|
+
return pending;
|
|
2939
|
+
}
|
|
2013
2940
|
var PATCH_STATE = /* @__PURE__ */ Symbol.for("dsh-model-think-level.pi-ai-header-transform");
|
|
2014
2941
|
var REGISTER_PATCH_STATE = /* @__PURE__ */ Symbol.for("dsh-model-think-level.pi-ai-header-register");
|
|
2015
2942
|
var CORDIS_ORIGINAL = /* @__PURE__ */ Symbol.for("cordis.original");
|
|
@@ -2040,6 +2967,11 @@ function routeOverrideOf(source, model) {
|
|
|
2040
2967
|
return void 0;
|
|
2041
2968
|
}
|
|
2042
2969
|
}
|
|
2970
|
+
function errorTextOf(event) {
|
|
2971
|
+
const error = isRecord(event["error"]) ? event["error"] : void 0;
|
|
2972
|
+
const parts = [error?.["message"], error?.["errorMessage"], event["reason"]];
|
|
2973
|
+
return parts.filter((part) => typeof part === "string").join(" ");
|
|
2974
|
+
}
|
|
2043
2975
|
function modelsProxyOf(models, source) {
|
|
2044
2976
|
return new Proxy(models, {
|
|
2045
2977
|
get(target, property, receiver) {
|
|
@@ -2060,10 +2992,125 @@ function modelsProxyOf(models, source) {
|
|
|
2060
2992
|
const override = routeOverrideOf(source, model);
|
|
2061
2993
|
return override?.hasHeaders ? { ...merged, ...override.headers } : merged;
|
|
2062
2994
|
};
|
|
2063
|
-
|
|
2064
|
-
...
|
|
2065
|
-
|
|
2995
|
+
const markedTransformHeaders = async (headers) => ({
|
|
2996
|
+
...await transformHeaders(headers),
|
|
2997
|
+
[WIRE_DISPATCH_HEADER]: "1"
|
|
2066
2998
|
});
|
|
2999
|
+
const modelId = typeof modelObject["id"] === "string" ? modelObject["id"] : void 0;
|
|
3000
|
+
const keyResolver = source.keys;
|
|
3001
|
+
if (keyResolver === void 0) {
|
|
3002
|
+
return streamSimple.call(target, model, context, {
|
|
3003
|
+
...options,
|
|
3004
|
+
transformHeaders
|
|
3005
|
+
});
|
|
3006
|
+
}
|
|
3007
|
+
const route = routeOverrideOf(source, model)?.route;
|
|
3008
|
+
const dispatch = () => {
|
|
3009
|
+
if (route === void 0) return Promise.resolve([]);
|
|
3010
|
+
return Promise.resolve(keyResolver({ route, model: modelId })).catch(() => []);
|
|
3011
|
+
};
|
|
3012
|
+
const startAttempt = async (candidate, avoid) => {
|
|
3013
|
+
const api = typeof modelObject["api"] === "string" ? modelObject["api"] : "openai-completions";
|
|
3014
|
+
const protocol = wireOf(candidate, api, modelId, avoid);
|
|
3015
|
+
const honoured = isHonoured(candidate, api);
|
|
3016
|
+
const autoEndpoint = honoured && candidate.protocol === void 0 && protocol !== api ? candidate.endpoints?.[endpointFamilyOf(protocol)] : void 0;
|
|
3017
|
+
const selectedBaseUrl = honoured && candidate.endpoint !== void 0 ? candidate.endpoint : autoEndpoint !== void 0 ? autoEndpoint : modelObject["baseUrl"];
|
|
3018
|
+
const baseUrl = protocol === "anthropic-messages" ? anthropicRequestBaseUrl(selectedBaseUrl) : protocol === "google-generative-ai" ? geminiRequestBaseUrl(selectedBaseUrl) : selectedBaseUrl;
|
|
3019
|
+
const switched = protocol !== api || baseUrl !== modelObject["baseUrl"];
|
|
3020
|
+
if (!switched) {
|
|
3021
|
+
return streamSimple.call(target, model, context, {
|
|
3022
|
+
...options,
|
|
3023
|
+
apiKey: candidate.value,
|
|
3024
|
+
transformHeaders: markedTransformHeaders
|
|
3025
|
+
});
|
|
3026
|
+
}
|
|
3027
|
+
const mergedHeaders = await transformHeaders(isRecord(options["headers"]) ? options["headers"] : {});
|
|
3028
|
+
const implementation = await loadProtocol(protocol);
|
|
3029
|
+
const dispatchedContext = await normalizeDispatchedContext(context);
|
|
3030
|
+
return implementation.streamSimple(
|
|
3031
|
+
{ ...modelObject, api: protocol, baseUrl },
|
|
3032
|
+
dispatchedContext,
|
|
3033
|
+
{
|
|
3034
|
+
...options,
|
|
3035
|
+
apiKey: candidate.value,
|
|
3036
|
+
headers: { ...mergedHeaders, [WIRE_DISPATCH_HEADER]: "1" },
|
|
3037
|
+
transformHeaders: void 0
|
|
3038
|
+
}
|
|
3039
|
+
);
|
|
3040
|
+
};
|
|
3041
|
+
const run = async function* () {
|
|
3042
|
+
const routeCandidates = await dispatch();
|
|
3043
|
+
if (routeCandidates.length === 0) {
|
|
3044
|
+
const stream = streamSimple.call(target, model, context, {
|
|
3045
|
+
...options,
|
|
3046
|
+
transformHeaders
|
|
3047
|
+
});
|
|
3048
|
+
for await (const event of stream) yield event;
|
|
3049
|
+
return;
|
|
3050
|
+
}
|
|
3051
|
+
const api = typeof modelObject["api"] === "string" ? modelObject["api"] : "openai-completions";
|
|
3052
|
+
const servedKey = `${route ?? ""}\0${modelId ?? ""}`;
|
|
3053
|
+
const avoidWire = failedWire.get(servedKey);
|
|
3054
|
+
const plan = planKeys(routeCandidates, {
|
|
3055
|
+
api,
|
|
3056
|
+
model: modelId,
|
|
3057
|
+
preferredWire: servedWire.get(servedKey),
|
|
3058
|
+
avoidWire,
|
|
3059
|
+
isResting: (candidate) => keyRest.isResting(restKeyOf(route ?? "", candidate.ref), Date.now())
|
|
3060
|
+
});
|
|
3061
|
+
recordRoutePlan(route ?? "", plan, { model: modelId });
|
|
3062
|
+
const wireOfAttempt = (candidate) => plan.entries.find((entry) => entry.candidate === candidate)?.wire ?? wireOf(candidate, api, modelId, avoidWire);
|
|
3063
|
+
let lastErrorEvent;
|
|
3064
|
+
let lastError;
|
|
3065
|
+
for (const candidate of plan.ordered) {
|
|
3066
|
+
const restKey = restKeyOf(route ?? "", candidate.ref);
|
|
3067
|
+
let stream;
|
|
3068
|
+
try {
|
|
3069
|
+
stream = await startAttempt(candidate, avoidWire);
|
|
3070
|
+
} catch (error) {
|
|
3071
|
+
lastError = error;
|
|
3072
|
+
if (candidate.protocol === void 0) rememberWireFact(failedWire, servedKey, wireOfAttempt(candidate));
|
|
3073
|
+
keyRest.note(restKey, classifyFailure(void 0, error instanceof Error ? error.message : "") ?? "refused");
|
|
3074
|
+
continue;
|
|
3075
|
+
}
|
|
3076
|
+
let committed = false;
|
|
3077
|
+
let failed = false;
|
|
3078
|
+
for await (const event of stream) {
|
|
3079
|
+
const type = isRecord(event) ? event["type"] : void 0;
|
|
3080
|
+
if (!committed) {
|
|
3081
|
+
if (type === "error") {
|
|
3082
|
+
lastErrorEvent = event;
|
|
3083
|
+
failed = true;
|
|
3084
|
+
if (candidate.protocol === void 0) rememberWireFact(failedWire, servedKey, wireOfAttempt(candidate));
|
|
3085
|
+
keyRest.note(restKey, classifyFailure(void 0, errorTextOf(event)) ?? "refused");
|
|
3086
|
+
break;
|
|
3087
|
+
}
|
|
3088
|
+
committed = true;
|
|
3089
|
+
keyRest.clear(restKey);
|
|
3090
|
+
rememberWireFact(servedWire, servedKey, wireOfAttempt(candidate));
|
|
3091
|
+
}
|
|
3092
|
+
yield event;
|
|
3093
|
+
}
|
|
3094
|
+
if (failed) continue;
|
|
3095
|
+
return;
|
|
3096
|
+
}
|
|
3097
|
+
if (lastErrorEvent !== void 0) {
|
|
3098
|
+
yield lastErrorEvent;
|
|
3099
|
+
return;
|
|
3100
|
+
}
|
|
3101
|
+
yield {
|
|
3102
|
+
type: "error",
|
|
3103
|
+
reason: "error",
|
|
3104
|
+
error: {
|
|
3105
|
+
role: "assistant",
|
|
3106
|
+
content: [],
|
|
3107
|
+
stopReason: "error",
|
|
3108
|
+
errorMessage: lastError instanceof Error ? lastError.message : "No API key could serve this model.",
|
|
3109
|
+
usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0 }
|
|
3110
|
+
}
|
|
3111
|
+
};
|
|
3112
|
+
};
|
|
3113
|
+
return run();
|
|
2067
3114
|
};
|
|
2068
3115
|
}
|
|
2069
3116
|
});
|
|
@@ -2478,6 +3525,45 @@ function enabledRefOf(profile) {
|
|
|
2478
3525
|
const ref = profile?.["apiKeyEnv"];
|
|
2479
3526
|
return typeof ref === "string" && ref.length > 0 ? ref : void 0;
|
|
2480
3527
|
}
|
|
3528
|
+
function readRouteKeyScopes(providers) {
|
|
3529
|
+
let document;
|
|
3530
|
+
try {
|
|
3531
|
+
document = readKeyIndex(keyIndexFilePath());
|
|
3532
|
+
} catch {
|
|
3533
|
+
return {};
|
|
3534
|
+
}
|
|
3535
|
+
const scopes = {};
|
|
3536
|
+
const routes = new Set(Object.keys(document.providers));
|
|
3537
|
+
for (const route of Object.keys(providers)) routes.add(route);
|
|
3538
|
+
for (const route of routes) {
|
|
3539
|
+
const endpoints = document.endpoints?.[route];
|
|
3540
|
+
const listed = keyEntriesOf(document, route);
|
|
3541
|
+
const active = listed.filter((entry) => entry.disabled !== true);
|
|
3542
|
+
const ordered = active.map((entry) => {
|
|
3543
|
+
const scope = { ref: entry.ref };
|
|
3544
|
+
if (entry.models !== void 0) scope.models = entry.models;
|
|
3545
|
+
if (entry.protocol !== void 0) {
|
|
3546
|
+
;
|
|
3547
|
+
scope.protocol = entry.protocol;
|
|
3548
|
+
const family = endpointFamilyOf(entry.protocol);
|
|
3549
|
+
const endpoint = endpoints?.[family];
|
|
3550
|
+
if (endpoint !== void 0) scope.endpoint = endpoint;
|
|
3551
|
+
} else if (endpoints !== void 0) {
|
|
3552
|
+
;
|
|
3553
|
+
scope.endpoints = endpoints;
|
|
3554
|
+
}
|
|
3555
|
+
return scope;
|
|
3556
|
+
});
|
|
3557
|
+
const seat = enabledRefOf(isRecord(providers[route]) ? providers[route] : void 0);
|
|
3558
|
+
if (seat !== void 0 && !listed.some((entry) => entry.ref === seat)) {
|
|
3559
|
+
const seatScope = { ref: seat };
|
|
3560
|
+
if (endpoints !== void 0) seatScope.endpoints = endpoints;
|
|
3561
|
+
ordered.push(seatScope);
|
|
3562
|
+
}
|
|
3563
|
+
if (ordered.length > 0) scopes[route] = ordered;
|
|
3564
|
+
}
|
|
3565
|
+
return scopes;
|
|
3566
|
+
}
|
|
2481
3567
|
function isRefusal(answer) {
|
|
2482
3568
|
return isRecord(answer) && answer["ok"] === false;
|
|
2483
3569
|
}
|
|
@@ -2635,94 +3721,6 @@ function sendJson(res, status, body) {
|
|
|
2635
3721
|
res.setHeader("content-type", "application/json; charset=utf-8");
|
|
2636
3722
|
res.end(JSON.stringify(body));
|
|
2637
3723
|
}
|
|
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
3724
|
function guardCallConfig(cfg, section) {
|
|
2727
3725
|
try {
|
|
2728
3726
|
if (typeof cfg["reasoningEffort"] !== "undefined") return cfg;
|
|
@@ -2843,6 +3841,14 @@ function apply(ctx, config = {}) {
|
|
|
2843
3841
|
});
|
|
2844
3842
|
}
|
|
2845
3843
|
const overlaySource = { current: emptyIndex() };
|
|
3844
|
+
const keyScopes = { current: {} };
|
|
3845
|
+
const providersOf = () => {
|
|
3846
|
+
const section = piSection();
|
|
3847
|
+
return isRecord(section) && isRecord(section["providers"]) ? section["providers"] : {};
|
|
3848
|
+
};
|
|
3849
|
+
const refreshKeyScopes = () => {
|
|
3850
|
+
keyScopes.current = readRouteKeyScopes(providersOf());
|
|
3851
|
+
};
|
|
2846
3852
|
const headerRetryTimers = /* @__PURE__ */ new Set();
|
|
2847
3853
|
let headerRetryAttempt = 0;
|
|
2848
3854
|
const stopHeaderRetries = () => {
|
|
@@ -2877,10 +3883,109 @@ function apply(ctx, config = {}) {
|
|
|
2877
3883
|
overlaySource.current = buildHeaderIndex(providers);
|
|
2878
3884
|
};
|
|
2879
3885
|
refreshHeaders();
|
|
3886
|
+
refreshKeyScopes();
|
|
2880
3887
|
ctx.effect(() => () => stopHeaderRetries(), "dsh-model-think-level: header retries");
|
|
2881
3888
|
ctx.on("settings/document-updated", (ns) => {
|
|
2882
|
-
if (ns === PI_NS)
|
|
3889
|
+
if (ns === PI_NS) {
|
|
3890
|
+
refreshHeaders();
|
|
3891
|
+
refreshKeyScopes();
|
|
3892
|
+
}
|
|
2883
3893
|
});
|
|
3894
|
+
overlaySource.keys = async ({ route, model }) => {
|
|
3895
|
+
const scopes = keyScopes.current[route] ?? [];
|
|
3896
|
+
if (scopes.length === 0) return [];
|
|
3897
|
+
const ordered = keysForModel(
|
|
3898
|
+
scopes.map((scope) => ({ ref: scope.ref, value: "", models: scope.models })),
|
|
3899
|
+
model
|
|
3900
|
+
);
|
|
3901
|
+
const credentials = ctx.get("credentials");
|
|
3902
|
+
const values = await Promise.all(ordered.map(async (candidate) => {
|
|
3903
|
+
try {
|
|
3904
|
+
return await resolveCredential(credentials, candidate.ref);
|
|
3905
|
+
} catch {
|
|
3906
|
+
return void 0;
|
|
3907
|
+
}
|
|
3908
|
+
}));
|
|
3909
|
+
const candidates = [];
|
|
3910
|
+
for (let index = 0; index < ordered.length; index += 1) {
|
|
3911
|
+
const value = values[index];
|
|
3912
|
+
if (value === void 0) continue;
|
|
3913
|
+
const candidate = ordered[index];
|
|
3914
|
+
const scope = scopes.find((entry) => entry.ref === candidate.ref);
|
|
3915
|
+
const resolved2 = { ref: candidate.ref, value, models: candidate.models };
|
|
3916
|
+
if (scope?.protocol !== void 0) resolved2.protocol = scope.protocol;
|
|
3917
|
+
if (scope?.endpoint !== void 0) resolved2.endpoint = scope.endpoint;
|
|
3918
|
+
if (scope?.endpoints !== void 0) resolved2.endpoints = scope.endpoints;
|
|
3919
|
+
candidates.push(resolved2);
|
|
3920
|
+
}
|
|
3921
|
+
return candidates;
|
|
3922
|
+
};
|
|
3923
|
+
overlaySource.modelListing = async ({ route, headers, keys, signal, send }) => {
|
|
3924
|
+
const profile = providerProfileOf(piSection(), route);
|
|
3925
|
+
const api = normalizeKeyProtocol(profile?.["api"]);
|
|
3926
|
+
const baseURL = typeof profile?.["baseURL"] === "string" ? profile["baseURL"].trim() : "";
|
|
3927
|
+
if (api === void 0 || !LISTABLE_PROTOCOLS.has(api) || baseURL.length === 0) return void 0;
|
|
3928
|
+
const recordsByRef = /* @__PURE__ */ new Map();
|
|
3929
|
+
const { results } = await collectKeyListings({
|
|
3930
|
+
keys,
|
|
3931
|
+
refOf: (key) => key.ref,
|
|
3932
|
+
probe: async (key) => {
|
|
3933
|
+
const targets = key.protocol !== void 0 ? [{ api: key.protocol, baseURL: key.endpoint ?? baseURL }] : [
|
|
3934
|
+
{ api, baseURL },
|
|
3935
|
+
...Object.entries(key.endpoints ?? {}).filter(([family]) => family !== endpointFamilyOf(api)).map(([family, url]) => ({ api: FAMILY_PROTOCOL[family], baseURL: url ?? "" }))
|
|
3936
|
+
];
|
|
3937
|
+
const sendListing = (url, init) => {
|
|
3938
|
+
const requestHeaders = new Headers(headers);
|
|
3939
|
+
requestHeaders.delete("authorization");
|
|
3940
|
+
requestHeaders.delete("x-api-key");
|
|
3941
|
+
requestHeaders.delete("x-goog-api-key");
|
|
3942
|
+
new Headers(init.headers).forEach((value, name2) => requestHeaders.set(name2, value));
|
|
3943
|
+
const requestSignal = signal == null ? init.signal : init.signal == null ? signal : AbortSignal.any([signal, init.signal]);
|
|
3944
|
+
return send(url, { ...init, headers: requestHeaders, signal: requestSignal });
|
|
3945
|
+
};
|
|
3946
|
+
const asked = /* @__PURE__ */ new Set();
|
|
3947
|
+
const records2 = [];
|
|
3948
|
+
for (const target of targets) {
|
|
3949
|
+
if (!LISTABLE_PROTOCOLS.has(target.api) || target.baseURL.length === 0) continue;
|
|
3950
|
+
const url = probeListingUrl(target.baseURL, target.api);
|
|
3951
|
+
if (asked.has(url)) continue;
|
|
3952
|
+
asked.add(url);
|
|
3953
|
+
const listing = await probeListing({
|
|
3954
|
+
baseURL: target.baseURL,
|
|
3955
|
+
api: target.api,
|
|
3956
|
+
apiKey: key.value,
|
|
3957
|
+
headers: profile?.["headers"],
|
|
3958
|
+
timeoutMs: resolved.probeTimeoutMs,
|
|
3959
|
+
send: sendListing
|
|
3960
|
+
});
|
|
3961
|
+
if (listing === void 0) continue;
|
|
3962
|
+
for (const model of listing) {
|
|
3963
|
+
if (records2.some((existing) => existing.id === model.id)) continue;
|
|
3964
|
+
records2.push(model);
|
|
3965
|
+
}
|
|
3966
|
+
}
|
|
3967
|
+
if (records2.length === 0) return { ref: key.ref, models: void 0, targets: [] };
|
|
3968
|
+
recordsByRef.set(key.ref, records2);
|
|
3969
|
+
return {
|
|
3970
|
+
ref: key.ref,
|
|
3971
|
+
models: records2.map((model) => model.id),
|
|
3972
|
+
targets: []
|
|
3973
|
+
};
|
|
3974
|
+
}
|
|
3975
|
+
});
|
|
3976
|
+
const records = /* @__PURE__ */ new Map();
|
|
3977
|
+
for (const result of results) {
|
|
3978
|
+
for (const model of recordsByRef.get(result.ref) ?? []) {
|
|
3979
|
+
if (!records.has(model.id)) records.set(model.id, model);
|
|
3980
|
+
}
|
|
3981
|
+
}
|
|
3982
|
+
const models = mergeKeyListings(results).ids.map((id) => records.get(id)).filter((model) => model !== void 0);
|
|
3983
|
+
if (models.length === 0) return void 0;
|
|
3984
|
+
return new Response(JSON.stringify({ data: models }), {
|
|
3985
|
+
status: 200,
|
|
3986
|
+
headers: { "content-type": "application/json" }
|
|
3987
|
+
});
|
|
3988
|
+
};
|
|
2884
3989
|
const overlay = installHeaderOverlay(overlaySource);
|
|
2885
3990
|
ctx.effect(() => () => {
|
|
2886
3991
|
overlay.dispose();
|
|
@@ -2974,7 +4079,7 @@ function apply(ctx, config = {}) {
|
|
|
2974
4079
|
}
|
|
2975
4080
|
}
|
|
2976
4081
|
try {
|
|
2977
|
-
const upstream = await
|
|
4082
|
+
const upstream = await headerOverlayBypass()(listingURL, {
|
|
2978
4083
|
method: "GET",
|
|
2979
4084
|
// Header composition mirrors the harness's own model discovery:
|
|
2980
4085
|
// the profile's configured request headers ride
|
|
@@ -3172,24 +4277,62 @@ function apply(ctx, config = {}) {
|
|
|
3172
4277
|
return "the settings document kept changing; try again";
|
|
3173
4278
|
};
|
|
3174
4279
|
const payload = async () => {
|
|
3175
|
-
const
|
|
3176
|
-
const
|
|
4280
|
+
const document = readKeyIndex(indexFile);
|
|
4281
|
+
const seat = enabledNow();
|
|
4282
|
+
const listed = keyEntriesOf(document, route);
|
|
3177
4283
|
const refs = listed.map((entry) => entry.ref);
|
|
3178
|
-
if (
|
|
4284
|
+
if (seat !== void 0 && !refs.includes(seat)) refs.push(seat);
|
|
3179
4285
|
const facts = await credentialFacts(credentials, refs);
|
|
4286
|
+
const now = Date.now();
|
|
3180
4287
|
const entries = refs.map((ref2) => {
|
|
3181
4288
|
const fact = facts[ref2];
|
|
4289
|
+
const stored = listed.find((candidate) => candidate.ref === ref2);
|
|
3182
4290
|
const view = {
|
|
3183
4291
|
ref: ref2,
|
|
3184
|
-
|
|
4292
|
+
// Being switched on is the default: a key the index has
|
|
4293
|
+
// never described (the seat) reads as in use.
|
|
4294
|
+
enabled: stored?.disabled !== true,
|
|
3185
4295
|
configured: fact?.configured ?? null
|
|
3186
4296
|
};
|
|
3187
4297
|
if (fact?.masked !== void 0) view.masked = fact.masked;
|
|
3188
|
-
const stored = listed.find((candidate) => candidate.ref === ref2);
|
|
3189
4298
|
if (stored?.alias !== void 0) view.alias = stored.alias;
|
|
4299
|
+
if (stored?.models !== void 0) view.models = stored.models;
|
|
4300
|
+
if (stored?.protocol !== void 0) view.protocol = stored.protocol;
|
|
4301
|
+
const rest = keyRest.stateOf(restKeyOf(route, ref2), now);
|
|
4302
|
+
if (rest !== void 0) {
|
|
4303
|
+
;
|
|
4304
|
+
view.rest = {
|
|
4305
|
+
kind: rest.kind,
|
|
4306
|
+
remainingMs: Math.max(0, rest.until - now),
|
|
4307
|
+
failures: rest.failures
|
|
4308
|
+
};
|
|
4309
|
+
}
|
|
3190
4310
|
return view;
|
|
3191
4311
|
});
|
|
3192
|
-
|
|
4312
|
+
const endpoints = document.endpoints?.[route];
|
|
4313
|
+
const routeModels = modelsOf(providersOf(), route).map((model) => typeof model["id"] === "string" ? model["id"] : "").filter((id) => id.length > 0);
|
|
4314
|
+
const merged = mergeKeyListings(
|
|
4315
|
+
listed.map((entry) => ({
|
|
4316
|
+
ref: entry.ref,
|
|
4317
|
+
targets: [],
|
|
4318
|
+
models: entry.models === void 0 ? void 0 : [...entry.models]
|
|
4319
|
+
}))
|
|
4320
|
+
);
|
|
4321
|
+
const declaredModels = new Set(routeModels);
|
|
4322
|
+
const unmergedModels = merged.ids.filter((id) => !declaredModels.has(id));
|
|
4323
|
+
return {
|
|
4324
|
+
ok: true,
|
|
4325
|
+
route,
|
|
4326
|
+
entries,
|
|
4327
|
+
models: routeModels,
|
|
4328
|
+
...merged.ids.length === 0 ? {} : { mergedModels: merged.ids },
|
|
4329
|
+
...unmergedModels.length === 0 ? {} : { unmergedModels },
|
|
4330
|
+
endpoints: {
|
|
4331
|
+
openai: endpoints?.openai ?? null,
|
|
4332
|
+
anthropic: endpoints?.anthropic ?? null,
|
|
4333
|
+
gemini: endpoints?.gemini ?? null
|
|
4334
|
+
}
|
|
4335
|
+
};
|
|
3193
4336
|
};
|
|
3194
4337
|
if (req.method === "GET") {
|
|
3195
4338
|
sendJson(res, 200, await payload());
|
|
@@ -3201,6 +4344,7 @@ function apply(ctx, config = {}) {
|
|
|
3201
4344
|
const alias = isRecord(body) ? normalizeKeyAlias(body["alias"]) : void 0;
|
|
3202
4345
|
const value = isRecord(body) && typeof body["value"] === "string" ? body["value"] : void 0;
|
|
3203
4346
|
const refuse = (error) => sendJson(res, 400, { ok: false, error });
|
|
4347
|
+
let probeReport;
|
|
3204
4348
|
if (op === "add") {
|
|
3205
4349
|
if (!isLegalKeyValue(value)) {
|
|
3206
4350
|
refuse("a key must be printable ASCII without spaces");
|
|
@@ -3241,25 +4385,149 @@ function apply(ctx, config = {}) {
|
|
|
3241
4385
|
}
|
|
3242
4386
|
throw error;
|
|
3243
4387
|
}
|
|
4388
|
+
} else if (op === "scope") {
|
|
4389
|
+
const document = readKeyIndex(indexFile);
|
|
4390
|
+
const known = ref !== void 0 && (hasKeyEntry(document, route, ref) || ref === enabledNow());
|
|
4391
|
+
if (ref === void 0 || !known) {
|
|
4392
|
+
refuse("that key is not listed for this provider");
|
|
4393
|
+
return;
|
|
4394
|
+
}
|
|
4395
|
+
const scope = isRecord(body) ? normalizeKeyModels(body["models"]) : void 0;
|
|
4396
|
+
const target = ref;
|
|
4397
|
+
updateKeyIndex(
|
|
4398
|
+
(document2) => hasKeyEntry(document2, route, target) ? withKeyModels(document2, route, target, scope) : withKeyEntry(document2, route, target, void 0),
|
|
4399
|
+
indexFile
|
|
4400
|
+
);
|
|
4401
|
+
} else if (op === "protocol") {
|
|
4402
|
+
const document = readKeyIndex(indexFile);
|
|
4403
|
+
const known = ref !== void 0 && (hasKeyEntry(document, route, ref) || ref === enabledNow());
|
|
4404
|
+
if (ref === void 0 || !known) {
|
|
4405
|
+
refuse("that key is not listed for this provider");
|
|
4406
|
+
return;
|
|
4407
|
+
}
|
|
4408
|
+
const protocol = isRecord(body) ? normalizeKeyProtocol(body["protocol"]) : void 0;
|
|
4409
|
+
if (isRecord(body) && body["protocol"] !== void 0 && body["protocol"] !== null && protocol === void 0) {
|
|
4410
|
+
refuse("that protocol is not supported");
|
|
4411
|
+
return;
|
|
4412
|
+
}
|
|
4413
|
+
const target = ref;
|
|
4414
|
+
updateKeyIndex(
|
|
4415
|
+
(document2) => hasKeyEntry(document2, route, target) ? withKeyProtocol(document2, route, target, protocol) : withKeyEntry(document2, route, target, void 0),
|
|
4416
|
+
indexFile
|
|
4417
|
+
);
|
|
3244
4418
|
} else if (op === "enable") {
|
|
3245
|
-
const
|
|
3246
|
-
|
|
4419
|
+
const document = readKeyIndex(indexFile);
|
|
4420
|
+
const known = ref !== void 0 && (hasKeyEntry(document, route, ref) || ref === enabledNow());
|
|
4421
|
+
if (ref === void 0 || !known) {
|
|
3247
4422
|
refuse("that key is not listed for this provider");
|
|
3248
4423
|
return;
|
|
3249
4424
|
}
|
|
3250
|
-
const
|
|
3251
|
-
if (
|
|
3252
|
-
const
|
|
4425
|
+
const enabling = !(isRecord(body) && body["enabled"] === false);
|
|
4426
|
+
if (!enabling) {
|
|
4427
|
+
const listed = keyEntriesOf(document, route);
|
|
4428
|
+
const seat = enabledNow();
|
|
4429
|
+
const others = listed.filter((entry) => entry.ref !== ref && entry.disabled !== true);
|
|
4430
|
+
const seatRemains = seat !== void 0 && seat !== ref && !listed.some((entry) => entry.ref === seat);
|
|
4431
|
+
if (others.length === 0 && !seatRemains) {
|
|
4432
|
+
refuse("the last enabled key cannot be switched off");
|
|
4433
|
+
return;
|
|
4434
|
+
}
|
|
4435
|
+
}
|
|
4436
|
+
const target = ref;
|
|
4437
|
+
updateKeyIndex(
|
|
4438
|
+
(document2) => {
|
|
4439
|
+
const indexed = hasKeyEntry(document2, route, target);
|
|
4440
|
+
if (!indexed && enabling) return document2;
|
|
4441
|
+
return withKeyDisabled(
|
|
4442
|
+
indexed ? document2 : withKeyEntry(document2, route, target, void 0),
|
|
4443
|
+
route,
|
|
4444
|
+
target,
|
|
4445
|
+
!enabling
|
|
4446
|
+
);
|
|
4447
|
+
},
|
|
4448
|
+
indexFile
|
|
4449
|
+
);
|
|
4450
|
+
} else if (op === "endpoints") {
|
|
4451
|
+
const endpoints = isRecord(body) ? normalizeRouteEndpoints({
|
|
4452
|
+
openai: body["openai"],
|
|
4453
|
+
anthropic: body["anthropic"],
|
|
4454
|
+
gemini: body["gemini"]
|
|
4455
|
+
}) : void 0;
|
|
4456
|
+
updateKeyIndex((document) => withRouteEndpoints(document, route, endpoints), indexFile);
|
|
4457
|
+
} else if (op === "discover") {
|
|
4458
|
+
const document = readKeyIndex(indexFile);
|
|
4459
|
+
const profile = providerProfileOf(entryOf()?.value, route);
|
|
4460
|
+
const baseURL = typeof profile?.["baseURL"] === "string" ? profile["baseURL"] : "";
|
|
4461
|
+
const api = typeof profile?.["api"] === "string" ? profile["api"] : "";
|
|
4462
|
+
if (baseURL.length === 0) {
|
|
4463
|
+
refuse("this provider has no address to interrogate; set each key's models by hand");
|
|
4464
|
+
return;
|
|
4465
|
+
}
|
|
4466
|
+
const seat = enabledNow();
|
|
4467
|
+
const listedEntries = keyEntriesOf(document, route);
|
|
4468
|
+
const listedRefs = listedEntries.map((candidate) => candidate.ref);
|
|
4469
|
+
if (seat !== void 0 && !listedRefs.includes(seat)) listedRefs.push(seat);
|
|
4470
|
+
if (listedRefs.length === 0) {
|
|
4471
|
+
refuse("add a key before detecting its models");
|
|
4472
|
+
return;
|
|
4473
|
+
}
|
|
4474
|
+
const endpoints = document.endpoints?.[route];
|
|
4475
|
+
const asks = [];
|
|
4476
|
+
for (const listed of listedRefs) {
|
|
4477
|
+
let apiKey;
|
|
4478
|
+
try {
|
|
4479
|
+
apiKey = await resolveCredential(credentials, listed);
|
|
4480
|
+
} catch {
|
|
4481
|
+
continue;
|
|
4482
|
+
}
|
|
4483
|
+
if (apiKey === void 0) continue;
|
|
4484
|
+
const pinned = listedEntries.find((candidate) => candidate.ref === listed)?.protocol;
|
|
4485
|
+
asks.push({
|
|
4486
|
+
ref: listed,
|
|
4487
|
+
apiKey,
|
|
4488
|
+
...pinned === void 0 ? {} : { pinned }
|
|
4489
|
+
});
|
|
4490
|
+
}
|
|
4491
|
+
const lastKnown = new Map(
|
|
4492
|
+
listedEntries.filter((candidate) => candidate.models !== void 0).map((candidate) => [candidate.ref, candidate.models])
|
|
4493
|
+
);
|
|
4494
|
+
const { results } = await collectKeyListings({
|
|
4495
|
+
keys: asks,
|
|
4496
|
+
lastKnown,
|
|
4497
|
+
refOf: (ask) => ask.ref,
|
|
4498
|
+
probe: async (ask) => probeKeyModels({
|
|
4499
|
+
ref: ask.ref,
|
|
4500
|
+
intent: {
|
|
4501
|
+
api,
|
|
4502
|
+
baseURL,
|
|
4503
|
+
...ask.pinned === void 0 ? {} : { pinned: ask.pinned },
|
|
4504
|
+
...endpoints === void 0 ? {} : { endpoints }
|
|
4505
|
+
},
|
|
4506
|
+
apiKey: ask.apiKey,
|
|
4507
|
+
profileHeaders: profile?.["headers"],
|
|
4508
|
+
timeoutMs: resolved.probeTimeoutMs,
|
|
4509
|
+
// Each key probes its OWN credential, so bypass the key-routing
|
|
4510
|
+
// wrapper (which would otherwise sign every probe with the first key).
|
|
4511
|
+
send: headerOverlayBypass(),
|
|
4512
|
+
headersFor: composeProbeHeaders,
|
|
4513
|
+
read: readBounded,
|
|
4514
|
+
entries: listingEntries
|
|
4515
|
+
})
|
|
4516
|
+
});
|
|
4517
|
+
for (const result of results) {
|
|
4518
|
+
if (result.models === void 0) continue;
|
|
3253
4519
|
updateKeyIndex(
|
|
3254
|
-
(
|
|
4520
|
+
(document2) => hasKeyEntry(document2, route, result.ref) ? withKeyModels(document2, route, result.ref, result.models) : withKeyEntry(document2, route, result.ref, void 0),
|
|
3255
4521
|
indexFile
|
|
3256
4522
|
);
|
|
3257
4523
|
}
|
|
3258
|
-
|
|
3259
|
-
|
|
3260
|
-
|
|
4524
|
+
if (results.every((result) => result.targets.length === 0)) {
|
|
4525
|
+
refuse(
|
|
4526
|
+
results.find((result) => result.skipped !== void 0)?.skipped ?? "this provider cannot be interrogated; set each key's models by hand"
|
|
4527
|
+
);
|
|
3261
4528
|
return;
|
|
3262
4529
|
}
|
|
4530
|
+
probeReport = results;
|
|
3263
4531
|
} else if (op === "rename") {
|
|
3264
4532
|
const known = ref !== void 0 && (hasKeyEntry(readKeyIndex(indexFile), route, ref) || ref === enabledNow());
|
|
3265
4533
|
if (ref === void 0 || !known) {
|
|
@@ -3318,7 +4586,7 @@ function apply(ctx, config = {}) {
|
|
|
3318
4586
|
const original = document.providers[route];
|
|
3319
4587
|
if (original === void 0) delete providers[route];
|
|
3320
4588
|
else providers[route] = original;
|
|
3321
|
-
return {
|
|
4589
|
+
return { ...current, providers };
|
|
3322
4590
|
}, indexFile);
|
|
3323
4591
|
} catch {
|
|
3324
4592
|
failures.push("key index rollback failed");
|
|
@@ -3329,11 +4597,66 @@ function apply(ctx, config = {}) {
|
|
|
3329
4597
|
}
|
|
3330
4598
|
throw new Error([error instanceof Error ? error.message : String(error), ...failures].join("; "));
|
|
3331
4599
|
}
|
|
4600
|
+
} else if (op === "applyMerged") {
|
|
4601
|
+
const merged = mergeKeyListings(
|
|
4602
|
+
keyEntriesOf(readKeyIndex(indexFile), route).map((candidate) => ({
|
|
4603
|
+
ref: candidate.ref,
|
|
4604
|
+
targets: [],
|
|
4605
|
+
models: candidate.models === void 0 ? void 0 : [...candidate.models]
|
|
4606
|
+
}))
|
|
4607
|
+
);
|
|
4608
|
+
if (merged.ids.length === 0) {
|
|
4609
|
+
refuse("no key models have been detected yet; detect them first");
|
|
4610
|
+
return;
|
|
4611
|
+
}
|
|
4612
|
+
const appendMerged = async () => {
|
|
4613
|
+
const descriptor = settings.describe().find((candidate) => candidate.ns === PI_NS);
|
|
4614
|
+
const user = isRecord(descriptor?.["user"]) ? descriptor["user"] : void 0;
|
|
4615
|
+
const userProviders = isRecord(user) && isRecord(user["providers"]) ? user["providers"] : void 0;
|
|
4616
|
+
const profile = isRecord(userProviders) ? userProviders[route] : void 0;
|
|
4617
|
+
const rawModels = isRecord(profile) && Array.isArray(profile["models"]) ? profile["models"] : [];
|
|
4618
|
+
const declared = new Set(
|
|
4619
|
+
rawModels.filter(isRecord).map((model) => typeof model["id"] === "string" ? model["id"].trim() : "").filter((id) => id.length > 0)
|
|
4620
|
+
);
|
|
4621
|
+
const additions = merged.ids.filter((id) => !declared.has(id));
|
|
4622
|
+
if (additions.length === 0) return void 0;
|
|
4623
|
+
try {
|
|
4624
|
+
await settings.mutate(
|
|
4625
|
+
PI_NS,
|
|
4626
|
+
[{
|
|
4627
|
+
op: "set",
|
|
4628
|
+
path: ["providers", route, "models"],
|
|
4629
|
+
value: [...rawModels, ...additions.map((id) => ({ id }))]
|
|
4630
|
+
}],
|
|
4631
|
+
descriptor?.revision
|
|
4632
|
+
);
|
|
4633
|
+
return void 0;
|
|
4634
|
+
} catch (error) {
|
|
4635
|
+
return {
|
|
4636
|
+
conflict: isConflictError(error),
|
|
4637
|
+
error: error instanceof Error ? error.message : String(error)
|
|
4638
|
+
};
|
|
4639
|
+
}
|
|
4640
|
+
};
|
|
4641
|
+
for (let attempt = 0; attempt < 2; attempt += 1) {
|
|
4642
|
+
const failed = await appendMerged();
|
|
4643
|
+
if (failed === void 0) break;
|
|
4644
|
+
if (attempt === 0 && failed.conflict) continue;
|
|
4645
|
+
refuse(failed.error);
|
|
4646
|
+
return;
|
|
4647
|
+
}
|
|
4648
|
+
piSectionCached = false;
|
|
3332
4649
|
} else {
|
|
3333
4650
|
refuse(`unknown key operation "${op}"`);
|
|
3334
4651
|
return;
|
|
3335
4652
|
}
|
|
3336
|
-
|
|
4653
|
+
refreshKeyScopes();
|
|
4654
|
+
const answer = await payload();
|
|
4655
|
+
sendJson(
|
|
4656
|
+
res,
|
|
4657
|
+
200,
|
|
4658
|
+
probeReport === void 0 || !isRecord(answer) ? answer : { ...answer, probe: probeReport }
|
|
4659
|
+
);
|
|
3337
4660
|
} catch (error) {
|
|
3338
4661
|
sendJson(res, error instanceof HostReadError ? 502 : 500, {
|
|
3339
4662
|
ok: false,
|