From 8379be163078b98cfd29623f5bbbe91e1eb241b5 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Wed, 11 Mar 2026 15:14:45 +0800 Subject: [PATCH] temp --- .cnb.yml | 12 ++++++++++++ agent/app.ts | 6 ------ agent/modules/cnb-manager.ts | 22 ++++++++++++++++++++-- agent/npc.ts | 3 +++ agent/routes/chat/chat.ts | 13 +++++++++---- package.json | 1 + 6 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 agent/npc.ts diff --git a/.cnb.yml b/.cnb.yml index 389ca25..f676d49 100644 --- a/.cnb.yml +++ b/.cnb.yml @@ -18,3 +18,15 @@ $: env: !reference [.common_env, env] imports: !reference [.common_env, imports] stages: !reference [.dev_template, stages] + + issue.comment@npc: + - docker: + image: docker.cnb.cool/kevisual/dev-env/bun:latest + services: + - docker + env: !reference [.common_env, env] + imports: !reference [.common_env, imports] + stages: + - name: "task" + script: | + bun run agent/npc.ts \ No newline at end of file diff --git a/agent/app.ts b/agent/app.ts index 16181c9..5f75178 100644 --- a/agent/app.ts +++ b/agent/app.ts @@ -35,9 +35,3 @@ export const notCNBCheck = (ctx: any) => { } return false; } -const repo = useKey('CNB_REPO_SLUG_LOWERCASE') as string || 'kevision/kevision'; -export const cnbAi = createOpenAICompatible({ - baseURL: `https://api.cnb.cool/${repo}/-/ai/`, - name: 'custom-cnb', - apiKey: token, -}); \ No newline at end of file diff --git a/agent/modules/cnb-manager.ts b/agent/modules/cnb-manager.ts index 26f3667..5e8e959 100644 --- a/agent/modules/cnb-manager.ts +++ b/agent/modules/cnb-manager.ts @@ -1,6 +1,7 @@ import { Result } from '@kevisual/query'; import { CNB } from '../../src/index.ts'; import { useKey } from '@kevisual/context'; +import { createOpenAICompatible } from '@ai-sdk/openai-compatible'; export const getConfig = async (opts: { token?: string }) => { const kevisualEnv = useKey('KEVISUAL_ENV') const baseUrl = kevisualEnv === 'production' ? 'https://kevisual.cn/api/router' : 'https://kevisual.xiongxiao.me/api/router'; @@ -32,7 +33,14 @@ type CNBItem = { runAt?: number owner?: boolean cnb: CNB + cnbAi: ReturnType } +// const repo = useKey('CNB_REPO_SLUG_LOWERCASE') as string || 'kevision/kevision'; +// export const cnbAi = createOpenAICompatible({ +// baseURL: `https://api.cnb.cool/${repo}/-/ai/`, +// name: 'custom-cnb', +// apiKey: token, +// }); export class CNBManager { cnbMap: Map = new Map() constructor() { @@ -71,20 +79,24 @@ export class CNBManager { * @returns CNB 实例 */ async getContext(ctx: any) { + const item = await this.getCNBItem(ctx) + return item.cnb + } + async getCNBItem(ctx: any) { const tokenUser = ctx?.state?.tokenUser const username = tokenUser?.username if (!username) { ctx.throw(403, 'Unauthorized') } if (username === 'default') { - return this.getDefaultCNB().cnb + return this.getDefaultCNB() } const kevisualToken = ctx.query?.token; const item = await this.getCNB({ username, kevisualToken }); if (!item) { ctx.throw(400, '不存在的 CNB 配置项,请检查 登录 Token 是否正确,或添加 CNB 配置') } - return item.cnb + return item; } addCNB(opts: Partial) { if (!opts.username || !opts.token) { @@ -98,6 +110,12 @@ export class CNBManager { const cnb = opts?.cnb || new CNB({ token: opts.token, cookie: opts.cookie }); opts.cnb = cnb; opts.runAt = Date.now() + const repoSlug = useKey('CNB_REPO_SLUG_LOWERCASE') as string || 'kevision/kevision'; + opts.cnbAi = createOpenAICompatible({ + baseURL: `https://api.cnb.cool/${repoSlug}/-/ai/`, + name: `custom-cnb-${opts.username}`, + apiKey: opts.token, + }) this.cnbMap.set(opts.username, opts as CNBItem) return opts as CNBItem } diff --git a/agent/npc.ts b/agent/npc.ts new file mode 100644 index 0000000..0144548 --- /dev/null +++ b/agent/npc.ts @@ -0,0 +1,3 @@ +import { app } from './index.ts'; + +import { getN } from '../src/index.ts' \ No newline at end of file diff --git a/agent/routes/chat/chat.ts b/agent/routes/chat/chat.ts index ef3fdae..94dc7ef 100644 --- a/agent/routes/chat/chat.ts +++ b/agent/routes/chat/chat.ts @@ -1,5 +1,5 @@ import { runAgent } from '@kevisual/ai/agent' -import { app, notCNBCheck, cnbAi } from '../../app.ts'; +import { app, notCNBCheck, cnbManager, cnb } from '../../app.ts'; import z from 'zod'; app.route({ @@ -13,7 +13,8 @@ app.route({ messages: z.array(z.object({ role: z.enum(['user', 'assistant']).describe('消息角色,user表示用户输入,assistant表示助手回复'), content: z.string().describe('消息内容') - })).describe('对话消息列表,按照时间顺序排列,包含用户和助手的历史消息') + })).describe('对话消息列表,按照时间顺序排列,包含用户和助手的历史消息'), + model: z.string().optional().describe('默认auto') } } }).define(async (ctx) => { @@ -22,6 +23,9 @@ app.route({ ctx.throw(400, '缺少必要参数,必须提供question或messages'); return; } + const model = ctx.args?.model || 'auto' + const item = await cnbManager.getCNBItem(ctx); + const cnbAi = item.cnbAi; const messages = ctx.args.messages || [{ role: 'user', content: ctx.args.question @@ -29,8 +33,9 @@ app.route({ const result = await runAgent({ app, messages: messages, - languageModel: cnbAi('auto'), - token: ctx.query?.token as string, + languageModel: cnbAi(model), + token: '', + // token: ctx.query.token as string, }); ctx.body = result; }).addTo(app); \ No newline at end of file diff --git a/package.json b/package.json index fcb66e0..675fffa 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "scripts": { "build": "bun bun.config.ts", "flow": "ev npm patch && pnpm build && ev npm publish npm -p", + "compile": "bun build --compile --minify agent/commander.ts --outfile=./dist/cnb ", "pub": "ev pack -u -m false -c -p" }, "keywords": [],