mslxdff 0.1.173 → 0.1.175

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 (122) 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/auto-race.js +26 -1
  19. package/src/chat-pipeline/index.js +131 -131
  20. package/src/chat-pipeline/policy.js +84 -76
  21. package/src/chat-pipeline/serial-trial.js +35 -8
  22. package/src/chat-pipeline/terminal-verdict.js +109 -0
  23. package/src/claude-desktop/apply.js +42 -5
  24. package/src/claude-desktop/context1m.js +38 -0
  25. package/src/claude-desktop/slots.js +66 -0
  26. package/src/cli/commands/claude-desktop.js +27 -7
  27. package/src/cli/commands/group.js +249 -249
  28. package/src/cli/commands/model/picks.js +50 -50
  29. package/src/cli/commands/provider/bench-via.js +247 -247
  30. package/src/cli/commands/provider/bench.js +141 -141
  31. package/src/cli/commands/provider/index.js +136 -133
  32. package/src/cli/commands/provider/keys-guard.js +50 -0
  33. package/src/cli/commands/provider/keys.js +7 -0
  34. package/src/cli/commands/provider/models.js +145 -145
  35. package/src/cli/commands/provider/qwenwork-login.js +119 -119
  36. package/src/cli/commands/provider/traework-checkin.js +144 -0
  37. package/src/cli/commands/provider/traework-login.js +7 -45
  38. package/src/cli/commands/sync.js +310 -310
  39. package/src/cli/help.js +4 -1
  40. package/src/cli/status.js +279 -279
  41. package/src/daemon.js +112 -112
  42. package/src/model-capabilities/enrich.js +86 -86
  43. package/src/model-capabilities/index.js +183 -183
  44. package/src/model-capabilities/parse.js +70 -70
  45. package/src/model-trace.js +2 -0
  46. package/src/models.js +225 -225
  47. package/src/opencode-identity.js +39 -19
  48. package/src/providers/cline/auth.js +228 -228
  49. package/src/providers/cline/chat.js +307 -307
  50. package/src/providers/cline.js +2 -2
  51. package/src/providers/codearts/chat.js +7 -1
  52. package/src/providers/codearts/const.js +6 -1
  53. package/src/providers/codearts/index.js +18 -11
  54. package/src/providers/globalqwenwork/cosy.js +123 -123
  55. package/src/providers/globalqwenwork/crypto.js +218 -218
  56. package/src/providers/globalqwenwork/http.js +20 -20
  57. package/src/providers/globalqwenwork/rsa.js +54 -54
  58. package/src/providers/globalqwenwork/sse.js +268 -268
  59. package/src/providers/globalqwenwork/stream.js +130 -130
  60. package/src/providers/globalqwenwork/upstream.js +122 -122
  61. package/src/providers/keyring.js +60 -60
  62. package/src/providers/qoder/chat.js +203 -203
  63. package/src/providers/qoder/index.js +249 -249
  64. package/src/providers/qoder/sse.js +145 -145
  65. package/src/providers/qwenwork/account-store.js +133 -133
  66. package/src/providers/qwenwork/constants.js +67 -67
  67. package/src/providers/qwenwork/cosy.js +120 -120
  68. package/src/providers/qwenwork/crypto.js +218 -218
  69. package/src/providers/qwenwork/http.js +20 -20
  70. package/src/providers/qwenwork/index.js +327 -327
  71. package/src/providers/qwenwork/payload.js +144 -144
  72. package/src/providers/qwenwork/rsa.js +54 -54
  73. package/src/providers/qwenwork/sse.js +268 -268
  74. package/src/providers/qwenwork/stream.js +130 -130
  75. package/src/providers/qwenwork/upstream.js +120 -120
  76. package/src/providers/qwenwork.js +1 -1
  77. package/src/providers/registry.js +79 -79
  78. package/src/providers/traework/account-store.js +87 -2
  79. package/src/providers/traework/checkin.js +51 -9
  80. package/src/providers/traework/constants.js +20 -0
  81. package/src/providers/traework/headers.js +42 -10
  82. package/src/providers/traework/index.js +14 -4
  83. package/src/providers/traework/market-identity.js +116 -0
  84. package/src/providers/workbuddy/chat.js +248 -248
  85. package/src/providers/workbuddy/reshape.js +152 -152
  86. package/src/providers/workbuddy.js +2 -2
  87. package/src/reasoning.js +32 -32
  88. package/src/routes/chat/gateway.js +46 -46
  89. package/src/routes/chat/relay-pipeline.js +294 -294
  90. package/src/routes/chat/via-route-handler.js +146 -146
  91. package/src/routes/hedge.js +255 -255
  92. package/src/routes/models-route.js +167 -167
  93. package/src/routes/peers.js +273 -273
  94. package/src/routes/stream.js +385 -385
  95. package/src/runtime/bootstrap.js +50 -50
  96. package/src/runtime/provider-gate.js +36 -36
  97. package/src/runtime/providers-setup.js +168 -165
  98. package/src/runtime/traework-checkin.js +26 -11
  99. package/src/server.js +69 -69
  100. package/src/state/schemas/allowlist.js +92 -92
  101. package/src/sync-opencode.js +280 -280
  102. package/src/transport/index.js +244 -244
  103. package/src/transport/pool.js +56 -56
  104. package/src/transport/retry.js +24 -24
  105. package/src/transport/sse.js +93 -93
  106. package/src/upstream-probe/display.js +52 -52
  107. package/src/upstream-probe/probe.js +49 -49
  108. package/src/upstream-probe/rotate.js +110 -110
  109. package/src/upstream-probe/start.js +45 -45
  110. package/src/upstream.js +291 -289
  111. package/src/warp-pool/config.js +71 -0
  112. package/src/warp-pool/health.js +65 -0
  113. package/src/warp-pool/index.js +252 -0
  114. package/src/warp-pool/keys.js +29 -0
  115. package/src/warp-pool/nodes.js +94 -0
  116. package/src/warp-pool/register.js +113 -0
  117. package/src/warp-pool/rotate.js +150 -0
  118. package/src/warp-pool/strategy.js +43 -0
  119. package/src/warp-pool/wireproxy.js +153 -0
  120. package/docs/cli_help_mini.md +0 -143
  121. package/src/providers/AGENTS.md +0 -49
  122. package/src/routes/AGENTS.md +0 -37
@@ -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
+ }