dsh-opendesign-corouter 0.1.16 → 0.1.18
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/THIRD_PARTY_NOTICES.md +1 -1
- package/lib/client-config-writes.d.ts +9 -1
- package/lib/client.d.ts +6 -1
- package/lib/client.js +79 -12
- package/lib/config.d.ts +24 -3
- package/lib/device-auth.d.ts +1 -1
- package/lib/generated/typert.host.js +5 -2
- package/lib/generated/typert.remote-client.js +5 -2
- package/lib/index.d.ts +16 -0
- package/lib/index.js +170 -20
- package/lib/index.js.map +4 -4
- package/lib/model-catalog-snapshot.d.ts +22 -0
- package/lib/remote.d.ts +1 -1
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -343,6 +343,71 @@ function serializableTransform(callback) {
|
|
|
343
343
|
});
|
|
344
344
|
}
|
|
345
345
|
|
|
346
|
+
// src/model-catalog-snapshot.ts
|
|
347
|
+
function parseModelCatalogSnapshot(value) {
|
|
348
|
+
const MODEL_ID4 = /^[A-Za-z0-9._:/-]{1,128}$/u;
|
|
349
|
+
const GROUPS = ["plan", "paid"];
|
|
350
|
+
const KEYS = ["id", "name", "group", "contextWindow", "maxOutputTokens", "inputModalities", "reasoningEfforts"];
|
|
351
|
+
const RESERVED = ["__proto__", "constructor", "prototype"];
|
|
352
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
|
|
353
|
+
const snapshot = value;
|
|
354
|
+
if (typeof snapshot.baseURL !== "string" || snapshot.baseURL.length === 0 || snapshot.baseURL.length > 2048) return void 0;
|
|
355
|
+
let base;
|
|
356
|
+
try {
|
|
357
|
+
base = new URL(snapshot.baseURL);
|
|
358
|
+
} catch {
|
|
359
|
+
return void 0;
|
|
360
|
+
}
|
|
361
|
+
if (!["https:", "http:"].includes(base.protocol) || base.username || base.password || base.search || base.hash) return void 0;
|
|
362
|
+
if (base.protocol === "http:" && !["localhost", "127.0.0.1", "[::1]"].includes(base.hostname)) return void 0;
|
|
363
|
+
const baseURL = base.href.replace(/\/+$/u, "");
|
|
364
|
+
if (baseURL.length === 0 || baseURL.length > 2048) return void 0;
|
|
365
|
+
if (typeof snapshot.updatedAt !== "string" || snapshot.updatedAt.length === 0 || snapshot.updatedAt.length > 64 || !Number.isFinite(Date.parse(snapshot.updatedAt))) return void 0;
|
|
366
|
+
if (!Array.isArray(snapshot.models) || snapshot.models.length === 0 || snapshot.models.length > 512) return void 0;
|
|
367
|
+
const seen = /* @__PURE__ */ new Set();
|
|
368
|
+
const models = [];
|
|
369
|
+
for (const entry of snapshot.models) {
|
|
370
|
+
if (typeof entry !== "object" || entry === null || Array.isArray(entry)) return void 0;
|
|
371
|
+
const item = entry;
|
|
372
|
+
for (const key of Object.keys(item)) if (!KEYS.includes(key)) return void 0;
|
|
373
|
+
const id = item.id, name2 = item.name, group2 = item.group;
|
|
374
|
+
if (typeof id !== "string" || id.length === 0 || id.length > 128 || !MODEL_ID4.test(id) || seen.has(id) || RESERVED.includes(id)) return void 0;
|
|
375
|
+
if (typeof name2 !== "string" || name2.trim().length === 0 || name2.length > 128) return void 0;
|
|
376
|
+
if (typeof group2 !== "string" || !GROUPS.includes(group2)) return void 0;
|
|
377
|
+
const model = { id, name: name2, group: group2 };
|
|
378
|
+
if (item.contextWindow !== void 0) {
|
|
379
|
+
if (!Number.isSafeInteger(item.contextWindow) || item.contextWindow <= 0 || item.contextWindow > 1e8) return void 0;
|
|
380
|
+
model.contextWindow = item.contextWindow;
|
|
381
|
+
}
|
|
382
|
+
if (item.maxOutputTokens !== void 0) {
|
|
383
|
+
if (!Number.isSafeInteger(item.maxOutputTokens) || item.maxOutputTokens <= 0 || item.maxOutputTokens > 1e7) return void 0;
|
|
384
|
+
model.maxOutputTokens = item.maxOutputTokens;
|
|
385
|
+
}
|
|
386
|
+
if (item.inputModalities !== void 0) {
|
|
387
|
+
if (!Array.isArray(item.inputModalities) || item.inputModalities.length < 1 || item.inputModalities.length > 2) return void 0;
|
|
388
|
+
const modalities = [];
|
|
389
|
+
for (const modality of item.inputModalities) {
|
|
390
|
+
if (modality !== "text" && modality !== "image") return void 0;
|
|
391
|
+
modalities.push(modality);
|
|
392
|
+
}
|
|
393
|
+
if (!modalities.includes("text") || new Set(modalities).size !== modalities.length) return void 0;
|
|
394
|
+
model.inputModalities = modalities;
|
|
395
|
+
}
|
|
396
|
+
if (item.reasoningEfforts !== void 0) {
|
|
397
|
+
if (!Array.isArray(item.reasoningEfforts) || item.reasoningEfforts.length > 16) return void 0;
|
|
398
|
+
const efforts = [];
|
|
399
|
+
for (const effort of item.reasoningEfforts) {
|
|
400
|
+
if (typeof effort !== "string" || effort.length === 0 || effort.length > 32) return void 0;
|
|
401
|
+
efforts.push(effort);
|
|
402
|
+
}
|
|
403
|
+
model.reasoningEfforts = efforts;
|
|
404
|
+
}
|
|
405
|
+
seen.add(id);
|
|
406
|
+
models.push(model);
|
|
407
|
+
}
|
|
408
|
+
return { baseURL, updatedAt: snapshot.updatedAt, models };
|
|
409
|
+
}
|
|
410
|
+
|
|
346
411
|
// src/image-budget.ts
|
|
347
412
|
var MIN_REQUEST_IMAGE_BYTES = 1024 * 1024;
|
|
348
413
|
var DEFAULT_MAX_REQUEST_IMAGE_BYTES = 20 * 1024 * 1024;
|
|
@@ -353,11 +418,13 @@ var SETTINGS_NS = "llm-opendesign-corouter";
|
|
|
353
418
|
var PLAN_PROVIDER = "opendesign-corouter-plan";
|
|
354
419
|
var PAID_PROVIDER = "opendesign-corouter-paid";
|
|
355
420
|
var DEFAULT_BASE_URL = "https://amr-link.open-design.ai/v1";
|
|
421
|
+
var DEFAULT_API_KEY_REF = "COROUTER_API_KEY";
|
|
356
422
|
var Config = Schema.object({
|
|
357
|
-
apiKeyEnv: Schema.string().max(128).pattern(/^[A-Za-z_][A-Za-z0-9_]*$/u).
|
|
423
|
+
apiKeyEnv: Schema.string().max(128).pattern(/^[A-Za-z_][A-Za-z0-9_]*$/u).description(`CoRouter API Key \u7684\u73AF\u5883\u53D8\u91CF\u6216 DSH \u51ED\u636E\u5F15\u7528\u540D\uFF1B\u4E0D\u8BBE\u7F6E\u65F6\u4F7F\u7528 ${DEFAULT_API_KEY_REF}`).volatile(),
|
|
358
424
|
baseURL: Schema.transform(Schema.string(), serializableTransform(assertBaseURL)).default(DEFAULT_BASE_URL).description("CoRouter \u7F51\u5173\u5730\u5740\uFF1B\u4E0D\u7528\u4E8E\u63A7\u5236\u53F0\u767B\u5F55\u6216\u989D\u5EA6\u67E5\u8BE2").volatile(),
|
|
359
425
|
apiProtocol: Schema.union(API_PROTOCOL_IDS).default("chat-completions").description("\u7F51\u5173\u652F\u6301\u7684\u63A8\u7406\u534F\u8BAE\uFF1B\u6A21\u578B\u76EE\u5F55\u901A\u8FC7\u8BE5\u7F51\u5173\u7684\u517C\u5BB9\u63A5\u53E3\u8BFB\u53D6").volatile(),
|
|
360
426
|
modelSettings: Schema.transform(Schema.dict(Schema.any()), serializableTransform(parseModelSettingsMap)).default({}).description("\u9010\u6A21\u578B\u542F\u7528\u72B6\u6001\u3001\u56FE\u7247\u8F93\u5165\u548C\u8BF7\u6C42\u53C2\u6570").volatile(),
|
|
427
|
+
modelCatalog: Schema.transform(Schema.any(), serializableTransform(parseModelCatalogSnapshot)).description("\u4E0A\u6B21\u6210\u529F\u8BFB\u53D6\u7684\u7F51\u5173\u6A21\u578B\u76EE\u5F55\u5FEB\u7167\uFF1B\u91CD\u542F\u540E\u7EE7\u7EED\u4F7F\u7528\uFF0C\u76F4\u5230\u518D\u6B21\u5237\u65B0").volatile(),
|
|
361
428
|
useDesktopCredential: Schema.boolean().default(true).description("\u4F18\u5148\u8BFB\u53D6 OpenDesign \u684C\u9762\u7AEF\u767B\u5F55\u6001\u67E5\u8BE2\u989D\u5EA6").volatile(),
|
|
362
429
|
emptyTurnGuard: Schema.boolean().default(true).description("\u4FDD\u62A4 DeepSeek V4.1 Flash \u5DE5\u5177\u5BF9\u8BDD\uFF1A\u6062\u590D\u7A7A\u56DE\u7B54\u4E0E\u5DF2\u77E5\u534F\u8BAE\u6B8B\u7559\u7ED3\u675F").volatile(),
|
|
363
430
|
turnCompletionReview: Schema.boolean().default(false).description("\u56DE\u590D\u7ED3\u675F\u540E\u8FFD\u52A0\u6709\u754C\u7684\u7ED3\u6784\u5316\u6536\u5C3E\u6838\u9A8C\uFF0C\u8981\u6C42\u6A21\u578B\u8C03\u7528\u6536\u5C3E\u5DE5\u5177\u62A5\u544A\u72B6\u6001").volatile(),
|
|
@@ -382,12 +449,19 @@ function assertBaseURL(raw) {
|
|
|
382
449
|
if (normalized.length > 2048) throw new Error("\u63A8\u7406 Base URL \u6700\u591A\u5141\u8BB8 2048 \u5B57\u7B26");
|
|
383
450
|
return normalized;
|
|
384
451
|
}
|
|
452
|
+
function apiKeyRef(config) {
|
|
453
|
+
const named = config.apiKeyEnv;
|
|
454
|
+
return named === void 0 || named === "" ? DEFAULT_API_KEY_REF : named;
|
|
455
|
+
}
|
|
385
456
|
function readConfig(config) {
|
|
457
|
+
const modelCatalog = parseModelCatalogSnapshot(config.modelCatalog.get());
|
|
458
|
+
const apiKeyEnv = config.apiKeyEnv.get();
|
|
386
459
|
return {
|
|
387
|
-
apiKeyEnv:
|
|
460
|
+
...apiKeyEnv === void 0 ? {} : { apiKeyEnv },
|
|
388
461
|
baseURL: assertBaseURL(config.baseURL.get()),
|
|
389
462
|
apiProtocol: config.apiProtocol.get(),
|
|
390
463
|
modelSettings: parseModelSettingsMap(config.modelSettings.get()),
|
|
464
|
+
...modelCatalog === void 0 ? {} : { modelCatalog },
|
|
391
465
|
quotaRefreshSeconds: config.quotaRefreshSeconds.get(),
|
|
392
466
|
useDesktopCredential: config.useDesktopCredential.get(),
|
|
393
467
|
emptyTurnGuard: config.emptyTurnGuard.get(),
|
|
@@ -756,6 +830,14 @@ function tokenUsage(value) {
|
|
|
756
830
|
...reasoning !== void 0 && reasoning >= 0 && reasoning <= output ? { reasoningTokens: reasoning } : {}
|
|
757
831
|
};
|
|
758
832
|
}
|
|
833
|
+
function reasoningDelta(delta) {
|
|
834
|
+
if (typeof delta.reasoning_content === "string" && delta.reasoning_content !== "") return delta.reasoning_content;
|
|
835
|
+
if (typeof delta.reasoning === "string" && delta.reasoning !== "") return delta.reasoning;
|
|
836
|
+
if (!Array.isArray(delta.reasoning_details)) return "";
|
|
837
|
+
let text2 = "";
|
|
838
|
+
for (const part of delta.reasoning_details) if (record(part) && typeof part.text === "string") text2 += part.text;
|
|
839
|
+
return text2;
|
|
840
|
+
}
|
|
759
841
|
async function* consumeSse(body, signal, idleMs) {
|
|
760
842
|
let nextIndex = 0, count2 = 0, sawTool = false, done = false;
|
|
761
843
|
let text2, reasoning;
|
|
@@ -793,9 +875,9 @@ async function* consumeSse(body, signal, idleMs) {
|
|
|
793
875
|
if (!record(choice)) throw new LlmError4("OpenDesign\uFF1A\u65E0\u6548\u7684\u6D41\u5F0F\u9009\u9879", "INVALID_RESPONSE");
|
|
794
876
|
const delta = record(choice.delta) ? choice.delta : {};
|
|
795
877
|
const out = [];
|
|
796
|
-
const
|
|
878
|
+
const thinking = reasoningDelta(delta);
|
|
879
|
+
const hasDelta = typeof delta.content === "string" && delta.content !== "" || thinking !== "" || Array.isArray(delta.tool_calls) && delta.tool_calls.length > 0;
|
|
797
880
|
if (finish && hasDelta) throw new LlmError4("OpenDesign\uFF1A\u54CD\u5E94\u7ED3\u675F\u540E\u4ECD\u6536\u5230\u5185\u5BB9", "INVALID_RESPONSE");
|
|
798
|
-
const thinking = typeof delta.reasoning_content === "string" ? delta.reasoning_content : typeof delta.reasoning === "string" ? delta.reasoning : "";
|
|
799
881
|
if (thinking) {
|
|
800
882
|
if (!reasoning) {
|
|
801
883
|
reasoning = { index: nextIndex++, text: "" };
|
|
@@ -2064,9 +2146,10 @@ var CoRouterAdapter = class extends LlmAdapter {
|
|
|
2064
2146
|
validateClaudeSampling(row.id, options.temperature ?? setting.temperature, setting.topP);
|
|
2065
2147
|
validateClaudeOutput(row.id, options.maxTokens ?? setting.maxTokens);
|
|
2066
2148
|
}
|
|
2067
|
-
const
|
|
2149
|
+
const keyRef = apiKeyRef(config);
|
|
2150
|
+
const raw = await this.options.apiKey(keyRef, config.baseURL);
|
|
2068
2151
|
if (!raw) throw new LlmError11("\u672A\u914D\u7F6E\u5F53\u524D\u7F51\u5173\u7684\u63A8\u7406\u51ED\u636E\uFF1B\u8BF7\u5B8C\u6210\u8BBE\u5907\u6388\u6743\u6216\u914D\u7F6E\u5BF9\u5E94 API Key", "MISSING_CREDENTIAL");
|
|
2069
|
-
const key = assertUsableApiKey(raw, "dsh-opendesign-corouter",
|
|
2152
|
+
const key = assertUsableApiKey(raw, "dsh-opendesign-corouter", keyRef);
|
|
2070
2153
|
signal.throwIfAborted();
|
|
2071
2154
|
let credentialIdentity;
|
|
2072
2155
|
if (protocolId === "messages") {
|
|
@@ -3425,7 +3508,7 @@ import { randomUUID as randomUUID2 } from "node:crypto";
|
|
|
3425
3508
|
|
|
3426
3509
|
// src/device-auth.ts
|
|
3427
3510
|
var DEVICE_AUTH_ENDPOINT = "https://amr-api.open-design.ai/api/v1/cli/device-authorizations";
|
|
3428
|
-
var DEVICE_AUTH_PLUGIN_VERSION = "0.1.
|
|
3511
|
+
var DEVICE_AUTH_PLUGIN_VERSION = "0.1.18";
|
|
3429
3512
|
var DEVICE_REQUEST_TIMEOUT_MS = 2e4;
|
|
3430
3513
|
var DEVICE_RESPONSE_MAX_BYTES = 65536;
|
|
3431
3514
|
var DEVICE_POLL_MIN_MS = 1e3;
|
|
@@ -4537,8 +4620,11 @@ function parseStatusModel(value) {
|
|
|
4537
4620
|
}
|
|
4538
4621
|
function parseStatusConfig(value) {
|
|
4539
4622
|
if (!record(value)) throw new Error("\u914D\u7F6E\u89C6\u56FE\u5FC5\u987B\u662F\u5BF9\u8C61");
|
|
4540
|
-
|
|
4541
|
-
|
|
4623
|
+
if (typeof value.apiKeyEnv !== "string" || value.apiKeyEnv.length > MAX_ENV_REF) {
|
|
4624
|
+
throw new Error(`\u51ED\u636E\u5F15\u7528\u540D\u5FC5\u987B\u662F\u6700\u591A ${MAX_ENV_REF} \u5B57\u7B26\u7684\u5B57\u7B26\u4E32`);
|
|
4625
|
+
}
|
|
4626
|
+
const apiKeyEnv = value.apiKeyEnv;
|
|
4627
|
+
if (apiKeyEnv !== "" && !ENV_REF.test(apiKeyEnv)) throw new Error("\u51ED\u636E\u5F15\u7528\u540D\u5FC5\u987B\u662F\u5408\u6CD5\u6807\u8BC6\u7B26");
|
|
4542
4628
|
return {
|
|
4543
4629
|
emptyTurnGuard: flag(value.emptyTurnGuard, "\u7A7A\u8F6E\u4FDD\u62A4\u5F00\u5173"),
|
|
4544
4630
|
...value.turnCompletionReview === void 0 ? {} : { turnCompletionReview: flag(value.turnCompletionReview, "\u7ED3\u675F\u56DE\u5408\u6838\u9A8C\u5F00\u5173") },
|
|
@@ -4622,7 +4708,13 @@ var name = "llm-opendesign-corouter";
|
|
|
4622
4708
|
var inject = ["llm"];
|
|
4623
4709
|
var API_KEY = credentialKey2(name, "api-key");
|
|
4624
4710
|
var SESSION = credentialKey2(name, "console");
|
|
4711
|
+
var SETTINGS_CONFLICT = "SETTINGS_CONFLICT";
|
|
4712
|
+
var NATIVE_AUTH = { env: [], oauth: true };
|
|
4625
4713
|
var hash = (value) => createHash2("sha256").update(JSON.stringify(value) ?? "").digest("hex");
|
|
4714
|
+
var stableConfig = (config) => {
|
|
4715
|
+
const { modelCatalog: _snapshot, ...rest } = config;
|
|
4716
|
+
return rest;
|
|
4717
|
+
};
|
|
4626
4718
|
var needLogin = () => ({ state: "need-login", updatedAt: (/* @__PURE__ */ new Date()).toISOString(), windows: [], balances: {}, note: "\u8BF7\u5728\u8FDE\u63A5\u4E0E\u767B\u5F55\u9875\u5B8C\u6210\u8D26\u53F7\u6388\u6743\u540E\u8BFB\u53D6\u989D\u5EA6\u3002" });
|
|
4627
4719
|
var unreadQuota = () => ({ state: "partial", updatedAt: (/* @__PURE__ */ new Date()).toISOString(), windows: [], balances: {}, note: "\u989D\u5EA6\u5C1A\u672A\u8BFB\u53D6\uFF0C\u8BF7\u5237\u65B0\u989D\u5EA6\u3002" });
|
|
4628
4720
|
function grantOf(value) {
|
|
@@ -4659,15 +4751,18 @@ var OpenDesignCoRouterService = class extends (_a = TypertRemoteService, _status
|
|
|
4659
4751
|
__publicField(this, "_modelsEpoch", 0);
|
|
4660
4752
|
__publicField(this, "_modelsPending");
|
|
4661
4753
|
__publicField(this, "_modelsController");
|
|
4754
|
+
__publicField(this, "_modelsWriteSerial", 0);
|
|
4755
|
+
__publicField(this, "_modelsWrite", Promise.resolve());
|
|
4662
4756
|
__publicField(this, "_prices");
|
|
4663
4757
|
__publicField(this, "_pricesPending");
|
|
4664
4758
|
this._config = config;
|
|
4759
|
+
this._restoreModels();
|
|
4665
4760
|
this._login = new ConsoleLogin({
|
|
4666
4761
|
capture: async (expectedRevision) => {
|
|
4667
4762
|
const state = await this._state();
|
|
4668
4763
|
this._expect(expectedRevision);
|
|
4669
4764
|
this._credentials();
|
|
4670
|
-
return { revision: expectedRevision, sessionHash: hash(state.sessionRecord), configHash: hash(state.config) };
|
|
4765
|
+
return { revision: expectedRevision, sessionHash: hash(state.sessionRecord), configHash: hash(stableConfig(state.config)) };
|
|
4671
4766
|
},
|
|
4672
4767
|
commit: async (tokens, captured, signal) => {
|
|
4673
4768
|
if (!record(captured) || typeof captured.revision !== "string" || typeof captured.sessionHash !== "string" || typeof captured.configHash !== "string") throw this._conflict();
|
|
@@ -4676,7 +4771,7 @@ var OpenDesignCoRouterService = class extends (_a = TypertRemoteService, _status
|
|
|
4676
4771
|
signal.throwIfAborted();
|
|
4677
4772
|
await this._credentials().modifyRecord(SESSION, async (current) => {
|
|
4678
4773
|
signal.throwIfAborted();
|
|
4679
|
-
if (hash(current) !== captured.sessionHash || hash(this._config()) !== captured.configHash) throw this._conflict();
|
|
4774
|
+
if (hash(current) !== captured.sessionHash || hash(stableConfig(this._config())) !== captured.configHash) throw this._conflict();
|
|
4680
4775
|
const grant = deviceGrant({ kind: "device", ...tokens });
|
|
4681
4776
|
if (!grant) throw new ConsoleError("auth");
|
|
4682
4777
|
return { kind: "grant", payload: { ...grant, savedAt: (/* @__PURE__ */ new Date()).toISOString() } };
|
|
@@ -4722,6 +4817,56 @@ var OpenDesignCoRouterService = class extends (_a = TypertRemoteService, _status
|
|
|
4722
4817
|
const [apiRecord, sessionRecord] = await Promise.all([credentials?.readRecord(API_KEY), credentials?.readRecord(SESSION)]);
|
|
4723
4818
|
return (await this._key(ref, baseURL, apiRecord, sessionRecord))?.key;
|
|
4724
4819
|
}
|
|
4820
|
+
/** Restore the persisted gateway catalog when it belongs to the active gateway. */
|
|
4821
|
+
_restoreModels() {
|
|
4822
|
+
const config = this._config();
|
|
4823
|
+
const snapshot = config.modelCatalog;
|
|
4824
|
+
if (!snapshot || snapshot.baseURL !== config.baseURL) return;
|
|
4825
|
+
this._models = snapshot.models;
|
|
4826
|
+
this._modelsUpdatedAt = snapshot.updatedAt;
|
|
4827
|
+
}
|
|
4828
|
+
/**
|
|
4829
|
+
* Persist the snapshot through the Host form service.
|
|
4830
|
+
* `unsupported` means no settings service is composed (nothing to report);
|
|
4831
|
+
* `unavailable` means the service exists but exposes no writable form for this plugin.
|
|
4832
|
+
*
|
|
4833
|
+
* A settings write rewrites the whole composed profile and may rebuild the plugin tree, so
|
|
4834
|
+
* only a concurrent revision conflict (`SETTINGS_CONFLICT`) is worth re-reading the revision
|
|
4835
|
+
* and trying once more. Every other failure repeats identically and is reported immediately.
|
|
4836
|
+
*/
|
|
4837
|
+
async _persistModels(value) {
|
|
4838
|
+
const settings = this.ctx.get("settings");
|
|
4839
|
+
if (!settings) return "unsupported";
|
|
4840
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
4841
|
+
let descriptor;
|
|
4842
|
+
try {
|
|
4843
|
+
descriptor = settings.describe().find((item) => item.ns === SETTINGS_NS);
|
|
4844
|
+
} catch {
|
|
4845
|
+
return "unavailable";
|
|
4846
|
+
}
|
|
4847
|
+
if (!descriptor) return "unavailable";
|
|
4848
|
+
try {
|
|
4849
|
+
await settings.update(SETTINGS_NS, { modelCatalog: value }, descriptor.revision);
|
|
4850
|
+
return "saved";
|
|
4851
|
+
} catch (error) {
|
|
4852
|
+
if (attempt > 0 || !record(error) || error.code !== SETTINGS_CONFLICT) return "failed";
|
|
4853
|
+
}
|
|
4854
|
+
}
|
|
4855
|
+
return "failed";
|
|
4856
|
+
}
|
|
4857
|
+
/** Serialize snapshot writes so the newest successful refresh is the last one persisted. */
|
|
4858
|
+
_queueModelsWrite(snapshot, epoch) {
|
|
4859
|
+
const serial = ++this._modelsWriteSerial;
|
|
4860
|
+
this._modelsWrite = this._modelsWrite.then(async () => {
|
|
4861
|
+
const result = await this._persistModels(snapshot);
|
|
4862
|
+
if (serial !== this._modelsWriteSerial || epoch !== this._modelsEpoch) return;
|
|
4863
|
+
if (result === "unavailable") this._modelsError = "\u6A21\u578B\u76EE\u5F55\u5DF2\u66F4\u65B0\uFF0C\u4F46\u5F53\u524D\u914D\u7F6E\u670D\u52A1\u4E0D\u53EF\u5199\uFF1B\u91CD\u542F\u540E\u9700\u8981\u91CD\u65B0\u5237\u65B0\u3002";
|
|
4864
|
+
else if (result === "failed") this._modelsError = "\u6A21\u578B\u76EE\u5F55\u5DF2\u66F4\u65B0\uFF0C\u4F46\u4FDD\u5B58\u5931\u8D25\uFF1B\u91CD\u542F\u540E\u9700\u8981\u91CD\u65B0\u5237\u65B0\u3002";
|
|
4865
|
+
}).catch(() => {
|
|
4866
|
+
if (serial !== this._modelsWriteSerial || epoch !== this._modelsEpoch) return;
|
|
4867
|
+
this._modelsError = "\u6A21\u578B\u76EE\u5F55\u5DF2\u66F4\u65B0\uFF0C\u4F46\u4FDD\u5B58\u5931\u8D25\uFF1B\u91CD\u542F\u540E\u9700\u8981\u91CD\u65B0\u5237\u65B0\u3002";
|
|
4868
|
+
});
|
|
4869
|
+
}
|
|
4725
4870
|
_invalidateModels() {
|
|
4726
4871
|
const changed = this._modelsUpdatedAt !== void 0;
|
|
4727
4872
|
this._modelsEpoch++;
|
|
@@ -4730,7 +4875,8 @@ var OpenDesignCoRouterService = class extends (_a = TypertRemoteService, _status
|
|
|
4730
4875
|
this._models = MODELS;
|
|
4731
4876
|
this._modelsUpdatedAt = void 0;
|
|
4732
4877
|
this._modelsError = void 0;
|
|
4733
|
-
|
|
4878
|
+
this._restoreModels();
|
|
4879
|
+
if (changed || this._modelsUpdatedAt !== void 0) {
|
|
4734
4880
|
try {
|
|
4735
4881
|
this.dependencies.modelsChanged?.();
|
|
4736
4882
|
} catch {
|
|
@@ -4740,7 +4886,7 @@ var OpenDesignCoRouterService = class extends (_a = TypertRemoteService, _status
|
|
|
4740
4886
|
async _state() {
|
|
4741
4887
|
const credentials = this.ctx.get("credentials"), config = this._config();
|
|
4742
4888
|
const [apiRecord, sessionRecord] = await Promise.all([credentials?.readRecord(API_KEY), credentials?.readRecord(SESSION)]);
|
|
4743
|
-
const resolved = await this._key(config
|
|
4889
|
+
const resolved = await this._key(apiKeyRef(config), config.baseURL, apiRecord, sessionRecord);
|
|
4744
4890
|
const key = resolved?.key;
|
|
4745
4891
|
const state = { apiRecord, sessionRecord, key, keySource: resolved?.source, config };
|
|
4746
4892
|
const modelsFingerprint = hash({ key, baseURL: config.baseURL });
|
|
@@ -4748,7 +4894,7 @@ var OpenDesignCoRouterService = class extends (_a = TypertRemoteService, _status
|
|
|
4748
4894
|
this._modelsFingerprint = modelsFingerprint;
|
|
4749
4895
|
this._invalidateModels();
|
|
4750
4896
|
}
|
|
4751
|
-
const fingerprint = hash(state);
|
|
4897
|
+
const fingerprint = hash({ ...state, config: stableConfig(state.config) });
|
|
4752
4898
|
if (fingerprint !== this._fingerprint) {
|
|
4753
4899
|
this._fingerprint = fingerprint;
|
|
4754
4900
|
this._revision = randomUUID3();
|
|
@@ -4783,7 +4929,7 @@ var OpenDesignCoRouterService = class extends (_a = TypertRemoteService, _status
|
|
|
4783
4929
|
...this._quota.source ? { sessionSource: this._quota.source } : {},
|
|
4784
4930
|
quota: this._quota,
|
|
4785
4931
|
login: this._login.status(),
|
|
4786
|
-
config: state.config,
|
|
4932
|
+
config: { ...state.config, apiKeyEnv: state.config.apiKeyEnv ?? "" },
|
|
4787
4933
|
guardedTurns: this.guardedTurns,
|
|
4788
4934
|
...this.turnMonitorStatus ? { turnMonitor: this.turnMonitorStatus() } : {},
|
|
4789
4935
|
models: this._models.map((row) => {
|
|
@@ -4824,16 +4970,20 @@ var OpenDesignCoRouterService = class extends (_a = TypertRemoteService, _status
|
|
|
4824
4970
|
if (signal.aborted || epoch !== this._modelsEpoch) return this.status();
|
|
4825
4971
|
const previousModels = this._models;
|
|
4826
4972
|
const previousUpdatedAt = this._modelsUpdatedAt;
|
|
4973
|
+
const updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
4827
4974
|
this._models = models;
|
|
4828
|
-
this._modelsUpdatedAt =
|
|
4975
|
+
this._modelsUpdatedAt = updatedAt;
|
|
4829
4976
|
this._modelsError = void 0;
|
|
4977
|
+
let accepted = true;
|
|
4830
4978
|
try {
|
|
4831
4979
|
this.dependencies.modelsChanged?.();
|
|
4832
4980
|
} catch {
|
|
4981
|
+
accepted = false;
|
|
4833
4982
|
this._models = previousModels;
|
|
4834
4983
|
this._modelsUpdatedAt = previousUpdatedAt;
|
|
4835
4984
|
this._modelsError = "\u6A21\u578B\u76EE\u5F55\u5DF2\u8BFB\u53D6\uFF0C\u4F46\u6A21\u578B\u9009\u62E9\u5668\u66F4\u65B0\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5\u3002";
|
|
4836
4985
|
}
|
|
4986
|
+
if (accepted) this._queueModelsWrite({ baseURL: state.config.baseURL, updatedAt, models }, epoch);
|
|
4837
4987
|
} catch (error) {
|
|
4838
4988
|
if (!signal.aborted && epoch === this._modelsEpoch) this._modelsError = error instanceof ModelDiscoveryError ? error.message : "\u6A21\u578B\u5217\u8868\u66F4\u65B0\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5\u3002";
|
|
4839
4989
|
}
|
|
@@ -5001,7 +5151,7 @@ var OpenDesignCoRouterService = class extends (_a = TypertRemoteService, _status
|
|
|
5001
5151
|
this._expect(request.expectedRevision);
|
|
5002
5152
|
const expected = hash(state.apiRecord);
|
|
5003
5153
|
await this._credentials().modifyRecord(API_KEY, async (current) => {
|
|
5004
|
-
if (hash(current) !== expected || hash(this._config()) !== hash(state.config)) throw this._conflict();
|
|
5154
|
+
if (hash(current) !== expected || hash(stableConfig(this._config())) !== hash(stableConfig(state.config))) throw this._conflict();
|
|
5005
5155
|
return { kind: "api-key", key: request.value };
|
|
5006
5156
|
});
|
|
5007
5157
|
return this.status();
|
|
@@ -5057,7 +5207,7 @@ function apply(ctx, config) {
|
|
|
5057
5207
|
ctx.llm.registerModelDiscovery(SETTINGS_NS, async (request, signal) => {
|
|
5058
5208
|
if (request.provider !== void 0 && ![PLAN_PROVIDER, PAID_PROVIDER].includes(request.provider)) throw new LlmError14("\u4E0D\u662F\u672C\u63D2\u4EF6\u7684\u6A21\u578B\u901A\u9053", "INVALID_REQUEST");
|
|
5059
5209
|
const config2 = current();
|
|
5060
|
-
const key = request.apiKey ?? await service.resolveApiKey(config2
|
|
5210
|
+
const key = request.apiKey ?? await service.resolveApiKey(apiKeyRef(config2), config2.baseURL);
|
|
5061
5211
|
if (!key) throw new LlmError14("\u8BF7\u5148\u914D\u7F6E CoRouter API Key", "MISSING_CREDENTIAL");
|
|
5062
5212
|
const models = await fetchModelCatalog(config2.baseURL, key, signal);
|
|
5063
5213
|
return models.map((row) => {
|
|
@@ -5066,8 +5216,8 @@ function apply(ctx, config) {
|
|
|
5066
5216
|
});
|
|
5067
5217
|
});
|
|
5068
5218
|
ctx.llm.registerConfigurableProviders([
|
|
5069
|
-
{ provider: PLAN_PROVIDER, displayName: "OpenDesign CoRouter \xB7 \u5957\u9910\u8D60\u9001", settingsNs: SETTINGS_NS, settingsPath: [], defaultBaseURL: current().baseURL },
|
|
5070
|
-
{ provider: PAID_PROVIDER, displayName: "OpenDesign CoRouter \xB7 \u4ED8\u8D39\u6A21\u578B", settingsNs: SETTINGS_NS, settingsPath: [], defaultBaseURL: current().baseURL }
|
|
5219
|
+
{ provider: PLAN_PROVIDER, displayName: "OpenDesign CoRouter \xB7 \u5957\u9910\u8D60\u9001", settingsNs: SETTINGS_NS, settingsPath: [], defaultBaseURL: current().baseURL, nativeAuth: NATIVE_AUTH },
|
|
5220
|
+
{ provider: PAID_PROVIDER, displayName: "OpenDesign CoRouter \xB7 \u4ED8\u8D39\u6A21\u578B", settingsNs: SETTINGS_NS, settingsPath: [], defaultBaseURL: current().baseURL, nativeAuth: NATIVE_AUTH }
|
|
5071
5221
|
]);
|
|
5072
5222
|
ctx.effect(() => () => {
|
|
5073
5223
|
adapter.dispose();
|