mslxdff 0.1.199 → 0.1.203

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 (327) hide show
  1. package/README.md +3 -3
  2. package/bin/mslxdff.js +1 -1
  3. package/package.json +38 -38
  4. package/plugins/README.md +14 -14
  5. package/plugins/prefer-model.mjs.example +14 -14
  6. package/src/anthropic/count-tokens.js +61 -61
  7. package/src/anthropic/request-body.js +126 -126
  8. package/src/anthropic/shared.js +18 -18
  9. package/src/anthropic/sse-aggregate.js +56 -56
  10. package/src/anthropic/translate.js +287 -287
  11. package/src/anthropic/web-search.js +361 -361
  12. package/src/api-kind.js +31 -31
  13. package/src/autostart.js +288 -288
  14. package/src/bench/probe.js +1 -1
  15. package/src/bench/runner.js +1 -1
  16. package/src/bench/via-probe.js +1 -1
  17. package/src/bench/via.js +144 -144
  18. package/src/bench/workbuddy-bench.js +1 -1
  19. package/src/chat/config.js +11 -11
  20. package/src/chat/cooling.js +81 -81
  21. package/src/chat/direct.js +58 -58
  22. package/src/chat/index.js +6 -6
  23. package/src/chat/spinner.js +38 -38
  24. package/src/chat/sse.js +69 -69
  25. package/src/chat/stats.js +247 -247
  26. package/src/chat/store.js +48 -48
  27. package/src/chat/tool-handlers.js +67 -67
  28. package/src/chat/tools.js +1 -1
  29. package/src/chat/upstream.js +59 -59
  30. package/src/chat-pipeline/auto-race.js +158 -158
  31. package/src/chat-pipeline/empty-turn.js +147 -147
  32. package/src/chat-pipeline/engine.js +17 -17
  33. package/src/chat-pipeline/serial-trial.js +283 -283
  34. package/src/chat-pipeline/terminal-verdict.js +147 -147
  35. package/src/chooser.js +54 -54
  36. package/src/claude-desktop/apply.js +101 -101
  37. package/src/claude-desktop/context1m.js +38 -38
  38. package/src/claude-desktop/doctor.js +146 -146
  39. package/src/claude-desktop/slots.js +152 -152
  40. package/src/cli/bootstrap.js +1 -1
  41. package/src/cli/commands/claude-desktop.js +284 -284
  42. package/src/cli/commands/daemon.js +231 -231
  43. package/src/cli/commands/model/list-live.js +22 -22
  44. package/src/cli/commands/model/list-providers.js +76 -76
  45. package/src/cli/commands/model/list-render.js +77 -77
  46. package/src/cli/commands/model/list-sort.js +52 -52
  47. package/src/cli/commands/model/list-tty.js +76 -76
  48. package/src/cli/commands/model/list.js +166 -166
  49. package/src/cli/commands/model/live-models.js +24 -24
  50. package/src/cli/commands/model/stats.js +43 -43
  51. package/src/cli/commands/model/status.js +47 -47
  52. package/src/cli/commands/model.js +51 -51
  53. package/src/cli/commands/provider/add.js +108 -108
  54. package/src/cli/commands/provider/allowlist.js +99 -99
  55. package/src/cli/commands/provider/cline-free.js +99 -99
  56. package/src/cli/commands/provider/cline-login.js +218 -218
  57. package/src/cli/commands/provider/cline-quota.js +65 -65
  58. package/src/cli/commands/provider/codearts-login.js +45 -45
  59. package/src/cli/commands/provider/config.js +119 -119
  60. package/src/cli/commands/provider/globalqwenwork-login.js +126 -126
  61. package/src/cli/commands/provider/keys-guard.js +50 -50
  62. package/src/cli/commands/provider/keys.js +161 -161
  63. package/src/cli/commands/provider/qoder-checkin.js +81 -81
  64. package/src/cli/commands/provider/qoder-login.js +102 -102
  65. package/src/cli/commands/provider/raccoon-login.js +180 -180
  66. package/src/cli/commands/provider/raccoon-password.js +162 -162
  67. package/src/cli/commands/provider/raccoon-quota.js +109 -109
  68. package/src/cli/commands/provider/traework-checkin.js +144 -144
  69. package/src/cli/commands/provider/traework-login.js +164 -164
  70. package/src/cli/commands/provider/workbuddy-login.js +153 -153
  71. package/src/cli/commands/provider/zcode-login.js +77 -77
  72. package/src/cli/commands/provider/zcode-quota.js +55 -55
  73. package/src/cli/commands/provider.js +1 -1
  74. package/src/cli/commands/stats.js +149 -149
  75. package/src/cli/commands/system.js +261 -261
  76. package/src/cli/commands/timezone.js +47 -47
  77. package/src/cli/commands/use-group.js +60 -60
  78. package/src/cli/commands/workbuddy.js +189 -189
  79. package/src/cli/format.js +141 -141
  80. package/src/cli/group-helpers.js +70 -70
  81. package/src/cli/help.js +80 -80
  82. package/src/cli/index.js +70 -70
  83. package/src/cli/interactive.js +121 -121
  84. package/src/cli/join-core.js +99 -99
  85. package/src/cli/join-wizard.js +175 -175
  86. package/src/cli/policy.js +136 -136
  87. package/src/cli/provider-row.js +126 -126
  88. package/src/cli/util.js +24 -24
  89. package/src/compat.js +53 -53
  90. package/src/events.js +38 -38
  91. package/src/free-lane.js +131 -131
  92. package/src/free-watcher.js +63 -63
  93. package/src/groups.js +262 -262
  94. package/src/logs.js +195 -195
  95. package/src/metrics.js +62 -62
  96. package/src/model-capabilities/context-lookup.js +65 -65
  97. package/src/model-capabilities/merge.js +142 -142
  98. package/src/model-trace.js +187 -187
  99. package/src/models.js +1 -1
  100. package/src/opencode-identity.js +72 -72
  101. package/src/peers.js +254 -251
  102. package/src/plugins.js +107 -107
  103. package/src/providers/base.js +164 -164
  104. package/src/providers/classify.js +22 -22
  105. package/src/providers/cline/allowlist-sync.js +93 -93
  106. package/src/providers/cline/free-catalog.js +71 -71
  107. package/src/providers/cline/headers.js +37 -37
  108. package/src/providers/cline/index.js +130 -130
  109. package/src/providers/cline/models.js +150 -150
  110. package/src/providers/cline/usage.js +274 -274
  111. package/src/providers/codearts/auth-pool.js +148 -148
  112. package/src/providers/codearts/concurrency.js +34 -34
  113. package/src/providers/codearts/const.js +65 -65
  114. package/src/providers/codearts/dpop.js +95 -95
  115. package/src/providers/codearts/index.js +175 -175
  116. package/src/providers/codearts/login.js +214 -214
  117. package/src/providers/codearts/models.js +182 -182
  118. package/src/providers/codearts/sign.js +96 -96
  119. package/src/providers/codearts/sse.js +204 -204
  120. package/src/providers/codearts/stream.js +209 -209
  121. package/src/providers/codearts/sts.js +124 -124
  122. package/src/providers/codearts.js +1 -1
  123. package/src/providers/dispatcher.js +146 -146
  124. package/src/providers/generic.js +156 -156
  125. package/src/providers/globalqwenwork/account-store.js +141 -141
  126. package/src/providers/globalqwenwork/constants.js +73 -73
  127. package/src/providers/globalqwenwork/crypto.js +2 -2
  128. package/src/providers/globalqwenwork/http.js +2 -2
  129. package/src/providers/globalqwenwork/index.js +331 -331
  130. package/src/providers/globalqwenwork/payload.js +147 -147
  131. package/src/providers/globalqwenwork/rsa.js +2 -2
  132. package/src/providers/globalqwenwork/sse.js +2 -2
  133. package/src/providers/globalqwenwork/stream.js +2 -2
  134. package/src/providers/globalqwenwork.js +1 -1
  135. package/src/providers/globalworkbuddy/account-store.js +198 -198
  136. package/src/providers/globalworkbuddy/auth.js +97 -97
  137. package/src/providers/globalworkbuddy/catalog.js +138 -138
  138. package/src/providers/globalworkbuddy/constants.js +73 -73
  139. package/src/providers/globalworkbuddy/device-auth.js +120 -120
  140. package/src/providers/globalworkbuddy/errors.js +98 -98
  141. package/src/providers/globalworkbuddy/headers.js +89 -89
  142. package/src/providers/globalworkbuddy/models.js +96 -96
  143. package/src/providers/globalworkbuddy/payload.js +80 -80
  144. package/src/providers/index.js +5 -5
  145. package/src/providers/model-id.js +172 -172
  146. package/src/providers/opencode.js +19 -19
  147. package/src/providers/openrouter.js +31 -31
  148. package/src/providers/qoder/account-store.js +106 -106
  149. package/src/providers/qoder/aggregate.js +60 -60
  150. package/src/providers/qoder/baseprompt.json +591 -591
  151. package/src/providers/qoder/checkin.js +160 -160
  152. package/src/providers/qoder/constants.js +44 -44
  153. package/src/providers/qoder/encode.js +41 -41
  154. package/src/providers/qoder/fingerprint.js +38 -38
  155. package/src/providers/qoder/models.js +77 -77
  156. package/src/providers/qoder/oauth.js +84 -84
  157. package/src/providers/qoder/payload.js +162 -162
  158. package/src/providers/qoder/request.js +27 -27
  159. package/src/providers/qoder/session.js +134 -134
  160. package/src/providers/qoder/sticky.js +38 -38
  161. package/src/providers/qoder/stream.js +116 -116
  162. package/src/providers/qoder.js +1 -1
  163. package/src/providers/raccoon/account-store.js +216 -216
  164. package/src/providers/raccoon/auth.js +67 -67
  165. package/src/providers/raccoon/chat.js +228 -228
  166. package/src/providers/raccoon/const.js +180 -165
  167. package/src/providers/raccoon/credits.js +185 -175
  168. package/src/providers/raccoon/crypto.js +43 -0
  169. package/src/providers/raccoon/envelope.js +31 -31
  170. package/src/providers/raccoon/headers.js +45 -45
  171. package/src/providers/raccoon/index.js +216 -216
  172. package/src/providers/raccoon/login.js +279 -279
  173. package/src/providers/raccoon/models.js +160 -160
  174. package/src/providers/raccoon/password-login.js +102 -87
  175. package/src/providers/raccoon/qr-codewords.js +115 -115
  176. package/src/providers/raccoon/qr.js +236 -236
  177. package/src/providers/raccoon/sse.js +135 -135
  178. package/src/providers/responses-channel.js +120 -120
  179. package/src/providers/share-keys.js +53 -53
  180. package/src/providers/traework/account-store.js +234 -234
  181. package/src/providers/traework/chat.js +217 -217
  182. package/src/providers/traework/checkin.js +77 -77
  183. package/src/providers/traework/constants.js +41 -41
  184. package/src/providers/traework/errors.js +52 -52
  185. package/src/providers/traework/headers.js +79 -79
  186. package/src/providers/traework/index.js +132 -132
  187. package/src/providers/traework/market-identity.js +116 -116
  188. package/src/providers/traework/models.js +98 -98
  189. package/src/providers/traework/payload.js +86 -86
  190. package/src/providers/traework/sse.js +184 -184
  191. package/src/providers/traework/token.js +52 -52
  192. package/src/providers/traework.js +2 -2
  193. package/src/providers/workbuddy/account-store.js +186 -186
  194. package/src/providers/workbuddy/auth.js +158 -158
  195. package/src/providers/workbuddy/balance.js +86 -86
  196. package/src/providers/workbuddy/cat-travel.js +125 -125
  197. package/src/providers/workbuddy/checkin.js +75 -75
  198. package/src/providers/workbuddy/desktop-info.js +78 -78
  199. package/src/providers/workbuddy/growth-api.js +67 -67
  200. package/src/providers/workbuddy/growth-plans.js +63 -63
  201. package/src/providers/workbuddy/growth.js +181 -181
  202. package/src/providers/workbuddy/index.js +217 -217
  203. package/src/providers/workbuddy/models.js +126 -126
  204. package/src/providers/workbuddy/payload.js +141 -141
  205. package/src/providers/workbuddy/rotation-log.js +83 -83
  206. package/src/providers/workbuddy/sanitize-tools.js +61 -61
  207. package/src/providers/workbuddy/sdk-chat.js +26 -26
  208. package/src/providers/workbuddy-balance.js +78 -78
  209. package/src/providers/zcode/account-store.js +129 -129
  210. package/src/providers/zcode/auth.js +28 -28
  211. package/src/providers/zcode/chat.js +179 -179
  212. package/src/providers/zcode/const.js +54 -54
  213. package/src/providers/zcode/context-shape.js +162 -162
  214. package/src/providers/zcode/headers.js +93 -93
  215. package/src/providers/zcode/index.js +124 -124
  216. package/src/providers/zcode/models.js +65 -65
  217. package/src/providers/zcode/oauth.js +120 -120
  218. package/src/providers/zcode/quota.js +176 -176
  219. package/src/providers/zcode/sse.js +196 -196
  220. package/src/readline-compat.js +25 -25
  221. package/src/responses/translate.js +311 -311
  222. package/src/routes/chat/broadband-handler.js +72 -72
  223. package/src/routes/chat/empty-turn-report.js +31 -31
  224. package/src/routes/chat/exhausted-handler.js +87 -87
  225. package/src/routes/chat/hedge-handler.js +130 -130
  226. package/src/routes/chat/index.js +1 -1
  227. package/src/routes/chat/local-handler.js +63 -63
  228. package/src/routes/chat/peer-handler.js +81 -81
  229. package/src/routes/chat.js +3 -3
  230. package/src/routes/fallback.js +88 -88
  231. package/src/routes/groups-relay.js +214 -214
  232. package/src/routes/groups.js +122 -122
  233. package/src/routes/helpers.js +83 -83
  234. package/src/routes/index.js +147 -147
  235. package/src/routes/messages-route.js +365 -365
  236. package/src/routes/peers.js +23 -1
  237. package/src/routes/relay-queue.js +181 -181
  238. package/src/routes/relay.js +60 -60
  239. package/src/routes/responses-route.js +213 -213
  240. package/src/routes/stream-body.js +84 -84
  241. package/src/routes/stream-hold.js +337 -337
  242. package/src/routes/stream-scan.js +366 -366
  243. package/src/routes.js +3 -3
  244. package/src/runtime/auto-update.js +113 -113
  245. package/src/runtime/bootstrap.js +8 -0
  246. package/src/runtime/broadband-stream.js +79 -79
  247. package/src/runtime/broadband.js +103 -103
  248. package/src/runtime/group-sync.js +81 -81
  249. package/src/runtime/keep-warm.js +120 -0
  250. package/src/runtime/lifecycle-forensics.js +116 -116
  251. package/src/runtime/lifecycle-log.js +87 -87
  252. package/src/runtime/log-stamp.js +73 -73
  253. package/src/runtime/proc-info.js +137 -137
  254. package/src/runtime/qoder-checkin.js +93 -93
  255. package/src/runtime/raccoon-checkin.js +126 -126
  256. package/src/runtime/server-lifecycle.js +189 -189
  257. package/src/runtime/traework-checkin.js +118 -118
  258. package/src/runtime/workbuddy-checkin.js +120 -120
  259. package/src/runtime/workbuddy-growth.js +133 -133
  260. package/src/server.js +7 -0
  261. package/src/state/facade.js +58 -58
  262. package/src/state/memory.js +162 -162
  263. package/src/state/merge.js +25 -25
  264. package/src/state/migrations/cline-unify.js +94 -94
  265. package/src/state/migrations.js +33 -33
  266. package/src/state/persist.js +42 -42
  267. package/src/state/provider-config.js +142 -142
  268. package/src/state/schemas/group.js +31 -31
  269. package/src/state/schemas/model.js +94 -94
  270. package/src/state/schemas/peer.js +31 -31
  271. package/src/state/schemas/port.js +10 -10
  272. package/src/state/schemas/provider.js +202 -202
  273. package/src/state/schemas/timezone.js +70 -70
  274. package/src/state/schemas/token.js +48 -48
  275. package/src/state/schemas/use-group.js +102 -102
  276. package/src/state/store.js +176 -176
  277. package/src/state.js +1 -1
  278. package/src/sync-claude-desktop.js +234 -234
  279. package/src/sync-claude.js +152 -152
  280. package/src/sync-codex.js +116 -116
  281. package/src/sync-workbuddy.js +158 -158
  282. package/src/talk-full-redact.js +129 -129
  283. package/src/talk-full.js +131 -131
  284. package/src/talk-log.js +257 -257
  285. package/src/text-tokens.js +52 -52
  286. package/src/thinking-effort.js +67 -67
  287. package/src/time.js +106 -106
  288. package/src/timeline.js +26 -26
  289. package/src/transport/index.js +1 -1
  290. package/src/upstream-engine/index.js +74 -72
  291. package/src/upstream-engine/mode.js +15 -15
  292. package/src/upstream-engine/sdk/attempt.js +221 -221
  293. package/src/upstream-engine/sdk/chat.js +28 -28
  294. package/src/upstream-engine/sdk/convert.js +192 -192
  295. package/src/upstream-engine/sdk/diagnose.js +62 -62
  296. package/src/upstream-engine/sdk/dispatch.js +55 -55
  297. package/src/upstream-engine/sdk/responses.js +176 -176
  298. package/src/upstream-engine/sdk/sse.js +166 -166
  299. package/src/upstream-responses.js +305 -305
  300. package/src/upstream.js +53 -1
  301. package/src/usage/record.js +106 -106
  302. package/src/usage/report.js +195 -195
  303. package/src/v6pool/claim.js +157 -157
  304. package/src/v6pool/config.js +65 -65
  305. package/src/v6pool/detect.js +130 -130
  306. package/src/v6pool/index.js +152 -152
  307. package/src/v6pool/prefix.js +140 -140
  308. package/src/v6pool/probe.js +40 -40
  309. package/src/v6pool/report.js +104 -104
  310. package/src/v6pool/rotate.js +263 -239
  311. package/src/warp-pool/config.js +74 -74
  312. package/src/warp-pool/health.js +106 -106
  313. package/src/warp-pool/identity.js +141 -141
  314. package/src/warp-pool/index.js +358 -358
  315. package/src/warp-pool/keys.js +29 -29
  316. package/src/warp-pool/nodes.js +95 -95
  317. package/src/warp-pool/ports.js +160 -160
  318. package/src/warp-pool/register.js +113 -113
  319. package/src/warp-pool/report.js +232 -232
  320. package/src/warp-pool/revive.js +168 -168
  321. package/src/warp-pool/rotate.js +214 -214
  322. package/src/warp-pool/strategy.js +43 -43
  323. package/src/warp-pool/verdict.js +69 -69
  324. package/src/warp-pool/wireproxy.js +162 -162
  325. package/docs/cli_help_mini.md +0 -146
  326. package/src/providers/AGENTS.md +0 -49
  327. package/src/routes/AGENTS.md +0 -37
