mslxdff 0.1.163 → 0.1.169
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 +1 -1
- package/src/anthropic/translate.js +397 -0
- package/src/anthropic/web-search.js +361 -0
- package/src/api-kind.js +31 -0
- package/src/chat-pipeline/auto-race.js +8 -0
- package/src/chat-pipeline/empty-turn.js +28 -0
- package/src/chat-pipeline/index.js +8 -4
- package/src/chat-pipeline/serial-trial.js +8 -1
- package/src/claude-desktop/apply.js +64 -0
- package/src/claude-desktop/doctor.js +146 -0
- package/src/claude-desktop/slots.js +86 -0
- package/src/cli/commands/claude-desktop.js +264 -0
- package/src/cli/commands/model/list-providers.js +1 -1
- package/src/cli/commands/provider/index.js +7 -0
- package/src/cli/commands/provider/models.js +3 -0
- package/src/cli/commands/provider/raccoon-login.js +131 -0
- package/src/cli/commands/provider/raccoon-quota.js +109 -0
- package/src/cli/commands/sync.js +80 -2
- package/src/cli/format.js +1 -1
- package/src/cli/help.js +3 -0
- package/src/cli/index.js +2 -0
- package/src/cli/provider-row.js +2 -2
- package/src/model-capabilities/parse.js +1 -1
- package/src/model-trace.js +2 -1
- package/src/providers/classify.js +1 -1
- package/src/providers/dispatcher.js +2 -2
- package/src/providers/keyring.js +1 -1
- package/src/providers/model-id.js +10 -0
- package/src/providers/raccoon/account-store.js +216 -0
- package/src/providers/raccoon/auth.js +67 -0
- package/src/providers/raccoon/chat.js +207 -0
- package/src/providers/raccoon/const.js +111 -0
- package/src/providers/raccoon/credits.js +161 -0
- package/src/providers/raccoon/envelope.js +23 -0
- package/src/providers/raccoon/headers.js +45 -0
- package/src/providers/raccoon/index.js +216 -0
- package/src/providers/raccoon/login.js +195 -0
- package/src/providers/raccoon/models.js +160 -0
- package/src/providers/raccoon/qr-codewords.js +115 -0
- package/src/providers/raccoon/qr.js +236 -0
- package/src/providers/raccoon/sse.js +135 -0
- package/src/providers/registry.js +5 -0
- package/src/providers/share-keys.js +1 -1
- package/src/providers/workbuddy/models.js +29 -14
- package/src/providers/workbuddy/payload.js +34 -0
- package/src/routes/chat/exhausted-handler.js +4 -4
- package/src/routes/chat/relay-pipeline.js +6 -7
- package/src/routes/index.js +13 -0
- package/src/routes/messages-route.js +401 -0
- package/src/routes/responses-route.js +2 -2
- package/src/runtime/bootstrap.js +5 -0
- package/src/runtime/broadband.js +4 -1
- package/src/runtime/provider-gate.js +3 -1
- package/src/runtime/raccoon-checkin.js +126 -0
- package/src/state/provider-config.js +1 -1
- package/src/sync-claude-desktop.js +234 -0
- package/src/sync-claude.js +152 -0
- package/src/talk-full.js +2 -2
- package/src/talk-log.js +9 -6
|
@@ -49,25 +49,40 @@ export function createModelsService({
|
|
|
49
49
|
async function listModels() {
|
|
50
50
|
const now = clock();
|
|
51
51
|
if (cache && now - fetchedAt < CACHE_TTL_MS) return cache;
|
|
52
|
+
// 逐 key 试到成功:单个 token 被上游撤销时整个目录不该空掉。2026-10-10 实测 4 个 token 里 3 个
|
|
53
|
+
// 已失效,旧逻辑取 ring.next() 首个 key 401 即 return [],CLI 端表现为 `workbuddy models (0 total)`
|
|
54
|
+
// → aggLoad 判 `!allowed.length` 把整家 workbuddy 跳过,用户侧就是「模型全没了」。
|
|
55
|
+
const tried = new Set();
|
|
56
|
+
let res = null;
|
|
52
57
|
try {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
for (let i = 0; i < 8; i++) {
|
|
59
|
+
const key = getKey ? getKey() : "";
|
|
60
|
+
if (tried.has(key)) break; // ring 绕回已试过的号 = 没有新号了
|
|
61
|
+
tried.add(key);
|
|
62
|
+
const auth = getAuth ? getAuth(key) : null;
|
|
63
|
+
if (auth && key) maybeProactiveRefresh?.(auth, key);
|
|
64
|
+
let r = await execList(key, auth);
|
|
65
|
+
if (!r.ok) {
|
|
66
|
+
let t = "";
|
|
67
|
+
try { t = await r.clone().text(); } catch {}
|
|
68
|
+
if (isAuthError?.(r.status, t)) {
|
|
69
|
+
const newKey = await refreshTokenFor?.(key, auth);
|
|
70
|
+
if (newKey && !tried.has(newKey)) {
|
|
71
|
+
tried.add(newKey);
|
|
72
|
+
r = await execList(newKey, getAuth ? getAuth(newKey) : auth);
|
|
73
|
+
}
|
|
65
74
|
}
|
|
66
75
|
}
|
|
76
|
+
if (r.ok) { res = r; break; }
|
|
67
77
|
}
|
|
68
|
-
if (!res
|
|
78
|
+
if (!res) return [];
|
|
69
79
|
const json = await res.json().catch(() => ({}));
|
|
70
|
-
|
|
80
|
+
// `/v3/config` 两种形状都出现过:`{code,msg,data}` 信封 / 产品文档裸放顶层
|
|
81
|
+
const doc = json && typeof json === "object"
|
|
82
|
+
? (json.data && typeof json.data === "object" && !Array.isArray(json.data) ? json.data
|
|
83
|
+
: (Array.isArray(json.models) || Array.isArray(json.agents) ? json : null))
|
|
84
|
+
: null;
|
|
85
|
+
const models = doc?.models;
|
|
71
86
|
if (!Array.isArray(models)) return [];
|
|
72
87
|
const sorted = [...models].sort((a, b) => creditsValue(a.credits) - creditsValue(b.credits));
|
|
73
88
|
cache = sorted.filter((m) => m && typeof m.id === "string").map((m) => ({ ...m, id: joinModelId(id, m.id) }));
|
|
@@ -5,8 +5,23 @@
|
|
|
5
5
|
// 显式 disabled 尊重并删 effort;缺档补默认 "high"
|
|
6
6
|
// 4. DeepSeek reasoning_content 回填:会话内任一 assistant 带 reasoning 痕迹时,
|
|
7
7
|
// 所有 assistant 必须带 reasoning_content(string,可空串)——requiresReasoningContentOnAssistantMessages
|
|
8
|
+
// 5. Claude Code 指纹剥离:上游扫 system/assistant 文本,命中官方固定串即 400 code=11128
|
|
9
|
+
// "Illegal API invocation from an unapproved channel"(拒当 Claude Code 的免费通道)。
|
|
10
|
+
// 实测 2026-10-10(拿真 Claude Code 27KB prompt 二分定位):触发形态共三种——行首 `x-anthropic-billing-header:` 整行 +
|
|
11
|
+
// 两句官方固定整句("You are Claude Code, Anthropic's official CLI for Claude." 与
|
|
12
|
+
// "To give feedback, users should report the issue at https://github.com/anthropics/claude-code/issues");
|
|
13
|
+
// 整句为**纯子串匹配**(去掉行首 `- ` 照拦),user/tool 文本不扫、换说法/中间片段/普通长文全放行
|
|
14
|
+
// → 只剥官方注入物不动用户内容;日后撞见新拦句,加进 CC_SENTENCES 即可
|
|
8
15
|
const DEEPSEEK_PREFIX = "deepseek";
|
|
9
16
|
const DEFAULT_DEEPSEEK_EFFORT = "high";
|
|
17
|
+
const CC_BILLING_LINE = /^x-anthropic-billing-header:.*\r?\n?/gim;
|
|
18
|
+
// 整句指纹清单:官方固定整句(实测为纯子串匹配,与行首前缀无关;新拦句直接往这里追加整句)
|
|
19
|
+
const CC_SENTENCES = [
|
|
20
|
+
"You are Claude Code, Anthropic's official CLI for Claude.",
|
|
21
|
+
"To give feedback, users should report the issue at https://github.com/anthropics/claude-code/issues",
|
|
22
|
+
];
|
|
23
|
+
// 廉价预检:上面三种形态的大小写不敏感超集——只多放行进剥离流程,绝不漏剥
|
|
24
|
+
const CC_MAYBE = /x-anthropic-billing-header|Claude Code|anthropics\/claude-code/i;
|
|
10
25
|
|
|
11
26
|
export function isDeepSeekModel(model) {
|
|
12
27
|
return String(model || "").trim().toLowerCase().startsWith(DEEPSEEK_PREFIX);
|
|
@@ -81,6 +96,24 @@ export function backfillReasoningContent(obj) {
|
|
|
81
96
|
return obj;
|
|
82
97
|
}
|
|
83
98
|
|
|
99
|
+
export function stripClaudeFingerprints(obj) {
|
|
100
|
+
const msgs = obj?.messages;
|
|
101
|
+
if (!Array.isArray(msgs)) return obj;
|
|
102
|
+
const clean = (text) => {
|
|
103
|
+
if (typeof text !== "string" || !CC_MAYBE.test(text)) return text;
|
|
104
|
+
let out = text.replace(CC_BILLING_LINE, "");
|
|
105
|
+
for (const s of CC_SENTENCES) out = out.split(s).join("");
|
|
106
|
+
return out;
|
|
107
|
+
};
|
|
108
|
+
for (const m of msgs) {
|
|
109
|
+
if (!m || typeof m !== "object") continue;
|
|
110
|
+
if (m.role !== "system" && m.role !== "assistant") continue; // 上游不扫 user/tool,不碰用户内容
|
|
111
|
+
if (typeof m.content === "string") m.content = clean(m.content);
|
|
112
|
+
else if (Array.isArray(m.content)) m.content = m.content.map((p) => (p && typeof p === "object" && typeof p.text === "string" ? { ...p, text: clean(p.text) } : p));
|
|
113
|
+
}
|
|
114
|
+
return obj;
|
|
115
|
+
}
|
|
116
|
+
|
|
84
117
|
// 入口:输入 chat body(OpenAI 格式),返回改写后的新对象(messages 浅拷贝,避免污染调用方)。
|
|
85
118
|
export function rewriteWorkbuddyPayload(body) {
|
|
86
119
|
if (!body || typeof body !== "object") return body;
|
|
@@ -89,6 +122,7 @@ export function rewriteWorkbuddyPayload(body) {
|
|
|
89
122
|
? body.messages.map((m) => (m && typeof m === "object" ? { ...m } : m))
|
|
90
123
|
: body.messages;
|
|
91
124
|
normalizeRoles(obj);
|
|
125
|
+
stripClaudeFingerprints(obj); // 11128:官方客户端指纹必须出网前剥掉(实测命中即秒拒,不进模型)
|
|
92
126
|
normalizeToolChoice(obj);
|
|
93
127
|
if (isDeepSeekModel(obj.model)) {
|
|
94
128
|
injectThinking(obj);
|
|
@@ -28,11 +28,11 @@ export async function handleExhaustedLocal({ res, body, lastErr, order, handlerC
|
|
|
28
28
|
// 绕过 relay-pipeline 的最后一站:对话日志照常落盘(正文只进 talk/*.log,不进 events.log)
|
|
29
29
|
// 本收尾点没有 attemptStartMs 可取(绕过 relay-pipeline 直连 relay)→ 不传 attemptMs,按回退路径走:
|
|
30
30
|
// elapsed=relay 内部计时(与 relayMs 同值,spec 降级 scenario);finishReason 传 out.detail.sawFinishReason 与 3.3 同源。
|
|
31
|
-
try { recordRelayTalk({ reqId: handlerCtx.reqId, model: lastErr.model, via: "local-exhausted", body, out, finishReason: out.detail?.sawFinishReason }); } catch {}
|
|
31
|
+
try { recordRelayTalk({ reqId: handlerCtx.reqId, api: handlerCtx?.api, model: lastErr.model, via: "local-exhausted", body, out, finishReason: out.detail?.sawFinishReason }); } catch {}
|
|
32
32
|
// 回路语料另起一条独立 try:talk.log 是人读环形稿、capture 是机读全量语料,两者生死独立。
|
|
33
33
|
// hops 走 handlerCtx.hops(serial-trial 以 {...handlerCtx} 展开传进来,与 reqId 同源);缺值时不兜 0 ——
|
|
34
34
|
// 交给 talk-full 落 null(「hops 未知」在语料里看得见,比伪造成 0 诚实),组员流量拦截靠真值。
|
|
35
|
-
try { recordAgentLoop({ reqId: handlerCtx.reqId, model: lastErr.model, via: "local-exhausted", hops: handlerCtx?.hops, body, out, sessionKey: handlerCtx?.sessionId, clientIp: handlerCtx?.clientIp }); } catch {} // attemptMs 缺省 → 回退 out.totalMs;缺 echo 的既有形状本期不补(tasks 3.4)
|
|
35
|
+
try { recordAgentLoop({ reqId: handlerCtx.reqId, api: handlerCtx?.api, model: lastErr.model, via: "local-exhausted", hops: handlerCtx?.hops, body, out, sessionKey: handlerCtx?.sessionId, clientIp: handlerCtx?.clientIp }); } catch {} // attemptMs 缺省 → 回退 out.totalMs;缺 echo 的既有形状本期不补(tasks 3.4)
|
|
36
36
|
evt("result", { reqId: handlerCtx.reqId, model: lastErr.model, status: out.status, via: "local", timing: lastErr.upstream._t ?? null, ttfMs: out.ttfMs, totalMs: out.totalMs, detail: out.detail ?? null });
|
|
37
37
|
evt("client-response", { requested, actual: lastErr.model, via: "local", fallback: false, status: out.status, reqId: handlerCtx.reqId });
|
|
38
38
|
// 最后一站:relay 超时/空轮留口路径都不写响应,这里没有上层,必须自己收尾
|
|
@@ -64,9 +64,9 @@ export async function handleExhaustedAll({ res, body, lastErr, order, requested,
|
|
|
64
64
|
// 同上:全链耗尽的最终转发也要留下问答原文
|
|
65
65
|
// 同上:本收尾点没有 attemptStartMs 可取(relay 直连、无尝试起点)→ 不传 attemptMs,
|
|
66
66
|
// recordRelayTalk/recordAgentLoop 内部按回退路径走 elapsed=relay 计时(与 relayMs 同值,spec 降级 scenario);finishReason 同源传 out.detail.sawFinishReason。
|
|
67
|
-
try { recordRelayTalk({ reqId: handlerCtx.reqId, model: lastErr.model, via: "local-final", body, out, finishReason: out.detail?.sawFinishReason }); } catch {}
|
|
67
|
+
try { recordRelayTalk({ reqId: handlerCtx.reqId, api: handlerCtx?.api, model: lastErr.model, via: "local-final", body, out, finishReason: out.detail?.sawFinishReason }); } catch {}
|
|
68
68
|
// 同上:全链耗尽的最终转发也要进语料(这条往往是「客户端只看到 502」时唯一的现场材料)
|
|
69
|
-
try { recordAgentLoop({ reqId: handlerCtx.reqId, model: lastErr.model, via: "local-final", hops: handlerCtx?.hops, body, out, sessionKey: handlerCtx?.sessionId, clientIp: handlerCtx?.clientIp }); } catch {} // attemptMs 缺省 → 回退 out.totalMs(见上行注释;缺 echo 的形状本期不补)
|
|
69
|
+
try { recordAgentLoop({ reqId: handlerCtx.reqId, api: handlerCtx?.api, model: lastErr.model, via: "local-final", hops: handlerCtx?.hops, body, out, sessionKey: handlerCtx?.sessionId, clientIp: handlerCtx?.clientIp }); } catch {} // attemptMs 缺省 → 回退 out.totalMs(见上行注释;缺 echo 的形状本期不补)
|
|
70
70
|
evt("result", { reqId: handlerCtx.reqId, model: lastErr.model, status: out.status, via: "local", timing: lastErr.upstream._t ?? null, ttfMs: out.ttfMs, totalMs: out.totalMs, detail: out.detail ?? null });
|
|
71
71
|
evt("client-response", { requested, actual: lastErr.model, via: "local", fallback: false, status: out.status, reqId: handlerCtx.reqId });
|
|
72
72
|
// 最后一站:relay 超时/空轮留口路径不写响应,这里没有上层 failover,必须自己收尾
|
|
@@ -81,8 +81,7 @@ export function createRelayPipeline({
|
|
|
81
81
|
const markFn = m2 || _mark;
|
|
82
82
|
const curStartedAt = sa2 ?? defaultStartedAt ?? Date.now();
|
|
83
83
|
const curStages = s2 ?? defaultStages ?? [];
|
|
84
|
-
const reqId = handlerCtx
|
|
85
|
-
const hops = handlerCtx?.hops;
|
|
84
|
+
const { reqId, api, hops } = handlerCtx || {};
|
|
86
85
|
|
|
87
86
|
// 1. logCall(pre) — 保持原 handler 的 logCall→fallback→relay-start 时序
|
|
88
87
|
try { _logCall(actual, upRes?.status); } catch {}
|
|
@@ -141,13 +140,16 @@ export function createRelayPipeline({
|
|
|
141
140
|
//(P0 回归实锤:落盘 elapsedMs=1791249258686;测试喂了同钟 perfNow 桩才把混钟掩盖住 —— 防回归见 test/agent-loop-wiring.test.js 真时钟两例)。
|
|
142
141
|
// attemptStartMs 非有限(调用方未给该 ctx,如管线直调)→ 回退 out.totalMs,此时 elapsed 与 relayMs 同值(spec 降级 scenario),不伪造。
|
|
143
142
|
const attemptMs = Number.isFinite(attemptStartMs) ? Math.round(performance.now() - attemptStartMs) : out.totalMs;
|
|
143
|
+
// ttft = relay 入口前消耗的等待(anchorMs) + relay 内首块;与 usage 行 rowTtfb 同源。anchorMs 只加给上报时长,闸门计时与 recordModelStats 不碰(design D2)。
|
|
144
|
+
const anchorMs = Number.isFinite(out?.preflightMs) && out.preflightMs > 0 ? out.preflightMs : 0;
|
|
145
|
+
const ttftMs = Number.isFinite(out?.ttfMs) && out.ttfMs >= 0 ? out.ttfMs + anchorMs : null;
|
|
144
146
|
// 环形对话日志:只记有真实输出的轮次(空轮交给下面的空转判定另行报错)。
|
|
145
147
|
// 纯旁路:不改闸门、不改计时、不改返回体;任何异常都吞掉,绝不影响转发。
|
|
146
|
-
try { recordRelayTalk({ reqId, model: actual, via, hops, body, out, echo: upstreamEcho(upRes), attemptMs, finishReason: out.detail?.sawFinishReason }); } catch {}
|
|
148
|
+
try { recordRelayTalk({ reqId, api, model: actual, via, hops, body, out, echo: upstreamEcho(upRes), attemptMs, ttftMs, finishReason: out.detail?.sawFinishReason }); } catch {}
|
|
147
149
|
// 回路语料(talk/full)另起一条独立 try,不与上面合并:talk.log 是人读环形稿、capture 是机读全量语料,
|
|
148
150
|
// 两者生死独立 —— 一边不落另一边照落,判据也不同(capture 只认 hops=0,且要带 sessionKey/clientIp 供分组与来源核对)。
|
|
149
151
|
// 纯旁路同上:不改闸门、不改计时、不改返回体;out.detail.talk 是原桶(logs.js 的 dropTalk 剥的是副本),这里不再剥。
|
|
150
|
-
try { recordAgentLoop({ reqId, model: actual, via, hops, body, out, sessionKey: handlerCtx?.sessionId, clientIp: handlerCtx?.clientIp, echo: upstreamEcho(upRes), attemptMs, relayMs: out.totalMs }); } catch {}
|
|
152
|
+
try { recordAgentLoop({ reqId, api, model: actual, via, hops, body, out, sessionKey: handlerCtx?.sessionId, clientIp: handlerCtx?.clientIp, echo: upstreamEcho(upRes), attemptMs, relayMs: out.totalMs }); } catch {}
|
|
151
153
|
|
|
152
154
|
// 5a0. 空转 200:流正常结束但零正文零工具调用 → 客户端会报 EMPTY_MODEL_RESPONSE
|
|
153
155
|
// ("The model ended its turn without producing any output");转 failover 而不是
|
|
@@ -181,9 +183,6 @@ export function createRelayPipeline({
|
|
|
181
183
|
return { handled: false, upRes: null, lastErr: { model: actual, upstream: null, status: 502, message: _errMsg, emptyTurn: true, heldOpen: out.heldOpen === true } };
|
|
182
184
|
}
|
|
183
185
|
|
|
184
|
-
// 上报锚点偏移:relay 入口之前消耗掉的等待(上游排队、建连、provider 为取判决预读的首帧)。
|
|
185
|
-
// 只加给 usage 行的时长;闸门计时与 recordModelStats 一律不碰 —— 见 design D2 与 Non-Goals。
|
|
186
|
-
const anchorMs = Number.isFinite(out?.preflightMs) && out.preflightMs > 0 ? out.preflightMs : 0;
|
|
187
186
|
// 5a. 首块超时未写字节 → 回退(显式 timedOut 字段,status 只是 HTTP 语义展示)
|
|
188
187
|
if (out.timedOut === true) {
|
|
189
188
|
const why = out.detail?.upstreamError ? ` (upstream read error: ${out.detail.upstreamError})` : "";
|
package/src/routes/index.js
CHANGED
|
@@ -7,6 +7,7 @@ import { heartbeatHandler, pollHandler, resultHandler, forwardHandler, streamHan
|
|
|
7
7
|
import { modelsHandler, modelsStatusHandler, providerModelsHandler, capabilitiesHandler } from "./models-route.js";
|
|
8
8
|
import { relayHandler } from "./relay.js";
|
|
9
9
|
import { responsesHandler } from "./responses-route.js";
|
|
10
|
+
import { messagesHandler, countTokensHandler } from "./messages-route.js";
|
|
10
11
|
|
|
11
12
|
export function createRouter({ token, upstream, models, auto, logs, peers, maxHops = DEFAULT_MAX_HOPS, groups, bans, bus, plugins }) {
|
|
12
13
|
return async function router(req, res) {
|
|
@@ -50,6 +51,18 @@ const ROUTES = [
|
|
|
50
51
|
requiresAuth: true,
|
|
51
52
|
handler: responsesHandler,
|
|
52
53
|
},
|
|
54
|
+
{
|
|
55
|
+
method: "POST",
|
|
56
|
+
path: "/v1/messages",
|
|
57
|
+
requiresAuth: true,
|
|
58
|
+
handler: messagesHandler,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
method: "POST",
|
|
62
|
+
path: "/v1/messages/count_tokens",
|
|
63
|
+
requiresAuth: true,
|
|
64
|
+
handler: countTokensHandler,
|
|
65
|
+
},
|
|
53
66
|
{
|
|
54
67
|
method: "POST",
|
|
55
68
|
path: "/v1/groups/join",
|
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* POST /v1/messages + /v1/messages/count_tokens — 给 Claude Code 用的 Anthropic Messages 外壳(ADR-0047)。
|
|
3
|
+
* 复用 ChatPipeline 全链路(auto/hedge/failover/tool_calls/空轮重试),只做形状翻译:
|
|
4
|
+
* 非流式:收集 chat JSON → 转 Anthropic message(上游只会流式时先把 SSE 聚合成 chat JSON);流式:逐块实时翻成 Anthropic 具名事件。
|
|
5
|
+
*
|
|
6
|
+
* 出站格式与 /v1/responses **不兼容**,故不能复用 createLiveForwarder(它写死 `data:` + `[DONE]`):
|
|
7
|
+
* Anthropic 用 `event: <type>` 具名帧、无 `[DONE]`,且需网关自发 `ping` 扛过客户端 idle watchdog。
|
|
8
|
+
*
|
|
9
|
+
* 三条收口纪律(第 1 轮评审 P0-2/P0-3 立下的,别改回去):
|
|
10
|
+
* 1. 上游一有动静(含 pipeline 的 keepalive 注释帧)就发头 + `message_start` + 武装 ping —— 首块前的静默期正是客户端 watchdog 咬人的窗口;
|
|
11
|
+
* 2. `headersSent`/`getHeader` 必须真反映已发头,否则 `helpers.js:32` 的「已 flush 走 in-band 错误帧」守卫永远判假,
|
|
12
|
+
* 错误会被当成普通文本喂进 push() 后静默消失;
|
|
13
|
+
* 3. 没等到 `finish_reason`(也没有 `[DONE]`)就收线 = 截断,发 `event: error`,**不伪装成功收场**。
|
|
14
|
+
*/
|
|
15
|
+
import { json, readBody } from "./helpers.js";
|
|
16
|
+
import { createChatPipeline } from "../chat-pipeline/index.js";
|
|
17
|
+
import { withBody, createCollector, createEmitter } from "./responses-route.js";
|
|
18
|
+
import {
|
|
19
|
+
messagesToChatBody,
|
|
20
|
+
chatJsonToAnthropic,
|
|
21
|
+
createAnthropicChunkTranslator,
|
|
22
|
+
estimateTokens,
|
|
23
|
+
anthropicError,
|
|
24
|
+
} from "../anthropic/translate.js";
|
|
25
|
+
import {
|
|
26
|
+
detectSearchRequest,
|
|
27
|
+
runWebSearch,
|
|
28
|
+
searchConfig,
|
|
29
|
+
buildSearchMessage,
|
|
30
|
+
searchEvents,
|
|
31
|
+
} from "../anthropic/web-search.js";
|
|
32
|
+
|
|
33
|
+
const ADEBUG = process.env.MSLXDFF_ANTHROPIC_DEBUG === "1";
|
|
34
|
+
function alog(...a) {
|
|
35
|
+
if (ADEBUG) console.log("[messages]", ...a);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// thinking 块外发:默认开,`MSLXDFF_ANTHROPIC_THINKING=0` 才关。外发的 thinking 块必须在封口前交一帧占位
|
|
39
|
+
// signature_delta(translate.js 的 thinkingSignature),缺它 Claude Code 整块丢弃 — ADR-0051 外发 / ADR-0052 签名
|
|
40
|
+
function emitThinking() { return process.env.MSLXDFF_ANTHROPIC_THINKING !== "0"; }
|
|
41
|
+
|
|
42
|
+
// 空闲 ping 周期(ms):`0` 关闭;客户端长思考期靠它扛过 streaming idle timeout
|
|
43
|
+
const PING_MS = (() => {
|
|
44
|
+
const raw = Number(process.env.MSLXDFF_ANTHROPIC_PING_MS);
|
|
45
|
+
return Number.isFinite(raw) && raw >= 0 ? raw : 15000;
|
|
46
|
+
})();
|
|
47
|
+
|
|
48
|
+
function frameOf(type, data) {
|
|
49
|
+
return `event: ${type}\ndata: ${JSON.stringify(data)}\n\n`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// pipeline 在已 flush 的流里用 OpenAI 形状收口(`{error:{...}}` + `[DONE]`):
|
|
53
|
+
// 检出后改发 Anthropic 的 `error` 帧,别把错误伪装成正常收场(grill-decisions Q2)
|
|
54
|
+
function detectInBandError(text) {
|
|
55
|
+
let msg = null;
|
|
56
|
+
for (const line of String(text || "").split("\n")) {
|
|
57
|
+
const t = line.trim();
|
|
58
|
+
if (!t.startsWith("data:")) continue;
|
|
59
|
+
const payload = t.slice(5).trim();
|
|
60
|
+
if (!payload || payload === "[DONE]") continue;
|
|
61
|
+
try {
|
|
62
|
+
const j = JSON.parse(payload);
|
|
63
|
+
const e = j?.error;
|
|
64
|
+
if (e) msg = String(e?.message || e).slice(0, 500);
|
|
65
|
+
} catch { /* 非 JSON 帧忽略 */ }
|
|
66
|
+
}
|
|
67
|
+
return msg;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
// pipeline 未 flush 时用 json() 直写聚合错误体(无 `data:` 前缀):认出来只取 message,
|
|
72
|
+
// 别把整坨 OpenAI JSON 当人话塞进 Anthropic error.message。
|
|
73
|
+
function jsonErrorText(text) {
|
|
74
|
+
const t = String(text || "").trim();
|
|
75
|
+
if (!t || t.startsWith(":") || t.includes("data:")) return null;
|
|
76
|
+
try {
|
|
77
|
+
const j = JSON.parse(t);
|
|
78
|
+
const e = j?.error;
|
|
79
|
+
if (!e) return null;
|
|
80
|
+
return String(e?.message || e).slice(0, 500);
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 两种上游收场形状(in-band `data:` 帧 / 聚合 JSON 体)统一取错误文案。 */
|
|
87
|
+
function errorText(text) {
|
|
88
|
+
return detectInBandError(text) || jsonErrorText(text);
|
|
89
|
+
}
|
|
90
|
+
// 客户端要流式而上游给了聚合 JSON(muse-spark / workbuddy 聚合 / zcode 非流式聚合都是现网常态):
|
|
91
|
+
// 那体没有 `data:` 前缀,会被 push() 当噪声整段丢掉 → 正文静默消失。认出来交给 fromChatJson 兜底。
|
|
92
|
+
function tryAggregate(text) {
|
|
93
|
+
const t = String(text || "").trim();
|
|
94
|
+
if (!t || t.includes("data:") || t.startsWith(":")) return null;
|
|
95
|
+
try {
|
|
96
|
+
const j = JSON.parse(t);
|
|
97
|
+
// 只认「非空 choices」:`{choices:[]}` 之类的空包什么也带不来,若当合法聚合包会跳过截断检测 → 零正文还谎报成功
|
|
98
|
+
return j && Array.isArray(j.choices) && j.choices.length > 0 ? j : null;
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
// 客户端要非流式而上游只会流式(workbuddy 在 provider 层写死 stream:true,SSE 硬吃回来):
|
|
104
|
+
// collector 收到的 text 是 SSE 帧序列而非 JSON,JSON.parse 必炸 → 此前恒 502。逐行吃 data: 帧
|
|
105
|
+
// 聚成 chat.completion JSON(与 qoder/aggregate.js、zcode 非流式聚合同构),出口复用 chatJsonToAnthropic;
|
|
106
|
+
// 一帧都没有或夹带 in-band 错误帧时返回 null,落回原有 502 + errorText 路径(错误绝不聚合成正文)。
|
|
107
|
+
function sseToChatJson(text, model) {
|
|
108
|
+
let content = "";
|
|
109
|
+
let finish = "";
|
|
110
|
+
let usage = null;
|
|
111
|
+
let sawChunk = false;
|
|
112
|
+
const toolSlots = new Map();
|
|
113
|
+
for (const raw of String(text || "").split("\n")) {
|
|
114
|
+
const line = raw.trim();
|
|
115
|
+
if (!line.startsWith("data:")) continue; // 注释帧/keepalive/事件名行全跳过
|
|
116
|
+
const payload = line.slice(5).trim();
|
|
117
|
+
if (!payload || payload === "[DONE]") continue;
|
|
118
|
+
let chunk;
|
|
119
|
+
try { chunk = JSON.parse(payload); } catch { continue; }
|
|
120
|
+
if (chunk?.error) return null;
|
|
121
|
+
sawChunk = true;
|
|
122
|
+
const choice = chunk.choices?.[0] || {};
|
|
123
|
+
const delta = choice.delta || {};
|
|
124
|
+
if (choice.finish_reason) finish = choice.finish_reason;
|
|
125
|
+
if (chunk.usage) usage = chunk.usage;
|
|
126
|
+
if (typeof delta.content === "string" && delta.content) content += delta.content;
|
|
127
|
+
for (const tc of delta.tool_calls || []) {
|
|
128
|
+
const slot = Number(tc?.index ?? 0);
|
|
129
|
+
let t = toolSlots.get(slot);
|
|
130
|
+
if (!t) { t = { id: "", name: "", args: "" }; toolSlots.set(slot, t); }
|
|
131
|
+
if (tc?.id) t.id = String(tc.id);
|
|
132
|
+
if (tc?.function?.name) t.name += String(tc.function.name);
|
|
133
|
+
if (tc?.function?.arguments) t.args += String(tc.function.arguments);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (!sawChunk) return null;
|
|
137
|
+
const toolCalls = [...toolSlots.values()].map((t, i) => ({ id: t.id || `call_${i}`, type: "function", function: { name: t.name, arguments: t.args || "{}" } }));
|
|
138
|
+
const message = { role: "assistant", content: content || (toolCalls.length ? null : "") };
|
|
139
|
+
if (toolCalls.length) message.tool_calls = toolCalls;
|
|
140
|
+
return { id: "", object: "chat.completion", created: Math.floor(Date.now() / 1000), model,
|
|
141
|
+
choices: [{ index: 0, message, finish_reason: finish || (toolCalls.length ? "tool_calls" : "stop") }],
|
|
142
|
+
usage: usage || { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 } };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Anthropic 出站垫片:具名事件 + 无 [DONE] + 空闲自发 ping + 真 headersSent。
|
|
148
|
+
* @param realRes 真 HTTP response
|
|
149
|
+
* @param translator createAnthropicChunkTranslator 实例
|
|
150
|
+
* @param opts { pingMs?: number } 测试注入口,缺省读 env
|
|
151
|
+
*/
|
|
152
|
+
export function createAnthropicForwarder(realRes, translator, opts = {}) {
|
|
153
|
+
const pingMs = opts.pingMs ?? PING_MS;
|
|
154
|
+
let status = 200;
|
|
155
|
+
let headSent = false;
|
|
156
|
+
let ended = false;
|
|
157
|
+
let timer = null;
|
|
158
|
+
let pendingError = null;
|
|
159
|
+
let aggregate = null;
|
|
160
|
+
let rawAcc = ""; // 攒住无事件字节,供聚合回落识别
|
|
161
|
+
const sent = () => headSent || realRes.headersSent === true; // headersSent 与 getHeader 的唯一口径
|
|
162
|
+
// 上游按字节粒度喂 Buffer/Uint8Array:非流式解码会把多字节汉字切成 U+FFFD 并永久固化在行缓冲里
|
|
163
|
+
const decoder = new TextDecoder("utf-8");
|
|
164
|
+
const decode = (c, flush) => {
|
|
165
|
+
if (c == null) return flush ? String(decoder.decode()) : "";
|
|
166
|
+
if (typeof c === "string") return c;
|
|
167
|
+
const bytes = (typeof Buffer !== "undefined" && Buffer.isBuffer(c)) || c instanceof Uint8Array ? c : null;
|
|
168
|
+
if (bytes) return decoder.decode(bytes, { stream: !flush });
|
|
169
|
+
return String(c);
|
|
170
|
+
};
|
|
171
|
+
const dead = () => ended || realRes.writableEnded === true || realRes.destroyed === true;
|
|
172
|
+
const raw = (type, data) => { if (dead()) return; try { realRes.write(frameOf(type, data)); } catch { /* 下游已断 */ } };
|
|
173
|
+
const stopPing = () => { if (timer) { clearTimeout(timer); timer = null; } };
|
|
174
|
+
const armPing = () => {
|
|
175
|
+
if (!pingMs || dead()) return;
|
|
176
|
+
stopPing();
|
|
177
|
+
timer = setTimeout(() => { if (dead()) return; raw("ping", { type: "ping" }); armPing(); }, pingMs);
|
|
178
|
+
if (typeof timer.unref === "function") timer.unref();
|
|
179
|
+
};
|
|
180
|
+
const sendHead = () => {
|
|
181
|
+
if (headSent) return;
|
|
182
|
+
headSent = true;
|
|
183
|
+
try { realRes.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", Connection: "keep-alive" }); } catch { /* 已发 */ }
|
|
184
|
+
for (const e of translator.begin()) raw(e.type, e);
|
|
185
|
+
armPing();
|
|
186
|
+
};
|
|
187
|
+
const finish = () => { if (ended) return; ended = true; stopPing(); try { realRes.end(); } catch { /* ignore */ } };
|
|
188
|
+
// 错误收场:一帧 error 后关连接,不补 message_delta/message_stop(真 Anthropic API 即此行为)
|
|
189
|
+
const closeError = (msg) => { sendHead(); raw("error", { type: "error", error: { type: "api_error", message: String(msg || "upstream error").slice(0, 500) } }); finish(); };
|
|
190
|
+
|
|
191
|
+
// 整包聚合体可能被分多次 write:攒住「没产出任何事件」的字节再判一次(有事件即清空,防陈旧拼接)
|
|
192
|
+
const RAW_ACC_CAP = 262144;
|
|
193
|
+
let rawOverflow = false;
|
|
194
|
+
const tryAccumulate = (text) => {
|
|
195
|
+
if (aggregate || !text || rawOverflow) return;
|
|
196
|
+
rawAcc += text;
|
|
197
|
+
if (rawAcc.length > RAW_ACC_CAP) {
|
|
198
|
+
// 超上限就停手并留标记:继续攒也拼不出完整 JSON,静默攒下去只会让错误文案说谎
|
|
199
|
+
rawAcc = rawAcc.slice(0, RAW_ACC_CAP);
|
|
200
|
+
rawOverflow = true;
|
|
201
|
+
}
|
|
202
|
+
const agg = tryAggregate(rawAcc);
|
|
203
|
+
if (agg) aggregate = agg;
|
|
204
|
+
};
|
|
205
|
+
const res = {
|
|
206
|
+
...createEmitter(),
|
|
207
|
+
set statusCode(v) { status = v; },
|
|
208
|
+
get statusCode() { return status; },
|
|
209
|
+
// helpers.js 的 json() 靠 headersSent + getHeader("content-type") 判「能不能回头」;
|
|
210
|
+
// 两者必须同源说真话,否则会出现「判成已 flush、却又拿不到 event-stream」的全静默窗口(错误既不 in-band 也不 JSON)。
|
|
211
|
+
get headersSent() { return sent(); },
|
|
212
|
+
// json() 的幂等早退判据;补上它才不会在我们已收场后再被当「可写」的响应对待
|
|
213
|
+
get writableEnded() { return ended || realRes.writableEnded === true; },
|
|
214
|
+
setHeader() { /* Anthropic SSE 的头由 sendHead 统一发,逐键 set 无意义 */ },
|
|
215
|
+
getHeader(name) {
|
|
216
|
+
if (String(name).toLowerCase() === "content-type" && sent()) return "text/event-stream";
|
|
217
|
+
return undefined;
|
|
218
|
+
},
|
|
219
|
+
write(c) {
|
|
220
|
+
if (ended) { alog("late-write-ignored", JSON.stringify({ id: translator.id, bytes: String(c?.length ?? 0) })); return true; }
|
|
221
|
+
const text = decode(c);
|
|
222
|
+
const err = errorText(text);
|
|
223
|
+
if (err) { pendingError = err; return true; } // 留给 end() 统一收口,不在这里半截发
|
|
224
|
+
// 上游一有动静(连 pipeline 的 keepalive 注释帧都算)就落头 + message_start + 武装 ping:
|
|
225
|
+
// 首块前的静默期正是客户端 idle watchdog 咬人的窗口。没收到任何字节时错误仍走干净 JSON(见 end())。
|
|
226
|
+
sendHead();
|
|
227
|
+
const evs = translator.push(text);
|
|
228
|
+
for (const e of evs) raw(e.type, e);
|
|
229
|
+
if (evs.length) rawAcc = ""; else tryAccumulate(text);
|
|
230
|
+
armPing();
|
|
231
|
+
return true;
|
|
232
|
+
},
|
|
233
|
+
end(c) {
|
|
234
|
+
if (ended) return;
|
|
235
|
+
const tail = decode(c, true);
|
|
236
|
+
const err = pendingError || errorText(tail);
|
|
237
|
+
if (status >= 400 && !headSent) {
|
|
238
|
+
ended = true;
|
|
239
|
+
stopPing();
|
|
240
|
+
json(realRes, status, anthropicError("api_error", err || String(tail).slice(0, 500) || "upstream error"));
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
if (err) { closeError(err); return; }
|
|
244
|
+
// 客户端要的是流:先落头 + message_start + 武装 ping,再处理残余
|
|
245
|
+
sendHead();
|
|
246
|
+
if (tail) {
|
|
247
|
+
const evs = translator.push(tail);
|
|
248
|
+
for (const e of evs) raw(e.type, e);
|
|
249
|
+
if (evs.length) rawAcc = ""; else tryAccumulate(tail);
|
|
250
|
+
}
|
|
251
|
+
// 末帧常常没有收尾换行:补一个 `\n` 把残行逼出来,否则合法正文与 finish_reason 会一起烂在缓冲里(被误判成截断)
|
|
252
|
+
for (const e of translator.push("\n")) raw(e.type, e);
|
|
253
|
+
// 上游整包聚合(无 SSE 帧):喂同一套状态机兜底,别让正文静默消失
|
|
254
|
+
if (aggregate && !translator.stats().blocks) { for (const e of translator.fromChatJson(aggregate)) raw(e.type, e); }
|
|
255
|
+
const fin = translator.getFinal();
|
|
256
|
+
if (!fin.finish && !aggregate) {
|
|
257
|
+
alog("truncated", JSON.stringify({ id: translator.id, ...translator.stats() }));
|
|
258
|
+
closeError(rawOverflow
|
|
259
|
+
? "upstream sent an oversized non-SSE body that could not be aggregated (no finish_reason)"
|
|
260
|
+
: "upstream stream ended before the model finished its turn (no finish_reason)");
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
for (const e of translator.end({ finish: fin.finish, usage: fin.usage })) raw(e.type, e);
|
|
264
|
+
finish();
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
// 客户端提前断开:pipeline 会感知 close,但 ping 定时器归本垫片管,必须自己停
|
|
268
|
+
try { realRes.on?.("close", () => { if (!ended) { ended = true; stopPing(); } }); } catch { /* ignore */ }
|
|
269
|
+
return res;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* 代跑结果回给客户端:非流式整包 JSON,流式由同一份块生成的具名事件(无 `[DONE]`)。
|
|
274
|
+
* 搜索在调用前就跑完了,这里一次写完即可,不需要 createAnthropicForwarder 的 ping 垫片。
|
|
275
|
+
*/
|
|
276
|
+
function respondSearch(res, { message, stream, provider }, isDead) {
|
|
277
|
+
if (isDead && isDead()) return undefined; // 搜索期间客户端断连:别再写任何字节
|
|
278
|
+
if (!stream) {
|
|
279
|
+
try { res.setHeader?.("x-mslxdff-web-search", provider); } catch { /* 已 flush */ }
|
|
280
|
+
try { return json(res, 200, message); } catch { return undefined; /* 下游已断 */ }
|
|
281
|
+
}
|
|
282
|
+
try {
|
|
283
|
+
res.writeHead(200, {
|
|
284
|
+
"Content-Type": "text/event-stream",
|
|
285
|
+
"Cache-Control": "no-cache",
|
|
286
|
+
Connection: "keep-alive",
|
|
287
|
+
"x-mslxdff-web-search": provider,
|
|
288
|
+
});
|
|
289
|
+
for (const e of searchEvents(message)) res.write(frameOf(e.type, e));
|
|
290
|
+
res.end();
|
|
291
|
+
} catch { /* 下游已断 */ }
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export async function messagesHandler(ctx) {
|
|
296
|
+
const { req, res } = ctx;
|
|
297
|
+
const t0 = Date.now();
|
|
298
|
+
let body;
|
|
299
|
+
try {
|
|
300
|
+
body = await readBody(req);
|
|
301
|
+
} catch {
|
|
302
|
+
return json(res, 400, anthropicError("invalid_request_error", "Invalid JSON body"));
|
|
303
|
+
}
|
|
304
|
+
// —— 网关代跑 web_search(ADR-0049):旁路请求**一律由网关作答**——命中判据即不打模型。
|
|
305
|
+
// 搜到就回结果块;关了/全挂/空查询就回一条明确的 400(把无工具的旁路丢给上游,只会让模型凭空
|
|
306
|
+
// 编 URL 并让客户端按「0 次搜索」空转重试,那才是改前的老毛病)。主循环请求不受影响。
|
|
307
|
+
// 整段包 try:搜索期最长 15s,其间客户端可能已断连(`isDead`),任何意外都不得逃逸成 5xx。
|
|
308
|
+
const det = detectSearchRequest(body);
|
|
309
|
+
if (det.hit) {
|
|
310
|
+
const isDead = () => res.writableEnded === true || res.destroyed === true || res.headersSent === true;
|
|
311
|
+
const wsCfg = searchConfig();
|
|
312
|
+
let r;
|
|
313
|
+
if (!wsCfg.enabled) r = { ok: false, error: "disabled by MSLXDFF_WEB_SEARCH=off" };
|
|
314
|
+
else {
|
|
315
|
+
try {
|
|
316
|
+
r = det.query ? await runWebSearch(det.query, wsCfg) : { ok: false, error: "empty query" };
|
|
317
|
+
} catch (e) {
|
|
318
|
+
r = { ok: false, error: String(e?.message || e) };
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (r.ok) {
|
|
322
|
+
const readout = { provider: r.provider, ms: r.ms, results: r.results.length, queryChars: r.query.length, chain: r.chain || [] };
|
|
323
|
+
alog("web-search", JSON.stringify(readout));
|
|
324
|
+
try { ctx.logs?.appendEvent?.({ type: "web-search", ...readout }); } catch { /* 观测产物不参与判决 */ }
|
|
325
|
+
return respondSearch(res, {
|
|
326
|
+
message: buildSearchMessage({ model: String(body.model || ""), query: r.query, results: r.results }),
|
|
327
|
+
stream: Boolean(body.stream),
|
|
328
|
+
provider: r.provider,
|
|
329
|
+
}, isDead);
|
|
330
|
+
}
|
|
331
|
+
const why = String(r.error || "unknown").slice(0, 200);
|
|
332
|
+
alog("web-search-unavailable", why);
|
|
333
|
+
try { ctx.logs?.appendEvent?.({ type: "web-search-unavailable", queryChars: (det.query || "").length, error: why, chain: r.chain || [] }); } catch { /* 同上 */ }
|
|
334
|
+
if (isDead()) return undefined;
|
|
335
|
+
try { return json(res, 400, anthropicError("invalid_request_error", `web search failed at the gateway: ${why}`)); } catch { return undefined; }
|
|
336
|
+
}
|
|
337
|
+
let chatBody;
|
|
338
|
+
try {
|
|
339
|
+
chatBody = messagesToChatBody(body);
|
|
340
|
+
} catch (e) {
|
|
341
|
+
alog("translate-req-400", String(e?.message || e));
|
|
342
|
+
return json(res, 400, anthropicError("invalid_request_error", String(e?.message || e)));
|
|
343
|
+
}
|
|
344
|
+
alog("req", JSON.stringify({
|
|
345
|
+
model: chatBody.model, stream: chatBody.stream,
|
|
346
|
+
msgs: chatBody.messages.map((m) => `${m.role}:${(typeof m.content === "string" ? m.content : "[parts]").length}${m.tool_calls ? `+${m.tool_calls.length}tc` : ""}`),
|
|
347
|
+
tools: Array.isArray(chatBody.tools) ? chatBody.tools.length : 0,
|
|
348
|
+
}));
|
|
349
|
+
const pipeline = createChatPipeline(ctx);
|
|
350
|
+
const fakeReq = withBody(req, chatBody);
|
|
351
|
+
const forwardClose = (shim) => { try { req.on?.("close", () => shim.emit("close")); } catch { /* ignore */ } };
|
|
352
|
+
try {
|
|
353
|
+
if (chatBody.stream) {
|
|
354
|
+
const translator = createAnthropicChunkTranslator(chatBody.model, { thinking: emitThinking() });
|
|
355
|
+
const live = createAnthropicForwarder(res, translator);
|
|
356
|
+
forwardClose(live);
|
|
357
|
+
await pipeline.execute({ req: fakeReq, res: live });
|
|
358
|
+
alog("done-stream", JSON.stringify({ ms: Date.now() - t0, id: translator.id, ...translator.stats(), ...translator.getFinal() }));
|
|
359
|
+
} else {
|
|
360
|
+
const cap = createCollector();
|
|
361
|
+
forwardClose(cap.res);
|
|
362
|
+
await pipeline.execute({ req: fakeReq, res: cap.res });
|
|
363
|
+
const { status, text } = cap.get();
|
|
364
|
+
if (status >= 400) return json(res, status, anthropicError("api_error", errorText(text) || String(text).slice(0, 500)));
|
|
365
|
+
let chatJson = null;
|
|
366
|
+
try { chatJson = JSON.parse(text); } catch { /* 非 JSON */ }
|
|
367
|
+
// 上游只会流式(workbuddy 写死 stream:true):非流式收到的 text 是 SSE → 聚成 chat JSON 再走同一翻译出口;
|
|
368
|
+
// 空流/错误帧聚合不出 JSON 时仍落回下方原 502 路径。
|
|
369
|
+
if (!chatJson) chatJson = sseToChatJson(text, chatBody.model);
|
|
370
|
+
if (!chatJson || chatJson.error || chatJson.object === "error") {
|
|
371
|
+
return json(res, status >= 400 ? status : 502, anthropicError("api_error", errorText(text) || String(text).slice(0, 500)));
|
|
372
|
+
}
|
|
373
|
+
// 上游偶发非 chat 形状(无 choices):包成单 text 块,绝不 500(照 responses-route.js 先例)
|
|
374
|
+
if (!Array.isArray(chatJson.choices)) {
|
|
375
|
+
const r = chatJsonToAnthropic({ choices: [{ message: { content: String(text).slice(0, 8000) } }] }, chatBody.model);
|
|
376
|
+
alog("done-nonchat", JSON.stringify({ ms: Date.now() - t0, bytes: text.length }));
|
|
377
|
+
return json(res, 200, r);
|
|
378
|
+
}
|
|
379
|
+
const out = chatJsonToAnthropic(chatJson, chatBody.model);
|
|
380
|
+
alog("done-json", JSON.stringify({ ms: Date.now() - t0, status, blocks: out.content.length, stop: out.stop_reason, usage: out.usage }));
|
|
381
|
+
return json(res, 200, out);
|
|
382
|
+
}
|
|
383
|
+
} catch (err) {
|
|
384
|
+
alog("execute-throw", String(err?.message || err).slice(0, 300));
|
|
385
|
+
if (!res.headersSent) return json(res, 502, anthropicError("api_error", String(err?.message || err)));
|
|
386
|
+
try { res.end(); } catch { /* ignore */ }
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
export async function countTokensHandler(ctx) {
|
|
391
|
+
const { req, res } = ctx;
|
|
392
|
+
let body;
|
|
393
|
+
try {
|
|
394
|
+
body = await readBody(req);
|
|
395
|
+
} catch {
|
|
396
|
+
return json(res, 400, anthropicError("invalid_request_error", "Invalid JSON body"));
|
|
397
|
+
}
|
|
398
|
+
const out = estimateTokens(body);
|
|
399
|
+
alog("count-tokens", JSON.stringify({ model: body?.model, input_tokens: out.input_tokens }));
|
|
400
|
+
return json(res, 200, out);
|
|
401
|
+
}
|
|
@@ -8,7 +8,7 @@ import { responsesToChatBody, chatJsonToResponse, createChunkTranslator, chunkTo
|
|
|
8
8
|
* 非流式:收集 chat JSON → 转 Response 对象;流式:逐块实时翻成 responses SSE。
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
function withBody(req, body) {
|
|
11
|
+
export function withBody(req, body) {
|
|
12
12
|
return Object.assign(Object.create(Object.getPrototypeOf(req)), req, { body });
|
|
13
13
|
}
|
|
14
14
|
|
|
@@ -22,7 +22,7 @@ function errorShape(text, status) {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
// 最小事件发射器:pipeline 靠 res.on("close") 感知下游断开,垫片必须有
|
|
25
|
-
function createEmitter() {
|
|
25
|
+
export function createEmitter() {
|
|
26
26
|
const map = new Map();
|
|
27
27
|
const self = {
|
|
28
28
|
on(ev, fn) { if (typeof fn === "function") { if (!map.has(ev)) map.set(ev, []); map.get(ev).push(fn); } return self; },
|
package/src/runtime/bootstrap.js
CHANGED
|
@@ -38,6 +38,11 @@ export async function startDaemonMain(VERSION) {
|
|
|
38
38
|
try { logs.appendEvent({ ts: Date.now(), type: "qoder-checkin-setup-failed", error: String(e?.message || e).slice(0, 200) }); } catch {}
|
|
39
39
|
});
|
|
40
40
|
|
|
41
|
+
const { setupRaccoonCheckin } = await import("./raccoon-checkin.js");
|
|
42
|
+
void setupRaccoonCheckin({ bus, logs }).catch((e) => {
|
|
43
|
+
try { logs.appendEvent({ ts: Date.now(), type: "raccoon-checkin-setup-failed", error: String(e?.message || e).slice(0, 200) }); } catch {}
|
|
44
|
+
});
|
|
45
|
+
|
|
41
46
|
const { setupWorkbuddyGrowth } = await import("./workbuddy-growth.js");
|
|
42
47
|
void setupWorkbuddyGrowth({ bus, logs }).catch((e) => {
|
|
43
48
|
try { logs.appendEvent({ ts: Date.now(), type: "workbuddy-growth-setup-failed", error: String(e?.message || e).slice(0, 200) }); } catch {}
|