alemonjs-aichat 1.0.20 → 1.0.21
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/response/zreply/res.js +38 -7
- package/package.json +1 -1
|
@@ -333,30 +333,61 @@ ${e.ClientError ? `当前错误信息:${e.ClientError}` : ""}
|
|
|
333
333
|
const useToolsMessage = responseMessage.tool_calls.map((toolCall) => {
|
|
334
334
|
return `工具调用: ${toolCall.function.name}(${toolCall.function.arguments})`;
|
|
335
335
|
});
|
|
336
|
-
message.send(format(Text(useToolsMessage.join("\n"))));
|
|
336
|
+
const msgId = await message.send(format(Text(useToolsMessage.join("\n"))));
|
|
337
|
+
// 30秒后撤回消息
|
|
338
|
+
setTimeout(() => {
|
|
339
|
+
message.delete({ messageId: msgId[0].data.message_id });
|
|
340
|
+
}, 30000);
|
|
337
341
|
for (const toolCall of responseMessage.tool_calls) {
|
|
338
342
|
const functionName = toolCall.function.name;
|
|
339
|
-
|
|
343
|
+
let functionArgs = {};
|
|
344
|
+
try {
|
|
345
|
+
functionArgs = toolCall.function.arguments
|
|
346
|
+
? JSON.parse(toolCall.function.arguments)
|
|
347
|
+
: {};
|
|
348
|
+
}
|
|
349
|
+
catch (err) {
|
|
350
|
+
console.error(`工具参数解析失败:${functionName}`, err);
|
|
351
|
+
functionArgs = {};
|
|
352
|
+
}
|
|
340
353
|
const functionToCall = availableTools[functionName];
|
|
354
|
+
let functionResult;
|
|
341
355
|
if (!functionToCall) {
|
|
342
356
|
console.error(`未找到函数:${functionName}`);
|
|
343
|
-
|
|
357
|
+
functionResult = {
|
|
358
|
+
ok: false,
|
|
359
|
+
error: `未找到函数:${functionName}`,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
try {
|
|
364
|
+
// 执行本地函数
|
|
365
|
+
functionResult = await functionToCall(functionArgs);
|
|
366
|
+
}
|
|
367
|
+
catch (err) {
|
|
368
|
+
console.error(`工具执行失败:${functionName}`, err);
|
|
369
|
+
functionResult = {
|
|
370
|
+
ok: false,
|
|
371
|
+
error: err?.message || String(err) || "工具执行失败",
|
|
372
|
+
};
|
|
373
|
+
}
|
|
344
374
|
}
|
|
345
|
-
|
|
346
|
-
|
|
375
|
+
const toolContent = JSON.stringify(typeof functionResult === "undefined"
|
|
376
|
+
? { ok: true, result: null }
|
|
377
|
+
: functionResult);
|
|
347
378
|
// 把工具执行结果作为 tool role 消息加回对话
|
|
348
379
|
messages.push({
|
|
349
380
|
role: "tool",
|
|
350
381
|
tool_call_id: toolCall.id,
|
|
351
382
|
name: functionName,
|
|
352
|
-
content:
|
|
383
|
+
content: toolContent,
|
|
353
384
|
});
|
|
354
385
|
// 记录工具调用结果
|
|
355
386
|
await redisClient.addAIChatHistory(e.guid, {
|
|
356
387
|
role: "tool",
|
|
357
388
|
tool_call_id: toolCall.id,
|
|
358
389
|
name: functionName,
|
|
359
|
-
content:
|
|
390
|
+
content: toolContent,
|
|
360
391
|
});
|
|
361
392
|
}
|
|
362
393
|
// 重新请求
|