mslxdff 0.1.188 → 0.1.197
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.
- package/bin/mslxdff.js +3 -3
- package/package.json +1 -1
- package/src/anthropic/count-tokens.js +10 -3
- package/src/auto.js +254 -254
- package/src/bench/cline-bench.js +42 -42
- package/src/bench/probe.js +70 -70
- package/src/bench/report.js +162 -162
- package/src/bench/runner.js +77 -77
- package/src/bench/via-probe.js +124 -124
- package/src/bench/via-routes.js +87 -87
- package/src/bench/workbuddy-bench.js +54 -54
- package/src/chat/engine.js +160 -160
- package/src/chat/gateway.js +163 -163
- package/src/chat/orchestrator.js +234 -234
- package/src/chat/prompt.js +70 -70
- package/src/chat/repl.js +88 -88
- package/src/chat/terminal.js +135 -135
- package/src/chat/tools.js +306 -306
- package/src/chat-pipeline/index.js +173 -173
- package/src/chat-pipeline/policy.js +84 -84
- package/src/chat-pipeline/terminal-verdict.js +15 -6
- package/src/cli/commands/group.js +249 -249
- package/src/cli/commands/model/picks.js +50 -50
- package/src/cli/commands/provider/bench-via.js +247 -247
- package/src/cli/commands/provider/bench.js +141 -141
- package/src/cli/commands/provider/index.js +136 -136
- package/src/cli/commands/provider/models.js +145 -145
- package/src/cli/commands/provider/qwenwork-login.js +119 -119
- package/src/cli/commands/provider/raccoon-login.js +37 -16
- package/src/cli/commands/sync.js +310 -310
- package/src/cli/status.js +279 -279
- package/src/daemon.js +232 -232
- package/src/logs.js +11 -1
- package/src/model-capabilities/enrich.js +86 -86
- package/src/model-capabilities/index.js +183 -183
- package/src/model-capabilities/parse.js +70 -70
- package/src/model-trace.js +8 -1
- package/src/models.js +225 -225
- package/src/providers/cline/auth.js +228 -228
- package/src/providers/cline/chat.js +307 -307
- package/src/providers/cline.js +2 -2
- package/src/providers/codearts/chat.js +182 -182
- package/src/providers/globalqwenwork/cosy.js +123 -123
- package/src/providers/globalqwenwork/crypto.js +218 -218
- package/src/providers/globalqwenwork/http.js +20 -20
- package/src/providers/globalqwenwork/rsa.js +54 -54
- package/src/providers/globalqwenwork/sse.js +268 -268
- package/src/providers/globalqwenwork/stream.js +130 -130
- package/src/providers/globalqwenwork/upstream.js +122 -122
- package/src/providers/keyring.js +60 -60
- package/src/providers/qoder/chat.js +203 -203
- package/src/providers/qoder/index.js +249 -249
- package/src/providers/qoder/sse.js +145 -145
- package/src/providers/qwenwork/account-store.js +133 -133
- package/src/providers/qwenwork/constants.js +67 -67
- package/src/providers/qwenwork/cosy.js +120 -120
- package/src/providers/qwenwork/crypto.js +218 -218
- package/src/providers/qwenwork/http.js +20 -20
- package/src/providers/qwenwork/index.js +337 -337
- package/src/providers/qwenwork/payload.js +144 -144
- package/src/providers/qwenwork/rsa.js +54 -54
- package/src/providers/qwenwork/sse.js +268 -268
- package/src/providers/qwenwork/stream.js +130 -130
- package/src/providers/qwenwork/upstream.js +120 -120
- package/src/providers/qwenwork.js +1 -1
- package/src/providers/raccoon/chat.js +28 -12
- package/src/providers/raccoon/const.js +35 -6
- package/src/providers/raccoon/credits.js +26 -12
- package/src/providers/raccoon/envelope.js +10 -2
- package/src/providers/raccoon/login.js +114 -30
- package/src/providers/registry.js +79 -79
- package/src/providers/workbuddy/auth.js +26 -1
- package/src/providers/workbuddy/chat.js +264 -248
- package/src/providers/workbuddy/index.js +1 -1
- package/src/providers/workbuddy/reshape.js +152 -152
- package/src/providers/workbuddy/rotation-log.js +30 -2
- package/src/providers/workbuddy.js +2 -2
- package/src/reasoning.js +32 -32
- package/src/responses/translate.js +5 -1
- package/src/routes/chat/gateway.js +46 -46
- package/src/routes/chat/relay-pipeline.js +282 -282
- package/src/routes/chat/via-route-handler.js +146 -146
- package/src/routes/hedge.js +260 -253
- package/src/routes/models-route.js +167 -167
- package/src/routes/peers.js +335 -335
- package/src/routes/stream-hold.js +66 -15
- package/src/routes/stream-scan.js +24 -4
- package/src/routes/stream.js +347 -343
- package/src/runtime/bootstrap.js +67 -67
- package/src/runtime/provider-gate.js +36 -36
- package/src/runtime/providers-setup.js +168 -168
- package/src/server.js +78 -78
- package/src/state/schemas/allowlist.js +92 -92
- package/src/sync-opencode.js +280 -280
- package/src/transport/index.js +244 -244
- package/src/transport/pool.js +56 -56
- package/src/transport/retry.js +24 -24
- package/src/transport/sse.js +93 -93
- package/src/upstream-engine/sdk/sse.js +15 -1
- package/src/upstream-probe/display.js +52 -52
- package/src/upstream-probe/probe.js +49 -49
- package/src/upstream-probe/rotate.js +110 -110
- package/src/upstream-probe/start.js +45 -45
- package/src/upstream.js +293 -293
- package/docs/cli_help_mini.md +0 -145
- package/src/providers/AGENTS.md +0 -49
- package/src/routes/AGENTS.md +0 -37
package/src/transport/sse.js
CHANGED
|
@@ -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
|
+
}
|
|
@@ -38,6 +38,8 @@ export function createSseSerializer(captured = null) {
|
|
|
38
38
|
let nextIndex = 0;
|
|
39
39
|
const toolIndex = new Map();
|
|
40
40
|
const toolDeltaIds = new Set();
|
|
41
|
+
// 「当前正在流式输入的工具」的 index:无 id 的 tool-input-delta 靠它归位(见该 case 的注释)
|
|
42
|
+
let lastToolIdx = null;
|
|
41
43
|
// 加密思考往返:reasoning-start 的 providerMetadata 带 itemId + 加密态,随首帧透出
|
|
42
44
|
let reasoningMeta = null;
|
|
43
45
|
let reasoningFrameSent = false;
|
|
@@ -104,18 +106,30 @@ export function createSseSerializer(captured = null) {
|
|
|
104
106
|
const idx = nextIndex++;
|
|
105
107
|
toolIndex.set(part.id, idx);
|
|
106
108
|
toolDeltaIds.add(part.id);
|
|
109
|
+
lastToolIdx = idx; // 后续无 id 的 delta 归到这个工具
|
|
107
110
|
return ensureRole() + frame({
|
|
108
111
|
tool_calls: [{ index: idx, id: part.id, type: "function", function: { name: part.toolName || "", arguments: "" } }],
|
|
109
112
|
});
|
|
110
113
|
}
|
|
111
114
|
case "tool-input-delta": {
|
|
112
|
-
|
|
115
|
+
// 未知 id **绝不能回退到 index 0**:那会把这段参数写进另一个工具(实测:callA 占 index 0 时,
|
|
116
|
+
// 一个 id 丢失的 delta 让 funcA 的参数变成 `{"hijack":true}`),客户端会拿着被污染的参数去调用
|
|
117
|
+
// 错误的函数。改为按 `tool-call` 分支的口径分配新 index —— 宁可多一个空工具槽,也不污染已有工具。
|
|
118
|
+
// 无 id 的 delta 属于「当前正在流式输入的那个工具」:复用上一次用过的 index。
|
|
119
|
+
// 每个都分配新 index 会把一段参数拆成多个工具(实测:无 id 的 `{"a":` 与 `1}` 分别落到
|
|
120
|
+
// index 1 和 2,两个都是非法 JSON)—— 比旧代码的「都落 index 0」更糟。
|
|
121
|
+
let idx;
|
|
122
|
+
if (part.id && toolIndex.has(part.id)) idx = toolIndex.get(part.id);
|
|
123
|
+
else if (!part.id && lastToolIdx != null) idx = lastToolIdx; // 无 id:沿用当前工具
|
|
124
|
+
else { idx = nextIndex++; if (part.id) toolIndex.set(part.id, idx); }
|
|
125
|
+
lastToolIdx = idx;
|
|
113
126
|
return frame({ tool_calls: [{ index: idx, function: { arguments: String(part.delta ?? "") } }] });
|
|
114
127
|
}
|
|
115
128
|
case "tool-call": {
|
|
116
129
|
const id = part.toolCallId;
|
|
117
130
|
const idx = toolIndex.has(id) ? toolIndex.get(id) : nextIndex++;
|
|
118
131
|
toolIndex.set(id, idx);
|
|
132
|
+
lastToolIdx = idx; // 同 tool-input-start:后续无 id 的 delta 归到这个工具
|
|
119
133
|
if (toolDeltaIds.has(id)) return null;
|
|
120
134
|
const args = typeof part.input === "string" ? part.input : JSON.stringify(part.input ?? {});
|
|
121
135
|
return ensureRole() + frame({ tool_calls: [{ index: idx, id, type: "function", function: { name: part.toolName || "", arguments: args } }] });
|
|
@@ -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
|
+
}
|