mslxdff 0.1.156 → 0.1.158

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 (144) hide show
  1. package/bin/mslxdff.js +4 -4
  2. package/docs/ARCHITECTURE.md +426 -0
  3. package/docs/FEATURE_TREE.md +164 -0
  4. package/docs/MOBILE.md +82 -0
  5. package/docs/adr/0001-reasoning-content-injection.md +14 -0
  6. package/docs/adr/0002-models-free-filter.md +12 -0
  7. package/docs/adr/0003-zero-state-no-auth.md +10 -0
  8. package/docs/adr/0004-bearer-token.md +18 -0
  9. package/docs/adr/0005-peer-mesh.md +53 -0
  10. package/docs/adr/0006-broadband-member.md +103 -0
  11. package/docs/adr/0007-multi-provider-prefix.md +25 -0
  12. package/docs/adr/0008-share-keys-to-peers.md +52 -0
  13. package/docs/adr/0009-chat-repl.md +37 -0
  14. package/docs/adr/0010-allowlist.md +36 -0
  15. package/docs/adr/0011-broadband-stream.md +30 -0
  16. package/docs/adr/0012-responses-endpoint-codex-sync.md +48 -0
  17. package/docs/adr/0013-node16-compat.md +41 -0
  18. package/docs/adr/0014-deepseek-provider.md +52 -0
  19. package/docs/adr/0015-upstream-probe-routing.md +50 -0
  20. package/docs/adr/0016-model-capabilities.md +27 -0
  21. package/docs/adr/0017-ai-sdk-upstream-engine.md +59 -0
  22. package/docs/adr/0018-zen-client-identity.md +52 -0
  23. package/docs/adr/0019-share-keys-always-lend.md +59 -0
  24. package/docs/adr/0020-zen-free-lane-agent-shape.md +52 -0
  25. package/docs/adr/0021-usage-report-jsonl.md +47 -0
  26. package/docs/adr/0022-models-capability-merge.md +61 -0
  27. package/docs/adr/0023-key-provider-default-direct.md +58 -0
  28. package/docs/adr/0024-node18-baseline.md +63 -0
  29. package/docs/adr/0025-workbuddy-authdir-follows-state.md +71 -0
  30. package/docs/adr/0026-cline-provider-id-unify.md +67 -0
  31. package/docs/adr/0027-codearts-provider.md +48 -0
  32. package/docs/adr/0028-traework-provider.md +34 -0
  33. package/docs/adr/0029-qoder-native-provider.md +60 -0
  34. package/docs/adr/0030-models-list-scoped-by-picks.md +53 -0
  35. package/docs/adr/0031-qoder-true-streaming.md +50 -0
  36. package/docs/adr/0032-generic-responses-channel.md +72 -0
  37. package/docs/adr/0033-cline-allowlist-auto-sync.md +75 -0
  38. package/docs/adr/0034-request-level-human-readable-observability.md +60 -0
  39. package/docs/adr/0035-sdk-channel-headers-timeout.md +49 -0
  40. package/docs/adr/0036-qoder-per-request-sticky-account.md +82 -0
  41. package/docs/adr/0037-qwenwork-independent-provider.md +82 -0
  42. package/docs/adr/0038-zcode-provider.md +140 -0
  43. package/docs/agents/domain.md +51 -0
  44. package/docs/agents/issue-tracker.md +30 -0
  45. package/docs/agents/triage-labels.md +15 -0
  46. package/docs/cli_help.md +1391 -0
  47. package/docs/cli_help_mini.md +133 -0
  48. package/docs/plans/bench-via-latency-2026-09-01.md +215 -0
  49. package/docs/plugins.md +187 -0
  50. package/package.json +1 -1
  51. package/src/auto.js +254 -254
  52. package/src/bench/cline-bench.js +42 -42
  53. package/src/bench/probe.js +70 -70
  54. package/src/bench/report.js +162 -162
  55. package/src/bench/runner.js +77 -77
  56. package/src/bench/via-probe.js +124 -124
  57. package/src/bench/via-routes.js +87 -87
  58. package/src/bench/workbuddy-bench.js +54 -54
  59. package/src/chat/engine.js +160 -160
  60. package/src/chat/gateway.js +163 -163
  61. package/src/chat/orchestrator.js +234 -234
  62. package/src/chat/prompt.js +70 -70
  63. package/src/chat/repl.js +88 -88
  64. package/src/chat/terminal.js +135 -135
  65. package/src/chat/tools.js +306 -306
  66. package/src/chat-pipeline/index.js +123 -123
  67. package/src/chat-pipeline/policy.js +76 -76
  68. package/src/chat-pipeline/serial-trial.js +210 -210
  69. package/src/cli/commands/group.js +249 -249
  70. package/src/cli/commands/model/list-providers.js +1 -1
  71. package/src/cli/commands/model/picks.js +50 -50
  72. package/src/cli/commands/provider/bench-via.js +247 -247
  73. package/src/cli/commands/provider/bench.js +141 -141
  74. package/src/cli/commands/provider/index.js +124 -116
  75. package/src/cli/commands/provider/models.js +139 -128
  76. package/src/cli/commands/provider/qwenwork-login.js +119 -0
  77. package/src/cli/commands/provider/zcode-login.js +77 -0
  78. package/src/cli/commands/provider/zcode-quota.js +55 -0
  79. package/src/cli/commands/sync.js +232 -232
  80. package/src/cli/provider-row.js +2 -2
  81. package/src/cli/status.js +279 -279
  82. package/src/daemon.js +96 -96
  83. package/src/model-capabilities/enrich.js +86 -86
  84. package/src/model-capabilities/index.js +183 -183
  85. package/src/model-capabilities/parse.js +70 -70
  86. package/src/models.js +225 -225
  87. package/src/providers/classify.js +1 -1
  88. package/src/providers/cline/auth.js +228 -228
  89. package/src/providers/cline/chat.js +307 -307
  90. package/src/providers/cline.js +2 -2
  91. package/src/providers/keyring.js +60 -56
  92. package/src/providers/qoder/chat.js +183 -174
  93. package/src/providers/qoder/index.js +230 -185
  94. package/src/providers/qoder/sse.js +103 -62
  95. package/src/providers/qwenwork/account-store.js +133 -0
  96. package/src/providers/qwenwork/constants.js +67 -0
  97. package/src/providers/qwenwork/cosy.js +120 -0
  98. package/src/providers/qwenwork/crypto.js +218 -0
  99. package/src/providers/qwenwork/http.js +20 -0
  100. package/src/providers/qwenwork/index.js +327 -0
  101. package/src/providers/qwenwork/payload.js +142 -0
  102. package/src/providers/qwenwork/rsa.js +54 -0
  103. package/src/providers/qwenwork/sse.js +268 -0
  104. package/src/providers/qwenwork/stream.js +130 -0
  105. package/src/providers/qwenwork/upstream.js +120 -0
  106. package/src/providers/qwenwork.js +1 -0
  107. package/src/providers/registry.js +66 -56
  108. package/src/providers/share-keys.js +2 -2
  109. package/src/providers/workbuddy/chat.js +248 -248
  110. package/src/providers/workbuddy/reshape.js +152 -152
  111. package/src/providers/workbuddy.js +2 -2
  112. package/src/providers/zcode/account-store.js +129 -0
  113. package/src/providers/zcode/auth.js +28 -0
  114. package/src/providers/zcode/chat.js +171 -0
  115. package/src/providers/zcode/const.js +54 -0
  116. package/src/providers/zcode/headers.js +55 -0
  117. package/src/providers/zcode/index.js +124 -0
  118. package/src/providers/zcode/models.js +65 -0
  119. package/src/providers/zcode/oauth.js +120 -0
  120. package/src/providers/zcode/quota.js +176 -0
  121. package/src/providers/zcode/sse.js +179 -0
  122. package/src/reasoning.js +32 -32
  123. package/src/routes/chat/gateway.js +46 -46
  124. package/src/routes/chat/relay-pipeline.js +250 -250
  125. package/src/routes/chat/via-route-handler.js +144 -144
  126. package/src/routes/hedge.js +255 -255
  127. package/src/routes/models-route.js +167 -167
  128. package/src/routes/peers.js +273 -273
  129. package/src/routes/stream.js +438 -438
  130. package/src/runtime/bootstrap.js +45 -45
  131. package/src/runtime/provider-gate.js +33 -30
  132. package/src/runtime/providers-setup.js +165 -165
  133. package/src/server.js +64 -64
  134. package/src/state/schemas/allowlist.js +92 -92
  135. package/src/sync-opencode.js +280 -280
  136. package/src/transport/index.js +244 -244
  137. package/src/transport/pool.js +56 -56
  138. package/src/transport/retry.js +24 -24
  139. package/src/transport/sse.js +93 -93
  140. package/src/upstream-probe/display.js +52 -52
  141. package/src/upstream-probe/probe.js +49 -49
  142. package/src/upstream-probe/rotate.js +110 -110
  143. package/src/upstream-probe/start.js +45 -45
  144. package/src/upstream.js +289 -289
