This commit is contained in:
2025-06-24 11:52:31 +08:00
parent a25f7c7eb4
commit b807cc9f38
6 changed files with 263 additions and 1 deletions

91
src/agent/analyze/cmd.ts Normal file
View File

@@ -0,0 +1,91 @@
import { agent } from '@/agent/agent.ts';
import { ai } from '../ai.ts';
import { getJsonFromString } from './content.ts';
import { logger } from '../logger.ts';
export const cmdList: {
category: string;
description?: string;
callPath?: {
path?: string;
key?: string;
};
}[] = [
{
category: '指令夸人',
description: `进行夸奖`,
callPath: {
path: 'tools',
key: 'good-job',
},
},
];
agent
.route({
path: 'analyze',
key: 'cmd',
description: '分析文本内容,意图分析,判断对应的指令内容',
})
.define(async (ctx) => {
const text = ctx.query?.text || '';
let result = {
category: 'default',
};
const prompt = `
请分析<context>包函的内容判断是否程序运行指令返回一个JSON对象。
识别的分类包括:
${cmdList.map((item) => `- ${item.category}: ${item.description}`).join('\n')}
返回内容示例:
\`\`\`json
{
"category": "daily_poetry"
}
\`\`\`
分析的内容是
<context>
${text}
</context>
`;
const now = Date.now();
console.log('start');
const res = await ai
.chat(
[
{
role: 'user',
content: prompt,
},
],
{
// @ts-ignore
enable_thinking: false,
},
)
.catch((err) => {
console.log('AI service error:', err.status);
ctx.throw(500, 'AI service error: ' + err.status);
return err;
});
console.log('end', Date.now() - now, 'ms');
const ans = res.choices[0]?.message?.content || '';
if (!ans) {
logger.error('Empty response from AI:', res);
}
const json = getJsonFromString(ans);
if (!json) {
logger.error('Invalid JSON format in response:', ans);
ctx.throw(400, 'Invalid JSON format in response');
}
result = {
category: json.category || 'default',
};
const cmd = cmdList.find((item) => item.category === result.category);
ctx.body = { cmd, text: text };
})
.addTo(agent);