mslxdff 0.1.203 → 0.1.207

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.
Files changed (330) hide show
  1. package/README.md +3 -3
  2. package/bin/mslxdff.js +1 -1
  3. package/docs/cli_help_mini.md +146 -0
  4. package/package.json +38 -38
  5. package/plugins/README.md +14 -14
  6. package/plugins/prefer-model.mjs.example +14 -14
  7. package/src/anthropic/canonical-json.js +55 -0
  8. package/src/anthropic/count-tokens.js +61 -61
  9. package/src/anthropic/request-body.js +183 -126
  10. package/src/anthropic/request-shape.js +60 -0
  11. package/src/anthropic/session-id.js +58 -0
  12. package/src/anthropic/shared.js +18 -18
  13. package/src/anthropic/sse-aggregate.js +56 -56
  14. package/src/anthropic/tool-media.js +75 -0
  15. package/src/anthropic/translate.js +296 -287
  16. package/src/anthropic/web-search.js +361 -361
  17. package/src/api-kind.js +31 -31
  18. package/src/autostart.js +288 -288
  19. package/src/bench/probe.js +1 -1
  20. package/src/bench/runner.js +1 -1
  21. package/src/bench/via-probe.js +1 -1
  22. package/src/bench/via.js +144 -144
  23. package/src/bench/workbuddy-bench.js +1 -1
  24. package/src/chat/config.js +11 -11
  25. package/src/chat/cooling.js +81 -81
  26. package/src/chat/direct.js +58 -58
  27. package/src/chat/index.js +6 -6
  28. package/src/chat/spinner.js +38 -38
  29. package/src/chat/sse.js +69 -69
  30. package/src/chat/stats.js +247 -247
  31. package/src/chat/store.js +48 -48
  32. package/src/chat/tool-handlers.js +67 -67
  33. package/src/chat/tools.js +1 -1
  34. package/src/chat/upstream.js +59 -59
  35. package/src/chat-pipeline/auto-race.js +158 -158
  36. package/src/chat-pipeline/empty-turn.js +147 -147
  37. package/src/chat-pipeline/engine.js +17 -17
  38. package/src/chat-pipeline/index.js +8 -40
  39. package/src/chat-pipeline/serial-trial.js +279 -283
  40. package/src/chat-pipeline/terminal-verdict.js +132 -147
  41. package/src/chooser.js +54 -54
  42. package/src/claude-desktop/apply.js +101 -101
  43. package/src/claude-desktop/context1m.js +38 -38
  44. package/src/claude-desktop/doctor.js +146 -146
  45. package/src/claude-desktop/slots.js +152 -152
  46. package/src/cli/bootstrap.js +1 -1
  47. package/src/cli/commands/claude-desktop.js +284 -284
  48. package/src/cli/commands/daemon.js +231 -231
  49. package/src/cli/commands/model/list-live.js +22 -22
  50. package/src/cli/commands/model/list-providers.js +76 -76
  51. package/src/cli/commands/model/list-render.js +77 -77
  52. package/src/cli/commands/model/list-sort.js +52 -52
  53. package/src/cli/commands/model/list-tty.js +76 -76
  54. package/src/cli/commands/model/list.js +166 -166
  55. package/src/cli/commands/model/live-models.js +24 -24
  56. package/src/cli/commands/model/stats.js +43 -43
  57. package/src/cli/commands/model/status.js +47 -47
  58. package/src/cli/commands/model.js +51 -51
  59. package/src/cli/commands/provider/add.js +108 -108
  60. package/src/cli/commands/provider/allowlist.js +99 -99
  61. package/src/cli/commands/provider/cline-free.js +99 -99
  62. package/src/cli/commands/provider/cline-login.js +218 -218
  63. package/src/cli/commands/provider/cline-quota.js +65 -65
  64. package/src/cli/commands/provider/codearts-login.js +45 -45
  65. package/src/cli/commands/provider/config.js +119 -119
  66. package/src/cli/commands/provider/globalqwenwork-login.js +126 -126
  67. package/src/cli/commands/provider/keys-guard.js +50 -50
  68. package/src/cli/commands/provider/keys.js +161 -161
  69. package/src/cli/commands/provider/qoder-checkin.js +81 -81
  70. package/src/cli/commands/provider/qoder-login.js +102 -102
  71. package/src/cli/commands/provider/raccoon-login.js +180 -180
  72. package/src/cli/commands/provider/raccoon-password.js +162 -162
  73. package/src/cli/commands/provider/raccoon-quota.js +109 -109
  74. package/src/cli/commands/provider/traework-checkin.js +144 -144
  75. package/src/cli/commands/provider/traework-login.js +164 -164
  76. package/src/cli/commands/provider/workbuddy-login.js +153 -153
  77. package/src/cli/commands/provider/zcode-login.js +77 -77
  78. package/src/cli/commands/provider/zcode-quota.js +55 -55
  79. package/src/cli/commands/provider.js +1 -1
  80. package/src/cli/commands/stats.js +158 -149
  81. package/src/cli/commands/system.js +261 -261
  82. package/src/cli/commands/timezone.js +47 -47
  83. package/src/cli/commands/use-group.js +60 -60
  84. package/src/cli/commands/workbuddy.js +189 -189
  85. package/src/cli/format.js +141 -141
  86. package/src/cli/group-helpers.js +70 -70
  87. package/src/cli/help.js +80 -80
  88. package/src/cli/index.js +70 -70
  89. package/src/cli/interactive.js +121 -121
  90. package/src/cli/join-core.js +99 -99
  91. package/src/cli/join-wizard.js +175 -175
  92. package/src/cli/policy.js +136 -136
  93. package/src/cli/provider-row.js +126 -126
  94. package/src/cli/util.js +24 -24
  95. package/src/compat.js +53 -53
  96. package/src/events.js +38 -38
  97. package/src/free-lane.js +131 -131
  98. package/src/free-watcher.js +63 -63
  99. package/src/groups.js +262 -262
  100. package/src/logs.js +195 -195
  101. package/src/metrics.js +71 -62
  102. package/src/model-capabilities/context-lookup.js +65 -65
  103. package/src/model-capabilities/merge.js +142 -142
  104. package/src/model-trace.js +187 -187
  105. package/src/models.js +1 -1
  106. package/src/opencode-identity.js +72 -72
  107. package/src/peers.js +254 -254
  108. package/src/plugins.js +107 -107
  109. package/src/providers/AGENTS.md +49 -0
  110. package/src/providers/base.js +164 -164
  111. package/src/providers/classify.js +22 -22
  112. package/src/providers/cline/allowlist-sync.js +93 -93
  113. package/src/providers/cline/free-catalog.js +71 -71
  114. package/src/providers/cline/headers.js +37 -37
  115. package/src/providers/cline/index.js +130 -130
  116. package/src/providers/cline/models.js +150 -150
  117. package/src/providers/cline/usage.js +274 -274
  118. package/src/providers/codearts/auth-pool.js +148 -148
  119. package/src/providers/codearts/concurrency.js +34 -34
  120. package/src/providers/codearts/const.js +65 -65
  121. package/src/providers/codearts/dpop.js +95 -95
  122. package/src/providers/codearts/index.js +175 -175
  123. package/src/providers/codearts/login.js +214 -214
  124. package/src/providers/codearts/models.js +182 -182
  125. package/src/providers/codearts/sign.js +96 -96
  126. package/src/providers/codearts/sse.js +204 -204
  127. package/src/providers/codearts/stream.js +209 -209
  128. package/src/providers/codearts/sts.js +124 -124
  129. package/src/providers/codearts.js +1 -1
  130. package/src/providers/dispatcher.js +146 -146
  131. package/src/providers/generic.js +156 -156
  132. package/src/providers/globalqwenwork/account-store.js +141 -141
  133. package/src/providers/globalqwenwork/constants.js +73 -73
  134. package/src/providers/globalqwenwork/crypto.js +2 -2
  135. package/src/providers/globalqwenwork/http.js +2 -2
  136. package/src/providers/globalqwenwork/index.js +331 -331
  137. package/src/providers/globalqwenwork/payload.js +147 -147
  138. package/src/providers/globalqwenwork/rsa.js +2 -2
  139. package/src/providers/globalqwenwork/sse.js +2 -2
  140. package/src/providers/globalqwenwork/stream.js +2 -2
  141. package/src/providers/globalqwenwork.js +1 -1
  142. package/src/providers/globalworkbuddy/account-store.js +198 -198
  143. package/src/providers/globalworkbuddy/auth.js +97 -97
  144. package/src/providers/globalworkbuddy/catalog.js +138 -138
  145. package/src/providers/globalworkbuddy/constants.js +73 -73
  146. package/src/providers/globalworkbuddy/device-auth.js +120 -120
  147. package/src/providers/globalworkbuddy/errors.js +98 -98
  148. package/src/providers/globalworkbuddy/headers.js +89 -89
  149. package/src/providers/globalworkbuddy/models.js +96 -96
  150. package/src/providers/globalworkbuddy/payload.js +80 -80
  151. package/src/providers/index.js +5 -5
  152. package/src/providers/model-id.js +172 -172
  153. package/src/providers/opencode.js +19 -19
  154. package/src/providers/openrouter.js +31 -31
  155. package/src/providers/qoder/account-store.js +106 -106
  156. package/src/providers/qoder/aggregate.js +60 -60
  157. package/src/providers/qoder/baseprompt.json +591 -591
  158. package/src/providers/qoder/checkin.js +160 -160
  159. package/src/providers/qoder/constants.js +44 -44
  160. package/src/providers/qoder/encode.js +41 -41
  161. package/src/providers/qoder/fingerprint.js +38 -38
  162. package/src/providers/qoder/models.js +77 -77
  163. package/src/providers/qoder/oauth.js +84 -84
  164. package/src/providers/qoder/payload.js +162 -162
  165. package/src/providers/qoder/request.js +27 -27
  166. package/src/providers/qoder/session.js +134 -134
  167. package/src/providers/qoder/sticky.js +38 -38
  168. package/src/providers/qoder/stream.js +116 -116
  169. package/src/providers/qoder.js +1 -1
  170. package/src/providers/raccoon/account-store.js +216 -216
  171. package/src/providers/raccoon/auth.js +67 -67
  172. package/src/providers/raccoon/chat.js +228 -228
  173. package/src/providers/raccoon/const.js +180 -180
  174. package/src/providers/raccoon/credits.js +185 -185
  175. package/src/providers/raccoon/crypto.js +43 -43
  176. package/src/providers/raccoon/envelope.js +31 -31
  177. package/src/providers/raccoon/headers.js +45 -45
  178. package/src/providers/raccoon/index.js +216 -216
  179. package/src/providers/raccoon/login.js +279 -279
  180. package/src/providers/raccoon/models.js +160 -160
  181. package/src/providers/raccoon/password-login.js +102 -102
  182. package/src/providers/raccoon/qr-codewords.js +115 -115
  183. package/src/providers/raccoon/qr.js +236 -236
  184. package/src/providers/raccoon/sse.js +135 -135
  185. package/src/providers/responses-channel.js +120 -120
  186. package/src/providers/share-keys.js +53 -53
  187. package/src/providers/traework/account-store.js +234 -234
  188. package/src/providers/traework/chat.js +217 -217
  189. package/src/providers/traework/checkin.js +77 -77
  190. package/src/providers/traework/constants.js +41 -41
  191. package/src/providers/traework/errors.js +52 -52
  192. package/src/providers/traework/headers.js +79 -79
  193. package/src/providers/traework/index.js +132 -132
  194. package/src/providers/traework/market-identity.js +116 -116
  195. package/src/providers/traework/models.js +98 -98
  196. package/src/providers/traework/payload.js +86 -86
  197. package/src/providers/traework/sse.js +184 -184
  198. package/src/providers/traework/token.js +52 -52
  199. package/src/providers/traework.js +2 -2
  200. package/src/providers/workbuddy/account-store.js +186 -186
  201. package/src/providers/workbuddy/auth.js +158 -158
  202. package/src/providers/workbuddy/balance.js +86 -86
  203. package/src/providers/workbuddy/cat-travel.js +125 -125
  204. package/src/providers/workbuddy/checkin.js +75 -75
  205. package/src/providers/workbuddy/desktop-info.js +78 -78
  206. package/src/providers/workbuddy/growth-api.js +67 -67
  207. package/src/providers/workbuddy/growth-plans.js +63 -63
  208. package/src/providers/workbuddy/growth.js +181 -181
  209. package/src/providers/workbuddy/index.js +217 -217
  210. package/src/providers/workbuddy/models.js +126 -126
  211. package/src/providers/workbuddy/payload.js +141 -141
  212. package/src/providers/workbuddy/rotation-log.js +83 -83
  213. package/src/providers/workbuddy/sanitize-tools.js +61 -61
  214. package/src/providers/workbuddy/sdk-chat.js +26 -26
  215. package/src/providers/workbuddy-balance.js +78 -78
  216. package/src/providers/zcode/account-store.js +129 -129
  217. package/src/providers/zcode/auth.js +28 -28
  218. package/src/providers/zcode/chat.js +179 -179
  219. package/src/providers/zcode/const.js +54 -54
  220. package/src/providers/zcode/context-shape.js +162 -162
  221. package/src/providers/zcode/headers.js +93 -93
  222. package/src/providers/zcode/index.js +124 -124
  223. package/src/providers/zcode/models.js +65 -65
  224. package/src/providers/zcode/oauth.js +120 -120
  225. package/src/providers/zcode/quota.js +176 -176
  226. package/src/providers/zcode/sse.js +196 -196
  227. package/src/readline-compat.js +25 -25
  228. package/src/responses/translate.js +311 -311
  229. package/src/routes/AGENTS.md +37 -0
  230. package/src/routes/chat/broadband-handler.js +72 -72
  231. package/src/routes/chat/empty-turn-report.js +31 -31
  232. package/src/routes/chat/exhausted-handler.js +87 -87
  233. package/src/routes/chat/hedge-handler.js +130 -130
  234. package/src/routes/chat/index.js +1 -1
  235. package/src/routes/chat/local-handler.js +63 -63
  236. package/src/routes/chat/peer-handler.js +81 -81
  237. package/src/routes/chat.js +3 -3
  238. package/src/routes/fallback.js +88 -88
  239. package/src/routes/groups-relay.js +214 -214
  240. package/src/routes/groups.js +122 -122
  241. package/src/routes/helpers.js +93 -83
  242. package/src/routes/index.js +147 -147
  243. package/src/routes/messages-route.js +375 -365
  244. package/src/routes/peers.js +10 -2
  245. package/src/routes/relay-queue.js +181 -181
  246. package/src/routes/relay.js +60 -60
  247. package/src/routes/responses-route.js +213 -213
  248. package/src/routes/stream-body.js +84 -84
  249. package/src/routes/stream-hold.js +337 -337
  250. package/src/routes/stream-scan.js +366 -366
  251. package/src/routes.js +3 -3
  252. package/src/runtime/auto-update.js +113 -113
  253. package/src/runtime/broadband-stream.js +79 -79
  254. package/src/runtime/broadband.js +103 -103
  255. package/src/runtime/group-sync.js +81 -81
  256. package/src/runtime/keep-warm.js +120 -120
  257. package/src/runtime/lifecycle-forensics.js +116 -116
  258. package/src/runtime/lifecycle-log.js +87 -87
  259. package/src/runtime/log-stamp.js +73 -73
  260. package/src/runtime/proc-info.js +137 -137
  261. package/src/runtime/qoder-checkin.js +93 -93
  262. package/src/runtime/raccoon-checkin.js +126 -126
  263. package/src/runtime/server-lifecycle.js +189 -189
  264. package/src/runtime/traework-checkin.js +118 -118
  265. package/src/runtime/workbuddy-checkin.js +120 -120
  266. package/src/runtime/workbuddy-growth.js +133 -133
  267. package/src/state/facade.js +58 -58
  268. package/src/state/memory.js +162 -162
  269. package/src/state/merge.js +25 -25
  270. package/src/state/migrations/cline-unify.js +94 -94
  271. package/src/state/migrations.js +33 -33
  272. package/src/state/persist.js +42 -42
  273. package/src/state/provider-config.js +142 -142
  274. package/src/state/schemas/group.js +31 -31
  275. package/src/state/schemas/model.js +94 -94
  276. package/src/state/schemas/peer.js +31 -31
  277. package/src/state/schemas/port.js +10 -10
  278. package/src/state/schemas/provider.js +202 -202
  279. package/src/state/schemas/timezone.js +70 -70
  280. package/src/state/schemas/token.js +48 -48
  281. package/src/state/schemas/use-group.js +102 -102
  282. package/src/state/store.js +176 -176
  283. package/src/state.js +1 -1
  284. package/src/sync-claude-desktop.js +234 -234
  285. package/src/sync-claude.js +152 -152
  286. package/src/sync-codex.js +116 -116
  287. package/src/sync-workbuddy.js +158 -158
  288. package/src/talk-full-redact.js +129 -129
  289. package/src/talk-full.js +131 -131
  290. package/src/talk-log.js +257 -257
  291. package/src/text-tokens.js +52 -52
  292. package/src/thinking-effort.js +67 -67
  293. package/src/time.js +106 -106
  294. package/src/timeline.js +26 -26
  295. package/src/transport/index.js +1 -1
  296. package/src/upstream-engine/index.js +74 -74
  297. package/src/upstream-engine/mode.js +15 -15
  298. package/src/upstream-engine/sdk/attempt.js +221 -221
  299. package/src/upstream-engine/sdk/chat.js +28 -28
  300. package/src/upstream-engine/sdk/convert.js +192 -192
  301. package/src/upstream-engine/sdk/diagnose.js +62 -62
  302. package/src/upstream-engine/sdk/dispatch.js +55 -55
  303. package/src/upstream-engine/sdk/responses.js +176 -176
  304. package/src/upstream-engine/sdk/sse.js +166 -166
  305. package/src/upstream-responses.js +305 -305
  306. package/src/usage/cache-fields.js +58 -0
  307. package/src/usage/record.js +109 -106
  308. package/src/usage/report.js +209 -195
  309. package/src/v6pool/claim.js +157 -157
  310. package/src/v6pool/config.js +65 -65
  311. package/src/v6pool/detect.js +130 -130
  312. package/src/v6pool/index.js +152 -152
  313. package/src/v6pool/prefix.js +140 -140
  314. package/src/v6pool/probe.js +40 -40
  315. package/src/v6pool/report.js +104 -104
  316. package/src/v6pool/rotate.js +263 -263
  317. package/src/warp-pool/config.js +74 -74
  318. package/src/warp-pool/health.js +106 -106
  319. package/src/warp-pool/identity.js +141 -141
  320. package/src/warp-pool/index.js +358 -358
  321. package/src/warp-pool/keys.js +29 -29
  322. package/src/warp-pool/nodes.js +95 -95
  323. package/src/warp-pool/ports.js +160 -160
  324. package/src/warp-pool/register.js +113 -113
  325. package/src/warp-pool/report.js +232 -232
  326. package/src/warp-pool/revive.js +168 -168
  327. package/src/warp-pool/rotate.js +214 -214
  328. package/src/warp-pool/strategy.js +43 -43
  329. package/src/warp-pool/verdict.js +69 -69
  330. package/src/warp-pool/wireproxy.js +162 -162
