mslxdff 0.1.186 → 0.1.188

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 (119) hide show
  1. package/bin/mslxdff.js +3 -3
  2. package/docs/cli_help_mini.md +145 -0
  3. package/package.json +1 -1
  4. package/src/auto.js +254 -254
  5. package/src/autostart.js +5 -2
  6. package/src/bench/cline-bench.js +42 -42
  7. package/src/bench/probe.js +70 -70
  8. package/src/bench/report.js +162 -162
  9. package/src/bench/runner.js +77 -77
  10. package/src/bench/via-probe.js +124 -124
  11. package/src/bench/via-routes.js +87 -87
  12. package/src/bench/workbuddy-bench.js +54 -54
  13. package/src/chat/engine.js +160 -160
  14. package/src/chat/gateway.js +163 -163
  15. package/src/chat/orchestrator.js +234 -234
  16. package/src/chat/prompt.js +70 -70
  17. package/src/chat/repl.js +88 -88
  18. package/src/chat/terminal.js +135 -135
  19. package/src/chat/tools.js +306 -306
  20. package/src/chat-pipeline/index.js +173 -131
  21. package/src/chat-pipeline/policy.js +84 -84
  22. package/src/chat-pipeline/serial-trial.js +6 -3
  23. package/src/chat-pipeline/terminal-verdict.js +29 -0
  24. package/src/cli/commands/daemon.js +55 -18
  25. package/src/cli/commands/group.js +249 -249
  26. package/src/cli/commands/model/picks.js +50 -50
  27. package/src/cli/commands/provider/bench-via.js +247 -247
  28. package/src/cli/commands/provider/bench.js +141 -141
  29. package/src/cli/commands/provider/index.js +136 -136
  30. package/src/cli/commands/provider/models.js +145 -145
  31. package/src/cli/commands/provider/qwenwork-login.js +119 -119
  32. package/src/cli/commands/sync.js +310 -310
  33. package/src/cli/commands/system.js +23 -7
  34. package/src/cli/join-wizard.js +8 -4
  35. package/src/cli/policy.js +14 -2
  36. package/src/cli/status.js +279 -279
  37. package/src/daemon.js +232 -195
  38. package/src/logs.js +27 -11
  39. package/src/model-capabilities/enrich.js +86 -86
  40. package/src/model-capabilities/index.js +183 -183
  41. package/src/model-capabilities/parse.js +70 -70
  42. package/src/model-trace.js +35 -4
  43. package/src/models.js +225 -225
  44. package/src/peers.js +20 -1
  45. package/src/providers/AGENTS.md +49 -0
  46. package/src/providers/cline/auth.js +228 -228
  47. package/src/providers/cline/chat.js +307 -307
  48. package/src/providers/cline.js +2 -2
  49. package/src/providers/codearts/chat.js +182 -182
  50. package/src/providers/globalqwenwork/cosy.js +123 -123
  51. package/src/providers/globalqwenwork/crypto.js +218 -218
  52. package/src/providers/globalqwenwork/http.js +20 -20
  53. package/src/providers/globalqwenwork/rsa.js +54 -54
  54. package/src/providers/globalqwenwork/sse.js +268 -268
  55. package/src/providers/globalqwenwork/stream.js +130 -130
  56. package/src/providers/globalqwenwork/upstream.js +122 -122
  57. package/src/providers/keyring.js +60 -60
  58. package/src/providers/qoder/chat.js +203 -203
  59. package/src/providers/qoder/index.js +249 -249
  60. package/src/providers/qoder/sse.js +145 -145
  61. package/src/providers/qwenwork/account-store.js +133 -133
  62. package/src/providers/qwenwork/constants.js +67 -67
  63. package/src/providers/qwenwork/cosy.js +120 -120
  64. package/src/providers/qwenwork/crypto.js +218 -218
  65. package/src/providers/qwenwork/http.js +20 -20
  66. package/src/providers/qwenwork/index.js +337 -327
  67. package/src/providers/qwenwork/payload.js +144 -144
  68. package/src/providers/qwenwork/rsa.js +54 -54
  69. package/src/providers/qwenwork/sse.js +268 -268
  70. package/src/providers/qwenwork/stream.js +130 -130
  71. package/src/providers/qwenwork/upstream.js +120 -120
  72. package/src/providers/qwenwork.js +1 -1
  73. package/src/providers/registry.js +79 -79
  74. package/src/providers/traework/chat.js +7 -1
  75. package/src/providers/workbuddy/chat.js +248 -248
  76. package/src/providers/workbuddy/reshape.js +152 -152
  77. package/src/providers/workbuddy.js +2 -2
  78. package/src/reasoning.js +32 -32
  79. package/src/routes/AGENTS.md +37 -0
  80. package/src/routes/chat/empty-turn-report.js +31 -0
  81. package/src/routes/chat/gateway.js +46 -46
  82. package/src/routes/chat/peer-handler.js +3 -2
  83. package/src/routes/chat/relay-pipeline.js +282 -294
  84. package/src/routes/chat/via-route-handler.js +146 -146
  85. package/src/routes/hedge.js +253 -255
  86. package/src/routes/index.js +14 -6
  87. package/src/routes/models-route.js +167 -167
  88. package/src/routes/peers.js +335 -273
  89. package/src/routes/responses-route.js +44 -2
  90. package/src/routes/stream-body.js +84 -0
  91. package/src/routes/stream-hold.js +152 -4
  92. package/src/routes/stream-scan.js +27 -2
  93. package/src/routes/stream.js +343 -385
  94. package/src/runtime/bootstrap.js +67 -67
  95. package/src/runtime/group-sync.js +70 -17
  96. package/src/runtime/provider-gate.js +36 -36
  97. package/src/runtime/providers-setup.js +168 -168
  98. package/src/runtime/server-lifecycle.js +1 -1
  99. package/src/server.js +78 -78
  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-engine/sdk/attempt.js +9 -1
  107. package/src/upstream-probe/display.js +52 -52
  108. package/src/upstream-probe/probe.js +49 -49
  109. package/src/upstream-probe/rotate.js +110 -110
  110. package/src/upstream-probe/start.js +45 -45
  111. package/src/upstream.js +293 -293
  112. package/src/v6pool/claim.js +157 -0
  113. package/src/v6pool/config.js +65 -0
  114. package/src/v6pool/detect.js +130 -0
  115. package/src/v6pool/index.js +152 -0
  116. package/src/v6pool/prefix.js +140 -0
  117. package/src/v6pool/probe.js +40 -0
  118. package/src/v6pool/report.js +104 -0
  119. package/src/v6pool/rotate.js +239 -0
