xiaozhou-chat 1.0.9 → 1.0.10
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/lib/chat.js +35 -2
- package/package.json +1 -1
package/lib/chat.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { Spinner, StreamPrinter } from "./ui.js";
|
|
3
3
|
import { sleep } from "./utils.js";
|
|
4
4
|
import { updateConfig, setProfileValue } from "./config.js";
|
|
5
|
+
import { builtInTools } from "./tools.js";
|
|
5
6
|
|
|
6
7
|
// 尝试加载 Markdown 渲染库
|
|
7
8
|
let marked;
|
|
@@ -272,14 +273,46 @@ export async function chatStream(context, userInput = null, options = {}) {
|
|
|
272
273
|
|
|
273
274
|
// Handle Tool Calls
|
|
274
275
|
if (toolCalls.length > 0) {
|
|
276
|
+
// 提取已知工具名
|
|
277
|
+
const knownToolNames = builtInTools.map(t => t.function.name);
|
|
278
|
+
|
|
275
279
|
for (const tc of toolCalls) {
|
|
276
|
-
|
|
280
|
+
let funcName = tc.function.name;
|
|
281
|
+
|
|
282
|
+
// 自动修正工具名粘连 (例如: read_fileread_file -> read_file)
|
|
283
|
+
if (!knownToolNames.includes(funcName)) {
|
|
284
|
+
// 按长度降序排序,优先匹配更长的工具名
|
|
285
|
+
const matched = knownToolNames
|
|
286
|
+
.sort((a, b) => b.length - a.length)
|
|
287
|
+
.find(name => funcName.includes(name));
|
|
288
|
+
|
|
289
|
+
if (matched) {
|
|
290
|
+
console.log(`⚠️ 检测到工具名异常 "${funcName}",自动修正为 "${matched}"`);
|
|
291
|
+
funcName = matched;
|
|
292
|
+
tc.function.name = matched; // 修正原始对象,这对后续消息历史至关重要
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
277
296
|
const argsStr = tc.function.arguments;
|
|
278
297
|
console.log(`🛠️ 调用工具: ${funcName}(${argsStr})`);
|
|
279
298
|
|
|
280
299
|
let result = null;
|
|
281
300
|
try {
|
|
282
|
-
|
|
301
|
+
let args;
|
|
302
|
+
try {
|
|
303
|
+
args = JSON.parse(argsStr);
|
|
304
|
+
} catch (e) {
|
|
305
|
+
// 尝试修复常见的 JSON 粘连问题 (例如: {"a":1}{"b":2})
|
|
306
|
+
if (argsStr.includes("}{")) {
|
|
307
|
+
console.log("⚠️ 检测到 JSON 粘连,尝试修复...");
|
|
308
|
+
// 简单策略:只取第一个 JSON
|
|
309
|
+
const fixStr = argsStr.split("}{")[0] + "}";
|
|
310
|
+
args = JSON.parse(fixStr);
|
|
311
|
+
} else {
|
|
312
|
+
throw e;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
283
316
|
// 1. Try built-in
|
|
284
317
|
result = await toolHandlers(funcName, args);
|
|
285
318
|
|