import { nanoid } from 'nanoid'; import { agent } from './agent.ts'; import { ai } from './ai.ts'; /** * 清除文本中的@信息 * @param text */ const clearAtInfo = (text: string = '') => { const newText = text.replace(/@[\u4e00-\u9fa5\w]+/g, '').replace(/#.*?#/g, ''); return newText.trim(); }; agent .route({ path: 'xhs', }) .define(async (ctx) => { const { text = '' } = ctx.query || {}; const id = nanoid(); const no_at_text = clearAtInfo(text); const resFix = await agent.call({ path: 'fix', key: 'xhs', payload: { text: no_at_text, }, }); if (resFix.code !== 200) { ctx.throw(500, 'AI 小红书prompt优化错误: ' + resFix.message); return; } else { console.log('小红书优化的文本', resFix.body); } const prompt_text = resFix.body || ''; const res = await ai .chat( [ { role: 'user', content: prompt_text, }, ], { // @ts-ignore enable_thinking: false, }, ) .catch((error) => { ctx.throw(500, 'AI 服务错误: ' + error.status); return error; }); ctx.body = res.choices?.[0]?.message?.content || ''; }) .addTo(agent);