mslxdff 0.1.231 → 0.1.232
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/bin/mslxdff.js +3 -3
- package/package.json +1 -1
- package/src/auto.js +254 -254
- package/src/bench/cline-bench.js +42 -42
- package/src/bench/probe.js +70 -70
- package/src/bench/report.js +162 -162
- package/src/bench/runner.js +77 -77
- package/src/bench/via-probe.js +124 -124
- package/src/bench/via-routes.js +87 -87
- package/src/bench/workbuddy-bench.js +54 -54
- package/src/chat/engine.js +160 -160
- package/src/chat/gateway.js +163 -163
- package/src/chat/orchestrator.js +234 -234
- package/src/chat/prompt.js +70 -70
- package/src/chat/repl.js +88 -88
- package/src/chat/terminal.js +135 -135
- package/src/chat/tools.js +306 -306
- package/src/chat-pipeline/index.js +145 -145
- package/src/chat-pipeline/policy.js +84 -84
- package/src/cli/commands/group.js +266 -266
- package/src/cli/commands/model/picks.js +50 -50
- package/src/cli/commands/provider/bench-via.js +247 -247
- package/src/cli/commands/provider/bench.js +141 -141
- package/src/cli/commands/provider/index.js +139 -139
- package/src/cli/commands/provider/models.js +149 -149
- package/src/cli/commands/provider/qwenwork-login.js +119 -119
- package/src/cli/commands/provider/raccoon-quota.js +156 -156
- package/src/cli/commands/sync.js +310 -310
- package/src/cli/status.js +279 -279
- package/src/daemon.js +246 -246
- package/src/model-capabilities/enrich.js +86 -86
- package/src/model-capabilities/index.js +183 -183
- package/src/model-capabilities/parse.js +70 -70
- package/src/models.js +225 -225
- package/src/peers.js +345 -345
- package/src/providers/agentrouter.js +238 -238
- package/src/providers/base.js +164 -164
- package/src/providers/cline/auth.js +228 -228
- package/src/providers/cline/chat.js +307 -307
- package/src/providers/cline.js +2 -2
- package/src/providers/codearts/chat.js +207 -207
- package/src/providers/dispatcher.js +147 -147
- package/src/providers/globalqwenwork/cosy.js +123 -123
- package/src/providers/globalqwenwork/crypto.js +218 -218
- package/src/providers/globalqwenwork/http.js +20 -20
- package/src/providers/globalqwenwork/rsa.js +54 -54
- package/src/providers/globalqwenwork/sse.js +268 -268
- package/src/providers/globalqwenwork/stream.js +130 -130
- package/src/providers/globalqwenwork/upstream.js +122 -122
- package/src/providers/keyring.js +60 -60
- package/src/providers/qoder/chat.js +203 -203
- package/src/providers/qoder/index.js +249 -249
- package/src/providers/qoder/sse.js +145 -145
- package/src/providers/qwenwork/account-store.js +133 -133
- package/src/providers/qwenwork/constants.js +67 -67
- package/src/providers/qwenwork/cosy.js +120 -120
- package/src/providers/qwenwork/crypto.js +218 -218
- package/src/providers/qwenwork/http.js +20 -20
- package/src/providers/qwenwork/index.js +337 -337
- package/src/providers/qwenwork/payload.js +144 -144
- package/src/providers/qwenwork/rsa.js +54 -54
- package/src/providers/qwenwork/sse.js +268 -268
- package/src/providers/qwenwork/stream.js +130 -130
- package/src/providers/qwenwork/upstream.js +120 -120
- package/src/providers/qwenwork.js +1 -1
- package/src/providers/raccoon/const.js +202 -202
- package/src/providers/raccoon/credits.js +285 -285
- package/src/providers/registry.js +89 -89
- package/src/providers/workbuddy/auth.js +16 -0
- package/src/providers/workbuddy/chat.js +298 -264
- package/src/providers/workbuddy/client-headers.js +148 -0
- package/src/providers/workbuddy/desktop-info.js +17 -1
- package/src/providers/workbuddy/models.js +8 -20
- package/src/providers/workbuddy/reshape.js +152 -152
- package/src/providers/workbuddy.js +2 -2
- package/src/reasoning.js +32 -32
- package/src/routes/chat/broadband-handler.js +80 -80
- package/src/routes/chat/gateway.js +46 -46
- package/src/routes/chat/relay-pipeline.js +282 -282
- package/src/routes/chat/via-route-handler.js +156 -156
- package/src/routes/hedge.js +260 -260
- package/src/routes/models-route.js +167 -167
- package/src/routes/peers.js +373 -373
- package/src/routes/stream.js +347 -347
- package/src/runtime/bootstrap.js +75 -75
- package/src/runtime/keep-warm.js +135 -135
- package/src/runtime/provider-gate.js +75 -75
- package/src/runtime/providers-setup.js +187 -187
- package/src/runtime/raccoon-checkin.js +175 -175
- package/src/server.js +85 -85
- package/src/state/schemas/allowlist.js +92 -92
- package/src/sync-opencode.js +280 -280
- package/src/transport/index.js +244 -244
- package/src/transport/pool.js +56 -56
- package/src/transport/retry.js +24 -24
- package/src/transport/sse.js +93 -93
- package/src/upstream-probe/display.js +52 -52
- package/src/upstream-probe/probe.js +49 -49
- package/src/upstream-probe/rotate.js +110 -110
- package/src/upstream-probe/start.js +45 -45
- package/src/upstream.js +355 -355
- package/docs/cli_help_mini.md +0 -148
- package/src/providers/AGENTS.md +0 -49
- package/src/routes/AGENTS.md +0 -37
|
@@ -1,70 +1,70 @@
|
|
|
1
|
-
// models.dev 能力目录 → mslxdff 能力形状的纯函数解析层(无 IO,测试接缝 S1)
|
|
2
|
-
// 源数据形态(实测 models.opencode.ai/api.json 2026-09-11):
|
|
3
|
-
// reasoning_options: [{type:"effort",values:["low","medium","high","max"]} | {type:"toggle"} | {type:"budget_tokens",min}]
|
|
4
|
-
// modalities.input 含 "image" 即可读图;limit.context/output;cost.input/output 为 $/M tokens
|
|
5
|
-
export function normalizeModelCaps(_id, m) {
|
|
6
|
-
const opts = Array.isArray(m?.reasoning_options) ? m.reasoning_options : [];
|
|
7
|
-
const effort = opts.find((o) => o?.type === "effort");
|
|
8
|
-
const toggle = opts.some((o) => o?.type === "toggle");
|
|
9
|
-
const budget = opts.find((o) => o?.type === "budget_tokens");
|
|
10
|
-
const input = Array.isArray(m?.modalities?.input) ? m.modalities.input : [];
|
|
11
|
-
return {
|
|
12
|
-
reasoning: Boolean(m?.reasoning),
|
|
13
|
-
effortType: effort ? "effort" : toggle ? "toggle" : budget ? "budget_tokens" : null,
|
|
14
|
-
effortValues: effort && Array.isArray(effort.values) ? effort.values.map(String) : null,
|
|
15
|
-
imageInput: input.includes("image"),
|
|
16
|
-
toolCall: Boolean(m?.tool_call),
|
|
17
|
-
context: Number(m?.limit?.context) || null,
|
|
18
|
-
maxOutput: Number(m?.limit?.output) || null,
|
|
19
|
-
costIn: Number(m?.cost?.input) || null,
|
|
20
|
-
costOut: Number(m?.cost?.output) || null,
|
|
21
|
-
// opencode Model 形状补充字段(-setto opencode 条目注入用)
|
|
22
|
-
attachment: Boolean(m?.attachment),
|
|
23
|
-
temperature: Boolean(m?.temperature),
|
|
24
|
-
releaseDate: typeof m?.release_date === "string" && m.release_date ? m.release_date : null,
|
|
25
|
-
inputModalities: input.length ? input : ["text"],
|
|
26
|
-
outputModalities: Array.isArray(m?.modalities?.output) && m.modalities.output.length ? m.modalities.output : ["text"],
|
|
27
|
-
// 模型级 SDK 覆盖(models.dev provider.npm):@ai-sdk/openai → responses 端点;null = 继承 provider 默认
|
|
28
|
-
npm: typeof m?.provider?.npm === "string" && m.provider.npm ? m.provider.npm : null,
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// provider.models 对象({ [modelId]: rawModel })→ { [modelId]: caps }
|
|
33
|
-
export function normalizeProviderModels(modelsObj) {
|
|
34
|
-
const out = {};
|
|
35
|
-
if (!modelsObj || typeof modelsObj !== "object") return out;
|
|
36
|
-
for (const [id, m] of Object.entries(modelsObj)) {
|
|
37
|
-
if (!m || typeof m !== "object") continue;
|
|
38
|
-
out[id] = normalizeModelCaps(id, m);
|
|
39
|
-
}
|
|
40
|
-
return out;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// workbuddy 上游原生字段(/v3/config 产品文档,first-party 最准)→ 统一 caps 形状
|
|
44
|
-
// 实测字段(2026-09-11,29 模型全覆盖):maxInputTokens/maxOutputTokens/supportsImages/
|
|
45
|
-
// supportsReasoning/reasoning{effort,summary}/supportsToolCall/disabledMultimodal/credits/tags/name/vendor
|
|
46
|
-
// reasoning 是"当前档位"非档位列表 → effortType=effort 但 effortValues=null、defaultEffort 记默认档
|
|
47
|
-
export function normalizeWorkbuddyCaps(_id, m) {
|
|
48
|
-
const imageOk = Boolean(m?.supportsImages) && !m?.disabledMultimodal;
|
|
49
|
-
const reasoning = Boolean(m?.supportsReasoning);
|
|
50
|
-
const effort = reasoning && m?.reasoning && typeof m.reasoning === "object" ? String(m.reasoning.effort || "").trim() : "";
|
|
51
|
-
return {
|
|
52
|
-
reasoning,
|
|
53
|
-
effortType: reasoning ? "effort" : null,
|
|
54
|
-
// 上游只给当前档位不给档位列表;实测 hy3 reasoning_effort low/high 均生效(思考块 99 vs 210),
|
|
55
|
-
// 网关层按 OpenAI 兼容标准给通用三档(上游不识别时忽略,无害)
|
|
56
|
-
effortValues: reasoning ? ["low", "medium", "high"] : null,
|
|
57
|
-
defaultEffort: effort || null,
|
|
58
|
-
imageInput: imageOk,
|
|
59
|
-
toolCall: Boolean(m?.supportsToolCall),
|
|
60
|
-
context: Number(m?.maxInputTokens) || null,
|
|
61
|
-
maxOutput: Number(m?.maxOutputTokens) || null,
|
|
62
|
-
costIn: null,
|
|
63
|
-
costOut: null,
|
|
64
|
-
attachment: false,
|
|
65
|
-
temperature: m?.temperature != null,
|
|
66
|
-
releaseDate: null,
|
|
67
|
-
inputModalities: imageOk ? ["text", "image"] : ["text"],
|
|
68
|
-
outputModalities: ["text"],
|
|
69
|
-
};
|
|
70
|
-
}
|
|
1
|
+
// models.dev 能力目录 → mslxdff 能力形状的纯函数解析层(无 IO,测试接缝 S1)
|
|
2
|
+
// 源数据形态(实测 models.opencode.ai/api.json 2026-09-11):
|
|
3
|
+
// reasoning_options: [{type:"effort",values:["low","medium","high","max"]} | {type:"toggle"} | {type:"budget_tokens",min}]
|
|
4
|
+
// modalities.input 含 "image" 即可读图;limit.context/output;cost.input/output 为 $/M tokens
|
|
5
|
+
export function normalizeModelCaps(_id, m) {
|
|
6
|
+
const opts = Array.isArray(m?.reasoning_options) ? m.reasoning_options : [];
|
|
7
|
+
const effort = opts.find((o) => o?.type === "effort");
|
|
8
|
+
const toggle = opts.some((o) => o?.type === "toggle");
|
|
9
|
+
const budget = opts.find((o) => o?.type === "budget_tokens");
|
|
10
|
+
const input = Array.isArray(m?.modalities?.input) ? m.modalities.input : [];
|
|
11
|
+
return {
|
|
12
|
+
reasoning: Boolean(m?.reasoning),
|
|
13
|
+
effortType: effort ? "effort" : toggle ? "toggle" : budget ? "budget_tokens" : null,
|
|
14
|
+
effortValues: effort && Array.isArray(effort.values) ? effort.values.map(String) : null,
|
|
15
|
+
imageInput: input.includes("image"),
|
|
16
|
+
toolCall: Boolean(m?.tool_call),
|
|
17
|
+
context: Number(m?.limit?.context) || null,
|
|
18
|
+
maxOutput: Number(m?.limit?.output) || null,
|
|
19
|
+
costIn: Number(m?.cost?.input) || null,
|
|
20
|
+
costOut: Number(m?.cost?.output) || null,
|
|
21
|
+
// opencode Model 形状补充字段(-setto opencode 条目注入用)
|
|
22
|
+
attachment: Boolean(m?.attachment),
|
|
23
|
+
temperature: Boolean(m?.temperature),
|
|
24
|
+
releaseDate: typeof m?.release_date === "string" && m.release_date ? m.release_date : null,
|
|
25
|
+
inputModalities: input.length ? input : ["text"],
|
|
26
|
+
outputModalities: Array.isArray(m?.modalities?.output) && m.modalities.output.length ? m.modalities.output : ["text"],
|
|
27
|
+
// 模型级 SDK 覆盖(models.dev provider.npm):@ai-sdk/openai → responses 端点;null = 继承 provider 默认
|
|
28
|
+
npm: typeof m?.provider?.npm === "string" && m.provider.npm ? m.provider.npm : null,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// provider.models 对象({ [modelId]: rawModel })→ { [modelId]: caps }
|
|
33
|
+
export function normalizeProviderModels(modelsObj) {
|
|
34
|
+
const out = {};
|
|
35
|
+
if (!modelsObj || typeof modelsObj !== "object") return out;
|
|
36
|
+
for (const [id, m] of Object.entries(modelsObj)) {
|
|
37
|
+
if (!m || typeof m !== "object") continue;
|
|
38
|
+
out[id] = normalizeModelCaps(id, m);
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// workbuddy 上游原生字段(/v3/config 产品文档,first-party 最准)→ 统一 caps 形状
|
|
44
|
+
// 实测字段(2026-09-11,29 模型全覆盖):maxInputTokens/maxOutputTokens/supportsImages/
|
|
45
|
+
// supportsReasoning/reasoning{effort,summary}/supportsToolCall/disabledMultimodal/credits/tags/name/vendor
|
|
46
|
+
// reasoning 是"当前档位"非档位列表 → effortType=effort 但 effortValues=null、defaultEffort 记默认档
|
|
47
|
+
export function normalizeWorkbuddyCaps(_id, m) {
|
|
48
|
+
const imageOk = Boolean(m?.supportsImages) && !m?.disabledMultimodal;
|
|
49
|
+
const reasoning = Boolean(m?.supportsReasoning);
|
|
50
|
+
const effort = reasoning && m?.reasoning && typeof m.reasoning === "object" ? String(m.reasoning.effort || "").trim() : "";
|
|
51
|
+
return {
|
|
52
|
+
reasoning,
|
|
53
|
+
effortType: reasoning ? "effort" : null,
|
|
54
|
+
// 上游只给当前档位不给档位列表;实测 hy3 reasoning_effort low/high 均生效(思考块 99 vs 210),
|
|
55
|
+
// 网关层按 OpenAI 兼容标准给通用三档(上游不识别时忽略,无害)
|
|
56
|
+
effortValues: reasoning ? ["low", "medium", "high"] : null,
|
|
57
|
+
defaultEffort: effort || null,
|
|
58
|
+
imageInput: imageOk,
|
|
59
|
+
toolCall: Boolean(m?.supportsToolCall),
|
|
60
|
+
context: Number(m?.maxInputTokens) || null,
|
|
61
|
+
maxOutput: Number(m?.maxOutputTokens) || null,
|
|
62
|
+
costIn: null,
|
|
63
|
+
costOut: null,
|
|
64
|
+
attachment: false,
|
|
65
|
+
temperature: m?.temperature != null,
|
|
66
|
+
releaseDate: null,
|
|
67
|
+
inputModalities: imageOk ? ["text", "image"] : ["text"],
|
|
68
|
+
outputModalities: ["text"],
|
|
69
|
+
};
|
|
70
|
+
}
|
package/src/models.js
CHANGED
|
@@ -1,227 +1,227 @@
|
|
|
1
|
-
const KNOWN_FREE_OPENCODE_MODELS = ["big-pickle"];
|
|
2
|
-
const CACHE_TTL_MS = 2 * 60 * 60 * 1000;
|
|
3
|
-
const DEFAULT_REFRESH_MS = 2 * 60 * 60 * 1000;
|
|
4
|
-
import { mkdirSync, writeFileSync } from "node:fs";
|
|
5
|
-
import { dirname } from "node:path";
|
|
1
|
+
const KNOWN_FREE_OPENCODE_MODELS = ["big-pickle"];
|
|
2
|
+
const CACHE_TTL_MS = 2 * 60 * 60 * 1000;
|
|
3
|
+
const DEFAULT_REFRESH_MS = 2 * 60 * 60 * 1000;
|
|
4
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { dirname } from "node:path";
|
|
6
6
|
import { compatFetch } from "./compat.js";
|
|
7
|
-
|
|
8
|
-
export function isFreeModel(id) {
|
|
9
|
-
return (typeof id === "string" && id.endsWith("-free")) ||
|
|
10
|
-
KNOWN_FREE_OPENCODE_MODELS.includes(id);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function filterFreeModels(list) {
|
|
14
|
-
const seen = new Set();
|
|
15
|
-
const out = [];
|
|
16
|
-
for (const m of list || []) {
|
|
17
|
-
if (!(m && m.id)) continue;
|
|
18
|
-
if (!isFreeModel(m.id)) continue;
|
|
19
|
-
if (seen.has(m.id)) continue;
|
|
20
|
-
seen.add(m.id);
|
|
21
|
-
out.push(m);
|
|
22
|
-
}
|
|
23
|
-
return out;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function createModelsService({ baseUrl, headers, ttlMs = CACHE_TTL_MS, refreshMs = DEFAULT_REFRESH_MS, cacheFile, providers } = {}) {
|
|
27
|
-
// 多供应商聚合模式:给 `providers` 数组,则缓存并返回所有供应商的模型列表
|
|
28
|
-
if (providers?.length) {
|
|
29
|
-
let aggregate = null;
|
|
30
|
-
let aggFetchedAt = 0;
|
|
31
|
-
let lastAllowlistKey = "";
|
|
32
|
-
async function currentAllowlistKey() {
|
|
33
|
-
try {
|
|
34
|
-
const { loadProviderAllowedModels, loadProviderAllowAnyModels } = await import("./state.js");
|
|
35
|
-
return providers.map((p) => {
|
|
36
|
-
const allowAny = loadProviderAllowAnyModels(p.id) ? "any" : "block";
|
|
37
|
-
return `${p.id}:${allowAny}:${loadProviderAllowedModels(p.id).join(",")}`;
|
|
38
|
-
}).join("|");
|
|
39
|
-
} catch {
|
|
40
|
-
return "";
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
async function aggLoad() {
|
|
44
|
-
const all = [];
|
|
45
|
-
for (const p of providers) {
|
|
46
|
-
try {
|
|
47
|
-
let list = (await p.listModels?.()) ?? [];
|
|
48
|
-
// 白名单过滤:空名单且 allowAny=false → 该供应商不暴露任何模型(安全默认全拦,auto 直接跳过该供应商)
|
|
49
|
-
try {
|
|
50
|
-
const { loadProviderAllowedModels, loadProviderAllowAnyModels } = await import("./state.js");
|
|
51
|
-
const allowed = loadProviderAllowedModels(p.id);
|
|
52
|
-
const allowAny = loadProviderAllowAnyModels(p.id);
|
|
53
|
-
if (!allowed.length && !allowAny) {
|
|
54
|
-
// 该供应商被全拦,auto 直接跳过整个供应商
|
|
55
|
-
continue;
|
|
56
|
-
}
|
|
57
|
-
if (allowed.length) {
|
|
58
|
-
const allowedSet = new Set(allowed);
|
|
59
|
-
const { splitModelId } = await import("./providers/model-id.js");
|
|
60
|
-
list = list.filter((m) => {
|
|
61
|
-
if (!m || !m.id) return false;
|
|
62
|
-
const { raw } = splitModelId(m.id, providers.map((x) => x.id));
|
|
63
|
-
return allowedSet.has(String(raw || "").trim());
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
} catch {}
|
|
67
|
-
all.push(...list);
|
|
68
|
-
} catch {
|
|
69
|
-
// 单供应商取数失败不拖垮整体
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
aggregate = { object: "list", data: all };
|
|
73
|
-
try {
|
|
74
|
-
lastAllowlistKey = await currentAllowlistKey();
|
|
75
|
-
} catch {}
|
|
76
|
-
if (cacheFile) persistModels(aggregate, cacheFile);
|
|
77
|
-
return aggregate;
|
|
78
|
-
}
|
|
79
|
-
async function get() {
|
|
80
|
-
const now = Date.now();
|
|
81
|
-
if (aggregate && now - aggFetchedAt < ttlMs) {
|
|
82
|
-
// 热更新白名单:allowlist 变化时,即使命中 TTL 也要重载(否则 clear 后仍返回旧过滤结果)
|
|
83
|
-
try {
|
|
84
|
-
const curKey = await currentAllowlistKey();
|
|
85
|
-
if (curKey !== lastAllowlistKey) {
|
|
86
|
-
const out = await aggLoad();
|
|
87
|
-
aggFetchedAt = Date.now();
|
|
88
|
-
return out;
|
|
89
|
-
}
|
|
90
|
-
} catch {}
|
|
91
|
-
// 否则尝试在缓存上二次过滤(处理 allowlist 从空变非空等未触发重载的场景)
|
|
92
|
-
try {
|
|
93
|
-
const { loadProviderAllowedModels, loadProviderAllowAnyModels } = await import("./state.js");
|
|
94
|
-
const { splitModelId } = await import("./providers/model-id.js");
|
|
95
|
-
const filteredData = aggregate.data.filter((m) => {
|
|
96
|
-
if (!m || !m.id) return false;
|
|
97
|
-
const { provider, raw } = splitModelId(m.id, providers.map((x) => x.id));
|
|
98
|
-
const allowed = loadProviderAllowedModels(provider);
|
|
99
|
-
const allowAny = loadProviderAllowAnyModels(provider);
|
|
100
|
-
if (!allowed.length && !allowAny) return false;
|
|
101
|
-
if (!allowed.length) return true;
|
|
102
|
-
return allowed.includes(String(raw || "").trim());
|
|
103
|
-
});
|
|
104
|
-
if (filteredData.length !== aggregate.data.length) return { ...aggregate, data: filteredData };
|
|
105
|
-
return aggregate;
|
|
106
|
-
} catch {
|
|
107
|
-
return aggregate;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
const out = await aggLoad();
|
|
111
|
-
aggFetchedAt = Date.now();
|
|
112
|
-
return out;
|
|
113
|
-
}
|
|
114
|
-
return { get, startAutoRefresh: () => {}, stopAutoRefresh: () => {} };
|
|
115
|
-
}
|
|
116
|
-
let cache = null;
|
|
117
|
-
let fetchedAt = 0;
|
|
118
|
-
let inflight = null;
|
|
119
|
-
let timer = null;
|
|
120
|
-
|
|
121
|
-
async function load() {
|
|
122
|
-
let data = await fetchUpstreamModels({ baseUrl, headers });
|
|
123
|
-
// 白名单过滤:opencode 亦支持 allowlist
|
|
124
|
-
try {
|
|
125
|
-
const { loadProviderAllowedModels } = await import("./state.js");
|
|
126
|
-
const allowed = loadProviderAllowedModels("opencode");
|
|
127
|
-
if (allowed.length) {
|
|
128
|
-
const allowedSet = new Set(allowed);
|
|
129
|
-
data = { ...data, data: (data.data || []).filter((m) => m && m.id && allowedSet.has(String(m.id).trim())) };
|
|
130
|
-
}
|
|
131
|
-
} catch {}
|
|
132
|
-
cache = data;
|
|
133
|
-
fetchedAt = Date.now();
|
|
134
|
-
if (cacheFile) persistModels(data, cacheFile);
|
|
135
|
-
return data;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
async function get() {
|
|
139
|
-
const now = Date.now();
|
|
140
|
-
if (cache && now - fetchedAt < ttlMs) return cache;
|
|
141
|
-
if (inflight) return inflight;
|
|
142
|
-
|
|
143
|
-
inflight = (async () => {
|
|
144
|
-
try {
|
|
145
|
-
return await load();
|
|
146
|
-
} catch (err) {
|
|
147
|
-
// serve stale on failure if we have it, else rethrow
|
|
148
|
-
if (cache) return cache;
|
|
149
|
-
throw err;
|
|
150
|
-
} finally {
|
|
151
|
-
inflight = null;
|
|
152
|
-
}
|
|
153
|
-
})();
|
|
154
|
-
return inflight;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function startAutoRefresh(intervalMs = refreshMs) {
|
|
158
|
-
if (timer) return stopAutoRefresh;
|
|
159
|
-
timer = setInterval(() => {
|
|
160
|
-
void load().catch(() => {
|
|
161
|
-
// keep serving stale cache on background refresh failure
|
|
162
|
-
});
|
|
163
|
-
}, intervalMs);
|
|
164
|
-
timer.unref?.();
|
|
165
|
-
return stopAutoRefresh;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function stopAutoRefresh() {
|
|
169
|
-
if (timer) {
|
|
170
|
-
clearInterval(timer);
|
|
171
|
-
timer = null;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
return { get, startAutoRefresh, stopAutoRefresh };
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async function fetchUpstreamModels({ baseUrl, headers, connectTimeoutMs = 30_000 }) {
|
|
179
|
-
const url = `${baseUrl}/zen/v1/models`;
|
|
180
|
-
for (let attempt = 0; ; attempt++) {
|
|
181
|
-
const res = await attemptFetch(url, headers, connectTimeoutMs);
|
|
182
|
-
if (res instanceof Error) {
|
|
183
|
-
if (attempt < NETWORK_RETRIES) continue;
|
|
184
|
-
throw res;
|
|
185
|
-
}
|
|
186
|
-
if (isRetryable(res.status) && attempt < STATUS_RETRIES) {
|
|
187
|
-
await sleep(2000);
|
|
188
|
-
continue;
|
|
189
|
-
}
|
|
190
|
-
if (!res.ok) throw new Error(`models fetch failed: HTTP ${res.status}`);
|
|
191
|
-
const json = await res.json().catch(() => ({}));
|
|
192
|
-
const raw = Array.isArray(json) ? json : json.data ?? json.models ?? [];
|
|
193
|
-
return { object: "list", data: filterFreeModels(raw) };
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async function attemptFetch(url, headers, connectTimeoutMs) {
|
|
198
|
-
const controller = new AbortController();
|
|
199
|
-
const timer = setTimeout(() => controller.abort(), connectTimeoutMs);
|
|
200
|
-
try {
|
|
201
|
-
return await compatFetch(url, { headers, signal: controller.signal });
|
|
202
|
-
} catch (err) {
|
|
203
|
-
return err;
|
|
204
|
-
} finally {
|
|
205
|
-
clearTimeout(timer);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
function isRetryable(status) {
|
|
210
|
-
return status === 429 || status === 502 || status === 503 || status === 504;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function persistModels(data, cacheFile) {
|
|
214
|
-
try {
|
|
215
|
-
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
216
|
-
writeFileSync(cacheFile, JSON.stringify({ cachedAt: Date.now(), ...data }));
|
|
217
|
-
} catch {
|
|
218
|
-
// persistence is best-effort
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
function sleep(ms) {
|
|
223
|
-
return new Promise((r) => setTimeout(r, ms));
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const NETWORK_RETRIES = 2;
|
|
7
|
+
|
|
8
|
+
export function isFreeModel(id) {
|
|
9
|
+
return (typeof id === "string" && id.endsWith("-free")) ||
|
|
10
|
+
KNOWN_FREE_OPENCODE_MODELS.includes(id);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function filterFreeModels(list) {
|
|
14
|
+
const seen = new Set();
|
|
15
|
+
const out = [];
|
|
16
|
+
for (const m of list || []) {
|
|
17
|
+
if (!(m && m.id)) continue;
|
|
18
|
+
if (!isFreeModel(m.id)) continue;
|
|
19
|
+
if (seen.has(m.id)) continue;
|
|
20
|
+
seen.add(m.id);
|
|
21
|
+
out.push(m);
|
|
22
|
+
}
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createModelsService({ baseUrl, headers, ttlMs = CACHE_TTL_MS, refreshMs = DEFAULT_REFRESH_MS, cacheFile, providers } = {}) {
|
|
27
|
+
// 多供应商聚合模式:给 `providers` 数组,则缓存并返回所有供应商的模型列表
|
|
28
|
+
if (providers?.length) {
|
|
29
|
+
let aggregate = null;
|
|
30
|
+
let aggFetchedAt = 0;
|
|
31
|
+
let lastAllowlistKey = "";
|
|
32
|
+
async function currentAllowlistKey() {
|
|
33
|
+
try {
|
|
34
|
+
const { loadProviderAllowedModels, loadProviderAllowAnyModels } = await import("./state.js");
|
|
35
|
+
return providers.map((p) => {
|
|
36
|
+
const allowAny = loadProviderAllowAnyModels(p.id) ? "any" : "block";
|
|
37
|
+
return `${p.id}:${allowAny}:${loadProviderAllowedModels(p.id).join(",")}`;
|
|
38
|
+
}).join("|");
|
|
39
|
+
} catch {
|
|
40
|
+
return "";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async function aggLoad() {
|
|
44
|
+
const all = [];
|
|
45
|
+
for (const p of providers) {
|
|
46
|
+
try {
|
|
47
|
+
let list = (await p.listModels?.()) ?? [];
|
|
48
|
+
// 白名单过滤:空名单且 allowAny=false → 该供应商不暴露任何模型(安全默认全拦,auto 直接跳过该供应商)
|
|
49
|
+
try {
|
|
50
|
+
const { loadProviderAllowedModels, loadProviderAllowAnyModels } = await import("./state.js");
|
|
51
|
+
const allowed = loadProviderAllowedModels(p.id);
|
|
52
|
+
const allowAny = loadProviderAllowAnyModels(p.id);
|
|
53
|
+
if (!allowed.length && !allowAny) {
|
|
54
|
+
// 该供应商被全拦,auto 直接跳过整个供应商
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (allowed.length) {
|
|
58
|
+
const allowedSet = new Set(allowed);
|
|
59
|
+
const { splitModelId } = await import("./providers/model-id.js");
|
|
60
|
+
list = list.filter((m) => {
|
|
61
|
+
if (!m || !m.id) return false;
|
|
62
|
+
const { raw } = splitModelId(m.id, providers.map((x) => x.id));
|
|
63
|
+
return allowedSet.has(String(raw || "").trim());
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
} catch {}
|
|
67
|
+
all.push(...list);
|
|
68
|
+
} catch {
|
|
69
|
+
// 单供应商取数失败不拖垮整体
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
aggregate = { object: "list", data: all };
|
|
73
|
+
try {
|
|
74
|
+
lastAllowlistKey = await currentAllowlistKey();
|
|
75
|
+
} catch {}
|
|
76
|
+
if (cacheFile) persistModels(aggregate, cacheFile);
|
|
77
|
+
return aggregate;
|
|
78
|
+
}
|
|
79
|
+
async function get() {
|
|
80
|
+
const now = Date.now();
|
|
81
|
+
if (aggregate && now - aggFetchedAt < ttlMs) {
|
|
82
|
+
// 热更新白名单:allowlist 变化时,即使命中 TTL 也要重载(否则 clear 后仍返回旧过滤结果)
|
|
83
|
+
try {
|
|
84
|
+
const curKey = await currentAllowlistKey();
|
|
85
|
+
if (curKey !== lastAllowlistKey) {
|
|
86
|
+
const out = await aggLoad();
|
|
87
|
+
aggFetchedAt = Date.now();
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
} catch {}
|
|
91
|
+
// 否则尝试在缓存上二次过滤(处理 allowlist 从空变非空等未触发重载的场景)
|
|
92
|
+
try {
|
|
93
|
+
const { loadProviderAllowedModels, loadProviderAllowAnyModels } = await import("./state.js");
|
|
94
|
+
const { splitModelId } = await import("./providers/model-id.js");
|
|
95
|
+
const filteredData = aggregate.data.filter((m) => {
|
|
96
|
+
if (!m || !m.id) return false;
|
|
97
|
+
const { provider, raw } = splitModelId(m.id, providers.map((x) => x.id));
|
|
98
|
+
const allowed = loadProviderAllowedModels(provider);
|
|
99
|
+
const allowAny = loadProviderAllowAnyModels(provider);
|
|
100
|
+
if (!allowed.length && !allowAny) return false;
|
|
101
|
+
if (!allowed.length) return true;
|
|
102
|
+
return allowed.includes(String(raw || "").trim());
|
|
103
|
+
});
|
|
104
|
+
if (filteredData.length !== aggregate.data.length) return { ...aggregate, data: filteredData };
|
|
105
|
+
return aggregate;
|
|
106
|
+
} catch {
|
|
107
|
+
return aggregate;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const out = await aggLoad();
|
|
111
|
+
aggFetchedAt = Date.now();
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
return { get, startAutoRefresh: () => {}, stopAutoRefresh: () => {} };
|
|
115
|
+
}
|
|
116
|
+
let cache = null;
|
|
117
|
+
let fetchedAt = 0;
|
|
118
|
+
let inflight = null;
|
|
119
|
+
let timer = null;
|
|
120
|
+
|
|
121
|
+
async function load() {
|
|
122
|
+
let data = await fetchUpstreamModels({ baseUrl, headers });
|
|
123
|
+
// 白名单过滤:opencode 亦支持 allowlist
|
|
124
|
+
try {
|
|
125
|
+
const { loadProviderAllowedModels } = await import("./state.js");
|
|
126
|
+
const allowed = loadProviderAllowedModels("opencode");
|
|
127
|
+
if (allowed.length) {
|
|
128
|
+
const allowedSet = new Set(allowed);
|
|
129
|
+
data = { ...data, data: (data.data || []).filter((m) => m && m.id && allowedSet.has(String(m.id).trim())) };
|
|
130
|
+
}
|
|
131
|
+
} catch {}
|
|
132
|
+
cache = data;
|
|
133
|
+
fetchedAt = Date.now();
|
|
134
|
+
if (cacheFile) persistModels(data, cacheFile);
|
|
135
|
+
return data;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function get() {
|
|
139
|
+
const now = Date.now();
|
|
140
|
+
if (cache && now - fetchedAt < ttlMs) return cache;
|
|
141
|
+
if (inflight) return inflight;
|
|
142
|
+
|
|
143
|
+
inflight = (async () => {
|
|
144
|
+
try {
|
|
145
|
+
return await load();
|
|
146
|
+
} catch (err) {
|
|
147
|
+
// serve stale on failure if we have it, else rethrow
|
|
148
|
+
if (cache) return cache;
|
|
149
|
+
throw err;
|
|
150
|
+
} finally {
|
|
151
|
+
inflight = null;
|
|
152
|
+
}
|
|
153
|
+
})();
|
|
154
|
+
return inflight;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function startAutoRefresh(intervalMs = refreshMs) {
|
|
158
|
+
if (timer) return stopAutoRefresh;
|
|
159
|
+
timer = setInterval(() => {
|
|
160
|
+
void load().catch(() => {
|
|
161
|
+
// keep serving stale cache on background refresh failure
|
|
162
|
+
});
|
|
163
|
+
}, intervalMs);
|
|
164
|
+
timer.unref?.();
|
|
165
|
+
return stopAutoRefresh;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function stopAutoRefresh() {
|
|
169
|
+
if (timer) {
|
|
170
|
+
clearInterval(timer);
|
|
171
|
+
timer = null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return { get, startAutoRefresh, stopAutoRefresh };
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function fetchUpstreamModels({ baseUrl, headers, connectTimeoutMs = 30_000 }) {
|
|
179
|
+
const url = `${baseUrl}/zen/v1/models`;
|
|
180
|
+
for (let attempt = 0; ; attempt++) {
|
|
181
|
+
const res = await attemptFetch(url, headers, connectTimeoutMs);
|
|
182
|
+
if (res instanceof Error) {
|
|
183
|
+
if (attempt < NETWORK_RETRIES) continue;
|
|
184
|
+
throw res;
|
|
185
|
+
}
|
|
186
|
+
if (isRetryable(res.status) && attempt < STATUS_RETRIES) {
|
|
187
|
+
await sleep(2000);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (!res.ok) throw new Error(`models fetch failed: HTTP ${res.status}`);
|
|
191
|
+
const json = await res.json().catch(() => ({}));
|
|
192
|
+
const raw = Array.isArray(json) ? json : json.data ?? json.models ?? [];
|
|
193
|
+
return { object: "list", data: filterFreeModels(raw) };
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async function attemptFetch(url, headers, connectTimeoutMs) {
|
|
198
|
+
const controller = new AbortController();
|
|
199
|
+
const timer = setTimeout(() => controller.abort(), connectTimeoutMs);
|
|
200
|
+
try {
|
|
201
|
+
return await compatFetch(url, { headers, signal: controller.signal });
|
|
202
|
+
} catch (err) {
|
|
203
|
+
return err;
|
|
204
|
+
} finally {
|
|
205
|
+
clearTimeout(timer);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function isRetryable(status) {
|
|
210
|
+
return status === 429 || status === 502 || status === 503 || status === 504;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function persistModels(data, cacheFile) {
|
|
214
|
+
try {
|
|
215
|
+
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
216
|
+
writeFileSync(cacheFile, JSON.stringify({ cachedAt: Date.now(), ...data }));
|
|
217
|
+
} catch {
|
|
218
|
+
// persistence is best-effort
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function sleep(ms) {
|
|
223
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const NETWORK_RETRIES = 2;
|
|
227
227
|
const STATUS_RETRIES = 2;
|