mslxdff 0.1.109 → 0.1.111
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/package.json +6 -1
- package/src/chat-pipeline/dedup.js +83 -0
- package/src/chat-pipeline/serial-trial.js +3 -3
- package/src/cli/commands/model/list.js +15 -2
- package/src/cli/commands/provider/models.js +14 -1
- package/src/cli/commands/sync.js +5 -0
- package/src/model-capabilities/enrich.js +86 -0
- package/src/model-capabilities/index.js +163 -0
- package/src/model-capabilities/parse.js +68 -0
- package/src/providers/base.js +11 -1
- package/src/providers/classify.js +22 -0
- package/src/providers/cline/chat.js +21 -6
- package/src/providers/model-id.js +29 -0
- package/src/providers/workbuddy/chat.js +19 -0
- package/src/providers/workbuddy/reshape.js +23 -16
- package/src/providers/workbuddy/sdk-chat.js +25 -0
- package/src/routes/hedge.js +2 -2
- package/src/routes/index.js +7 -1
- package/src/routes/models-route.js +54 -0
- package/src/runtime/providers-setup.js +2 -1
- package/src/state/schemas/use-group.js +16 -0
- package/src/sync-opencode.js +39 -12
- package/src/upstream-engine/index.js +69 -0
- package/src/upstream-engine/mode.js +13 -0
- package/src/upstream-engine/sdk/attempt.js +136 -0
- package/src/upstream-engine/sdk/chat.js +28 -0
- package/src/upstream-engine/sdk/convert.js +133 -0
- package/src/upstream-engine/sdk/dispatch.js +55 -0
- package/src/upstream-engine/sdk/responses.js +85 -0
- package/src/upstream-engine/sdk/sse.js +93 -0
- package/src/upstream-probe/display.js +52 -0
- package/src/upstream-probe/probe.js +49 -0
- package/src/upstream-probe/rotate.js +110 -0
- package/src/upstream-probe/start.js +45 -0
- package/src/upstream.js +23 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mslxdff",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.111",
|
|
4
4
|
"description": "测试项目,请勿使用。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -29,5 +29,10 @@
|
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"undici": "^5.28.4"
|
|
32
|
+
},
|
|
33
|
+
"optionalDependencies": {
|
|
34
|
+
"@ai-sdk/openai-compatible": "2.0.41",
|
|
35
|
+
"@ai-sdk/openai": "3.0.84",
|
|
36
|
+
"zod": "^3.25.76"
|
|
32
37
|
}
|
|
33
38
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 请求去重(防前端双击/重试风暴)
|
|
5
|
+
* key = ip | requested | stream | bodyHash(messages+model变体)
|
|
6
|
+
* 窗口内重复到达的相同请求直接 429 返回,提示前端去重
|
|
7
|
+
* 默认窗口 1000ms,可用 MSLXDFF_DEDUP_WINDOW_MS 覆盖,0 为关闭
|
|
8
|
+
*/
|
|
9
|
+
export function dedupWindowMs() {
|
|
10
|
+
const raw = process.env.MSLXDFF_DEDUP_WINDOW_MS;
|
|
11
|
+
if (raw != null && String(raw).trim() !== "") {
|
|
12
|
+
const n = Number(raw);
|
|
13
|
+
if (Number.isFinite(n) && n >= 0) return Math.floor(n);
|
|
14
|
+
}
|
|
15
|
+
return 1000;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function hashBody(body) {
|
|
19
|
+
try {
|
|
20
|
+
const m = body?.messages;
|
|
21
|
+
const s = JSON.stringify({
|
|
22
|
+
model: body?.model || "",
|
|
23
|
+
stream: Boolean(body?.stream),
|
|
24
|
+
max_tokens: body?.max_tokens ?? body?.maxTokens ?? null,
|
|
25
|
+
messages: Array.isArray(m) ? m.map((x) => ({ role: x.role, content: typeof x.content === "string" ? x.content.slice(0, 4000) : JSON.stringify(x.content).slice(0, 4000) })) : [],
|
|
26
|
+
// 工具调用等也纳入,避免误判
|
|
27
|
+
tools: body?.tools ? JSON.stringify(body.tools).slice(0, 1000) : "",
|
|
28
|
+
});
|
|
29
|
+
return createHash("sha1").update(s).digest("hex").slice(0, 16);
|
|
30
|
+
} catch {
|
|
31
|
+
return String(body?.model || "").slice(0, 32);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let _global = null;
|
|
36
|
+
export function globalDedup() {
|
|
37
|
+
if (!_global) _global = createDedup({ windowMs: dedupWindowMs() });
|
|
38
|
+
// 若环境变量在运行时被改,同步窗口
|
|
39
|
+
const want = dedupWindowMs();
|
|
40
|
+
if (_global.windowMs !== want) {
|
|
41
|
+
_global.windowMs = want;
|
|
42
|
+
}
|
|
43
|
+
return _global;
|
|
44
|
+
}
|
|
45
|
+
export function _resetGlobalDedup() { _global = null; }
|
|
46
|
+
|
|
47
|
+
export function createDedup({ windowMs = dedupWindowMs(), now = Date.now } = {}) {
|
|
48
|
+
const map = new Map(); // key -> at
|
|
49
|
+
let sweepAt = 0;
|
|
50
|
+
|
|
51
|
+
function sweep() {
|
|
52
|
+
const t = now();
|
|
53
|
+
if (t - sweepAt < windowMs) return;
|
|
54
|
+
sweepAt = t;
|
|
55
|
+
for (const [k, at] of map) {
|
|
56
|
+
if (t - at > windowMs) map.delete(k);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function keyFor({ ip, requested, body }) {
|
|
61
|
+
const h = hashBody(body);
|
|
62
|
+
const stream = body?.stream ? "1" : "0";
|
|
63
|
+
return `${ip || "-"}|${requested || "-"}|${stream}|${h}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function check({ ip, requested, body }) {
|
|
67
|
+
if (!windowMs) return { dup: false, key: null };
|
|
68
|
+
sweep();
|
|
69
|
+
const key = keyFor({ ip, requested, body });
|
|
70
|
+
const at = map.get(key);
|
|
71
|
+
const t = now();
|
|
72
|
+
if (at != null && t - at < windowMs) {
|
|
73
|
+
return { dup: true, key, ageMs: t - at };
|
|
74
|
+
}
|
|
75
|
+
map.set(key, t);
|
|
76
|
+
return { dup: false, key };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function _size() { return map.size; }
|
|
80
|
+
function _clear() { map.clear(); }
|
|
81
|
+
|
|
82
|
+
return { check, keyFor, _size, _clear, windowMs };
|
|
83
|
+
}
|
|
@@ -9,7 +9,7 @@ import { handlePeerRelay } from "../routes/chat/peer-handler.js";
|
|
|
9
9
|
import { handleBroadbandRelay } from "../routes/chat/broadband-handler.js";
|
|
10
10
|
import { handleViaRoute } from "../routes/chat/via-route-handler.js";
|
|
11
11
|
import { handleExhaustedLocal, handleExhaustedAll } from "../routes/chat/exhausted-handler.js";
|
|
12
|
-
import { shouldUseGroupForModel } from "../state/schemas/use-group.js";
|
|
12
|
+
import { shouldUseGroupForModel, isHardLocalOnly } from "../state/schemas/use-group.js";
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* 串行 trial — 从 engine.js 抽出的第二段:via-route 单路径 → 串行 trial →
|
|
@@ -131,7 +131,7 @@ export async function runSerialTrial(ctx, deps = {}) {
|
|
|
131
131
|
}
|
|
132
132
|
if (canForwardPeers) {
|
|
133
133
|
if (!shouldUseGroupForModel(model)) {
|
|
134
|
-
evt("group-skip", { reqId, model, reason: "useGroup=off (peer)" });
|
|
134
|
+
evt("group-skip", { reqId, model, reason: isHardLocalOnly(model) ? "provider local-only(禁组员,仅本机直连)" : "useGroup=off (peer)" });
|
|
135
135
|
} else {
|
|
136
136
|
const pr = await peerRelay({ model, body, lastErr, requested, useAuto, lockModel, auto, peers, handlerCtx, evt, logCall, mark, perf0, stages, startedAt, plugins, res });
|
|
137
137
|
if (pr.handled) return { done: true };
|
|
@@ -139,7 +139,7 @@ export async function runSerialTrial(ctx, deps = {}) {
|
|
|
139
139
|
}
|
|
140
140
|
if (groups) {
|
|
141
141
|
if (!shouldUseGroupForModel(model)) {
|
|
142
|
-
evt("group-skip", { reqId, model, reason: "useGroup=off (broadband)" });
|
|
142
|
+
evt("group-skip", { reqId, model, reason: isHardLocalOnly(model) ? "provider local-only(禁组员,仅本机直连)" : "useGroup=off (broadband)" });
|
|
143
143
|
} else {
|
|
144
144
|
const br = await broadbandRelay({ model, body, hops, lastErr, requested, useAuto, lockModel, auto, groups, token, bus, logs, handlerCtx, evt, mark, perf0, stages, res, startedAt, plugins });
|
|
145
145
|
if (br.handled) return { done: true };
|
|
@@ -9,6 +9,7 @@ import { readModelsCache } from "../../util.js";
|
|
|
9
9
|
import { pickInteractiveMulti } from "../../interactive.js";
|
|
10
10
|
import { buildAliasMap, readFullAliases, renderProviderList, renderFreeList, groupByProvider } from "./list-render.js";
|
|
11
11
|
import { renderOtherProviders } from "./list-providers.js";
|
|
12
|
+
import { filterStalePicks } from "../../../providers/model-id.js";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* `-model list` 全流程:参数解析 → 刷新/回退 → provider 分支 → TTY 交互 → 分组渲染。
|
|
@@ -137,21 +138,33 @@ export async function handleModelList(args, idx, sub) {
|
|
|
137
138
|
const pickedIds = loadModelPicks();
|
|
138
139
|
const combinedIds = [...ids];
|
|
139
140
|
const seen = new Set(combinedIds);
|
|
141
|
+
let staleFilter = null;
|
|
140
142
|
try {
|
|
141
143
|
const { loadProviderConfigs, loadProviderAllowedModels } = await import("../../../state.js");
|
|
144
|
+
const { buildProviderRows } = await import("../../provider-row.js");
|
|
145
|
+
// 已知 = 已启用(与 -provider list 同一 enabled 口径:有 baseUrl 且有 key;
|
|
146
|
+
// openrouter 有 key 即算,opencode 内置恒启用)。未启用 provider 的 picks
|
|
147
|
+
// 不进列表,启用后自动回来(数据未动)。
|
|
148
|
+
const knownProviders = buildProviderRows({}).filter((r) => r.enabled).map((r) => r.id);
|
|
142
149
|
const configs = loadProviderConfigs();
|
|
143
|
-
|
|
150
|
+
const allowedIds = [];
|
|
151
|
+
for (const pid of knownProviders.filter((k) => String(k).toLowerCase() !== "opencode")) {
|
|
144
152
|
const allowed = loadProviderAllowedModels(pid);
|
|
145
153
|
for (const raw of allowed) {
|
|
146
154
|
const canonical = `${pid}/${raw}`;
|
|
155
|
+
allowedIds.push(canonical);
|
|
147
156
|
if (!seen.has(canonical)) {
|
|
148
157
|
seen.add(canonical);
|
|
149
158
|
combinedIds.push(canonical);
|
|
150
159
|
}
|
|
151
160
|
}
|
|
152
161
|
}
|
|
162
|
+
staleFilter = { knownProviders, liveIds: ids, allowedIds };
|
|
153
163
|
} catch {}
|
|
154
|
-
|
|
164
|
+
// provider 已不存在的 picks 孤儿不进交互列表(数据不动,status --all 仍可审计)。
|
|
165
|
+
// configs 读失败时 staleFilter 为空 → 不过滤(默认放行)。
|
|
166
|
+
const visiblePicks = staleFilter ? filterStalePicks(pickedIds, staleFilter) : pickedIds;
|
|
167
|
+
for (const pid of visiblePicks) {
|
|
155
168
|
if (!seen.has(pid)) {
|
|
156
169
|
seen.add(pid);
|
|
157
170
|
combinedIds.push(pid);
|
|
@@ -75,15 +75,28 @@ export async function handleProviderModels(id, sub, args, rest) {
|
|
|
75
75
|
const part = String(b).split(":")[1];
|
|
76
76
|
return part ? ` [${part}]` : ` [${b}]`;
|
|
77
77
|
};
|
|
78
|
+
// 能力列:上下文 k + 📷识图 🧠推理 🔧工具调用(workbuddy 原生字段;其他供应商无这些字段则显示 —)
|
|
79
|
+
const fmtCaps = (m) => {
|
|
80
|
+
const ctx = Number(m.maxInputTokens) || null;
|
|
81
|
+
const hasAny = ctx || m.supportsImages != null || m.supportsReasoning != null || m.supportsToolCall != null;
|
|
82
|
+
if (!hasAny) return "—";
|
|
83
|
+
const parts = [];
|
|
84
|
+
if (ctx) parts.push(ctx >= 1_000_000 ? `${Math.round(ctx / 100000) / 10}M` : `${Math.round(ctx / 1000)}k`);
|
|
85
|
+
if (m.supportsImages && !m.disabledMultimodal) parts.push("📷");
|
|
86
|
+
if (m.supportsReasoning) parts.push("🧠");
|
|
87
|
+
if (m.supportsToolCall) parts.push("🔧");
|
|
88
|
+
return parts.join(" ");
|
|
89
|
+
};
|
|
78
90
|
const idW = Math.max(22, ...all.map((m) => String(m.id).length)) + 2;
|
|
79
91
|
const priceW = Math.max(6, ...all.map((m) => fmtPrice(m).length)) + 2;
|
|
92
|
+
const capsW = Math.max(4, ...all.map((m) => fmtCaps(m).length)) + 2;
|
|
80
93
|
for (const m of all) {
|
|
81
94
|
const ok = markAllowed(m.id);
|
|
82
95
|
const price = fmtPrice(m);
|
|
83
96
|
const badge = fmtBadge(m);
|
|
84
97
|
const name = m.name ? ` ${m.name}` : "";
|
|
85
98
|
const blocked = ok ? "" : " [blocked — allowlist]";
|
|
86
|
-
const line = ` ${ok ? "✓" : "x"} ${String(m.id).padEnd(idW)}${String(price).padEnd(priceW)}${name}${badge}${blocked}`;
|
|
99
|
+
const line = ` ${ok ? "✓" : "x"} ${String(m.id).padEnd(idW)}${String(price).padEnd(priceW)}${fmtCaps(m).padEnd(capsW)}${name}${badge}${blocked}`;
|
|
87
100
|
console.log(line);
|
|
88
101
|
}
|
|
89
102
|
if (!all.length) console.log(` (no models — check baseUrl/keys or try: curl ${baseUrl}/models)`);
|
package/src/cli/commands/sync.js
CHANGED
|
@@ -88,6 +88,8 @@ export async function handleSetto(args) {
|
|
|
88
88
|
if (r.action === "inserted") inserted++; else updated++;
|
|
89
89
|
prunedTotal += r.pruned || 0;
|
|
90
90
|
console.log(` ${r.action} "${r.id}" -> ${r.internal} @ ${file}`);
|
|
91
|
+
if (r.capsSummaryText) console.log(` ${r.capsSummaryText}`);
|
|
92
|
+
if (r.upgraded) console.log(` 能力补齐 ${r.upgraded} 个旧条目`);
|
|
91
93
|
}
|
|
92
94
|
console.log(`synced to opencode: ${inserted} inserted, ${updated} updated, total ${list.length} @ ${file}`);
|
|
93
95
|
if (prunedTotal) console.log(` pruned ${prunedTotal} 个失效模型(未在 picks,不再于 opencode 显示)`);
|
|
@@ -158,6 +160,9 @@ export async function handleSetto(args) {
|
|
|
158
160
|
const file = opencodeConfigPath();
|
|
159
161
|
const r = await syncToOpencode({ id, token, port, file, keep: pruneKeep(), ensureAll: pruneKeep() });
|
|
160
162
|
console.log(`synced to opencode: ${r.action} "${r.id}" @ ${file}`);
|
|
163
|
+
if (r.capsSummaryText) console.log(` 能力: ${r.capsSummaryText}`);
|
|
164
|
+
else console.log(` 能力: 未收录该模型的能力目录,条目仅含名称(不影响使用)`);
|
|
165
|
+
if (r.upgraded) console.log(` 能力补齐 ${r.upgraded} 个旧条目(此前仅含名称,已注入推理档位/读图/上下文)`);
|
|
161
166
|
if (r.backfilled) console.log(` backfilled ${r.backfilled} 个 picks 模型(此前 pick 了但未同步过,现已补齐)`);
|
|
162
167
|
if (r.pruned) console.log(` pruned ${r.pruned} 个失效模型(未在 picks,不再于 opencode 显示)`);
|
|
163
168
|
console.log(` url: http://127.0.0.1:${port}/v1`);
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// -setto opencode 条目能力注入(ADR-0016 联动):把 models.dev 能力写进 opencode.json
|
|
2
|
+
// 的 per-model 条目(opencode Model 形状,实测 debug config 原样保留并生效),
|
|
3
|
+
// 并产出一行人话摘要供 CLI 展示。查不到能力(未收录 provider/模型)时原样返回不硬造。
|
|
4
|
+
import { globalCapabilities } from "./index.js";
|
|
5
|
+
|
|
6
|
+
// "bai/glm-5.3-flash" → { provider: "bai", raw: "glm-5.3-flash" };裸 id 归 opencode
|
|
7
|
+
function splitProvider(modelId) {
|
|
8
|
+
const s = String(modelId || "").trim();
|
|
9
|
+
const i = s.indexOf("/");
|
|
10
|
+
if (i > 0) return { provider: s.slice(0, i).toLowerCase(), raw: s.slice(i + 1) };
|
|
11
|
+
return { provider: "opencode", raw: s };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// caps → opencode Model 形状增量(只含能力字段,name 等原有键不动)
|
|
15
|
+
// variants:config 条目显式档位(provider.ts config 路径 mergeDeep(config.variants) 优先于启发式,
|
|
16
|
+
// 绕过 transform.ts 黑名单——glm/kimi/deepseek-v3/minimax/qwen/big-pickle 启发式恒 return {})。
|
|
17
|
+
// 参数形状 reasoningEffort 对齐 @ai-sdk/openai-compatible(muse-spark 启发式同款)。
|
|
18
|
+
// toggle/budget_tokens 型不注入(OpenAI 兼容协议无标准开关参数,留启发式判断)。
|
|
19
|
+
function capsToEntryFields(caps) {
|
|
20
|
+
if (!caps) return null;
|
|
21
|
+
const fields = {
|
|
22
|
+
reasoning: Boolean(caps.reasoning),
|
|
23
|
+
tool_call: Boolean(caps.toolCall),
|
|
24
|
+
attachment: Boolean(caps.attachment),
|
|
25
|
+
temperature: Boolean(caps.temperature),
|
|
26
|
+
modalities: { input: caps.inputModalities || ["text"], output: caps.outputModalities || ["text"] },
|
|
27
|
+
limit: { context: Number(caps.context) || 0, output: Number(caps.maxOutput) || 0 },
|
|
28
|
+
};
|
|
29
|
+
if (caps.effortType === "effort" && Array.isArray(caps.effortValues) && caps.effortValues.length) {
|
|
30
|
+
fields.variants = Object.fromEntries(caps.effortValues.map((v) => [String(v), { reasoningEffort: String(v) }]));
|
|
31
|
+
}
|
|
32
|
+
if (caps.releaseDate) fields.release_date = caps.releaseDate;
|
|
33
|
+
if (caps.costIn != null || caps.costOut != null) {
|
|
34
|
+
fields.cost = { input: Number(caps.costIn) || 0, output: Number(caps.costOut) || 0 };
|
|
35
|
+
}
|
|
36
|
+
return fields;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// entry: 现有条目(含 name 等);modelId: 内部 canonical id;capsSvc: 可注入(默认全局单例)
|
|
40
|
+
// wbSource: workbuddy 动态源接缝(测试注入;生产 = workbuddyAllModels 上游原生字段)
|
|
41
|
+
// 返回 { entry: 增强后条目, caps: caps|null };服务异常静默降级(同步命令不能因目录拉取失败而挂)
|
|
42
|
+
export async function enrichOpencodeEntry(entry, modelId, capsSvc, wbSource) {
|
|
43
|
+
const base = entry && typeof entry === "object" && !Array.isArray(entry) ? entry : {};
|
|
44
|
+
const svc = capsSvc === undefined ? globalCapabilities() : capsSvc;
|
|
45
|
+
if (!svc) return { entry: base, caps: null };
|
|
46
|
+
const { provider, raw } = splitProvider(modelId);
|
|
47
|
+
let caps = null;
|
|
48
|
+
try {
|
|
49
|
+
await svc.ready();
|
|
50
|
+
caps = svc.get(provider, raw) || null;
|
|
51
|
+
// 降级匹配:mslxdff 自建目录的 -free/-search-free/-expert-free 后缀在 models.dev 无后缀
|
|
52
|
+
if (!caps && /-free$/.test(raw)) caps = svc.get(provider, raw.replace(/-free$/, "")) || null;
|
|
53
|
+
// workbuddy 不在 models.dev 目录 → 上游原生字段兜底(maxInputTokens/supportsImages/... first-party 最准)
|
|
54
|
+
if (!caps && provider === "workbuddy") {
|
|
55
|
+
const { workbuddyCapsFromModels, workbuddyAllModels } = await import("./index.js");
|
|
56
|
+
const map = await workbuddyCapsFromModels(wbSource || workbuddyAllModels)();
|
|
57
|
+
caps = map[raw] || null;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
caps = null; // 目录不可用:降级为无能力条目(staleness 兜底在服务内已做)
|
|
61
|
+
}
|
|
62
|
+
const fields = capsToEntryFields(caps);
|
|
63
|
+
if (!fields) return { entry: base, caps: null };
|
|
64
|
+
return { entry: { ...base, ...fields }, caps };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// caps → 一行人话摘要(无有效信息返回 "",调用方按空跳过不噪音)
|
|
68
|
+
export function capsSummary(caps) {
|
|
69
|
+
if (!caps) return "";
|
|
70
|
+
const parts = [];
|
|
71
|
+
if (caps.effortType === "effort" && Array.isArray(caps.effortValues) && caps.effortValues.length) {
|
|
72
|
+
parts.push(`推理档 ${caps.effortValues.join("/")}`);
|
|
73
|
+
} else if (caps.effortType === "toggle") {
|
|
74
|
+
parts.push("推理 开关型");
|
|
75
|
+
} else if (caps.effortType === "budget_tokens") {
|
|
76
|
+
parts.push("推理 budget_tokens");
|
|
77
|
+
} else if (caps.reasoning) {
|
|
78
|
+
parts.push(caps.defaultEffort ? `推理(默认 ${caps.defaultEffort})` : "推理模型");
|
|
79
|
+
}
|
|
80
|
+
if (caps.imageInput) parts.push("📷读图");
|
|
81
|
+
if (caps.context) parts.push(`上下文 ${caps.context >= 1000 ? `${Math.round(caps.context / 1000)}k` : caps.context}`);
|
|
82
|
+
if (caps.costIn != null || caps.costOut != null) {
|
|
83
|
+
parts.push(`$${caps.costIn ?? 0}/${caps.costOut ?? 0} 每M`);
|
|
84
|
+
}
|
|
85
|
+
return parts.join(" · ");
|
|
86
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// 模型能力目录服务(opencode 官方同源 models.dev):fetch + 磁盘缓存 + TTL + staleness 降级
|
|
2
|
+
// 源:https://models.opencode.ai/api.json(opencode core models-dev.ts:160 同款;备选 https://models.dev/api.json)
|
|
3
|
+
// 形状:{ [providerId]: { models: { [modelId]: raw } } };mslxdff 裸 id 归 opencode,`prov/id` 前缀路由到对应 provider
|
|
4
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { dirname, join } from "node:path";
|
|
7
|
+
import { normalizeProviderModels, normalizeWorkbuddyCaps } from "./parse.js";
|
|
8
|
+
import { compatFetch } from "../compat.js";
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_SOURCE_URL = "https://models.opencode.ai/api.json";
|
|
11
|
+
|
|
12
|
+
export function sourceUrl() {
|
|
13
|
+
const raw = process.env.MSLXDFF_MODELS_DEV_URL;
|
|
14
|
+
return raw && String(raw).trim() ? String(raw).trim() : DEFAULT_SOURCE_URL;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function ttlMs() {
|
|
18
|
+
const n = Number(process.env.MSLXDFF_MODELS_DEV_TTL_MS);
|
|
19
|
+
return Number.isInteger(n) && n >= 0 ? n : 86_400_000; // 默认 24h
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function createCapabilitiesService({
|
|
23
|
+
fetchImpl = compatFetch,
|
|
24
|
+
cacheFile = "",
|
|
25
|
+
ttlMs: ttl = ttlMs(),
|
|
26
|
+
url = sourceUrl(),
|
|
27
|
+
now = Date.now,
|
|
28
|
+
} = {}) {
|
|
29
|
+
let raw = null; // 原始目录(全 provider)
|
|
30
|
+
let capsIndex = new Map(); // providerId -> { modelId -> caps }
|
|
31
|
+
let loadedAt = 0;
|
|
32
|
+
let inflight = null;
|
|
33
|
+
|
|
34
|
+
function buildIndex(data) {
|
|
35
|
+
const idx = new Map();
|
|
36
|
+
for (const [pid, p] of Object.entries(data || {})) {
|
|
37
|
+
if (!p || typeof p !== "object") continue;
|
|
38
|
+
idx.set(pid, normalizeProviderModels(p.models || {}));
|
|
39
|
+
}
|
|
40
|
+
return idx;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function readCache() {
|
|
44
|
+
if (!cacheFile) return null;
|
|
45
|
+
try { return JSON.parse(readFileSync(cacheFile, "utf8")); } catch { return null; }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function writeCache(data) {
|
|
49
|
+
if (!cacheFile || !data) return;
|
|
50
|
+
try {
|
|
51
|
+
mkdirSync(dirname(cacheFile), { recursive: true });
|
|
52
|
+
writeFileSync(cacheFile, JSON.stringify(data));
|
|
53
|
+
} catch { /* 缓存失败不致命 */ }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function fetchFresh() {
|
|
57
|
+
const res = await fetchImpl(url, { headers: { Accept: "application/json" } });
|
|
58
|
+
if (!res?.ok) throw new Error(`models.dev fetch ${res?.status || "network"}`);
|
|
59
|
+
return res.json();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ready:缓存新鲜直接用;否则拉新;失败回退旧缓存(含磁盘),完全无数据才抛
|
|
63
|
+
async function ready() {
|
|
64
|
+
const t = now();
|
|
65
|
+
if (raw && t - loadedAt < ttl) return;
|
|
66
|
+
if (inflight) return inflight;
|
|
67
|
+
inflight = (async () => {
|
|
68
|
+
try {
|
|
69
|
+
const data = await fetchFresh();
|
|
70
|
+
raw = data;
|
|
71
|
+
capsIndex = buildIndex(data);
|
|
72
|
+
loadedAt = t;
|
|
73
|
+
writeCache(data);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
if (raw) return; // 内存还有旧的,继续用
|
|
76
|
+
const disk = readCache();
|
|
77
|
+
if (disk) {
|
|
78
|
+
raw = disk;
|
|
79
|
+
capsIndex = buildIndex(disk);
|
|
80
|
+
loadedAt = t; // 视作刚加载,避免每请求都重试打上游
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
throw e;
|
|
84
|
+
} finally {
|
|
85
|
+
inflight = null;
|
|
86
|
+
}
|
|
87
|
+
})();
|
|
88
|
+
return inflight;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function capsFor(pid) {
|
|
92
|
+
return capsIndex.get(pid) || null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function get(providerId, modelId) {
|
|
96
|
+
const pid = String(providerId || "opencode").toLowerCase();
|
|
97
|
+
const mid = String(modelId || "");
|
|
98
|
+
const map = capsFor(pid);
|
|
99
|
+
if (!map) return null;
|
|
100
|
+
return map[mid] || null;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function list(providerId) {
|
|
104
|
+
const pid = String(providerId || "opencode").toLowerCase();
|
|
105
|
+
const map = capsFor(pid);
|
|
106
|
+
if (!map) return [];
|
|
107
|
+
return Object.entries(map).map(([id, capabilities]) => ({ id, capabilities }));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function providers() {
|
|
111
|
+
return [...capsIndex.keys()].sort();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return { ready, get, list, providers };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 模块级单例(与 globalDedup 同模式):HTTP handler 懒加载,测试 _reset 后注入
|
|
118
|
+
let _global = null;
|
|
119
|
+
export function globalCapabilities() {
|
|
120
|
+
if (!_global) _global = createCapabilitiesService({ cacheFile: defaultCacheFile() });
|
|
121
|
+
return _global;
|
|
122
|
+
}
|
|
123
|
+
export function _resetGlobalCapabilities() { _global = null; }
|
|
124
|
+
|
|
125
|
+
// 缓存落盘位置:MSLXDFF_MODELS_DEV_CACHE 覆盖 > ~/.config/mslxdff/models-dev.json(与 state 同目录)
|
|
126
|
+
function defaultCacheFile() {
|
|
127
|
+
const override = process.env.MSLXDFF_MODELS_DEV_CACHE;
|
|
128
|
+
if (override && String(override).trim()) return String(override).trim();
|
|
129
|
+
return join(homedir(), ".config", "mslxdff", "models-dev.json");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// workbuddy 动态源:从聚合模型服务(models.get(),带 10min 上游缓存)拉 workbuddy/ 前缀条目
|
|
133
|
+
// → 统一 caps 形状 map({ rawId -> caps })。上游原生字段 first-party 最准,不走 models.dev。
|
|
134
|
+
export function workbuddyCapsFromModels(getModels, normalizeFn) {
|
|
135
|
+
const normalize = normalizeFn || ((m) => normalizeWorkbuddyCaps(m.id, m));
|
|
136
|
+
return async () => {
|
|
137
|
+
const agg = await getModels();
|
|
138
|
+
const all = Array.isArray(agg?.data) ? agg.data : [];
|
|
139
|
+
const map = {};
|
|
140
|
+
for (const entry of all) {
|
|
141
|
+
const id = String(entry?.id || "");
|
|
142
|
+
if (!id.toLowerCase().startsWith("workbuddy/")) continue;
|
|
143
|
+
const raw = id.slice("workbuddy/".length);
|
|
144
|
+
if (!raw) continue;
|
|
145
|
+
map[raw] = normalize(entry);
|
|
146
|
+
}
|
|
147
|
+
return map;
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// workbuddy provider 单例(直连上游 listModels 自带 10min 缓存):HTTP handler 与
|
|
152
|
+
// -setto opencode 能力注入共享同一个实例,避免双份连接池/缓存。
|
|
153
|
+
let _wbProv = null;
|
|
154
|
+
export function _resetWorkbuddyProv() { _wbProv = null; }
|
|
155
|
+
export async function workbuddyAllModels() {
|
|
156
|
+
if (!_wbProv) {
|
|
157
|
+
const { createWorkbuddyProvider } = await import("../providers/workbuddy.js");
|
|
158
|
+
const { defaultStateFile } = await import("../state.js");
|
|
159
|
+
_wbProv = createWorkbuddyProvider({ file: defaultStateFile() });
|
|
160
|
+
}
|
|
161
|
+
const list = await _wbProv.listModels();
|
|
162
|
+
return { object: "list", data: Array.isArray(list) ? list : [] };
|
|
163
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// provider.models 对象({ [modelId]: rawModel })→ { [modelId]: caps }
|
|
31
|
+
export function normalizeProviderModels(modelsObj) {
|
|
32
|
+
const out = {};
|
|
33
|
+
if (!modelsObj || typeof modelsObj !== "object") return out;
|
|
34
|
+
for (const [id, m] of Object.entries(modelsObj)) {
|
|
35
|
+
if (!m || typeof m !== "object") continue;
|
|
36
|
+
out[id] = normalizeModelCaps(id, m);
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// workbuddy 上游原生字段(/console/enterprises/personal/models,first-party 最准)→ 统一 caps 形状
|
|
42
|
+
// 实测字段(2026-09-11,29 模型全覆盖):maxInputTokens/maxOutputTokens/supportsImages/
|
|
43
|
+
// supportsReasoning/reasoning{effort,summary}/supportsToolCall/disabledMultimodal/credits/tags/name/vendor
|
|
44
|
+
// reasoning 是"当前档位"非档位列表 → effortType=effort 但 effortValues=null、defaultEffort 记默认档
|
|
45
|
+
export function normalizeWorkbuddyCaps(_id, m) {
|
|
46
|
+
const imageOk = Boolean(m?.supportsImages) && !m?.disabledMultimodal;
|
|
47
|
+
const reasoning = Boolean(m?.supportsReasoning);
|
|
48
|
+
const effort = reasoning && m?.reasoning && typeof m.reasoning === "object" ? String(m.reasoning.effort || "").trim() : "";
|
|
49
|
+
return {
|
|
50
|
+
reasoning,
|
|
51
|
+
effortType: reasoning ? "effort" : null,
|
|
52
|
+
// 上游只给当前档位不给档位列表;实测 hy3 reasoning_effort low/high 均生效(思考块 99 vs 210),
|
|
53
|
+
// 网关层按 OpenAI 兼容标准给通用三档(上游不识别时忽略,无害)
|
|
54
|
+
effortValues: reasoning ? ["low", "medium", "high"] : null,
|
|
55
|
+
defaultEffort: effort || null,
|
|
56
|
+
imageInput: imageOk,
|
|
57
|
+
toolCall: Boolean(m?.supportsToolCall),
|
|
58
|
+
context: Number(m?.maxInputTokens) || null,
|
|
59
|
+
maxOutput: Number(m?.maxOutputTokens) || null,
|
|
60
|
+
costIn: null,
|
|
61
|
+
costOut: null,
|
|
62
|
+
attachment: false,
|
|
63
|
+
temperature: m?.temperature != null,
|
|
64
|
+
releaseDate: null,
|
|
65
|
+
inputModalities: imageOk ? ["text", "image"] : ["text"],
|
|
66
|
+
outputModalities: ["text"],
|
|
67
|
+
};
|
|
68
|
+
}
|
package/src/providers/base.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createKeyRing } from "./keyring.js";
|
|
2
2
|
import { joinModelId } from "./model-id.js";
|
|
3
3
|
import { getUndici as compatGetUndici, compatFetch } from "../compat.js";
|
|
4
|
+
import { createSdkDispatch } from "../upstream-engine/sdk/dispatch.js";
|
|
4
5
|
|
|
5
6
|
const { fetch: UndiciFetch, Agent: UndiciAgent } = compatGetUndici();
|
|
6
7
|
|
|
@@ -44,8 +45,17 @@ export function collectApiKeysGeneric(id, apiKeys, apiKey, loadKeys) {
|
|
|
44
45
|
return [...new Set(list.map((k) => k.trim()))];
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
export function createChatRunner({ id, ring, cooldownMs, retry, fetchImpl, dispatcher, buildHeaders, getUrl, connectTimeoutMs = 30_000 }) {
|
|
48
|
+
export function createChatRunner({ id, ring, cooldownMs, retry, fetchImpl, dispatcher, buildHeaders, getUrl, connectTimeoutMs = 30_000, sdkDispatch } = {}) {
|
|
49
|
+
// SDK 通道(缺省):仅流式;非流式委派原生,避免把 JSON 客户端 SSE 化。
|
|
50
|
+
const sdk = sdkDispatch === undefined ? createSdkDispatch({ id, providerName: id }) : sdkDispatch;
|
|
51
|
+
|
|
48
52
|
async function attemptOnce(url, body, key, activeRing) {
|
|
53
|
+
if (sdk?.enabled && body?.stream !== false) {
|
|
54
|
+
let r;
|
|
55
|
+
try { r = await sdk.trySdk({ url, body, headers: buildHeaders(body, key), fetchImpl }); }
|
|
56
|
+
catch (err) { return err; } // 交 runChat 的 network 重试/冷却
|
|
57
|
+
if (r) return r.status === 401 && !activeRing.size ? { __needKey: true } : r;
|
|
58
|
+
}
|
|
49
59
|
const controller = new AbortController();
|
|
50
60
|
const timer = setTimeout(() => controller.abort(new Error(`${id} timed out after ${connectTimeoutMs}ms`)), connectTimeoutMs);
|
|
51
61
|
try {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// ADR-0015:供应商路由分类三态 + EMA 纯函数(零依赖,防循环 import)
|
|
2
|
+
// local-only:本机账号绑定(workbuddy),组员转发无效
|
|
3
|
+
// quota-pool:图额度不图速度(opencode free),直连先行、429 后组员兜底
|
|
4
|
+
// latency-compare:key/token 类,direct vs link+remote 比延迟
|
|
5
|
+
export function classifyProvider(id) {
|
|
6
|
+
const s = String(id || "").trim().toLowerCase();
|
|
7
|
+
if (s === "workbuddy") return "local-only";
|
|
8
|
+
if (s === "opencode") return "quota-pool";
|
|
9
|
+
return "latency-compare";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// EMA(α=0.3):prev 为空取 now;now 非法样本不参与;两者皆非法返 null
|
|
13
|
+
// 0ms 是合法样本(本地/瞬时响应),只有 null/undefined/NaN/负数才非法
|
|
14
|
+
export function emaMerge(prev, now, alpha = 0.3) {
|
|
15
|
+
const norm = (v) => (v === null || v === undefined ? null : (Number.isFinite(Number(v)) && Number(v) >= 0 ? Number(v) : null));
|
|
16
|
+
const a = norm(prev);
|
|
17
|
+
const b = norm(now);
|
|
18
|
+
if (a === null && b === null) return null;
|
|
19
|
+
if (a === null) return Math.round(b);
|
|
20
|
+
if (b === null) return Math.round(a);
|
|
21
|
+
return Math.round(a * (1 - alpha) + b * alpha);
|
|
22
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { joinUrl, sleep } from "../base.js";
|
|
2
2
|
import { clineHeaders } from "./headers.js";
|
|
3
3
|
import { createTransport } from "../../transport/index.js";
|
|
4
|
+
import { createSdkDispatch } from "../../upstream-engine/sdk/dispatch.js";
|
|
4
5
|
|
|
5
6
|
function genSessionId() { return `sess_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`; }
|
|
6
7
|
|
|
@@ -70,12 +71,18 @@ export function createChatService({
|
|
|
70
71
|
const resolvedBase = String(baseUrl).trim().replace(/\/+$/, "");
|
|
71
72
|
const resolvedChat = chatPath || (String(resolvedBase).includes("/api/v1") ? "/chat/completions" : "/api/v1/chat/completions");
|
|
72
73
|
const transport = createTransport({ fetchImpl, dispatcher, keepAlive: !!dispatcher, timeoutMs: connectTimeoutMs, retry: {} });
|
|
74
|
+
const sdk = createSdkDispatch({ id, providerName: id || "cline" });
|
|
73
75
|
|
|
74
|
-
async function clineFetch(body, sessionId) {
|
|
76
|
+
async function clineFetch(body, sessionId, allowSdk = false) {
|
|
75
77
|
const token = await authPool.getAccessToken();
|
|
76
78
|
const headers = clineHeaders(sessionId, token);
|
|
77
79
|
const finalUrl = joinUrl(resolvedBase, resolvedChat);
|
|
78
80
|
const isStream = body?.stream === true;
|
|
81
|
+
// SDK 通道(缺省):仅客户端显式流式;forceStream 聚合与非流式保持原生(避免丢 nonStreamWithContentCheck)。
|
|
82
|
+
if (allowSdk && sdk.enabled) {
|
|
83
|
+
const r = await sdk.trySdk({ url: finalUrl, body, headers, fetchImpl });
|
|
84
|
+
if (r) return r;
|
|
85
|
+
}
|
|
79
86
|
return transport.request({ url: finalUrl, headers, body, stream: isStream, timeoutMs: connectTimeoutMs });
|
|
80
87
|
}
|
|
81
88
|
|
|
@@ -85,14 +92,22 @@ export function createChatService({
|
|
|
85
92
|
return false;
|
|
86
93
|
}
|
|
87
94
|
|
|
88
|
-
async function clineFetchWithRetry(body, sessionId) {
|
|
95
|
+
async function clineFetchWithRetry(body, sessionId, allowSdk = false) {
|
|
89
96
|
const maxRetries = 4;
|
|
90
97
|
let lastResp = null;
|
|
91
98
|
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
92
|
-
const
|
|
93
|
-
|
|
99
|
+
const resp0 = await authPool.enqueue(() => clineFetch(body, sessionId, allowSdk));
|
|
100
|
+
let resp = resp0;
|
|
94
101
|
let bodyText = "";
|
|
95
|
-
if (resp.status !== 200) {
|
|
102
|
+
if (resp.status !== 200) {
|
|
103
|
+
try { bodyText = await resp.text(); } catch {}
|
|
104
|
+
// 错误响应体已在此读走:原生 transport 的 text() 有缓存,而 SDK Response 的 body 一次性,
|
|
105
|
+
// 重建以便上层/客户端仍能读到错误详情(如 429 的 INFERENCE_CAP_ERROR)。
|
|
106
|
+
try {
|
|
107
|
+
resp = new Response(bodyText, { status: resp0.status, statusText: resp0.statusText, headers: new Headers(resp0.headers) });
|
|
108
|
+
} catch {}
|
|
109
|
+
}
|
|
110
|
+
lastResp = resp;
|
|
96
111
|
const hit = isLimitHit(resp.status, bodyText);
|
|
97
112
|
if (hit) {
|
|
98
113
|
const { parseCooldown } = await import("./auth.js");
|
|
@@ -174,7 +189,7 @@ export function createChatService({
|
|
|
174
189
|
}
|
|
175
190
|
for (let netAttempt = 0; netAttempt < 3; netAttempt++) {
|
|
176
191
|
try {
|
|
177
|
-
const resp = await clineFetchWithRetry(upstreamBody, sessionId);
|
|
192
|
+
const resp = await clineFetchWithRetry(upstreamBody, sessionId, isStream);
|
|
178
193
|
if (!resp) throw new Error("empty response");
|
|
179
194
|
if (!resp.ok) return resp;
|
|
180
195
|
if (isStream) return resp;
|