mslxdff 0.1.110 → 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/model-capabilities/enrich.js +16 -2
- package/src/model-capabilities/index.js +34 -1
- package/src/model-capabilities/parse.js +29 -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/models-route.js +32 -4
- package/src/runtime/providers-setup.js +2 -1
- package/src/state/schemas/use-group.js +16 -0
- package/src/sync-opencode.js +3 -2
- 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)`);
|
|
@@ -12,6 +12,10 @@ function splitProvider(modelId) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
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 兼容协议无标准开关参数,留启发式判断)。
|
|
15
19
|
function capsToEntryFields(caps) {
|
|
16
20
|
if (!caps) return null;
|
|
17
21
|
const fields = {
|
|
@@ -22,6 +26,9 @@ function capsToEntryFields(caps) {
|
|
|
22
26
|
modalities: { input: caps.inputModalities || ["text"], output: caps.outputModalities || ["text"] },
|
|
23
27
|
limit: { context: Number(caps.context) || 0, output: Number(caps.maxOutput) || 0 },
|
|
24
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
|
+
}
|
|
25
32
|
if (caps.releaseDate) fields.release_date = caps.releaseDate;
|
|
26
33
|
if (caps.costIn != null || caps.costOut != null) {
|
|
27
34
|
fields.cost = { input: Number(caps.costIn) || 0, output: Number(caps.costOut) || 0 };
|
|
@@ -30,8 +37,9 @@ function capsToEntryFields(caps) {
|
|
|
30
37
|
}
|
|
31
38
|
|
|
32
39
|
// entry: 现有条目(含 name 等);modelId: 内部 canonical id;capsSvc: 可注入(默认全局单例)
|
|
40
|
+
// wbSource: workbuddy 动态源接缝(测试注入;生产 = workbuddyAllModels 上游原生字段)
|
|
33
41
|
// 返回 { entry: 增强后条目, caps: caps|null };服务异常静默降级(同步命令不能因目录拉取失败而挂)
|
|
34
|
-
export async function enrichOpencodeEntry(entry, modelId, capsSvc) {
|
|
42
|
+
export async function enrichOpencodeEntry(entry, modelId, capsSvc, wbSource) {
|
|
35
43
|
const base = entry && typeof entry === "object" && !Array.isArray(entry) ? entry : {};
|
|
36
44
|
const svc = capsSvc === undefined ? globalCapabilities() : capsSvc;
|
|
37
45
|
if (!svc) return { entry: base, caps: null };
|
|
@@ -42,6 +50,12 @@ export async function enrichOpencodeEntry(entry, modelId, capsSvc) {
|
|
|
42
50
|
caps = svc.get(provider, raw) || null;
|
|
43
51
|
// 降级匹配:mslxdff 自建目录的 -free/-search-free/-expert-free 后缀在 models.dev 无后缀
|
|
44
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
|
+
}
|
|
45
59
|
} catch {
|
|
46
60
|
caps = null; // 目录不可用:降级为无能力条目(staleness 兜底在服务内已做)
|
|
47
61
|
}
|
|
@@ -61,7 +75,7 @@ export function capsSummary(caps) {
|
|
|
61
75
|
} else if (caps.effortType === "budget_tokens") {
|
|
62
76
|
parts.push("推理 budget_tokens");
|
|
63
77
|
} else if (caps.reasoning) {
|
|
64
|
-
parts.push("推理模型");
|
|
78
|
+
parts.push(caps.defaultEffort ? `推理(默认 ${caps.defaultEffort})` : "推理模型");
|
|
65
79
|
}
|
|
66
80
|
if (caps.imageInput) parts.push("📷读图");
|
|
67
81
|
if (caps.context) parts.push(`上下文 ${caps.context >= 1000 ? `${Math.round(caps.context / 1000)}k` : caps.context}`);
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
5
5
|
import { homedir } from "node:os";
|
|
6
6
|
import { dirname, join } from "node:path";
|
|
7
|
-
import { normalizeProviderModels } from "./parse.js";
|
|
7
|
+
import { normalizeProviderModels, normalizeWorkbuddyCaps } from "./parse.js";
|
|
8
8
|
import { compatFetch } from "../compat.js";
|
|
9
9
|
|
|
10
10
|
export const DEFAULT_SOURCE_URL = "https://models.opencode.ai/api.json";
|
|
@@ -128,3 +128,36 @@ function defaultCacheFile() {
|
|
|
128
128
|
if (override && String(override).trim()) return String(override).trim();
|
|
129
129
|
return join(homedir(), ".config", "mslxdff", "models-dev.json");
|
|
130
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
|
+
}
|
|
@@ -37,3 +37,32 @@ export function normalizeProviderModels(modelsObj) {
|
|
|
37
37
|
}
|
|
38
38
|
return out;
|
|
39
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;
|
|
@@ -131,4 +131,33 @@ export function normalizeFullId(id, knownProviders = []) {
|
|
|
131
131
|
return `${head}/${s.slice(idx + 1)}`;
|
|
132
132
|
}
|
|
133
133
|
return `${DEFAULT_PROVIDER}/${s}`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const BUILTIN_PROVIDERS = new Set([DEFAULT_PROVIDER, "openrouter"]);
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 过滤 provider 已不存在的 picks 孤儿(纯函数,零 IO,可单测)。
|
|
140
|
+
* - 含 `/` 前缀:head(归一+小写)∉ 已知集合 → stale(如 amddev/*)。
|
|
141
|
+
* - Bare id:∉ 上游 liveIds ∪ allowlist 展开 → stale(如 laguna-s-2.1-free)。
|
|
142
|
+
* - provider 存在但未启用(如 deepseek 缺 baseUrl)→ 保留,不可用由 [error] 表达。
|
|
143
|
+
* - 未知一律保留(默认放行不误删显示);dash 别名经 resolveAlias 还原后判定;
|
|
144
|
+
* 上游 liveIds 为空时 bare 一律保留(无法判定则放行)。
|
|
145
|
+
* 数据不动,只决定进不进 `-models` 交互列表 — 见 .scratch/models-stale-picks/SPEC.md。
|
|
146
|
+
*/
|
|
147
|
+
export function filterStalePicks(pickedIds, { knownProviders = [], liveIds = [], allowedIds = [], resolveAlias = getModelAlias } = {}) {
|
|
148
|
+
const known = new Set([...BUILTIN_PROVIDERS, ...(knownProviders || []).map((p) => normalizeProviderId(p).toLowerCase())]);
|
|
149
|
+
const live = new Set((liveIds || []).map((id) => String(id).toLowerCase()));
|
|
150
|
+
const allowed = new Set((allowedIds || []).map((id) => String(id).toLowerCase()));
|
|
151
|
+
return (pickedIds || []).filter((pick) => {
|
|
152
|
+
const raw = String(pick || "").trim();
|
|
153
|
+
if (!raw) return false;
|
|
154
|
+
let id = raw;
|
|
155
|
+
try { id = resolveAlias(raw) || raw; } catch { id = raw; }
|
|
156
|
+
const slash = String(id).indexOf("/");
|
|
157
|
+
if (slash > 0) return known.has(normalizeProviderId(id.slice(0, slash)).toLowerCase());
|
|
158
|
+
// 上游列表为空(刷新与缓存双失败的极端情况)时无法判定 bare 生死 → 保留(默认放行)
|
|
159
|
+
if (live.size === 0) return true;
|
|
160
|
+
const low = String(id).toLowerCase();
|
|
161
|
+
return live.has(low) || allowed.has(low);
|
|
162
|
+
});
|
|
134
163
|
}
|
|
@@ -3,6 +3,8 @@ import { isAuthError, isInsufficientStatus } from "./auth.js";
|
|
|
3
3
|
import { appendRotationLog as defaultAppend } from "./rotation-log.js";
|
|
4
4
|
import { createTransport } from "../../transport/index.js";
|
|
5
5
|
import { reshapeWorkbuddySse } from "./reshape.js";
|
|
6
|
+
import { attemptOnceSdk } from "./sdk-chat.js";
|
|
7
|
+
import { resolveEngineMode } from "../../upstream-engine/mode.js";
|
|
6
8
|
|
|
7
9
|
function buildAuthHeaders(key, auth) {
|
|
8
10
|
const h = {
|
|
@@ -79,7 +81,24 @@ export function createChatService({
|
|
|
79
81
|
return { uid: "", domain: "www.codebuddy.cn", enterpriseId: "", refreshToken: "" };
|
|
80
82
|
}
|
|
81
83
|
|
|
84
|
+
// SDK 通道(缺省启用):缺省底层走 @ai-sdk/openai-compatible,`legacy`/关闭词
|
|
85
|
+
// (局部 MSLXDFF_WORKBUDDY_SDK,未设则继承全局 MSLXDFF_UPSTREAM_ENGINE)回退原生 transport;
|
|
86
|
+
// 不可用(Node16/未安装)自动回退并告警一次。上层轮换/刷新/reshape 全链复用。
|
|
87
|
+
// Note: 为什么默认 SDK、翻译层代价与回退语义 — 见 .agents/notes/implemented/feature/2026-09-12-workbuddy-sdk-channel.md
|
|
88
|
+
const engineMode = resolveEngineMode(process.env, "MSLXDFF_WORKBUDDY_SDK");
|
|
89
|
+
let sdkFallbackLogged = false;
|
|
82
90
|
async function fetchOnce(url, body, key, auth) {
|
|
91
|
+
if (engineMode === "sdk") {
|
|
92
|
+
try {
|
|
93
|
+
return await attemptOnceSdk({ url, body, key, auth, buildHeaders: buildAuthHeaders });
|
|
94
|
+
} catch (e) {
|
|
95
|
+
if (!e || !e._sdkLoadFailed) throw e;
|
|
96
|
+
if (!sdkFallbackLogged) {
|
|
97
|
+
sdkFallbackLogged = true;
|
|
98
|
+
try { console.error(`[workbuddy] ${e.message} — 回退原生通道`); } catch {}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
83
102
|
return transport.request({ url, headers: buildAuthHeaders(key, auth), body: { ...body, stream: true }, stream: true });
|
|
84
103
|
}
|
|
85
104
|
|
|
@@ -107,24 +107,31 @@ export function reshapeWorkbuddySse(res) {
|
|
|
107
107
|
async pull(controller) {
|
|
108
108
|
if (closed) { try { controller.close(); } catch {} return; }
|
|
109
109
|
try {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
110
|
+
// 循环读源,直到"有输出 / 流结束 / 出错"才返回。
|
|
111
|
+
// WHATWG 流规范:pull 的 promise 解析后若未 enqueue,规范不会再自动调度 pull
|
|
112
|
+
// (仅当解析期间有新读请求触发 pullAgain 才重调)——消费者只挂一个待处理 read 时,
|
|
113
|
+
// 任何一次"只吞帧不出数据"的拉取都会让整流永久停摆。workbuddy 思考流按帧到达
|
|
114
|
+
// (role 帧后每个 reasoning 碎片都不足 150 字符阈值)时必现,即"首帧后卡死"根因。
|
|
115
|
+
for (;;) {
|
|
116
|
+
const { done, value } = await reader.read();
|
|
117
|
+
if (done) {
|
|
118
|
+
closed = true;
|
|
119
|
+
const out = [];
|
|
120
|
+
if (buf) { processInto(buf + "\n", out); buf = ""; }
|
|
121
|
+
flushReasoningInto(out);
|
|
122
|
+
if (out.length) controller.enqueue(encoder.encode(out.join("")));
|
|
123
|
+
controller.close();
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
buf += decoder.decode(value, { stream: true });
|
|
127
|
+
const idx = buf.lastIndexOf("\n");
|
|
128
|
+
if (idx < 0) continue;
|
|
129
|
+
const complete = buf.slice(0, idx + 1);
|
|
130
|
+
buf = buf.slice(idx + 1);
|
|
113
131
|
const out = [];
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
if (out.length) controller.enqueue(encoder.encode(out.join("")));
|
|
117
|
-
controller.close();
|
|
118
|
-
return;
|
|
132
|
+
processInto(complete, out);
|
|
133
|
+
if (out.length) { controller.enqueue(encoder.encode(out.join(""))); return; }
|
|
119
134
|
}
|
|
120
|
-
buf += decoder.decode(value, { stream: true });
|
|
121
|
-
const idx = buf.lastIndexOf("\n");
|
|
122
|
-
if (idx < 0) return;
|
|
123
|
-
const complete = buf.slice(0, idx + 1);
|
|
124
|
-
buf = buf.slice(idx + 1);
|
|
125
|
-
const out = [];
|
|
126
|
-
processInto(complete, out);
|
|
127
|
-
if (out.length) controller.enqueue(encoder.encode(out.join("")));
|
|
128
135
|
} catch {
|
|
129
136
|
closed = true;
|
|
130
137
|
try { controller.close(); } catch {}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// workbuddy SDK 实验通道(薄壳):转调共用执行器 upstream-engine/sdk/attempt.js。
|
|
2
|
+
// 契约保持:attemptOnceSdk({url, body, key, auth, buildHeaders}) → Response(SSE)。
|
|
3
|
+
// HTTP 错误映射与 _sdkLoadFailed 语义在共用层;本壳只注入 workbuddy 特有参数。
|
|
4
|
+
// 见 .scratch/workbuddy-sdk-channel/SPEC.md。
|
|
5
|
+
import { attemptOnceSdk as attemptGeneric, sdkBaseFromUrl } from "../../upstream-engine/sdk/attempt.js";
|
|
6
|
+
|
|
7
|
+
export { sdkBaseFromUrl };
|
|
8
|
+
|
|
9
|
+
let channelLogged = false;
|
|
10
|
+
|
|
11
|
+
export async function attemptOnceSdk({ url, body, key, auth, buildHeaders, clock = Date.now } = {}) {
|
|
12
|
+
const out = await attemptGeneric({
|
|
13
|
+
url,
|
|
14
|
+
body,
|
|
15
|
+
headers: buildHeaders ? buildHeaders(key, auth) : {},
|
|
16
|
+
providerName: "workbuddy",
|
|
17
|
+
marker: { name: "x-mslxdff-workbuddy-channel", value: "sdk" },
|
|
18
|
+
clock,
|
|
19
|
+
});
|
|
20
|
+
if (!channelLogged) {
|
|
21
|
+
channelLogged = true;
|
|
22
|
+
try { console.error(`[workbuddy-sdk-channel] active via @ai-sdk/openai-compatible (model=${body?.model || ""} uid=${auth?.uid || ""})`); } catch {}
|
|
23
|
+
}
|
|
24
|
+
return out;
|
|
25
|
+
}
|
package/src/routes/hedge.js
CHANGED
|
@@ -18,8 +18,8 @@ function shouldHedge({ isStream, canForwardPeers, hedgeDelayMs: d, hasPeers, mod
|
|
|
18
18
|
if (!canForwardPeers) return false;
|
|
19
19
|
if (!hasPeers) return false;
|
|
20
20
|
if (!d || d <= 0) return false;
|
|
21
|
-
//
|
|
22
|
-
// muse-spark
|
|
21
|
+
// 供应商级禁对冲在 shouldUseGroupForModel 层收敛(workbuddy local-only 硬禁,见 ADR-0015);
|
|
22
|
+
// 这里只留 muse-spark 特判:走 /responses 流式(event: 包装 + 加密 reasoning),组员旧版无此整形且聚合 JSON 带错 header,必抢赢本地慢首块,需本地直出
|
|
23
23
|
if (String(model || "").toLowerCase().startsWith("muse-spark")) return false;
|
|
24
24
|
return true;
|
|
25
25
|
}
|