From da6d4041ad4b032bb5eba81c2c5cce0bdf2bfca6 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sat, 21 Jun 2025 18:21:49 +0800 Subject: [PATCH] perf --- packages/xhs/src/app.ts | 8 +-- packages/xhs/src/index.ts | 4 +- packages/xhs/src/libs/xhs.ts | 3 + packages/xhs/src/services/xhs-services.ts | 4 +- src/agent/analyze/category.ts | 80 +++++++++++++++++++++++ src/agent/analyze/content.ts | 2 +- src/agent/index.ts | 2 + src/agent/test/category.ts | 21 ++++++ src/task/task.ts | 9 ++- 9 files changed, 121 insertions(+), 12 deletions(-) create mode 100644 src/agent/analyze/category.ts create mode 100644 src/agent/test/category.ts diff --git a/packages/xhs/src/app.ts b/packages/xhs/src/app.ts index 80af588..ed0788e 100644 --- a/packages/xhs/src/app.ts +++ b/packages/xhs/src/app.ts @@ -1,13 +1,13 @@ import { QueryRouterServer } from '@kevisual/router/browser'; -import { XhsServices } from '@kevisual/xhs/services/xhs-services.ts'; +import { XhsServices, XhsClient } from '@kevisual/xhs/services/xhs-services.ts'; export const app = new QueryRouterServer(); -export const xhsServices = new XhsServices();// Semicolon separated Cookie File +export const xhsServices = new XhsServices(); // Semicolon separated Cookie File const cookie = - 'a1=1978d0cdcb7p5neac7cesgfm9yat0b4a7hnesexkp30000220066;abRequestId=f98f27d6-cceb-53d9-a9ef-dbd68786231b;access-token-creator.xiaohongshu.com=customer.creator.AT-68c517518075239703025713copo06lnusenqic1;customer-sso-sid=68c517518075239698553486mipyx4lwk2ac5jaj;customerClientId=716985104518687;galaxy_creator_session_id=wda55FJhoiAWZ8cekT2aGiM9fuztft3mOdRM;galaxy.creator.beaker.session.id=1750438297110093146037;gid=yjWYf8Si4SWjyjWYf8SfSxdCDWU2Id0SWSdMITUki6jv0dq898D40M888JJ88KK8fdKS8jiy;loadts=1750444792784;sec_poison_id=2f592adb-ec34-48cd-a6ce-22b90bd67c3b;unread={%22ub%22:%226852a4100000000022030063%22%2C%22ue%22:%226854e0db00000000100251f6%22%2C%22uc%22:28};web_session=040069b6528dbc23c355980a603a4b3e03bb6a;webBuild=4.68.0;webId=1dbb23b746393db622165a22357897d5;websectiga=10f9a40ba454a07755a08f27ef8194c53637eba4551cf9751c009d9afb564467;x-user-id-creator.xiaohongshu.com=6726cef4000000001c019303;xsecappid=xhs-pc-web;acw_tc=0a00d41117504447915317110e41effee63c597aa77f2c9f0dc1b0a10248f8;'; + 'a1=****;abRequestId=f98f27d6-cceb-53d9-a9ef-dbd68786231b;access-token-creator.xiaohongshu.com=customer.creator.AT-68c517518075239703025713copo06lnusenqic1;customer-sso-sid=68c517518075239698553486mipyx4lwk2ac5jaj;customerClientId=716985104518687;galaxy_creator_session_id=wda55FJhoiAWZ8cekT2aGiM9fuztft3mOdRM;galaxy.creator.beaker.session.id=1750438297110093146037;gid=yjWYf8Si4SWjyjWYf8SfSxdCDWU2Id0SWSdMITUki6jv0dq898D40M888JJ88KK8fdKS8jiy;loadts=1750444792784;sec_poison_id=2f592adb-ec34-48cd-a6ce-22b90bd67c3b;unread={%22ub%22:%226852a4100000000022030063%22%2C%22ue%22:%226854e0db00000000100251f6%22%2C%22uc%22:28};web_session=040069b6528dbc23c355980a603a4b3e03bb6a;webBuild=4.68.0;webId=1dbb23b746393db622165a22357897d5;websectiga=10f9a40ba454a07755a08f27ef8194c53637eba4551cf9751c009d9afb564467;x-user-id-creator.xiaohongshu.com=6726cef4000000001c019303;xsecappid=xhs-pc-web;acw_tc=0a00d41117504447915317110e41effee63c597aa77f2c9f0dc1b0a10248f8;'; -xhsServices.createRoot({ +export const xhsRootClient: XhsClient = xhsServices.createRoot({ cookie, signConfig: { signUrl: 'http://light.xiongxiao.me:5006/sign', diff --git a/packages/xhs/src/index.ts b/packages/xhs/src/index.ts index d5fde62..4e0074d 100644 --- a/packages/xhs/src/index.ts +++ b/packages/xhs/src/index.ts @@ -1,5 +1,5 @@ import { XhsClient } from './libs/xhs.ts'; -import { app, xhsServices } from './app.ts'; +import { app, xhsServices, xhsRootClient } from './app.ts'; import './routes/index.ts'; -export { XhsClient, app, xhsServices }; +export { XhsClient, app, xhsServices, xhsRootClient }; diff --git a/packages/xhs/src/libs/xhs.ts b/packages/xhs/src/libs/xhs.ts index 595595a..5ea7687 100644 --- a/packages/xhs/src/libs/xhs.ts +++ b/packages/xhs/src/libs/xhs.ts @@ -66,6 +66,9 @@ export class XhsClient extends XhsClientBase { constructor(opts: XhsOptions) { super(opts as any); } + setCookie(cookie: string) { + this.cookie = cookie; + } getApiInfo = getApiInfo; printResult(msg: string, data: any) { if (msg === 'response') { diff --git a/packages/xhs/src/services/xhs-services.ts b/packages/xhs/src/services/xhs-services.ts index 86cf14a..0396528 100644 --- a/packages/xhs/src/services/xhs-services.ts +++ b/packages/xhs/src/services/xhs-services.ts @@ -3,7 +3,7 @@ import { Sequelize } from 'sequelize'; // import { createSequelize } from '@kevisual/xhs/services/xhs-db/db.ts'; import path from 'node:path'; import fs from 'node:fs'; - +export { XhsClient }; type XhsClientOptions = { key: string; cookie: string; @@ -69,7 +69,7 @@ export class XhsServices { createRoot(options: Partial) { options.key = options.key || this.root; - return this.createClient(options as XhsClientOptions); + return this.createClient(options as XhsClientOptions) as XhsClient; } getKey(key?: string) { if (!key) key = this.root; diff --git a/src/agent/analyze/category.ts b/src/agent/analyze/category.ts new file mode 100644 index 0000000..da4e1f4 --- /dev/null +++ b/src/agent/analyze/category.ts @@ -0,0 +1,80 @@ +import { agent } from '@/agent/agent.ts'; +import { ai } from '../ai.ts'; +import { getJsonFromString } from './content.ts'; +export type Category = { + category: string; + description: string; +}; +export const categoryList: Category[] = [ + { + category: 'daily_poetry', + description: '获取一篇今日诗词', + }, + { + category: 'irony', + description: '反讽一下', + }, + { + category: 'random_beautiful_text', + description: '随机生成一段优美的文字', + }, + { + category: 'random_joke', + description: '随机生成一个笑话', + }, + { + category: '', + description: '教教我,怎么做' + }, + { + category: 'default', + description: '默认分类,无法识别的请求', + }, +]; +agent + .route({ + path: 'analyze', + key: 'category', + description: '分析文本内容,判断文本的请求分类。比如,1. 获取一篇今日诗词。2. 反讽一下 3. 随机生成一段优美的文字。', + }) + .define(async (ctx) => { + const text = ctx.query?.text || ''; + + const prompt = ` +请分析以下文本内容,判断文本的请求分类,并返回一个JSON对象。 + +识别的分类包括: +${categoryList.map((item) => `- ${item.category}: ${item.description}`).join('\n')} + +返回内容示例: +\`\`\`json +{ + "category": "daily_poetry" +} +\`\`\` + +${text} + +`; + const res = await ai + .chat( + [ + { + role: 'user', + content: prompt, + }, + ], + { + enable_thinking: false, + }, + ) + .catch((error) => { + ctx.throw(500, 'AI 服务错误: ' + error.status); + return error; + }); + const content = res.choices?.[0]?.message?.content || ''; + const jsonResponse = getJsonFromString(content); + const category = jsonResponse?.category || 'default'; + ctx.body = { category }; + }) + .addTo(agent); diff --git a/src/agent/analyze/content.ts b/src/agent/analyze/content.ts index 453888e..acfd74f 100644 --- a/src/agent/analyze/content.ts +++ b/src/agent/analyze/content.ts @@ -1,7 +1,7 @@ import { agent } from '@/agent/agent.ts'; import { ai } from '../ai.ts'; import { logger } from '@/agent/logger.ts'; -const getJsonFromString = (str: string) => { +export const getJsonFromString = (str: string) => { // 尝试从字符串中提取JSON对象 try { const jsonMatch = str.match(/```json\s*([\s\S]*?)\s*```/); diff --git a/src/agent/index.ts b/src/agent/index.ts index d03e659..6cd4adf 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -1,5 +1,7 @@ import { agent } from './agent.ts'; import './analyze/content.ts'; +import './analyze/category.ts'; + import './fix/prompt.ts'; import './xhs.ts'; diff --git a/src/agent/test/category.ts b/src/agent/test/category.ts new file mode 100644 index 0000000..eb9c118 --- /dev/null +++ b/src/agent/test/category.ts @@ -0,0 +1,21 @@ +import { agent } from '../index.ts'; + +const main = async () => { + const text1 = '解答一下这个笔记。'; + const text2 = '分析一下这个图片'; + const text3 = '这个视频介绍的是什么'; + const text4 = '评价一下这个评论。'; + const text5 = '关于这个评论。'; + const text6 = '1+1='; + const text7 = '反讽一下'; + const text8 = '随机生成一段优美的文字'; + const res = await agent.call({ + path: 'analyze', + key: 'category', + payload: { + text: text1, + }, + }); + console.log('analyze category res', res.code, 'content', res.body); +}; +main(); diff --git a/src/task/task.ts b/src/task/task.ts index ee49ef7..686e923 100644 --- a/src/task/task.ts +++ b/src/task/task.ts @@ -3,13 +3,16 @@ import { QueryRouterServer } from '@kevisual/router'; import { redis } from '@/modules/redis.ts'; import { Queue } from 'bullmq'; -import { app as xhsApp } from '@kevisual/xhs/index'; +import { app as xhsApp, xhsServices, xhsRootClient, XhsClient } from '@kevisual/xhs/index'; import { nanoid } from 'nanoid'; export const XHS_GET_UNREAD = 'unread_count'; export const XHS_QUEUE_NAME = 'XHS_QUEUE'; - +import { config } from '../modules/config.ts'; +const xhsClient: XhsClient = xhsRootClient; +xhsClient.setCookie(config.XHS_ROOT_COOKIE || ''); +console.log('XHS_ROOT_COOKIE', xhsClient.cookie); export const taskApp = new QueryRouterServer(); -export { xhsApp }; +export { xhsApp, xhsServices, xhsRootClient }; export const queue = new Queue(XHS_QUEUE_NAME, { connection: redis, });