mslxdff 0.1.231 → 0.1.232

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 (104) hide show
  1. package/bin/mslxdff.js +3 -3
  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/index.js +145 -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/models.js +225 -225
  35. package/src/peers.js +345 -345
  36. package/src/providers/agentrouter.js +238 -238
  37. package/src/providers/base.js +164 -164
  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/codearts/chat.js +207 -207
  42. package/src/providers/dispatcher.js +147 -147
  43. package/src/providers/globalqwenwork/cosy.js +123 -123
  44. package/src/providers/globalqwenwork/crypto.js +218 -218
  45. package/src/providers/globalqwenwork/http.js +20 -20
  46. package/src/providers/globalqwenwork/rsa.js +54 -54
  47. package/src/providers/globalqwenwork/sse.js +268 -268
  48. package/src/providers/globalqwenwork/stream.js +130 -130
  49. package/src/providers/globalqwenwork/upstream.js +122 -122
  50. package/src/providers/keyring.js +60 -60
  51. package/src/providers/qoder/chat.js +203 -203
  52. package/src/providers/qoder/index.js +249 -249
  53. package/src/providers/qoder/sse.js +145 -145
  54. package/src/providers/qwenwork/account-store.js +133 -133
  55. package/src/providers/qwenwork/constants.js +67 -67
  56. package/src/providers/qwenwork/cosy.js +120 -120
  57. package/src/providers/qwenwork/crypto.js +218 -218
  58. package/src/providers/qwenwork/http.js +20 -20
  59. package/src/providers/qwenwork/index.js +337 -337
  60. package/src/providers/qwenwork/payload.js +144 -144
  61. package/src/providers/qwenwork/rsa.js +54 -54
  62. package/src/providers/qwenwork/sse.js +268 -268
  63. package/src/providers/qwenwork/stream.js +130 -130
  64. package/src/providers/qwenwork/upstream.js +120 -120
  65. package/src/providers/qwenwork.js +1 -1
  66. package/src/providers/raccoon/const.js +202 -202
  67. package/src/providers/raccoon/credits.js +285 -285
  68. package/src/providers/registry.js +89 -89
  69. package/src/providers/workbuddy/auth.js +16 -0
  70. package/src/providers/workbuddy/chat.js +298 -264
  71. package/src/providers/workbuddy/client-headers.js +148 -0
  72. package/src/providers/workbuddy/desktop-info.js +17 -1
  73. package/src/providers/workbuddy/models.js +8 -20
  74. package/src/providers/workbuddy/reshape.js +152 -152
  75. package/src/providers/workbuddy.js +2 -2
  76. package/src/reasoning.js +32 -32
  77. package/src/routes/chat/broadband-handler.js +80 -80
  78. package/src/routes/chat/gateway.js +46 -46
  79. package/src/routes/chat/relay-pipeline.js +282 -282
  80. package/src/routes/chat/via-route-handler.js +156 -156
  81. package/src/routes/hedge.js +260 -260
  82. package/src/routes/models-route.js +167 -167
  83. package/src/routes/peers.js +373 -373
  84. package/src/routes/stream.js +347 -347
  85. package/src/runtime/bootstrap.js +75 -75
  86. package/src/runtime/keep-warm.js +135 -135
  87. package/src/runtime/provider-gate.js +75 -75
  88. package/src/runtime/providers-setup.js +187 -187
  89. package/src/runtime/raccoon-checkin.js +175 -175
  90. package/src/server.js +85 -85
  91. package/src/state/schemas/allowlist.js +92 -92
  92. package/src/sync-opencode.js +280 -280
  93. package/src/transport/index.js +244 -244
  94. package/src/transport/pool.js +56 -56
  95. package/src/transport/retry.js +24 -24
  96. package/src/transport/sse.js +93 -93
  97. package/src/upstream-probe/display.js +52 -52
  98. package/src/upstream-probe/probe.js +49 -49
  99. package/src/upstream-probe/rotate.js +110 -110
  100. package/src/upstream-probe/start.js +45 -45
  101. package/src/upstream.js +355 -355
  102. package/docs/cli_help_mini.md +0 -148
  103. package/src/providers/AGENTS.md +0 -49
  104. package/src/routes/AGENTS.md +0 -37
@@ -1,71 +1,71 @@
1
- import { joinUrl } from "../providers/base.js";
2
- import { getCustomNormalizer } from "../providers/registry.js";
3
- import { createTransport } from "../transport/index.js";
1
+ import { joinUrl } from "../providers/base.js";
2
+ import { getCustomNormalizer } from "../providers/registry.js";
3
+ import { createTransport } from "../transport/index.js";
4
4
  import { compatFetch } from "../compat.js";
