mslxdff 0.1.159 → 0.1.160

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 (95) hide show
  1. package/bin/mslxdff.js +4 -4
  2. package/package.json +1 -1
  3. package/src/auto.js +254 -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/auto-race.js +1 -1
  19. package/src/chat-pipeline/index.js +123 -123
  20. package/src/chat-pipeline/policy.js +76 -76
  21. package/src/chat-pipeline/serial-trial.js +210 -210
  22. package/src/cli/commands/group.js +249 -249
  23. package/src/cli/commands/model/picks.js +50 -50
  24. package/src/cli/commands/provider/bench-via.js +247 -247
  25. package/src/cli/commands/provider/bench.js +141 -141
  26. package/src/cli/commands/provider/index.js +124 -124
  27. package/src/cli/commands/provider/models.js +139 -139
  28. package/src/cli/commands/provider/qwenwork-login.js +119 -119
  29. package/src/cli/commands/stats.js +20 -5
  30. package/src/cli/commands/sync.js +232 -232
  31. package/src/cli/provider-row.js +2 -2
  32. package/src/cli/status.js +279 -279
  33. package/src/daemon.js +96 -96
  34. package/src/model-capabilities/enrich.js +86 -86
  35. package/src/model-capabilities/index.js +183 -183
  36. package/src/model-capabilities/parse.js +70 -70
  37. package/src/models.js +225 -225
  38. package/src/providers/cline/auth.js +228 -228
  39. package/src/providers/cline/chat.js +307 -307
  40. package/src/providers/cline.js +2 -2
  41. package/src/providers/keyring.js +60 -60
  42. package/src/providers/qoder/chat.js +183 -183
  43. package/src/providers/qoder/index.js +230 -230
  44. package/src/providers/qoder/sse.js +103 -103
  45. package/src/providers/qwenwork/account-store.js +133 -133
  46. package/src/providers/qwenwork/constants.js +67 -67
  47. package/src/providers/qwenwork/cosy.js +120 -120
  48. package/src/providers/qwenwork/crypto.js +218 -218
  49. package/src/providers/qwenwork/http.js +20 -20
  50. package/src/providers/qwenwork/index.js +327 -327
  51. package/src/providers/qwenwork/payload.js +142 -142
  52. package/src/providers/qwenwork/rsa.js +54 -54
  53. package/src/providers/qwenwork/sse.js +268 -268
  54. package/src/providers/qwenwork/stream.js +130 -130
  55. package/src/providers/qwenwork/upstream.js +120 -120
  56. package/src/providers/qwenwork.js +1 -1
  57. package/src/providers/registry.js +66 -66
  58. package/src/providers/workbuddy/chat.js +248 -248
  59. package/src/providers/workbuddy/reshape.js +152 -152
  60. package/src/providers/workbuddy.js +2 -2
  61. package/src/providers/zcode/chat.js +11 -10
  62. package/src/providers/zcode/const.js +1 -1
  63. package/src/providers/zcode/context-shape.js +162 -0
  64. package/src/providers/zcode/headers.js +38 -0
  65. package/src/reasoning.js +32 -32
  66. package/src/routes/chat/broadband-handler.js +3 -2
  67. package/src/routes/chat/gateway.js +46 -46
  68. package/src/routes/chat/hedge-handler.js +3 -0
  69. package/src/routes/chat/local-handler.js +2 -0
  70. package/src/routes/chat/peer-handler.js +2 -0
  71. package/src/routes/chat/relay-pipeline.js +264 -250
  72. package/src/routes/chat/via-route-handler.js +146 -144
  73. package/src/routes/hedge.js +255 -255
  74. package/src/routes/models-route.js +167 -167
  75. package/src/routes/peers.js +273 -273
  76. package/src/routes/stream-scan.js +86 -0
  77. package/src/routes/stream.js +393 -438
  78. package/src/runtime/bootstrap.js +45 -45
  79. package/src/runtime/provider-gate.js +33 -33
  80. package/src/runtime/providers-setup.js +165 -165
  81. package/src/server.js +64 -64
  82. package/src/state/schemas/allowlist.js +92 -92
  83. package/src/sync-opencode.js +280 -280
  84. package/src/transport/index.js +244 -244
  85. package/src/transport/pool.js +56 -56
  86. package/src/transport/retry.js +24 -24
  87. package/src/transport/sse.js +93 -93
  88. package/src/upstream-probe/display.js +52 -52
  89. package/src/upstream-probe/probe.js +49 -49
  90. package/src/upstream-probe/rotate.js +110 -110
  91. package/src/upstream-probe/start.js +45 -45
  92. package/src/upstream.js +289 -289
  93. package/src/usage/record.js +8 -1
  94. package/src/usage/report.js +39 -2
  95. package/docs/cli_help_mini.md +0 -133
