mslxdff 0.1.231 → 0.1.233

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 (117) hide show
  1. package/bin/mslxdff.js +3 -3
  2. package/package.json +1 -1
  3. package/src/auto.js +264 -254
  4. package/src/bench/cline-bench.js +42 -42
  5. package/src/bench/probe.js +70 -70
  6. package/src/bench/report.js +162 -162
  7. package/src/bench/runner.js +77 -77
  8. package/src/bench/via-probe.js +124 -124
  9. package/src/bench/via-routes.js +87 -87
  10. package/src/bench/workbuddy-bench.js +54 -54
  11. package/src/chat/engine.js +160 -160
  12. package/src/chat/gateway.js +163 -163
  13. package/src/chat/orchestrator.js +234 -234
  14. package/src/chat/prompt.js +70 -70
  15. package/src/chat/repl.js +88 -88
  16. package/src/chat/terminal.js +135 -135
  17. package/src/chat/tools.js +306 -306
  18. package/src/chat-pipeline/index.js +155 -145
  19. package/src/chat-pipeline/policy.js +84 -84
  20. package/src/cli/commands/group.js +266 -266
  21. package/src/cli/commands/model/picks.js +50 -50
  22. package/src/cli/commands/provider/bench-via.js +247 -247
  23. package/src/cli/commands/provider/bench.js +141 -141
  24. package/src/cli/commands/provider/index.js +139 -139
  25. package/src/cli/commands/provider/models.js +149 -149
  26. package/src/cli/commands/provider/qwenwork-login.js +119 -119
  27. package/src/cli/commands/provider/raccoon-quota.js +156 -156
  28. package/src/cli/commands/sync.js +310 -310
  29. package/src/cli/status.js +279 -279
  30. package/src/daemon.js +246 -246
  31. package/src/model-capabilities/enrich.js +86 -86
  32. package/src/model-capabilities/index.js +183 -183
  33. package/src/model-capabilities/parse.js +70 -70
  34. package/src/model-trace.js +10 -2
  35. package/src/models.js +292 -225
  36. package/src/peers.js +345 -345
  37. package/src/providers/agentrouter.js +238 -238
  38. package/src/providers/base.js +164 -164
  39. package/src/providers/cline/auth.js +228 -228
  40. package/src/providers/cline/chat.js +307 -307
  41. package/src/providers/cline.js +2 -2
  42. package/src/providers/codearts/chat.js +207 -207
  43. package/src/providers/dispatcher.js +147 -147
  44. package/src/providers/globalqwenwork/cosy.js +123 -123
  45. package/src/providers/globalqwenwork/crypto.js +218 -218
  46. package/src/providers/globalqwenwork/http.js +20 -20
  47. package/src/providers/globalqwenwork/rsa.js +54 -54
  48. package/src/providers/globalqwenwork/sse.js +268 -268
  49. package/src/providers/globalqwenwork/stream.js +130 -130
  50. package/src/providers/globalqwenwork/upstream.js +122 -122
  51. package/src/providers/keyring.js +60 -60
  52. package/src/providers/qoder/account-store.js +18 -0
  53. package/src/providers/qoder/chat.js +292 -203
  54. package/src/providers/qoder/checkin.js +25 -12
  55. package/src/providers/qoder/index.js +311 -249
  56. package/src/providers/qoder/models.js +64 -2
  57. package/src/providers/qoder/oauth.js +42 -1
  58. package/src/providers/qoder/payload.js +116 -80
  59. package/src/providers/qoder/request.js +7 -2
  60. package/src/providers/qoder/session.js +26 -10
  61. package/src/providers/qoder/sse.js +233 -145
  62. package/src/providers/qoder/sticky.js +5 -2
  63. package/src/providers/qoder/wire-messages.js +242 -0
  64. package/src/providers/qwenwork/account-store.js +133 -133
  65. package/src/providers/qwenwork/constants.js +67 -67
  66. package/src/providers/qwenwork/cosy.js +120 -120
  67. package/src/providers/qwenwork/crypto.js +218 -218
  68. package/src/providers/qwenwork/http.js +20 -20
  69. package/src/providers/qwenwork/index.js +337 -337
  70. package/src/providers/qwenwork/payload.js +144 -144
  71. package/src/providers/qwenwork/rsa.js +54 -54
  72. package/src/providers/qwenwork/sse.js +268 -268
  73. package/src/providers/qwenwork/stream.js +130 -130
  74. package/src/providers/qwenwork/upstream.js +120 -120
  75. package/src/providers/qwenwork.js +1 -1
  76. package/src/providers/raccoon/chat.js +1 -1
  77. package/src/providers/raccoon/const.js +219 -202
  78. package/src/providers/raccoon/credits.js +285 -285
  79. package/src/providers/raccoon/index.js +15 -2
  80. package/src/providers/raccoon/models.js +68 -17
  81. package/src/providers/registry.js +89 -89
  82. package/src/providers/workbuddy/auth.js +16 -0
  83. package/src/providers/workbuddy/chat.js +298 -264
  84. package/src/providers/workbuddy/client-headers.js +148 -0
  85. package/src/providers/workbuddy/desktop-info.js +17 -1
  86. package/src/providers/workbuddy/models.js +8 -20
  87. package/src/providers/workbuddy/reshape.js +152 -152
  88. package/src/providers/workbuddy.js +2 -2
  89. package/src/reasoning.js +32 -32
  90. package/src/routes/chat/broadband-handler.js +80 -80
  91. package/src/routes/chat/gateway.js +46 -46
  92. package/src/routes/chat/relay-pipeline.js +282 -282
  93. package/src/routes/chat/via-route-handler.js +156 -156
  94. package/src/routes/hedge.js +260 -260
  95. package/src/routes/models-route.js +167 -167
  96. package/src/routes/peers.js +373 -373
  97. package/src/routes/stream.js +364 -347
  98. package/src/runtime/bootstrap.js +75 -75
  99. package/src/runtime/keep-warm.js +135 -135
  100. package/src/runtime/provider-gate.js +75 -75
  101. package/src/runtime/providers-setup.js +187 -187
  102. package/src/runtime/raccoon-checkin.js +175 -175
  103. package/src/server.js +85 -85
  104. package/src/state/schemas/allowlist.js +92 -92
  105. package/src/sync-opencode.js +280 -280
  106. package/src/transport/index.js +244 -244
  107. package/src/transport/pool.js +56 -56
  108. package/src/transport/retry.js +24 -24
  109. package/src/transport/sse.js +93 -93
  110. package/src/upstream-probe/display.js +52 -52
  111. package/src/upstream-probe/probe.js +49 -49
  112. package/src/upstream-probe/rotate.js +110 -110
  113. package/src/upstream-probe/start.js +45 -45
  114. package/src/upstream.js +355 -355
  115. package/docs/cli_help_mini.md +0 -148
  116. package/src/providers/AGENTS.md +0 -49
  117. package/src/routes/AGENTS.md +0 -37
