101 lines
3.5 KiB
TypeScript
101 lines
3.5 KiB
TypeScript
import { tool } from '@kevisual/router';
|
||
import { app, cnbManager, notCNBCheck } from '../../app.ts';
|
||
import { addKeepAliveData, KeepAliveData, removeKeepAliveData, createLiveData } from '../../../src/workspace/keep-file-live.ts';
|
||
import { useKey } from '@kevisual/context';
|
||
|
||
// 保持工作空间存活技能
|
||
app.route({
|
||
path: 'cnb',
|
||
key: 'keep-workspace-alive',
|
||
description: '保持工作空间存活技能,参数repo:代码仓库路径,例如 user/repo,pipelineId:流水线ID,例如 cnb-708-1ji9sog7o-001',
|
||
middleware: ['auth'],
|
||
metadata: {
|
||
tags: [],
|
||
...({
|
||
args: {
|
||
repo: tool.schema.string().describe('代码仓库路径,例如 user/repo'),
|
||
pipelineId: tool.schema.string().describe('流水线ID,例如 cnb-708-1ji9sog7o-001'),
|
||
}
|
||
})
|
||
}
|
||
}).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');
|
||
}
|
||
const validCookie = await cnb.user.checkCookieValid()
|
||
if (validCookie.code !== 200) {
|
||
ctx.throw(401, 'CNB_COOKIE 环境变量无效或已过期,请重新登录获取新的cookie');
|
||
}
|
||
const res = await cnb.workspace.getWorkspaceCookie(repo, pipelineId);
|
||
if (res.code !== 200 || !res.data?.cookie) {
|
||
ctx.throw(500, `获取工作空间 Cookie 失败: ${res.message}`);
|
||
}
|
||
|
||
// 添加保活数据
|
||
const liveData = createLiveData({
|
||
repo,
|
||
pipelineId,
|
||
cookie: res.data.cookie
|
||
});
|
||
addKeepAliveData(liveData);
|
||
console.log('已添加 keep-alive 数据');
|
||
|
||
ctx.body = { content: `已启动保持工作空间 ${repo}/${pipelineId} 存活的任务`, data: liveData };
|
||
}).addTo(app);
|
||
|
||
// 停止保持工作空间存活技能
|
||
app.route({
|
||
path: 'cnb',
|
||
key: 'stop-keep-workspace-alive',
|
||
description: '停止保持工作空间存活技能, 参数repo:代码仓库路径,例如 user/repo,pipelineId:流水线ID,例如 cnb-708-1ji9sog7o-001',
|
||
middleware: ['auth'],
|
||
metadata: {
|
||
tags: [],
|
||
...({
|
||
args: {
|
||
repo: tool.schema.string().describe('代码仓库路径,例如 user/repo'),
|
||
pipelineId: tool.schema.string().describe('流水线ID,例如 cnb-708-1ji9sog7o-001'),
|
||
}
|
||
})
|
||
}
|
||
}).define(async (ctx) => {
|
||
if (notCNBCheck(ctx)) return;
|
||
const repo = ctx.query?.repo as string;
|
||
const pipelineId = ctx.query?.pipelineId as string;
|
||
|
||
if (!repo || !pipelineId) {
|
||
ctx.throw(400, '缺少参数 repo 或 pipelineId');
|
||
}
|
||
removeKeepAliveData(repo, pipelineId);
|
||
ctx.body = { content: `已停止保持工作空间 ${repo}/${pipelineId} 存活的任务` };
|
||
}).addTo(app);
|
||
|
||
|
||
|
||
|
||
app.route({
|
||
path: 'cnb',
|
||
key: 'keep-alive-current-workspace',
|
||
description: '保持当前工作空间存活技能',
|
||
middleware: ['auth'],
|
||
metadata: {
|
||
tags: ['opencode'],
|
||
skill: 'keep-alive-current-workspace',
|
||
title: '保持当前工作空间存活',
|
||
summary: '保持当前工作空间存活,防止被关闭或释放资源',
|
||
}
|
||
}).define(async (ctx) => {
|
||
if (notCNBCheck(ctx)) return;
|
||
const pipelineId = useKey('CNB_PIPELINE_ID');
|
||
const repo = useKey('CNB_REPO_SLUG_LOWERCASE');
|
||
if (!pipelineId || !repo) {
|
||
ctx.throw(400, '当前环境缺少 CNB_PIPELINE_ID 或 CNB_REPO_SLUG_LOWERCASE 环境变量,无法保持工作空间存活');
|
||
}
|
||
const res = await app.run({ path: 'cnb', key: 'keep-workspace-alive', payload: { repo, pipelineId } }, ctx);
|
||
ctx.forward(res);
|
||
}).addTo(app); |