mslxdff 0.1.231 → 0.1.232
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/mslxdff.js +3 -3
- package/package.json +1 -1
- package/src/auto.js +254 -254
- package/src/bench/cline-bench.js +42 -42
- package/src/bench/probe.js +70 -70
- package/src/bench/report.js +162 -162
- package/src/bench/runner.js +77 -77
- package/src/bench/via-probe.js +124 -124
- package/src/bench/via-routes.js +87 -87
- package/src/bench/workbuddy-bench.js +54 -54
- package/src/chat/engine.js +160 -160
- package/src/chat/gateway.js +163 -163
- package/src/chat/orchestrator.js +234 -234
- package/src/chat/prompt.js +70 -70
- package/src/chat/repl.js +88 -88
- package/src/chat/terminal.js +135 -135
- package/src/chat/tools.js +306 -306
- package/src/chat-pipeline/index.js +145 -145
- package/src/chat-pipeline/policy.js +84 -84
- package/src/cli/commands/group.js +266 -266
- package/src/cli/commands/model/picks.js +50 -50
- package/src/cli/commands/provider/bench-via.js +247 -247
- package/src/cli/commands/provider/bench.js +141 -141
- package/src/cli/commands/provider/index.js +139 -139
- package/src/cli/commands/provider/models.js +149 -149
- package/src/cli/commands/provider/qwenwork-login.js +119 -119
- package/src/cli/commands/provider/raccoon-quota.js +156 -156
- package/src/cli/commands/sync.js +310 -310
- package/src/cli/status.js +279 -279
- package/src/daemon.js +246 -246
- package/src/model-capabilities/enrich.js +86 -86
- package/src/model-capabilities/index.js +183 -183
- package/src/model-capabilities/parse.js +70 -70
- package/src/models.js +225 -225
- package/src/peers.js +345 -345
- package/src/providers/agentrouter.js +238 -238
- package/src/providers/base.js +164 -164
- package/src/providers/cline/auth.js +228 -228
- package/src/providers/cline/chat.js +307 -307
- package/src/providers/cline.js +2 -2
- package/src/providers/codearts/chat.js +207 -207
- package/src/providers/dispatcher.js +147 -147
- package/src/providers/globalqwenwork/cosy.js +123 -123
- package/src/providers/globalqwenwork/crypto.js +218 -218
- package/src/providers/globalqwenwork/http.js +20 -20
- package/src/providers/globalqwenwork/rsa.js +54 -54
- package/src/providers/globalqwenwork/sse.js +268 -268
- package/src/providers/globalqwenwork/stream.js +130 -130
- package/src/providers/globalqwenwork/upstream.js +122 -122
- package/src/providers/keyring.js +60 -60
- package/src/providers/qoder/chat.js +203 -203
- package/src/providers/qoder/index.js +249 -249
- package/src/providers/qoder/sse.js +145 -145
- package/src/providers/qwenwork/account-store.js +133 -133
- package/src/providers/qwenwork/constants.js +67 -67
- package/src/providers/qwenwork/cosy.js +120 -120
- package/src/providers/qwenwork/crypto.js +218 -218
- package/src/providers/qwenwork/http.js +20 -20
- package/src/providers/qwenwork/index.js +337 -337
- package/src/providers/qwenwork/payload.js +144 -144
- package/src/providers/qwenwork/rsa.js +54 -54
- package/src/providers/qwenwork/sse.js +268 -268
- package/src/providers/qwenwork/stream.js +130 -130
- package/src/providers/qwenwork/upstream.js +120 -120
- package/src/providers/qwenwork.js +1 -1
- package/src/providers/raccoon/const.js +202 -202
- package/src/providers/raccoon/credits.js +285 -285
- package/src/providers/registry.js +89 -89
- package/src/providers/workbuddy/auth.js +16 -0
- package/src/providers/workbuddy/chat.js +298 -264
- package/src/providers/workbuddy/client-headers.js +148 -0
- package/src/providers/workbuddy/desktop-info.js +17 -1
- package/src/providers/workbuddy/models.js +8 -20
- package/src/providers/workbuddy/reshape.js +152 -152
- package/src/providers/workbuddy.js +2 -2
- package/src/reasoning.js +32 -32
- package/src/routes/chat/broadband-handler.js +80 -80
- package/src/routes/chat/gateway.js +46 -46
- package/src/routes/chat/relay-pipeline.js +282 -282
- package/src/routes/chat/via-route-handler.js +156 -156
- package/src/routes/hedge.js +260 -260
- package/src/routes/models-route.js +167 -167
- package/src/routes/peers.js +373 -373
- package/src/routes/stream.js +347 -347
- package/src/runtime/bootstrap.js +75 -75
- package/src/runtime/keep-warm.js +135 -135
- package/src/runtime/provider-gate.js +75 -75
- package/src/runtime/providers-setup.js +187 -187
- package/src/runtime/raccoon-checkin.js +175 -175
- package/src/server.js +85 -85
- package/src/state/schemas/allowlist.js +92 -92
- package/src/sync-opencode.js +280 -280
- package/src/transport/index.js +244 -244
- package/src/transport/pool.js +56 -56
- package/src/transport/retry.js +24 -24
- package/src/transport/sse.js +93 -93
- package/src/upstream-probe/display.js +52 -52
- package/src/upstream-probe/probe.js +49 -49
- package/src/upstream-probe/rotate.js +110 -110
- package/src/upstream-probe/start.js +45 -45
- package/src/upstream.js +355 -355
- package/docs/cli_help_mini.md +0 -148
- package/src/providers/AGENTS.md +0 -49
- package/src/routes/AGENTS.md +0 -37
|
@@ -1,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
|
+
}
|