mslxdff 0.1.161 → 0.1.162

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 (93) hide show
  1. package/bin/mslxdff.js +4 -4
  2. package/package.json +1 -1
  3. package/src/auto.js +254 -254
  4. package/src/bench/cline-bench.js +42 -42
  5. package/src/bench/probe.js +70 -70
  6. package/src/bench/report.js +162 -162
  7. package/src/bench/runner.js +77 -77
  8. package/src/bench/via-probe.js +124 -124
  9. package/src/bench/via-routes.js +87 -87
  10. package/src/bench/workbuddy-bench.js +54 -54
  11. package/src/chat/engine.js +160 -160
  12. package/src/chat/gateway.js +163 -163
  13. package/src/chat/orchestrator.js +234 -234
  14. package/src/chat/prompt.js +70 -70
  15. package/src/chat/repl.js +88 -88
  16. package/src/chat/terminal.js +135 -135
  17. package/src/chat/tools.js +306 -306
  18. package/src/chat-pipeline/index.js +126 -126
  19. package/src/chat-pipeline/policy.js +76 -76
  20. package/src/cli/commands/group.js +249 -249
  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 +126 -126
  25. package/src/cli/commands/provider/models.js +142 -142
  26. package/src/cli/commands/provider/qwenwork-login.js +119 -119
  27. package/src/cli/commands/sync.js +232 -232
  28. package/src/cli/format.js +1 -0
  29. package/src/cli/status.js +279 -279
  30. package/src/daemon.js +101 -101
  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/model-trace.js +4 -3
  35. package/src/models.js +225 -225
  36. package/src/providers/cline/auth.js +228 -228
  37. package/src/providers/cline/chat.js +307 -307
  38. package/src/providers/cline.js +2 -2
  39. package/src/providers/dispatcher.js +26 -14
  40. package/src/providers/globalqwenwork/cosy.js +123 -123
  41. package/src/providers/globalqwenwork/crypto.js +218 -218
  42. package/src/providers/globalqwenwork/http.js +20 -20
  43. package/src/providers/globalqwenwork/rsa.js +54 -54
  44. package/src/providers/globalqwenwork/sse.js +268 -268
  45. package/src/providers/globalqwenwork/stream.js +130 -130
  46. package/src/providers/globalqwenwork/upstream.js +122 -122
  47. package/src/providers/keyring.js +60 -60
  48. package/src/providers/qoder/chat.js +193 -183
  49. package/src/providers/qoder/index.js +239 -230
  50. package/src/providers/qoder/sse.js +141 -103
  51. package/src/providers/qwenwork/account-store.js +133 -133
  52. package/src/providers/qwenwork/constants.js +67 -67
  53. package/src/providers/qwenwork/cosy.js +120 -120
  54. package/src/providers/qwenwork/crypto.js +218 -218
  55. package/src/providers/qwenwork/http.js +20 -20
  56. package/src/providers/qwenwork/index.js +327 -327
  57. package/src/providers/qwenwork/payload.js +142 -142
  58. package/src/providers/qwenwork/rsa.js +54 -54
  59. package/src/providers/qwenwork/sse.js +268 -268
  60. package/src/providers/qwenwork/stream.js +130 -130
  61. package/src/providers/qwenwork/upstream.js +120 -120
  62. package/src/providers/qwenwork.js +1 -1
  63. package/src/providers/registry.js +74 -74
  64. package/src/providers/workbuddy/chat.js +248 -248
  65. package/src/providers/workbuddy/reshape.js +152 -152
  66. package/src/providers/workbuddy.js +2 -2
  67. package/src/reasoning.js +32 -32
  68. package/src/routes/chat/gateway.js +46 -46
  69. package/src/routes/chat/relay-pipeline.js +276 -276
  70. package/src/routes/chat/via-route-handler.js +146 -146
  71. package/src/routes/hedge.js +255 -255
  72. package/src/routes/models-route.js +167 -167
  73. package/src/routes/peers.js +273 -273
  74. package/src/routes/stream.js +386 -386
  75. package/src/runtime/bootstrap.js +45 -45
  76. package/src/runtime/provider-gate.js +34 -34
  77. package/src/runtime/providers-setup.js +165 -165
  78. package/src/runtime/server-lifecycle.js +5 -6
  79. package/src/server.js +64 -64
  80. package/src/state/schemas/allowlist.js +92 -92
  81. package/src/sync-opencode.js +280 -280
  82. package/src/transport/index.js +244 -244
  83. package/src/transport/pool.js +56 -56
  84. package/src/transport/retry.js +24 -24
  85. package/src/transport/sse.js +93 -93
  86. package/src/upstream-probe/display.js +52 -52
  87. package/src/upstream-probe/probe.js +49 -49
  88. package/src/upstream-probe/rotate.js +110 -110
  89. package/src/upstream-probe/start.js +45 -45
  90. package/src/upstream.js +289 -289
  91. package/docs/cli_help_mini.md +0 -135
  92. package/src/providers/AGENTS.md +0 -46
  93. 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
+ }