mslxdff 0.1.170 → 0.1.172
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/docs/cli_help_mini.md +143 -0
- package/package.json +1 -1
- package/src/anthropic/count-tokens.js +39 -0
- package/src/anthropic/translate.js +20 -41
- 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 +131 -131
- package/src/chat-pipeline/policy.js +76 -76
- 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 +133 -133
- package/src/cli/commands/provider/models.js +145 -145
- package/src/cli/commands/provider/qwenwork-login.js +119 -119
- package/src/cli/commands/sync.js +310 -310
- package/src/cli/status.js +279 -279
- package/src/daemon.js +112 -112
- package/src/model-capabilities/context-lookup.js +65 -0
- package/src/model-capabilities/enrich.js +86 -86
- package/src/model-capabilities/index.js +183 -183
- package/src/model-capabilities/merge.js +50 -35
- package/src/model-capabilities/parse.js +70 -70
- package/src/models.js +225 -225
- package/src/providers/AGENTS.md +49 -0
- 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/concurrency.js +34 -0
- package/src/providers/codearts/index.js +11 -3
- 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/payload.js +4 -2
- 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 -200
- 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 +327 -327
- package/src/providers/qwenwork/payload.js +144 -142
- 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 +10 -5
- package/src/providers/raccoon/const.js +4 -4
- package/src/providers/registry.js +79 -79
- package/src/providers/traework/models.js +4 -3
- package/src/providers/workbuddy/chat.js +248 -248
- package/src/providers/workbuddy/payload.js +16 -7
- package/src/providers/workbuddy/reshape.js +152 -152
- package/src/providers/workbuddy.js +2 -2
- package/src/providers/zcode/chat.js +7 -0
- package/src/reasoning.js +32 -32
- package/src/responses/translate.js +4 -0
- package/src/routes/AGENTS.md +37 -0
- package/src/routes/chat/exhausted-handler.js +5 -0
- package/src/routes/chat/gateway.js +46 -46
- package/src/routes/chat/relay-pipeline.js +294 -291
- package/src/routes/chat/via-route-handler.js +146 -146
- package/src/routes/hedge.js +255 -255
- package/src/routes/messages-route.js +1 -1
- package/src/routes/models-route.js +167 -167
- package/src/routes/peers.js +273 -273
- package/src/routes/stream-scan.js +47 -21
- package/src/routes/stream.js +385 -385
- package/src/runtime/bootstrap.js +50 -50
- package/src/runtime/provider-gate.js +36 -36
- package/src/runtime/providers-setup.js +165 -165
- package/src/server.js +69 -69
- package/src/state/schemas/allowlist.js +92 -92
- package/src/sync-opencode.js +280 -280
- package/src/talk-full.js +1 -0
- package/src/talk-log.js +3 -2
- package/src/thinking-effort.js +67 -0
- 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/convert.js +6 -0
- 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-responses.js +5 -0
- package/src/upstream.js +289 -289
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// 思考档位词表的单一来源(change: fix-thinking-effort-passthrough)。
|
|
2
|
+
// 入站翻译层保留档位字段后,各家供应商整形器都从这里读,避免像过去那样五处各写各的映射。
|
|
3
|
+
// 契约:
|
|
4
|
+
// - effortFromBody(body):按固定优先级取出客户端声明的档位原始串(不解释语义)。
|
|
5
|
+
// - isEffortOff(v):单个值是否表示"关思考"。
|
|
6
|
+
// - isReasoningEnabled(effort, fallback):布尔家(qoder/qwenwork 系)的推导。
|
|
7
|
+
// - effortLevelFromBody(body):只取「真档位载体」(reasoning_effort > reasoningEffort > effort),不含 thinking.type 开关。
|
|
8
|
+
// 关断值(off/none/disabled,大小写不敏感)**最高优先**:误开思考烧 token 不可逆,误关只是少想一点。
|
|
9
|
+
|
|
10
|
+
const OFF_VALUES = new Set(["off", "none", "disabled"]);
|
|
11
|
+
|
|
12
|
+
/** 单个档位值是否表示"关思考"。非字符串/空串/null 一律不算关。 */
|
|
13
|
+
export function isEffortOff(v) {
|
|
14
|
+
return OFF_VALUES.has(String(v ?? "").trim().toLowerCase());
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** 取对象里的字符串档位值(非字符串返回空串)。 */
|
|
18
|
+
function str(v) {
|
|
19
|
+
return typeof v === "string" ? v.trim() : v == null ? "" : String(v).trim();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 取 thinking 对象的 type(非对象返回空串)。 */
|
|
23
|
+
function thinkingType(th) {
|
|
24
|
+
return th && typeof th === "object" ? str(th.type) : "";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 客户端声明的思考档位,优先级:
|
|
29
|
+
* extra_body.thinking.type > reasoning_effort > reasoningEffort > effort > thinking.type
|
|
30
|
+
* 任一候选是关断值即返回该值(关断最高优先);否则返回首个非空候选;全空返回 ""。
|
|
31
|
+
*/
|
|
32
|
+
export function effortFromBody(body) {
|
|
33
|
+
const b = body && typeof body === "object" ? body : {};
|
|
34
|
+
const candidates = [
|
|
35
|
+
thinkingType(b.extra_body?.thinking),
|
|
36
|
+
str(b.reasoning_effort),
|
|
37
|
+
str(b.reasoningEffort),
|
|
38
|
+
str(b.effort),
|
|
39
|
+
thinkingType(b.thinking),
|
|
40
|
+
];
|
|
41
|
+
for (const c of candidates) if (c && isEffortOff(c)) return c;
|
|
42
|
+
return candidates.find((c) => c) || "";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 布尔家推导:有档位时"关断→false、其余→true";无档位时回退到调用方给的默认(如模型目录结论)。
|
|
47
|
+
*/
|
|
48
|
+
export function isReasoningEnabled(effort, fallback = false) {
|
|
49
|
+
const v = str(effort);
|
|
50
|
+
if (!v) return fallback === true;
|
|
51
|
+
return !isEffortOff(v);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const LEVEL_KEYS = ["reasoning_effort", "reasoningEffort", "effort"];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 只从「真档位载体」取级别串(不含 `thinking.type` 这类开关载体——它的 "enabled"/"disabled" 不是档位级别,
|
|
58
|
+
* 拿去当 `reasoning_effort` 发上游必被拒)。空串/纯空白一律不算声明。全空返回 ""。
|
|
59
|
+
*/
|
|
60
|
+
export function effortLevelFromBody(body) {
|
|
61
|
+
const b = body && typeof body === "object" ? body : {};
|
|
62
|
+
for (const k of LEVEL_KEYS) {
|
|
63
|
+
const v = str(b[k]);
|
|
64
|
+
if (v) return v;
|
|
65
|
+
}
|
|
66
|
+
return "";
|
|
67
|
+
}
|
package/src/transport/index.js
CHANGED
|
@@ -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
|
+
}
|
package/src/transport/pool.js
CHANGED
|
@@ -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
|
+
}
|