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);

View File

@ -1,9 +1,11 @@
import { agent } from './agent.ts';
import './analyze/content.ts';
import './analyze/category.ts';
import './analyze/cmd.ts';
import './fix/prompt.ts';
import './xhs.ts';
import './tools/kuaren.ts';
export { agent };

17
src/agent/test/cmd.ts Normal file
View File

@ -0,0 +1,17 @@
import { agent } from '../index.ts';
const main = async () => {
const text1 = '夸一下这个人。';
const text2 = '指令这个人很不错';
const text3 = '指令夸人, 这个人写代码写的非常好';
const text4 = '我想飞';
const res = await agent.call({
path: 'analyze',
key: 'cmd',
payload: {
text: text4
},
});
console.log('analyze category res', res.code, 'content', res.body);
};
main();

109
src/agent/tools/kuaren.ts Normal file
View File

@ -0,0 +1,109 @@
import { agent } from '../agent.ts';
import { ai } from '../ai.ts';
const kuarenPrompt = `### 浮夸夸人
****
vs
---
#### ** & **
1. **/**
- ****"你这张脸是上帝亲手雕的吧?下凡辛苦了!"
- ****
2. **/**
- ****"你这大脑是装了个量子计算机吗?!建议直接保送诺贝尔奖!"
- ****
3. **/**
- ****"你说话是撒了魔法金粉吗?听一句我灵魂都被净化了!"
- ****
4. **/**_重点_
- ****"你刚刚递咖啡的姿势,直接拍成广告能救活整个咖啡行业!"
- ****
---
#### ****
****
>
> 0.1
****
>
>
****
> XX XX
>
****_配合文字使用效果翻倍_
> .jpg 🍵
> .gif 🙇
---
#### ****
💥 ** 1**
> 线
💥 ** 2**
> 🌌
💥 ** 3**
> 🗿
####
`;
agent
.route({
path: 'tools',
key: 'good-job',
})
.define(async (ctx) => {
let { text } = ctx.query;
if (!text) {
text = '作者发了一篇好的文章';
}
const prompt = `${kuarenPrompt} ${text}`;
const res = await ai
.chat(
[
{
role: 'user',
content: prompt,
},
],
{
enable_thinking: false,
},
)
.catch((err) => {
console.error('AI service error:', err.status);
ctx.throw(500, 'AI service error: ' + err.status);
return err;
});
const ans = res.choices[0]?.message?.content || '';
if (!ans) {
ctx.throw(500, 'AI response is empty');
}
ctx.body = ans;
console.log('AI response:', ans);
})
.addTo(agent);

View File

@ -9,6 +9,34 @@ const clearAtInfo = (text: string = '') => {
const newText = text.replace(/@[\u4e00-\u9fa5\w]+/g, '').replace(/#.*?#/g, '');
return newText.trim();
};
const cmdList: {
cmd: string;
description?: string;
callPath?: {
path?: string;
key?: string;
};
}[] = [
{
cmd: '指令夸人',
description: `进行夸奖`,
callPath: {
path: 'tools',
key: 'good-job',
},
},
];
const getTextCmd = (text: string, cmdList = []) => {
const text20 = text.length > 20 ? text.slice(0, 20) : text;
const cmd = cmdList.find((item) => text20.includes(item.cmd));
if (cmd) {
return cmd;
}
return {
cmd: undefined,
};
};
agent
.route({
path: 'xhs',
@ -17,6 +45,21 @@ agent
const { text = '' } = ctx.query || {};
const id = nanoid();
const no_at_text = clearAtInfo(text);
const cmd = getTextCmd(no_at_text, cmdList);
if (cmd.cmd) {
const newText = no_at_text.replace(cmd.cmd, '').trim();
if (cmd.callPath) {
const res = await agent.call({
path: cmd.callPath.path,
key: cmd.callPath.key,
payload: {
text: newText,
},
});
ctx.body = res.body || '';
}
return;
}
const resFix = await agent.call({
path: 'fix',
key: 'xhs',