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