@@ -1,146 +1,156 @@
1
- import { performance } from "node:perf_hooks";
2
- import { analyzePolicy } from "./policy.js";
3
- import { createEngine } from "./engine.js";
4
- import { runHook } from "../plugins.js";
5
- import { isFreeModel } from "../models.js";
6
- import { clientIp, summarizePrompt } from "../routes/helpers.js";
7
- import { formatTimeline } from "../timeline.js";
8
- import { summarizeRequest, shouldTraceModel } from "../model-trace.js";
9
- import { apiKindOf } from "../api-kind.js";
10
-
11
- /**
12
- * ChatPipeline 深模块门面 — 对外 execute(req) 单一 inlet
13
- * 内部组合 Policy→Engine:解析 header/model → 产 order → 委托 engine 执行
14
- * gateway 仅薄适配:readBody + request:received hook + 调 execute
15
- */
16
-
17
- export function createChatPipeline({ upstream, auto, logs, peers, groups, bus, token, plugins, maxHops } = {}) {
18
- const engine = createEngine();
19
-
20
- async function execute({ req, res } = {}) {
21
- const startedAt = Date.now();
22
- const reqId = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
23
- // 入站协议门(chat|responses|messages|other):全场只在这里判一次,之后随 handlerCtx 透传;
24
- // 各日志写点不得再各自匹配路径(第二份判定迟早漂移)—— 见 src/api-kind.js
25
- const api = apiKindOf(req);
26
- const perf0 = performance.now();
27
- const stages = [];
28
- const mark = (name) => stages.push([name, Math.round(performance.now() - perf0)]);
29
-
30
- const policy = analyzePolicy({ headers: req?.headers || {}, body: req?.body || {} });
31
- const { requested, useAuto, autoProvider, lockModel, hops, shareKeys, workbuddyUid, aliasInfo } = policy;
32
- mark("parsed");
33
- if (aliasInfo) { try { res?.setHeader?.("x-mslxdff-alias", aliasInfo); } catch {} }
34
- // mslxdff/ 前缀或 alias 命中时,把 body.model 改写为还原后的模型(与原 gateway 语义一致)
35
- const timeline = { direct: [], peers: [], retries: 0, retryResult: null, result: null };
36
- if (aliasInfo && req?.body && req.body.model !== requested) {
37
- req.body = { ...req.body, model: requested };
38
- }
39
-
40
- // 事件/日志帮手先于 order 推导声明:evt 在 auto-scope 分支即被使用,
41
- // 声明置后会让 useAuto && autoProvider 的请求在 TDZ 上崩(线上 -chat 回退路径)
42
- const logCall = (model, status) => logs?.appendCall({ reqId, api, model, auto: useAuto, status, durationMs: Date.now() - startedAt, stream: Boolean(req?.body?.stream), stages });
43
- const logError = (model, status, message) => logs?.appendError({ reqId, api, model, auto: useAuto, status, message, stages });
44
- const evt = (type, data) => {
45
- const entry = { ts: Date.now(), reqId, type, ...data, api, model: data.model ?? requested, auto: useAuto, durationMs: Date.now() - startedAt, stages: [...stages] };
46
- const traceModel = entry.model || requested;
47
- if (shouldTraceModel(type)) {
48
- // Note: 模型日志只投影安全字段,不能把含 prompt 的 entry 原样下传 — 见 .agents/notes/implemented/feature/2026-09-25-model-trace-log.md
49
- const safeTraceData = { ...entry };
50
- if (safeTraceData.prompt !== undefined) delete safeTraceData.prompt;
51
- logs?.appendModelTrace?.(traceModel, { type, reqId, model: traceModel, data: safeTraceData, request: type === "request" ? summarizeRequest(req?.body) : null, totalMs: entry.durationMs });
52
- }
53
- if (type === "peer-forward") timeline.peers.push({ peer: entry.peer, ok: entry.ok === true, status: entry.status, latencyMs: entry.latencyMs, message: entry.message || entry.error || "" });
54
- if (type === "upstream-done" || type === "upstream-error") timeline.direct.push({ status: entry.status, reason: entry.message || entry.error || "" });
55
- if (type === "peer-error") {
56
- const known = timeline.peers.find((p) => p.peer === entry.peer);
57
- if (known) { known.ok = false; known.status = entry.status ?? known.status; known.message = entry.message || entry.error || known.message; }
58
- else timeline.peers.push({ peer: entry.peer, ok: false, status: entry.status, message: entry.message || entry.error || "" });
59
- }
60
- if (type === "empty-turn-retry") timeline.retries += 1;
61
- // 空转重试必须交代结局:只记次数的话,"重试后救回来"和"重试到死"在日志里长得一模一样
62
- if (type === "empty-turn-recovered") timeline.retryResult = "ok";
63
- if (type === "empty-turn-exhausted") timeline.retryResult = timeline.retryResult || "lost";
64
- if (type === "result" || (type === "client-response" && !timeline.result)) {
65
- timeline.result = { status: entry.status, detail: entry.detail || null };
66
- try { logs?.appendTimeline?.(formatTimeline({ reqId, model: entry.model || requested, ...timeline, totalMs: Date.now() - startedAt })); } catch {}
67
- }
68
- if (bus) bus.emit(entry);
69
- logs?.appendEvent?.(entry);
70
- };
71
- const done = (info) => {
72
- if (!plugins?.length) return;
73
- runHook(plugins, "request:completed", { reqId, requested, useAuto, hops, stream: Boolean(req?.body?.stream), durationMs: Date.now() - startedAt, ...info }).catch(() => {});
74
- };
75
-
76
- // order 推导 + plugin model:select 可改
77
- // 语义:指定模型 = 死锁单模型(本机→组员同款,挂了就报挂,不兜其他 picks);只有 auto 才轮 picks
78
- // 指定模型的额度耗尽时**不换模型**:横向只在同模型的组员/组长间接力(本机出口 IP 耗尽 → 换组员机器),
79
- // 模型永不漂移。详见 serial-trial 的 order 单元素路径与 peer 接力。
80
- // x-mslxdff-auto-provider 头可把 auto 候选限定到单供应商(opencode=裸 id 免费池),-chat 默认带
81
- let order;
82
- if (lockModel) order = [requested];
83
- else if (useAuto) {
84
- let cands = auto ? await auto.candidates() : [""];
85
- if (autoProvider) {
86
- const before = cands.length;
87
- cands = cands.filter((m) => (autoProvider === "opencode"
88
- ? !String(m).includes("/") && isFreeModel(m)
89
- : String(m).startsWith(`${autoProvider}/`)));
90
- evt("auto-scope", { reqId, provider: autoProvider, before, after: cands.length });
91
- }
92
- order = cands;
93
- } else {
94
- // 指定模型 = 死锁单模型:额度耗尽只换组员/组长(同模型换机器),**绝不换模型**。
95
- // candidatesFor 的持久化副作用(把显式指定的模型加入勾选集,供日后 auto 用)必须保留——
96
- // 见 auto.js:180「显式指定某模型 = 认可它,自动加入勾选集」,历史上删它会丢勾选记录。
97
- if (auto && requested) {
98
- try { await auto.candidatesFor(requested); } catch {}
99
- }
100
- order = [requested];
101
- }
102
- if (!order.length) order = [""];
103
- const canFallback = order.length > 1;
104
- // hi 探针(x-mslxdff-probe:1)只问「这台机器此刻能不能打通上游」,不代表真实业务:
105
- // 它绝不能再触发本机的组员/宽带接力——否则探针会跟着走完整条接力链(最坏 45s 预算),
106
- // 而调用方只看它几百毫秒内活不活。组员侧就地直连,成败如实回给调用方。
107
- const isProbe = String(req?.headers?.["x-mslxdff-probe"] || "") === "1";
108
- const canForwardPeers = !isProbe && Boolean(peers) && hops < (maxHops ?? 3);
109
- mark("ordered");
110
-
111
- const reqIp = clientIp(req); // 一次解析两处用:request 事件的 ip 字段 + 回路语料的 clientIp(routes/helpers.js 取 x-forwarded-for 首段)
112
- evt("request", { reqId, hops, ip: reqIp, stream: Boolean(req?.body?.stream), prompt: summarizePrompt(req?.body), rawModel: policy.rawModel, requested, lockModel: lockModel || null });
113
- if (aliasInfo) evt("alias", { reqId, alias: aliasInfo, rawModel: policy.rawModel, requested });
114
- if (Object.keys(shareKeys).length) evt("share-keys", { reqId, providers: Object.keys(shareKeys) });
115
- evt("ordered", { reqId, order, canFallback, canForwardPeers, useAuto, statuses: auto?.statuses?.() ?? null });
116
-
117
- if (plugins?.length && !lockModel) {
118
- const sel = await runHook(plugins, "model:select", { reqId, requested, useAuto, order: [...order], hops, stream: Boolean(req?.body?.stream) });
119
- if (sel.changed && Array.isArray(sel.value) && sel.value.length) {
120
- order = sel.value.filter(Boolean);
121
- if (!order.length) order = [requested];
122
- evt("plugin-hook", { reqId, hook: "model:select", applied: true, order: [...order] });
123
- }
124
- for (const e of sel.errors) evt("plugin-hook-error", { reqId, hook: "model:select", plugin: e.plugin, error: e.error });
125
- }
126
-
127
- // 客户端会话标识(opencode 插件 chat.headers 注入)→ 透传上游做粘性路由/缓存亲和;
128
- // 无头时由 upstream.js 按对话首两条消息哈希兜底(不再每请求随机)
129
- const clientSession = String(req?.headers?.["x-session-affinity"] || req?.headers?.["x-session-id"] || "").trim() || null;
130
- // triedUrls 挂请求级并**按引用**共享:同一请求内"同一组员不重复试"的契约跨候选生效
131
- // (peer 阶段各分支会 spread 出上下文副本,集合在这里建一次才不会各存一份)。
132
- const handlerCtx = { reqId, api, model: null, body: req?.body, hops, peers, plugins, evt, logError, logCall, logs, workbuddyUid, sessionId: clientSession, clientIp: reqIp, triedUrls: new Set(), isProbe };
133
- if (clientSession) evt("client-session", { reqId, sessionId: clientSession.slice(0, 24) });
134
-
135
- await engine.run({
136
- reqId, startedAt, req, res, body: req?.body, policy,
137
- useAuto, lockModel, requested, hops,
138
- canFallback, canForwardPeers,
139
- perf0, stages, mark, evt, logCall, logError, done, handlerCtx,
140
- auto, upstream, peers, groups, bus, token, plugins, logs,
141
- order,
142
- });
143
- }
144
-
145
- return { execute, _policy: analyzePolicy, _engine: engine };
1
+ import { performance } from "node:perf_hooks";
2
+ import { analyzePolicy } from "./policy.js";
3
+ import { createEngine } from "./engine.js";
4
+ import { runHook } from "../plugins.js";
5
+ import { isFreeModel } from "../models.js";
6
+ import { clientIp, summarizePrompt } from "../routes/helpers.js";
7
+ import { formatTimeline } from "../timeline.js";
8
+ import { summarizeRequest, shouldTraceModel } from "../model-trace.js";
9
+ import { apiKindOf } from "../api-kind.js";
10
+
11
+ /**
12
+ * ChatPipeline 深模块门面 — 对外 execute(req) 单一 inlet
13
+ * 内部组合 Policy→Engine:解析 header/model → 产 order → 委托 engine 执行
14
+ * gateway 仅薄适配:readBody + request:received hook + 调 execute
15
+ */
16
+
17
+ export function createChatPipeline({ upstream, auto, logs, peers, groups, bus, token, plugins, maxHops } = {}) {
18
+ const engine = createEngine();
19
+
20
+ async function execute({ req, res } = {}) {
21
+ const startedAt = Date.now();
22
+ const reqId = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`;
23
+ // 入站协议门(chat|responses|messages|other):全场只在这里判一次,之后随 handlerCtx 透传;
24
+ // 各日志写点不得再各自匹配路径(第二份判定迟早漂移)—— 见 src/api-kind.js
25
+ const api = apiKindOf(req);
26
+ const perf0 = performance.now();
27
+ const stages = [];
28
+ const mark = (name) => stages.push([name, Math.round(performance.now() - perf0)]);
29
+
30
+ const policy = analyzePolicy({ headers: req?.headers || {}, body: req?.body || {} });
31
+ const { requested, useAuto, autoProvider, lockModel, hops, shareKeys, workbuddyUid, aliasInfo } = policy;
32
+ mark("parsed");
33
+ if (aliasInfo) { try { res?.setHeader?.("x-mslxdff-alias", aliasInfo); } catch {} }
34
+ // mslxdff/ 前缀或 alias 命中时,把 body.model 改写为还原后的模型(与原 gateway 语义一致)
35
+ const timeline = { direct: [], peers: [], retries: 0, retryResult: null, result: null };
36
+ if (aliasInfo && req?.body && req.body.model !== requested) {
37
+ req.body = { ...req.body, model: requested };
38
+ }
39
+
40
+ // 事件/日志帮手先于 order 推导声明:evt 在 auto-scope 分支即被使用,
41
+ // 声明置后会让 useAuto && autoProvider 的请求在 TDZ 上崩(线上 -chat 回退路径)
42
+ const logCall = (model, status) => logs?.appendCall({ reqId, api, model, auto: useAuto, status, durationMs: Date.now() - startedAt, stream: Boolean(req?.body?.stream), stages });
43
+ const logError = (model, status, message) => logs?.appendError({ reqId, api, model, auto: useAuto, status, message, stages });
44
+ const evt = (type, data) => {
45
+ const entry = { ts: Date.now(), reqId, type, ...data, api, model: data.model ?? requested, auto: useAuto, durationMs: Date.now() - startedAt, stages: [...stages] };
46
+ const traceModel = entry.model || requested;
47
+ if (shouldTraceModel(type)) {
48
+ // Note: 模型日志只投影安全字段,不能把含 prompt 的 entry 原样下传 — 见 .agents/notes/implemented/feature/2026-09-25-model-trace-log.md
49
+ const safeTraceData = { ...entry };
50
+ if (safeTraceData.prompt !== undefined) delete safeTraceData.prompt;
51
+ logs?.appendModelTrace?.(traceModel, { type, reqId, model: traceModel, data: safeTraceData, request: type === "request" ? summarizeRequest(req?.body) : null, totalMs: entry.durationMs });
52
+ }
53
+ if (type === "peer-forward") timeline.peers.push({ peer: entry.peer, ok: entry.ok === true, status: entry.status, latencyMs: entry.latencyMs, message: entry.message || entry.error || "" });
54
+ if (type === "upstream-done" || type === "upstream-error") timeline.direct.push({ status: entry.status, reason: entry.message || entry.error || "" });
55
+ if (type === "peer-error") {
56
+ const known = timeline.peers.find((p) => p.peer === entry.peer);
57
+ if (known) { known.ok = false; known.status = entry.status ?? known.status; known.message = entry.message || entry.error || known.message; }
58
+ else timeline.peers.push({ peer: entry.peer, ok: false, status: entry.status, message: entry.message || entry.error || "" });
59
+ }
60
+ if (type === "empty-turn-retry") timeline.retries += 1;
61
+ // 空转重试必须交代结局:只记次数的话,"重试后救回来"和"重试到死"在日志里长得一模一样
62
+ if (type === "empty-turn-recovered") timeline.retryResult = "ok";
63
+ if (type === "empty-turn-exhausted") timeline.retryResult = timeline.retryResult || "lost";
64
+ if (type === "result" || (type === "client-response" && !timeline.result)) {
65
+ timeline.result = { status: entry.status, detail: entry.detail || null };
66
+ try { logs?.appendTimeline?.(formatTimeline({ reqId, model: entry.model || requested, ...timeline, totalMs: Date.now() - startedAt })); } catch {}
67
+ }
68
+ if (bus) bus.emit(entry);
69
+ logs?.appendEvent?.(entry);
70
+ };
71
+ const done = (info) => {
72
+ if (!plugins?.length) return;
73
+ runHook(plugins, "request:completed", { reqId, requested, useAuto, hops, stream: Boolean(req?.body?.stream), durationMs: Date.now() - startedAt, ...info }).catch(() => {});
74
+ };
75
+
76
+ // order 推导 + plugin model:select 可改
77
+ // 语义:指定模型 = 死锁单模型(本机→组员同款,挂了就报挂,不兜其他 picks);只有 auto 才轮 picks
78
+ // 指定模型的额度耗尽时**不换模型**:横向只在同模型的组员/组长间接力(本机出口 IP 耗尽 → 换组员机器),
79
+ // 模型永不漂移。详见 serial-trial 的 order 单元素路径与 peer 接力。
80
+ // x-mslxdff-auto-provider 头可把 auto 候选限定到单供应商(opencode=裸 id 免费池),-chat 默认带
81
+ let order;
82
+ if (lockModel) order = [requested];
83
+ else if (useAuto) {
84
+ let cands = auto ? await auto.candidates() : [""];
85
+ if (autoProvider) {
86
+ const before = cands.length;
87
+ cands = cands.filter((m) => (autoProvider === "opencode"
88
+ ? !String(m).includes("/") && isFreeModel(m)
89
+ : String(m).startsWith(`${autoProvider}/`)));
90
+ evt("auto-scope", { reqId, provider: autoProvider, before, after: cands.length });
91
+ }
92
+ order = cands;
93
+ } else {
94
+ // 指定模型 = 死锁单模型:额度耗尽只换组员/组长(同模型换机器),**绝不换模型**。
95
+ // candidatesFor 的持久化副作用(把显式指定的模型加入勾选集,供日后 auto 用)必须保留——
96
+ // 见 auto.js:180「显式指定某模型 = 认可它,自动加入勾选集」,历史上删它会丢勾选记录。
97
+ // 但**不 await**(2026-10-22 日志定罪:loadList 拉全量模型目录烧 24s,写盘 persistPicks 仅 0ms)。
98
+ // 此路径 order 已定死 [requested],candidatesFor 返回值根本不被消费,只为触发"加入勾选集"副作用——
99
+ // 为副作用让主请求干等 24s 是自伤。改后台 fire-and-forget:勾选照样持久化,主请求零阻塞。
100
+ if (auto && requested) {
101
+ const tCf = performance.now();
102
+ Promise.resolve()
103
+ .then(() => auto.candidatesFor(requested))
104
+ .catch(() => {})
105
+ .finally(() => {
106
+ const ms = Math.round(performance.now() - tCf);
107
+ if (ms > 5000) console.log(`[pipeline-bg] candidatesFor 后台耗时 model=${requested} ms=${ms}(未阻塞主请求)`);
108
+ });
109
+ }
110
+ order = [requested];
111
+ }
112
+ if (!order.length) order = [""];
113
+ const canFallback = order.length > 1;
114
+ // hi 探针(x-mslxdff-probe:1)只问「这台机器此刻能不能打通上游」,不代表真实业务:
115
+ // 它绝不能再触发本机的组员/宽带接力——否则探针会跟着走完整条接力链(最坏 45s 预算),
116
+ // 而调用方只看它几百毫秒内活不活。组员侧就地直连,成败如实回给调用方。
117
+ const isProbe = String(req?.headers?.["x-mslxdff-probe"] || "") === "1";
118
+ const canForwardPeers = !isProbe && Boolean(peers) && hops < (maxHops ?? 3);
119
+ mark("ordered");
120
+
121
+ const reqIp = clientIp(req); // 一次解析两处用:request 事件的 ip 字段 + 回路语料的 clientIp(routes/helpers.js 取 x-forwarded-for 首段)
122
+ evt("request", { reqId, hops, ip: reqIp, stream: Boolean(req?.body?.stream), prompt: summarizePrompt(req?.body), rawModel: policy.rawModel, requested, lockModel: lockModel || null });
123
+ if (aliasInfo) evt("alias", { reqId, alias: aliasInfo, rawModel: policy.rawModel, requested });
124
+ if (Object.keys(shareKeys).length) evt("share-keys", { reqId, providers: Object.keys(shareKeys) });
125
+ evt("ordered", { reqId, order, canFallback, canForwardPeers, useAuto, statuses: auto?.statuses?.() ?? null });
126
+
127
+ if (plugins?.length && !lockModel) {
128
+ const sel = await runHook(plugins, "model:select", { reqId, requested, useAuto, order: [...order], hops, stream: Boolean(req?.body?.stream) });
129
+ if (sel.changed && Array.isArray(sel.value) && sel.value.length) {
130
+ order = sel.value.filter(Boolean);
131
+ if (!order.length) order = [requested];
132
+ evt("plugin-hook", { reqId, hook: "model:select", applied: true, order: [...order] });
133
+ }
134
+ for (const e of sel.errors) evt("plugin-hook-error", { reqId, hook: "model:select", plugin: e.plugin, error: e.error });
135
+ }
136
+
137
+ // 客户端会话标识(opencode 插件 chat.headers 注入)→ 透传上游做粘性路由/缓存亲和;
138
+ // 无头时由 upstream.js 按对话首两条消息哈希兜底(不再每请求随机)
139
+ const clientSession = String(req?.headers?.["x-session-affinity"] || req?.headers?.["x-session-id"] || "").trim() || null;
140
+ // triedUrls 挂请求级并**按引用**共享:同一请求内"同一组员不重复试"的契约跨候选生效
141
+ // (peer 阶段各分支会 spread 出上下文副本,集合在这里建一次才不会各存一份)。
142
+ const handlerCtx = { reqId, api, model: null, body: req?.body, hops, peers, plugins, evt, logError, logCall, logs, workbuddyUid, sessionId: clientSession, clientIp: reqIp, triedUrls: new Set(), isProbe };
143
+ if (clientSession) evt("client-session", { reqId, sessionId: clientSession.slice(0, 24) });
144
+
145
+ await engine.run({
146
+ reqId, startedAt, req, res, body: req?.body, policy,
147
+ useAuto, lockModel, requested, hops,
148
+ canFallback, canForwardPeers,
149
+ perf0, stages, mark, evt, logCall, logError, done, handlerCtx,
150
+ auto, upstream, peers, groups, bus, token, plugins, logs,
151
+ order,
152
+ });
153
+ }
154
+
155
+ return { execute, _policy: analyzePolicy, _engine: engine };
146
156
  }
@@ -1,84 +1,84 @@
1
- import { normalizeModel } from "../reasoning.js";
2
- import { isAutoModel } from "../auto.js";
3
- import { parseHops } from "../routes/helpers.js";
4
- import { parseShareKeysHeader, SHARE_KEYS_HEADER } from "../providers/share-keys.js";
5
- import { normalizeFullId, getModelAlias } from "../providers/model-id.js";
6
-
7
- /**
8
- * PolicyStage 纯函数 — 解析 model 别名 / 白名单前置 / header 透传
9
- * 零网络副作用,输入 headers+body,输出 PolicyResult
10
- */
11
- export function analyzePolicy({ headers = {}, body = {} } = {}) {
12
- const hops = parseHops(headers["x-mslxdff-hops"] || headers["X-Mslxdff-Hops"] || "");
13
- const shareKeys = parseShareKeysHeader(headers[SHARE_KEYS_HEADER] || headers["x-mslxdff-share-keys"] || "");
14
- const workbuddyUid = (headers["x-mslxdff-workbuddy-uid"] || headers["x-workbuddy-uid"] || "").toString().trim();
15
- const lockModel = (headers["x-mslxdff-model-lock"] || headers["X-Mslxdff-Model-Lock"] || "").toString();
16
- // auto 范围限定:x-mslxdff-auto-provider: opencode → auto 候选只留该供应商(opencode=裸 id 免费池)
17
- const autoProvider = (headers["x-mslxdff-auto-provider"] || headers["X-Mslxdff-Auto-Provider"] || "").toString().trim().toLowerCase() || null;
18
- const rawModel = body.model || "";
19
-
20
- let normalizedRequested = normalizeModel(lockModel || rawModel || "");
21
- // [1m] 后缀回退(Claude Desktop 1M 条目 / Claude Code 1M 记法,ADR-0056):alias 直查未命中时
22
- // 剥去尾部 [1m] 再查一次。仅用于本次查找——不回写、不注册 alias,未命中路径逐字不变(design D2)。
23
- // 两个 alias 查找点(首轮 + mslxdff/ 前缀二段)共用同一回退,否则 `mslxdff/<槽位>[1m]` 不等价(评审 1 P1)。
24
- const resolveAlias = (id) => {
25
- const hit = getModelAlias(id);
26
- if (hit) return hit;
27
- return /\[1m\]$/i.test(id) ? getModelAlias(id.replace(/\[1m\]$/i, "")) : null;
28
- };
29
- let aliasResolved = resolveAlias(normalizedRequested);
30
- if (aliasResolved) {
31
- normalizedRequested = aliasResolved;
32
- }
33
- let requested = normalizedRequested;
34
- let aliasInfo = null;
35
-
36
- if (requested.startsWith("mslxdff/")) {
37
- const rawPart = requested.slice("mslxdff/".length);
38
- aliasInfo = `${requested} -> ${rawPart} (mslxdff provider stripped)`;
39
- requested = rawPart;
40
- const alias2 = resolveAlias(requested);
41
- if (alias2) {
42
- aliasInfo = `${rawModel} -> ${alias2} (mslxdff + alias)`;
43
- requested = alias2;
44
- }
45
- } else if (aliasResolved) {
46
- // 非 mslxdff 的 dash 形态已在首轮 aliasResolved 处理
47
- aliasInfo = aliasResolved !== (lockModel || rawModel) ? `${rawModel} -> ${aliasResolved} (alias)` : null;
48
- if (!aliasInfo) aliasInfo = null;
49
- }
50
-
51
- // workbuddy <uid>:model 形式的 uid 钉死在 normalizeFullId 侧处理,这里透传原始 requested
52
- // 若 requested 含 workbuddy/ 前缀且含 :,则尝试提取 uid
53
- let extractedUid = workbuddyUid;
54
- if (!extractedUid && requested.startsWith("workbuddy/") && requested.includes(":")) {
55
- const after = requested.slice("workbuddy/".length);
56
- const uidPart = after.split(":")[0];
57
- if (uidPart) extractedUid = uidPart;
58
- }
59
-
60
- const useAuto = isAutoModel(requested);
61
-
62
- // 对 workbuddy 前缀的 model,做 normalizeFullId 归一(剥 uid 供上游)
63
- // 但保留 requested 为完整带前缀形态,供 serial-trial 做 ViaRoute 判定与组员路由
64
- let normalizedForUpstream = requested;
65
- try {
66
- const norm = normalizeFullId(requested);
67
- if (norm && norm.raw) normalizedForUpstream = norm.raw ? `${norm.provider ? norm.provider + "/" : ""}${norm.raw}` : requested;
68
- } catch {}
69
-
70
- return {
71
- rawModel,
72
- requested,
73
- normalizedRequested,
74
- normalizedForUpstream,
75
- aliasInfo,
76
- useAuto,
77
- autoProvider,
78
- shareKeys,
79
- workbuddyUid: extractedUid,
80
- lockModel,
81
- hops,
82
- bodyModel: body.model || null,
83
- };
84
- }
1
+ import { normalizeModel } from "../reasoning.js";
2
+ import { isAutoModel } from "../auto.js";
3
+ import { parseHops } from "../routes/helpers.js";
4
+ import { parseShareKeysHeader, SHARE_KEYS_HEADER } from "../providers/share-keys.js";
5
+ import { normalizeFullId, getModelAlias } from "../providers/model-id.js";
6
+
7
+ /**
8
+ * PolicyStage 纯函数 — 解析 model 别名 / 白名单前置 / header 透传
9
+ * 零网络副作用,输入 headers+body,输出 PolicyResult
10
+ */
11
+ export function analyzePolicy({ headers = {}, body = {} } = {}) {
12
+ const hops = parseHops(headers["x-mslxdff-hops"] || headers["X-Mslxdff-Hops"] || "");
13
+ const shareKeys = parseShareKeysHeader(headers[SHARE_KEYS_HEADER] || headers["x-mslxdff-share-keys"] || "");
14
+ const workbuddyUid = (headers["x-mslxdff-workbuddy-uid"] || headers["x-workbuddy-uid"] || "").toString().trim();
15
+ const lockModel = (headers["x-mslxdff-model-lock"] || headers["X-Mslxdff-Model-Lock"] || "").toString();
16
+ // auto 范围限定:x-mslxdff-auto-provider: opencode → auto 候选只留该供应商(opencode=裸 id 免费池)
17
+ const autoProvider = (headers["x-mslxdff-auto-provider"] || headers["X-Mslxdff-Auto-Provider"] || "").toString().trim().toLowerCase() || null;
18
+ const rawModel = body.model || "";
19
+
20
+ let normalizedRequested = normalizeModel(lockModel || rawModel || "");
21
+ // [1m] 后缀回退(Claude Desktop 1M 条目 / Claude Code 1M 记法,ADR-0056):alias 直查未命中时
22
+ // 剥去尾部 [1m] 再查一次。仅用于本次查找——不回写、不注册 alias,未命中路径逐字不变(design D2)。
23
+ // 两个 alias 查找点(首轮 + mslxdff/ 前缀二段)共用同一回退,否则 `mslxdff/<槽位>[1m]` 不等价(评审 1 P1)。
24
+ const resolveAlias = (id) => {
25
+ const hit = getModelAlias(id);
26
+ if (hit) return hit;
27
+ return /\[1m\]$/i.test(id) ? getModelAlias(id.replace(/\[1m\]$/i, "")) : null;
28
+ };
29
+ let aliasResolved = resolveAlias(normalizedRequested);
30
+ if (aliasResolved) {
31
+ normalizedRequested = aliasResolved;
32
+ }
33
+ let requested = normalizedRequested;
34
+ let aliasInfo = null;
35
+
36
+ if (requested.startsWith("mslxdff/")) {
37
+ const rawPart = requested.slice("mslxdff/".length);
38
+ aliasInfo = `${requested} -> ${rawPart} (mslxdff provider stripped)`;
39
+ requested = rawPart;
40
+ const alias2 = resolveAlias(requested);
41
+ if (alias2) {
42
+ aliasInfo = `${rawModel} -> ${alias2} (mslxdff + alias)`;
43
+ requested = alias2;
44
+ }
45
+ } else if (aliasResolved) {
46
+ // 非 mslxdff 的 dash 形态已在首轮 aliasResolved 处理
47
+ aliasInfo = aliasResolved !== (lockModel || rawModel) ? `${rawModel} -> ${aliasResolved} (alias)` : null;
48
+ if (!aliasInfo) aliasInfo = null;
49
+ }
50
+
51
+ // workbuddy <uid>:model 形式的 uid 钉死在 normalizeFullId 侧处理,这里透传原始 requested
52
+ // 若 requested 含 workbuddy/ 前缀且含 :,则尝试提取 uid
53
+ let extractedUid = workbuddyUid;
54
+ if (!extractedUid && requested.startsWith("workbuddy/") && requested.includes(":")) {
55
+ const after = requested.slice("workbuddy/".length);
56
+ const uidPart = after.split(":")[0];
57
+ if (uidPart) extractedUid = uidPart;
58
+ }
59
+
60
+ const useAuto = isAutoModel(requested);
61
+
62
+ // 对 workbuddy 前缀的 model,做 normalizeFullId 归一(剥 uid 供上游)
63
+ // 但保留 requested 为完整带前缀形态,供 serial-trial 做 ViaRoute 判定与组员路由
64
+ let normalizedForUpstream = requested;
65
+ try {
66
+ const norm = normalizeFullId(requested);
67
+ if (norm && norm.raw) normalizedForUpstream = norm.raw ? `${norm.provider ? norm.provider + "/" : ""}${norm.raw}` : requested;
68
+ } catch {}
69
+
70
+ return {
71
+ rawModel,
72
+ requested,
73
+ normalizedRequested,
74
+ normalizedForUpstream,
75
+ aliasInfo,
76
+ useAuto,
77
+ autoProvider,
78
+ shareKeys,
79
+ workbuddyUid: extractedUid,
80
+ lockModel,
81
+ hops,
82
+ bodyModel: body.model || null,
83
+ };
84
+ }