mslxdff 0.1.231 → 0.1.232
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/mslxdff.js +3 -3
- package/package.json +1 -1
- package/src/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 +145 -145
- package/src/chat-pipeline/policy.js +84 -84
- package/src/cli/commands/group.js +266 -266
- 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 +139 -139
- package/src/cli/commands/provider/models.js +149 -149
- package/src/cli/commands/provider/qwenwork-login.js +119 -119
- package/src/cli/commands/provider/raccoon-quota.js +156 -156
- package/src/cli/commands/sync.js +310 -310
- package/src/cli/status.js +279 -279
- package/src/daemon.js +246 -246
- package/src/model-capabilities/enrich.js +86 -86
- package/src/model-capabilities/index.js +183 -183
- package/src/model-capabilities/parse.js +70 -70
- package/src/models.js +225 -225
- package/src/peers.js +345 -345
- package/src/providers/agentrouter.js +238 -238
- package/src/providers/base.js +164 -164
- package/src/providers/cline/auth.js +228 -228
- package/src/providers/cline/chat.js +307 -307
- package/src/providers/cline.js +2 -2
- package/src/providers/codearts/chat.js +207 -207
- package/src/providers/dispatcher.js +147 -147
- package/src/providers/globalqwenwork/cosy.js +123 -123
- package/src/providers/globalqwenwork/crypto.js +218 -218
- package/src/providers/globalqwenwork/http.js +20 -20
- package/src/providers/globalqwenwork/rsa.js +54 -54
- package/src/providers/globalqwenwork/sse.js +268 -268
- package/src/providers/globalqwenwork/stream.js +130 -130
- package/src/providers/globalqwenwork/upstream.js +122 -122
- package/src/providers/keyring.js +60 -60
- package/src/providers/qoder/chat.js +203 -203
- package/src/providers/qoder/index.js +249 -249
- package/src/providers/qoder/sse.js +145 -145
- package/src/providers/qwenwork/account-store.js +133 -133
- package/src/providers/qwenwork/constants.js +67 -67
- package/src/providers/qwenwork/cosy.js +120 -120
- package/src/providers/qwenwork/crypto.js +218 -218
- package/src/providers/qwenwork/http.js +20 -20
- package/src/providers/qwenwork/index.js +337 -337
- package/src/providers/qwenwork/payload.js +144 -144
- package/src/providers/qwenwork/rsa.js +54 -54
- package/src/providers/qwenwork/sse.js +268 -268
- package/src/providers/qwenwork/stream.js +130 -130
- package/src/providers/qwenwork/upstream.js +120 -120
- package/src/providers/qwenwork.js +1 -1
- package/src/providers/raccoon/const.js +202 -202
- package/src/providers/raccoon/credits.js +285 -285
- package/src/providers/registry.js +89 -89
- package/src/providers/workbuddy/auth.js +16 -0
- package/src/providers/workbuddy/chat.js +298 -264
- package/src/providers/workbuddy/client-headers.js +148 -0
- package/src/providers/workbuddy/desktop-info.js +17 -1
- package/src/providers/workbuddy/models.js +8 -20
- package/src/providers/workbuddy/reshape.js +152 -152
- package/src/providers/workbuddy.js +2 -2
- package/src/reasoning.js +32 -32
- package/src/routes/chat/broadband-handler.js +80 -80
- package/src/routes/chat/gateway.js +46 -46
- package/src/routes/chat/relay-pipeline.js +282 -282
- package/src/routes/chat/via-route-handler.js +156 -156
- package/src/routes/hedge.js +260 -260
- package/src/routes/models-route.js +167 -167
- package/src/routes/peers.js +373 -373
- package/src/routes/stream.js +347 -347
- package/src/runtime/bootstrap.js +75 -75
- package/src/runtime/keep-warm.js +135 -135
- package/src/runtime/provider-gate.js +75 -75
- package/src/runtime/providers-setup.js +187 -187
- package/src/runtime/raccoon-checkin.js +175 -175
- package/src/server.js +85 -85
- package/src/state/schemas/allowlist.js +92 -92
- package/src/sync-opencode.js +280 -280
- package/src/transport/index.js +244 -244
- package/src/transport/pool.js +56 -56
- package/src/transport/retry.js +24 -24
- package/src/transport/sse.js +93 -93
- package/src/upstream-probe/display.js +52 -52
- package/src/upstream-probe/probe.js +49 -49
- package/src/upstream-probe/rotate.js +110 -110
- package/src/upstream-probe/start.js +45 -45
- package/src/upstream.js +355 -355
- package/docs/cli_help_mini.md +0 -148
- package/src/providers/AGENTS.md +0 -49
- package/src/routes/AGENTS.md +0 -37
package/src/providers/base.js
CHANGED
|
@@ -1,164 +1,164 @@
|
|
|
1
|
-
import { createKeyRing } from "./keyring.js";
|
|
2
|
-
import { joinModelId } from "./model-id.js";
|
|
3
|
-
import { getUndici as compatGetUndici, compatFetch } from "../compat.js";
|
|
4
|
-
import { createSdkDispatch } from "../upstream-engine/sdk/dispatch.js";
|
|
5
|
-
|
|
6
|
-
const { fetch: UndiciFetch, Agent: UndiciAgent } = compatGetUndici();
|
|
7
|
-
|
|
8
|
-
export function envInt(name, fallback) {
|
|
9
|
-
const v = Number(process.env[name]);
|
|
10
|
-
return Number.isInteger(v) && v > 0 ? v : fallback;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function joinUrl(base, path) {
|
|
14
|
-
const b = String(base || "").trim().replace(/\/+$/, "");
|
|
15
|
-
const p = String(path || "").trim();
|
|
16
|
-
if (!p) return b;
|
|
17
|
-
const pp = p.startsWith("/") ? p : `/${p}`;
|
|
18
|
-
return `${b}${pp}`;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function sleep(ms) {
|
|
22
|
-
return new Promise((r) => setTimeout(r, ms));
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function getUndici() {
|
|
26
|
-
return { UndiciAgent, UndiciFetch, fetch: UndiciFetch || compatFetch, Agent: UndiciAgent };
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function createAgent({ keepAliveTimeout = 30_000, keepAliveMaxTimeout = 60_000, connections = 20 } = {}) {
|
|
30
|
-
if (!UndiciAgent) return { agent: null, dispatcher: null };
|
|
31
|
-
try {
|
|
32
|
-
const agent = new UndiciAgent({ keepAliveTimeout, keepAliveMaxTimeout, connections, pipelining: 1 });
|
|
33
|
-
return { agent, dispatcher: agent };
|
|
34
|
-
} catch {
|
|
35
|
-
return { agent: null, dispatcher: null };
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function collectApiKeysGeneric(id, apiKeys, apiKey, loadKeys) {
|
|
40
|
-
const list = [
|
|
41
|
-
...(Array.isArray(apiKeys) ? apiKeys : [apiKeys].filter(Boolean)),
|
|
42
|
-
apiKey,
|
|
43
|
-
...(apiKeys === undefined && apiKey === undefined ? loadKeys(id) : []),
|
|
44
|
-
].filter((k) => typeof k === "string" && k.trim().length);
|
|
45
|
-
return [...new Set(list.map((k) => k.trim()))];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function createChatRunner({ id, ring, cooldownMs, retry, fetchImpl, dispatcher, buildHeaders, getUrl, connectTimeoutMs = 30_000, sdkDispatch } = {}) {
|
|
49
|
-
// SDK 通道(缺省):仅流式;非流式委派原生,避免把 JSON 客户端 SSE 化。
|
|
50
|
-
const sdk = sdkDispatch === undefined ? createSdkDispatch({ id, providerName: id }) : sdkDispatch;
|
|
51
|
-
|
|
52
|
-
async function attemptOnce(url, body, key, activeRing) {
|
|
53
|
-
if (sdk?.enabled && body?.stream !== false) {
|
|
54
|
-
let r;
|
|
55
|
-
try { r = await sdk.trySdk({ url, body, headers: buildHeaders(body, key), fetchImpl }); }
|
|
56
|
-
catch (err) { return err; } // 交 runChat 的 network 重试/冷却
|
|
57
|
-
if (r) return r.status === 401 && !activeRing.size ? { __needKey: true } : r;
|
|
58
|
-
}
|
|
59
|
-
const controller = new AbortController();
|
|
60
|
-
const timer = setTimeout(() => controller.abort(new Error(`${id} timed out after ${connectTimeoutMs}ms`)), connectTimeoutMs);
|
|
61
|
-
try {
|
|
62
|
-
const opts = { method: "POST", headers: buildHeaders(body, key), body: JSON.stringify(body), signal: controller.signal };
|
|
63
|
-
if (dispatcher) opts.dispatcher = dispatcher;
|
|
64
|
-
const res = await fetchImpl(url, opts);
|
|
65
|
-
if (res.status === 401 && !activeRing.size) return { __needKey: true };
|
|
66
|
-
return res;
|
|
67
|
-
} catch (err) {
|
|
68
|
-
return err;
|
|
69
|
-
} finally {
|
|
70
|
-
clearTimeout(timer);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
async function runChat(body, activeRing, sourceKey) {
|
|
75
|
-
const url = getUrl();
|
|
76
|
-
const t0 = performance.now();
|
|
77
|
-
const attempts = [];
|
|
78
|
-
let waitMs = 0;
|
|
79
|
-
const key = activeRing.next();
|
|
80
|
-
if (!key && activeRing.size > 0) {
|
|
81
|
-
const err = new Error(`${id}: all API keys are in cooldown (last error < ${cooldownMs}ms ago) — provider temporarily unavailable`);
|
|
82
|
-
err._t = { attempts: [], waitMs: 0, totalMs: Math.round(performance.now() - t0), cooldownMs };
|
|
83
|
-
throw err;
|
|
84
|
-
}
|
|
85
|
-
for (let attempt = 0; ; attempt++) {
|
|
86
|
-
const t = performance.now();
|
|
87
|
-
const result = await attemptOnce(url, body, key, activeRing);
|
|
88
|
-
const type = result instanceof Error ? "network" : `http${result?.status}`;
|
|
89
|
-
attempts.push({ attempt, type, ms: Math.round(performance.now() - t) });
|
|
90
|
-
if (result?.__needKey) {
|
|
91
|
-
const err = new Error(`${id}: missing ${sourceKey} (chat requires a real key)`);
|
|
92
|
-
err._t = { attempts, waitMs, totalMs: Math.round(performance.now() - t0) };
|
|
93
|
-
throw err;
|
|
94
|
-
}
|
|
95
|
-
if (result instanceof Error) {
|
|
96
|
-
const entry = retry?.network;
|
|
97
|
-
if (entry && attempt < entry.attempts) { await sleep(entry.delayMs); waitMs += entry.delayMs; continue; }
|
|
98
|
-
activeRing.onError(key);
|
|
99
|
-
result._t = { attempts, waitMs, totalMs: Math.round(performance.now() - t0) };
|
|
100
|
-
throw result;
|
|
101
|
-
}
|
|
102
|
-
const entry = retry?.[result.status];
|
|
103
|
-
if (entry && attempt < entry.attempts) { await sleep(entry.delayMs); waitMs += entry.delayMs; continue; }
|
|
104
|
-
if (result.status === 401 || result.status === 403 || result.status === 429 || result.status >= 500) activeRing.onError(key);
|
|
105
|
-
result._t = { attempts, waitMs, totalMs: Math.round(performance.now() - t0) };
|
|
106
|
-
return result;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return { attemptOnce, runChat };
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function createListModelsRunner({ id, ring, dispatcher, fetchImpl, getUrl, mapModel }) {
|
|
114
|
-
const CACHE_TTL_MS = 10 * 60 * 1000;
|
|
115
|
-
let cache = null;
|
|
116
|
-
let fetchedAt = 0;
|
|
117
|
-
async function listModels() {
|
|
118
|
-
const now = Date.now();
|
|
119
|
-
if (cache && now - fetchedAt < CACHE_TTL_MS) return cache;
|
|
120
|
-
const url = getUrl();
|
|
121
|
-
const controller = new AbortController();
|
|
122
|
-
const timer = setTimeout(() => controller.abort(new Error(`${id} models timed out`)), 15_000);
|
|
123
|
-
try {
|
|
124
|
-
const headers = { Accept: "application/json" };
|
|
125
|
-
const key = ring.next();
|
|
126
|
-
if (key) headers["Authorization"] = `Bearer ${key}`;
|
|
127
|
-
const opts = { headers, signal: controller.signal };
|
|
128
|
-
if (dispatcher) opts.dispatcher = dispatcher;
|
|
129
|
-
const res = await fetchImpl(url, opts);
|
|
130
|
-
if (!res.ok) return [];
|
|
131
|
-
const json = await res.json().catch(() => ({}));
|
|
132
|
-
const raw = Array.isArray(json.data) ? json.data : Array.isArray(json) ? json : [];
|
|
133
|
-
const mapped = mapModel ? mapModel(raw) : raw;
|
|
134
|
-
cache = mapped.filter((m) => m && typeof m.id === "string").map((m) => ({ ...m, id: joinModelId(id, m.id) }));
|
|
135
|
-
fetchedAt = now;
|
|
136
|
-
return cache;
|
|
137
|
-
} catch {
|
|
138
|
-
return [];
|
|
139
|
-
} finally {
|
|
140
|
-
clearTimeout(timer);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return { listModels, _cache: () => cache };
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export function createPreheatRunner({ dispatcher, fetchImpl, getUrl, id, ring, loadKeys }) {
|
|
147
|
-
async function preheat() {
|
|
148
|
-
const url = getUrl();
|
|
149
|
-
const t0 = performance.now();
|
|
150
|
-
try {
|
|
151
|
-
const headers = { Accept: "application/json" };
|
|
152
|
-
const key = (loadKeys ? loadKeys(id)[0] : ring.next()) || ring.next();
|
|
153
|
-
if (key) headers["Authorization"] = `Bearer ${key}`;
|
|
154
|
-
const opts = { headers };
|
|
155
|
-
if (dispatcher) opts.dispatcher = dispatcher;
|
|
156
|
-
const res = await fetchImpl(url, opts);
|
|
157
|
-
try { if (res.body) await res.text().catch(() => {}); } catch {}
|
|
158
|
-
return { ok: res.ok, status: res.status, ms: Math.round(performance.now() - t0) };
|
|
159
|
-
} catch (err) {
|
|
160
|
-
return { ok: false, error: String(err?.message || err), ms: Math.round(performance.now() - t0) };
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
return { preheat };
|
|
164
|
-
}
|
|
1
|
+
import { createKeyRing } from "./keyring.js";
|
|
2
|
+
import { joinModelId } from "./model-id.js";
|
|
3
|
+
import { getUndici as compatGetUndici, compatFetch } from "../compat.js";
|
|
4
|
+
import { createSdkDispatch } from "../upstream-engine/sdk/dispatch.js";
|
|
5
|
+
|
|
6
|
+
const { fetch: UndiciFetch, Agent: UndiciAgent } = compatGetUndici();
|
|
7
|
+
|
|
8
|
+
export function envInt(name, fallback) {
|
|
9
|
+
const v = Number(process.env[name]);
|
|
10
|
+
return Number.isInteger(v) && v > 0 ? v : fallback;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function joinUrl(base, path) {
|
|
14
|
+
const b = String(base || "").trim().replace(/\/+$/, "");
|
|
15
|
+
const p = String(path || "").trim();
|
|
16
|
+
if (!p) return b;
|
|
17
|
+
const pp = p.startsWith("/") ? p : `/${p}`;
|
|
18
|
+
return `${b}${pp}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function sleep(ms) {
|
|
22
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getUndici() {
|
|
26
|
+
return { UndiciAgent, UndiciFetch, fetch: UndiciFetch || compatFetch, Agent: UndiciAgent };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function createAgent({ keepAliveTimeout = 30_000, keepAliveMaxTimeout = 60_000, connections = 20 } = {}) {
|
|
30
|
+
if (!UndiciAgent) return { agent: null, dispatcher: null };
|
|
31
|
+
try {
|
|
32
|
+
const agent = new UndiciAgent({ keepAliveTimeout, keepAliveMaxTimeout, connections, pipelining: 1 });
|
|
33
|
+
return { agent, dispatcher: agent };
|
|
34
|
+
} catch {
|
|
35
|
+
return { agent: null, dispatcher: null };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function collectApiKeysGeneric(id, apiKeys, apiKey, loadKeys) {
|
|
40
|
+
const list = [
|
|
41
|
+
...(Array.isArray(apiKeys) ? apiKeys : [apiKeys].filter(Boolean)),
|
|
42
|
+
apiKey,
|
|
43
|
+
...(apiKeys === undefined && apiKey === undefined ? loadKeys(id) : []),
|
|
44
|
+
].filter((k) => typeof k === "string" && k.trim().length);
|
|
45
|
+
return [...new Set(list.map((k) => k.trim()))];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function createChatRunner({ id, ring, cooldownMs, retry, fetchImpl, dispatcher, buildHeaders, getUrl, connectTimeoutMs = 30_000, sdkDispatch } = {}) {
|
|
49
|
+
// SDK 通道(缺省):仅流式;非流式委派原生,避免把 JSON 客户端 SSE 化。
|
|
50
|
+
const sdk = sdkDispatch === undefined ? createSdkDispatch({ id, providerName: id }) : sdkDispatch;
|
|
51
|
+
|
|
52
|
+
async function attemptOnce(url, body, key, activeRing) {
|
|
53
|
+
if (sdk?.enabled && body?.stream !== false) {
|
|
54
|
+
let r;
|
|
55
|
+
try { r = await sdk.trySdk({ url, body, headers: buildHeaders(body, key), fetchImpl }); }
|
|
56
|
+
catch (err) { return err; } // 交 runChat 的 network 重试/冷却
|
|
57
|
+
if (r) return r.status === 401 && !activeRing.size ? { __needKey: true } : r;
|
|
58
|
+
}
|
|
59
|
+
const controller = new AbortController();
|
|
60
|
+
const timer = setTimeout(() => controller.abort(new Error(`${id} timed out after ${connectTimeoutMs}ms`)), connectTimeoutMs);
|
|
61
|
+
try {
|
|
62
|
+
const opts = { method: "POST", headers: buildHeaders(body, key), body: JSON.stringify(body), signal: controller.signal };
|
|
63
|
+
if (dispatcher) opts.dispatcher = dispatcher;
|
|
64
|
+
const res = await fetchImpl(url, opts);
|
|
65
|
+
if (res.status === 401 && !activeRing.size) return { __needKey: true };
|
|
66
|
+
return res;
|
|
67
|
+
} catch (err) {
|
|
68
|
+
return err;
|
|
69
|
+
} finally {
|
|
70
|
+
clearTimeout(timer);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function runChat(body, activeRing, sourceKey) {
|
|
75
|
+
const url = getUrl();
|
|
76
|
+
const t0 = performance.now();
|
|
77
|
+
const attempts = [];
|
|
78
|
+
let waitMs = 0;
|
|
79
|
+
const key = activeRing.next();
|
|
80
|
+
if (!key && activeRing.size > 0) {
|
|
81
|
+
const err = new Error(`${id}: all API keys are in cooldown (last error < ${cooldownMs}ms ago) — provider temporarily unavailable`);
|
|
82
|
+
err._t = { attempts: [], waitMs: 0, totalMs: Math.round(performance.now() - t0), cooldownMs };
|
|
83
|
+
throw err;
|
|
84
|
+
}
|
|
85
|
+
for (let attempt = 0; ; attempt++) {
|
|
86
|
+
const t = performance.now();
|
|
87
|
+
const result = await attemptOnce(url, body, key, activeRing);
|
|
88
|
+
const type = result instanceof Error ? "network" : `http${result?.status}`;
|
|
89
|
+
attempts.push({ attempt, type, ms: Math.round(performance.now() - t) });
|
|
90
|
+
if (result?.__needKey) {
|
|
91
|
+
const err = new Error(`${id}: missing ${sourceKey} (chat requires a real key)`);
|
|
92
|
+
err._t = { attempts, waitMs, totalMs: Math.round(performance.now() - t0) };
|
|
93
|
+
throw err;
|
|
94
|
+
}
|
|
95
|
+
if (result instanceof Error) {
|
|
96
|
+
const entry = retry?.network;
|
|
97
|
+
if (entry && attempt < entry.attempts) { await sleep(entry.delayMs); waitMs += entry.delayMs; continue; }
|
|
98
|
+
activeRing.onError(key);
|
|
99
|
+
result._t = { attempts, waitMs, totalMs: Math.round(performance.now() - t0) };
|
|
100
|
+
throw result;
|
|
101
|
+
}
|
|
102
|
+
const entry = retry?.[result.status];
|
|
103
|
+
if (entry && attempt < entry.attempts) { await sleep(entry.delayMs); waitMs += entry.delayMs; continue; }
|
|
104
|
+
if (result.status === 401 || result.status === 403 || result.status === 429 || result.status >= 500) activeRing.onError(key);
|
|
105
|
+
result._t = { attempts, waitMs, totalMs: Math.round(performance.now() - t0) };
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return { attemptOnce, runChat };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function createListModelsRunner({ id, ring, dispatcher, fetchImpl, getUrl, mapModel }) {
|
|
114
|
+
const CACHE_TTL_MS = 10 * 60 * 1000;
|
|
115
|
+
let cache = null;
|
|
116
|
+
let fetchedAt = 0;
|
|
117
|
+
async function listModels() {
|
|
118
|
+
const now = Date.now();
|
|
119
|
+
if (cache && now - fetchedAt < CACHE_TTL_MS) return cache;
|
|
120
|
+
const url = getUrl();
|
|
121
|
+
const controller = new AbortController();
|
|
122
|
+
const timer = setTimeout(() => controller.abort(new Error(`${id} models timed out`)), 15_000);
|
|
123
|
+
try {
|
|
124
|
+
const headers = { Accept: "application/json" };
|
|
125
|
+
const key = ring.next();
|
|
126
|
+
if (key) headers["Authorization"] = `Bearer ${key}`;
|
|
127
|
+
const opts = { headers, signal: controller.signal };
|
|
128
|
+
if (dispatcher) opts.dispatcher = dispatcher;
|
|
129
|
+
const res = await fetchImpl(url, opts);
|
|
130
|
+
if (!res.ok) return [];
|
|
131
|
+
const json = await res.json().catch(() => ({}));
|
|
132
|
+
const raw = Array.isArray(json.data) ? json.data : Array.isArray(json) ? json : [];
|
|
133
|
+
const mapped = mapModel ? mapModel(raw) : raw;
|
|
134
|
+
cache = mapped.filter((m) => m && typeof m.id === "string").map((m) => ({ ...m, id: joinModelId(id, m.id) }));
|
|
135
|
+
fetchedAt = now;
|
|
136
|
+
return cache;
|
|
137
|
+
} catch {
|
|
138
|
+
return [];
|
|
139
|
+
} finally {
|
|
140
|
+
clearTimeout(timer);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return { listModels, _cache: () => cache };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function createPreheatRunner({ dispatcher, fetchImpl, getUrl, id, ring, loadKeys }) {
|
|
147
|
+
async function preheat() {
|
|
148
|
+
const url = getUrl();
|
|
149
|
+
const t0 = performance.now();
|
|
150
|
+
try {
|
|
151
|
+
const headers = { Accept: "application/json" };
|
|
152
|
+
const key = (loadKeys ? loadKeys(id)[0] : ring.next()) || ring.next();
|
|
153
|
+
if (key) headers["Authorization"] = `Bearer ${key}`;
|
|
154
|
+
const opts = { headers };
|
|
155
|
+
if (dispatcher) opts.dispatcher = dispatcher;
|
|
156
|
+
const res = await fetchImpl(url, opts);
|
|
157
|
+
try { if (res.body) await res.text().catch(() => {}); } catch {}
|
|
158
|
+
return { ok: res.ok, status: res.status, ms: Math.round(performance.now() - t0) };
|
|
159
|
+
} catch (err) {
|
|
160
|
+
return { ok: false, error: String(err?.message || err), ms: Math.round(performance.now() - t0) };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return { preheat };
|
|
164
|
+
}
|