@@ -1,213 +1,213 @@
1
- import { json, readBody } from "./helpers.js";
2
- import { createChatPipeline } from "../chat-pipeline/index.js";
3
- import { responsesToChatBody, chatJsonToResponse, createChunkTranslator, chunkToString } from "../responses/translate.js";
4
-
5
- /**
6
- * POST /v1/responses — 给 Codex/ChatGPT 桌面端用的 Responses API 薄壳。
7
- * 复用 ChatPipeline 全链路(auto/hedge/failover/tool_calls),只做形状翻译。
8
- * 非流式:收集 chat JSON → 转 Response 对象;流式:逐块实时翻成 responses SSE。
9
- */
10
-
11
- export function withBody(req, body) {
12
- return Object.assign(Object.create(Object.getPrototypeOf(req)), req, { body });
13
- }
14
-
15
- function errorShape(text, status) {
16
- let msg = String(text || "").slice(0, 500);
17
- try {
18
- const j = JSON.parse(String(text || ""));
19
- msg = String(j?.error?.message || j?.error || msg);
20
- } catch {}
21
- return { error: { message: msg || "upstream error", type: "server_error", code: status } };
22
- }
23
-
24
- // 最小事件发射器:pipeline 靠 res.on("close") 感知下游断开,垫片必须有
25
- export function createEmitter() {
26
- const map = new Map();
27
- const self = {
28
- on(ev, fn) { if (typeof fn === "function") { if (!map.has(ev)) map.set(ev, []); map.get(ev).push(fn); } return self; },
29
- once(ev, fn) { const w = (...a) => { self.off(ev, w); fn(...a); }; return self.on(ev, w); },
30
- off(ev, fn) { const l = map.get(ev); if (l) { const i = l.indexOf(fn); if (i >= 0) l.splice(i, 1); } return self; },
31
- removeListener(ev, fn) { return self.off(ev, fn); },
32
- emit(ev, ...a) { for (const fn of [...(map.get(ev) || [])]) { try { fn(...a); } catch {} } return true; },
33
- };
34
- return self;
35
- }
36
-
37
- // 非流式:收集捕获
38
- export function createCollector() {
39
- let status = 200;
40
- let text = "";
41
- const res = {
42
- ...createEmitter(),
43
- set statusCode(v) { status = v; },
44
- get statusCode() { return status; },
45
- headersSent: false,
46
- setHeader() {},
47
- write(c) { text += chunkToString(c); return true; },
48
- end(c) { if (c != null) text += chunkToString(c); res.headersSent = true; },
49
- };
50
- return { res, get: () => ({ status, text }) };
51
- }
52
-
53
- /**
54
- * 纯错误帧直通判定(垫片 write 的前置拦截):整段只含 `data:` 行、且每帧都是
55
- * `{error:{…}}` 形状(无 `choices`)时,返回原样 SSE 帧数组;否则返回 null(交翻译器)。
56
- * 为什么必须拦:翻译器只认 chat 帧(choices.delta),错误帧会被它静默 `continue` 掉。
57
- */
58
- function passthroughErrorFrames(text) {
59
- const src = String(text ?? "");
60
- if (!src.includes("data:")) return null;
61
- const out = [];
62
- let sawError = false;
63
- for (const rawLine of src.split("\n")) {
64
- const line = rawLine.trim();
65
- if (!line) continue;
66
- if (line.startsWith(":")) { out.push(`${line}\n\n`); continue; } // 注释帧照原样(翻译器也是跳过)
67
- if (!line.startsWith("data:")) return null;
68
- const payload = line.slice(5).trim();
69
- if (!payload) continue;
70
- if (payload === "[DONE]") { out.push("data: [DONE]\n\n"); continue; }
71
- let j;
72
- try { j = JSON.parse(payload); } catch { return null; } // 解析失败 = 不是我们的错误帧,交翻译器(它自会记 jsonFails)
73
- if (!j || typeof j !== "object" || Array.isArray(j) || j.choices) return null; // 有 choices = chat 帧,绝不拦
74
- if (!j.error) return null; // 无 choices 又无 error:形状未知,交翻译器
75
- sawError = true;
76
- out.push(`data: ${JSON.stringify(j)}\n\n`);
77
- }
78
- return sawError ? out : null;
79
- }
80
-
81
- // 流式:逐 write 实时翻译并转发(head 延迟到首块,避免错误状态码已发送)
82
- export function createLiveForwarder(realRes, translator) {
83
- let status = 200;
84
- let headSent = false;
85
- let ended = false;
86
- const sendHead = () => {
87
- if (headSent) return;
88
- headSent = true;
89
- realRes.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", Connection: "keep-alive" });
90
- for (const e of translator.begin()) realRes.write(`data: ${JSON.stringify(e)}\n\n`);
91
- };
92
- // headersSent 必须是**计算值**:垫片的 write() 会调 sendHead() 真发头,若 headersSent 仍是普通属性
93
- // (只在 end() 里置真),则「头已发但流未结束」这段窗口里它为假 ⇒ helpers.json() 会走 `!headersSent`
94
- // 分支去 setHeader+end(而本垫片 setHeader 是 no-op、且真头已发)⇒ 错误体静默丢失、只剩裸关流。
95
- // messages-route 的 createAnthropicForwarder 早已用 getter 实现并点名了同一个坑;本处是同契约的遗漏。
96
- // 口径与 messages-route 的 createAnthropicForwarder 逐字对齐(哪里算「头已发」只此一处判)。
97
- const sent = () => headSent || realRes.headersSent === true;
98
- const res = {
99
- ...createEmitter(),
100
- set statusCode(v) { status = v; },
101
- get statusCode() { return status; },
102
- get headersSent() { return sent(); },
103
- set headersSent(_v) { /* 兼容旧赋值点:真实状态由 headSent/ended 推导,不接受外部覆写 */ },
104
- getHeader(k) { return String(k).toLowerCase() === "content-type" && sent() ? "text/event-stream" : undefined; },
105
- setHeader() {},
106
- write(c) {
107
- if (ended) return true;
108
- sendHead();
109
- const text = chunkToString(c);
110
- // 纯错误帧直写:helpers.json() 的 in-band 收场写的是 OpenAI 形状 `{error:{…}}`(无 choices),
111
- // 若交给 chat→responses 翻译器,会被当作「非 chat 帧」静默忽略(translate.js 只认 choices.delta)——
112
- // 错误在翻译层被吞掉,客户端只看到流被关。错误帧与 chat 帧形状互斥,故这里拦下直通。
113
- const errOut = passthroughErrorFrames(text);
114
- if (errOut != null) { for (const chunk of errOut) realRes.write(chunk); return true; }
115
- for (const e of translator.push(text)) realRes.write(`data: ${JSON.stringify(e)}\n\n`);
116
- return true;
117
- },
118
- end(c) {
119
- if (ended) return;
120
- ended = true;
121
- res.headersSent = true;
122
- if (status >= 400 && !headSent) {
123
- json(realRes, status, errorShape(c, status));
124
- return;
125
- }
126
- sendHead();
127
- if (c != null) for (const e of translator.push(chunkToString(c))) realRes.write(`data: ${JSON.stringify(e)}\n\n`);
128
- const fin = translator.getFinal();
129
- for (const e of translator.end({ finish: fin.finish, usage: fin.usage })) realRes.write(`data: ${JSON.stringify(e)}\n\n`);
130
- realRes.write("data: [DONE]\n\n");
131
- try { realRes.end(); } catch {}
132
- },
133
- };
134
- return res;
135
- }
136
-
137
- const RDEBUG = process.env.MSLXDFF_RESPONSES_DEBUG === "1";
138
- function rlog(...a) {
139
- if (RDEBUG) console.log("[responses]", ...a);
140
- }
141
-
142
- export async function responsesHandler(ctx) {
143
- const { req, res } = ctx;
144
- const t0 = Date.now();
145
- let body;
146
- try {
147
- body = await readBody(req);
148
- } catch {
149
- return json(res, 400, { error: { message: "Invalid JSON body" } });
150
- }
151
- rlog("req", JSON.stringify({
152
- model: body?.model, stream: body?.stream,
153
- inputType: typeof body?.input, inputLen: Array.isArray(body?.input) ? body.input.length : String(body?.input || "").length,
154
- inputKinds: Array.isArray(body?.input) ? [...new Set(body.input.map((i) => i?.type))] : null,
155
- tools: Array.isArray(body?.tools) ? body.tools.length : 0,
156
- instructionsLen: String(body?.instructions || "").length,
157
- // 文本丢失定位:各 message item 的 content 形状(part 类型 + 文本长度 + 头部)
158
- contentShapes: Array.isArray(body?.input)
159
- ? body.input.filter((i) => i && (i.type === "message" || i.role)).map((m) => ({
160
- type: m.type ?? "(无type)", role: m.role,
161
- cType: Array.isArray(m.content) ? "array" : typeof m.content,
162
- parts: Array.isArray(m.content) ? m.content.map((p) => `${p?.type ?? "?"}:${typeof p?.text === "string" ? p.text.length : "-"}`).slice(0, 6) : null,
163
- textHead: typeof m.content === "string" ? m.content.slice(0, 50) : null,
164
- })).slice(0, 10)
165
- : null,
166
- }));
167
- let chatBody;
168
- try {
169
- chatBody = responsesToChatBody(body);
170
- } catch (e) {
171
- rlog("translate-req-400", String(e?.message || e));
172
- return json(res, 400, { error: { message: String(e?.message || e) } });
173
- }
174
- rlog("chat", JSON.stringify({ model: chatBody.model, stream: chatBody.stream, msgs: chatBody.messages.map((m) => `${m.role}:${String(m.content || "").length}${m.tool_calls ? `+${m.tool_calls.length}tc` : ""}`) }));
175
- const pipeline = createChatPipeline(ctx);
176
- const fakeReq = withBody(req, chatBody);
177
- // 真连接断开 → 透传给垫片,pipeline 的 abort 链路不断
178
- const forwardClose = (shim) => { try { req.on?.("close", () => shim.emit("close")); } catch {} };
179
- try {
180
- if (chatBody.stream) {
181
- const translator = createChunkTranslator(chatBody.model);
182
- const live = createLiveForwarder(res, translator);
183
- forwardClose(live);
184
- await pipeline.execute({ req: fakeReq, res: live });
185
- const st = translator.stats();
186
- rlog("done-stream", JSON.stringify({ ms: Date.now() - t0, model: chatBody.model, ...st, finish: translator.getFinal().finish, usage: translator.getFinal().usage }));
187
- } else {
188
- const cap = createCollector();
189
- forwardClose(cap.res);
190
- await pipeline.execute({ req: fakeReq, res: cap.res });
191
- const { status, text } = cap.get();
192
- rlog("done-json", JSON.stringify({ ms: Date.now() - t0, model: chatBody.model, status, bytes: text.length, head: text.slice(0, 160) }));
193
- if (status >= 400) return json(res, status, errorShape(text, status));
194
- let chatJson = null;
195
- try { chatJson = JSON.parse(text); } catch {}
196
- if (!chatJson || chatJson.object === "error" || chatJson.error) {
197
- rlog("translate-resp-502", text.slice(0, 300));
198
- return json(res, status >= 400 ? status : 502, errorShape(text, status));
199
- }
200
- const respObj = chatJsonToResponse(chatJson, chatBody.model);
201
- rlog("done-resp", JSON.stringify({ ms: Date.now() - t0, model: chatBody.model, status: respObj.status, items: respObj.output.length, textLen: JSON.stringify(respObj.output).length, usage: respObj.usage }));
202
- // 非 JSON 透传(上游偶发):包成纯文本 Response
203
- if (!chatJson.choices) {
204
- return json(res, 200, { id: chatJson.id || `resp_${Date.now()}`, object: "response", created_at: Math.floor(Date.now() / 1000), status: "completed", model: chatBody.model, output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: String(text).slice(0, 8000) }] }], usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 } });
205
- }
206
- return json(res, 200, respObj);
207
- }
208
- } catch (err) {
209
- rlog("execute-throw", String(err?.message || err).slice(0, 300));
210
- if (!res.headersSent) return json(res, 502, { error: { message: String(err?.message || err).slice(0, 500) } });
211
- try { res.end(); } catch {}
212
- }
213
- }
1
+ import { json, readBody } from "./helpers.js";
2
+ import { createChatPipeline } from "../chat-pipeline/index.js";
3
+ import { responsesToChatBody, chatJsonToResponse, createChunkTranslator, chunkToString } from "../responses/translate.js";
4
+
5
+ /**
6
+ * POST /v1/responses — 给 Codex/ChatGPT 桌面端用的 Responses API 薄壳。
7
+ * 复用 ChatPipeline 全链路(auto/hedge/failover/tool_calls),只做形状翻译。
8
+ * 非流式:收集 chat JSON → 转 Response 对象;流式:逐块实时翻成 responses SSE。
9
+ */
10
+
11
+ export function withBody(req, body) {
12
+ return Object.assign(Object.create(Object.getPrototypeOf(req)), req, { body });
13
+ }
14
+
15
+ function errorShape(text, status) {
16
+ let msg = String(text || "").slice(0, 500);
17
+ try {
18
+ const j = JSON.parse(String(text || ""));
19
+ msg = String(j?.error?.message || j?.error || msg);
20
+ } catch {}
21
+ return { error: { message: msg || "upstream error", type: "server_error", code: status } };
22
+ }
23
+
24
+ // 最小事件发射器:pipeline 靠 res.on("close") 感知下游断开,垫片必须有
25
+ export function createEmitter() {
26
+ const map = new Map();
27
+ const self = {
28
+ on(ev, fn) { if (typeof fn === "function") { if (!map.has(ev)) map.set(ev, []); map.get(ev).push(fn); } return self; },
29
+ once(ev, fn) { const w = (...a) => { self.off(ev, w); fn(...a); }; return self.on(ev, w); },
30
+ off(ev, fn) { const l = map.get(ev); if (l) { const i = l.indexOf(fn); if (i >= 0) l.splice(i, 1); } return self; },
31
+ removeListener(ev, fn) { return self.off(ev, fn); },
32
+ emit(ev, ...a) { for (const fn of [...(map.get(ev) || [])]) { try { fn(...a); } catch {} } return true; },
33
+ };
34
+ return self;
35
+ }
36
+
37
+ // 非流式:收集捕获
38
+ export function createCollector() {
39
+ let status = 200;
40
+ let text = "";
41
+ const res = {
42
+ ...createEmitter(),
43
+ set statusCode(v) { status = v; },
44
+ get statusCode() { return status; },
45
+ headersSent: false,
46
+ setHeader() {},
47
+ write(c) { text += chunkToString(c); return true; },
48
+ end(c) { if (c != null) text += chunkToString(c); res.headersSent = true; },
49
+ };
50
+ return { res, get: () => ({ status, text }) };
51
+ }
52
+
53
+ /**
54
+ * 纯错误帧直通判定(垫片 write 的前置拦截):整段只含 `data:` 行、且每帧都是
55
+ * `{error:{…}}` 形状(无 `choices`)时,返回原样 SSE 帧数组;否则返回 null(交翻译器)。
56
+ * 为什么必须拦:翻译器只认 chat 帧(choices.delta),错误帧会被它静默 `continue` 掉。
57
+ */
58
+ function passthroughErrorFrames(text) {
59
+ const src = String(text ?? "");
60
+ if (!src.includes("data:")) return null;
61
+ const out = [];
62
+ let sawError = false;
63
+ for (const rawLine of src.split("\n")) {
64
+ const line = rawLine.trim();
65
+ if (!line) continue;
66
+ if (line.startsWith(":")) { out.push(`${line}\n\n`); continue; } // 注释帧照原样(翻译器也是跳过)
67
+ if (!line.startsWith("data:")) return null;
68
+ const payload = line.slice(5).trim();
69
+ if (!payload) continue;
70
+ if (payload === "[DONE]") { out.push("data: [DONE]\n\n"); continue; }
71
+ let j;
72
+ try { j = JSON.parse(payload); } catch { return null; } // 解析失败 = 不是我们的错误帧,交翻译器(它自会记 jsonFails)
73
+ if (!j || typeof j !== "object" || Array.isArray(j) || j.choices) return null; // 有 choices = chat 帧,绝不拦
74
+ if (!j.error) return null; // 无 choices 又无 error:形状未知,交翻译器
75
+ sawError = true;
76
+ out.push(`data: ${JSON.stringify(j)}\n\n`);
77
+ }
78
+ return sawError ? out : null;
79
+ }
80
+
81
+ // 流式:逐 write 实时翻译并转发(head 延迟到首块,避免错误状态码已发送)
82
+ export function createLiveForwarder(realRes, translator) {
83
+ let status = 200;
84
+ let headSent = false;
85
+ let ended = false;
86
+ const sendHead = () => {
87
+ if (headSent) return;
88
+ headSent = true;
89
+ realRes.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", Connection: "keep-alive" });
90
+ for (const e of translator.begin()) realRes.write(`data: ${JSON.stringify(e)}\n\n`);
91
+ };
92
+ // headersSent 必须是**计算值**:垫片的 write() 会调 sendHead() 真发头,若 headersSent 仍是普通属性
93
+ // (只在 end() 里置真),则「头已发但流未结束」这段窗口里它为假 ⇒ helpers.json() 会走 `!headersSent`
94
+ // 分支去 setHeader+end(而本垫片 setHeader 是 no-op、且真头已发)⇒ 错误体静默丢失、只剩裸关流。
95
+ // messages-route 的 createAnthropicForwarder 早已用 getter 实现并点名了同一个坑;本处是同契约的遗漏。
96
+ // 口径与 messages-route 的 createAnthropicForwarder 逐字对齐(哪里算「头已发」只此一处判)。
97
+ const sent = () => headSent || realRes.headersSent === true;
98
+ const res = {
99
+ ...createEmitter(),
100
+ set statusCode(v) { status = v; },
101
+ get statusCode() { return status; },
102
+ get headersSent() { return sent(); },
103
+ set headersSent(_v) { /* 兼容旧赋值点:真实状态由 headSent/ended 推导,不接受外部覆写 */ },
104
+ getHeader(k) { return String(k).toLowerCase() === "content-type" && sent() ? "text/event-stream" : undefined; },
105
+ setHeader() {},
106
+ write(c) {
107
+ if (ended) return true;
108
+ sendHead();
109
+ const text = chunkToString(c);
110
+ // 纯错误帧直写:helpers.json() 的 in-band 收场写的是 OpenAI 形状 `{error:{…}}`(无 choices),
111
+ // 若交给 chat→responses 翻译器,会被当作「非 chat 帧」静默忽略(translate.js 只认 choices.delta)——
112
+ // 错误在翻译层被吞掉,客户端只看到流被关。错误帧与 chat 帧形状互斥,故这里拦下直通。
113
+ const errOut = passthroughErrorFrames(text);
114
+ if (errOut != null) { for (const chunk of errOut) realRes.write(chunk); return true; }
115
+ for (const e of translator.push(text)) realRes.write(`data: ${JSON.stringify(e)}\n\n`);
116
+ return true;
117
+ },
118
+ end(c) {
119
+ if (ended) return;
120
+ ended = true;
121
+ res.headersSent = true;
122
+ if (status >= 400 && !headSent) {
123
+ json(realRes, status, errorShape(c, status));
124
+ return;
125
+ }
126
+ sendHead();
127
+ if (c != null) for (const e of translator.push(chunkToString(c))) realRes.write(`data: ${JSON.stringify(e)}\n\n`);
128
+ const fin = translator.getFinal();
129
+ for (const e of translator.end({ finish: fin.finish, usage: fin.usage })) realRes.write(`data: ${JSON.stringify(e)}\n\n`);
130
+ realRes.write("data: [DONE]\n\n");
131
+ try { realRes.end(); } catch {}
132
+ },
133
+ };
134
+ return res;
135
+ }
136
+
137
+ const RDEBUG = process.env.MSLXDFF_RESPONSES_DEBUG === "1";
138
+ function rlog(...a) {
139
+ if (RDEBUG) console.log("[responses]", ...a);
140
+ }
141
+
142
+ export async function responsesHandler(ctx) {
143
+ const { req, res } = ctx;
144
+ const t0 = Date.now();
145
+ let body;
146
+ try {
147
+ body = await readBody(req);
148
+ } catch {
149
+ return json(res, 400, { error: { message: "Invalid JSON body" } });
150
+ }
151
+ rlog("req", JSON.stringify({
152
+ model: body?.model, stream: body?.stream,
153
+ inputType: typeof body?.input, inputLen: Array.isArray(body?.input) ? body.input.length : String(body?.input || "").length,
154
+ inputKinds: Array.isArray(body?.input) ? [...new Set(body.input.map((i) => i?.type))] : null,
155
+ tools: Array.isArray(body?.tools) ? body.tools.length : 0,
156
+ instructionsLen: String(body?.instructions || "").length,
157
+ // 文本丢失定位:各 message item 的 content 形状(part 类型 + 文本长度 + 头部)
158
+ contentShapes: Array.isArray(body?.input)
159
+ ? body.input.filter((i) => i && (i.type === "message" || i.role)).map((m) => ({
160
+ type: m.type ?? "(无type)", role: m.role,
161
+ cType: Array.isArray(m.content) ? "array" : typeof m.content,
162
+ parts: Array.isArray(m.content) ? m.content.map((p) => `${p?.type ?? "?"}:${typeof p?.text === "string" ? p.text.length : "-"}`).slice(0, 6) : null,
163
+ textHead: typeof m.content === "string" ? m.content.slice(0, 50) : null,
164
+ })).slice(0, 10)
165
+ : null,
166
+ }));
167
+ let chatBody;
168
+ try {
169
+ chatBody = responsesToChatBody(body);
170
+ } catch (e) {
171
+ rlog("translate-req-400", String(e?.message || e));
172
+ return json(res, 400, { error: { message: String(e?.message || e) } });
173
+ }
174
+ rlog("chat", JSON.stringify({ model: chatBody.model, stream: chatBody.stream, msgs: chatBody.messages.map((m) => `${m.role}:${String(m.content || "").length}${m.tool_calls ? `+${m.tool_calls.length}tc` : ""}`) }));
175
+ const pipeline = createChatPipeline(ctx);
176
+ const fakeReq = withBody(req, chatBody);
177
+ // 真连接断开 → 透传给垫片,pipeline 的 abort 链路不断
178
+ const forwardClose = (shim) => { try { req.on?.("close", () => shim.emit("close")); } catch {} };
179
+ try {
180
+ if (chatBody.stream) {
181
+ const translator = createChunkTranslator(chatBody.model);
182
+ const live = createLiveForwarder(res, translator);
183
+ forwardClose(live);
184
+ await pipeline.execute({ req: fakeReq, res: live });
185
+ const st = translator.stats();
186
+ rlog("done-stream", JSON.stringify({ ms: Date.now() - t0, model: chatBody.model, ...st, finish: translator.getFinal().finish, usage: translator.getFinal().usage }));
187
+ } else {
188
+ const cap = createCollector();
189
+ forwardClose(cap.res);
190
+ await pipeline.execute({ req: fakeReq, res: cap.res });
191
+ const { status, text } = cap.get();
192
+ rlog("done-json", JSON.stringify({ ms: Date.now() - t0, model: chatBody.model, status, bytes: text.length, head: text.slice(0, 160) }));
193
+ if (status >= 400) return json(res, status, errorShape(text, status));
194
+ let chatJson = null;
195
+ try { chatJson = JSON.parse(text); } catch {}
196
+ if (!chatJson || chatJson.object === "error" || chatJson.error) {
197
+ rlog("translate-resp-502", text.slice(0, 300));
198
+ return json(res, status >= 400 ? status : 502, errorShape(text, status));
199
+ }
200
+ const respObj = chatJsonToResponse(chatJson, chatBody.model);
201
+ rlog("done-resp", JSON.stringify({ ms: Date.now() - t0, model: chatBody.model, status: respObj.status, items: respObj.output.length, textLen: JSON.stringify(respObj.output).length, usage: respObj.usage }));
202
+ // 非 JSON 透传(上游偶发):包成纯文本 Response
203
+ if (!chatJson.choices) {
204
+ return json(res, 200, { id: chatJson.id || `resp_${Date.now()}`, object: "response", created_at: Math.floor(Date.now() / 1000), status: "completed", model: chatBody.model, output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: String(text).slice(0, 8000) }] }], usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 } });
205
+ }
206
+ return json(res, 200, respObj);
207
+ }
208
+ } catch (err) {
209
+ rlog("execute-throw", String(err?.message || err).slice(0, 300));
210
+ if (!res.headersSent) return json(res, 502, { error: { message: String(err?.message || err).slice(0, 500) } });
211
+ try { res.end(); } catch {}
212
+ }
213
+ }
@@ -1,84 +1,84 @@
1
- // relay 的读取层工具(从 stream.js 拆出:HEAD 版已 19.8KB / 20KB 硬门,先拆后写)。
2
- // 全部是「不碰闸门、不碰 res、不碰计时」的纯 IO 适配:字节→文本、取消上游、注释帧判定、读取迭代。
3
- // 语义与拆出前逐字一致 —— 这里只搬家,不改行为。
4
- import { enrichSseChunkText, enrichNonStreamJson as enrichNonStreamJsonFn } from "./fallback.js";
5
- import { extractUsageFromSseText as extractUsageFn } from "../metrics.js";
6
- import { scanNonStreamBody as scanNonStreamBodyFn } from "./stream-scan.js";
7
-
8
- // SDK 通道(TextEncoder)产出 Uint8Array,legacy 通道为 Buffer;
9
- // 统一转文本,避免 [DONE]/finish_reason/usage/chars 统计在 SDK 路径下静默失效。
10
- export function chunkText(chunk) {
11
- if (typeof chunk === "string") return chunk;
12
- if (Buffer.isBuffer(chunk)) return chunk.toString("utf8");
13
- if (chunk instanceof Uint8Array) return Buffer.from(chunk).toString("utf8");
14
- return "";
15
- }
16
-
17
- // body.cancel() 可能返回非 Promise(自定义/AI SDK 流)——同步异常与 rejection 双路径都要吞掉
18
- export function cancelBody(body) {
19
- try {
20
- const p = typeof body?.cancel === "function" ? body.cancel() : null;
21
- if (p && typeof p.catch === "function") p.catch(() => {});
22
- } catch { /* ignore */ }
23
- }
24
-
25
- // 注释帧(": keepalive" 等)不是模型输出:不算首块、不解除闸门、不触发超时救回,但照常透传
26
- export function hasPayload(text) {
27
- for (const line of String(text).split("\n")) {
28
- const t = line.trim();
29
- if (!t || t.startsWith(":")) continue;
30
- return true;
31
- }
32
- return false;
33
- }
34
-
35
- /**
36
- * 非流式的「先判后写」:在 headers 还没 flush 之前解析上游体,决定能不能富化 / 是不是空轮。
37
- * 返回 { enriched, emptyTurn }:enriched=null 表示上游给的不是可解析 JSON(调用方走原文透传);
38
- * emptyTurn=true 表示这一发是空轮、且留口是开的(调用方沿用留口语义,不写任何响应)。
39
- * 纯搬家:行为与拆出前逐字一致。
40
- */
41
- export function scanNonStreamReply(detail, text, { fallback, holdEnd, isEmptyTurn, captureFallback }) {
42
- let enriched = null;
43
- try {
44
- const parsed = JSON.parse(text);
45
- scanNonStreamBodyFn(detail, parsed);
46
- enriched = enrichNonStreamJsonFn(parsed, fallback);
47
- } catch { /* 上游给的不是可解析 JSON:调用方走原文透传 */ }
48
- if (enriched === null) {
49
- // 纯文本时按长度估 chars;若文本其实是 SSE(client 未要流但上游给流)仍尝试提 usage
50
- try { detail.chars = text.length; } catch {}
51
- try { const u = extractUsageFn(text); if (u) detail.usage = u; } catch {}
52
- try { if (detail.talk) captureFallback(detail.talk, text); } catch {}
53
- return { enriched: null, emptyTurn: false };
54
- }
55
- const emptyTurn = Boolean(holdEnd() && !detail.downstreamClosed && isEmptyTurn(detail));
56
- return { enriched, emptyTurn };
57
- }
58
-
59
- // 上游回的是普通 reply(无流式 delta)时,客户端仍要看到增量:把整块按 fallback 富化成 SSE 帧。
60
- // 只在首块前、且本轮尚未写出 payload 时调用一次;解析不出就原样返回(绝不改字节)。
61
- export function enrichFirstReply(chunk, fallback) {
62
- if (!fallback?.fallback) return chunk;
63
- try {
64
- const txt = chunkText(chunk);
65
- if (!txt.includes("data:")) return chunk;
66
- const enriched = enrichSseChunkText(txt, fallback);
67
- return enriched !== txt ? Buffer.from(enriched, "utf8") : chunk;
68
- } catch {
69
- return chunk;
70
- }
71
- }
72
-
73
- // 自持 reader 优先:for-await 会锁定 ReadableStream,使 body.cancel() 必 reject(真流上等于空操作),
74
- // 超时/断下游要真能掐上游必须走 reader.cancel()
75
- export async function* bodyChunks(body, reader) {
76
- if (reader) {
77
- for (;;) {
78
- const { done, value } = await reader.read();
79
- if (done) return;
80
- yield value;
81
- }
82
- }
83
- for await (const c of body) yield c;
84
- }
1
+ // relay 的读取层工具(从 stream.js 拆出:HEAD 版已 19.8KB / 20KB 硬门,先拆后写)。
2
+ // 全部是「不碰闸门、不碰 res、不碰计时」的纯 IO 适配:字节→文本、取消上游、注释帧判定、读取迭代。
3
+ // 语义与拆出前逐字一致 —— 这里只搬家,不改行为。
4
+ import { enrichSseChunkText, enrichNonStreamJson as enrichNonStreamJsonFn } from "./fallback.js";
5
+ import { extractUsageFromSseText as extractUsageFn } from "../metrics.js";
6
+ import { scanNonStreamBody as scanNonStreamBodyFn } from "./stream-scan.js";
7
+
8
+ // SDK 通道(TextEncoder)产出 Uint8Array,legacy 通道为 Buffer;
9
+ // 统一转文本,避免 [DONE]/finish_reason/usage/chars 统计在 SDK 路径下静默失效。
10
+ export function chunkText(chunk) {
11
+ if (typeof chunk === "string") return chunk;
12
+ if (Buffer.isBuffer(chunk)) return chunk.toString("utf8");
13
+ if (chunk instanceof Uint8Array) return Buffer.from(chunk).toString("utf8");
14
+ return "";
15
+ }
16
+
17
+ // body.cancel() 可能返回非 Promise(自定义/AI SDK 流)——同步异常与 rejection 双路径都要吞掉
18
+ export function cancelBody(body) {
19
+ try {
20
+ const p = typeof body?.cancel === "function" ? body.cancel() : null;
21
+ if (p && typeof p.catch === "function") p.catch(() => {});
22
+ } catch { /* ignore */ }
23
+ }
24
+
25
+ // 注释帧(": keepalive" 等)不是模型输出:不算首块、不解除闸门、不触发超时救回,但照常透传
26
+ export function hasPayload(text) {
27
+ for (const line of String(text).split("\n")) {
28
+ const t = line.trim();
29
+ if (!t || t.startsWith(":")) continue;
30
+ return true;
31
+ }
32
+ return false;
33
+ }
34
+
35
+ /**
36
+ * 非流式的「先判后写」:在 headers 还没 flush 之前解析上游体,决定能不能富化 / 是不是空轮。
37
+ * 返回 { enriched, emptyTurn }:enriched=null 表示上游给的不是可解析 JSON(调用方走原文透传);
38
+ * emptyTurn=true 表示这一发是空轮、且留口是开的(调用方沿用留口语义,不写任何响应)。
39
+ * 纯搬家:行为与拆出前逐字一致。
40
+ */
41
+ export function scanNonStreamReply(detail, text, { fallback, holdEnd, isEmptyTurn, captureFallback }) {
42
+ let enriched = null;
43
+ try {
44
+ const parsed = JSON.parse(text);
45
+ scanNonStreamBodyFn(detail, parsed);
46
+ enriched = enrichNonStreamJsonFn(parsed, fallback);
47
+ } catch { /* 上游给的不是可解析 JSON:调用方走原文透传 */ }
48
+ if (enriched === null) {
49
+ // 纯文本时按长度估 chars;若文本其实是 SSE(client 未要流但上游给流)仍尝试提 usage
50
+ try { detail.chars = text.length; } catch {}
51
+ try { const u = extractUsageFn(text); if (u) detail.usage = u; } catch {}
52
+ try { if (detail.talk) captureFallback(detail.talk, text); } catch {}
53
+ return { enriched: null, emptyTurn: false };
54
+ }
55
+ const emptyTurn = Boolean(holdEnd() && !detail.downstreamClosed && isEmptyTurn(detail));
56
+ return { enriched, emptyTurn };
57
+ }
58
+
59
+ // 上游回的是普通 reply(无流式 delta)时,客户端仍要看到增量:把整块按 fallback 富化成 SSE 帧。
60
+ // 只在首块前、且本轮尚未写出 payload 时调用一次;解析不出就原样返回(绝不改字节)。
61
+ export function enrichFirstReply(chunk, fallback) {
62
+ if (!fallback?.fallback) return chunk;
63
+ try {
64
+ const txt = chunkText(chunk);
65
+ if (!txt.includes("data:")) return chunk;
66
+ const enriched = enrichSseChunkText(txt, fallback);
67
+ return enriched !== txt ? Buffer.from(enriched, "utf8") : chunk;
68
+ } catch {
69
+ return chunk;
70
+ }
71
+ }
72
+
73
+ // 自持 reader 优先:for-await 会锁定 ReadableStream,使 body.cancel() 必 reject(真流上等于空操作),
74
+ // 超时/断下游要真能掐上游必须走 reader.cancel()
75
+ export async function* bodyChunks(body, reader) {
76
+ if (reader) {
77
+ for (;;) {
78
+ const { done, value } = await reader.read();
79
+ if (done) return;
80
+ yield value;
81
+ }
82
+ }
83
+ for await (const c of body) yield c;
84
+ }