mslxdff 0.1.156 → 0.1.159

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