@@ -1,160 +1,160 @@
1
- import { performance } from "node:perf_hooks";
2
- import { estimateChars, needsCompress } from "./store.js";
3
- import { CHAT_KEEP_RECENT, CHAT_MAX_TOOL_LOOPS } from "./config.js";
4
- import { buildDedupKey, runTool } from "./tool-handlers.js";
5
-
6
- // 纯引擎:无 readline/ANSI/spinner,单一 runTurn 可测
7
- export function createEngine({
8
- chatWithFallback,
9
- summarizeHistory,
10
- getToolDefs = () => [],
11
- execCommand,
12
- readFileTool,
13
- curlTool,
14
- onTrace = () => {},
15
- config = {},
16
- } = {}) {
17
- const CWF = chatWithFallback || (async () => ({ ok: false, error: "no chat" }));
18
- const SUM = summarizeHistory || (async () => null);
19
- const GTOOLS = getToolDefs;
20
- const EXEC = execCommand || (async () => ({ ok: false, output: "no exec" }));
21
- const READ = readFileTool || (async () => ({ ok: false, output: "no read" }));
22
- const CURL = curlTool || (async () => ({ ok: false, output: "no curl" }));
23
-
24
- async function maybeCompress(messages) {
25
- if (!needsCompress(messages)) return [...messages];
26
- const sys = messages[0];
27
- const rest = messages.slice(1);
28
- if (rest.length <= CHAT_KEEP_RECENT + 2) return [...messages];
29
- const t0 = performance.now();
30
- const toSummarize = rest.slice(0, -CHAT_KEEP_RECENT);
31
- const keep = rest.slice(-CHAT_KEEP_RECENT);
32
- const chars = estimateChars(messages);
33
- onTrace(`[压缩] 触发 ${chars}字 > 400000阈值 · 待压 ${toSummarize.length}条 保留 ${keep.length}条`);
34
- const summary = await SUM(toSummarize);
35
- const dt = Math.round(performance.now() - t0);
36
- if (!summary) {
37
- onTrace(`[压缩] 失败/空 · ${dt}ms → 截断`);
38
- return [sys, ...keep];
39
- }
40
- const summaryMsg = { role: "system", content: summary };
41
- const next = [sys, summaryMsg, ...keep];
42
- onTrace(`[压缩] 完成 ${toSummarize.length}条→${summary.length}字 · ${dt}ms · 新总量约 ${estimateChars(next)}字`);
43
- return next;
44
- }
45
-
46
- async function runTurn(userText, messages, modelOverride) {
47
- const tools = GTOOLS();
48
- messages.push({ role: "user", content: userText });
49
- let loops = 0;
50
- let lastModel = null;
51
- let lastProvider = null;
52
- let lastUsage = null;
53
- let lastFallback = false;
54
- let lastFallbackGateway = false;
55
- let lastLatency = 0;
56
- const t0 = performance.now();
57
- const turnStart = performance.now();
58
- const seenCalls = new Map();
59
- let duplicateStrikes = 0;
60
- let forceNoTools = false;
61
- onTrace(`[turn] 开始 "${userText.slice(0, 60)}${userText.length > 60 ? "…" : ""}" · 历史 ${messages.length}条 约 ${estimateChars(messages)}字`);
62
- while (loops < CHAT_MAX_TOOL_LOOPS) {
63
- const tLoop = performance.now();
64
- const tComp = performance.now();
65
- const cur = await maybeCompress(messages);
66
- const compressMs = Math.round(performance.now() - tComp);
67
- if (compressMs > 50) onTrace(`[loop ${loops}] 压缩耗时 ${compressMs}ms`);
68
- messages.length = 0;
69
- for (const m of cur) messages.push(m);
70
- const tCall = performance.now();
71
- let res;
72
- const activeTools = forceNoTools ? [] : tools;
73
- res = await CWF({ messages, tools: activeTools, model: modelOverride || undefined });
74
- if (forceNoTools && res.ok && res.message?.tool_calls?.length) {
75
- onTrace(`[guard] 禁工具模式下仍收到 tool_calls,已拦截`);
76
- res.message.tool_calls = [];
77
- if (!res.message.content) res.message.content = "(已拦截违规工具调用,请基于已有结果直接回答)";
78
- }
79
- const llmMs = Math.round(performance.now() - tCall);
80
- onTrace(`[loop ${loops}] LLM ${llmMs}ms${compressMs > 50 ? ` (含压缩 ${compressMs}ms)` : ""} · ${estimateChars(messages)}字上下文`);
81
- lastLatency = llmMs;
82
- if (!res.ok) {
83
- const err = `大模型暂不可用:${res.error}`;
84
- messages.push({ role: "assistant", content: err });
85
- return { text: err, model: null, latency: lastLatency, usage: null, fallback: false, ok: false };
86
- }
87
- lastModel = res.model;
88
- lastProvider = res.provider || null;
89
- lastUsage = res.usage || null;
90
- lastFallback = !!res.fallback;
91
- lastFallbackGateway = !!res.fallbackGateway || !!res.viaGateway;
92
- const msg = res.message;
93
- const toolCalls = msg.tool_calls || [];
94
- let fallbackCmd = null;
95
- if (!toolCalls.length && msg.content) {
96
- const m = String(msg.content).match(/\{[^}]*"command"\s*:\s*"([^"]+)"[^}]*\}/);
97
- if (m) fallbackCmd = m[1];
98
- }
99
- if (!toolCalls.length && !fallbackCmd) {
100
- const text = String(msg.content || "").trim() || "(空回复)";
101
- messages.push({ role: "assistant", content: text });
102
- const totalMs = Math.round(performance.now() - t0);
103
- let note = "";
104
- if (lastFallbackGateway) note = "\n[注:mimo/big-pickle 均不可用,已自动切本地网关 auto(:8989)]";
105
- else if (lastFallback) note = "\n[注:mimo 不可用,已用 big-pickle]";
106
- onTrace(`[turn] 完成 总计 ${totalMs}ms · LLM ${lastLatency}ms · 0 工具${lastFallbackGateway ? " · gateway-fallback" : ""}`);
107
- return { text: text + note, model: lastModel, provider: lastProvider, latency: lastLatency, usage: lastUsage, fallback: lastFallback, fallbackGateway: lastFallbackGateway, ok: true, totalMs };
108
- }
109
- const calls = toolCalls.length ? toolCalls : [{ id: "fallback-1", function: { name: "run_command", arguments: JSON.stringify({ command: fallbackCmd }) } }];
110
- messages.push({ role: "assistant", content: msg.content || "", tool_calls: calls.map((c) => ({ id: c.id, type: "function", function: c.function })) });
111
- onTrace(`[tools] 本轮 ${calls.length} 个调用 ${calls.map((c) => c.function?.name).join(",")} · 顺序执行`);
112
- const tTools = performance.now();
113
- const toolResults = [];
114
- const toolDeps = { execCommand: EXEC, readFileTool: READ, curlTool: CURL, onTrace };
115
- for (const c of calls) {
116
- const name = c.function?.name;
117
- let args = {};
118
- try { args = JSON.parse(c.function?.arguments || "{}"); } catch {}
119
- const t1 = performance.now();
120
- const dedupKey = buildDedupKey(name, args);
121
- const seen = seenCalls.get(dedupKey);
122
- if (seen) {
123
- const dt = Math.round(performance.now() - t1);
124
- onTrace(`[tool] ${name} 重复调用已跳过 · ${dt}ms · 之前 ${seen.count} 次`);
125
- toolResults.push({
126
- id: c.id,
127
- content: `SKIPPED_DUP: 此工具调用在本轮已执行过 ${seen.count} 次,结果相同请直接基于已有信息回答用户,不要再重复调用。\n--- 首次结果复用 ---\n${seen.firstResult.slice(0, 6000)}`,
128
- });
129
- continue;
130
- }
131
- const result = await runTool({ name, args, userText, ...toolDeps });
132
- if (!seenCalls.has(dedupKey)) seenCalls.set(dedupKey, { count: 1, firstResult: result });
133
- else seenCalls.get(dedupKey).count++;
134
- toolResults.push({ id: c.id, content: result });
135
- }
136
- for (const tr of toolResults) messages.push({ role: "tool", tool_call_id: tr.id, content: tr.content });
137
- if (toolResults.some((tr) => String(tr.content).startsWith("SKIPPED_DUP"))) {
138
- duplicateStrikes++;
139
- forceNoTools = true;
140
- messages.push({ role: "system", content: "系统提示:你已重复调用相同工具,工具侧已复用首次结果并跳过执行。你已被禁止再调用任何工具,必须立即基于以上工具结果用中文直接回答用户,0 工具调用。" });
141
- onTrace(`[dup] 检测到重复调用 ${duplicateStrikes} 次,已禁用后续工具调用`);
142
- if (duplicateStrikes >= 2) {
143
- const seen = [...seenCalls.values()].map((v) => v.firstResult).join("\n---\n").slice(0, 6000);
144
- const synth = `检测到重复调用已达 ${duplicateStrikes} 次,为避免空转,直接基于已有结果回答:\n\n${seen}`;
145
- messages.push({ role: "assistant", content: synth });
146
- const totalMs = Math.round(performance.now() - t0);
147
- onTrace(`[turn] 提前结束(重复阈值) 总计 ${totalMs}ms`);
148
- return { text: synth, model: lastModel || "local", provider: lastProvider, latency: lastLatency, usage: lastUsage, fallback: lastFallback, ok: true, totalMs };
149
- }
150
- }
151
- const toolsMs = Math.round(performance.now() - tTools);
152
- const loopMs = Math.round(performance.now() - tLoop);
153
- onTrace(`[loop ${loops}] 工具 ${toolsMs}ms · 本轮总 ${loopMs}ms · 累计 ${Math.round(performance.now() - turnStart)}ms`);
154
- loops++;
155
- }
156
- return { text: "(工具调用次数已达上限,已停止)", model: lastModel, latency: lastLatency, usage: lastUsage, fallback: lastFallback, fallbackGateway: lastFallbackGateway, ok: false };
157
- }
158
-
159
- return { runTurn, maybeCompress };
160
- }
1
+ import { performance } from "node:perf_hooks";
2
+ import { estimateChars, needsCompress } from "./store.js";
3
+ import { CHAT_KEEP_RECENT, CHAT_MAX_TOOL_LOOPS } from "./config.js";
4
+ import { buildDedupKey, runTool } from "./tool-handlers.js";
5
+
6
+ // 纯引擎:无 readline/ANSI/spinner,单一 runTurn 可测
7
+ export function createEngine({
8
+ chatWithFallback,
9
+ summarizeHistory,
10
+ getToolDefs = () => [],
11
+ execCommand,
12
+ readFileTool,
13
+ curlTool,
14
+ onTrace = () => {},
15
+ config = {},
16
+ } = {}) {
17
+ const CWF = chatWithFallback || (async () => ({ ok: false, error: "no chat" }));
18
+ const SUM = summarizeHistory || (async () => null);
19
+ const GTOOLS = getToolDefs;
20
+ const EXEC = execCommand || (async () => ({ ok: false, output: "no exec" }));
21
+ const READ = readFileTool || (async () => ({ ok: false, output: "no read" }));
22
+ const CURL = curlTool || (async () => ({ ok: false, output: "no curl" }));
23
+
24
+ async function maybeCompress(messages) {
25
+ if (!needsCompress(messages)) return [...messages];
26
+ const sys = messages[0];
27
+ const rest = messages.slice(1);
28
+ if (rest.length <= CHAT_KEEP_RECENT + 2) return [...messages];
29
+ const t0 = performance.now();
30
+ const toSummarize = rest.slice(0, -CHAT_KEEP_RECENT);
31
+ const keep = rest.slice(-CHAT_KEEP_RECENT);
32
+ const chars = estimateChars(messages);
33
+ onTrace(`[压缩] 触发 ${chars}字 > 400000阈值 · 待压 ${toSummarize.length}条 保留 ${keep.length}条`);
34
+ const summary = await SUM(toSummarize);
35
+ const dt = Math.round(performance.now() - t0);
36
+ if (!summary) {
37
+ onTrace(`[压缩] 失败/空 · ${dt}ms → 截断`);
38
+ return [sys, ...keep];
39
+ }
40
+ const summaryMsg = { role: "system", content: summary };
41
+ const next = [sys, summaryMsg, ...keep];
42
+ onTrace(`[压缩] 完成 ${toSummarize.length}条→${summary.length}字 · ${dt}ms · 新总量约 ${estimateChars(next)}字`);
43
+ return next;
44
+ }
45
+
46
+ async function runTurn(userText, messages, modelOverride) {
47
+ const tools = GTOOLS();
48
+ messages.push({ role: "user", content: userText });
49
+ let loops = 0;
50
+ let lastModel = null;
51
+ let lastProvider = null;
52
+ let lastUsage = null;
53
+ let lastFallback = false;
54
+ let lastFallbackGateway = false;
55
+ let lastLatency = 0;
56
+ const t0 = performance.now();
57
+ const turnStart = performance.now();
58
+ const seenCalls = new Map();
59
+ let duplicateStrikes = 0;
60
+ let forceNoTools = false;
61
+ onTrace(`[turn] 开始 "${userText.slice(0, 60)}${userText.length > 60 ? "…" : ""}" · 历史 ${messages.length}条 约 ${estimateChars(messages)}字`);
62
+ while (loops < CHAT_MAX_TOOL_LOOPS) {
63
+ const tLoop = performance.now();
64
+ const tComp = performance.now();
65
+ const cur = await maybeCompress(messages);
66
+ const compressMs = Math.round(performance.now() - tComp);
67
+ if (compressMs > 50) onTrace(`[loop ${loops}] 压缩耗时 ${compressMs}ms`);
68
+ messages.length = 0;
69
+ for (const m of cur) messages.push(m);
70
+ const tCall = performance.now();
71
+ let res;
72
+ const activeTools = forceNoTools ? [] : tools;
73
+ res = await CWF({ messages, tools: activeTools, model: modelOverride || undefined });
74
+ if (forceNoTools && res.ok && res.message?.tool_calls?.length) {
75
+ onTrace(`[guard] 禁工具模式下仍收到 tool_calls,已拦截`);
76
+ res.message.tool_calls = [];
77
+ if (!res.message.content) res.message.content = "(已拦截违规工具调用,请基于已有结果直接回答)";
78
+ }
79
+ const llmMs = Math.round(performance.now() - tCall);
80
+ onTrace(`[loop ${loops}] LLM ${llmMs}ms${compressMs > 50 ? ` (含压缩 ${compressMs}ms)` : ""} · ${estimateChars(messages)}字上下文`);
81
+ lastLatency = llmMs;
82
+ if (!res.ok) {
83
+ const err = `大模型暂不可用:${res.error}`;
84
+ messages.push({ role: "assistant", content: err });
85
+ return { text: err, model: null, latency: lastLatency, usage: null, fallback: false, ok: false };
86
+ }
87
+ lastModel = res.model;
88
+ lastProvider = res.provider || null;
89
+ lastUsage = res.usage || null;
90
+ lastFallback = !!res.fallback;
91
+ lastFallbackGateway = !!res.fallbackGateway || !!res.viaGateway;
92
+ const msg = res.message;
93
+ const toolCalls = msg.tool_calls || [];
94
+ let fallbackCmd = null;
95
+ if (!toolCalls.length && msg.content) {
96
+ const m = String(msg.content).match(/\{[^}]*"command"\s*:\s*"([^"]+)"[^}]*\}/);
97
+ if (m) fallbackCmd = m[1];
98
+ }
99
+ if (!toolCalls.length && !fallbackCmd) {
100
+ const text = String(msg.content || "").trim() || "(空回复)";
101
+ messages.push({ role: "assistant", content: text });
102
+ const totalMs = Math.round(performance.now() - t0);
103
+ let note = "";
104
+ if (lastFallbackGateway) note = "\n[注:mimo/big-pickle 均不可用,已自动切本地网关 auto(:8989)]";
105
+ else if (lastFallback) note = "\n[注:mimo 不可用,已用 big-pickle]";
106
+ onTrace(`[turn] 完成 总计 ${totalMs}ms · LLM ${lastLatency}ms · 0 工具${lastFallbackGateway ? " · gateway-fallback" : ""}`);
107
+ return { text: text + note, model: lastModel, provider: lastProvider, latency: lastLatency, usage: lastUsage, fallback: lastFallback, fallbackGateway: lastFallbackGateway, ok: true, totalMs };
108
+ }
109
+ const calls = toolCalls.length ? toolCalls : [{ id: "fallback-1", function: { name: "run_command", arguments: JSON.stringify({ command: fallbackCmd }) } }];
110
+ messages.push({ role: "assistant", content: msg.content || "", tool_calls: calls.map((c) => ({ id: c.id, type: "function", function: c.function })) });
111
+ onTrace(`[tools] 本轮 ${calls.length} 个调用 ${calls.map((c) => c.function?.name).join(",")} · 顺序执行`);
112
+ const tTools = performance.now();
113
+ const toolResults = [];
114
+ const toolDeps = { execCommand: EXEC, readFileTool: READ, curlTool: CURL, onTrace };
115
+ for (const c of calls) {
116
+ const name = c.function?.name;
117
+ let args = {};
118
+ try { args = JSON.parse(c.function?.arguments || "{}"); } catch {}
119
+ const t1 = performance.now();
120
+ const dedupKey = buildDedupKey(name, args);
121
+ const seen = seenCalls.get(dedupKey);
122
+ if (seen) {
123
+ const dt = Math.round(performance.now() - t1);
124
+ onTrace(`[tool] ${name} 重复调用已跳过 · ${dt}ms · 之前 ${seen.count} 次`);
125
+ toolResults.push({
126
+ id: c.id,
127
+ content: `SKIPPED_DUP: 此工具调用在本轮已执行过 ${seen.count} 次,结果相同请直接基于已有信息回答用户,不要再重复调用。\n--- 首次结果复用 ---\n${seen.firstResult.slice(0, 6000)}`,
128
+ });
129
+ continue;
130
+ }
131
+ const result = await runTool({ name, args, userText, ...toolDeps });
132
+ if (!seenCalls.has(dedupKey)) seenCalls.set(dedupKey, { count: 1, firstResult: result });
133
+ else seenCalls.get(dedupKey).count++;
134
+ toolResults.push({ id: c.id, content: result });
135
+ }
136
+ for (const tr of toolResults) messages.push({ role: "tool", tool_call_id: tr.id, content: tr.content });
137
+ if (toolResults.some((tr) => String(tr.content).startsWith("SKIPPED_DUP"))) {
138
+ duplicateStrikes++;
139
+ forceNoTools = true;
140
+ messages.push({ role: "system", content: "系统提示:你已重复调用相同工具,工具侧已复用首次结果并跳过执行。你已被禁止再调用任何工具,必须立即基于以上工具结果用中文直接回答用户,0 工具调用。" });
141
+ onTrace(`[dup] 检测到重复调用 ${duplicateStrikes} 次,已禁用后续工具调用`);
142
+ if (duplicateStrikes >= 2) {
143
+ const seen = [...seenCalls.values()].map((v) => v.firstResult).join("\n---\n").slice(0, 6000);
144
+ const synth = `检测到重复调用已达 ${duplicateStrikes} 次,为避免空转,直接基于已有结果回答:\n\n${seen}`;
145
+ messages.push({ role: "assistant", content: synth });
146
+ const totalMs = Math.round(performance.now() - t0);
147
+ onTrace(`[turn] 提前结束(重复阈值) 总计 ${totalMs}ms`);
148
+ return { text: synth, model: lastModel || "local", provider: lastProvider, latency: lastLatency, usage: lastUsage, fallback: lastFallback, ok: true, totalMs };
149
+ }
150
+ }
151
+ const toolsMs = Math.round(performance.now() - tTools);
152
+ const loopMs = Math.round(performance.now() - tLoop);
153
+ onTrace(`[loop ${loops}] 工具 ${toolsMs}ms · 本轮总 ${loopMs}ms · 累计 ${Math.round(performance.now() - turnStart)}ms`);
154
+ loops++;
155
+ }
156
+ return { text: "(工具调用次数已达上限,已停止)", model: lastModel, latency: lastLatency, usage: lastUsage, fallback: lastFallback, fallbackGateway: lastFallbackGateway, ok: false };
157
+ }
158
+
159
+ return { runTurn, maybeCompress };
160
+ }