feat: 将所有路由的中间件从 'auth' 更新为 'admin-auth',并更新版本至 0.0.13

This commit is contained in:
2026-01-31 00:30:37 +08:00
parent 0d17d56628
commit 6e5a642ab2
13 changed files with 109 additions and 73 deletions

View File

@@ -2,12 +2,13 @@ import { createSkill } from '@kevisual/router'
import { app } from '../../app.ts' import { app } from '../../app.ts'
import { tool } from '@opencode-ai/plugin/tool' import { tool } from '@opencode-ai/plugin/tool'
if (!app.hasRoute('call')) {
// "调用 path: cnb key: list-repos" // "调用 path: cnb key: list-repos"
app.route({ app.route({
path: 'call', path: 'call',
key: '', key: '',
description: '调用', description: '调用',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -30,3 +31,4 @@ app.route({
const res = await ctx.run({ path, key, payload: ctx.query.payload || {} }); const res = await ctx.run({ path, key, payload: ctx.query.payload || {} });
ctx.forward(res); ctx.forward(res);
}).addTo(app) }).addTo(app)
}

View File

@@ -7,7 +7,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'user-check', key: 'user-check',
description: '检查用户登录状态参数checkToken,default true; checkCookie, default false', description: '检查用户登录状态参数checkToken,default true; checkCookie, default false',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({

View File

@@ -6,7 +6,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'set-cnb-cookie', key: 'set-cnb-cookie',
description: '设置当前cnb工作空间的cookie环境变量', description: '设置当前cnb工作空间的cookie环境变量',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -33,7 +33,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'get-cnb-cookie', key: 'get-cnb-cookie',
description: '获取当前cnb工作空间的cookie环境变量', description: '获取当前cnb工作空间的cookie环境变量',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({

View File

@@ -11,7 +11,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'get-cnb-port-uri', key: 'get-cnb-port-uri',
description: '获取当前cnb工作空间的port代理uri', description: '获取当前cnb工作空间的port代理uri',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -40,7 +40,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'get-cnb-vscode-uri', key: 'get-cnb-vscode-uri',
description: '获取当前cnb工作空间的vscode代理uri, 包括多种访问方式, 如web、vscode、codebuddy、cursor、ssh', description: '获取当前cnb工作空间的vscode代理uri, 包括多种访问方式, 如web、vscode、codebuddy、cursor、ssh',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({

View File

@@ -7,7 +7,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'create-issue', key: 'create-issue',
description: '创建 Issue, 参数 repo, title, body, assignees, labels, priority', description: '创建 Issue, 参数 repo, title, body, assignees, labels, priority',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -51,7 +51,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'complete-issue', key: 'complete-issue',
description: '完成 Issue, 参数 repo, issueNumber', description: '完成 Issue, 参数 repo, issueNumber',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({

View File

@@ -6,7 +6,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'list-issues', key: 'list-issues',
description: '查询 Issue 列表, 参数 repo, state, keyword, labels, page, page_size 等', description: '查询 Issue 列表, 参数 repo, state, keyword, labels, page, page_size 等',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({

View File

@@ -12,7 +12,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'cnb-ai-chat', key: 'cnb-ai-chat',
description: '调用cnb的知识库ai对话功能进行聊天', description: '调用cnb的知识库ai对话功能进行聊天',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -88,7 +88,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'cnb-rag-query', key: 'cnb-rag-query',
description: '调用cnb的知识库RAG查询功能进行问答', description: '调用cnb的知识库RAG查询功能进行问答',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({

View File

@@ -8,7 +8,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'list-repos', key: 'list-repos',
description: '列出我的代码仓库', description: '列出我的代码仓库',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({

View File

@@ -7,7 +7,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'create-repo', key: 'create-repo',
description: '创建代码仓库, 参数name, visibility, description', description: '创建代码仓库, 参数name, visibility, description',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -47,7 +47,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'create-repo-file', key: 'create-repo-file',
description: '在代码仓库中创建文件, repoName, filePath, content, encoding', description: '在代码仓库中创建文件, repoName, filePath, content, encoding',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -86,7 +86,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'delete-repo', key: 'delete-repo',
description: '删除代码仓库, 参数name', description: '删除代码仓库, 参数name',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({

View File

@@ -9,7 +9,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'start-workspace', key: 'start-workspace',
description: '启动开发工作空间, 参数 repo', description: '启动开发工作空间, 参数 repo',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -42,7 +42,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'list-workspace', key: 'list-workspace',
description: '获取cnb开发工作空间列表可选参数 status=running 获取运行中的环境', description: '获取cnb开发工作空间列表可选参数 status=running 获取运行中的环境',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -73,7 +73,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'get-workspace', key: 'get-workspace',
description: '获取工作空间详情,通过 repo 和 sn 获取', description: '获取工作空间详情,通过 repo 和 sn 获取',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -104,7 +104,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'delete-workspace', key: 'delete-workspace',
description: '删除工作空间,通过 pipelineId 或 sn', description: '删除工作空间,通过 pipelineId 或 sn',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({
@@ -143,7 +143,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'stop-workspace', key: 'stop-workspace',
description: '停止工作空间,通过 pipelineId 或 sn', description: '停止工作空间,通过 pipelineId 或 sn',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({

View File

@@ -1,5 +1,6 @@
import { createSkill, tool } from '@kevisual/router'; import { createSkill, tool } from '@kevisual/router';
import { app, cnb } from '../../app.ts'; import { app, cnb } from '../../app.ts';
import { nanoid } from 'nanoid';
import { createKeepAlive } from '../../../src/keep.ts'; import { createKeepAlive } from '../../../src/keep.ts';
@@ -7,6 +8,7 @@ type AliveInfo = {
startTime: number; startTime: number;
updatedTime?: number; updatedTime?: number;
KeepAlive: ReturnType<typeof createKeepAlive>; KeepAlive: ReturnType<typeof createKeepAlive>;
id: string;// 6位唯一标识符
} }
const keepAliveMap = new Map<string, AliveInfo>(); const keepAliveMap = new Map<string, AliveInfo>();
@@ -16,7 +18,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'keep-workspace-alive', key: 'keep-workspace-alive',
description: '保持工作空间存活技能,参数wsUrl:工作空间访问URLcookie:访问工作空间所需的cookie', description: '保持工作空间存活技能,参数wsUrl:工作空间访问URLcookie:访问工作空间所需的cookie',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: [], tags: [],
...({ ...({
@@ -36,10 +38,10 @@ app.route({
ctx.throw(400, '缺少访问工作空间所需的cookie参数'); ctx.throw(400, '缺少访问工作空间所需的cookie参数');
} }
// 检测是否已在运行 // 检测是否已在运行(通过 wsUrl 遍历检查)
const existing = keepAliveMap.get(wsUrl); const existing = Array.from(keepAliveMap.values()).find(info => (info as AliveInfo).id && (info as any).KeepAlive?.wsUrl === wsUrl);
if (existing) { if (existing) {
ctx.body = { message: `工作空间 ${wsUrl} 的保持存活任务已在运行中` }; ctx.body = { message: `工作空间 ${wsUrl} 的保持存活任务已在运行中`, id: (existing as AliveInfo).id };
return; return;
} }
@@ -53,21 +55,31 @@ app.route({
onMessage: (data) => { onMessage: (data) => {
// 可选:处理收到的消息 // 可选:处理收到的消息
// console.log(`工作空间 ${wsUrl} 收到消息: ${data}`); // console.log(`工作空间 ${wsUrl} 收到消息: ${data}`);
const aliveInfo = keepAliveMap.get(wsUrl); // 通过 wsUrl 找到对应的 id 并更新时间
if (aliveInfo) { for (const info of keepAliveMap.values()) {
aliveInfo.updatedTime = Date.now(); if ((info as any).KeepAlive?.wsUrl === wsUrl) {
info.updatedTime = Date.now();
break;
}
} }
}, },
debug: true, debug: true,
onExit: (code) => { onExit: (code) => {
console.log(`工作空间 ${wsUrl} 保持存活任务已退出,退出码: ${code}`); console.log(`工作空间 ${wsUrl} 保持存活任务已退出,退出码: ${code}`);
keepAliveMap.delete(wsUrl); // 通过 wsUrl 找到对应的 id 并删除
for (const [id, info] of keepAliveMap.entries()) {
if ((info as any).KeepAlive?.wsUrl === wsUrl) {
keepAliveMap.delete(id);
break;
}
}
} }
}); });
keepAliveMap.set(wsUrl, { startTime: Date.now(), updatedTime: Date.now(), KeepAlive: keep }); const id = nanoid(6).toLowerCase();
keepAliveMap.set(id, { startTime: Date.now(), updatedTime: Date.now(), KeepAlive: keep, id });
ctx.body = { message: `已启动保持工作空间 ${wsUrl} 存活的任务` }; ctx.body = { content: `已启动保持工作空间 ${wsUrl} 存活的任务`, id };
}).addTo(app); }).addTo(app);
// 获取保持工作空间存活任务列表技能 // 获取保持工作空间存活任务列表技能
@@ -75,13 +87,14 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'list-keep-alive-tasks', key: 'list-keep-alive-tasks',
description: '获取保持工作空间存活任务列表技能', description: '获取保持工作空间存活任务列表技能',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: [], tags: [],
} }
}).define(async (ctx) => { }).define(async (ctx) => {
const list = Array.from(keepAliveMap.entries()).map(([wsUrl, info]) => ({ const list = Array.from(keepAliveMap.entries()).map(([id, info]) => ({
wsUrl, id,
wsUrl: (info as any).KeepAlive?.wsUrl,
startTime: info.startTime, startTime: info.startTime,
updatedTime: info.updatedTime updatedTime: info.updatedTime
})); }));
@@ -92,30 +105,51 @@ app.route({
app.route({ app.route({
path: 'cnb', path: 'cnb',
key: 'stop-keep-workspace-alive', key: 'stop-keep-workspace-alive',
description: '停止保持工作空间存活技能, 参数wsUrl:工作空间访问URL', description: '停止保持工作空间存活技能, 参数wsUrl:工作空间访问URL或者id',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: [], tags: [],
...({ ...({
args: { args: {
wsUrl: tool.schema.string().describe('工作空间的访问URL'), wsUrl: tool.schema.string().optional().describe('工作空间的访问URL'),
id: tool.schema.string().optional().describe('保持存活任务的唯一标识符'),
} }
}) })
} }
}).define(async (ctx) => { }).define(async (ctx) => {
const wsUrl = ctx.query?.wsUrl as string; const wsUrl = ctx.query?.wsUrl as string;
if (!wsUrl) { const id = ctx.query?.id as string;
ctx.throw(400, '缺少工作空间访问URL参数'); if (!wsUrl && !id) {
ctx.throw(400, '缺少工作空间访问URL参数或唯一标识符');
} }
const keepAlive = keepAliveMap.get(wsUrl); let targetId: string | undefined;
if (keepAlive) { let wsUrlFound: string | undefined;
if (id) {
const info = keepAliveMap.get(id);
if (info) {
targetId = id;
wsUrlFound = (info as any).KeepAlive?.wsUrl;
}
} else if (wsUrl) {
for (const [key, info] of keepAliveMap.entries()) {
if ((info as any).KeepAlive?.wsUrl === wsUrl) {
targetId = key;
wsUrlFound = wsUrl;
break;
}
}
}
if (targetId) {
const keepAlive = keepAliveMap.get(targetId);
const endTime = Date.now(); const endTime = Date.now();
const duration = endTime - keepAlive.startTime; const duration = endTime - keepAlive!.startTime;
keepAlive?.KeepAlive?.disconnect(); keepAlive?.KeepAlive?.disconnect();
keepAliveMap.delete(wsUrl); keepAliveMap.delete(targetId);
ctx.body = { message: `已停止保持工作空间 ${wsUrl} 存活的任务,持续时间: ${duration}ms` }; ctx.body = { content: `已停止保持工作空间 ${wsUrlFound} 存活的任务,持续时间: ${duration}ms`, id: targetId };
} else { } else {
ctx.body = { message: `没有找到工作空间 ${wsUrl}保持存活任务` }; ctx.body = { content: `没有找到对应的工作空间保持存活任务` };
} }
}).addTo(app); }).addTo(app);

View File

@@ -35,7 +35,7 @@ app.route({
path: 'cnb', path: 'cnb',
key: 'clean-closed-workspace', key: 'clean-closed-workspace',
description: '批量删除已停止的cnb工作空间', description: '批量删除已停止的cnb工作空间',
middleware: ['auth'], middleware: ['admin-auth'],
metadata: { metadata: {
tags: ['opencode'], tags: ['opencode'],
...createSkill({ ...createSkill({

View File

@@ -1,6 +1,6 @@
{ {
"name": "@kevisual/cnb", "name": "@kevisual/cnb",
"version": "0.0.12", "version": "0.0.13",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {