feat: 重构CNB管理模块,添加清理记录功能,更新中间件为统一认证方式,优化工作空间相关路由

This commit is contained in:
xiongxiao
2026-03-09 18:54:33 +08:00
committed by cnb
parent 21ba07e55b
commit 382c4809ea
25 changed files with 296 additions and 73 deletions

View File

@@ -1,5 +1,5 @@
import { createSkill, tool } from '@kevisual/router';
import { app, cnb } from '../../app.ts';
import { app, cnbManager, notCNBCheck } from '../../app.ts';
import z from 'zod';
import './skills.ts';
import './keep.ts';
@@ -9,7 +9,7 @@ app.route({
path: 'cnb',
key: 'start-workspace',
description: '启动开发工作空间, 参数 repo',
middleware: ['auth-admin'],
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
@@ -24,6 +24,7 @@ app.route({
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
const repo = ctx.query?.repo;
const branch = ctx.query?.branch;
const ref = ctx.query?.ref;
@@ -42,7 +43,7 @@ app.route({
path: 'cnb',
key: 'list-workspace',
description: '获取cnb开发工作空间列表可选参数 status=running 获取运行中的环境',
middleware: ['auth-admin'],
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
@@ -59,6 +60,7 @@ app.route({
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
const { status = 'running', page, pageSize, slug, branch } = ctx.query || {};
const res = await cnb.workspace.list({
status: status as 'running' | 'closed' | undefined,
@@ -73,7 +75,7 @@ app.route({
path: 'cnb',
key: 'get-workspace',
description: '获取工作空间详情,通过 repo 和 sn 获取',
middleware: ['auth-admin'],
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
@@ -87,6 +89,7 @@ app.route({
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
const repo = ctx.query?.repo;
const sn = ctx.query?.sn;
if (!repo) {
@@ -104,7 +107,7 @@ app.route({
path: 'cnb',
key: 'delete-workspace',
description: '删除工作空间,通过 pipelineId 或 sn',
middleware: ['auth-admin'],
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
@@ -119,6 +122,7 @@ app.route({
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
const pipelineId = ctx.query?.pipelineId;
const sn = ctx.query?.sn;
const sns = ctx.query?.sns;
@@ -143,7 +147,7 @@ app.route({
path: 'cnb',
key: 'stop-workspace',
description: '停止工作空间,通过 pipelineId 或 sn',
middleware: ['auth-admin'],
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
@@ -157,6 +161,8 @@ app.route({
})
}
}).define(async (ctx) => {
if (notCNBCheck(ctx)) { return; }
const cnb = await cnbManager.getContext(ctx);
const pipelineId = ctx.query?.pipelineId;
const sn = ctx.query?.sn;
if (!pipelineId && !sn) {

View File

@@ -1,5 +1,5 @@
import { tool } from '@kevisual/router';
import { app, cnb } from '../../app.ts';
import { app, cnbManager, notCNBCheck } from '../../app.ts';
import { addKeepAliveData, KeepAliveData, removeKeepAliveData, createLiveData } from '../../../src/workspace/keep-file-live.ts';
import { useKey } from '@kevisual/context';
@@ -8,7 +8,7 @@ app.route({
path: 'cnb',
key: 'keep-workspace-alive',
description: '保持工作空间存活技能参数repo:代码仓库路径,例如 user/repopipelineId:流水线ID例如 cnb-708-1ji9sog7o-001',
middleware: ['auth-admin'],
middleware: ['auth'],
metadata: {
tags: [],
...({
@@ -19,9 +19,11 @@ app.route({
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
const repo = ctx.query?.repo as string;
const pipelineId = ctx.query?.pipelineId as string;
if (notCNBCheck(ctx)) return;
if (!repo || !pipelineId) {
ctx.throw(400, '缺少参数 repo 或 pipelineId');
}
@@ -51,7 +53,7 @@ app.route({
path: 'cnb',
key: 'stop-keep-workspace-alive',
description: '停止保持工作空间存活技能, 参数repo:代码仓库路径,例如 user/repopipelineId:流水线ID例如 cnb-708-1ji9sog7o-001',
middleware: ['auth-admin'],
middleware: ['auth'],
metadata: {
tags: [],
...({
@@ -62,6 +64,7 @@ app.route({
})
}
}).define(async (ctx) => {
if (notCNBCheck(ctx)) return;
const repo = ctx.query?.repo as string;
const pipelineId = ctx.query?.pipelineId as string;
@@ -79,7 +82,7 @@ app.route({
path: 'cnb',
key: 'keep-alive-current-workspace',
description: '保持当前工作空间存活技能',
middleware: ['auth-admin'],
middleware: ['auth'],
metadata: {
tags: ['opencode'],
skill: 'keep-alive-current-workspace',
@@ -87,6 +90,7 @@ app.route({
summary: '保持当前工作空间存活,防止被关闭或释放资源',
}
}).define(async (ctx) => {
if (notCNBCheck(ctx)) return;
const pipelineId = useKey('CNB_PIPELINE_ID');
const repo = useKey('CNB_REPO_SLUG_LOWERCASE');
if (!pipelineId || !repo) {

View File

@@ -1,5 +1,5 @@
import { createSkill, tool } from '@kevisual/router';
import { app, cnb } from '../../app.ts';
import { app, cnbManager } from '../../app.ts';
// 批量删除已停止的cnb工作空间
// app.route({
@@ -35,7 +35,7 @@ app.route({
path: 'cnb',
key: 'clean-closed-workspace',
description: '批量删除已停止的cnb工作空间',
middleware: ['auth-admin'],
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
@@ -45,6 +45,7 @@ app.route({
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
const closedWorkspaces = await cnb.workspace.list({ status: 'closed', pageSize: 100 });
if (closedWorkspaces.code !== 200) {
ctx.throw(500, '获取已关闭工作空间列表失败');