@@ -1,245 +1,245 @@
1
- import { performance } from "node:perf_hooks";
2
- import { resolveRetry, sleep, backoffDelay } from "./retry.js";
3
- import { createSseParser } from "./sse.js";
4
- import { createPool } from "./pool.js";
5
- import { compatFetch, getUndici } from "../compat.js";
6
-
1
+ import { performance } from "node:perf_hooks";
2
+ import { resolveRetry, sleep, backoffDelay } from "./retry.js";
3
+ import { createSseParser } from "./sse.js";
4
+ import { createPool } from "./pool.js";
5
+ import { compatFetch, getUndici } from "../compat.js";
6
+
7
7
  const UndiciFetch = getUndici().fetch;
8
-
9
- const DEFAULT_RETRY = {
10
- network: { attempts: 2, delayMs: 300 },
11
- 429: { attempts: 1, delayMs: 100 },
12
- 502: { attempts: 1, delayMs: 100 },
13
- 503: { attempts: 1, delayMs: 100 },
14
- 504: { attempts: 1, delayMs: 100 },
15
- };
16
-
17
- export function createTransport({
18
- baseUrl,
19
- headers: baseHeaders = {},
20
- keepAlive = true,
21
- fetchImpl,
22
- dispatcher: extDispatcher,
23
- timeoutMs: defaultTimeoutMs = 30_000,
24
- retry: defaultRetry = DEFAULT_RETRY,
25
- hooks,
26
- } = {}) {
27
- if (!fetchImpl) fetchImpl = UndiciFetch || compatFetch;
28
- const pool = keepAlive && !extDispatcher ? createPool({ keepAlive }) : null;
29
- const getDispatcher = () => extDispatcher || pool?.dispatcher || null;
30
-
31
- let closed = false;
32
-
33
- function resolveUrl(url) {
34
- if (!url) return baseUrl || "";
35
- if (/^https?:\/\//i.test(url)) return url;
36
- if (!baseUrl) return url;
37
- return `${String(baseUrl).replace(/\/+$/, "")}/${String(url).replace(/^\/+/, "")}`;
38
- }
39
-
40
- async function applyHooks(name, ctx) {
41
- if (!hooks) return null;
42
- try { return await hooks(name, ctx); } catch { return null; }
43
- }
44
-
45
- async function request({
46
- url,
47
- method = "POST",
48
- headers = {},
49
- body,
50
- stream,
51
- timeoutMs,
52
- retry,
53
- dispatcher,
54
- } = {}) {
55
- const finalUrl = resolveUrl(url);
56
- const retryCfg = retry ?? defaultRetry;
57
- const timeout = Number(timeoutMs ?? defaultTimeoutMs) || 30_000;
58
- const disp = dispatcher ?? getDispatcher();
59
- const t0 = performance.now();
60
- const attempts = [];
61
- let waitMs = 0;
62
-
63
- // 合并 headers
64
- const finalHeaders = { ...baseHeaders, ...headers };
65
- if (body != null && !finalHeaders["Content-Type"] && !finalHeaders["content-type"]) {
66
- finalHeaders["Content-Type"] = "application/json";
67
- }
68
- if (stream && !finalHeaders["Accept"] && !finalHeaders["accept"]) {
69
- finalHeaders["Accept"] = "text/event-stream";
70
- }
71
-
72
- const bodyStr = body != null && typeof body !== "string" ? JSON.stringify(body) : body;
73
-
74
- for (let attempt = 0; ; attempt++) {
75
- const tAttempt = performance.now();
76
- let res;
77
- let err = null;
78
- const controller = new AbortController();
79
- const timer = setTimeout(() => controller.abort(new Error(`upstream timed out after ${timeout}ms`)), timeout);
80
- try {
81
- let reqUrl = finalUrl;
82
- let reqHeaders = { ...finalHeaders };
83
- // hooks
84
- const hh = await applyHooks("upstream:headers", { url: reqUrl, body, headers: reqHeaders });
85
- if (hh?.changed && hh.value?.headers) reqHeaders = hh.value.headers;
86
- const br = await applyHooks("upstream:before-request", { url: reqUrl, method, body, headers: reqHeaders });
87
- if (br?.changed && br.value) {
88
- if (typeof br.value.url === "string" && br.value.url) reqUrl = br.value.url;
89
- if (br.value.headers && typeof br.value.headers === "object") reqHeaders = br.value.headers;
90
- }
91
- const opts = { method, headers: reqHeaders, body: bodyStr, signal: controller.signal };
92
- if (disp) opts.dispatcher = disp;
93
- res = await fetchImpl(reqUrl, opts);
94
- } catch (e) {
95
- err = e;
96
- } finally {
97
- clearTimeout(timer);
98
- }
99
- const ms = Math.round(performance.now() - tAttempt);
100
-
101
- if (err) {
102
- attempts.push({ type: "network", ms });
103
- const { shouldRetry, delayMs } = resolveRetry("network", attempt, retryCfg);
104
- if (shouldRetry) {
105
- await sleep(delayMs);
106
- waitMs += delayMs;
107
- continue;
108
- }
109
- err._t = { attempts, waitMs, totalMs: Math.round(performance.now() - t0) };
110
- throw err;
111
- }
112
-
113
- // http
114
- attempts.push({ type: `http${res.status}`, ms });
115
- const { shouldRetry, delayMs } = resolveRetry(res.status, attempt, retryCfg);
116
- if (shouldRetry) {
117
- // 消耗 body 以释放连接
118
- try { if (res.body) await res.text().catch(() => {}); } catch {}
119
- await sleep(delayMs);
120
- waitMs += delayMs;
121
- continue;
122
- }
123
-
124
- const ttfbMs = Math.round(performance.now() - t0);
125
- const isStreamRequested = stream === true;
126
- const contentType = res.headers.get("content-type") || "";
127
- // 若请求为 stream 但上游返回的是 JSON(测试桩常见),则回退为非流式处理,避免 SSE 空聚合
128
- const isStream = isStreamRequested && contentType.includes("text/event-stream");
129
- if (!isStream) {
130
- // 非流式:预读 body 以得 totalMs 与缓存(兼容 stream:true 但返回 JSON 的桩)
131
- let cachedText = "";
132
- let readMs = ttfbMs;
133
- try {
134
- cachedText = await res.text();
135
- readMs = Math.round(performance.now() - t0);
136
- } catch {
137
- cachedText = "";
138
- readMs = ttfbMs;
139
- }
140
- const totalMs = readMs;
141
- const headers = res.headers;
142
- return {
143
- status: res.status,
144
- headers,
145
- ok: res.ok,
146
- ttfbMs,
147
- totalMs,
148
- _t: { attempts, waitMs, totalMs },
149
- async json() { try { return JSON.parse(cachedText); } catch { return cachedText; } },
150
- async text() { return cachedText; },
151
- stream() { throw new Error("not-streaming: call with stream:true"); },
152
- get body() { return null; },
153
- };
154
- } else {
155
- // 流式:保留原始 res 用于 stream()
156
- const headers = res.headers;
157
- let firstTtfb = ttfbMs; // 更新为首事件到达时刻(测得即所得)
158
- let firstDone = false;
159
- let lastRead = ttfbMs;
160
- const markFirst = () => {
161
- if (!firstDone) { firstTtfb = Math.round(performance.now() - t0); firstDone = true; }
162
- };
163
- let _t = { attempts, waitMs, totalMs: ttfbMs };
164
- return {
165
- status: res.status,
166
- headers,
167
- ok: res.ok,
168
- get ttfbMs() { return firstDone ? firstTtfb : ttfbMs; },
169
- get totalMs() { return lastRead; },
170
- get _t() { return _t; },
171
- set _t(v) { _t = v; },
172
- async json() {
173
- let acc = "";
174
- for await (const chunk of this.stream()) acc += chunk;
175
- try { return JSON.parse(acc); } catch { return acc; }
176
- },
177
- async text() {
178
- let acc = "";
179
- for await (const chunk of this.stream()) acc += chunk;
180
- return acc;
181
- },
182
- get body() { return res.body; },
183
- async *stream() {
184
- const parser = createSseParser();
185
- if (res.body && typeof res.body.getReader === "function") {
186
- const reader = res.body.getReader();
187
- const decoder = new TextDecoder();
188
- while (true) {
189
- const { done, value } = await reader.read();
190
- if (done) break;
191
- lastRead = Math.round(performance.now() - t0);
192
- try { _t.totalMs = lastRead; } catch {}
193
- const text = decoder.decode(value, { stream: true });
194
- const evs = parser.push(text);
195
- for (const e of evs) {
196
- if (e === "[DONE]") return;
197
- if (e === "") continue;
198
- markFirst();
199
- yield e;
200
- }
201
- }
202
- } else if (typeof res.text === "function") {
203
- const txt = await res.text();
204
- const evs = parser.push(txt);
205
- for (const e of evs) {
206
- if (e === "[DONE]") return;
207
- if (e === "") continue;
208
- markFirst();
209
- yield e;
210
- }
211
- }
212
- },
213
- };
214
- }
215
- }
216
- }
217
-
218
- async function preheat(url) {
219
- const target = url || (baseUrl ? `${String(baseUrl).replace(/\/+$/, "")}/zen/v1/models` : null);
220
- if (!target) return { ok: false, skipped: true };
221
- const t0 = performance.now();
222
- try {
223
- const res = await request({ url: target, method: "GET", stream: false, timeoutMs: 3000 });
224
- try { if (res.text) await res.text().catch(() => {}); } catch {}
225
- return { ok: res.ok, status: res.status, ms: Math.round(performance.now() - t0) };
226
- } catch (e) {
227
- return { ok: false, error: String(e?.message || e), ms: Math.round(performance.now() - t0) };
228
- }
229
- }
230
-
231
- async function close() {
232
- if (closed) return;
233
- closed = true;
234
- if (pool) await pool.close();
235
- }
236
-
237
- return {
238
- request,
239
- preheat,
240
- close,
241
- get dispatcher() { return getDispatcher(); },
242
- get agent() { return pool?.agent || null; },
243
- [Symbol.asyncDispose]: close,
244
- };
245
- }
8
+
9
+ const DEFAULT_RETRY = {
10
+ network: { attempts: 2, delayMs: 300 },
11
+ 429: { attempts: 1, delayMs: 100 },
12
+ 502: { attempts: 1, delayMs: 100 },
13
+ 503: { attempts: 1, delayMs: 100 },
14
+ 504: { attempts: 1, delayMs: 100 },
15
+ };
16
+
17
+ export function createTransport({
18
+ baseUrl,
19
+ headers: baseHeaders = {},
20
+ keepAlive = true,
21
+ fetchImpl,
22
+ dispatcher: extDispatcher,
23
+ timeoutMs: defaultTimeoutMs = 30_000,
24
+ retry: defaultRetry = DEFAULT_RETRY,
25
+ hooks,
26
+ } = {}) {
27
+ if (!fetchImpl) fetchImpl = UndiciFetch || compatFetch;
28
+ const pool = keepAlive && !extDispatcher ? createPool({ keepAlive }) : null;
29
+ const getDispatcher = () => extDispatcher || pool?.dispatcher || null;
30
+
31
+ let closed = false;
32
+
33
+ function resolveUrl(url) {
34
+ if (!url) return baseUrl || "";
35
+ if (/^https?:\/\//i.test(url)) return url;
36
+ if (!baseUrl) return url;
37
+ return `${String(baseUrl).replace(/\/+$/, "")}/${String(url).replace(/^\/+/, "")}`;
38
+ }
39
+
40
+ async function applyHooks(name, ctx) {
41
+ if (!hooks) return null;
42
+ try { return await hooks(name, ctx); } catch { return null; }
43
+ }
44
+
45
+ async function request({
46
+ url,
47
+ method = "POST",
48
+ headers = {},
49
+ body,
50
+ stream,
51
+ timeoutMs,
52
+ retry,
53
+ dispatcher,
54
+ } = {}) {
55
+ const finalUrl = resolveUrl(url);
56
+ const retryCfg = retry ?? defaultRetry;
57
+ const timeout = Number(timeoutMs ?? defaultTimeoutMs) || 30_000;
58
+ const disp = dispatcher ?? getDispatcher();
59
+ const t0 = performance.now();
60
+ const attempts = [];
61
+ let waitMs = 0;
62
+
63
+ // 合并 headers
64
+ const finalHeaders = { ...baseHeaders, ...headers };
65
+ if (body != null && !finalHeaders["Content-Type"] && !finalHeaders["content-type"]) {
66
+ finalHeaders["Content-Type"] = "application/json";
67
+ }
68
+ if (stream && !finalHeaders["Accept"] && !finalHeaders["accept"]) {
69
+ finalHeaders["Accept"] = "text/event-stream";
70
+ }
71
+
72
+ const bodyStr = body != null && typeof body !== "string" ? JSON.stringify(body) : body;
73
+
74
+ for (let attempt = 0; ; attempt++) {
75
+ const tAttempt = performance.now();
76
+ let res;
77
+ let err = null;
78
+ const controller = new AbortController();
79
+ const timer = setTimeout(() => controller.abort(new Error(`upstream timed out after ${timeout}ms`)), timeout);
80
+ try {
81
+ let reqUrl = finalUrl;
82
+ let reqHeaders = { ...finalHeaders };
83
+ // hooks
84
+ const hh = await applyHooks("upstream:headers", { url: reqUrl, body, headers: reqHeaders });
85
+ if (hh?.changed && hh.value?.headers) reqHeaders = hh.value.headers;
86
+ const br = await applyHooks("upstream:before-request", { url: reqUrl, method, body, headers: reqHeaders });
87
+ if (br?.changed && br.value) {
88
+ if (typeof br.value.url === "string" && br.value.url) reqUrl = br.value.url;
89
+ if (br.value.headers && typeof br.value.headers === "object") reqHeaders = br.value.headers;
90
+ }
91
+ const opts = { method, headers: reqHeaders, body: bodyStr, signal: controller.signal };
92
+ if (disp) opts.dispatcher = disp;
93
+ res = await fetchImpl(reqUrl, opts);
94
+ } catch (e) {
95
+ err = e;
96
+ } finally {
97
+ clearTimeout(timer);
98
+ }
99
+ const ms = Math.round(performance.now() - tAttempt);
100
+
101
+ if (err) {
102
+ attempts.push({ type: "network", ms });
103
+ const { shouldRetry, delayMs } = resolveRetry("network", attempt, retryCfg);
104
+ if (shouldRetry) {
105
+ await sleep(delayMs);
106
+ waitMs += delayMs;
107
+ continue;
108
+ }
109
+ err._t = { attempts, waitMs, totalMs: Math.round(performance.now() - t0) };
110
+ throw err;
111
+ }
112
+
113
+ // http
114
+ attempts.push({ type: `http${res.status}`, ms });
115
+ const { shouldRetry, delayMs } = resolveRetry(res.status, attempt, retryCfg);
116
+ if (shouldRetry) {
117
+ // 消耗 body 以释放连接
118
+ try { if (res.body) await res.text().catch(() => {}); } catch {}
119
+ await sleep(delayMs);
120
+ waitMs += delayMs;
121
+ continue;
122
+ }
123
+
124
+ const ttfbMs = Math.round(performance.now() - t0);
125
+ const isStreamRequested = stream === true;
126
+ const contentType = res.headers.get("content-type") || "";
127
+ // 若请求为 stream 但上游返回的是 JSON(测试桩常见),则回退为非流式处理,避免 SSE 空聚合
128
+ const isStream = isStreamRequested && contentType.includes("text/event-stream");
129
+ if (!isStream) {
130
+ // 非流式:预读 body 以得 totalMs 与缓存(兼容 stream:true 但返回 JSON 的桩)
131
+ let cachedText = "";
132
+ let readMs = ttfbMs;
133
+ try {
134
+ cachedText = await res.text();
135
+ readMs = Math.round(performance.now() - t0);
136
+ } catch {
137
+ cachedText = "";
138
+ readMs = ttfbMs;
139
+ }
140
+ const totalMs = readMs;
141
+ const headers = res.headers;
142
+ return {
143
+ status: res.status,
144
+ headers,
145
+ ok: res.ok,
146
+ ttfbMs,
147
+ totalMs,
148
+ _t: { attempts, waitMs, totalMs },
149
+ async json() { try { return JSON.parse(cachedText); } catch { return cachedText; } },
150
+ async text() { return cachedText; },
151
+ stream() { throw new Error("not-streaming: call with stream:true"); },
152
+ get body() { return null; },
153
+ };
154
+ } else {
155
+ // 流式:保留原始 res 用于 stream()
156
+ const headers = res.headers;
157
+ let firstTtfb = ttfbMs; // 更新为首事件到达时刻(测得即所得)
158
+ let firstDone = false;
159
+ let lastRead = ttfbMs;
160
+ const markFirst = () => {
161
+ if (!firstDone) { firstTtfb = Math.round(performance.now() - t0); firstDone = true; }
162
+ };
163
+ let _t = { attempts, waitMs, totalMs: ttfbMs };
164
+ return {
165
+ status: res.status,
166
+ headers,
167
+ ok: res.ok,
168
+ get ttfbMs() { return firstDone ? firstTtfb : ttfbMs; },
169
+ get totalMs() { return lastRead; },
170
+ get _t() { return _t; },
171
+ set _t(v) { _t = v; },
172
+ async json() {
173
+ let acc = "";
174
+ for await (const chunk of this.stream()) acc += chunk;
175
+ try { return JSON.parse(acc); } catch { return acc; }
176
+ },
177
+ async text() {
178
+ let acc = "";
179
+ for await (const chunk of this.stream()) acc += chunk;
180
+ return acc;
181
+ },
182
+ get body() { return res.body; },
183
+ async *stream() {
184
+ const parser = createSseParser();
185
+ if (res.body && typeof res.body.getReader === "function") {
186
+ const reader = res.body.getReader();
187
+ const decoder = new TextDecoder();
188
+ while (true) {
189
+ const { done, value } = await reader.read();
190
+ if (done) break;
191
+ lastRead = Math.round(performance.now() - t0);
192
+ try { _t.totalMs = lastRead; } catch {}
193
+ const text = decoder.decode(value, { stream: true });
194
+ const evs = parser.push(text);
195
+ for (const e of evs) {
196
+ if (e === "[DONE]") return;
197
+ if (e === "") continue;
198
+ markFirst();
199
+ yield e;
200
+ }
201
+ }
202
+ } else if (typeof res.text === "function") {
203
+ const txt = await res.text();
204
+ const evs = parser.push(txt);
205
+ for (const e of evs) {
206
+ if (e === "[DONE]") return;
207
+ if (e === "") continue;
208
+ markFirst();
209
+ yield e;
210
+ }
211
+ }
212
+ },
213
+ };
214
+ }
215
+ }
216
+ }
217
+
218
+ async function preheat(url) {
219
+ const target = url || (baseUrl ? `${String(baseUrl).replace(/\/+$/, "")}/zen/v1/models` : null);
220
+ if (!target) return { ok: false, skipped: true };
221
+ const t0 = performance.now();
222
+ try {
223
+ const res = await request({ url: target, method: "GET", stream: false, timeoutMs: 3000 });
224
+ try { if (res.text) await res.text().catch(() => {}); } catch {}
225
+ return { ok: res.ok, status: res.status, ms: Math.round(performance.now() - t0) };
226
+ } catch (e) {
227
+ return { ok: false, error: String(e?.message || e), ms: Math.round(performance.now() - t0) };
228
+ }
229
+ }
230
+
231
+ async function close() {
232
+ if (closed) return;
233
+ closed = true;
234
+ if (pool) await pool.close();
235
+ }
236
+
237
+ return {
238
+ request,
239
+ preheat,
240
+ close,
241
+ get dispatcher() { return getDispatcher(); },
242
+ get agent() { return pool?.agent || null; },
243
+ [Symbol.asyncDispose]: close,
244
+ };
245
+ }
@@ -1,56 +1,56 @@
1
- import { getUndici } from "../compat.js";
2
-
3
- const UndiciAgent = getUndici().Agent;
4
-
5
- function envInt(name, fallback) {
6
- const v = Number(process.env[name]);
7
- return Number.isInteger(v) && v > 0 ? v : fallback;
8
- }
9
-
10
- export function createPool({
11
- keepAlive = true,
12
- keepAliveTimeout,
13
- keepAliveMaxTimeout,
14
- connections,
15
- } = {}) {
16
- const keepAliveTimeoutMs = keepAliveTimeout ?? envInt("MSLXDFF_UPSTREAM_KEEPALIVE_TIMEOUT", 30_000);
17
- const keepAliveMaxTimeoutMs = keepAliveMaxTimeout ?? envInt("MSLXDFF_UPSTREAM_KEEPALIVE_MAX_TIMEOUT", 60_000);
18
- const keepAliveConnections = connections ?? envInt("MSLXDFF_UPSTREAM_KEEPALIVE_CONNECTIONS", 20);
19
-
20
- let agent = null;
21
- let dispatcher = null;
22
- let closed = false;
23
-
24
- if (keepAlive && UndiciAgent) {
25
- try {
26
- agent = new UndiciAgent({
27
- keepAliveTimeout: keepAliveTimeoutMs,
28
- keepAliveMaxTimeout: keepAliveMaxTimeoutMs,
29
- connections: keepAliveConnections,
30
- pipelining: 1,
31
- });
32
- dispatcher = agent;
33
- } catch {
34
- agent = null;
35
- dispatcher = null;
36
- }
37
- }
38
-
39
- async function close() {
40
- if (closed) return;
41
- closed = true;
42
- if (agent && typeof agent.close === "function") {
43
- try { await agent.close(); } catch {}
44
- } else if (dispatcher && typeof dispatcher.close === "function" && dispatcher !== agent) {
45
- try { await dispatcher.close(); } catch {}
46
- }
47
- }
48
-
49
- return {
50
- get dispatcher() { return dispatcher; },
51
- get agent() { return agent; },
52
- get closed() { return closed; },
53
- close,
54
- [Symbol.asyncDispose]: close,
55
- };
56
- }
1
+ import { getUndici } from "../compat.js";
2
+
3
+ const UndiciAgent = getUndici().Agent;
4
+
5
+ function envInt(name, fallback) {
6
+ const v = Number(process.env[name]);
7
+ return Number.isInteger(v) && v > 0 ? v : fallback;
8
+ }
9
+
10
+ export function createPool({
11
+ keepAlive = true,
12
+ keepAliveTimeout,
13
+ keepAliveMaxTimeout,
14
+ connections,
15
+ } = {}) {
16
+ const keepAliveTimeoutMs = keepAliveTimeout ?? envInt("MSLXDFF_UPSTREAM_KEEPALIVE_TIMEOUT", 30_000);
17
+ const keepAliveMaxTimeoutMs = keepAliveMaxTimeout ?? envInt("MSLXDFF_UPSTREAM_KEEPALIVE_MAX_TIMEOUT", 60_000);
18
+ const keepAliveConnections = connections ?? envInt("MSLXDFF_UPSTREAM_KEEPALIVE_CONNECTIONS", 20);
19
+
20
+ let agent = null;
21
+ let dispatcher = null;
22
+ let closed = false;
23
+
24
+ if (keepAlive && UndiciAgent) {
25
+ try {
26
+ agent = new UndiciAgent({
27
+ keepAliveTimeout: keepAliveTimeoutMs,
28
+ keepAliveMaxTimeout: keepAliveMaxTimeoutMs,
29
+ connections: keepAliveConnections,
30
+ pipelining: 1,
31
+ });
32
+ dispatcher = agent;
33
+ } catch {
34
+ agent = null;
35
+ dispatcher = null;
36
+ }
37
+ }
38
+
39
+ async function close() {
40
+ if (closed) return;
41
+ closed = true;
42
+ if (agent && typeof agent.close === "function") {
43
+ try { await agent.close(); } catch {}
44
+ } else if (dispatcher && typeof dispatcher.close === "function" && dispatcher !== agent) {
45
+ try { await dispatcher.close(); } catch {}
46
+ }
47
+ }
48
+
49
+ return {
50
+ get dispatcher() { return dispatcher; },
51
+ get agent() { return agent; },
52
+ get closed() { return closed; },
53
+ close,
54
+ [Symbol.asyncDispose]: close,
55
+ };
56
+ }
@@ -1,24 +1,24 @@
1
- export function backoffDelay(baseMs, attempt) {
2
- // attempt 0 => base, 1 => base*2, 2 => base*4
3
- return baseMs * (1 << attempt);
4
- }
5
-
6
- export function resolveRetry(status, attempt, config) {
7
- const key = status === "network" ? "network" : String(status);
8
- const entry = config?.[key];
9
- if (!entry) return { shouldRetry: false, delayMs: 0 };
10
- const attempts = Number(entry.attempts) || 0;
11
- const base = Number(entry.delayMs) || 0;
12
- if (attempt < attempts) {
13
- return { shouldRetry: true, delayMs: backoffDelay(base, attempt) };
14
- }
15
- return { shouldRetry: false, delayMs: 0 };
16
- }
17
-
18
- export function shouldRetry(status, attempt, config) {
19
- return resolveRetry(status, attempt, config).shouldRetry;
20
- }
21
-
22
- export function sleep(ms) {
23
- return new Promise((r) => setTimeout(r, ms));
24
- }
1
+ export function backoffDelay(baseMs, attempt) {
2
+ // attempt 0 => base, 1 => base*2, 2 => base*4
3
+ return baseMs * (1 << attempt);
4
+ }
5
+
6
+ export function resolveRetry(status, attempt, config) {
7
+ const key = status === "network" ? "network" : String(status);
8
+ const entry = config?.[key];
9
+ if (!entry) return { shouldRetry: false, delayMs: 0 };
10
+ const attempts = Number(entry.attempts) || 0;
11
+ const base = Number(entry.delayMs) || 0;
12
+ if (attempt < attempts) {
13
+ return { shouldRetry: true, delayMs: backoffDelay(base, attempt) };
14
+ }
15
+ return { shouldRetry: false, delayMs: 0 };
16
+ }
17
+
18
+ export function shouldRetry(status, attempt, config) {
19
+ return resolveRetry(status, attempt, config).shouldRetry;
20
+ }
21
+
22
+ export function sleep(ms) {
23
+ return new Promise((r) => setTimeout(r, ms));
24
+ }