5
-
6
- function normalizeModelsPayload(json, baseUrl = "") {
7
- if (!json) return [];
8
- try {
9
- const custom = getCustomNormalizer(baseUrl);
10
- if (custom) {
11
- const v = custom(json);
12
- if (Array.isArray(v)) return v;
13
- }
14
- } catch {}
15
- if (Array.isArray(json)) return json;
16
- if (Array.isArray(json.data)) return json.data;
17
- if (Array.isArray(json.models)) return json.models;
18
- if (json.data && typeof json.data === "object" && Array.isArray(json.data.data)) return json.data.data;
19
- return [];
20
- }
21
-
22
- function toModelId(m) {
23
- if (!m) return "";
24
- if (typeof m === "string") return m;
25
- if (typeof m.id === "string") return m.id;
26
- if (typeof m.model === "string") return m.model;
27
- if (typeof m.name === "string") return m.name;
28
- return "";
29
- }
30
-
31
- export async function probeModels({
32
- baseUrl,
33
- modelsPath,
34
- chatPath,
35
- headers = {},
36
- fetchImpl = compatFetch,
37
- timeoutMs = 8000,
38
- } = {}) {
39
- const base = String(baseUrl || "").replace(/\/+$/, "");
40
- if (!base) return { ok: false, error: "missing baseUrl", tried: [], data: [] };
41
- const candidates = [];
42
- const seen = new Set();
43
- const push = (p) => {
44
- const s = String(p || "").trim();
45
- if (!s) return;
46
- const norm = s.startsWith("/") ? s : `/${s}`;
47
- if (!seen.has(norm)) { seen.add(norm); candidates.push(norm); }
48
- };
49
- if (modelsPath) push(modelsPath);
50
- push("/v1/models");
51
- push("/models");
52
- const tr = createTransport({ fetchImpl, keepAlive: false, retry: {}, timeoutMs });
53
- const tried = [];
54
- let lastError = "";
55
- for (const p of candidates) {
56
- const url = joinUrl(base, p);
57
- tried.push(url);
58
- try {
59
- const res = await tr.request({ url, method: "GET", headers, stream: false });
60
- if (!res.ok) { lastError = `HTTP ${res.status}`; continue; }
61
- let json = {};
62
- try { json = await res.json(); } catch { json = {}; }
63
- const raw = normalizeModelsPayload(json, baseUrl);
64
- const data = raw.map((m) => ({ id: toModelId(m), raw: m })).filter((x) => x.id).map((x) => ({ id: x.id, ...x.raw }));
65
- return { ok: true, data, tried, url, rawCount: raw.length };
66
- } catch (e) {
67
- lastError = e?.message || String(e);
68
- }
69
- }
70
- return { ok: false, error: lastError || "all probes failed", tried, data: [] };
71
- }
5
+
6
+ function normalizeModelsPayload(json, baseUrl = "") {
7
+ if (!json) return [];
8
+ try {
9
+ const custom = getCustomNormalizer(baseUrl);
10
+ if (custom) {
11
+ const v = custom(json);
12
+ if (Array.isArray(v)) return v;
13
+ }
14
+ } catch {}
15
+ if (Array.isArray(json)) return json;
16
+ if (Array.isArray(json.data)) return json.data;
17
+ if (Array.isArray(json.models)) return json.models;
18
+ if (json.data && typeof json.data === "object" && Array.isArray(json.data.data)) return json.data.data;
19
+ return [];
20
+ }
21
+
22
+ function toModelId(m) {
23
+ if (!m) return "";
24
+ if (typeof m === "string") return m;
25
+ if (typeof m.id === "string") return m.id;
26
+ if (typeof m.model === "string") return m.model;
27
+ if (typeof m.name === "string") return m.name;
28
+ return "";
29
+ }
30
+
31
+ export async function probeModels({
32
+ baseUrl,
33
+ modelsPath,
34
+ chatPath,
35
+ headers = {},
36
+ fetchImpl = compatFetch,
37
+ timeoutMs = 8000,
38
+ } = {}) {
39
+ const base = String(baseUrl || "").replace(/\/+$/, "");
40
+ if (!base) return { ok: false, error: "missing baseUrl", tried: [], data: [] };
41
+ const candidates = [];
42
+ const seen = new Set();
43
+ const push = (p) => {
44
+ const s = String(p || "").trim();
45
+ if (!s) return;
46
+ const norm = s.startsWith("/") ? s : `/${s}`;
47
+ if (!seen.has(norm)) { seen.add(norm); candidates.push(norm); }
48
+ };
49
+ if (modelsPath) push(modelsPath);
50
+ push("/v1/models");
51
+ push("/models");
52
+ const tr = createTransport({ fetchImpl, keepAlive: false, retry: {}, timeoutMs });
53
+ const tried = [];
54
+ let lastError = "";
55
+ for (const p of candidates) {
56
+ const url = joinUrl(base, p);
57
+ tried.push(url);
58
+ try {
59
+ const res = await tr.request({ url, method: "GET", headers, stream: false });
60
+ if (!res.ok) { lastError = `HTTP ${res.status}`; continue; }
61
+ let json = {};
62
+ try { json = await res.json(); } catch { json = {}; }
63
+ const raw = normalizeModelsPayload(json, baseUrl);
64
+ const data = raw.map((m) => ({ id: toModelId(m), raw: m })).filter((x) => x.id).map((x) => ({ id: x.id, ...x.raw }));
65
+ return { ok: true, data, tried, url, rawCount: raw.length };
66
+ } catch (e) {
67
+ lastError = e?.message || String(e);
68
+ }
69
+ }
70
+ return { ok: false, error: lastError || "all probes failed", tried, data: [] };
71
+ }
@@ -1,162 +1,162 @@
1
- export function sortResults(list) {
2
- const arr = [...(list || [])];
3
- arr.sort((a, b) => {
4
- if (a.ok !== b.ok) return a.ok ? -1 : 1;
5
- if (a.ok && b.ok) {
6
- const at = a.ttfbMs ?? a.totalMs ?? 1e9;
7
- const bt = b.ttfbMs ?? b.totalMs ?? 1e9;
8
- if (at !== bt) return at - bt;
9
- const ap = a.tps ?? a.charsPerSec ?? -1;
10
- const bp = b.tps ?? b.charsPerSec ?? -1;
11
- return bp - ap;
12
- }
13
- return 0;
14
- });
15
- return arr;
16
- }
17
-
18
- function pad(s, n, align = "left") {
19
- const str = String(s ?? "");
20
- if (str.length >= n) return str.slice(0, n);
21
- const d = n - str.length;
22
- if (align === "right") return " ".repeat(d) + str;
23
- return str + " ".repeat(d);
24
- }
25
-
26
- function shortPeerLabel(p) {
27
- const raw = String(p?.name || p?.id || p?.url || String(p || "")).trim();
28
- if (!raw) return "peer";
29
- // 优先用 name;若是 url 则取 host:port 的尾段
30
- if (raw.startsWith("http://") || raw.startsWith("https://") || raw.startsWith("relay://")) {
31
- try { const u = new URL(raw); const host = u.hostname; const port = u.port ? `:${u.port}` : ""; if (host) return `${host}${port}`; } catch {}
32
- return raw.slice(-12);
33
- }
34
- // name 可能是 url 字符串(如 "http://141.98..." 存于 name 字段)
35
- if (raw.includes("://")) {
36
- try { const u = new URL(raw); return u.hostname + (u.port ? `:${u.port}` : ""); } catch {}
37
- }
38
- return raw;
39
- }
40
- export function formatViaReport(results, { peers = [], meta = {}, json = false } = {}) {
41
- const peerIds = (peers || []).map((p) => shortPeerLabel(p));
42
- const samples = meta.samples ?? 1;
43
- const timeout = meta.timeout ?? 30000;
44
- const includeOpencode = Boolean(meta.includeOpencode);
45
- const opencodeTag = includeOpencode ? "opencode=included" : "opencode=skipped";
46
- const at = meta.at || new Date().toISOString();
47
- // build json shape
48
- const jsonObj = {
49
- meta: { at, samples, timeout, includeOpencode, peers: peerIds, opencodeSkipped: !includeOpencode, ...meta, peers: peerIds },
50
- results: (results || []).map((r) => ({
51
- provider: r.provider,
52
- model: r.model || r.id,
53
- direct: r.direct ? { ttfb: r.direct.ttfbMs, total: r.direct.totalMs, ok: r.direct.ok, label: r.direct.label, error: r.direct.error } : null,
54
- via: Object.fromEntries(Object.entries(r.via || {}).map(([k, v]) => [k, v.ok ? { ttfb: v.ttfbMs, total: v.totalMs } : { error: v.label || v.error || "offline", label: v.label }])),
55
- best: r.best,
56
- deltaMs: r.deltaMs,
57
- opencodeSkipped: r.opencodeSkipped,
58
- })),
59
- advice: (() => {
60
- const viaBest = (results || []).find((r) => r.best?.startsWith("via:"));
61
- if (viaBest) return `${viaBest.provider}/${viaBest.model} 经 ${viaBest.best.slice(4)} 最快`;
62
- if ((results || []).length) return `${results[0].provider} 走 direct 即可`;
63
- return "无数据";
64
- })(),
65
- };
66
- if (json) {
67
- return { text: JSON.stringify(jsonObj, null, 2), json: jsonObj };
68
- }
69
- const lines = [];
70
- lines.push(`bench-via: direct vs via peers (samples=${samples}, timeout=${timeout / 1000}s, ${opencodeTag}) peers=${peerIds.join(",") || "(none)"}`);
71
- lines.push("");
72
- // 表头:直连 + 每个组员一列(短化为 host:port 或 name),兼容旧测试 via B 断言与用户“直连/组员B”心智
73
- const colW = 18;
74
- const header = `${pad("Provider", 12)} ${pad("Model", 24)} ${pad("direct", colW, "right")} ${peerIds.map((id) => pad(`via ${id}`, colW, "right")).join(" ")} ${pad("best", 14)}`;
75
- lines.push(header);
76
- lines.push("─".repeat(header.length));
77
- for (const r of results || []) {
78
- const provider = pad(r.provider || "", 12);
79
- const model = pad(r.model || r.id || "", 24);
80
- const directOk = r.direct?.ok;
81
- const directTxt = directOk ? `${r.direct.ttfbMs ?? "—"}ms` : (r.direct?.label || "—");
82
- // determine best ttfb for ★
83
- const all = [];
84
- if (r.direct?.ok) all.push({ key: "direct", ttfb: r.direct.ttfbMs ?? r.direct.totalMs });
85
- for (const pid of peerIds) {
86
- const v = r.via?.[pid];
87
- if (v?.ok) all.push({ key: `via:${pid}`, ttfb: v.ttfbMs ?? v.totalMs });
88
- }
89
- let bestKey = r.best;
90
- if (!bestKey && all.length) bestKey = all.sort((a, b) => a.ttfb - b.ttfb)[0].key;
91
- const isDirectBest = bestKey === "direct";
92
- const directCell = pad(`${directTxt}${isDirectBest ? "★" : ""}`, colW, "right");
93
- const viaCells = peerIds.map((pid) => {
94
- // via keys 可能存为完整 url(如 http://172...)而 peerIds 已短化,兼容两者
95
- let v = r.via?.[pid];
96
- if (!v) {
97
- // 尝试完整 url 变体
98
- const full = Object.keys(r.via || {}).find((k) => {
99
- try { const u = new URL(k); return (u.hostname + (u.port ? `:${u.port}` : "")) === pid; } catch { return false; }
100
- });
101
- if (full) v = r.via[full];
102
- }
103
- if (!v) return pad("—", colW, "right");
104
- if (v.ok) {
105
- const txt = `${v.ttfbMs ?? v.totalMs ?? "—"}ms`;
106
- const star = bestKey === `via:${pid}` ? "★" : "";
107
- return pad(`${txt}${star}`, colW, "right");
108
- }
109
- // 失败也展示延迟(先测延迟):如 42ms 鉴权失败,说明网络可达但该组员未配此供应商
110
- const ms = v.ttfbMs != null ? `${v.ttfbMs}ms ` : "";
111
- const label = v.label || v.error || "offline";
112
- const short = label.includes("离线") ? "offline" : label.slice(0, 8);
113
- return pad(`${ms}— ${short}`, colW, "right");
114
- }).join(" ");
115
- let bestTxt = r.best || "direct";
116
- if (r.deltaMs != null && r.best?.startsWith("via:")) {
117
- const pct = r.direct?.ttfbMs ? Math.round((r.deltaMs / r.direct.ttfbMs) * 100) : 0;
118
- bestTxt = `${r.best} ${pct}%`;
119
- }
120
- lines.push(`${provider} ${model} ${directCell} ${viaCells} ${pad(bestTxt, 14)}`);
121
- }
122
- lines.push("─".repeat(header.length));
123
- const viaBestExample = (results || []).find((r) => r.best?.startsWith("via:"));
124
- if (viaBestExample) lines.push(`建议:A 经 ${viaBestExample.best.slice(4)} 打 ${viaBestExample.provider} 最快;其余走 direct。`);
125
- else if ((results || []).length) lines.push(`建议:${results[0].provider} 走 direct 即可。`);
126
- else lines.push("建议:无数据");
127
- lines.push(`提示:via 已跳过 opencode(省额度),需对比 opencode 请加 --include-opencode`);
128
- lines.push(`* via 单样本,仅作参考,多次 --samples 2 取均值更稳`);
129
- return { text: lines.join("\n"), json: jsonObj };
130
- }
131
-
132
- export function formatReport(results, { json = false } = {}) {
133
- const sorted = sortResults(results);
134
- const winner = sorted.find((r) => r.ok) || null;
135
- if (json) {
136
- return { text: JSON.stringify(sorted, null, 2), json: sorted, winner, sorted };
137
- }
138
- const lines = [];
139
- lines.push("模型 状态 TTFB 总耗时 速度 tokens 备注");
140
- lines.push("─".repeat(84));
141
- for (const r of sorted) {
142
- const isWin = winner && r.id === winner.id;
143
- const mark = isWin ? "*" : " ";
144
- const id = pad((isWin ? "* " : " ") + r.id, 28);
145
- const label = pad(r.label || (r.ok ? "成功" : "失败"), 8);
146
- const ttfb = pad(r.ttfbMs != null ? `${r.ttfbMs}ms` : "—", 6, "right");
147
- const total = pad(r.totalMs != null ? `${r.totalMs}ms` : "—", 7, "right");
148
- let speed = "—";
149
- if (r.tps != null) speed = `${r.tps} t/s`;
150
- else if (r.charsPerSec != null) speed = `${r.charsPerSec} 字/秒`;
151
- speed = pad(speed, 11, "right");
152
- const tok = r.tokens?.completion != null ? String(r.tokens.completion) : r.chars != null ? String(r.chars) : "—";
153
- const note = r.ok ? "" : (r.error || "").slice(0, 50);
154
- lines.push(`${mark}${id} ${label} ${ttfb} ${total} ${speed} ${pad(tok, 6, "right")} ${note}`);
155
- }
156
- lines.push("─".repeat(84));
157
- if (winner) lines.push(`最快:${winner.id} TTFB ${winner.ttfbMs}ms 总 ${winner.totalMs}ms ${winner.tps != null ? `${winner.tps} t/s` : `${winner.charsPerSec ?? "—"} 字/秒`}`);
158
- else lines.push("无可用模型(均失败)");
159
- const failed = sorted.filter((r) => !r.ok).length;
160
- lines.push(`完成:${sorted.length} 个,成功 ${sorted.length - failed},失败 ${failed}`);
161
- return { text: lines.join("\n"), json: sorted, winner, sorted };
162
- }
1
+ export function sortResults(list) {
2
+ const arr = [...(list || [])];
3
+ arr.sort((a, b) => {
4
+ if (a.ok !== b.ok) return a.ok ? -1 : 1;
5
+ if (a.ok && b.ok) {
6
+ const at = a.ttfbMs ?? a.totalMs ?? 1e9;
7
+ const bt = b.ttfbMs ?? b.totalMs ?? 1e9;
8
+ if (at !== bt) return at - bt;
9
+ const ap = a.tps ?? a.charsPerSec ?? -1;
10
+ const bp = b.tps ?? b.charsPerSec ?? -1;
11
+ return bp - ap;
12
+ }
13
+ return 0;
14
+ });
15
+ return arr;
16
+ }
17
+
18
+ function pad(s, n, align = "left") {
19
+ const str = String(s ?? "");
20
+ if (str.length >= n) return str.slice(0, n);
21
+ const d = n - str.length;
22
+ if (align === "right") return " ".repeat(d) + str;
23
+ return str + " ".repeat(d);
24
+ }
25
+
26
+ function shortPeerLabel(p) {
27
+ const raw = String(p?.name || p?.id || p?.url || String(p || "")).trim();
28
+ if (!raw) return "peer";
29
+ // 优先用 name;若是 url 则取 host:port 的尾段
30
+ if (raw.startsWith("http://") || raw.startsWith("https://") || raw.startsWith("relay://")) {
31
+ try { const u = new URL(raw); const host = u.hostname; const port = u.port ? `:${u.port}` : ""; if (host) return `${host}${port}`; } catch {}
32
+ return raw.slice(-12);
33
+ }
34
+ // name 可能是 url 字符串(如 "http://141.98..." 存于 name 字段)
35
+ if (raw.includes("://")) {
36
+ try { const u = new URL(raw); return u.hostname + (u.port ? `:${u.port}` : ""); } catch {}
37
+ }
38
+ return raw;
39
+ }
40
+ export function formatViaReport(results, { peers = [], meta = {}, json = false } = {}) {
41
+ const peerIds = (peers || []).map((p) => shortPeerLabel(p));
42
+ const samples = meta.samples ?? 1;
43
+ const timeout = meta.timeout ?? 30000;
44
+ const includeOpencode = Boolean(meta.includeOpencode);
45
+ const opencodeTag = includeOpencode ? "opencode=included" : "opencode=skipped";
46
+ const at = meta.at || new Date().toISOString();
47
+ // build json shape
48
+ const jsonObj = {
49
+ meta: { at, samples, timeout, includeOpencode, peers: peerIds, opencodeSkipped: !includeOpencode, ...meta, peers: peerIds },
50
+ results: (results || []).map((r) => ({
51
+ provider: r.provider,
52
+ model: r.model || r.id,
53
+ direct: r.direct ? { ttfb: r.direct.ttfbMs, total: r.direct.totalMs, ok: r.direct.ok, label: r.direct.label, error: r.direct.error } : null,
54
+ via: Object.fromEntries(Object.entries(r.via || {}).map(([k, v]) => [k, v.ok ? { ttfb: v.ttfbMs, total: v.totalMs } : { error: v.label || v.error || "offline", label: v.label }])),
55
+ best: r.best,
56
+ deltaMs: r.deltaMs,
57
+ opencodeSkipped: r.opencodeSkipped,
58
+ })),
59
+ advice: (() => {
60
+ const viaBest = (results || []).find((r) => r.best?.startsWith("via:"));
61
+ if (viaBest) return `${viaBest.provider}/${viaBest.model} 经 ${viaBest.best.slice(4)} 最快`;
62
+ if ((results || []).length) return `${results[0].provider} 走 direct 即可`;
63
+ return "无数据";
64
+ })(),
65
+ };
66
+ if (json) {
67
+ return { text: JSON.stringify(jsonObj, null, 2), json: jsonObj };
68
+ }
69
+ const lines = [];
70
+ lines.push(`bench-via: direct vs via peers (samples=${samples}, timeout=${timeout / 1000}s, ${opencodeTag}) peers=${peerIds.join(",") || "(none)"}`);
71
+ lines.push("");
72
+ // 表头:直连 + 每个组员一列(短化为 host:port 或 name),兼容旧测试 via B 断言与用户“直连/组员B”心智
73
+ const colW = 18;
74
+ const header = `${pad("Provider", 12)} ${pad("Model", 24)} ${pad("direct", colW, "right")} ${peerIds.map((id) => pad(`via ${id}`, colW, "right")).join(" ")} ${pad("best", 14)}`;
75
+ lines.push(header);
76
+ lines.push("─".repeat(header.length));
77
+ for (const r of results || []) {
78
+ const provider = pad(r.provider || "", 12);
79
+ const model = pad(r.model || r.id || "", 24);
80
+ const directOk = r.direct?.ok;
81
+ const directTxt = directOk ? `${r.direct.ttfbMs ?? "—"}ms` : (r.direct?.label || "—");
82
+ // determine best ttfb for ★
83
+ const all = [];
84
+ if (r.direct?.ok) all.push({ key: "direct", ttfb: r.direct.ttfbMs ?? r.direct.totalMs });
85
+ for (const pid of peerIds) {
86
+ const v = r.via?.[pid];
87
+ if (v?.ok) all.push({ key: `via:${pid}`, ttfb: v.ttfbMs ?? v.totalMs });
88
+ }
89
+ let bestKey = r.best;
90
+ if (!bestKey && all.length) bestKey = all.sort((a, b) => a.ttfb - b.ttfb)[0].key;
91
+ const isDirectBest = bestKey === "direct";
92
+ const directCell = pad(`${directTxt}${isDirectBest ? "★" : ""}`, colW, "right");
93
+ const viaCells = peerIds.map((pid) => {
94
+ // via keys 可能存为完整 url(如 http://172...)而 peerIds 已短化,兼容两者
95
+ let v = r.via?.[pid];
96
+ if (!v) {
97
+ // 尝试完整 url 变体
98
+ const full = Object.keys(r.via || {}).find((k) => {
99
+ try { const u = new URL(k); return (u.hostname + (u.port ? `:${u.port}` : "")) === pid; } catch { return false; }
100
+ });
101
+ if (full) v = r.via[full];
102
+ }
103
+ if (!v) return pad("—", colW, "right");
104
+ if (v.ok) {
105
+ const txt = `${v.ttfbMs ?? v.totalMs ?? "—"}ms`;
106
+ const star = bestKey === `via:${pid}` ? "★" : "";
107
+ return pad(`${txt}${star}`, colW, "right");
108
+ }
109
+ // 失败也展示延迟(先测延迟):如 42ms 鉴权失败,说明网络可达但该组员未配此供应商
110
+ const ms = v.ttfbMs != null ? `${v.ttfbMs}ms ` : "";
111
+ const label = v.label || v.error || "offline";
112
+ const short = label.includes("离线") ? "offline" : label.slice(0, 8);
113
+ return pad(`${ms}— ${short}`, colW, "right");
114
+ }).join(" ");
115
+ let bestTxt = r.best || "direct";
116
+ if (r.deltaMs != null && r.best?.startsWith("via:")) {
117
+ const pct = r.direct?.ttfbMs ? Math.round((r.deltaMs / r.direct.ttfbMs) * 100) : 0;
118
+ bestTxt = `${r.best} ${pct}%`;
119
+ }
120
+ lines.push(`${provider} ${model} ${directCell} ${viaCells} ${pad(bestTxt, 14)}`);
121
+ }
122
+ lines.push("─".repeat(header.length));
123
+ const viaBestExample = (results || []).find((r) => r.best?.startsWith("via:"));
124
+ if (viaBestExample) lines.push(`建议:A 经 ${viaBestExample.best.slice(4)} 打 ${viaBestExample.provider} 最快;其余走 direct。`);
125
+ else if ((results || []).length) lines.push(`建议:${results[0].provider} 走 direct 即可。`);
126
+ else lines.push("建议:无数据");
127
+ lines.push(`提示:via 已跳过 opencode(省额度),需对比 opencode 请加 --include-opencode`);
128
+ lines.push(`* via 单样本,仅作参考,多次 --samples 2 取均值更稳`);
129
+ return { text: lines.join("\n"), json: jsonObj };
130
+ }
131
+
132
+ export function formatReport(results, { json = false } = {}) {
133
+ const sorted = sortResults(results);
134
+ const winner = sorted.find((r) => r.ok) || null;
135
+ if (json) {
136
+ return { text: JSON.stringify(sorted, null, 2), json: sorted, winner, sorted };
137
+ }
138
+ const lines = [];
139
+ lines.push("模型 状态 TTFB 总耗时 速度 tokens 备注");
140
+ lines.push("─".repeat(84));
141
+ for (const r of sorted) {
142
+ const isWin = winner && r.id === winner.id;
143
+ const mark = isWin ? "*" : " ";
144
+ const id = pad((isWin ? "* " : " ") + r.id, 28);
145
+ const label = pad(r.label || (r.ok ? "成功" : "失败"), 8);
146
+ const ttfb = pad(r.ttfbMs != null ? `${r.ttfbMs}ms` : "—", 6, "right");
147
+ const total = pad(r.totalMs != null ? `${r.totalMs}ms` : "—", 7, "right");
148
+ let speed = "—";
149
+ if (r.tps != null) speed = `${r.tps} t/s`;
150
+ else if (r.charsPerSec != null) speed = `${r.charsPerSec} 字/秒`;
151
+ speed = pad(speed, 11, "right");
152
+ const tok = r.tokens?.completion != null ? String(r.tokens.completion) : r.chars != null ? String(r.chars) : "—";
153
+ const note = r.ok ? "" : (r.error || "").slice(0, 50);
154
+ lines.push(`${mark}${id} ${label} ${ttfb} ${total} ${speed} ${pad(tok, 6, "right")} ${note}`);
155
+ }
156
+ lines.push("─".repeat(84));
157
+ if (winner) lines.push(`最快:${winner.id} TTFB ${winner.ttfbMs}ms 总 ${winner.totalMs}ms ${winner.tps != null ? `${winner.tps} t/s` : `${winner.charsPerSec ?? "—"} 字/秒`}`);
158
+ else lines.push("无可用模型(均失败)");
159
+ const failed = sorted.filter((r) => !r.ok).length;
160
+ lines.push(`完成:${sorted.length} 个,成功 ${sorted.length - failed},失败 ${failed}`);
161
+ return { text: lines.join("\n"), json: sorted, winner, sorted };
162
+ }
@@ -1,78 +1,78 @@
1
- import { joinUrl } from "../providers/base.js";
2
- import { computeMetrics, extractUsageFromJson } from "../metrics.js";
3
- import { createTransport } from "../transport/index.js";
1
+ import { joinUrl } from "../providers/base.js";
2
+ import { computeMetrics, extractUsageFromJson } from "../metrics.js";
3
+ import { createTransport } from "../transport/index.js";
4
4
  import { compatFetch } from "../compat.js";