@@ -1,93 +1,93 @@
1
- /**
2
- * SSE 行协议深模块 — 纯函数 + 流式
3
- * 负责 `data: / : / event: / [DONE]` 的解析与粘包 remain 处理
4
- */
5
-
6
- export function parseSseChunk(buf) {
7
- const events = [];
8
- let remain = String(buf || "");
9
- // 按 \n\n 分帧,\r\n 也兼容
10
- // 保留最后一帧若不以 \n\n 结尾则为 remain
11
- const normalized = remain.replace(/\r\n/g, "\n");
12
- const parts = normalized.split("\n\n");
13
- // 最后一部分若原串不以 \n\n 结尾则为 remain
14
- const endsWithDelim = normalized.endsWith("\n\n") || normalized.endsWith("\n\r\n");
15
- // parts 最后一项在非结尾时为未完成帧
16
- const complete = endsWithDelim ? parts : parts.slice(0, -1);
17
- remain = endsWithDelim ? "" : parts[parts.length - 1] || "";
18
- for (const frame of complete) {
19
- if (!frame) continue; // 空帧
20
- const lines = frame.split("\n");
21
- const dataLines = [];
22
- for (const line of lines) {
23
- if (!line) continue;
24
- if (line.startsWith(":")) continue; // 注释/keepalive
25
- if (line.startsWith("event:")) continue; // 事件类型忽略
26
- if (line.startsWith("data:")) {
27
- let v = line.slice(5);
28
- if (v.startsWith(" ")) v = v.slice(1);
29
- dataLines.push(v);
30
- }
31
- }
32
- if (dataLines.length === 0) continue;
33
- // 多行 data 按 \n 拼接(SSE 规范)
34
- const payload = dataLines.join("\n");
35
- // 空 data: 仍产生事件,由上层过滤;此处保留以便测试可见
36
- events.push(payload);
37
- }
38
- return { events, remain };
39
- }
40
-
41
- export function createSseParser() {
42
- let buf = "";
43
- return {
44
- push(chunk) {
45
- buf += String(chunk || "");
46
- const { events, remain } = parseSseChunk(buf);
47
- buf = remain;
48
- return events;
49
- },
50
- flush() {
51
- // 未形成完整帧的不发(避免半包误触发)
52
- if (!buf.trim()) { buf = ""; return []; }
53
- // 若残留包含完整 data: 行但缺结尾,不强行发
54
- return [];
55
- },
56
- getRemain() { return buf; },
57
- };
58
- }
59
-
60
- export async function* streamFromResponse(res) {
61
- if (!res || !res.body) return;
62
- const parser = createSseParser();
63
- const reader = res.body.getReader ? res.body.getReader() : null;
64
- const decoder = new TextDecoder();
65
- if (reader) {
66
- while (true) {
67
- const { done, value } = await reader.read();
68
- if (done) break;
69
- const text = decoder.decode(value, { stream: true });
70
- const evs = parser.push(text);
71
- for (const e of evs) {
72
- if (e === "[DONE]") return;
73
- if (e === "") continue;
74
- yield e;
75
- }
76
- }
77
- // flush 残留
78
- const rest = parser.flush();
79
- for (const e of rest) {
80
- if (e === "[DONE]") return;
81
- if (e) yield e;
82
- }
83
- } else if (typeof res.text === "function") {
84
- // 回退:一次性 text
85
- const txt = await res.text();
86
- const evs = parser.push(txt);
87
- for (const e of evs) {
88
- if (e === "[DONE]") return;
89
- if (e === "") continue;
90
- yield e;
91
- }
92
- }
93
- }
1
+ /**
2
+ * SSE 行协议深模块 — 纯函数 + 流式
3
+ * 负责 `data: / : / event: / [DONE]` 的解析与粘包 remain 处理
4
+ */
5
+
6
+ export function parseSseChunk(buf) {
7
+ const events = [];
8
+ let remain = String(buf || "");
9
+ // 按 \n\n 分帧,\r\n 也兼容
10
+ // 保留最后一帧若不以 \n\n 结尾则为 remain
11
+ const normalized = remain.replace(/\r\n/g, "\n");
12
+ const parts = normalized.split("\n\n");
13
+ // 最后一部分若原串不以 \n\n 结尾则为 remain
14
+ const endsWithDelim = normalized.endsWith("\n\n") || normalized.endsWith("\n\r\n");
15
+ // parts 最后一项在非结尾时为未完成帧
16
+ const complete = endsWithDelim ? parts : parts.slice(0, -1);
17
+ remain = endsWithDelim ? "" : parts[parts.length - 1] || "";
18
+ for (const frame of complete) {
19
+ if (!frame) continue; // 空帧
20
+ const lines = frame.split("\n");
21
+ const dataLines = [];
22
+ for (const line of lines) {
23
+ if (!line) continue;
24
+ if (line.startsWith(":")) continue; // 注释/keepalive
25
+ if (line.startsWith("event:")) continue; // 事件类型忽略
26
+ if (line.startsWith("data:")) {
27
+ let v = line.slice(5);
28
+ if (v.startsWith(" ")) v = v.slice(1);
29
+ dataLines.push(v);
30
+ }
31
+ }
32
+ if (dataLines.length === 0) continue;
33
+ // 多行 data 按 \n 拼接(SSE 规范)
34
+ const payload = dataLines.join("\n");
35
+ // 空 data: 仍产生事件,由上层过滤;此处保留以便测试可见
36
+ events.push(payload);
37
+ }
38
+ return { events, remain };
39
+ }
40
+
41
+ export function createSseParser() {
42
+ let buf = "";
43
+ return {
44
+ push(chunk) {
45
+ buf += String(chunk || "");
46
+ const { events, remain } = parseSseChunk(buf);
47
+ buf = remain;
48
+ return events;
49
+ },
50
+ flush() {
51
+ // 未形成完整帧的不发(避免半包误触发)
52
+ if (!buf.trim()) { buf = ""; return []; }
53
+ // 若残留包含完整 data: 行但缺结尾,不强行发
54
+ return [];
55
+ },
56
+ getRemain() { return buf; },
57
+ };
58
+ }
59
+
60
+ export async function* streamFromResponse(res) {
61
+ if (!res || !res.body) return;
62
+ const parser = createSseParser();
63
+ const reader = res.body.getReader ? res.body.getReader() : null;
64
+ const decoder = new TextDecoder();
65
+ if (reader) {
66
+ while (true) {
67
+ const { done, value } = await reader.read();
68
+ if (done) break;
69
+ const text = decoder.decode(value, { stream: true });
70
+ const evs = parser.push(text);
71
+ for (const e of evs) {
72
+ if (e === "[DONE]") return;
73
+ if (e === "") continue;
74
+ yield e;
75
+ }
76
+ }
77
+ // flush 残留
78
+ const rest = parser.flush();
79
+ for (const e of rest) {
80
+ if (e === "[DONE]") return;
81
+ if (e) yield e;
82
+ }
83
+ } else if (typeof res.text === "function") {
84
+ // 回退:一次性 text
85
+ const txt = await res.text();
86
+ const evs = parser.push(txt);
87
+ for (const e of evs) {
88
+ if (e === "[DONE]") return;
89
+ if (e === "") continue;
90
+ yield e;
91
+ }
92
+ }
93
+ }
@@ -1,52 +1,52 @@
1
- // ADR-0015 展示渲染:-group list 行缀 + -status 汇总(纯函数,输入 routes 数据)
2
- // 四态文案:无数据→引导;有数据→摘要;坏数据→标 offline
3
-
4
- function bestDisplay(entry) {
5
- if (!entry) return null;
6
- const best = String(entry.best || "direct");
7
- const ms = best === "direct" ? (entry.direct?.ttfbMs ?? entry.direct?.totalMs) : (entry.via?.[best.slice(4)]?.ttfbMs ?? entry.via?.[best.slice(4)]?.totalMs);
8
- const tag = best === "direct" ? "direct" : `via ${best.slice(4)}`;
9
- if (entry.direct?.ok === false && !best.startsWith("via:")) return `${tag} (${entry.direct?.label || "offline"})`;
10
- return ms != null ? `${tag} ${ms}ms` : tag;
11
- }
12
-
13
- // -group list 静态成员行尾缀:该成员作为 best 出现时的供应商与相对直连收益
14
- export function groupRowSuffix(peerUrl, routes) {
15
- if (!peerUrl || !routes || typeof routes !== "object") return null;
16
- const label = (() => {
17
- const raw = String(peerUrl);
18
- if (raw.includes("://")) {
19
- try { const u = new URL(raw); return `${u.hostname}${u.port ? `:${u.port}` : ""}`; } catch { return raw.slice(-16); }
20
- }
21
- return raw;
22
- })();
23
- const wins = [];
24
- for (const [key, entry] of Object.entries(routes)) {
25
- if (!key.startsWith("provider:")) continue;
26
- const prov = key.slice(9);
27
- if (entry?.best === `via:${label}`) {
28
- const d = entry.deltaMs;
29
- wins.push(d != null ? `${prov} ${d > 0 ? "+" : ""}${d}ms` : prov);
30
- }
31
- }
32
- if (!wins.length) return null;
33
- return ` via-routes: ${wins.join(", ")}`;
34
- }
35
-
36
- // -status 汇总(多行字符串):探针条目摘要 ≤5 条;空数据给引导
37
- export function statusViaSummary({ routes, meta, probeMs }) {
38
- const list = Object.entries(routes || {}).filter(([k]) => k.startsWith("provider:"));
39
- if (!list.length) {
40
- return `via-routes: 无探针数据(后台探针 ${probeMs === 0 ? "已关闭" : "自动运行中"},加入组并配置 key/token 类供应商后自动生成)`;
41
- }
42
- const at = meta?.at ? new Date(meta.at) : null;
43
- const atStr = at && !Number.isNaN(at.getTime()) ? at.toISOString().slice(11, 19) + "Z" : "";
44
- const lines = [`via-routes: ${list.length} 条(探针自动${atStr ? `,${atStr} 刷新` : ""})`];
45
- for (const [key, entry] of list.slice(0, 5)) {
46
- const prov = key.slice(9);
47
- const b = bestDisplay(entry);
48
- if (b) lines.push(` ${prov} → ${b}${entry.deltaMs != null ? ` (${entry.deltaMs > 0 ? "+" : ""}${entry.deltaMs}ms vs direct)` : ""}`);
49
- }
50
- if (list.length > 5) lines.push(` … 其余 ${list.length - 5} 条见 via-routes.json`);
51
- return lines.join("\n");
52
- }
1
+ // ADR-0015 展示渲染:-group list 行缀 + -status 汇总(纯函数,输入 routes 数据)
2
+ // 四态文案:无数据→引导;有数据→摘要;坏数据→标 offline
3
+
4
+ function bestDisplay(entry) {
5
+ if (!entry) return null;
6
+ const best = String(entry.best || "direct");
7
+ const ms = best === "direct" ? (entry.direct?.ttfbMs ?? entry.direct?.totalMs) : (entry.via?.[best.slice(4)]?.ttfbMs ?? entry.via?.[best.slice(4)]?.totalMs);
8
+ const tag = best === "direct" ? "direct" : `via ${best.slice(4)}`;
9
+ if (entry.direct?.ok === false && !best.startsWith("via:")) return `${tag} (${entry.direct?.label || "offline"})`;
10
+ return ms != null ? `${tag} ${ms}ms` : tag;
11
+ }
12
+
13
+ // -group list 静态成员行尾缀:该成员作为 best 出现时的供应商与相对直连收益
14
+ export function groupRowSuffix(peerUrl, routes) {
15
+ if (!peerUrl || !routes || typeof routes !== "object") return null;
16
+ const label = (() => {
17
+ const raw = String(peerUrl);
18
+ if (raw.includes("://")) {
19
+ try { const u = new URL(raw); return `${u.hostname}${u.port ? `:${u.port}` : ""}`; } catch { return raw.slice(-16); }
20
+ }
21
+ return raw;
22
+ })();
23
+ const wins = [];
24
+ for (const [key, entry] of Object.entries(routes)) {
25
+ if (!key.startsWith("provider:")) continue;
26
+ const prov = key.slice(9);
27
+ if (entry?.best === `via:${label}`) {
28
+ const d = entry.deltaMs;
29
+ wins.push(d != null ? `${prov} ${d > 0 ? "+" : ""}${d}ms` : prov);
30
+ }
31
+ }
32
+ if (!wins.length) return null;
33
+ return ` via-routes: ${wins.join(", ")}`;
34
+ }
35
+
36
+ // -status 汇总(多行字符串):探针条目摘要 ≤5 条;空数据给引导
37
+ export function statusViaSummary({ routes, meta, probeMs }) {
38
+ const list = Object.entries(routes || {}).filter(([k]) => k.startsWith("provider:"));
39
+ if (!list.length) {
40
+ return `via-routes: 无探针数据(后台探针 ${probeMs === 0 ? "已关闭" : "自动运行中"},加入组并配置 key/token 类供应商后自动生成)`;
41
+ }
42
+ const at = meta?.at ? new Date(meta.at) : null;
43
+ const atStr = at && !Number.isNaN(at.getTime()) ? at.toISOString().slice(11, 19) + "Z" : "";
44
+ const lines = [`via-routes: ${list.length} 条(探针自动${atStr ? `,${atStr} 刷新` : ""})`];
45
+ for (const [key, entry] of list.slice(0, 5)) {
46
+ const prov = key.slice(9);
47
+ const b = bestDisplay(entry);
48
+ if (b) lines.push(` ${prov} → ${b}${entry.deltaMs != null ? ` (${entry.deltaMs > 0 ? "+" : ""}${entry.deltaMs}ms vs direct)` : ""}`);
49
+ }
50
+ if (list.length > 5) lines.push(` … 其余 ${list.length - 5} 条见 via-routes.json`);
51
+ return lines.join("\n");
52
+ }
@@ -1,49 +1,49 @@
1
- // ADR-0015 探针薄层:direct GET 探针 + 组员 /v1/relay 代发探针
2
- // 只测 GET <modelsPath>,不烧 token;组长侧计时。
3
- import { errMsg } from "../cli/util.js";
4
- import { compatFetch, timeoutSignal } from "../compat.js";
5
-
6
- export const PROBE_TIMEOUT_MS = 5000;
7
-
8
- export async function directProbe({ baseUrl, modelsPath = "/models", key = "", timeoutMs = PROBE_TIMEOUT_MS, fetchImpl = compatFetch, clock = Date.now, sampleMs } = {}) {
9
- const base = String(baseUrl || "").replace(/\/+$/, "");
10
- if (!base) return { ok: false, label: "配置错误", error: "missing baseUrl", ttfbMs: null };
11
- const p = String(modelsPath || "/models");
12
- const url = `${base}${p.startsWith("/") ? p : `/${p}`}`;
13
- const t0 = sampleMs ? sampleMs() : clock();
14
- try {
15
- const headers = { Accept: "application/json" };
16
- if (key) headers.Authorization = `Bearer ${key}`;
17
- const res = await fetchImpl(url, { method: "GET", headers, signal: timeoutSignal(timeoutMs) });
18
- const ms = sampleMs ? sampleMs() - t0 : clock() - t0;
19
- const status = typeof res.status === "number" ? res.status : (res.ok ? 200 : 0);
20
- if (status >= 400) return { ok: false, label: `HTTP ${status}`, ttfbMs: null };
21
- return { ok: true, ttfbMs: Math.max(0, Math.round(ms)) };
22
- } catch (e) {
23
- return { ok: false, label: errMsg(e).slice(0, 60), ttfbMs: null };
24
- }
25
- }
26
-
27
- // 组员代发:POST <peer>/v1/relay { targetUrl, method:"GET", headers } → 组员 fetch 上游后原样回
28
- export async function relayViaProbe({ peerUrl, peerToken = "", targetUrl, authHeader = "", timeoutMs = PROBE_TIMEOUT_MS, fetchImpl = compatFetch, clock = Date.now, sampleMs } = {}) {
29
- const peer = String(peerUrl || "").replace(/\/+$/, "");
30
- if (!peer || !targetUrl) return { ok: false, label: "配置错误", ttfbMs: null };
31
- const t0 = sampleMs ? sampleMs() : clock();
32
- try {
33
- const headers = { "Content-Type": "application/json", Accept: "application/json" };
34
- if (peerToken) headers.Authorization = `Bearer ${peerToken}`;
35
- const res = await fetchImpl(`${peer}/v1/relay`, {
36
- method: "POST",
37
- headers,
38
- body: JSON.stringify({ targetUrl, method: "GET", headers: authHeader ? { Authorization: authHeader } : {} }),
39
- signal: timeoutSignal(timeoutMs),
40
- });
41
- const ms = sampleMs ? sampleMs() - t0 : clock() - t0;
42
- const relayStatus = res.headers?.get?.("x-mslxdff-relay-status");
43
- const status = relayStatus != null ? Number(relayStatus) : (res.status ?? 0);
44
- if (status >= 400) return { ok: false, label: `HTTP ${status}`, ttfbMs: null };
45
- return { ok: true, ttfbMs: Math.max(0, Math.round(ms)) };
46
- } catch (e) {
47
- return { ok: false, label: errMsg(e).slice(0, 60), ttfbMs: null };
48
- }
49
- }
1
+ // ADR-0015 探针薄层:direct GET 探针 + 组员 /v1/relay 代发探针
2
+ // 只测 GET <modelsPath>,不烧 token;组长侧计时。
3
+ import { errMsg } from "../cli/util.js";
4
+ import { compatFetch, timeoutSignal } from "../compat.js";
5
+
6
+ export const PROBE_TIMEOUT_MS = 5000;
7
+
8
+ export async function directProbe({ baseUrl, modelsPath = "/models", key = "", timeoutMs = PROBE_TIMEOUT_MS, fetchImpl = compatFetch, clock = Date.now, sampleMs } = {}) {
9
+ const base = String(baseUrl || "").replace(/\/+$/, "");
10
+ if (!base) return { ok: false, label: "配置错误", error: "missing baseUrl", ttfbMs: null };
11
+ const p = String(modelsPath || "/models");
12
+ const url = `${base}${p.startsWith("/") ? p : `/${p}`}`;
13
+ const t0 = sampleMs ? sampleMs() : clock();
14
+ try {
15
+ const headers = { Accept: "application/json" };
16
+ if (key) headers.Authorization = `Bearer ${key}`;
17
+ const res = await fetchImpl(url, { method: "GET", headers, signal: timeoutSignal(timeoutMs) });
18
+ const ms = sampleMs ? sampleMs() - t0 : clock() - t0;
19
+ const status = typeof res.status === "number" ? res.status : (res.ok ? 200 : 0);
20
+ if (status >= 400) return { ok: false, label: `HTTP ${status}`, ttfbMs: null };
21
+ return { ok: true, ttfbMs: Math.max(0, Math.round(ms)) };
22
+ } catch (e) {
23
+ return { ok: false, label: errMsg(e).slice(0, 60), ttfbMs: null };
24
+ }
25
+ }
26
+
27
+ // 组员代发:POST <peer>/v1/relay { targetUrl, method:"GET", headers } → 组员 fetch 上游后原样回
28
+ export async function relayViaProbe({ peerUrl, peerToken = "", targetUrl, authHeader = "", timeoutMs = PROBE_TIMEOUT_MS, fetchImpl = compatFetch, clock = Date.now, sampleMs } = {}) {
29
+ const peer = String(peerUrl || "").replace(/\/+$/, "");
30
+ if (!peer || !targetUrl) return { ok: false, label: "配置错误", ttfbMs: null };
31
+ const t0 = sampleMs ? sampleMs() : clock();
32
+ try {
33
+ const headers = { "Content-Type": "application/json", Accept: "application/json" };
34
+ if (peerToken) headers.Authorization = `Bearer ${peerToken}`;
35
+ const res = await fetchImpl(`${peer}/v1/relay`, {
36
+ method: "POST",
37
+ headers,
38
+ body: JSON.stringify({ targetUrl, method: "GET", headers: authHeader ? { Authorization: authHeader } : {} }),
39
+ signal: timeoutSignal(timeoutMs),
40
+ });
41
+ const ms = sampleMs ? sampleMs() - t0 : clock() - t0;
42
+ const relayStatus = res.headers?.get?.("x-mslxdff-relay-status");
43
+ const status = relayStatus != null ? Number(relayStatus) : (res.status ?? 0);
44
+ if (status >= 400) return { ok: false, label: `HTTP ${status}`, ttfbMs: null };
45
+ return { ok: true, ttfbMs: Math.max(0, Math.round(ms)) };
46
+ } catch (e) {
47
+ return { ok: false, label: errMsg(e).slice(0, 60), ttfbMs: null };
48
+ }
49
+ }
@@ -1,110 +1,110 @@
1
- // ADR-0015 轮转调度:每 tick 探 1 家供应商(direct + 逐 peer relay),EMA 合并旧值后落 via-routes.json
2
- // 复用 bench/via-routes.js 的表格式与读写;探针写 provider:<id> 键,getViaRoute 精确键优先、provider 级回退。
3
- import { directProbe, relayViaProbe } from "./probe.js";
4
- import { classifyProvider, emaMerge } from "../providers/classify.js";
5
- import { saveViaRoutes, loadViaRoutes } from "../bench/via-routes.js";
6
- import { errMsg } from "../cli/util.js";
7
-
8
- export const PROBE_DELAY_MS = Number(process.env.MSLXDFF_BENCH_DELAY_MS || 120) || 0;
9
-
10
- // 从 providerConfigs 收集探针目标:local-only(workbuddy/cline系)/quota-pool(opencode) 排除,无 baseUrl 跳过
11
- export function probeTargetsFromState({ loadProviderConfigs, loadProviderKeys, loadProviderBaseUrl } = {}) {
12
- const out = [];
13
- const ids = new Set(Object.keys(loadProviderConfigs?.() || {}));
14
- for (const id of ids) {
15
- if (classifyProvider(id) !== "latency-compare") continue;
16
- const cfg = (loadProviderConfigs?.() || {})[id] || {};
17
- const baseUrl = String(loadProviderBaseUrl?.(id) || cfg.baseUrl || "").replace(/\/+$/, "");
18
- if (!baseUrl) continue;
19
- out.push({ id, baseUrl, modelsPath: cfg.modelsPath || "/models", key: (loadProviderKeys?.(id) || [])[0] || "" });
20
- }
21
- return out;
22
- }
23
-
24
- export function nextCursor(cursor, total) {
25
- if (!Number.isInteger(total) || total <= 0) return 0;
26
- const c = Number.isInteger(cursor) ? cursor : 0;
27
- return c + 1 >= total ? 0 : c + 1;
28
- }
29
-
30
- function peerLabel(p) {
31
- const raw = String(p?.url || "");
32
- if (!raw) return "";
33
- if (raw.includes("://")) {
34
- try { const u = new URL(raw); return `${u.hostname}${u.port ? `:${u.port}` : ""}`; } catch { return raw.slice(-16); }
35
- }
36
- return raw;
37
- }
38
-
39
- // 单 tick:探 targets[cursor] 一家;返回 { probed, skipped }
40
- export async function rotateTick({
41
- targets,
42
- cursor = 0,
43
- peers,
44
- timeoutMs = 5000,
45
- delayMs = PROBE_DELAY_MS,
46
- evt = () => {},
47
- fetchImpl,
48
- clock = Date.now,
49
- sampleMs,
50
- file,
51
- } = {}) {
52
- const list = Array.isArray(targets) ? targets : [];
53
- if (!list.length) return { probed: null, skipped: true };
54
- const idx = cursor % list.length;
55
- const target = list[idx];
56
- const peerList = (peers?.all?.() || []).filter((p) => p?.url && !String(p.url).startsWith("relay://"));
57
- if (!peerList.length) {
58
- evt("upstream-probe-skip", { provider: target.id, reason: "no peers" });
59
- return { probed: null, skipped: true };
60
- }
61
- const authHeader = target.key ? `Bearer ${target.key}` : "";
62
- const direct = await directProbe({ baseUrl: target.baseUrl, modelsPath: target.modelsPath, key: target.key, timeoutMs, fetchImpl, clock, sampleMs });
63
- const via = {};
64
- for (const p of peerList) {
65
- const label = peerLabel(p) || String(p.url);
66
- const r = await relayViaProbe({ peerUrl: p.url, peerToken: p.token || "", targetUrl: `${target.baseUrl}${target.modelsPath}`, authHeader, timeoutMs, fetchImpl, clock, sampleMs });
67
- via[label] = { ok: r.ok, ttfbMs: r.ok ? r.ttfbMs : null, totalMs: r.ok ? r.ttfbMs : null, label: r.ok ? null : r.label };
68
- if (delayMs) await new Promise((res) => setTimeout(res, delayMs));
69
- }
70
- const prev = loadViaRoutes(file).routes?.[`provider:${target.id}`] || null;
71
- const fresh = prev?.at && Date.now() - Date.parse(prev.at) < 10 * 60_000 ? prev : null;
72
- const merged = mergeEntry({ prev: fresh, direct, via });
73
- const results = [{ model: `provider:${target.id}`, ...merged, provider: target.id }];
74
- try {
75
- saveViaRoutes(results, { file, meta: { probe: true } });
76
- } catch (e) {
77
- evt("upstream-probe-error", { provider: target.id, error: errMsg(e).slice(0, 120) });
78
- return { probed: target.id, skipped: false, error: errMsg(e) };
79
- }
80
- evt("upstream-probe", { provider: target.id, direct: direct.ok ? direct.ttfbMs : null, best: merged.best, peers: Object.keys(via).length });
81
- return { probed: target.id, skipped: false };
82
- }
83
-
84
- // best 计算 + EMA 合并(相对 direct:via 胜出记负 delta)
85
- function mergeEntry({ prev, direct, via }) {
86
- const ema = (p, r) => emaMerge(p, r?.ok ? r.ttfbMs : null);
87
- const directOut = {
88
- ok: Boolean(direct?.ok),
89
- ttfbMs: direct?.ok ? ema(prev?.direct?.ttfbMs, direct) : null,
90
- totalMs: direct?.ok ? ema(prev?.direct?.totalMs ?? prev?.direct?.ttfbMs, direct) : null,
91
- label: direct?.ok ? null : (direct?.label || "offline"),
92
- error: direct?.ok ? null : (direct?.label || null),
93
- };
94
- const viaOut = {};
95
- for (const [k, v] of Object.entries(via || {})) {
96
- viaOut[k] = v.ok
97
- ? { ok: true, ttfbMs: ema(prev?.via?.[k]?.ttfbMs, v), totalMs: ema(prev?.via?.[k]?.totalMs ?? prev?.via?.[k]?.ttfbMs, v) }
98
- : { ok: false, ttfbMs: v.ttfbMs ?? null, totalMs: v.totalMs ?? null, label: v.label || "offline" };
99
- }
100
- const directMs = directOut.ok ? (directOut.ttfbMs ?? directOut.totalMs) : Infinity;
101
- let best = "direct";
102
- let bestMs = directMs;
103
- for (const [k, v] of Object.entries(viaOut)) {
104
- if (!v.ok) continue;
105
- const t = v.ttfbMs ?? v.totalMs;
106
- if (Number.isFinite(t) && t < bestMs) { bestMs = t; best = `via:${k}`; }
107
- }
108
- const deltaMs = best.startsWith("via:") && Number.isFinite(directMs) ? Math.round(bestMs - directMs) : null;
109
- return { best, direct: directOut, via: viaOut, deltaMs };
110
- }
1
+ // ADR-0015 轮转调度:每 tick 探 1 家供应商(direct + 逐 peer relay),EMA 合并旧值后落 via-routes.json
2
+ // 复用 bench/via-routes.js 的表格式与读写;探针写 provider:<id> 键,getViaRoute 精确键优先、provider 级回退。
3
+ import { directProbe, relayViaProbe } from "./probe.js";
4
+ import { classifyProvider, emaMerge } from "../providers/classify.js";
5
+ import { saveViaRoutes, loadViaRoutes } from "../bench/via-routes.js";
6
+ import { errMsg } from "../cli/util.js";
7
+
8
+ export const PROBE_DELAY_MS = Number(process.env.MSLXDFF_BENCH_DELAY_MS || 120) || 0;
9
+
10
+ // 从 providerConfigs 收集探针目标:local-only(workbuddy/cline系)/quota-pool(opencode) 排除,无 baseUrl 跳过
11
+ export function probeTargetsFromState({ loadProviderConfigs, loadProviderKeys, loadProviderBaseUrl } = {}) {
12
+ const out = [];
13
+ const ids = new Set(Object.keys(loadProviderConfigs?.() || {}));
14
+ for (const id of ids) {
15
+ if (classifyProvider(id) !== "latency-compare") continue;
16
+ const cfg = (loadProviderConfigs?.() || {})[id] || {};
17
+ const baseUrl = String(loadProviderBaseUrl?.(id) || cfg.baseUrl || "").replace(/\/+$/, "");
18
+ if (!baseUrl) continue;
19
+ out.push({ id, baseUrl, modelsPath: cfg.modelsPath || "/models", key: (loadProviderKeys?.(id) || [])[0] || "" });
20
+ }
21
+ return out;
22
+ }
23
+
24
+ export function nextCursor(cursor, total) {
25
+ if (!Number.isInteger(total) || total <= 0) return 0;
26
+ const c = Number.isInteger(cursor) ? cursor : 0;
27
+ return c + 1 >= total ? 0 : c + 1;
28
+ }
29
+
30
+ function peerLabel(p) {
31
+ const raw = String(p?.url || "");
32
+ if (!raw) return "";
33
+ if (raw.includes("://")) {
34
+ try { const u = new URL(raw); return `${u.hostname}${u.port ? `:${u.port}` : ""}`; } catch { return raw.slice(-16); }
35
+ }
36
+ return raw;
37
+ }
38
+
39
+ // 单 tick:探 targets[cursor] 一家;返回 { probed, skipped }
40
+ export async function rotateTick({
41
+ targets,
42
+ cursor = 0,
43
+ peers,
44
+ timeoutMs = 5000,
45
+ delayMs = PROBE_DELAY_MS,
46
+ evt = () => {},
47
+ fetchImpl,
48
+ clock = Date.now,
49
+ sampleMs,
50
+ file,
51
+ } = {}) {
52
+ const list = Array.isArray(targets) ? targets : [];
53
+ if (!list.length) return { probed: null, skipped: true };
54
+ const idx = cursor % list.length;
55
+ const target = list[idx];
56
+ const peerList = (peers?.all?.() || []).filter((p) => p?.url && !String(p.url).startsWith("relay://"));
57
+ if (!peerList.length) {
58
+ evt("upstream-probe-skip", { provider: target.id, reason: "no peers" });
59
+ return { probed: null, skipped: true };
60
+ }
61
+ const authHeader = target.key ? `Bearer ${target.key}` : "";
62
+ const direct = await directProbe({ baseUrl: target.baseUrl, modelsPath: target.modelsPath, key: target.key, timeoutMs, fetchImpl, clock, sampleMs });
63
+ const via = {};
64
+ for (const p of peerList) {
65
+ const label = peerLabel(p) || String(p.url);
66
+ const r = await relayViaProbe({ peerUrl: p.url, peerToken: p.token || "", targetUrl: `${target.baseUrl}${target.modelsPath}`, authHeader, timeoutMs, fetchImpl, clock, sampleMs });
67
+ via[label] = { ok: r.ok, ttfbMs: r.ok ? r.ttfbMs : null, totalMs: r.ok ? r.ttfbMs : null, label: r.ok ? null : r.label };
68
+ if (delayMs) await new Promise((res) => setTimeout(res, delayMs));
69
+ }
70
+ const prev = loadViaRoutes(file).routes?.[`provider:${target.id}`] || null;
71
+ const fresh = prev?.at && Date.now() - Date.parse(prev.at) < 10 * 60_000 ? prev : null;
72
+ const merged = mergeEntry({ prev: fresh, direct, via });
73
+ const results = [{ model: `provider:${target.id}`, ...merged, provider: target.id }];
74
+ try {
75
+ saveViaRoutes(results, { file, meta: { probe: true } });
76
+ } catch (e) {
77
+ evt("upstream-probe-error", { provider: target.id, error: errMsg(e).slice(0, 120) });
78
+ return { probed: target.id, skipped: false, error: errMsg(e) };
79
+ }
80
+ evt("upstream-probe", { provider: target.id, direct: direct.ok ? direct.ttfbMs : null, best: merged.best, peers: Object.keys(via).length });
81
+ return { probed: target.id, skipped: false };
82
+ }
83
+
84
+ // best 计算 + EMA 合并(相对 direct:via 胜出记负 delta)
85
+ function mergeEntry({ prev, direct, via }) {
86
+ const ema = (p, r) => emaMerge(p, r?.ok ? r.ttfbMs : null);
87
+ const directOut = {
88
+ ok: Boolean(direct?.ok),
89
+ ttfbMs: direct?.ok ? ema(prev?.direct?.ttfbMs, direct) : null,
90
+ totalMs: direct?.ok ? ema(prev?.direct?.totalMs ?? prev?.direct?.ttfbMs, direct) : null,
91
+ label: direct?.ok ? null : (direct?.label || "offline"),
92
+ error: direct?.ok ? null : (direct?.label || null),
93
+ };
94
+ const viaOut = {};
95
+ for (const [k, v] of Object.entries(via || {})) {
96
+ viaOut[k] = v.ok
97
+ ? { ok: true, ttfbMs: ema(prev?.via?.[k]?.ttfbMs, v), totalMs: ema(prev?.via?.[k]?.totalMs ?? prev?.via?.[k]?.ttfbMs, v) }
98
+ : { ok: false, ttfbMs: v.ttfbMs ?? null, totalMs: v.totalMs ?? null, label: v.label || "offline" };
99
+ }
100
+ const directMs = directOut.ok ? (directOut.ttfbMs ?? directOut.totalMs) : Infinity;
101
+ let best = "direct";
102
+ let bestMs = directMs;
103
+ for (const [k, v] of Object.entries(viaOut)) {
104
+ if (!v.ok) continue;
105
+ const t = v.ttfbMs ?? v.totalMs;
106
+ if (Number.isFinite(t) && t < bestMs) { bestMs = t; best = `via:${k}`; }
107
+ }
108
+ const deltaMs = best.startsWith("via:") && Number.isFinite(directMs) ? Math.round(bestMs - directMs) : null;
109
+ return { best, direct: directOut, via: viaOut, deltaMs };
110
+ }