This commit is contained in:
熊潇 2025-06-21 18:21:49 +08:00
parent a76b506327
commit da6d4041ad
9 changed files with 121 additions and 12 deletions

View File

@ -1,13 +1,13 @@
import { QueryRouterServer } from '@kevisual/router/browser'; 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 app = new QueryRouterServer();
export const xhsServices = new XhsServices(); // Semicolon separated Cookie File export const xhsServices = new XhsServices(); // Semicolon separated Cookie File
const cookie = 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, cookie,
signConfig: { signConfig: {
signUrl: 'http://light.xiongxiao.me:5006/sign', signUrl: 'http://light.xiongxiao.me:5006/sign',

View File

@ -1,5 +1,5 @@
import { XhsClient } from './libs/xhs.ts'; import { XhsClient } from './libs/xhs.ts';
import { app, xhsServices } from './app.ts'; import { app, xhsServices, xhsRootClient } from './app.ts';
import './routes/index.ts'; import './routes/index.ts';
export { XhsClient, app, xhsServices }; export { XhsClient, app, xhsServices, xhsRootClient };

View File

@ -66,6 +66,9 @@ export class XhsClient extends XhsClientBase {
constructor(opts: XhsOptions) { constructor(opts: XhsOptions) {
super(opts as any); super(opts as any);
} }
setCookie(cookie: string) {
this.cookie = cookie;
}
getApiInfo = getApiInfo; getApiInfo = getApiInfo;
printResult(msg: string, data: any) { printResult(msg: string, data: any) {
if (msg === 'response') { if (msg === 'response') {

View File

@ -3,7 +3,7 @@ import { Sequelize } from 'sequelize';
// import { createSequelize } from '@kevisual/xhs/services/xhs-db/db.ts'; // import { createSequelize } from '@kevisual/xhs/services/xhs-db/db.ts';
import path from 'node:path'; import path from 'node:path';
import fs from 'node:fs'; import fs from 'node:fs';
export { XhsClient };
type XhsClientOptions = { type XhsClientOptions = {
key: string; key: string;
cookie: string; cookie: string;
@ -69,7 +69,7 @@ export class XhsServices {
createRoot(options: Partial<XhsClientOptions>) { createRoot(options: Partial<XhsClientOptions>) {
options.key = options.key || this.root; options.key = options.key || this.root;
return this.createClient(options as XhsClientOptions); return this.createClient(options as XhsClientOptions) as XhsClient;
} }
getKey(key?: string) { getKey(key?: string) {
if (!key) key = this.root; if (!key) key = this.root;

View File

@ -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"
}
\`\`\`
<context>
${text}
</context>
`;
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);

View File

@ -1,7 +1,7 @@
import { agent } from '@/agent/agent.ts'; import { agent } from '@/agent/agent.ts';
import { ai } from '../ai.ts'; import { ai } from '../ai.ts';
import { logger } from '@/agent/logger.ts'; import { logger } from '@/agent/logger.ts';
const getJsonFromString = (str: string) => { export const getJsonFromString = (str: string) => {
// 尝试从字符串中提取JSON对象 // 尝试从字符串中提取JSON对象
try { try {
const jsonMatch = str.match(/```json\s*([\s\S]*?)\s*```/); const jsonMatch = str.match(/```json\s*([\s\S]*?)\s*```/);

View File

@ -1,5 +1,7 @@
import { agent } from './agent.ts'; import { agent } from './agent.ts';
import './analyze/content.ts'; import './analyze/content.ts';
import './analyze/category.ts';
import './fix/prompt.ts'; import './fix/prompt.ts';
import './xhs.ts'; import './xhs.ts';

View File

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

View File

@ -3,13 +3,16 @@
import { QueryRouterServer } from '@kevisual/router'; import { QueryRouterServer } from '@kevisual/router';
import { redis } from '@/modules/redis.ts'; import { redis } from '@/modules/redis.ts';
import { Queue } from 'bullmq'; 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'; import { nanoid } from 'nanoid';
export const XHS_GET_UNREAD = 'unread_count'; export const XHS_GET_UNREAD = 'unread_count';
export const XHS_QUEUE_NAME = 'XHS_QUEUE'; 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 const taskApp = new QueryRouterServer();
export { xhsApp }; export { xhsApp, xhsServices, xhsRootClient };
export const queue = new Queue(XHS_QUEUE_NAME, { export const queue = new Queue(XHS_QUEUE_NAME, {
connection: redis, connection: redis,
}); });