import { agent } from '@/agent/agent.ts'; import { ai } from '../ai.ts'; import { getJsonFromString } from './content.ts'; // import { getJsonFromString } from '@kevisual/ai/src/utils/json.ts'; import { logger } from '../logger.ts'; export const cmdList: { category: string; description?: string; action?: { path?: string; key?: string; }; }[] = [ { category: '指令夸人', description: `进行夸奖`, action: { path: 'tools', key: 'good-job', }, }, { category: '指令来了', description: `召唤小助手过来`, action: { path: 'tools', key: 'call-xiaoxiao', }, }, { category: '指令总结', description: `总结当前的笔记,缩写当前笔记的内容`, action: { path: 'tools', key: 'summarize-note', }, }, { category: '指令安慰', description: `安慰用户`, action: { path: 'tools', key: 'comfort-user', }, }, ]; agent .route({ path: 'analyze', key: 'cmd', description: '分析文本内容,意图分析,判断对应的指令内容', }) .define(async (ctx) => { let text = ctx.query?.text || ''; if (text.length > 40) { text = text.slice(0, 40).trim(); } let result = { category: 'default', }; const prompt = ` 请分析包函的内容,判断是否程序运行指令,返回一个JSON对象。 识别的分类包括: ${cmdList.map((item) => `- ${item.category}: ${item.description}`).join('\n')} 返回内容示例: \`\`\`json { "category": "daily_poetry" } \`\`\` 分析的内容是 ${text} `; logger.info('Command analysis prompt:', prompt); 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; }); const ans = res.choices[0]?.message?.content || ''; console.log('Command analysis response:', ans); 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); logger.info('Command analysis result:', cmd?.category, cmd?.action); ctx.body = { cmd, text: text }; }) .addTo(agent);