alemonjs-aichat 1.0.19 → 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 +42 -7
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { API } from '@alemonjs/onebot';
|
|
2
2
|
import { emojiMap } from '../../emoji.js';
|
|
3
3
|
import { uploadImageToR2 } from '../../s3.js';
|
|
4
|
-
import { useMessage,
|
|
4
|
+
import { useMessage, Text, Mention, getConfigValue, ImageFile, ImageURL, useClient } from 'alemonjs';
|
|
5
5
|
import { log } from 'console';
|
|
6
6
|
import OpenAi from 'openai';
|
|
7
7
|
import { TTSClient } from '../../api/tts.js';
|
|
@@ -330,29 +330,64 @@ ${e.ClientError ? `当前错误信息:${e.ClientError}` : ""}
|
|
|
330
330
|
messages.push(responseMessage);
|
|
331
331
|
await redisClient.addAIChatHistory(e.guid, responseMessage);
|
|
332
332
|
console.log("模型决定调用工具:", responseMessage.tool_calls);
|
|
333
|
+
const useToolsMessage = responseMessage.tool_calls.map((toolCall) => {
|
|
334
|
+
return `工具调用: ${toolCall.function.name}(${toolCall.function.arguments})`;
|
|
335
|
+
});
|
|
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);
|
|
333
341
|
for (const toolCall of responseMessage.tool_calls) {
|
|
334
342
|
const functionName = toolCall.function.name;
|
|
335
|
-
|
|
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
|
+
}
|
|
336
353
|
const functionToCall = availableTools[functionName];
|
|
354
|
+
let functionResult;
|
|
337
355
|
if (!functionToCall) {
|
|
338
356
|
console.error(`未找到函数:${functionName}`);
|
|
339
|
-
|
|
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
|
+
}
|
|
340
374
|
}
|
|
341
|
-
|
|
342
|
-
|
|
375
|
+
const toolContent = JSON.stringify(typeof functionResult === "undefined"
|
|
376
|
+
? { ok: true, result: null }
|
|
377
|
+
: functionResult);
|
|
343
378
|
// 把工具执行结果作为 tool role 消息加回对话
|
|
344
379
|
messages.push({
|
|
345
380
|
role: "tool",
|
|
346
381
|
tool_call_id: toolCall.id,
|
|
347
382
|
name: functionName,
|
|
348
|
-
content:
|
|
383
|
+
content: toolContent,
|
|
349
384
|
});
|
|
350
385
|
// 记录工具调用结果
|
|
351
386
|
await redisClient.addAIChatHistory(e.guid, {
|
|
352
387
|
role: "tool",
|
|
353
388
|
tool_call_id: toolCall.id,
|
|
354
389
|
name: functionName,
|
|
355
|
-
content:
|
|
390
|
+
content: toolContent,
|
|
356
391
|
});
|
|
357
392
|
}
|
|
358
393
|
// 重新请求
|