5
-
6
- function extractInnerMessage(bodyText) {
7
- const t = String(bodyText || "");
8
- try {
9
- const j = JSON.parse(t);
10
- const m = j?.error?.message || j?.error || j?.message || j?.data?.error || "";
11
- if (typeof m === "string" && m.trim()) return m.trim().slice(0, 300);
12
- if (typeof j?.error === "string") return j.error.slice(0, 300);
13
- } catch {}
14
- return t.slice(0, 300);
15
- }
16
-
17
- function classifyError(status, bodyText) {
18
- const t = String(bodyText || "").slice(0, 500);
19
- const low = t.toLowerCase();
20
- if (status === 401) return { label: "鉴权失败", retryable: false };
21
- if (status === 402 || /insufficient balance/i.test(t)) return { label: "余额不足", retryable: false };
22
- if (low.includes("only available via cline")) return { label: "仅 Cline 客户端可用", retryable: false };
23
- if (low.includes("invalid model format")) return { label: "模型格式错误", retryable: false };
24
- if (status === 403) return { label: /insufficient/i.test(t) ? "余额不足" : "鉴权失败", retryable: false };
25
- if (status === 429) return { label: "限流", retryable: true };
26
- if (status >= 500) return { label: `上游错误 ${status}`, retryable: true };
27
- if (status === 404) return { label: "模型不存在", retryable: false };
28
- return { label: `HTTP ${status}`, retryable: false };
29
- }
30
-
31
- export async function runOne({
32
- baseUrl,
33
- chatPath = "/v1/chat/completions",
34
- model,
35
- providerId,
36
- apiKey,
37
- headers = {},
38
- prompt = "hi",
39
- maxTokens = 32,
40
- timeoutMs = 30000,
41
- fetchImpl = compatFetch,
42
- } = {}) {
43
- if (!baseUrl) return { id: model, ok: false, error: "missing baseUrl", label: "配置错误", ttfbMs: null, totalMs: 0, tps: null, charsPerSec: null, tokens: null };
44
- if (!model) return { id: model, ok: false, error: "missing model", label: "配置错误", ttfbMs: null, totalMs: 0, tps: null, charsPerSec: null, tokens: null };
45
- if (!apiKey) return { id: model, ok: false, error: "missing apiKey", label: "未配置 Key", ttfbMs: null, totalMs: 0, tps: null, charsPerSec: null, tokens: null };
46
- const url = joinUrl(String(baseUrl).replace(/\/+$/, ""), chatPath);
47
- let rawModel = String(model || "").trim();
48
- if (providerId && rawModel.startsWith(`${providerId}/`)) rawModel = rawModel.slice(providerId.length + 1);
49
- const body = { model: rawModel, stream: false, messages: [{ role: "user", content: prompt }], max_tokens: maxTokens };
50
- const finalHeaders = { "Content-Type": "application/json", Accept: "application/json", ...headers };
51
- if (apiKey && !finalHeaders.Authorization) finalHeaders.Authorization = `Bearer ${apiKey}`;
52
- const tr = createTransport({ fetchImpl, keepAlive: false, retry: {}, timeoutMs });
53
- try {
54
- const res = await tr.request({ url, method: "POST", headers: finalHeaders, body, stream: false });
55
- const ttfbMs = res.ttfbMs;
56
- const totalMs = res.totalMs;
57
- if (!res.ok) {
58
- let txt = ""; try { txt = await res.text(); } catch {}
59
- const cls = classifyError(res.status, txt);
60
- const msg = extractInnerMessage(txt) || `HTTP ${res.status}`;
61
- return { id: model, providerId, ok: false, status: res.status, error: msg, label: cls.label, ttfbMs, totalMs, tps: null, charsPerSec: null, tokens: null };
62
- }
63
- let json = {}; let txt = "";
64
- try { txt = await res.text(); json = JSON.parse(txt); } catch { json = {}; }
65
- const usage = extractUsageFromJson(json);
66
- const content = json?.choices?.[0]?.message?.content || json?.choices?.[0]?.text || txt || "";
67
- const chars = typeof content === "string" ? content.length : 0;
68
- const promptTokens = usage?.prompt_tokens ?? null;
69
- const completionTokens = usage?.completion_tokens ?? null;
70
- const { tps, charsPerSec } = computeMetrics({ ttfbMs, totalMs, promptTokens, completionTokens, chars });
71
- const totalTokens = usage?.total_tokens ?? (promptTokens !== null && completionTokens !== null ? promptTokens + completionTokens : null);
72
- return { id: model, providerId, ok: true, status: res.status, ttfbMs, totalMs, tps, charsPerSec, tokens: { prompt: promptTokens, completion: completionTokens, total: totalTokens }, chars, label: "成功", raw: json };
73
- } catch (e) {
74
- const msg = e?.message || String(e);
75
- const isTimeout = /timeout|abort/i.test(msg);
76
- return { id: model, providerId, ok: false, error: msg.slice(0, 300), label: isTimeout ? "超时" : "网络错误", ttfbMs: null, totalMs: null, tps: null, charsPerSec: null, tokens: null };
77
- }
78
- }
5
+
6
+ function extractInnerMessage(bodyText) {
7
+ const t = String(bodyText || "");
8
+ try {
9
+ const j = JSON.parse(t);
10
+ const m = j?.error?.message || j?.error || j?.message || j?.data?.error || "";
11
+ if (typeof m === "string" && m.trim()) return m.trim().slice(0, 300);
12
+ if (typeof j?.error === "string") return j.error.slice(0, 300);
13
+ } catch {}
14
+ return t.slice(0, 300);
15
+ }
16
+
17
+ function classifyError(status, bodyText) {
18
+ const t = String(bodyText || "").slice(0, 500);
19
+ const low = t.toLowerCase();
20
+ if (status === 401) return { label: "鉴权失败", retryable: false };
21
+ if (status === 402 || /insufficient balance/i.test(t)) return { label: "余额不足", retryable: false };
22
+ if (low.includes("only available via cline")) return { label: "仅 Cline 客户端可用", retryable: false };
23
+ if (low.includes("invalid model format")) return { label: "模型格式错误", retryable: false };
24
+ if (status === 403) return { label: /insufficient/i.test(t) ? "余额不足" : "鉴权失败", retryable: false };
25
+ if (status === 429) return { label: "限流", retryable: true };
26
+ if (status >= 500) return { label: `上游错误 ${status}`, retryable: true };
27
+ if (status === 404) return { label: "模型不存在", retryable: false };
28
+ return { label: `HTTP ${status}`, retryable: false };
29
+ }
30
+
31
+ export async function runOne({
32
+ baseUrl,
33
+ chatPath = "/v1/chat/completions",
34
+ model,
35
+ providerId,
36
+ apiKey,
37
+ headers = {},
38
+ prompt = "hi",
39
+ maxTokens = 32,
40
+ timeoutMs = 30000,
41
+ fetchImpl = compatFetch,
42
+ } = {}) {
43
+ if (!baseUrl) return { id: model, ok: false, error: "missing baseUrl", label: "配置错误", ttfbMs: null, totalMs: 0, tps: null, charsPerSec: null, tokens: null };
44
+ if (!model) return { id: model, ok: false, error: "missing model", label: "配置错误", ttfbMs: null, totalMs: 0, tps: null, charsPerSec: null, tokens: null };
45
+ if (!apiKey) return { id: model, ok: false, error: "missing apiKey", label: "未配置 Key", ttfbMs: null, totalMs: 0, tps: null, charsPerSec: null, tokens: null };
46
+ const url = joinUrl(String(baseUrl).replace(/\/+$/, ""), chatPath);
47
+ let rawModel = String(model || "").trim();
48
+ if (providerId && rawModel.startsWith(`${providerId}/`)) rawModel = rawModel.slice(providerId.length + 1);
49
+ const body = { model: rawModel, stream: false, messages: [{ role: "user", content: prompt }], max_tokens: maxTokens };
50
+ const finalHeaders = { "Content-Type": "application/json", Accept: "application/json", ...headers };
51
+ if (apiKey && !finalHeaders.Authorization) finalHeaders.Authorization = `Bearer ${apiKey}`;
52
+ const tr = createTransport({ fetchImpl, keepAlive: false, retry: {}, timeoutMs });
53
+ try {
54
+ const res = await tr.request({ url, method: "POST", headers: finalHeaders, body, stream: false });
55
+ const ttfbMs = res.ttfbMs;
56
+ const totalMs = res.totalMs;
57
+ if (!res.ok) {
58
+ let txt = ""; try { txt = await res.text(); } catch {}
59
+ const cls = classifyError(res.status, txt);
60
+ const msg = extractInnerMessage(txt) || `HTTP ${res.status}`;
61
+ return { id: model, providerId, ok: false, status: res.status, error: msg, label: cls.label, ttfbMs, totalMs, tps: null, charsPerSec: null, tokens: null };
62
+ }
63
+ let json = {}; let txt = "";
64
+ try { txt = await res.text(); json = JSON.parse(txt); } catch { json = {}; }
65
+ const usage = extractUsageFromJson(json);
66
+ const content = json?.choices?.[0]?.message?.content || json?.choices?.[0]?.text || txt || "";
67
+ const chars = typeof content === "string" ? content.length : 0;
68
+ const promptTokens = usage?.prompt_tokens ?? null;
69
+ const completionTokens = usage?.completion_tokens ?? null;
70
+ const { tps, charsPerSec } = computeMetrics({ ttfbMs, totalMs, promptTokens, completionTokens, chars });
71
+ const totalTokens = usage?.total_tokens ?? (promptTokens !== null && completionTokens !== null ? promptTokens + completionTokens : null);
72
+ return { id: model, providerId, ok: true, status: res.status, ttfbMs, totalMs, tps, charsPerSec, tokens: { prompt: promptTokens, completion: completionTokens, total: totalTokens }, chars, label: "成功", raw: json };
73
+ } catch (e) {
74
+ const msg = e?.message || String(e);
75
+ const isTimeout = /timeout|abort/i.test(msg);
76
+ return { id: model, providerId, ok: false, error: msg.slice(0, 300), label: isTimeout ? "超时" : "网络错误", ttfbMs: null, totalMs: null, tps: null, charsPerSec: null, tokens: null };
77
+ }
78
+ }