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,163 +1,163 @@
1
- import { performance } from "node:perf_hooks";
2
- import { parseSse } from "./sse.js";
3
- import { DEFAULT_PORT } from "../state.js";
4
- import { compatFetch } from "../compat.js";
5
-
6
- /**
7
- * 网关深模块:POST 127.0.0.1:port/v1/chat/completions model:auto
8
- * 注入化:fetch/loadToken/getPort/readModelsJson 均可伪,便于单测
9
- */
10
- export function createGatewayClient({
11
- fetchImpl = compatFetch,
12
- loadToken,
13
- getPort,
14
- defaultPort = DEFAULT_PORT,
15
- readModelsJson,
16
- gatewayTimeoutMs = 25000,
17
- env = process.env,
18
- } = {}) {
19
- const _fetch = fetchImpl;
20
- const _loadToken = loadToken || (async () => {
21
- try {
22
- const state = await import("../state.js");
23
- const loaded = await state.loadToken();
24
- return String(loaded?.token || "").trim();
25
- } catch { return ""; }
26
- });
27
- // 默认实现与 loadToken/readModelsJson 同款:懒加载 state(ESM 里没有 require)
28
- // Note: 这里曾用 require 导致 state.port 被静默忽略 — 见 .agents/notes/implemented/bug-fix/2026-09-17-esm-dead-require-and-orphan-cooling.md
29
- const _getPort = getPort || (async () => {
30
- try {
31
- const s = await import("../state.js");
32
- const p = s.getPort();
33
- if (Number.isInteger(p) && p > 0) return p;
34
- } catch {}
35
- const v = Number(env.MSLXDFF_PORT);
36
- if (Number.isInteger(v) && v > 0) return v;
37
- return defaultPort;
38
- });
39
- const _readModels = readModelsJson || (async () => {
40
- try {
41
- const { readFileSync, existsSync } = await import("node:fs");
42
- const { join } = await import("node:path");
43
- const { homedir } = await import("node:os");
44
- const cache = join(homedir(), ".config", "mslxdff", "models.json");
45
- if (existsSync(cache)) return JSON.parse(readFileSync(cache, "utf8"));
46
- } catch {}
47
- return { data: [] };
48
- });
49
-
50
- async function chatViaGateway({ messages, tools, autoProvider = "opencode" }) {
51
- const TRACE = env.MSLXDFF_CHAT_TRACE !== "0";
52
- const t0 = TRACE ? performance.now() : 0;
53
- let port = defaultPort;
54
- let token = "";
55
- try {
56
- token = String((await _loadToken()) || "").trim();
57
- const p = await _getPort();
58
- if (Number.isInteger(p) && p > 0) port = p;
59
- } catch {}
60
- if (!token) {
61
- try {
62
- const { readFileSync, existsSync } = await import("node:fs");
63
- const { join } = await import("node:path");
64
- const { homedir } = await import("node:os");
65
- const sf = env.MSLXDFF_STATE_FILE || join(homedir(), ".config", "mslxdff", "state.json");
66
- if (existsSync(sf)) {
67
- const j = JSON.parse(readFileSync(sf, "utf8"));
68
- if (typeof j.token === "string" && j.token.trim()) token = j.token.trim();
69
- }
70
- } catch {}
71
- }
72
- const url = `http://127.0.0.1:${port}/v1/chat/completions`;
73
- const body = { model: "auto", messages, stream: false };
74
- if (tools?.length) { body.tools = tools; body.tool_choice = "auto"; }
75
-
76
- try {
77
- const controller = new AbortController();
78
- const timer = setTimeout(() => controller.abort(), gatewayTimeoutMs);
79
- const res = await _fetch(url, {
80
- method: "POST",
81
- headers: {
82
- "Content-Type": "application/json",
83
- ...(autoProvider ? { "x-mslxdff-auto-provider": autoProvider } : {}),
84
- ...(token ? { Authorization: `Bearer ${token}` } : {}),
85
- },
86
- body: JSON.stringify(body),
87
- signal: controller.signal,
88
- });
89
- clearTimeout(timer);
90
- const txt = await res.text();
91
- let j;
92
- try { j = JSON.parse(txt); } catch {
93
- if (txt.includes("data:")) {
94
- try {
95
- const parsed = parseSse(txt);
96
- if (parsed.sseOk && (parsed.content || parsed.toolCalls.length)) {
97
- const hasToolCalls = parsed.toolCalls.length > 0;
98
- const hasContent = !!parsed.content;
99
- let finishReason = parsed.finishReason;
100
- if (hasToolCalls && !hasContent) finishReason = "tool_calls";
101
- const tool_calls = hasToolCalls ? parsed.toolCalls : undefined;
102
- const msg = { role: "assistant", content: parsed.content || "" };
103
- if (tool_calls) msg.tool_calls = tool_calls;
104
- j = { id: `sse-${Date.now()}`, object: "chat.completion", model: parsed.model, choices: [{ index: 0, finish_reason: finishReason, message: msg }], usage: parsed.usage };
105
- } else if (parsed.sseOk) {
106
- return { ok: false, error: `gateway SSE no content: ${txt.slice(0, 800)}`, status: res.status };
107
- } else {
108
- return { ok: false, error: `gateway non-json: ${txt.slice(0, 800)}`, status: res.status };
109
- }
110
- } catch {
111
- return { ok: false, error: `gateway non-json: ${txt.slice(0, 800)}`, status: res.status };
112
- }
113
- } else {
114
- return { ok: false, error: `gateway non-json: ${txt.slice(0, 800)}`, status: res.status };
115
- }
116
- }
117
- if (!res.ok) {
118
- const msg = j?.error?.message || j?.error || j?.data?.error?.message || j?.data?.error || txt.slice(0, 800);
119
- if (TRACE) console.log(`\x1b[90m· [LLM] gateway auto FAIL · ${Math.round(performance.now() - t0)}ms · HTTP ${res.status} ${String(msg).slice(0, 80)}\x1b[0m`);
120
- return { ok: false, error: msg, status: res.status };
121
- }
122
- const choice = j.choices?.[0] || j.data?.choices?.[0];
123
- const effectiveJ = j.choices ? j : (j.data?.choices ? j.data : j);
124
- if (!choice) {
125
- if (TRACE) console.log(`\x1b[90m· [gateway debug] no choice, txt=${txt.slice(0, 800)} · j=${JSON.stringify(j).slice(0, 800)}\x1b[0m`);
126
- return { ok: false, error: `gateway no choice: ${txt.slice(0, 800)}`, status: res.status };
127
- }
128
- let provider = "opencode";
129
- const rawModel = effectiveJ.model || j.model || choice.message?.model || "auto";
130
- try {
131
- const c = await _readModels();
132
- const ids = (c.data || []).map((x) => x.id).filter(Boolean);
133
- for (const pid of ids) {
134
- const slash = pid.indexOf("/");
135
- const prov = slash > 0 ? pid.slice(0, slash) : "opencode";
136
- const raw = slash > 0 ? pid.slice(slash + 1) : pid;
137
- if (pid === rawModel || raw === rawModel || pid.endsWith("/" + rawModel)) { provider = prov; break; }
138
- }
139
- if (provider === "opencode" && rawModel.includes("/")) {
140
- const maybe = rawModel.split("/")[0];
141
- if (["workbuddy", "cline", "sensenova", "openrouter", "generic"].includes(maybe)) provider = maybe;
142
- }
143
- } catch {}
144
- if (TRACE) {
145
- const dt = Math.round(performance.now() - t0);
146
- console.log(`\x1b[90m· [LLM] gateway auto OK · ${dt}ms · 模型 ${provider !== "opencode" ? provider + "/" : ""}${rawModel} · 总 ${dt}ms (gateway-fallback)\x1b[0m`);
147
- }
148
- return { ok: true, message: choice.message, usage: effectiveJ.usage || j.usage, raw: j, status: res.status, model: rawModel, provider, viaGateway: true };
149
- } catch (err) {
150
- const raw = String(err?.message || err);
151
- const msg = raw.slice(0, 800);
152
- const isConnRefused = /ECONNREFUSED|Failed to fetch|fetch failed|connect ECONNREFUSED|ECONNRESET|EADDRNOTAVAIL/i.test(raw);
153
- if (isConnRefused) {
154
- const friendly = `本地服务没有启动无法使用auto模式,请mslxdff -d 启动(本地网关 http://127.0.0.1:${port} 拒绝连接)— 3ms 内失败说明未触达任何模型(非模型额度问题);若已改端口请用 mslxdff -port N 或 MSLXDFF_PORT=${port} 保持一致`;
155
- if (TRACE) console.log(`\x1b[90m· [LLM] gateway auto FAIL · ${Math.round(performance.now() - t0)}ms · ${friendly} · ${msg.slice(0, 80)}\x1b[0m`);
156
- return { ok: false, error: friendly, status: 502, code: "GATEWAY_NOT_RUNNING", port, raw: msg };
157
- }
158
- return { ok: false, error: `gateway ${msg}`, status: 502 };
159
- }
160
- }
161
-
162
- return { chatViaGateway };
163
- }
1
+ import { performance } from "node:perf_hooks";
2
+ import { parseSse } from "./sse.js";
3
+ import { DEFAULT_PORT } from "../state.js";
4
+ import { compatFetch } from "../compat.js";
5
+
6
+ /**
7
+ * 网关深模块:POST 127.0.0.1:port/v1/chat/completions model:auto
8
+ * 注入化:fetch/loadToken/getPort/readModelsJson 均可伪,便于单测
9
+ */
10
+ export function createGatewayClient({
11
+ fetchImpl = compatFetch,
12
+ loadToken,
13
+ getPort,
14
+ defaultPort = DEFAULT_PORT,
15
+ readModelsJson,
16
+ gatewayTimeoutMs = 25000,
17
+ env = process.env,
18
+ } = {}) {
19
+ const _fetch = fetchImpl;
20
+ const _loadToken = loadToken || (async () => {
21
+ try {
22
+ const state = await import("../state.js");
23
+ const loaded = await state.loadToken();
24
+ return String(loaded?.token || "").trim();
25
+ } catch { return ""; }
26
+ });
27
+ // 默认实现与 loadToken/readModelsJson 同款:懒加载 state(ESM 里没有 require)
28
+ // Note: 这里曾用 require 导致 state.port 被静默忽略 — 见 .agents/notes/implemented/bug-fix/2026-09-17-esm-dead-require-and-orphan-cooling.md
29
+ const _getPort = getPort || (async () => {
30
+ try {
31
+ const s = await import("../state.js");
32
+ const p = s.getPort();
33
+ if (Number.isInteger(p) && p > 0) return p;
34
+ } catch {}
35
+ const v = Number(env.MSLXDFF_PORT);
36
+ if (Number.isInteger(v) && v > 0) return v;
37
+ return defaultPort;
38
+ });
39
+ const _readModels = readModelsJson || (async () => {
40
+ try {
41
+ const { readFileSync, existsSync } = await import("node:fs");
42
+ const { join } = await import("node:path");
43
+ const { homedir } = await import("node:os");
44
+ const cache = join(homedir(), ".config", "mslxdff", "models.json");
45
+ if (existsSync(cache)) return JSON.parse(readFileSync(cache, "utf8"));
46
+ } catch {}
47
+ return { data: [] };
48
+ });
49
+
50
+ async function chatViaGateway({ messages, tools, autoProvider = "opencode" }) {
51
+ const TRACE = env.MSLXDFF_CHAT_TRACE !== "0";
52
+ const t0 = TRACE ? performance.now() : 0;
53
+ let port = defaultPort;
54
+ let token = "";
55
+ try {
56
+ token = String((await _loadToken()) || "").trim();
57
+ const p = await _getPort();
58
+ if (Number.isInteger(p) && p > 0) port = p;
59
+ } catch {}
60
+ if (!token) {
61
+ try {
62
+ const { readFileSync, existsSync } = await import("node:fs");
63
+ const { join } = await import("node:path");
64
+ const { homedir } = await import("node:os");
65
+ const sf = env.MSLXDFF_STATE_FILE || join(homedir(), ".config", "mslxdff", "state.json");
66
+ if (existsSync(sf)) {
67
+ const j = JSON.parse(readFileSync(sf, "utf8"));
68
+ if (typeof j.token === "string" && j.token.trim()) token = j.token.trim();
69
+ }
70
+ } catch {}
71
+ }
72
+ const url = `http://127.0.0.1:${port}/v1/chat/completions`;
73
+ const body = { model: "auto", messages, stream: false };
74
+ if (tools?.length) { body.tools = tools; body.tool_choice = "auto"; }
75
+
76
+ try {
77
+ const controller = new AbortController();
78
+ const timer = setTimeout(() => controller.abort(), gatewayTimeoutMs);
79
+ const res = await _fetch(url, {
80
+ method: "POST",
81
+ headers: {
82
+ "Content-Type": "application/json",
83
+ ...(autoProvider ? { "x-mslxdff-auto-provider": autoProvider } : {}),
84
+ ...(token ? { Authorization: `Bearer ${token}` } : {}),
85
+ },
86
+ body: JSON.stringify(body),
87
+ signal: controller.signal,
88
+ });
89
+ clearTimeout(timer);
90
+ const txt = await res.text();
91
+ let j;
92
+ try { j = JSON.parse(txt); } catch {
93
+ if (txt.includes("data:")) {
94
+ try {
95
+ const parsed = parseSse(txt);
96
+ if (parsed.sseOk && (parsed.content || parsed.toolCalls.length)) {
97
+ const hasToolCalls = parsed.toolCalls.length > 0;
98
+ const hasContent = !!parsed.content;
99
+ let finishReason = parsed.finishReason;
100
+ if (hasToolCalls && !hasContent) finishReason = "tool_calls";
101
+ const tool_calls = hasToolCalls ? parsed.toolCalls : undefined;
102
+ const msg = { role: "assistant", content: parsed.content || "" };
103
+ if (tool_calls) msg.tool_calls = tool_calls;
104
+ j = { id: `sse-${Date.now()}`, object: "chat.completion", model: parsed.model, choices: [{ index: 0, finish_reason: finishReason, message: msg }], usage: parsed.usage };
105
+ } else if (parsed.sseOk) {
106
+ return { ok: false, error: `gateway SSE no content: ${txt.slice(0, 800)}`, status: res.status };
107
+ } else {
108
+ return { ok: false, error: `gateway non-json: ${txt.slice(0, 800)}`, status: res.status };
109
+ }
110
+ } catch {
111
+ return { ok: false, error: `gateway non-json: ${txt.slice(0, 800)}`, status: res.status };
112
+ }
113
+ } else {
114
+ return { ok: false, error: `gateway non-json: ${txt.slice(0, 800)}`, status: res.status };
115
+ }
116
+ }
117
+ if (!res.ok) {
118
+ const msg = j?.error?.message || j?.error || j?.data?.error?.message || j?.data?.error || txt.slice(0, 800);
119
+ if (TRACE) console.log(`\x1b[90m· [LLM] gateway auto FAIL · ${Math.round(performance.now() - t0)}ms · HTTP ${res.status} ${String(msg).slice(0, 80)}\x1b[0m`);
120
+ return { ok: false, error: msg, status: res.status };
121
+ }
122
+ const choice = j.choices?.[0] || j.data?.choices?.[0];
123
+ const effectiveJ = j.choices ? j : (j.data?.choices ? j.data : j);
124
+ if (!choice) {
125
+ if (TRACE) console.log(`\x1b[90m· [gateway debug] no choice, txt=${txt.slice(0, 800)} · j=${JSON.stringify(j).slice(0, 800)}\x1b[0m`);
126
+ return { ok: false, error: `gateway no choice: ${txt.slice(0, 800)}`, status: res.status };
127
+ }
128
+ let provider = "opencode";
129
+ const rawModel = effectiveJ.model || j.model || choice.message?.model || "auto";
130
+ try {
131
+ const c = await _readModels();
132
+ const ids = (c.data || []).map((x) => x.id).filter(Boolean);
133
+ for (const pid of ids) {
134
+ const slash = pid.indexOf("/");
135
+ const prov = slash > 0 ? pid.slice(0, slash) : "opencode";
136
+ const raw = slash > 0 ? pid.slice(slash + 1) : pid;
137
+ if (pid === rawModel || raw === rawModel || pid.endsWith("/" + rawModel)) { provider = prov; break; }
138
+ }
139
+ if (provider === "opencode" && rawModel.includes("/")) {
140
+ const maybe = rawModel.split("/")[0];
141
+ if (["workbuddy", "cline", "sensenova", "openrouter", "generic"].includes(maybe)) provider = maybe;
142
+ }
143
+ } catch {}
144
+ if (TRACE) {
145
+ const dt = Math.round(performance.now() - t0);
146
+ console.log(`\x1b[90m· [LLM] gateway auto OK · ${dt}ms · 模型 ${provider !== "opencode" ? provider + "/" : ""}${rawModel} · 总 ${dt}ms (gateway-fallback)\x1b[0m`);
147
+ }
148
+ return { ok: true, message: choice.message, usage: effectiveJ.usage || j.usage, raw: j, status: res.status, model: rawModel, provider, viaGateway: true };
149
+ } catch (err) {
150
+ const raw = String(err?.message || err);
151
+ const msg = raw.slice(0, 800);
152
+ const isConnRefused = /ECONNREFUSED|Failed to fetch|fetch failed|connect ECONNREFUSED|ECONNRESET|EADDRNOTAVAIL/i.test(raw);
153
+ if (isConnRefused) {
154
+ const friendly = `本地服务没有启动无法使用auto模式,请mslxdff -d 启动(本地网关 http://127.0.0.1:${port} 拒绝连接)— 3ms 内失败说明未触达任何模型(非模型额度问题);若已改端口请用 mslxdff -port N 或 MSLXDFF_PORT=${port} 保持一致`;
155
+ if (TRACE) console.log(`\x1b[90m· [LLM] gateway auto FAIL · ${Math.round(performance.now() - t0)}ms · ${friendly} · ${msg.slice(0, 80)}\x1b[0m`);
156
+ return { ok: false, error: friendly, status: 502, code: "GATEWAY_NOT_RUNNING", port, raw: msg };
157
+ }
158
+ return { ok: false, error: `gateway ${msg}`, status: 502 };
159
+ }
160
+ }
161
+
162
+ return { chatViaGateway };
163
+ }