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
@@ -52,7 +52,7 @@ export function peerStageCtx(ctx = {}) {
52
52
  // 连接级超时(DNS + TCP/TLS 握手):黑洞节点(端口挂起)必须快速失败,
53
53
  // 不能占用 30s 的响应超时(曾把一次请求拖到 92s)。仅 undici 可用时生效。
54
54
  let peerDispatcher = null;
55
- function getPeerDispatcher() {
55
+ export function getPeerDispatcher() {
56
56
  if (peerDispatcher) return peerDispatcher;
57
57
  const { Agent } = getUndici();
58
58
  if (!Agent) return null;
@@ -68,6 +68,28 @@ function getPeerDispatcher() {
68
68
  return peerDispatcher;
69
69
  }
70
70
 
71
+ /**
72
+ * 保热:用**组员转发同一条连接池**(getPeerDispatcher)打 GET /health,把 (本机→组员) 的连接
73
+ * 保持在温热、且**正是 chat 会复用的那条 socket**(裸 fetch 的全局池热的是另一条,等于白热)。
74
+ * /health 无鉴权、无副作用、零额度。绝不抛;返回 {ok}。
75
+ */
76
+ export async function warmPeerHealth(url, { timeoutMs = 5000 } = {}) {
77
+ const base = String(url || "").replace(/\/+$/, "");
78
+ if (!base) return { ok: false, error: "no url" };
79
+ const dispatcher = getPeerDispatcher();
80
+ try {
81
+ const res = await compatFetch(`${base}/health`, {
82
+ headers: { Accept: "application/json" },
83
+ signal: timeoutSignal(timeoutMs),
84
+ ...(dispatcher ? { dispatcher } : {}),
85
+ });
86
+ try { if (res.body) await res.text().catch(() => {}); } catch {}
87
+ return { ok: Boolean(res?.ok), status: res?.status };
88
+ } catch (e) {
89
+ return { ok: false, error: errMsg(e) };
90
+ }
91
+ }
92
+
71
93
  // 跨 caller 中继必须剥离 reasoning 加密态:encrypted_content 由上游按 caller(出口)签发,
72
94
  // 换个节点转发会被拒(400 "reasoning encrypted_content was not issued to this caller")。
73
95
  // 明文 reasoning_content 保留(不绑定 caller,thinking 模式需要)。
@@ -1,181 +1,181 @@
1
- import { loadGroupsJoined } from "../state.js";
2
- import { compatFetch } from "../compat.js";
3
-
4
- const relayPending = new Map();
5
- const relayPendingByReqId = new Map();
6
- const streamSubscribers = new Map(); // key group::target -> Set<res>
7
-
8
- function streamKey(group, target) { return `${group}::${target}`; }
9
-
10
- export function subscribeStream({ group, target, res }) {
11
- const k = streamKey(group, target);
12
- const set = streamSubscribers.get(k) || new Set();
13
- set.add(res);
14
- streamSubscribers.set(k, set);
15
- }
16
-
17
- export function unsubscribeStream({ group, target, res }) {
18
- const k = streamKey(group, target);
19
- const set = streamSubscribers.get(k);
20
- if (!set) return;
21
- set.delete(res);
22
- if (set.size === 0) streamSubscribers.delete(k);
23
- }
24
-
25
- export function clearStreamSubscribers() { streamSubscribers.clear(); }
26
- export function getStreamSubscribers() { return streamSubscribers; }
27
-
28
- export function pushToStream({ group, target, entry }) {
29
- const k = streamKey(group, target);
30
- const set = streamSubscribers.get(k);
31
- if (!set || set.size === 0) return false;
32
- const payload = JSON.stringify({ reqId: entry.reqId, body: entry.body, hops: entry.hops });
33
- const frame = `event: relay\ndata: ${payload}\n\n`;
34
- let pushed = 0;
35
- for (const res of [...set]) {
36
- try {
37
- res.write(frame);
38
- pushed++;
39
- } catch {
40
- set.delete(res);
41
- }
42
- }
43
- if (set.size === 0) streamSubscribers.delete(k);
44
- return pushed > 0;
45
- }
46
-
47
- export function enqueueRelay({ group, target, reqId, body, hops }) {
48
- const key = `${group}::${target}`;
49
- // try stream push first — if any subscriber, push and don't enqueue to poll queue
50
- const entryForPush = { reqId, body, hops };
51
- if (pushToStream({ group, target, entry: entryForPush })) {
52
- return new Promise((resolve, reject) => {
53
- const timer = setTimeout(() => {
54
- relayPendingByReqId.delete(reqId);
55
- reject(new Error("relay timeout"));
56
- }, 30_000);
57
- timer.unref?.();
58
- const entry = { reqId, body, hops, resolve, reject, timer };
59
- relayPendingByReqId.set(reqId, entry);
60
- });
61
- }
62
- const list = relayPending.get(key) || [];
63
- return new Promise((resolve, reject) => {
64
- const timer = setTimeout(() => {
65
- const idx = list.findIndex((e) => e.reqId === reqId);
66
- if (idx >= 0) list.splice(idx, 1);
67
- relayPendingByReqId.delete(reqId);
68
- reject(new Error("relay timeout"));
69
- }, 30_000);
70
- timer.unref?.();
71
- const entry = { reqId, body, hops, resolve, reject, timer };
72
- list.push(entry);
73
- relayPending.set(key, list);
74
- relayPendingByReqId.set(reqId, entry);
75
- });
76
- }
77
-
78
- export function dequeueRelayForPoll({ group, target, limit = 10 }) {
79
- const key = `${group}::${target}`;
80
- const list = relayPending.get(key) || [];
81
- const batch = list.splice(0, limit);
82
- if (list.length) relayPending.set(key, list);
83
- else relayPending.delete(key);
84
- return batch.map((e) => ({ reqId: e.reqId, body: e.body, hops: e.hops }));
85
- }
86
-
87
- export function resolveRelay(reqId, result) {
88
- const entry = relayPendingByReqId.get(reqId);
89
- if (!entry) return false;
90
- clearTimeout(entry.timer);
91
- relayPendingByReqId.delete(reqId);
92
- entry.resolve(result);
93
- return true;
94
- }
95
-
96
- export function getRelayPending() { return relayPending; }
97
- export function getRelayPendingByReqId() { return relayPendingByReqId; }
98
-
99
- export async function tryBroadbandRelay({ groups, token: myToken, model, body, hops, bus, logs, reqId, evt, res, mark, perf0, stages }) {
100
- try {
101
- const joined = loadGroupsJoined();
102
- const broadbandGroups = joined.filter((g) => g.kind === "broadband" || g.myUrl?.startsWith("relay://"));
103
- const allCandidates = [];
104
- if (groups) {
105
- const localGroups = groups.list();
106
- for (const [gName, g] of Object.entries(localGroups)) {
107
- for (const [id, m] of Object.entries(g.members || {})) {
108
- if (id === "leader") continue;
109
- const isBb = m?.kind === "broadband" || String(m?.url || "").startsWith("relay://");
110
- if (!isBb) continue;
111
- const staleMs = Number(process.env.MSLXDFF_BROADBAND_STALE_MS) > 0 ? Number(process.env.MSLXDFF_BROADBAND_STALE_MS) : 90_000;
112
- if (typeof m.lastSeen === "number" && Date.now() - m.lastSeen > staleMs) continue;
113
- allCandidates.push({ group: gName, target: m.url, member: m, via: "local-leader", leaderUrl: null });
114
- }
115
- }
116
- }
117
- for (const g of joined) {
118
- if (!g.leaderUrl) continue;
119
- try {
120
- const controller = new AbortController();
121
- const timer = setTimeout(() => controller.abort(), 5000);
122
- const r = await compatFetch(`${g.leaderUrl}/v1/groups/join`, {
123
- method: "POST",
124
- headers: { "Content-Type": "application/json", "Authorization": `Bearer ${myToken}` },
125
- body: JSON.stringify({ name: g.name, memberName: g.memberName, url: g.myUrl, token: myToken }),
126
- signal: controller.signal,
127
- });
128
- clearTimeout(timer);
129
- if (!r.ok) continue;
130
- const data = await r.json().catch(() => ({}));
131
- const members = data.members || {};
132
- for (const [id, m] of Object.entries(members)) {
133
- if (id === "leader") continue;
134
- const isBb = m?.kind === "broadband" || String(m?.url || "").startsWith("relay://");
135
- if (!isBb) continue;
136
- if (m.url === g.myUrl) continue;
137
- const staleMs = Number(process.env.MSLXDFF_BROADBAND_STALE_MS) > 0 ? Number(process.env.MSLXDFF_BROADBAND_STALE_MS) : 90_000;
138
- if (typeof m.lastSeen === "number" && Date.now() - m.lastSeen > staleMs) continue;
139
- allCandidates.push({ group: g.name, target: m.url, member: m, via: "via-leader", leaderUrl: g.leaderUrl });
140
- }
141
- } catch {}
142
- }
143
- if (!allCandidates.length) return null;
144
- for (const cand of allCandidates) {
145
- try {
146
- evt?.("relay-try", { reqId, model, via: cand.via, target: cand.target, group: cand.group });
147
- if (!cand.leaderUrl) {
148
- const fwdBody = { model, ...body, model };
149
- const reqIdLocal = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
150
- const promise = enqueueRelay({ group: cand.group, target: cand.target, reqId: reqIdLocal, body: fwdBody, hops });
151
- const result = await promise;
152
- if (result && result.status) {
153
- return { via: "broadband-local", result, target: cand.target, group: cand.group };
154
- }
155
- } else {
156
- const ctrl = new AbortController();
157
- const t = setTimeout(() => ctrl.abort(), 35_000);
158
- const r = await compatFetch(`${cand.leaderUrl}/v1/groups/relay/forward`, {
159
- method: "POST",
160
- headers: { "Content-Type": "application/json", "Authorization": `Bearer ${myToken}`, "x-mslxdff-hops": String(hops + 1) },
161
- body: JSON.stringify({ group: cand.group, target: cand.target, body: { ...body, model }, hops: hops + 1, reqId }),
162
- signal: ctrl.signal,
163
- });
164
- clearTimeout(t);
165
- if (!r.ok) {
166
- const txt = await r.text().catch(() => "");
167
- evt?.("relay-fail", { reqId, model, via: cand.via, target: cand.target, status: r.status, message: txt.slice(0, 200) });
168
- continue;
169
- }
170
- return { via: "broadband-via-leader", result: r, target: cand.target, group: cand.group };
171
- }
172
- } catch (err) {
173
- evt?.("relay-fail", { reqId, model, via: cand.via, target: cand.target, message: String(err?.message || err).slice(0, 200) });
174
- continue;
175
- }
176
- }
177
- return null;
178
- } catch {
179
- return null;
180
- }
181
- }
1
+ import { loadGroupsJoined } from "../state.js";
2
+ import { compatFetch } from "../compat.js";
3
+
4
+ const relayPending = new Map();
5
+ const relayPendingByReqId = new Map();
6
+ const streamSubscribers = new Map(); // key group::target -> Set<res>
7
+
8
+ function streamKey(group, target) { return `${group}::${target}`; }
9
+
10
+ export function subscribeStream({ group, target, res }) {
11
+ const k = streamKey(group, target);
12
+ const set = streamSubscribers.get(k) || new Set();
13
+ set.add(res);
14
+ streamSubscribers.set(k, set);
15
+ }
16
+
17
+ export function unsubscribeStream({ group, target, res }) {
18
+ const k = streamKey(group, target);
19
+ const set = streamSubscribers.get(k);
20
+ if (!set) return;
21
+ set.delete(res);
22
+ if (set.size === 0) streamSubscribers.delete(k);
23
+ }
24
+
25
+ export function clearStreamSubscribers() { streamSubscribers.clear(); }
26
+ export function getStreamSubscribers() { return streamSubscribers; }
27
+
28
+ export function pushToStream({ group, target, entry }) {
29
+ const k = streamKey(group, target);
30
+ const set = streamSubscribers.get(k);
31
+ if (!set || set.size === 0) return false;
32
+ const payload = JSON.stringify({ reqId: entry.reqId, body: entry.body, hops: entry.hops });
33
+ const frame = `event: relay\ndata: ${payload}\n\n`;
34
+ let pushed = 0;
35
+ for (const res of [...set]) {
36
+ try {
37
+ res.write(frame);
38
+ pushed++;
39
+ } catch {
40
+ set.delete(res);
41
+ }
42
+ }
43
+ if (set.size === 0) streamSubscribers.delete(k);
44
+ return pushed > 0;
45
+ }
46
+
47
+ export function enqueueRelay({ group, target, reqId, body, hops }) {
48
+ const key = `${group}::${target}`;
49
+ // try stream push first — if any subscriber, push and don't enqueue to poll queue
50
+ const entryForPush = { reqId, body, hops };
51
+ if (pushToStream({ group, target, entry: entryForPush })) {
52
+ return new Promise((resolve, reject) => {
53
+ const timer = setTimeout(() => {
54
+ relayPendingByReqId.delete(reqId);
55
+ reject(new Error("relay timeout"));
56
+ }, 30_000);
57
+ timer.unref?.();
58
+ const entry = { reqId, body, hops, resolve, reject, timer };
59
+ relayPendingByReqId.set(reqId, entry);
60
+ });
61
+ }
62
+ const list = relayPending.get(key) || [];
63
+ return new Promise((resolve, reject) => {
64
+ const timer = setTimeout(() => {
65
+ const idx = list.findIndex((e) => e.reqId === reqId);
66
+ if (idx >= 0) list.splice(idx, 1);
67
+ relayPendingByReqId.delete(reqId);
68
+ reject(new Error("relay timeout"));
69
+ }, 30_000);
70
+ timer.unref?.();
71
+ const entry = { reqId, body, hops, resolve, reject, timer };
72
+ list.push(entry);
73
+ relayPending.set(key, list);
74
+ relayPendingByReqId.set(reqId, entry);
75
+ });
76
+ }
77
+
78
+ export function dequeueRelayForPoll({ group, target, limit = 10 }) {
79
+ const key = `${group}::${target}`;
80
+ const list = relayPending.get(key) || [];
81
+ const batch = list.splice(0, limit);
82
+ if (list.length) relayPending.set(key, list);
83
+ else relayPending.delete(key);
84
+ return batch.map((e) => ({ reqId: e.reqId, body: e.body, hops: e.hops }));
85
+ }
86
+
87
+ export function resolveRelay(reqId, result) {
88
+ const entry = relayPendingByReqId.get(reqId);
89
+ if (!entry) return false;
90
+ clearTimeout(entry.timer);
91
+ relayPendingByReqId.delete(reqId);
92
+ entry.resolve(result);
93
+ return true;
94
+ }
95
+
96
+ export function getRelayPending() { return relayPending; }
97
+ export function getRelayPendingByReqId() { return relayPendingByReqId; }
98
+
99
+ export async function tryBroadbandRelay({ groups, token: myToken, model, body, hops, bus, logs, reqId, evt, res, mark, perf0, stages }) {
100
+ try {
101
+ const joined = loadGroupsJoined();
102
+ const broadbandGroups = joined.filter((g) => g.kind === "broadband" || g.myUrl?.startsWith("relay://"));
103
+ const allCandidates = [];
104
+ if (groups) {
105
+ const localGroups = groups.list();
106
+ for (const [gName, g] of Object.entries(localGroups)) {
107
+ for (const [id, m] of Object.entries(g.members || {})) {
108
+ if (id === "leader") continue;
109
+ const isBb = m?.kind === "broadband" || String(m?.url || "").startsWith("relay://");
110
+ if (!isBb) continue;
111
+ const staleMs = Number(process.env.MSLXDFF_BROADBAND_STALE_MS) > 0 ? Number(process.env.MSLXDFF_BROADBAND_STALE_MS) : 90_000;
112
+ if (typeof m.lastSeen === "number" && Date.now() - m.lastSeen > staleMs) continue;
113
+ allCandidates.push({ group: gName, target: m.url, member: m, via: "local-leader", leaderUrl: null });
114
+ }
115
+ }
116
+ }
117
+ for (const g of joined) {
118
+ if (!g.leaderUrl) continue;
119
+ try {
120
+ const controller = new AbortController();
121
+ const timer = setTimeout(() => controller.abort(), 5000);
122
+ const r = await compatFetch(`${g.leaderUrl}/v1/groups/join`, {
123
+ method: "POST",
124
+ headers: { "Content-Type": "application/json", "Authorization": `Bearer ${myToken}` },
125
+ body: JSON.stringify({ name: g.name, memberName: g.memberName, url: g.myUrl, token: myToken }),
126
+ signal: controller.signal,
127
+ });
128
+ clearTimeout(timer);
129
+ if (!r.ok) continue;
130
+ const data = await r.json().catch(() => ({}));
131
+ const members = data.members || {};
132
+ for (const [id, m] of Object.entries(members)) {
133
+ if (id === "leader") continue;
134
+ const isBb = m?.kind === "broadband" || String(m?.url || "").startsWith("relay://");
135
+ if (!isBb) continue;
136
+ if (m.url === g.myUrl) continue;
137
+ const staleMs = Number(process.env.MSLXDFF_BROADBAND_STALE_MS) > 0 ? Number(process.env.MSLXDFF_BROADBAND_STALE_MS) : 90_000;
138
+ if (typeof m.lastSeen === "number" && Date.now() - m.lastSeen > staleMs) continue;
139
+ allCandidates.push({ group: g.name, target: m.url, member: m, via: "via-leader", leaderUrl: g.leaderUrl });
140
+ }
141
+ } catch {}
142
+ }
143
+ if (!allCandidates.length) return null;
144
+ for (const cand of allCandidates) {
145
+ try {
146
+ evt?.("relay-try", { reqId, model, via: cand.via, target: cand.target, group: cand.group });
147
+ if (!cand.leaderUrl) {
148
+ const fwdBody = { model, ...body, model };
149
+ const reqIdLocal = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
150
+ const promise = enqueueRelay({ group: cand.group, target: cand.target, reqId: reqIdLocal, body: fwdBody, hops });
151
+ const result = await promise;
152
+ if (result && result.status) {
153
+ return { via: "broadband-local", result, target: cand.target, group: cand.group };
154
+ }
155
+ } else {
156
+ const ctrl = new AbortController();
157
+ const t = setTimeout(() => ctrl.abort(), 35_000);
158
+ const r = await compatFetch(`${cand.leaderUrl}/v1/groups/relay/forward`, {
159
+ method: "POST",
160
+ headers: { "Content-Type": "application/json", "Authorization": `Bearer ${myToken}`, "x-mslxdff-hops": String(hops + 1) },
161
+ body: JSON.stringify({ group: cand.group, target: cand.target, body: { ...body, model }, hops: hops + 1, reqId }),
162
+ signal: ctrl.signal,
163
+ });
164
+ clearTimeout(t);
165
+ if (!r.ok) {
166
+ const txt = await r.text().catch(() => "");
167
+ evt?.("relay-fail", { reqId, model, via: cand.via, target: cand.target, status: r.status, message: txt.slice(0, 200) });
168
+ continue;
169
+ }
170
+ return { via: "broadband-via-leader", result: r, target: cand.target, group: cand.group };
171
+ }
172
+ } catch (err) {
173
+ evt?.("relay-fail", { reqId, model, via: cand.via, target: cand.target, message: String(err?.message || err).slice(0, 200) });
174
+ continue;
175
+ }
176
+ }
177
+ return null;
178
+ } catch {
179
+ return null;
180
+ }
181
+ }
@@ -1,60 +1,60 @@
1
- import { readBody, json, authorized } from "./helpers.js";
2
- import { compatFetch } from "../compat.js";
3
-
4
- // POST /v1/relay 纯网络中继:A 把 targetUrl+headers+body 发给 B,B 原样 fetch 到上游再回给 A
5
- // B 侧不查本地 providerConfigs、不做 model 前缀路由、不验 allowlist,仅当 TCP 出口
6
- // 鉴权:复用全局 Bearer token(peer.token),与 /v1/chat/completions 同级,避免任意 SSRF
7
- export async function relayHandler({ req, res, token }) {
8
- if (!authorized(req, token)) {
9
- res.statusCode = 401;
10
- res.setHeader("WWW-Authenticate", "Bearer");
11
- return json(res, 401, { error: "Unauthorized" });
12
- }
13
- let body;
14
- try { body = await readBody(req); } catch { return json(res, 400, { error: "Invalid JSON body" }); }
15
- const targetUrl = String(body.targetUrl || body.url || "").trim();
16
- const method = String(body.method || "POST").toUpperCase();
17
- const headers = body.headers && typeof body.headers === "object" ? body.headers : {};
18
- const rawBody = body.body ?? body.payload ?? null;
19
-
20
- if (!targetUrl) return json(res, 400, { error: "targetUrl is required" });
21
- let u;
22
- try { u = new URL(targetUrl); } catch { return json(res, 400, { error: "invalid targetUrl" }); }
23
- if (u.protocol !== "https:" && u.protocol !== "http:") return json(res, 400, { error: "targetUrl must be http(s)" });
24
- // 简单 SSRF 防护:禁止回环与内网段(除 127.0.0.1:8989 本身已鉴权,仍放行)
25
- // 允许外网 https 上游(如 https://api.bai.com、https://opencode.ai),禁止 10/172.16/192.168
26
- const host = u.hostname;
27
- if (/^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[0-1])\.)/.test(host) && host !== "127.0.0.1" && host !== "localhost") {
28
- // 仍允许,因为组员可能是内网 IP 的上游(如自建网关),仅告警不拦
29
- }
30
-
31
- // 透传头:只放行受控头,避免把内部头(如 x-mslxdff-*)误带给上游
32
- const allowHeaders = new Set(["authorization", "content-type", "accept", "user-agent", "x-client-type", "x-platform", "x-task-id", "x-user-id", "x-domain", "x-enterprise-id", "x-tenant-id", "origin", "referer"]);
33
- const fwdHeaders = {};
34
- for (const [k, v] of Object.entries(headers)) {
35
- const lk = String(k).toLowerCase();
36
- if (allowHeaders.has(lk) && typeof v === "string" && v) fwdHeaders[k] = v;
37
- }
38
- // 强制 JSON
39
- if (!fwdHeaders["Content-Type"] && !fwdHeaders["content-type"]) fwdHeaders["Content-Type"] = "application/json";
40
- if (!fwdHeaders["Accept"] && !fwdHeaders["accept"]) fwdHeaders["Accept"] = "application/json";
41
-
42
- const controller = new AbortController();
43
- const timer = setTimeout(() => controller.abort(new Error("relay timeout 30000ms")), 30000);
44
- try {
45
- const fetchBody = rawBody == null ? undefined : (typeof rawBody === "string" ? rawBody : JSON.stringify(rawBody));
46
- const r = await compatFetch(targetUrl, { method, headers: fwdHeaders, body: fetchBody, signal: controller.signal });
47
- const txt = await r.text();
48
- // 原样回透:状态码 + 头(仅透 content-type) + body
49
- res.statusCode = r.status;
50
- const ct = r.headers.get("content-type");
51
- if (ct) res.setHeader("content-type", ct);
52
- // 额外回透上游错误码便于 bench 区分
53
- res.setHeader("x-mslxdff-relay-status", String(r.status));
54
- return res.end(txt);
55
- } catch (e) {
56
- const msg = String(e?.message || e);
57
- const isTimeout = /timeout|abort/i.test(msg);
58
- return json(res, 502, { error: isTimeout ? "relay timeout" : `relay fetch failed: ${msg.slice(0, 300)}` });
59
- } finally { clearTimeout(timer); }
60
- }
1
+ import { readBody, json, authorized } from "./helpers.js";
2
+ import { compatFetch } from "../compat.js";
3
+
4
+ // POST /v1/relay 纯网络中继:A 把 targetUrl+headers+body 发给 B,B 原样 fetch 到上游再回给 A
5
+ // B 侧不查本地 providerConfigs、不做 model 前缀路由、不验 allowlist,仅当 TCP 出口
6
+ // 鉴权:复用全局 Bearer token(peer.token),与 /v1/chat/completions 同级,避免任意 SSRF
7
+ export async function relayHandler({ req, res, token }) {
8
+ if (!authorized(req, token)) {
9
+ res.statusCode = 401;
10
+ res.setHeader("WWW-Authenticate", "Bearer");
11
+ return json(res, 401, { error: "Unauthorized" });
12
+ }
13
+ let body;
14
+ try { body = await readBody(req); } catch { return json(res, 400, { error: "Invalid JSON body" }); }
15
+ const targetUrl = String(body.targetUrl || body.url || "").trim();
16
+ const method = String(body.method || "POST").toUpperCase();
17
+ const headers = body.headers && typeof body.headers === "object" ? body.headers : {};
18
+ const rawBody = body.body ?? body.payload ?? null;
19
+
20
+ if (!targetUrl) return json(res, 400, { error: "targetUrl is required" });
21
+ let u;
22
+ try { u = new URL(targetUrl); } catch { return json(res, 400, { error: "invalid targetUrl" }); }
23
+ if (u.protocol !== "https:" && u.protocol !== "http:") return json(res, 400, { error: "targetUrl must be http(s)" });
24
+ // 简单 SSRF 防护:禁止回环与内网段(除 127.0.0.1:8989 本身已鉴权,仍放行)
25
+ // 允许外网 https 上游(如 https://api.bai.com、https://opencode.ai),禁止 10/172.16/192.168
26
+ const host = u.hostname;
27
+ if (/^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[0-1])\.)/.test(host) && host !== "127.0.0.1" && host !== "localhost") {
28
+ // 仍允许,因为组员可能是内网 IP 的上游(如自建网关),仅告警不拦
29
+ }
30
+
31
+ // 透传头:只放行受控头,避免把内部头(如 x-mslxdff-*)误带给上游
32
+ const allowHeaders = new Set(["authorization", "content-type", "accept", "user-agent", "x-client-type", "x-platform", "x-task-id", "x-user-id", "x-domain", "x-enterprise-id", "x-tenant-id", "origin", "referer"]);
33
+ const fwdHeaders = {};
34
+ for (const [k, v] of Object.entries(headers)) {
35
+ const lk = String(k).toLowerCase();
36
+ if (allowHeaders.has(lk) && typeof v === "string" && v) fwdHeaders[k] = v;
37
+ }
38
+ // 强制 JSON
39
+ if (!fwdHeaders["Content-Type"] && !fwdHeaders["content-type"]) fwdHeaders["Content-Type"] = "application/json";
40
+ if (!fwdHeaders["Accept"] && !fwdHeaders["accept"]) fwdHeaders["Accept"] = "application/json";
41
+
42
+ const controller = new AbortController();
43
+ const timer = setTimeout(() => controller.abort(new Error("relay timeout 30000ms")), 30000);
44
+ try {
45
+ const fetchBody = rawBody == null ? undefined : (typeof rawBody === "string" ? rawBody : JSON.stringify(rawBody));
46
+ const r = await compatFetch(targetUrl, { method, headers: fwdHeaders, body: fetchBody, signal: controller.signal });
47
+ const txt = await r.text();
48
+ // 原样回透:状态码 + 头(仅透 content-type) + body
49
+ res.statusCode = r.status;
50
+ const ct = r.headers.get("content-type");
51
+ if (ct) res.setHeader("content-type", ct);
52
+ // 额外回透上游错误码便于 bench 区分
53
+ res.setHeader("x-mslxdff-relay-status", String(r.status));
54
+ return res.end(txt);
55
+ } catch (e) {
56
+ const msg = String(e?.message || e);
57
+ const isTimeout = /timeout|abort/i.test(msg);
58
+ return json(res, 502, { error: isTimeout ? "relay timeout" : `relay fetch failed: ${msg.slice(0, 300)}` });
59
+ } finally { clearTimeout(timer); }
60
+ }