mslxdff 0.1.236 → 0.1.237
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 +264 -264
- package/src/autostart.js +62 -15
- 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 +155 -155
- 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 +282 -282
- 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 +293 -293
- package/src/peers.js +389 -389
- 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 +292 -292
- package/src/providers/qoder/index.js +311 -311
- package/src/providers/qoder/sse.js +233 -233
- 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 +219 -219
- package/src/providers/raccoon/credits.js +285 -285
- package/src/providers/registry.js +89 -89
- package/src/providers/workbuddy/chat.js +298 -298
- 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 +158 -158
- 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 +364 -364
- 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/runtime/server-lifecycle.js +34 -10
- 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 -149
- package/src/providers/AGENTS.md +0 -49
- package/src/routes/AGENTS.md +0 -37
|
@@ -1,148 +1,148 @@
|
|
|
1
|
-
import { appendEvent } from "../logs.js";
|
|
2
|
-
import { splitModelId, DEFAULT_PROVIDER, joinModelId } from "./model-id.js";
|
|
3
|
-
import { isModelAllowed as stateIsAllowed, loadProviderAllowedModels as stateLoadAllowed, loadProviderAllowAnyModels as stateLoadAllowAny } from "../state.js";
|
|
4
|
-
// 定制预热名单:registry customProviders + 默认 opencode;openrouter/自配 generic 不预热(按需自拉)
|
|
5
|
-
// agentrouter 已于 2026-10-21 随 registry 一并摘除(见 src/providers/registry.js 注释)。
|
|
6
|
-
const CUSTOM_PREHEAT_IDS = new Set(["opencode", "workbuddy", "traework", "cline", "codearts", "qoder", "globalqwenwork", "qwenwork", "zcode", "raccoon"]);
|
|
7
|
-
function isPreheatDisabled() {
|
|
8
|
-
const raw = process.env.MSLXDFF_PREHEAT;
|
|
9
|
-
if (raw === undefined || raw === null || raw === "") return false;
|
|
10
|
-
const s = String(raw).trim().toLowerCase();
|
|
11
|
-
return s === "0" || s === "off" || s === "false" || s === "no" || s === "disable" || s === "disabled";
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// 多供应商 dispatcher:把多个 Provider 聚合成一个 `upstream` 形状(chat/preheat/close),
|
|
15
|
-
// 按 body.model 的前缀路由到对应供应商,转发上游前剥掉前缀只发原始 id。
|
|
16
|
-
// 纯化:第二参可注入纯函数 isAllowed/getAllowed/getAllowAny,便于单测不读盘
|
|
17
|
-
export function createProviderDispatcher(providers = [], opts = {}) {
|
|
18
|
-
const byId = new Map(providers.map((p) => [p.id, p]));
|
|
19
|
-
// 纯化注入:第二参可为 {isAllowed, getAllowedModels, getAllowAny} 或直接函数 isAllowed
|
|
20
|
-
let isAllowedFn, getAllowedFn, getAllowAnyFn;
|
|
21
|
-
if (typeof opts === "function") {
|
|
22
|
-
isAllowedFn = opts;
|
|
23
|
-
getAllowedFn = stateLoadAllowed;
|
|
24
|
-
getAllowAnyFn = stateLoadAllowAny;
|
|
25
|
-
} else {
|
|
26
|
-
isAllowedFn = opts.isAllowed || stateIsAllowed;
|
|
27
|
-
getAllowedFn = opts.getAllowedModels || stateLoadAllowed;
|
|
28
|
-
getAllowAnyFn = opts.getAllowAny || stateLoadAllowAny;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function resolve(model) {
|
|
32
|
-
const split = splitModelId(model, providers.map((p) => p.id));
|
|
33
|
-
return {
|
|
34
|
-
provider: byId.get(split.provider) ?? byId.get(DEFAULT_PROVIDER) ?? providers[0],
|
|
35
|
-
raw: split.raw,
|
|
36
|
-
prefixed: split.prefixed,
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async function chat(body, opts = {}) {
|
|
41
|
-
let { provider, raw } = resolve(body?.model);
|
|
42
|
-
if (!provider) throw new Error(`no provider for model ${body?.model ?? "(empty)"}`);
|
|
43
|
-
let workbuddyUid = opts?.workbuddyUid ? String(opts.workbuddyUid).trim() : "";
|
|
44
|
-
// model 前缀钉死:workbuddy/<uid>:<rawId> -> 剥 uid,rawId 为真实模型
|
|
45
|
-
if (provider.id === "workbuddy" && typeof raw === "string" && raw.includes(":")) {
|
|
46
|
-
const idx = raw.indexOf(":");
|
|
47
|
-
const uidPart = raw.slice(0, idx).trim();
|
|
48
|
-
const modelPart = raw.slice(idx + 1).trim();
|
|
49
|
-
if (uidPart && modelPart) {
|
|
50
|
-
if (!workbuddyUid) workbuddyUid = uidPart;
|
|
51
|
-
raw = modelPart;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (!isAllowedFn(provider.id, raw)) {
|
|
55
|
-
const allowed = getAllowedFn(provider.id) || [];
|
|
56
|
-
const msg = `model not allowed for provider "${provider.id}": "${raw}" — allowed: ${allowed.join(", ") || "(none)"} (use: mslxdff -provider ${provider.id} allowlist add <model>)`;
|
|
57
|
-
appendEvent({ type: "provider-model-state", provider: provider.id, model: body?.model, rawModel: raw, state: "blocked", reason: "allowlist", status: 403 });
|
|
58
|
-
return new Response(JSON.stringify({ error: msg }), { status: 403, headers: { "Content-Type": "application/json", "x-mslxdff-allowlist": "1" } });
|
|
59
|
-
}
|
|
60
|
-
const forwarded = raw === body?.model ? body : { ...body, model: raw };
|
|
61
|
-
// ADR-0008:本请求携带瞬时共享 key(shareKeys 由组员侧按 header 解析后传入)。
|
|
62
|
-
const sharedKeys = opts?.shareKeys?.[provider.id];
|
|
63
|
-
const startedAt = Date.now();
|
|
64
|
-
let res;
|
|
65
|
-
try {
|
|
66
|
-
if (sharedKeys && sharedKeys.length && typeof provider.chatWithKeys === "function") {
|
|
67
|
-
res = await provider.chatWithKeys(forwarded, sharedKeys, opts);
|
|
68
|
-
} else if (provider.id === "workbuddy" && workbuddyUid) {
|
|
69
|
-
res = await provider.chat(forwarded, { ...opts, workbuddyUid });
|
|
70
|
-
} else {
|
|
71
|
-
res = await provider.chat(forwarded, opts);
|
|
72
|
-
}
|
|
73
|
-
} catch (err) {
|
|
74
|
-
appendEvent({ type: "provider-model-state", provider: provider.id, model: body?.model, rawModel: raw, state: "upstream-error", error: String(err?.message || err), durationMs: Date.now() - startedAt });
|
|
75
|
-
throw err;
|
|
76
|
-
}
|
|
77
|
-
appendEvent({
|
|
78
|
-
type: "provider-model-state",
|
|
79
|
-
provider: provider.id,
|
|
80
|
-
model: body?.model,
|
|
81
|
-
rawModel: raw,
|
|
82
|
-
status: res?.status,
|
|
83
|
-
state: res?.status === 429 ? "limited" : res?.ok ? "ok" : "upstream-error",
|
|
84
|
-
durationMs: Date.now() - startedAt,
|
|
85
|
-
});
|
|
86
|
-
return res;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// 聚合所有供应商的模型列表;默认供应商(opencode)裸 id,其它带前缀
|
|
90
|
-
// 若某供应商设置了 allowlist(非空),则仅暴露白名单内的模型(按 raw id 匹配)
|
|
91
|
-
async function listModels() {
|
|
92
|
-
const out = [];
|
|
93
|
-
const seen = new Set();
|
|
94
|
-
for (const p of providers) {
|
|
95
|
-
let list;
|
|
96
|
-
try {
|
|
97
|
-
list = (await p.listModels?.()) ?? [];
|
|
98
|
-
} catch {
|
|
99
|
-
list = [];
|
|
100
|
-
}
|
|
101
|
-
const allowed = getAllowedFn(p.id) || [];
|
|
102
|
-
const allowAny = getAllowAnyFn(p.id);
|
|
103
|
-
const allowedSet = allowed.length ? new Set(allowed) : null;
|
|
104
|
-
// 空名单且不允许任意模型 => 该供应商不暴露任何模型(安全默认)
|
|
105
|
-
if (!allowedSet && !allowAny) continue;
|
|
106
|
-
for (const m of list) {
|
|
107
|
-
if (!m || !m.id) continue;
|
|
108
|
-
if (seen.has(m.id)) continue;
|
|
109
|
-
if (allowedSet) {
|
|
110
|
-
// m.id 已是带前缀的对外 id,需剥回 raw 再比对
|
|
111
|
-
const { raw } = splitModelId(m.id, providers.map((x) => x.id));
|
|
112
|
-
const rawNorm = String(raw || "").trim();
|
|
113
|
-
if (!allowedSet.has(rawNorm)) continue;
|
|
114
|
-
// 对于 opencode 裸 id,rawNorm 即 m.id 本身
|
|
115
|
-
}
|
|
116
|
-
seen.add(m.id);
|
|
117
|
-
out.push(m);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
return out;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// 预热定制供应商 + 默认供应商(opencode):registry 名单 8 家 + opencode 共 9 家并发各打一次;
|
|
124
|
-
// openrouter/用户自配 generic 不在此列(按需首次请求自拉)。单家失败静默,不拖累启动。
|
|
125
|
-
async function preheat() {
|
|
126
|
-
if (isPreheatDisabled()) return { ok: true, skipped: true };
|
|
127
|
-
const targets = providers.filter((p) => p && typeof p.preheat === "function" && CUSTOM_PREHEAT_IDS.has(p.id));
|
|
128
|
-
if (!targets.length) return { ok: false, skipped: true };
|
|
129
|
-
const settled = await Promise.all(targets.map(async (p) => {
|
|
130
|
-
try {
|
|
131
|
-
const r = await p.preheat();
|
|
132
|
-
return { id: p.id, ...(r && typeof r === "object" ? r : { ok: false }) };
|
|
133
|
-
} catch {
|
|
134
|
-
return { id: p.id, ok: false, error: "preheat failed" };
|
|
135
|
-
}
|
|
136
|
-
}));
|
|
137
|
-
const okCount = settled.filter((r) => r && r.ok).length;
|
|
138
|
-
return { ok: okCount > 0, results: settled, okCount, total: settled.length };
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async function close() {
|
|
142
|
-
for (const p of providers) {
|
|
143
|
-
try { await p.close?.(); } catch {}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return { providers, byId, resolve, listModels, chat, preheat, close, joinModelId };
|
|
1
|
+
import { appendEvent } from "../logs.js";
|
|
2
|
+
import { splitModelId, DEFAULT_PROVIDER, joinModelId } from "./model-id.js";
|
|
3
|
+
import { isModelAllowed as stateIsAllowed, loadProviderAllowedModels as stateLoadAllowed, loadProviderAllowAnyModels as stateLoadAllowAny } from "../state.js";
|
|
4
|
+
// 定制预热名单:registry customProviders + 默认 opencode;openrouter/自配 generic 不预热(按需自拉)
|
|
5
|
+
// agentrouter 已于 2026-10-21 随 registry 一并摘除(见 src/providers/registry.js 注释)。
|
|
6
|
+
const CUSTOM_PREHEAT_IDS = new Set(["opencode", "workbuddy", "traework", "cline", "codearts", "qoder", "globalqwenwork", "qwenwork", "zcode", "raccoon"]);
|
|
7
|
+
function isPreheatDisabled() {
|
|
8
|
+
const raw = process.env.MSLXDFF_PREHEAT;
|
|
9
|
+
if (raw === undefined || raw === null || raw === "") return false;
|
|
10
|
+
const s = String(raw).trim().toLowerCase();
|
|
11
|
+
return s === "0" || s === "off" || s === "false" || s === "no" || s === "disable" || s === "disabled";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// 多供应商 dispatcher:把多个 Provider 聚合成一个 `upstream` 形状(chat/preheat/close),
|
|
15
|
+
// 按 body.model 的前缀路由到对应供应商,转发上游前剥掉前缀只发原始 id。
|
|
16
|
+
// 纯化:第二参可注入纯函数 isAllowed/getAllowed/getAllowAny,便于单测不读盘
|
|
17
|
+
export function createProviderDispatcher(providers = [], opts = {}) {
|
|
18
|
+
const byId = new Map(providers.map((p) => [p.id, p]));
|
|
19
|
+
// 纯化注入:第二参可为 {isAllowed, getAllowedModels, getAllowAny} 或直接函数 isAllowed
|
|
20
|
+
let isAllowedFn, getAllowedFn, getAllowAnyFn;
|
|
21
|
+
if (typeof opts === "function") {
|
|
22
|
+
isAllowedFn = opts;
|
|
23
|
+
getAllowedFn = stateLoadAllowed;
|
|
24
|
+
getAllowAnyFn = stateLoadAllowAny;
|
|
25
|
+
} else {
|
|
26
|
+
isAllowedFn = opts.isAllowed || stateIsAllowed;
|
|
27
|
+
getAllowedFn = opts.getAllowedModels || stateLoadAllowed;
|
|
28
|
+
getAllowAnyFn = opts.getAllowAny || stateLoadAllowAny;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function resolve(model) {
|
|
32
|
+
const split = splitModelId(model, providers.map((p) => p.id));
|
|
33
|
+
return {
|
|
34
|
+
provider: byId.get(split.provider) ?? byId.get(DEFAULT_PROVIDER) ?? providers[0],
|
|
35
|
+
raw: split.raw,
|
|
36
|
+
prefixed: split.prefixed,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function chat(body, opts = {}) {
|
|
41
|
+
let { provider, raw } = resolve(body?.model);
|
|
42
|
+
if (!provider) throw new Error(`no provider for model ${body?.model ?? "(empty)"}`);
|
|
43
|
+
let workbuddyUid = opts?.workbuddyUid ? String(opts.workbuddyUid).trim() : "";
|
|
44
|
+
// model 前缀钉死:workbuddy/<uid>:<rawId> -> 剥 uid,rawId 为真实模型
|
|
45
|
+
if (provider.id === "workbuddy" && typeof raw === "string" && raw.includes(":")) {
|
|
46
|
+
const idx = raw.indexOf(":");
|
|
47
|
+
const uidPart = raw.slice(0, idx).trim();
|
|
48
|
+
const modelPart = raw.slice(idx + 1).trim();
|
|
49
|
+
if (uidPart && modelPart) {
|
|
50
|
+
if (!workbuddyUid) workbuddyUid = uidPart;
|
|
51
|
+
raw = modelPart;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (!isAllowedFn(provider.id, raw)) {
|
|
55
|
+
const allowed = getAllowedFn(provider.id) || [];
|
|
56
|
+
const msg = `model not allowed for provider "${provider.id}": "${raw}" — allowed: ${allowed.join(", ") || "(none)"} (use: mslxdff -provider ${provider.id} allowlist add <model>)`;
|
|
57
|
+
appendEvent({ type: "provider-model-state", provider: provider.id, model: body?.model, rawModel: raw, state: "blocked", reason: "allowlist", status: 403 });
|
|
58
|
+
return new Response(JSON.stringify({ error: msg }), { status: 403, headers: { "Content-Type": "application/json", "x-mslxdff-allowlist": "1" } });
|
|
59
|
+
}
|
|
60
|
+
const forwarded = raw === body?.model ? body : { ...body, model: raw };
|
|
61
|
+
// ADR-0008:本请求携带瞬时共享 key(shareKeys 由组员侧按 header 解析后传入)。
|
|
62
|
+
const sharedKeys = opts?.shareKeys?.[provider.id];
|
|
63
|
+
const startedAt = Date.now();
|
|
64
|
+
let res;
|
|
65
|
+
try {
|
|
66
|
+
if (sharedKeys && sharedKeys.length && typeof provider.chatWithKeys === "function") {
|
|
67
|
+
res = await provider.chatWithKeys(forwarded, sharedKeys, opts);
|
|
68
|
+
} else if (provider.id === "workbuddy" && workbuddyUid) {
|
|
69
|
+
res = await provider.chat(forwarded, { ...opts, workbuddyUid });
|
|
70
|
+
} else {
|
|
71
|
+
res = await provider.chat(forwarded, opts);
|
|
72
|
+
}
|
|
73
|
+
} catch (err) {
|
|
74
|
+
appendEvent({ type: "provider-model-state", provider: provider.id, model: body?.model, rawModel: raw, state: "upstream-error", error: String(err?.message || err), durationMs: Date.now() - startedAt });
|
|
75
|
+
throw err;
|
|
76
|
+
}
|
|
77
|
+
appendEvent({
|
|
78
|
+
type: "provider-model-state",
|
|
79
|
+
provider: provider.id,
|
|
80
|
+
model: body?.model,
|
|
81
|
+
rawModel: raw,
|
|
82
|
+
status: res?.status,
|
|
83
|
+
state: res?.status === 429 ? "limited" : res?.ok ? "ok" : "upstream-error",
|
|
84
|
+
durationMs: Date.now() - startedAt,
|
|
85
|
+
});
|
|
86
|
+
return res;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 聚合所有供应商的模型列表;默认供应商(opencode)裸 id,其它带前缀
|
|
90
|
+
// 若某供应商设置了 allowlist(非空),则仅暴露白名单内的模型(按 raw id 匹配)
|
|
91
|
+
async function listModels() {
|
|
92
|
+
const out = [];
|
|
93
|
+
const seen = new Set();
|
|
94
|
+
for (const p of providers) {
|
|
95
|
+
let list;
|
|
96
|
+
try {
|
|
97
|
+
list = (await p.listModels?.()) ?? [];
|
|
98
|
+
} catch {
|
|
99
|
+
list = [];
|
|
100
|
+
}
|
|
101
|
+
const allowed = getAllowedFn(p.id) || [];
|
|
102
|
+
const allowAny = getAllowAnyFn(p.id);
|
|
103
|
+
const allowedSet = allowed.length ? new Set(allowed) : null;
|
|
104
|
+
// 空名单且不允许任意模型 => 该供应商不暴露任何模型(安全默认)
|
|
105
|
+
if (!allowedSet && !allowAny) continue;
|
|
106
|
+
for (const m of list) {
|
|
107
|
+
if (!m || !m.id) continue;
|
|
108
|
+
if (seen.has(m.id)) continue;
|
|
109
|
+
if (allowedSet) {
|
|
110
|
+
// m.id 已是带前缀的对外 id,需剥回 raw 再比对
|
|
111
|
+
const { raw } = splitModelId(m.id, providers.map((x) => x.id));
|
|
112
|
+
const rawNorm = String(raw || "").trim();
|
|
113
|
+
if (!allowedSet.has(rawNorm)) continue;
|
|
114
|
+
// 对于 opencode 裸 id,rawNorm 即 m.id 本身
|
|
115
|
+
}
|
|
116
|
+
seen.add(m.id);
|
|
117
|
+
out.push(m);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 预热定制供应商 + 默认供应商(opencode):registry 名单 8 家 + opencode 共 9 家并发各打一次;
|
|
124
|
+
// openrouter/用户自配 generic 不在此列(按需首次请求自拉)。单家失败静默,不拖累启动。
|
|
125
|
+
async function preheat() {
|
|
126
|
+
if (isPreheatDisabled()) return { ok: true, skipped: true };
|
|
127
|
+
const targets = providers.filter((p) => p && typeof p.preheat === "function" && CUSTOM_PREHEAT_IDS.has(p.id));
|
|
128
|
+
if (!targets.length) return { ok: false, skipped: true };
|
|
129
|
+
const settled = await Promise.all(targets.map(async (p) => {
|
|
130
|
+
try {
|
|
131
|
+
const r = await p.preheat();
|
|
132
|
+
return { id: p.id, ...(r && typeof r === "object" ? r : { ok: false }) };
|
|
133
|
+
} catch {
|
|
134
|
+
return { id: p.id, ok: false, error: "preheat failed" };
|
|
135
|
+
}
|
|
136
|
+
}));
|
|
137
|
+
const okCount = settled.filter((r) => r && r.ok).length;
|
|
138
|
+
return { ok: okCount > 0, results: settled, okCount, total: settled.length };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function close() {
|
|
142
|
+
for (const p of providers) {
|
|
143
|
+
try { await p.close?.(); } catch {}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return { providers, byId, resolve, listModels, chat, preheat, close, joinModelId };
|
|
148
148
|
}
|
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
// cosy.js(COSY 会话 + 签名头)— vendor 自 src/providers/qwenwork/cosy.js 逐字搬;
|
|
2
|
-
// 本目录 import 全为相对路径,自动绑定本目录 constants/crypto/rsa。线上头 x-qwenwork-* 与 cn 同名,**禁止改名**(实测 .ai 接受)。
|
|
3
|
-
// 关键不变式(改动即 403):
|
|
4
|
-
// · identity/payload 都必须 key 升序紧凑 JSON(sortedCompact)
|
|
5
|
-
// · 签名 = md5(payloadB64 \n cosyKey \n date \n body \n pathSig),pathSig 去 /algo 前缀、不含 query
|
|
6
|
-
// · authorization = `Bearer COSY.${payloadB64}.${sig}`;payloadB64 是**标准 base64(带 padding)**
|
|
7
|
-
// · 同一 requestId 重放会被上游判 `code 103 Duplicate request`
|
|
8
|
-
import { uuid, b64, b64url, utf8, aesCbcEncrypt, md5 } from "./crypto.js";
|
|
9
|
-
import { rsaPkcs1Encrypt } from "./rsa.js";
|
|
10
|
-
import {
|
|
11
|
-
COSY_VERSION, IDE_VERSION, RELEASE_VERSION, BUILD, CLIENT_TYPE,
|
|
12
|
-
BUSINESS_PRODUCT, BUSINESS_TYPE, SCENE, MACHINE_OS, UA,
|
|
13
|
-
} from "./constants.js";
|
|
14
|
-
|
|
15
|
-
const cache = new Map();
|
|
16
|
-
|
|
17
|
-
function sortedCompact(obj) {
|
|
18
|
-
const keys = Object.keys(obj).sort();
|
|
19
|
-
let out = "{";
|
|
20
|
-
for (let i = 0; i < keys.length; i++) {
|
|
21
|
-
if (i > 0) out += ",";
|
|
22
|
-
out += JSON.stringify(keys[i]) + ":" + JSON.stringify(obj[keys[i]]);
|
|
23
|
-
}
|
|
24
|
-
return out + "}";
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function newSession(account) {
|
|
28
|
-
const machineID = uuid();
|
|
29
|
-
const seed = (uuid() + uuid()).slice(0, 50);
|
|
30
|
-
const machineToken = b64url(utf8(seed));
|
|
31
|
-
const machineType = uuid().replace(/-/g, "").slice(0, 18);
|
|
32
|
-
const tempKey = uuid().replace(/-/g, "").slice(0, 16);
|
|
33
|
-
|
|
34
|
-
const identity = sortedCompact({
|
|
35
|
-
uid: account.uid || "",
|
|
36
|
-
aid: account.uid || "",
|
|
37
|
-
name: account.nickname || "",
|
|
38
|
-
email: account.email || "",
|
|
39
|
-
security_oauth_token: account.accessToken,
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
machineID,
|
|
44
|
-
machineToken,
|
|
45
|
-
machineType,
|
|
46
|
-
tempKey,
|
|
47
|
-
cosyKey: rsaPkcs1Encrypt(utf8(tempKey)),
|
|
48
|
-
info: b64(aesCbcEncrypt(utf8(identity), utf8(tempKey))),
|
|
49
|
-
accessToken: account.accessToken,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export function sessionFor(account) {
|
|
54
|
-
if (!account || !account.accessToken) throw new Error("cosy: empty access token");
|
|
55
|
-
const key = account.id || account.uid || account.accessToken.slice(0, 16);
|
|
56
|
-
const hit = cache.get(key);
|
|
57
|
-
if (hit && hit.accessToken === account.accessToken) return hit;
|
|
58
|
-
if (cache.size > 128) cache.clear();
|
|
59
|
-
const sess = newSession(account);
|
|
60
|
-
cache.set(key, sess);
|
|
61
|
-
return sess;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function invalidateSession(account) {
|
|
65
|
-
const key = account && (account.id || account.uid);
|
|
66
|
-
if (key) cache.delete(key);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function pathSig(rawURL) {
|
|
70
|
-
const u = new URL(rawURL);
|
|
71
|
-
let p = u.pathname;
|
|
72
|
-
if (p.startsWith("/algo")) p = p.slice("/algo".length);
|
|
73
|
-
return p;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
export function signHeaders(account, body, rawURL, modelKey, accept) {
|
|
77
|
-
const sess = sessionFor(account);
|
|
78
|
-
const payload = sortedCompact({
|
|
79
|
-
cosyVersion: COSY_VERSION,
|
|
80
|
-
ideVersion: IDE_VERSION,
|
|
81
|
-
info: sess.info,
|
|
82
|
-
requestId: uuid(),
|
|
83
|
-
version: "v1",
|
|
84
|
-
});
|
|
85
|
-
const payloadB64 = b64(utf8(payload));
|
|
86
|
-
const date = Math.floor(Date.now() / 1000).toString();
|
|
87
|
-
const sig = md5(`${payloadB64}\n${sess.cosyKey}\n${date}\n${body}\n${pathSig(rawURL)}`);
|
|
88
|
-
|
|
89
|
-
const headers = {
|
|
90
|
-
accept: accept || "text/event-stream",
|
|
91
|
-
"content-type": "application/json",
|
|
92
|
-
"user-agent": UA,
|
|
93
|
-
"x-request-id": uuid(),
|
|
94
|
-
"x-qwenwork-version": IDE_VERSION,
|
|
95
|
-
"x-qwenwork-release-version": RELEASE_VERSION,
|
|
96
|
-
"x-qwenwork-build": BUILD,
|
|
97
|
-
"x-qwenwork-platform": "win32",
|
|
98
|
-
"x-qwenwork-arch": "x64",
|
|
99
|
-
"x-qwenwork-channel": "stable",
|
|
100
|
-
"cosy-version": COSY_VERSION,
|
|
101
|
-
"cosy-clienttype": CLIENT_TYPE,
|
|
102
|
-
"cosy-business-product": BUSINESS_PRODUCT,
|
|
103
|
-
"cosy-business-type": BUSINESS_TYPE,
|
|
104
|
-
"cosy-scene": SCENE,
|
|
105
|
-
"cosy-machineos": MACHINE_OS,
|
|
106
|
-
"login-version": "v2",
|
|
107
|
-
authorization: `Bearer COSY.${payloadB64}.${sig}`,
|
|
108
|
-
"cosy-key": sess.cosyKey,
|
|
109
|
-
"cosy-user": account.uid || "",
|
|
110
|
-
"cosy-date": date,
|
|
111
|
-
"cosy-machineid": sess.machineID,
|
|
112
|
-
"accept-encoding": "identity",
|
|
113
|
-
connection: "keep-alive",
|
|
114
|
-
"cache-control": "no-cache",
|
|
115
|
-
};
|
|
116
|
-
if (modelKey) {
|
|
117
|
-
headers["x-model-key"] = modelKey;
|
|
118
|
-
headers["x-model-source"] = "system";
|
|
119
|
-
}
|
|
120
|
-
return headers;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// globalqwenwork 变体(国际站 gateway.qwenwork.ai):协议与 cn 逐字同构,2026-10-01 现网实测签名/请求 200 — 见 docs/adr/0041-globalqwenwork-international-provider.md
|
|
1
|
+
// cosy.js(COSY 会话 + 签名头)— vendor 自 src/providers/qwenwork/cosy.js 逐字搬;
|
|
2
|
+
// 本目录 import 全为相对路径,自动绑定本目录 constants/crypto/rsa。线上头 x-qwenwork-* 与 cn 同名,**禁止改名**(实测 .ai 接受)。
|
|
3
|
+
// 关键不变式(改动即 403):
|
|
4
|
+
// · identity/payload 都必须 key 升序紧凑 JSON(sortedCompact)
|
|
5
|
+
// · 签名 = md5(payloadB64 \n cosyKey \n date \n body \n pathSig),pathSig 去 /algo 前缀、不含 query
|
|
6
|
+
// · authorization = `Bearer COSY.${payloadB64}.${sig}`;payloadB64 是**标准 base64(带 padding)**
|
|
7
|
+
// · 同一 requestId 重放会被上游判 `code 103 Duplicate request`
|
|
8
|
+
import { uuid, b64, b64url, utf8, aesCbcEncrypt, md5 } from "./crypto.js";
|
|
9
|
+
import { rsaPkcs1Encrypt } from "./rsa.js";
|
|
10
|
+
import {
|
|
11
|
+
COSY_VERSION, IDE_VERSION, RELEASE_VERSION, BUILD, CLIENT_TYPE,
|
|
12
|
+
BUSINESS_PRODUCT, BUSINESS_TYPE, SCENE, MACHINE_OS, UA,
|
|
13
|
+
} from "./constants.js";
|
|
14
|
+
|
|
15
|
+
const cache = new Map();
|
|
16
|
+
|
|
17
|
+
function sortedCompact(obj) {
|
|
18
|
+
const keys = Object.keys(obj).sort();
|
|
19
|
+
let out = "{";
|
|
20
|
+
for (let i = 0; i < keys.length; i++) {
|
|
21
|
+
if (i > 0) out += ",";
|
|
22
|
+
out += JSON.stringify(keys[i]) + ":" + JSON.stringify(obj[keys[i]]);
|
|
23
|
+
}
|
|
24
|
+
return out + "}";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function newSession(account) {
|
|
28
|
+
const machineID = uuid();
|
|
29
|
+
const seed = (uuid() + uuid()).slice(0, 50);
|
|
30
|
+
const machineToken = b64url(utf8(seed));
|
|
31
|
+
const machineType = uuid().replace(/-/g, "").slice(0, 18);
|
|
32
|
+
const tempKey = uuid().replace(/-/g, "").slice(0, 16);
|
|
33
|
+
|
|
34
|
+
const identity = sortedCompact({
|
|
35
|
+
uid: account.uid || "",
|
|
36
|
+
aid: account.uid || "",
|
|
37
|
+
name: account.nickname || "",
|
|
38
|
+
email: account.email || "",
|
|
39
|
+
security_oauth_token: account.accessToken,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return {
|
|
43
|
+
machineID,
|
|
44
|
+
machineToken,
|
|
45
|
+
machineType,
|
|
46
|
+
tempKey,
|
|
47
|
+
cosyKey: rsaPkcs1Encrypt(utf8(tempKey)),
|
|
48
|
+
info: b64(aesCbcEncrypt(utf8(identity), utf8(tempKey))),
|
|
49
|
+
accessToken: account.accessToken,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function sessionFor(account) {
|
|
54
|
+
if (!account || !account.accessToken) throw new Error("cosy: empty access token");
|
|
55
|
+
const key = account.id || account.uid || account.accessToken.slice(0, 16);
|
|
56
|
+
const hit = cache.get(key);
|
|
57
|
+
if (hit && hit.accessToken === account.accessToken) return hit;
|
|
58
|
+
if (cache.size > 128) cache.clear();
|
|
59
|
+
const sess = newSession(account);
|
|
60
|
+
cache.set(key, sess);
|
|
61
|
+
return sess;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function invalidateSession(account) {
|
|
65
|
+
const key = account && (account.id || account.uid);
|
|
66
|
+
if (key) cache.delete(key);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function pathSig(rawURL) {
|
|
70
|
+
const u = new URL(rawURL);
|
|
71
|
+
let p = u.pathname;
|
|
72
|
+
if (p.startsWith("/algo")) p = p.slice("/algo".length);
|
|
73
|
+
return p;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function signHeaders(account, body, rawURL, modelKey, accept) {
|
|
77
|
+
const sess = sessionFor(account);
|
|
78
|
+
const payload = sortedCompact({
|
|
79
|
+
cosyVersion: COSY_VERSION,
|
|
80
|
+
ideVersion: IDE_VERSION,
|
|
81
|
+
info: sess.info,
|
|
82
|
+
requestId: uuid(),
|
|
83
|
+
version: "v1",
|
|
84
|
+
});
|
|
85
|
+
const payloadB64 = b64(utf8(payload));
|
|
86
|
+
const date = Math.floor(Date.now() / 1000).toString();
|
|
87
|
+
const sig = md5(`${payloadB64}\n${sess.cosyKey}\n${date}\n${body}\n${pathSig(rawURL)}`);
|
|
88
|
+
|
|
89
|
+
const headers = {
|
|
90
|
+
accept: accept || "text/event-stream",
|
|
91
|
+
"content-type": "application/json",
|
|
92
|
+
"user-agent": UA,
|
|
93
|
+
"x-request-id": uuid(),
|
|
94
|
+
"x-qwenwork-version": IDE_VERSION,
|
|
95
|
+
"x-qwenwork-release-version": RELEASE_VERSION,
|
|
96
|
+
"x-qwenwork-build": BUILD,
|
|
97
|
+
"x-qwenwork-platform": "win32",
|
|
98
|
+
"x-qwenwork-arch": "x64",
|
|
99
|
+
"x-qwenwork-channel": "stable",
|
|
100
|
+
"cosy-version": COSY_VERSION,
|
|
101
|
+
"cosy-clienttype": CLIENT_TYPE,
|
|
102
|
+
"cosy-business-product": BUSINESS_PRODUCT,
|
|
103
|
+
"cosy-business-type": BUSINESS_TYPE,
|
|
104
|
+
"cosy-scene": SCENE,
|
|
105
|
+
"cosy-machineos": MACHINE_OS,
|
|
106
|
+
"login-version": "v2",
|
|
107
|
+
authorization: `Bearer COSY.${payloadB64}.${sig}`,
|
|
108
|
+
"cosy-key": sess.cosyKey,
|
|
109
|
+
"cosy-user": account.uid || "",
|
|
110
|
+
"cosy-date": date,
|
|
111
|
+
"cosy-machineid": sess.machineID,
|
|
112
|
+
"accept-encoding": "identity",
|
|
113
|
+
connection: "keep-alive",
|
|
114
|
+
"cache-control": "no-cache",
|
|
115
|
+
};
|
|
116
|
+
if (modelKey) {
|
|
117
|
+
headers["x-model-key"] = modelKey;
|
|
118
|
+
headers["x-model-source"] = "system";
|
|
119
|
+
}
|
|
120
|
+
return headers;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// globalqwenwork 变体(国际站 gateway.qwenwork.ai):协议与 cn 逐字同构,2026-10-01 现网实测签名/请求 200 — 见 docs/adr/0041-globalqwenwork-international-provider.md
|