package/src/talk-log.js CHANGED
@@ -1,257 +1,257 @@
1
- // 环形对话日志(talk log):按「供应商-模型-talk.log」分文件,只保留最近 1 小时的完整对话。
2
- // 记三件事:我问了什么 / 模型怎么想(reasoning)/ 模型怎么答(content),供人工调研模型的回答方式。
3
- // 与 model-trace 相反:那边刻意不落正文(「加日志」≠「倒数据」),这里专门落正文 —— 所以必须脱敏 + 封顶。
4
- // 默认开;MSLXDFF_TALK_LOG=0 关闭,窗口 MSLXDFF_TALK_LOG_WINDOW_MIN(分,默认 60),单文件 MSLXDFF_TALK_LOG_MAX_KB(默认 5MB)。
5
- import { appendFileSync, chmodSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, readdirSync, readSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
6
- import { dirname, join } from "node:path";
7
- import { logDir } from "./logs.js";
8
- import { fmtShanghaiYMDHMS } from "./time.js";
9
-
10
- const SUBDIR = "talk";
11
- const DEFAULT_WINDOW_MIN = 60;
12
- const DEFAULT_MAX_KB = 5 * 1024; // 5MB per talk-log file (ring buffer)
13
- const MARK = "#talk-entry";
14
- // 正常路径只 append;整文件重写(环形淘汰)只在「最老条目出窗」或「超字节上限」时发生。
15
- // oldestCache 记住每个文件当前最老的 ts,避免每次写入都回读全文件。
16
- const oldestCache = new Map();
17
-
18
- export function talkLogEnabled() { return String(process.env.MSLXDFF_TALK_LOG ?? "1") !== "0"; }
19
- function windowMs() { const n = Number(process.env.MSLXDFF_TALK_LOG_WINDOW_MIN); return Number.isFinite(n) && n > 0 ? n * 60000 : DEFAULT_WINDOW_MIN * 60000; }
20
- function maxBytes() { const n = Number(process.env.MSLXDFF_TALK_LOG_MAX_KB); return Number.isFinite(n) && n > 0 ? n * 1024 : DEFAULT_MAX_KB * 1024; }
21
- /** 仅测试用:进程重启等价物(环形窗口的内存基线清空)。 */
22
- export function resetTalkLogCache() { oldestCache.clear(); }
23
-
24
- /** 供应商-模型-talk.log:qoder/qfmodel → qoder-qfmodel-talk.log;裸 id(免费池)直接用一个名字段。 */
25
- export function talkLogName(model) {
26
- const s = String(model || "unknown").trim().toLowerCase();
27
- const segs = s.includes("/") ? s.split("/").filter((x) => x && x !== "unknown") : [s];
28
- const safe = segs.join("-").replace(/[^a-z0-9._-]+/g, "-").replace(/^[-.]+|[-.]+$/g, "").slice(0, 180);
29
- return `${safe || "unknown"}-talk.log`;
30
- }
31
-
32
- export function talkDir() { return join(logDir(), SUBDIR); }
33
- export function talkLogFile(model) { return join(talkDir(), talkLogName(model)); }
34
-
35
- // 正文里的凭据一律先抹再落盘(保留换行,不像 model-trace 那样压成一行 —— 这里要读的是回答本身)。
36
- const MASKS = [
37
- [/([?&](?:token|refresh[_-]?token|access[_-]?token|api[_-]?key|apikey|key|secret|password|cookie)=)[^\s&]+/gi, "$1[已脱敏]"],
38
- [/(authorization|bearer|proxy-authorization|cookie|x-api-key)\s*[:=]\s*[^\s,;]+/gi, "$1=[已脱敏]"],
39
- [/("(?:apiKey|api_key|accessToken|refreshToken|secret|password|client_secret)"\s*:\s*)"[^"]*"/gi, '$1"[已脱敏]"'],
40
- [/\beyJ[\w-]{6,}\.[\w-]{6,}\.[\w-]{6,}\b/g, "[已脱敏-JWT]"],
41
- [/\b(?:sk|pk|ghp|gho|ghu|ghs|ghr|xox[baprs])-[\w-]{10,}\b/g, "[已脱敏-key]"],
42
- // 裸 k=v(提问里常出现 token=…、password=…):只掐足够长的值,避免把「token 数量」这类正常技术讨论也糊掉
43
- [/\b(token|accessToken|refreshToken|api[_-]?key|apiKey|secret|password|cookie|client_secret|authorization)\s*[:=]\s*[\w./+-]{12,}/gi, "$1=[已脱敏]"],
44
- ];
45
-
46
- const CUT_MARK = "\n…[已截断]";
47
-
48
- /** 脱敏 + 按字节封顶截断(中文 3 字节/字,逐字累加才不会超预算),并报告这一刀是否真的落下。 */
49
- function maskCut(v, cap = 100000) {
50
- let s = String(v ?? "");
51
- for (const [re, repl] of MASKS) s = s.replace(re, repl);
52
- if (Buffer.byteLength(s, "utf8") <= cap) return { text: s, capped: false };
53
- const tail = Buffer.from(CUT_MARK, "utf8");
54
- const budget = Math.max(0, cap - tail.length);
55
- let bytes = 0, i = 0;
56
- for (const ch of s) {
57
- const n = Buffer.byteLength(ch, "utf8");
58
- if (bytes + n > budget) break;
59
- bytes += n;
60
- i += ch.length;
61
- }
62
- return { text: s.slice(0, i) + CUT_MARK, capped: true };
63
- }
64
-
65
- /** 脱敏 + 按字节封顶截断(对外口径与改前逐字一致;要知道是否砍过用 maskCut/pushBlock)。 */
66
- export function maskText(v, cap = 100000) { return maskCut(v, cap).text; }
67
-
68
- function textOf(content) {
69
- if (typeof content === "string") return content;
70
- if (Array.isArray(content)) return content.map((p) => p?.text || (p?.image_url ? `[图片 ${String(p.image_url.url).slice(0, 60)}]` : "")).filter(Boolean).join("\n");
71
- return "";
72
- }
73
-
74
- /** 只取「我发给大模型的问题」= 最后一条 user 消息(agent 回路每轮都带全量历史,全落会把环形窗口占满)。 */
75
- export function lastUserQuestion(body) {
76
- const msgs = Array.isArray(body?.messages) ? body.messages : null;
77
- if (msgs) { for (let i = msgs.length - 1; i >= 0; i--) if (msgs[i]?.role === "user") return textOf(msgs[i].content); }
78
- const inp = body?.input; // Responses 端点形状兜底
79
- if (typeof inp === "string") return inp;
80
- if (Array.isArray(inp)) { for (let i = inp.length - 1; i >= 0; i--) if (inp[i]?.role === "user") return textOf(inp[i].content); }
81
- return "";
82
- }
83
-
84
- const CUT_IN = "----8<----", CUT_OUT = "---->8----";
85
-
86
- // 标签行的「N 字」在字节封顶之后只是**已落盘**的字数;不带封顶标记,就会被读成「模型只说了这么多」。
87
- function pushBlock(out, label, text, cap) {
88
- const { text: m, capped } = maskCut(text, cap);
89
- if (!m.trim()) return false;
90
- out.push(`[${label} · ${m.length} 字${capped ? " · 已封顶" : ""}]`, CUT_IN, m, CUT_OUT, "");
91
- return capped;
92
- }
93
-
94
- // 撞捕获上限时紧跟头部行的人类可读提醒:不必先认识 truncated=1 这个键名也能一眼看出「可能不完整」。
95
- const TRUNC_NOTE = "[!] 本轮已撞捕获上限(truncated=1):思考/正文后半段未落盘,内容可能不完整 —— 别当成模型的完整回答";
96
-
97
- /** 一条对话 = 一个可读块:头部一行元信息(ts= 是环形淘汰的唯一依据),正文三段。elapsed= 取本次尝试墙钟(design D3),finish=/relayMs= 为 ADR-0046 新读数,api= 为入站协议门短码(认不出的路 = other;未拿到判定值则整段省略,绝不伪造)。 */
98
- export function formatTalkEntry({ reqId, api, model, via, hops, status, stream, elapsedMs, question, thinking, answer, tools, usage, upstream, account, pick, ts, talkCapped = false, talkChars, talkCap, talkDropped, finishReason, relayMs, ttftMs } = {}) {
99
- const fval = finishReason || "-"; // 缺值占位(grill Q5):「没给 finish」不得被读成 stop;字段恒在场
100
- const now = Number.isFinite(ts) && ts > 0 ? ts : Date.now();
101
- const trunc = talkCapped === true;
102
- const head = [
103
- `${MARK} ts=${now}`, `time=${fmtShanghaiYMDHMS(new Date(now))}`, `req=${reqId || "-"}`, `model=${model || "-"}`,
104
- api ? `api=${api}` : "", via ? `via=${via}` : "", hops != null ? `hops=${hops}` : "", `status=${status ?? "-"}`,
105
- stream ? "stream=1" : "stream=0", Number.isFinite(elapsedMs) ? `elapsed=${Math.round(elapsedMs)}ms` : "",
106
- // 尝试墙钟与 relay 内部计时同值时不重复打印(头行噪音克制,grill Q3);两值不同(hedge/缓冲重放)才另列 relayMs
107
- Number.isFinite(relayMs) && Number.isFinite(elapsedMs) && Math.round(relayMs) !== Math.round(elapsedMs) ? `relayMs=${Math.round(relayMs)}ms` : "",
108
- // 首字耗时(客户端从本次尝试起点到网关写出第一帧,含上游 prefill):「答案忽然冒出」类问题的第一读数。
109
- // 只在真有值时出现(非流式/拿不到首块 = null,不打印而非写 0,0 会被读成「秒回」)。
110
- Number.isFinite(ttftMs) ? `ttft=${Math.round(ttftMs)}ms` : "",
111
- `finish=${fval}`,
112
- usage ? `usage(prompt=${usage.prompt_tokens ?? "-"},completion=${usage.completion_tokens ?? "-"})` : "",
113
- upstream ? `upstream=${upstream}` : "", account ? `account=${account}` : "", pick ? `pick=${pick}` : "",
114
- // 截断读数只在真截断时出现(未截断不得新增噪音字段):cap= 上限、chars= 已捕获字符数
115
- trunc ? `truncated=1 cap=${Number.isFinite(talkCap) && talkCap > 0 ? talkCap : "-"} chars=${Number.isFinite(talkChars) ? talkChars : "-"}` : "",
116
- Number(talkDropped) > 0 ? `dropped=${Math.round(Number(talkDropped))}` : "", // 不可逆丢弃帧计数:只在真丢时出现(与 truncated 同「未丢不添噪音」口径,review P1-1)
117
- ].filter(Boolean).join(" ");
118
- const out = [head];
119
- if (trunc) out.push(TRUNC_NOTE);
120
- out.push("");
121
- pushBlock(out, "我问", question, 20000);
122
- pushBlock(out, "思考", thinking, 100000);
123
- // 零正文不再静默跳块(spec「零输出轮必须可判读」/grill Q4):缺块与「正文该有却没落盘」同形,缺席必须是可读读数。
124
- // 只落一行标注 + 空行(没有内容可围,不套 CUT 围栏);有真实正文的字符串照旧走 pushBlock,该段与改动前逐字节一致。
125
- // 判据看 trim 不看长度(P1-B):" "/"\n" 进 pushBlock 也会被 !m.trim() 整块跳过 → 与「正文丢盘」再度同形,而「只调工具 + 吐个换行」是真实 agent 形态。
126
- if (!String(answer ?? "").trim()) out.push(`[回答 · 0 字 · 本轮无正文(finish=${fval})]`, "");
127
- else pushBlock(out, "回答", answer, 200000);
128
- const t = tools == null || tools === "" ? "" : String(tools);
129
- pushBlock(out, "工具调用", t, 20000);
130
- out.push("[END]", "");
131
- return out.join("\n") + "\n";
132
- }
133
-
134
- /** 只读文件头 8KB 取最老一条的 ts(进程重启后重建内存基线,不必回读全文件)。 */
135
- function firstTs(file) {
136
- let fd = null;
137
- try {
138
- fd = openSync(file, "r");
139
- const buf = Buffer.alloc(8192);
140
- const n = readSync(fd, buf, 0, 8192, 0);
141
- const m = buf.subarray(0, n).toString("utf8").match(/^#talk-entry ts=(\d+)/m);
142
- return m ? Number(m[1]) : null;
143
- } catch { return null; }
144
- finally { if (fd != null) { try { closeSync(fd); } catch {} } }
145
- }
146
-
147
- /** 追加一条(正常路径只 append;到窗口或超预算才整写淘汰)。 */
148
- export function appendTalkEntry({ file, block, atMs } = {}) {
149
- try {
150
- if (!file || !block) return 0;
151
- mkdirSync(dirname(file), { recursive: true });
152
- const isNew = !existsSync(file);
153
- appendFileSync(file, block);
154
- // mode 只在新建时生效;存量文件补一次 chmod(POSIX;Windows 无意义)
155
- if (isNew) { try { chmodSync(file, 0o600); } catch {} }
156
- const now = Number.isFinite(atMs) && atMs > 0 ? atMs : Date.now();
157
- let base = oldestCache.get(file);
158
- if (base == null) { base = firstTs(file); if (base == null) base = now; }
159
- const size = (() => { try { return statSync(file).size; } catch { return 0; } })();
160
- if (now - base > windowMs() || size > maxBytes()) trimTalkLog(file, now);
161
- else oldestCache.set(file, Math.min(base, now));
162
- return 1;
163
- } catch { return 0; }
164
- }
165
-
166
- /** 按 `#talk-entry` 头行切块(内容行理论上可能撞前缀,故要求严格 ts= 形状才认头)。 */
167
- export function splitEntries(txt) {
168
- const entries = [];
169
- const lines = String(txt ?? "").split("\n");
170
- let start = -1;
171
- const close = (end) => {
172
- if (start < 0) return;
173
- const chunk = lines.slice(start, end);
174
- const m = chunk[0].match(/^#talk-entry ts=(\d+)/);
175
- entries.push({ ts: m ? Number(m[1]) : 0, text: chunk.join("\n") + "\n" });
176
- };
177
- for (let i = 0; i < lines.length; i++) {
178
- if (/^#talk-entry ts=\d+/.test(lines[i])) { close(i); start = i; }
179
- }
180
- close(lines.length);
181
- return entries;
182
- }
183
-
184
- /** 环形淘汰:先按时间窗口丢,再按字节预算从最老的丢(至少留最新 1 条);tmp+rename 原子替换。 */
185
- export function trimTalkLog(file, nowTs = Date.now()) {
186
- try {
187
- if (!existsSync(file)) return 0;
188
- const limit = nowTs - windowMs();
189
- const cap = maxBytes();
190
- const all = splitEntries(readFileSync(file, "utf8"));
191
- // 多进程(restart 交接窗口)可能交错追加 → 按 ts 重排,保证「环形」语义与阅读顺序一致
192
- let kept = all.filter((e) => e.ts > 0 && e.ts >= limit).sort((a, b) => a.ts - b.ts);
193
- if (kept.length === 0) { rmSync(file, { force: true }); oldestCache.delete(file); return all.length; }
194
- let bytes = Buffer.byteLength(kept.map((e) => e.text).join(""), "utf8");
195
- while (bytes > cap && kept.length > 1) bytes -= Buffer.byteLength(kept.shift().text, "utf8");
196
- const tmp = `${file}.${process.pid}.trim`;
197
- writeFileSync(tmp, kept.map((e) => e.text).join(""), { mode: 0o600 });
198
- renameSync(tmp, file);
199
- oldestCache.set(file, kept[0].ts);
200
- return all.length - kept.length;
201
- } catch { return 0; }
202
- }
203
-
204
- /**
205
- * relay 落盘的唯一入口:把 relay() 累积的 detail.talk(思考/正文/工具分片)拼成一条对话。
206
- * 空轮(零正文零思考零工具)不占环形窗口 —— 那是 relay-pipeline 已经另行报错的场景。
207
- * 桶的 capped/n/cap 必须一路传到 formatTalkEntry:撞顶的那条要能看出「后半截没了」。
208
- * attemptMs(本次上游尝试墙钟)由调用点传入;未传(拿不到尝试起点)回退 out.totalMs —— 此时 elapsed 与 relayMs 同值(spec 降级 scenario)。
209
- */
210
- export function recordRelayTalk({ reqId, api, model, via, hops, body, out, echo = {}, attemptMs, ttftMs, finishReason } = {}) {
211
- if (!talkLogEnabled()) return false;
212
- try {
213
- const talk = out?.detail?.talk;
214
- const answer = (talk?.content || []).join("");
215
- const thinking = (talk?.reasoning || []).join("");
216
- const tools = talk?.tools?.length ? JSON.stringify(talk.tools) : "";
217
- const status = out?.status;
218
- if (!answer && !thinking && !tools && Number(status) !== 200) return false;
219
- if (!answer && !thinking && !tools) return false;
220
- const ts = Date.now();
221
- const block = formatTalkEntry({
222
- reqId, api, model, via, hops, status, stream: Boolean(body?.stream), finishReason: finishReason ?? out?.detail?.sawFinishReason,
223
- elapsedMs: Number.isFinite(Number(attemptMs)) ? Number(attemptMs) : out?.totalMs, // 新口径:本次尝试墙钟优先(design D3)
224
- relayMs: out?.totalMs, ttftMs, question: lastUserQuestion(body || {}),
225
- thinking, answer, tools, usage: out?.detail?.usage, ...echo, ts,
226
- talkCapped: talk?.capped === true, talkChars: talk?.n, talkCap: talk?.cap, talkDropped: Number(talk?.dropped) || 0,
227
- });
228
- appendTalkEntry({ file: talkLogFile(model), block, atMs: ts });
229
- return true;
230
- } catch { return false; }
231
- }
232
-
233
- /** 读最近 N 条(倒序,最新在前)—— 给 CLI/排障用。 */
234
- export function recentBlocks({ model, n = 10, file } = {}) {
235
- try {
236
- const target = file || talkLogFile(model);
237
- if (!existsSync(target)) return [];
238
- return splitEntries(readFileSync(target, "utf8")).map((e) => e.text).reverse().slice(0, n);
239
- } catch { return []; }
240
- }
241
-
242
- /** 最近活跃的文件清单(哪些供应商×模型在这一小时里说过话)。 */
243
- export function listRecentFiles({ limit = 20 } = {}) {
244
- try {
245
- const dir = talkDir();
246
- if (!existsSync(dir)) return [];
247
- return readdirSync(dir, { withFileTypes: true })
248
- .filter((d) => d.isFile() && d.name.endsWith("-talk.log"))
249
- .map((d) => {
250
- let mtime = 0;
251
- try { mtime = statSync(join(dir, d.name)).mtimeMs; } catch {}
252
- return { name: d.name, mtime, time: fmtShanghaiYMDHMS(new Date(mtime)) };
253
- })
254
- .sort((a, b) => b.mtime - a.mtime)
255
- .slice(0, limit);
256
- } catch { return []; }
257
- }
1
+ // 环形对话日志(talk log):按「供应商-模型-talk.log」分文件,只保留最近 1 小时的完整对话。
2
+ // 记三件事:我问了什么 / 模型怎么想(reasoning)/ 模型怎么答(content),供人工调研模型的回答方式。
3
+ // 与 model-trace 相反:那边刻意不落正文(「加日志」≠「倒数据」),这里专门落正文 —— 所以必须脱敏 + 封顶。
4
+ // 默认开;MSLXDFF_TALK_LOG=0 关闭,窗口 MSLXDFF_TALK_LOG_WINDOW_MIN(分,默认 60),单文件 MSLXDFF_TALK_LOG_MAX_KB(默认 5MB)。
5
+ import { appendFileSync, chmodSync, closeSync, existsSync, mkdirSync, openSync, readFileSync, readdirSync, readSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
6
+ import { dirname, join } from "node:path";
7
+ import { logDir } from "./logs.js";
8
+ import { fmtShanghaiYMDHMS } from "./time.js";
9
+
10
+ const SUBDIR = "talk";
11
+ const DEFAULT_WINDOW_MIN = 60;
12
+ const DEFAULT_MAX_KB = 5 * 1024; // 5MB per talk-log file (ring buffer)
13
+ const MARK = "#talk-entry";
14
+ // 正常路径只 append;整文件重写(环形淘汰)只在「最老条目出窗」或「超字节上限」时发生。
15
+ // oldestCache 记住每个文件当前最老的 ts,避免每次写入都回读全文件。
16
+ const oldestCache = new Map();
17
+
18
+ export function talkLogEnabled() { return String(process.env.MSLXDFF_TALK_LOG ?? "1") !== "0"; }
19
+ function windowMs() { const n = Number(process.env.MSLXDFF_TALK_LOG_WINDOW_MIN); return Number.isFinite(n) && n > 0 ? n * 60000 : DEFAULT_WINDOW_MIN * 60000; }
20
+ function maxBytes() { const n = Number(process.env.MSLXDFF_TALK_LOG_MAX_KB); return Number.isFinite(n) && n > 0 ? n * 1024 : DEFAULT_MAX_KB * 1024; }
21
+ /** 仅测试用:进程重启等价物(环形窗口的内存基线清空)。 */
22
+ export function resetTalkLogCache() { oldestCache.clear(); }
23
+
24
+ /** 供应商-模型-talk.log:qoder/qfmodel → qoder-qfmodel-talk.log;裸 id(免费池)直接用一个名字段。 */
25
+ export function talkLogName(model) {
26
+ const s = String(model || "unknown").trim().toLowerCase();
27
+ const segs = s.includes("/") ? s.split("/").filter((x) => x && x !== "unknown") : [s];
28
+ const safe = segs.join("-").replace(/[^a-z0-9._-]+/g, "-").replace(/^[-.]+|[-.]+$/g, "").slice(0, 180);
29
+ return `${safe || "unknown"}-talk.log`;
30
+ }
31
+
32
+ export function talkDir() { return join(logDir(), SUBDIR); }
33
+ export function talkLogFile(model) { return join(talkDir(), talkLogName(model)); }
34
+
35
+ // 正文里的凭据一律先抹再落盘(保留换行,不像 model-trace 那样压成一行 —— 这里要读的是回答本身)。
36
+ const MASKS = [
37
+ [/([?&](?:token|refresh[_-]?token|access[_-]?token|api[_-]?key|apikey|key|secret|password|cookie)=)[^\s&]+/gi, "$1[已脱敏]"],
38
+ [/(authorization|bearer|proxy-authorization|cookie|x-api-key)\s*[:=]\s*[^\s,;]+/gi, "$1=[已脱敏]"],
39
+ [/("(?:apiKey|api_key|accessToken|refreshToken|secret|password|client_secret)"\s*:\s*)"[^"]*"/gi, '$1"[已脱敏]"'],
40
+ [/\beyJ[\w-]{6,}\.[\w-]{6,}\.[\w-]{6,}\b/g, "[已脱敏-JWT]"],
41
+ [/\b(?:sk|pk|ghp|gho|ghu|ghs|ghr|xox[baprs])-[\w-]{10,}\b/g, "[已脱敏-key]"],
42
+ // 裸 k=v(提问里常出现 token=…、password=…):只掐足够长的值,避免把「token 数量」这类正常技术讨论也糊掉
43
+ [/\b(token|accessToken|refreshToken|api[_-]?key|apiKey|secret|password|cookie|client_secret|authorization)\s*[:=]\s*[\w./+-]{12,}/gi, "$1=[已脱敏]"],
44
+ ];
45
+
46
+ const CUT_MARK = "\n…[已截断]";
47
+
48
+ /** 脱敏 + 按字节封顶截断(中文 3 字节/字,逐字累加才不会超预算),并报告这一刀是否真的落下。 */
49
+ function maskCut(v, cap = 100000) {
50
+ let s = String(v ?? "");
51
+ for (const [re, repl] of MASKS) s = s.replace(re, repl);
52
+ if (Buffer.byteLength(s, "utf8") <= cap) return { text: s, capped: false };
53
+ const tail = Buffer.from(CUT_MARK, "utf8");
54
+ const budget = Math.max(0, cap - tail.length);
55
+ let bytes = 0, i = 0;
56
+ for (const ch of s) {
57
+ const n = Buffer.byteLength(ch, "utf8");
58
+ if (bytes + n > budget) break;
59
+ bytes += n;
60
+ i += ch.length;
61
+ }
62
+ return { text: s.slice(0, i) + CUT_MARK, capped: true };
63
+ }
64
+
65
+ /** 脱敏 + 按字节封顶截断(对外口径与改前逐字一致;要知道是否砍过用 maskCut/pushBlock)。 */
66
+ export function maskText(v, cap = 100000) { return maskCut(v, cap).text; }
67
+
68
+ function textOf(content) {
69
+ if (typeof content === "string") return content;
70
+ if (Array.isArray(content)) return content.map((p) => p?.text || (p?.image_url ? `[图片 ${String(p.image_url.url).slice(0, 60)}]` : "")).filter(Boolean).join("\n");
71
+ return "";
72
+ }
73
+
74
+ /** 只取「我发给大模型的问题」= 最后一条 user 消息(agent 回路每轮都带全量历史,全落会把环形窗口占满)。 */
75
+ export function lastUserQuestion(body) {
76
+ const msgs = Array.isArray(body?.messages) ? body.messages : null;
77
+ if (msgs) { for (let i = msgs.length - 1; i >= 0; i--) if (msgs[i]?.role === "user") return textOf(msgs[i].content); }
78
+ const inp = body?.input; // Responses 端点形状兜底
79
+ if (typeof inp === "string") return inp;
80
+ if (Array.isArray(inp)) { for (let i = inp.length - 1; i >= 0; i--) if (inp[i]?.role === "user") return textOf(inp[i].content); }
81
+ return "";
82
+ }
83
+
84
+ const CUT_IN = "----8<----", CUT_OUT = "---->8----";
85
+
86
+ // 标签行的「N 字」在字节封顶之后只是**已落盘**的字数;不带封顶标记,就会被读成「模型只说了这么多」。
87
+ function pushBlock(out, label, text, cap) {
88
+ const { text: m, capped } = maskCut(text, cap);
89
+ if (!m.trim()) return false;
90
+ out.push(`[${label} · ${m.length} 字${capped ? " · 已封顶" : ""}]`, CUT_IN, m, CUT_OUT, "");
91
+ return capped;
92
+ }
93
+
94
+ // 撞捕获上限时紧跟头部行的人类可读提醒:不必先认识 truncated=1 这个键名也能一眼看出「可能不完整」。
95
+ const TRUNC_NOTE = "[!] 本轮已撞捕获上限(truncated=1):思考/正文后半段未落盘,内容可能不完整 —— 别当成模型的完整回答";
96
+
97
+ /** 一条对话 = 一个可读块:头部一行元信息(ts= 是环形淘汰的唯一依据),正文三段。elapsed= 取本次尝试墙钟(design D3),finish=/relayMs= 为 ADR-0046 新读数,api= 为入站协议门短码(认不出的路 = other;未拿到判定值则整段省略,绝不伪造)。 */
98
+ export function formatTalkEntry({ reqId, api, model, via, hops, status, stream, elapsedMs, question, thinking, answer, tools, usage, upstream, account, pick, ts, talkCapped = false, talkChars, talkCap, talkDropped, finishReason, relayMs, ttftMs } = {}) {
99
+ const fval = finishReason || "-"; // 缺值占位(grill Q5):「没给 finish」不得被读成 stop;字段恒在场
100
+ const now = Number.isFinite(ts) && ts > 0 ? ts : Date.now();
101
+ const trunc = talkCapped === true;
102
+ const head = [
103
+ `${MARK} ts=${now}`, `time=${fmtShanghaiYMDHMS(new Date(now))}`, `req=${reqId || "-"}`, `model=${model || "-"}`,
104
+ api ? `api=${api}` : "", via ? `via=${via}` : "", hops != null ? `hops=${hops}` : "", `status=${status ?? "-"}`,
105
+ stream ? "stream=1" : "stream=0", Number.isFinite(elapsedMs) ? `elapsed=${Math.round(elapsedMs)}ms` : "",
106
+ // 尝试墙钟与 relay 内部计时同值时不重复打印(头行噪音克制,grill Q3);两值不同(hedge/缓冲重放)才另列 relayMs
107
+ Number.isFinite(relayMs) && Number.isFinite(elapsedMs) && Math.round(relayMs) !== Math.round(elapsedMs) ? `relayMs=${Math.round(relayMs)}ms` : "",
108
+ // 首字耗时(客户端从本次尝试起点到网关写出第一帧,含上游 prefill):「答案忽然冒出」类问题的第一读数。
109
+ // 只在真有值时出现(非流式/拿不到首块 = null,不打印而非写 0,0 会被读成「秒回」)。
110
+ Number.isFinite(ttftMs) ? `ttft=${Math.round(ttftMs)}ms` : "",
111
+ `finish=${fval}`,
112
+ usage ? `usage(prompt=${usage.prompt_tokens ?? "-"},completion=${usage.completion_tokens ?? "-"})` : "",
113
+ upstream ? `upstream=${upstream}` : "", account ? `account=${account}` : "", pick ? `pick=${pick}` : "",
114
+ // 截断读数只在真截断时出现(未截断不得新增噪音字段):cap= 上限、chars= 已捕获字符数
115
+ trunc ? `truncated=1 cap=${Number.isFinite(talkCap) && talkCap > 0 ? talkCap : "-"} chars=${Number.isFinite(talkChars) ? talkChars : "-"}` : "",
116
+ Number(talkDropped) > 0 ? `dropped=${Math.round(Number(talkDropped))}` : "", // 不可逆丢弃帧计数:只在真丢时出现(与 truncated 同「未丢不添噪音」口径,review P1-1)
117
+ ].filter(Boolean).join(" ");
118
+ const out = [head];
119
+ if (trunc) out.push(TRUNC_NOTE);
120
+ out.push("");
121
+ pushBlock(out, "我问", question, 20000);
122
+ pushBlock(out, "思考", thinking, 100000);
123
+ // 零正文不再静默跳块(spec「零输出轮必须可判读」/grill Q4):缺块与「正文该有却没落盘」同形,缺席必须是可读读数。
124
+ // 只落一行标注 + 空行(没有内容可围,不套 CUT 围栏);有真实正文的字符串照旧走 pushBlock,该段与改动前逐字节一致。
125
+ // 判据看 trim 不看长度(P1-B):" "/"\n" 进 pushBlock 也会被 !m.trim() 整块跳过 → 与「正文丢盘」再度同形,而「只调工具 + 吐个换行」是真实 agent 形态。
126
+ if (!String(answer ?? "").trim()) out.push(`[回答 · 0 字 · 本轮无正文(finish=${fval})]`, "");
127
+ else pushBlock(out, "回答", answer, 200000);
128
+ const t = tools == null || tools === "" ? "" : String(tools);
129
+ pushBlock(out, "工具调用", t, 20000);
130
+ out.push("[END]", "");
131
+ return out.join("\n") + "\n";
132
+ }
133
+
134
+ /** 只读文件头 8KB 取最老一条的 ts(进程重启后重建内存基线,不必回读全文件)。 */
135
+ function firstTs(file) {
136
+ let fd = null;
137
+ try {
138
+ fd = openSync(file, "r");
139
+ const buf = Buffer.alloc(8192);
140
+ const n = readSync(fd, buf, 0, 8192, 0);
141
+ const m = buf.subarray(0, n).toString("utf8").match(/^#talk-entry ts=(\d+)/m);
142
+ return m ? Number(m[1]) : null;
143
+ } catch { return null; }
144
+ finally { if (fd != null) { try { closeSync(fd); } catch {} } }
145
+ }
146
+
147
+ /** 追加一条(正常路径只 append;到窗口或超预算才整写淘汰)。 */
148
+ export function appendTalkEntry({ file, block, atMs } = {}) {
149
+ try {
150
+ if (!file || !block) return 0;
151
+ mkdirSync(dirname(file), { recursive: true });
152
+ const isNew = !existsSync(file);
153
+ appendFileSync(file, block);
154
+ // mode 只在新建时生效;存量文件补一次 chmod(POSIX;Windows 无意义)
155
+ if (isNew) { try { chmodSync(file, 0o600); } catch {} }
156
+ const now = Number.isFinite(atMs) && atMs > 0 ? atMs : Date.now();
157
+ let base = oldestCache.get(file);
158
+ if (base == null) { base = firstTs(file); if (base == null) base = now; }
159
+ const size = (() => { try { return statSync(file).size; } catch { return 0; } })();
160
+ if (now - base > windowMs() || size > maxBytes()) trimTalkLog(file, now);
161
+ else oldestCache.set(file, Math.min(base, now));
162
+ return 1;
163
+ } catch { return 0; }
164
+ }
165
+
166
+ /** 按 `#talk-entry` 头行切块(内容行理论上可能撞前缀,故要求严格 ts= 形状才认头)。 */
167
+ export function splitEntries(txt) {
168
+ const entries = [];
169
+ const lines = String(txt ?? "").split("\n");
170
+ let start = -1;
171
+ const close = (end) => {
172
+ if (start < 0) return;
173
+ const chunk = lines.slice(start, end);
174
+ const m = chunk[0].match(/^#talk-entry ts=(\d+)/);
175
+ entries.push({ ts: m ? Number(m[1]) : 0, text: chunk.join("\n") + "\n" });
176
+ };
177
+ for (let i = 0; i < lines.length; i++) {
178
+ if (/^#talk-entry ts=\d+/.test(lines[i])) { close(i); start = i; }
179
+ }
180
+ close(lines.length);
181
+ return entries;
182
+ }
183
+
184
+ /** 环形淘汰:先按时间窗口丢,再按字节预算从最老的丢(至少留最新 1 条);tmp+rename 原子替换。 */
185
+ export function trimTalkLog(file, nowTs = Date.now()) {
186
+ try {
187
+ if (!existsSync(file)) return 0;
188
+ const limit = nowTs - windowMs();
189
+ const cap = maxBytes();
190
+ const all = splitEntries(readFileSync(file, "utf8"));
191
+ // 多进程(restart 交接窗口)可能交错追加 → 按 ts 重排,保证「环形」语义与阅读顺序一致
192
+ let kept = all.filter((e) => e.ts > 0 && e.ts >= limit).sort((a, b) => a.ts - b.ts);
193
+ if (kept.length === 0) { rmSync(file, { force: true }); oldestCache.delete(file); return all.length; }
194
+ let bytes = Buffer.byteLength(kept.map((e) => e.text).join(""), "utf8");
195
+ while (bytes > cap && kept.length > 1) bytes -= Buffer.byteLength(kept.shift().text, "utf8");
196
+ const tmp = `${file}.${process.pid}.trim`;
197
+ writeFileSync(tmp, kept.map((e) => e.text).join(""), { mode: 0o600 });
198
+ renameSync(tmp, file);
199
+ oldestCache.set(file, kept[0].ts);
200
+ return all.length - kept.length;
201
+ } catch { return 0; }
202
+ }
203
+
204
+ /**
205
+ * relay 落盘的唯一入口:把 relay() 累积的 detail.talk(思考/正文/工具分片)拼成一条对话。
206
+ * 空轮(零正文零思考零工具)不占环形窗口 —— 那是 relay-pipeline 已经另行报错的场景。
207
+ * 桶的 capped/n/cap 必须一路传到 formatTalkEntry:撞顶的那条要能看出「后半截没了」。
208
+ * attemptMs(本次上游尝试墙钟)由调用点传入;未传(拿不到尝试起点)回退 out.totalMs —— 此时 elapsed 与 relayMs 同值(spec 降级 scenario)。
209
+ */
210
+ export function recordRelayTalk({ reqId, api, model, via, hops, body, out, echo = {}, attemptMs, ttftMs, finishReason } = {}) {
211
+ if (!talkLogEnabled()) return false;
212
+ try {
213
+ const talk = out?.detail?.talk;
214
+ const answer = (talk?.content || []).join("");
215
+ const thinking = (talk?.reasoning || []).join("");
216
+ const tools = talk?.tools?.length ? JSON.stringify(talk.tools) : "";
217
+ const status = out?.status;
218
+ if (!answer && !thinking && !tools && Number(status) !== 200) return false;
219
+ if (!answer && !thinking && !tools) return false;
220
+ const ts = Date.now();
221
+ const block = formatTalkEntry({
222
+ reqId, api, model, via, hops, status, stream: Boolean(body?.stream), finishReason: finishReason ?? out?.detail?.sawFinishReason,
223
+ elapsedMs: Number.isFinite(Number(attemptMs)) ? Number(attemptMs) : out?.totalMs, // 新口径:本次尝试墙钟优先(design D3)
224
+ relayMs: out?.totalMs, ttftMs, question: lastUserQuestion(body || {}),
225
+ thinking, answer, tools, usage: out?.detail?.usage, ...echo, ts,
226
+ talkCapped: talk?.capped === true, talkChars: talk?.n, talkCap: talk?.cap, talkDropped: Number(talk?.dropped) || 0,
227
+ });
228
+ appendTalkEntry({ file: talkLogFile(model), block, atMs: ts });
229
+ return true;
230
+ } catch { return false; }
231
+ }
232
+
233
+ /** 读最近 N 条(倒序,最新在前)—— 给 CLI/排障用。 */
234
+ export function recentBlocks({ model, n = 10, file } = {}) {
235
+ try {
236
+ const target = file || talkLogFile(model);
237
+ if (!existsSync(target)) return [];
238
+ return splitEntries(readFileSync(target, "utf8")).map((e) => e.text).reverse().slice(0, n);
239
+ } catch { return []; }
240
+ }
241
+
242
+ /** 最近活跃的文件清单(哪些供应商×模型在这一小时里说过话)。 */
243
+ export function listRecentFiles({ limit = 20 } = {}) {
244
+ try {
245
+ const dir = talkDir();
246
+ if (!existsSync(dir)) return [];
247
+ return readdirSync(dir, { withFileTypes: true })
248
+ .filter((d) => d.isFile() && d.name.endsWith("-talk.log"))
249
+ .map((d) => {
250
+ let mtime = 0;
251
+ try { mtime = statSync(join(dir, d.name)).mtimeMs; } catch {}
252
+ return { name: d.name, mtime, time: fmtShanghaiYMDHMS(new Date(mtime)) };
253
+ })
254
+ .sort((a, b) => b.mtime - a.mtime)
255
+ .slice(0, limit);
256
+ } catch { return []; }
257
+ }
@@ -1,52 +1,52 @@
1
- // `/v1/messages` 面 token 估算的单一来源(ADR-0055):`count_tokens` 读数与响应 `usage` 兜底共用一个公式。
2
- // 共用面:`src/anthropic/count-tokens.js`(请求侧逐段累加,走 `estimateTextTokens`)、
3
- // `src/anthropic/translate.js`(上游未报 usage 时的兜底与流式生成侧累计,走 `createTokenCounter`)。
4
- // 两处必须同源,否则 `count_tokens` 与响应 `usage` 会各说一套读数
5
- // —— 同 ADR-0053 把 effort 词表收成单一来源的理由。
6
- //
7
- // 公式照抄参照实现 `wb_proxy.py:12841-12848`:`cjk + max(1, int(other / 3.6))`(空文本 0),
8
- // CJK 区间 = \u4e00-\u9fff 主区 + \u3400-\u4dbf 扩展 A。自造系数=造第二个真相源(design.md 决策 2)。
9
- // 实现不抄 `re.sub` 再数长度差:那要额外分配一份与原文等长的新串,本实现单遍扫描、无分配。
10
- // 与 Python 的两处已知差异(不影响本仓自洽,但别写成「逐值相等」):① astral 字符(emoji)JS 按
11
- // 2 个 UTF-16 单元计、Python 按 1 个码点计 —— emoji 密集文本可差到 2 倍以上;② falsy 非串(`0`/`false`)
12
- // Python 直接返回 0、本实现按 `String()` 后非空计 ≥1。现所有调用方传的都是字符串,两条均不可达。
13
-
14
- const CJK_HI_START = 0x4e00;
15
- const CJK_HI_END = 0x9fff; // 主区,占实际中文文本的绝对多数
16
- const CJK_EXT_A_START = 0x3400;
17
- const CJK_EXT_A_END = 0x4dbf;
18
- const OTHER_CHARS_PER_TOKEN = 3.6;
19
-
20
- /**
21
- * 跨多段累计的计数器。逐段各套一次公式会把参照实现的 `max(1, …)` 下限乘上段数
22
- * (流式按 1~3 字一帧下发时,1000 个 ASCII 字会被估成 1000 tokens 而非 277),
23
- * 所以累计的是字数、最后一次性套公式。
24
- * @returns {{ add: (text: unknown) => void, tokens: () => number }}
25
- */
26
- export function createTokenCounter() {
27
- let cjk = 0;
28
- let other = 0;
29
- return {
30
- add(text) {
31
- if (text == null) return;
32
- const s = typeof text === "string" ? text : String(text);
33
- for (let i = 0; i < s.length; i++) {
34
- const c = s.charCodeAt(i);
35
- if (c >= CJK_HI_START && c <= CJK_HI_END) cjk++;
36
- else if (c >= CJK_EXT_A_START && c <= CJK_EXT_A_END) cjk++;
37
- else other++;
38
- }
39
- },
40
- tokens() {
41
- if (cjk + other <= 0) return 0; // 空文本给 0,不吃 +1 下限
42
- return cjk + Math.max(1, Math.floor(other / OTHER_CHARS_PER_TOKEN));
43
- },
44
- };
45
- }
46
-
47
- /** 单段文本的 token 估算(CJK 每字 1 token,其余每 3.6 字符 1 token,非空至少 1)。 */
48
- export function estimateTextTokens(text) {
49
- const counter = createTokenCounter();
50
- counter.add(text);
51
- return counter.tokens();
52
- }
1
+ // `/v1/messages` 面 token 估算的单一来源(ADR-0055):`count_tokens` 读数与响应 `usage` 兜底共用一个公式。
2
+ // 共用面:`src/anthropic/count-tokens.js`(请求侧逐段累加,走 `estimateTextTokens`)、
3
+ // `src/anthropic/translate.js`(上游未报 usage 时的兜底与流式生成侧累计,走 `createTokenCounter`)。
4
+ // 两处必须同源,否则 `count_tokens` 与响应 `usage` 会各说一套读数
5
+ // —— 同 ADR-0053 把 effort 词表收成单一来源的理由。
6
+ //
7
+ // 公式照抄参照实现 `wb_proxy.py:12841-12848`:`cjk + max(1, int(other / 3.6))`(空文本 0),
8
+ // CJK 区间 = \u4e00-\u9fff 主区 + \u3400-\u4dbf 扩展 A。自造系数=造第二个真相源(design.md 决策 2)。
9
+ // 实现不抄 `re.sub` 再数长度差:那要额外分配一份与原文等长的新串,本实现单遍扫描、无分配。
10
+ // 与 Python 的两处已知差异(不影响本仓自洽,但别写成「逐值相等」):① astral 字符(emoji)JS 按
11
+ // 2 个 UTF-16 单元计、Python 按 1 个码点计 —— emoji 密集文本可差到 2 倍以上;② falsy 非串(`0`/`false`)
12
+ // Python 直接返回 0、本实现按 `String()` 后非空计 ≥1。现所有调用方传的都是字符串,两条均不可达。
13
+
14
+ const CJK_HI_START = 0x4e00;
15
+ const CJK_HI_END = 0x9fff; // 主区,占实际中文文本的绝对多数
16
+ const CJK_EXT_A_START = 0x3400;
17
+ const CJK_EXT_A_END = 0x4dbf;
18
+ const OTHER_CHARS_PER_TOKEN = 3.6;
19
+
20
+ /**
21
+ * 跨多段累计的计数器。逐段各套一次公式会把参照实现的 `max(1, …)` 下限乘上段数
22
+ * (流式按 1~3 字一帧下发时,1000 个 ASCII 字会被估成 1000 tokens 而非 277),
23
+ * 所以累计的是字数、最后一次性套公式。
24
+ * @returns {{ add: (text: unknown) => void, tokens: () => number }}
25
+ */
26
+ export function createTokenCounter() {
27
+ let cjk = 0;
28
+ let other = 0;
29
+ return {
30
+ add(text) {
31
+ if (text == null) return;
32
+ const s = typeof text === "string" ? text : String(text);
33
+ for (let i = 0; i < s.length; i++) {
34
+ const c = s.charCodeAt(i);
35
+ if (c >= CJK_HI_START && c <= CJK_HI_END) cjk++;
36
+ else if (c >= CJK_EXT_A_START && c <= CJK_EXT_A_END) cjk++;
37
+ else other++;
38
+ }
39
+ },
40
+ tokens() {
41
+ if (cjk + other <= 0) return 0; // 空文本给 0,不吃 +1 下限
42
+ return cjk + Math.max(1, Math.floor(other / OTHER_CHARS_PER_TOKEN));
43
+ },
44
+ };
45
+ }
46
+
47
+ /** 单段文本的 token 估算(CJK 每字 1 token,其余每 3.6 字符 1 token,非空至少 1)。 */
48
+ export function estimateTextTokens(text) {
49
+ const counter = createTokenCounter();
50
+ counter.add(text);
51
+ return counter.tokens();
52
+ }