mslxdff 0.1.236 → 0.1.238
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/keys-guard.js +20 -0
- 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/codearts/const.js +12 -5
- package/src/providers/codearts/models.js +23 -5
- 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,207 +1,207 @@
|
|
|
1
|
-
// CodeArts 聊天服务:上游 body/头组装 + SDK-HMAC-SHA256 签名 + 401 刷新重试 + 环形切号。
|
|
2
|
-
// 上游恒 stream:true(chatBodyV2 契约);流式客户端转 OpenAI SSE,非流式聚合回 JSON。
|
|
3
|
-
import crypto from "node:crypto";
|
|
4
|
-
import { joinUrl } from "../base.js";
|
|
5
|
-
import { createTransport } from "../../transport/index.js";
|
|
6
|
-
import { SNAP_BASE, EP_CHAT_V2, chatBaseHeaders, MAAS_TYPE_HEADER, MAAS_BENEFIT, MAX_TOKENS_CEILING } from "./const.js";
|
|
7
|
-
import { signRequest } from "./sign.js";
|
|
8
|
-
import { newChatId, preflightResponse, sseToOpenAIResponse, aggregateToCompletion, UpstreamEventError } from "./stream.js";
|
|
9
|
-
|
|
10
|
-
const MODEL_RULES = [
|
|
11
|
-
{ match: (m) => String(m || "").toLowerCase().includes("deepseek"), scope: "all" },
|
|
12
|
-
{ match: (m) => /(^kimi-|kimi)/i.test(String(m || "")), scope: "toolCalls" },
|
|
13
|
-
];
|
|
14
|
-
|
|
15
|
-
// DeepSeek 系思考模式要求 assistant 消息回传 reasoning_content,缺失补 " " 占位(ADR-0001 同款)。
|
|
16
|
-
export function injectReasoningPlaceholder(messages, model) {
|
|
17
|
-
const rule = MODEL_RULES.find((r) => r.match(model));
|
|
18
|
-
if (!rule || !Array.isArray(messages)) return messages;
|
|
19
|
-
return messages.map((m) => {
|
|
20
|
-
if (m?.role !== "assistant") return m;
|
|
21
|
-
if (typeof m.reasoning_content === "string" && m.reasoning_content.length) return m;
|
|
22
|
-
if (rule.scope === "toolCalls" && !(Array.isArray(m.tool_calls) && m.tool_calls.length)) return m;
|
|
23
|
-
return { ...m, reasoning_content: " " };
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const PASSTHROUGH_KEYS = ["temperature", "top_p", "max_tokens", "max_completion_tokens", "reasoning_effort", "tools", "tool_choice", "stop", "presence_penalty", "frequency_penalty", "response_format", "user", "n", "seed"];
|
|
28
|
-
|
|
29
|
-
// 账号级「会话/并发饱和」:上游按账号限同时活跃会话数(TM.00001041 = 并发会话数已达上限(3个))。
|
|
30
|
-
// 这是账号此刻进不去,不是本请求参数错;槽位在上游保留分钟级才回收(与本地进程无关)。
|
|
31
|
-
// 多号场景换另一个账号通常立即成功——故短冷却本账号让下一次 next() 换号并 continue 重试。
|
|
32
|
-
const SESSION_SATURATED_RE = /TM\.00001041|并发会话数已达上限/;
|
|
33
|
-
const SESSION_SATURATED_COOLDOWN_MS = Number.parseInt(process.env.MSLXDFF_CODEARTS_SESSION_COOLDOWN_MS ?? "", 10) || 30_000;
|
|
34
|
-
|
|
35
|
-
export function buildUpstreamBody(body, { chatId, upstreamModel }) {
|
|
36
|
-
const out = {
|
|
37
|
-
model: upstreamModel,
|
|
38
|
-
stream: true, // v2 端点契约:恒流式,非流式由本层聚合
|
|
39
|
-
messages: injectReasoningPlaceholder(body?.messages || [], upstreamModel),
|
|
40
|
-
prompt_cache_key: chatId,
|
|
41
|
-
tool_stream: true,
|
|
42
|
-
chat_id: chatId,
|
|
43
|
-
};
|
|
44
|
-
for (const k of PASSTHROUGH_KEYS) {
|
|
45
|
-
if (k === "max_completion_tokens") continue; // v2 端点不认这个名字(透传 → InferHub.001001005.400,2026-10-16 现网 A/B 复现)
|
|
46
|
-
if (body?.[k] !== undefined && body?.[k] !== null) out[k] = body[k];
|
|
47
|
-
}
|
|
48
|
-
// Responses 风格客户端只发 max_completion_tokens:映射成 v2 认的 max_tokens(显式 max_tokens 优先)
|
|
49
|
-
if (out.max_tokens === undefined && body?.max_completion_tokens != null) out.max_tokens = body.max_completion_tokens;
|
|
50
|
-
// 超上限的 max_tokens 上游一律回 InferHub.001001005.400(参数非法),在这里夹住(env 可放宽)
|
|
51
|
-
const ceiling = Number.parseInt(process.env.MSLXDFF_CODEARTS_MAX_TOKENS_CEILING ?? "", 10) || MAX_TOKENS_CEILING;
|
|
52
|
-
if (typeof out.max_tokens === "number" && out.max_tokens > ceiling) out.max_tokens = ceiling;
|
|
53
|
-
return out;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// Session-Id:由 chatId 派生的稳定会话标识(prompt cache 亲和,对齐 Go sessionIDFor)。
|
|
57
|
-
function sessionIdFor(chatId) {
|
|
58
|
-
return crypto.createHash("sha256").update(`codearts-session:${chatId}`, "utf8").digest("hex").slice(0, 32);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function stripOwnPrefix(model, providerId) {
|
|
62
|
-
const s = String(model || "");
|
|
63
|
-
const i = s.indexOf("/");
|
|
64
|
-
if (i <= 0) return s;
|
|
65
|
-
return s.slice(0, i).toLowerCase() === String(providerId || "").toLowerCase() ? s.slice(i + 1) : s;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// 从 4xx/5xx 正文里判「账号级会话饱和」:命中则可换号重试(不同于参数类 400,换号无意义)。
|
|
69
|
-
export function isSessionSaturated(status, bodyText) {
|
|
70
|
-
if (status !== 400 && status !== 429 && status < 500) return false;
|
|
71
|
-
return SESSION_SATURATED_RE.test(String(bodyText || ""));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
export function createChatService({
|
|
75
|
-
id = "codearts",
|
|
76
|
-
baseUrl = SNAP_BASE,
|
|
77
|
-
chatPath = EP_CHAT_V2,
|
|
78
|
-
fetchImpl,
|
|
79
|
-
dispatcher,
|
|
80
|
-
authPool,
|
|
81
|
-
catalog,
|
|
82
|
-
connectTimeoutMs = 30_000,
|
|
83
|
-
clock = Date.now,
|
|
84
|
-
} = {}) {
|
|
85
|
-
const resolvedBase = String(baseUrl).trim().replace(/\/+$/, "") || SNAP_BASE;
|
|
86
|
-
const transport = createTransport({ fetchImpl, dispatcher, keepAlive: !!dispatcher, timeoutMs: connectTimeoutMs, retry: {} });
|
|
87
|
-
|
|
88
|
-
async function sendOnce({ bodyStr, account, benefit, chatId, sessionId, traceId }) {
|
|
89
|
-
const url = joinUrl(resolvedBase, chatPath);
|
|
90
|
-
const h = chatBaseHeaders(account.securityToken, traceId);
|
|
91
|
-
if (benefit) h[MAAS_TYPE_HEADER] = MAAS_BENEFIT;
|
|
92
|
-
const { headers } = signRequest({ method: "POST", url, headers: h, body: bodyStr, cred: account });
|
|
93
|
-
// Chat-Id / Session-Id 在签名之后追加(对齐官方客户端:不进 SignedHeaders)。
|
|
94
|
-
headers["chat-id"] = chatId;
|
|
95
|
-
headers["session-id"] = sessionId;
|
|
96
|
-
return transport.request({ url, method: "POST", headers, body: bodyStr, stream: true, timeoutMs: connectTimeoutMs });
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function errRes(status, msg, code) {
|
|
100
|
-
return new Response(JSON.stringify({ error: { message: msg, type: "upstream_error", ...(code ? { code } : {}) } }), {
|
|
101
|
-
status,
|
|
102
|
-
headers: { "Content-Type": "application/json" },
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function withTiming(res, t0) {
|
|
107
|
-
try { res._t = { attempts: [], waitMs: 0, totalMs: Math.round(clock() - t0) }; } catch {}
|
|
108
|
-
return res;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* 跑一次对话。body.model 已由 dispatcher 剥前缀(容忍自带前缀)。
|
|
113
|
-
* @returns {Promise<Response>} 流式 → OpenAI SSE;非流式 → chat.completion JSON
|
|
114
|
-
*/
|
|
115
|
-
async function runChat(body, activeRing, opts = {}) {
|
|
116
|
-
const t0 = clock();
|
|
117
|
-
const requestedModel = String(body?.model || "");
|
|
118
|
-
const rawModel = stripOwnPrefix(requestedModel, id);
|
|
119
|
-
const upstreamModel = catalog?.canonical(rawModel) || rawModel;
|
|
120
|
-
const chatId = newChatId(body, opts);
|
|
121
|
-
const sessionId = sessionIdFor(chatId);
|
|
122
|
-
const upstreamBody = buildUpstreamBody(body, { chatId, upstreamModel });
|
|
123
|
-
const bodyStr = JSON.stringify(upstreamBody);
|
|
124
|
-
const isStream = body?.stream === true;
|
|
125
|
-
let lastErr = null;
|
|
126
|
-
|
|
127
|
-
const maxTries = Math.max(1, activeRing.size || 1);
|
|
128
|
-
for (let attempt = 0; attempt < maxTries; attempt++) {
|
|
129
|
-
const blob = activeRing.next();
|
|
130
|
-
if (!blob) break;
|
|
131
|
-
let account;
|
|
132
|
-
try {
|
|
133
|
-
account = await authPool.getCredential(blob);
|
|
134
|
-
} catch (err) {
|
|
135
|
-
try { activeRing.onError(blob); } catch {}
|
|
136
|
-
lastErr = err;
|
|
137
|
-
continue; // 死号/不在池:换下一个账号
|
|
138
|
-
}
|
|
139
|
-
const benefit = catalog?.isBenefit ? catalog.isBenefit(account.userId, upstreamModel) : false;
|
|
140
|
-
const traceId = crypto.randomBytes(8).toString("hex") + String(clock());
|
|
141
|
-
let res;
|
|
142
|
-
try {
|
|
143
|
-
res = await sendOnce({ bodyStr, account, benefit, chatId, sessionId, traceId });
|
|
144
|
-
} catch (err) {
|
|
145
|
-
try { activeRing.onError(blob); } catch {}
|
|
146
|
-
lastErr = err;
|
|
147
|
-
continue;
|
|
148
|
-
}
|
|
149
|
-
if (res.status === 401 || res.status === 403) {
|
|
150
|
-
// STS 临时凭证失效:强制刷新重试一次,仍 401/403 换号
|
|
151
|
-
authPool.invalidate(blob);
|
|
152
|
-
let account2 = null;
|
|
153
|
-
try { account2 = await authPool.getCredential(blob); } catch { /* 死号 → 换号 */ }
|
|
154
|
-
if (account2) {
|
|
155
|
-
const traceId2 = crypto.randomBytes(8).toString("hex") + String(clock());
|
|
156
|
-
try { res = await sendOnce({ bodyStr, account: account2, benefit, chatId, sessionId, traceId: traceId2 }); } catch (err2) { lastErr = err2; try { activeRing.onError(blob); } catch {} continue; }
|
|
157
|
-
if (res.status === 401 || res.status === 403) {
|
|
158
|
-
try { await res.text(); } catch {}
|
|
159
|
-
try { activeRing.onError(blob); } catch {}
|
|
160
|
-
lastErr = new Error(`codearts auth still failing after refresh (http ${res.status})`);
|
|
161
|
-
continue;
|
|
162
|
-
}
|
|
163
|
-
} else {
|
|
164
|
-
try { activeRing.onError(blob); } catch {}
|
|
165
|
-
continue;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
if (res.status >= 400) {
|
|
169
|
-
const txt = await res.text().catch(() => "");
|
|
170
|
-
const msg = `codearts upstream http ${res.status}: ${String(txt).slice(0, 300)}`;
|
|
171
|
-
// 账号会话饱和:短冷却本号并换号重试(多号场景另一号通常立即成功);
|
|
172
|
-
// 所有号都饱和才落到重试耗尽分支,交给上层按上游不可用输出。
|
|
173
|
-
if (isSessionSaturated(res.status, txt)) {
|
|
174
|
-
try { activeRing.onError(blob, SESSION_SATURATED_COOLDOWN_MS); } catch {}
|
|
175
|
-
lastErr = Object.assign(new Error(msg), { upstreamStatus: res.status, upstreamBody: txt });
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
if (res.status === 429 || res.status >= 500) try { activeRing.onError(blob); } catch {}
|
|
179
|
-
return withTiming(errRes(res.status, msg), t0);
|
|
180
|
-
}
|
|
181
|
-
try {
|
|
182
|
-
const pre = await preflightResponse(res); // HTTP 200 内嵌错误(排队/未注册/福利未领)→ 抛 UpstreamEventError
|
|
183
|
-
if (isStream) return withTiming(sseToOpenAIResponse(pre, { model: requestedModel || upstreamModel, id: `chatcmpl-${chatId.slice(0, 12)}` }), t0);
|
|
184
|
-
const data = await aggregateToCompletion(pre, { model: requestedModel || upstreamModel, id: `chatcmpl-${chatId.slice(0, 12)}` });
|
|
185
|
-
return withTiming(new Response(JSON.stringify(data), { status: 200, headers: { "Content-Type": "application/json" } }), t0);
|
|
186
|
-
} catch (err) {
|
|
187
|
-
if (err instanceof UpstreamEventError) {
|
|
188
|
-
if (err.status === 429 || err.status >= 500) try { activeRing.onError(blob); } catch {}
|
|
189
|
-
return withTiming(errRes(err.status, `codearts upstream: ${err.message}`, err.code), t0);
|
|
190
|
-
}
|
|
191
|
-
try { activeRing.onError(blob); } catch {}
|
|
192
|
-
lastErr = err;
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
// 全部号跑完仍无成功:若最后一个是账号会话饱和(非参数错),如实回传上游 400,
|
|
197
|
-
// 别退化成笼统的 "all accounts unavailable"——用户要看得到「并发会话数已达上限」。
|
|
198
|
-
if (lastErr?.upstreamStatus && lastErr?.upstreamBody != null && isSessionSaturated(lastErr.upstreamStatus, lastErr.upstreamBody)) {
|
|
199
|
-
return withTiming(errRes(lastErr.upstreamStatus, lastErr.message), t0);
|
|
200
|
-
}
|
|
201
|
-
throw Object.assign(lastErr || new Error("codearts: all accounts unavailable"), {
|
|
202
|
-
_t: { attempts: [], waitMs: 0, totalMs: Math.round(clock() - t0) },
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return { runChat, buildUpstreamBody, injectReasoningPlaceholder, _sendOnce: sendOnce };
|
|
207
|
-
}
|
|
1
|
+
// CodeArts 聊天服务:上游 body/头组装 + SDK-HMAC-SHA256 签名 + 401 刷新重试 + 环形切号。
|
|
2
|
+
// 上游恒 stream:true(chatBodyV2 契约);流式客户端转 OpenAI SSE,非流式聚合回 JSON。
|
|
3
|
+
import crypto from "node:crypto";
|
|
4
|
+
import { joinUrl } from "../base.js";
|
|
5
|
+
import { createTransport } from "../../transport/index.js";
|
|
6
|
+
import { SNAP_BASE, EP_CHAT_V2, chatBaseHeaders, MAAS_TYPE_HEADER, MAAS_BENEFIT, MAX_TOKENS_CEILING } from "./const.js";
|
|
7
|
+
import { signRequest } from "./sign.js";
|
|
8
|
+
import { newChatId, preflightResponse, sseToOpenAIResponse, aggregateToCompletion, UpstreamEventError } from "./stream.js";
|
|
9
|
+
|
|
10
|
+
const MODEL_RULES = [
|
|
11
|
+
{ match: (m) => String(m || "").toLowerCase().includes("deepseek"), scope: "all" },
|
|
12
|
+
{ match: (m) => /(^kimi-|kimi)/i.test(String(m || "")), scope: "toolCalls" },
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
// DeepSeek 系思考模式要求 assistant 消息回传 reasoning_content,缺失补 " " 占位(ADR-0001 同款)。
|
|
16
|
+
export function injectReasoningPlaceholder(messages, model) {
|
|
17
|
+
const rule = MODEL_RULES.find((r) => r.match(model));
|
|
18
|
+
if (!rule || !Array.isArray(messages)) return messages;
|
|
19
|
+
return messages.map((m) => {
|
|
20
|
+
if (m?.role !== "assistant") return m;
|
|
21
|
+
if (typeof m.reasoning_content === "string" && m.reasoning_content.length) return m;
|
|
22
|
+
if (rule.scope === "toolCalls" && !(Array.isArray(m.tool_calls) && m.tool_calls.length)) return m;
|
|
23
|
+
return { ...m, reasoning_content: " " };
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const PASSTHROUGH_KEYS = ["temperature", "top_p", "max_tokens", "max_completion_tokens", "reasoning_effort", "tools", "tool_choice", "stop", "presence_penalty", "frequency_penalty", "response_format", "user", "n", "seed"];
|
|
28
|
+
|
|
29
|
+
// 账号级「会话/并发饱和」:上游按账号限同时活跃会话数(TM.00001041 = 并发会话数已达上限(3个))。
|
|
30
|
+
// 这是账号此刻进不去,不是本请求参数错;槽位在上游保留分钟级才回收(与本地进程无关)。
|
|
31
|
+
// 多号场景换另一个账号通常立即成功——故短冷却本账号让下一次 next() 换号并 continue 重试。
|
|
32
|
+
const SESSION_SATURATED_RE = /TM\.00001041|并发会话数已达上限/;
|
|
33
|
+
const SESSION_SATURATED_COOLDOWN_MS = Number.parseInt(process.env.MSLXDFF_CODEARTS_SESSION_COOLDOWN_MS ?? "", 10) || 30_000;
|
|
34
|
+
|
|
35
|
+
export function buildUpstreamBody(body, { chatId, upstreamModel }) {
|
|
36
|
+
const out = {
|
|
37
|
+
model: upstreamModel,
|
|
38
|
+
stream: true, // v2 端点契约:恒流式,非流式由本层聚合
|
|
39
|
+
messages: injectReasoningPlaceholder(body?.messages || [], upstreamModel),
|
|
40
|
+
prompt_cache_key: chatId,
|
|
41
|
+
tool_stream: true,
|
|
42
|
+
chat_id: chatId,
|
|
43
|
+
};
|
|
44
|
+
for (const k of PASSTHROUGH_KEYS) {
|
|
45
|
+
if (k === "max_completion_tokens") continue; // v2 端点不认这个名字(透传 → InferHub.001001005.400,2026-10-16 现网 A/B 复现)
|
|
46
|
+
if (body?.[k] !== undefined && body?.[k] !== null) out[k] = body[k];
|
|
47
|
+
}
|
|
48
|
+
// Responses 风格客户端只发 max_completion_tokens:映射成 v2 认的 max_tokens(显式 max_tokens 优先)
|
|
49
|
+
if (out.max_tokens === undefined && body?.max_completion_tokens != null) out.max_tokens = body.max_completion_tokens;
|
|
50
|
+
// 超上限的 max_tokens 上游一律回 InferHub.001001005.400(参数非法),在这里夹住(env 可放宽)
|
|
51
|
+
const ceiling = Number.parseInt(process.env.MSLXDFF_CODEARTS_MAX_TOKENS_CEILING ?? "", 10) || MAX_TOKENS_CEILING;
|
|
52
|
+
if (typeof out.max_tokens === "number" && out.max_tokens > ceiling) out.max_tokens = ceiling;
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Session-Id:由 chatId 派生的稳定会话标识(prompt cache 亲和,对齐 Go sessionIDFor)。
|
|
57
|
+
function sessionIdFor(chatId) {
|
|
58
|
+
return crypto.createHash("sha256").update(`codearts-session:${chatId}`, "utf8").digest("hex").slice(0, 32);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function stripOwnPrefix(model, providerId) {
|
|
62
|
+
const s = String(model || "");
|
|
63
|
+
const i = s.indexOf("/");
|
|
64
|
+
if (i <= 0) return s;
|
|
65
|
+
return s.slice(0, i).toLowerCase() === String(providerId || "").toLowerCase() ? s.slice(i + 1) : s;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// 从 4xx/5xx 正文里判「账号级会话饱和」:命中则可换号重试(不同于参数类 400,换号无意义)。
|
|
69
|
+
export function isSessionSaturated(status, bodyText) {
|
|
70
|
+
if (status !== 400 && status !== 429 && status < 500) return false;
|
|
71
|
+
return SESSION_SATURATED_RE.test(String(bodyText || ""));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function createChatService({
|
|
75
|
+
id = "codearts",
|
|
76
|
+
baseUrl = SNAP_BASE,
|
|
77
|
+
chatPath = EP_CHAT_V2,
|
|
78
|
+
fetchImpl,
|
|
79
|
+
dispatcher,
|
|
80
|
+
authPool,
|
|
81
|
+
catalog,
|
|
82
|
+
connectTimeoutMs = 30_000,
|
|
83
|
+
clock = Date.now,
|
|
84
|
+
} = {}) {
|
|
85
|
+
const resolvedBase = String(baseUrl).trim().replace(/\/+$/, "") || SNAP_BASE;
|
|
86
|
+
const transport = createTransport({ fetchImpl, dispatcher, keepAlive: !!dispatcher, timeoutMs: connectTimeoutMs, retry: {} });
|
|
87
|
+
|
|
88
|
+
async function sendOnce({ bodyStr, account, benefit, chatId, sessionId, traceId }) {
|
|
89
|
+
const url = joinUrl(resolvedBase, chatPath);
|
|
90
|
+
const h = chatBaseHeaders(account.securityToken, traceId);
|
|
91
|
+
if (benefit) h[MAAS_TYPE_HEADER] = MAAS_BENEFIT;
|
|
92
|
+
const { headers } = signRequest({ method: "POST", url, headers: h, body: bodyStr, cred: account });
|
|
93
|
+
// Chat-Id / Session-Id 在签名之后追加(对齐官方客户端:不进 SignedHeaders)。
|
|
94
|
+
headers["chat-id"] = chatId;
|
|
95
|
+
headers["session-id"] = sessionId;
|
|
96
|
+
return transport.request({ url, method: "POST", headers, body: bodyStr, stream: true, timeoutMs: connectTimeoutMs });
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function errRes(status, msg, code) {
|
|
100
|
+
return new Response(JSON.stringify({ error: { message: msg, type: "upstream_error", ...(code ? { code } : {}) } }), {
|
|
101
|
+
status,
|
|
102
|
+
headers: { "Content-Type": "application/json" },
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function withTiming(res, t0) {
|
|
107
|
+
try { res._t = { attempts: [], waitMs: 0, totalMs: Math.round(clock() - t0) }; } catch {}
|
|
108
|
+
return res;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 跑一次对话。body.model 已由 dispatcher 剥前缀(容忍自带前缀)。
|
|
113
|
+
* @returns {Promise<Response>} 流式 → OpenAI SSE;非流式 → chat.completion JSON
|
|
114
|
+
*/
|
|
115
|
+
async function runChat(body, activeRing, opts = {}) {
|
|
116
|
+
const t0 = clock();
|
|
117
|
+
const requestedModel = String(body?.model || "");
|
|
118
|
+
const rawModel = stripOwnPrefix(requestedModel, id);
|
|
119
|
+
const upstreamModel = catalog?.canonical(rawModel) || rawModel;
|
|
120
|
+
const chatId = newChatId(body, opts);
|
|
121
|
+
const sessionId = sessionIdFor(chatId);
|
|
122
|
+
const upstreamBody = buildUpstreamBody(body, { chatId, upstreamModel });
|
|
123
|
+
const bodyStr = JSON.stringify(upstreamBody);
|
|
124
|
+
const isStream = body?.stream === true;
|
|
125
|
+
let lastErr = null;
|
|
126
|
+
|
|
127
|
+
const maxTries = Math.max(1, activeRing.size || 1);
|
|
128
|
+
for (let attempt = 0; attempt < maxTries; attempt++) {
|
|
129
|
+
const blob = activeRing.next();
|
|
130
|
+
if (!blob) break;
|
|
131
|
+
let account;
|
|
132
|
+
try {
|
|
133
|
+
account = await authPool.getCredential(blob);
|
|
134
|
+
} catch (err) {
|
|
135
|
+
try { activeRing.onError(blob); } catch {}
|
|
136
|
+
lastErr = err;
|
|
137
|
+
continue; // 死号/不在池:换下一个账号
|
|
138
|
+
}
|
|
139
|
+
const benefit = catalog?.isBenefit ? catalog.isBenefit(account.userId, upstreamModel) : false;
|
|
140
|
+
const traceId = crypto.randomBytes(8).toString("hex") + String(clock());
|
|
141
|
+
let res;
|
|
142
|
+
try {
|
|
143
|
+
res = await sendOnce({ bodyStr, account, benefit, chatId, sessionId, traceId });
|
|
144
|
+
} catch (err) {
|
|
145
|
+
try { activeRing.onError(blob); } catch {}
|
|
146
|
+
lastErr = err;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (res.status === 401 || res.status === 403) {
|
|
150
|
+
// STS 临时凭证失效:强制刷新重试一次,仍 401/403 换号
|
|
151
|
+
authPool.invalidate(blob);
|
|
152
|
+
let account2 = null;
|
|
153
|
+
try { account2 = await authPool.getCredential(blob); } catch { /* 死号 → 换号 */ }
|
|
154
|
+
if (account2) {
|
|
155
|
+
const traceId2 = crypto.randomBytes(8).toString("hex") + String(clock());
|
|
156
|
+
try { res = await sendOnce({ bodyStr, account: account2, benefit, chatId, sessionId, traceId: traceId2 }); } catch (err2) { lastErr = err2; try { activeRing.onError(blob); } catch {} continue; }
|
|
157
|
+
if (res.status === 401 || res.status === 403) {
|
|
158
|
+
try { await res.text(); } catch {}
|
|
159
|
+
try { activeRing.onError(blob); } catch {}
|
|
160
|
+
lastErr = new Error(`codearts auth still failing after refresh (http ${res.status})`);
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
} else {
|
|
164
|
+
try { activeRing.onError(blob); } catch {}
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (res.status >= 400) {
|
|
169
|
+
const txt = await res.text().catch(() => "");
|
|
170
|
+
const msg = `codearts upstream http ${res.status}: ${String(txt).slice(0, 300)}`;
|
|
171
|
+
// 账号会话饱和:短冷却本号并换号重试(多号场景另一号通常立即成功);
|
|
172
|
+
// 所有号都饱和才落到重试耗尽分支,交给上层按上游不可用输出。
|
|
173
|
+
if (isSessionSaturated(res.status, txt)) {
|
|
174
|
+
try { activeRing.onError(blob, SESSION_SATURATED_COOLDOWN_MS); } catch {}
|
|
175
|
+
lastErr = Object.assign(new Error(msg), { upstreamStatus: res.status, upstreamBody: txt });
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (res.status === 429 || res.status >= 500) try { activeRing.onError(blob); } catch {}
|
|
179
|
+
return withTiming(errRes(res.status, msg), t0);
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
const pre = await preflightResponse(res); // HTTP 200 内嵌错误(排队/未注册/福利未领)→ 抛 UpstreamEventError
|
|
183
|
+
if (isStream) return withTiming(sseToOpenAIResponse(pre, { model: requestedModel || upstreamModel, id: `chatcmpl-${chatId.slice(0, 12)}` }), t0);
|
|
184
|
+
const data = await aggregateToCompletion(pre, { model: requestedModel || upstreamModel, id: `chatcmpl-${chatId.slice(0, 12)}` });
|
|
185
|
+
return withTiming(new Response(JSON.stringify(data), { status: 200, headers: { "Content-Type": "application/json" } }), t0);
|
|
186
|
+
} catch (err) {
|
|
187
|
+
if (err instanceof UpstreamEventError) {
|
|
188
|
+
if (err.status === 429 || err.status >= 500) try { activeRing.onError(blob); } catch {}
|
|
189
|
+
return withTiming(errRes(err.status, `codearts upstream: ${err.message}`, err.code), t0);
|
|
190
|
+
}
|
|
191
|
+
try { activeRing.onError(blob); } catch {}
|
|
192
|
+
lastErr = err;
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// 全部号跑完仍无成功:若最后一个是账号会话饱和(非参数错),如实回传上游 400,
|
|
197
|
+
// 别退化成笼统的 "all accounts unavailable"——用户要看得到「并发会话数已达上限」。
|
|
198
|
+
if (lastErr?.upstreamStatus && lastErr?.upstreamBody != null && isSessionSaturated(lastErr.upstreamStatus, lastErr.upstreamBody)) {
|
|
199
|
+
return withTiming(errRes(lastErr.upstreamStatus, lastErr.message), t0);
|
|
200
|
+
}
|
|
201
|
+
throw Object.assign(lastErr || new Error("codearts: all accounts unavailable"), {
|
|
202
|
+
_t: { attempts: [], waitMs: 0, totalMs: Math.round(clock() - t0) },
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return { runChat, buildUpstreamBody, injectReasoningPlaceholder, _sendOnce: sendOnce };
|
|
207
|
+
}
|
|
@@ -44,12 +44,19 @@ export const MAAS_TYPE_HEADER = "maas_type";
|
|
|
44
44
|
export const MAAS_BENEFIT = "benefit";
|
|
45
45
|
|
|
46
46
|
// 冷启动种子:福利套餐轮换后新模型在首次发现前也按福利路由(宁多带不少带,少带即 404)。
|
|
47
|
-
// 现网取证 2026-10-16(福利网关 /api/v1/gateway/config,error_code=0000)共 4 位:下列
|
|
48
|
-
|
|
49
|
-
//
|
|
47
|
+
// 现网取证 2026-10-16(福利网关 /api/v1/gateway/config,error_code=0000)共 4 位:下列 id 逐字对齐上游。
|
|
48
|
+
// 2026-10-24 再取证:`deepseek-v4.1-flash` 已转 builtin 正式模型(福利网关该号已空)。它留在福利种子里
|
|
49
|
+
// 是有害的 —— preheat 未回来时带福利头 → InferHub.4004.200 benefit not found,不带才 200。故移出、转入正式表。
|
|
50
|
+
export const SEED_BENEFIT_MODELS = ["deepseek-v4-flash-0731", "deepseek-v4-pro-0813", "glm-5.3-flash"];
|
|
51
|
+
// 内置模型种子(仅用于大小写归一,不标福利)。id 一律写上游真正的 model_id 大小写:
|
|
52
|
+
// `deepseek-v4.1-flash` 是全小写,`GLM-5.2` 就是混合大小写——猜错即 InferHub.002002009.404。
|
|
50
53
|
export const SEED_KNOWN_MODELS = [
|
|
51
|
-
|
|
52
|
-
|
|
54
|
+
// 现网取证 2026-10-24(builtin 路真 model_id 逐字):这些就是全小写,展示名(OpenPangu-2.0-Pro /
|
|
55
|
+
// GLM-5.3 / Kimi-K3)出自 agent-center 的 model_name,当 id 发上去必 InferHub.002002009.404。
|
|
56
|
+
// 同名漂移记在 name 字段,不占用 id:GLM-5.2-ArkTS-SPARK 的真 id 是 glm-5.2-sft-harmony。
|
|
57
|
+
"deepseek-v4.1-flash", "glm-5.3", "kimi-k3", "openpangu-2.0-pro", "openpangu-2.0-flash",
|
|
58
|
+
"glm-5.2-sft-harmony", "kimi-k2.6-vl",
|
|
59
|
+
"GLM-5.2", "GLM-5.1", "GLM-4.7",
|
|
53
60
|
"Qwen3-VL-235B", "Qwen3.5-397B-A17B-VL", "Qwen3.6-27B-VL",
|
|
54
61
|
];
|
|
55
62
|
|
|
@@ -14,8 +14,19 @@ const lower = (s) => String(s || "").trim().toLowerCase();
|
|
|
14
14
|
|
|
15
15
|
export function createCatalog() {
|
|
16
16
|
const known = new Map(); // lower → 精确 ID(种子 + 已发现,大小写归一单点)
|
|
17
|
+
const knownReal = new Set(); // 该精确 ID 出自上游真正的 model_id(不是展示名兜底)
|
|
18
|
+
|
|
19
|
+
// id 权威性:真正的 model_id > 展示名(agent-center 无 model_id 时拿 model_name 顶包,
|
|
20
|
+
// 发上去必 InferHub.002002009.404);同级才用历史规则「非小写的精确大小写覆盖小写」。
|
|
21
|
+
function noteKnown(key, id, byModelId) {
|
|
22
|
+
if (!known.has(key)) { known.set(key, id); if (byModelId) knownReal.add(key); return; }
|
|
23
|
+
if (byModelId && !knownReal.has(key)) { known.set(key, id); knownReal.add(key); return; }
|
|
24
|
+
if (!!byModelId === knownReal.has(key) && id !== key && known.get(key) === key) known.set(key, id);
|
|
25
|
+
}
|
|
26
|
+
|
|
17
27
|
for (const id of [...SEED_KNOWN_MODELS, ...SEED_BENEFIT_MODELS]) {
|
|
18
|
-
|
|
28
|
+
const key = lower(id);
|
|
29
|
+
if (key) noteKnown(key, id, false); // 种子按展示名记账:真 model_id 一发现就覆盖它
|
|
19
30
|
}
|
|
20
31
|
const accounts = new Map(); // userId → {models, benefit:Set, knows:Set, lastBenefit:Map, fetched}
|
|
21
32
|
|
|
@@ -28,7 +39,7 @@ export function createCatalog() {
|
|
|
28
39
|
const key = lower(m.id);
|
|
29
40
|
ac.knows.add(key);
|
|
30
41
|
if (m.benefit) { ac.benefit.add(key); ac.lastBenefit.set(key, m); }
|
|
31
|
-
|
|
42
|
+
noteKnown(key, m.id, m.byModelId);
|
|
32
43
|
ac.models.push(m);
|
|
33
44
|
}
|
|
34
45
|
if (keepBenefit) {
|
|
@@ -38,7 +49,7 @@ export function createCatalog() {
|
|
|
38
49
|
ac.benefit.add(key);
|
|
39
50
|
ac.knows.add(key);
|
|
40
51
|
ac.models.push(mi);
|
|
41
|
-
|
|
52
|
+
noteKnown(key, mi.id, mi.byModelId);
|
|
42
53
|
}
|
|
43
54
|
ac.models.sort((a, b) => (lower(a.id) < lower(b.id) ? -1 : lower(a.id) > lower(b.id) ? 1 : (a.id < b.id ? -1 : 1)));
|
|
44
55
|
}
|
|
@@ -72,7 +83,7 @@ export function createCatalog() {
|
|
|
72
83
|
return { setAccount, isBenefit, canonical, accountModels, isStale };
|
|
73
84
|
}
|
|
74
85
|
|
|
75
|
-
/** 多路合并:按小写 ID
|
|
86
|
+
/** 多路合并:按小写 ID 去重,福利标记取或,真 model_id 优先 > 参数更全 > 精确大小写,按小写 ID 排序。 */
|
|
76
87
|
export function mergeModels(sets) {
|
|
77
88
|
const merged = new Map();
|
|
78
89
|
for (const set of sets || []) {
|
|
@@ -81,7 +92,11 @@ export function mergeModels(sets) {
|
|
|
81
92
|
const key = lower(m.id);
|
|
82
93
|
const cur = merged.get(key);
|
|
83
94
|
if (!cur) { merged.set(key, { ...m }); continue; }
|
|
84
|
-
|
|
95
|
+
// 上游真正的 model_id 优先于展示名:agent-center 那份只有 model_name(DeepSeek-V4.1-Flash),
|
|
96
|
+
// builtin 那份才是可请求的 id(deepseek-v4.1-flash);发错大小写 = InferHub.002002009.404。
|
|
97
|
+
const keep = !!m.byModelId !== !!cur.byModelId
|
|
98
|
+
? (m.byModelId ? m : cur)
|
|
99
|
+
: ((m.contextWindow > 0 && !cur.contextWindow) || (m.id !== lower(m.id) && cur.id === lower(cur.id)) ? m : cur);
|
|
85
100
|
const drop = keep === m ? cur : m;
|
|
86
101
|
merged.set(key, {
|
|
87
102
|
...keep,
|
|
@@ -130,6 +145,9 @@ const toModelInfo = (m, { benefit = false } = {}) => {
|
|
|
130
145
|
if (!id) return null;
|
|
131
146
|
return {
|
|
132
147
|
id: String(id),
|
|
148
|
+
// 出处记账:只有真正的 model_id 字段才敢当「发给上游的 id」;靠 model_name/modelAlias
|
|
149
|
+
// 兜底出来的是展示名,上游 InferHub 不认(现网 2026-10-24:DeepSeek-V4.1-Flash 必 404)。
|
|
150
|
+
byModelId: !!m?.model_id,
|
|
133
151
|
name: String(m?.model_name || id),
|
|
134
152
|
contextWindow: Number(m?.context_window) || 0,
|
|
135
153
|
maxTokens: Number(m?.max_tokens) || 0,
|