Files
cli/assistant/src/routes/opencode/session/index.ts
xiongxiao 35d3cae9f1 update
2026-03-16 03:23:40 +08:00

283 lines
9.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { app } from '@/app.ts'
import { createSkill, tool } from '@kevisual/router';
import { opencodeManager } from '../module/open.ts';
// 查询 - 列出所有 session
app.route({
path: 'opencode-session',
key: 'list',
middleware: ['auth-admin'],
description: '列出所有 OpenCode Session',
metadata: {
tags: ['session'],
...createSkill({
skill: 'list-opencode-sessions',
title: '列出所有 Session',
summary: '列出 OpenCode 中的所有会话,可按目录过滤',
args: {
port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 4096'),
}
})
}
}).define(async (ctx) => {
const { port } = ctx.query;
const client = await opencodeManager.getClient({ port });
const result = await client.session.list();
ctx.body = { data: result.data, content: `${(result.data as any[])?.length ?? 0} 个 Session` };
}).addTo(app);
// 查询 - 获取单个 session
app.route({
path: 'opencode-session',
key: 'get',
middleware: ['auth-admin'],
description: '获取指定 OpenCode Session',
metadata: {
tags: ['session'],
...createSkill({
skill: 'get-opencode-session',
title: '获取 Session',
summary: '根据 ID 获取指定的 OpenCode 会话信息',
args: {
id: tool.schema.string().describe('Session ID'),
port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 4096'),
}
})
}
}).define(async (ctx) => {
const { id, port } = ctx.query;
if (!id) {
ctx.throw(400, 'Session ID 不能为空');
return;
}
const client = await opencodeManager.getClient({ port });
const result = await client.session.get({ path: { id } });
ctx.body = { data: result.data, content: `已获取 Session: ${id}` };
}).addTo(app);
// 查询 - 获取 session 状态
app.route({
path: 'opencode-session',
key: 'status',
middleware: ['auth-admin'],
description: '获取 OpenCode Session 状态',
metadata: {
tags: ['session'],
...createSkill({
skill: 'get-opencode-session-status',
title: '获取 Session 状态',
summary: '获取当前 OpenCode 会话的运行状态,可按目录过滤',
args: {
directory: tool.schema.string().optional().describe('工作目录'),
port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 4096'),
}
})
}
}).define(async (ctx) => {
const { directory, port } = ctx.query;
const client = await opencodeManager.getClient({ port });
const result = await client.session.status(directory ? { query: { directory } } : undefined);
ctx.body = { data: result.data, content: `Session 状态已获取` };
}).addTo(app);
// 查询 - 列出 session 消息
app.route({
path: 'opencode-session',
key: 'messages',
middleware: ['auth-admin'],
description: '列出 OpenCode Session 消息',
metadata: {
tags: ['session'],
...createSkill({
skill: 'list-opencode-session-messages',
title: '列出 Session 消息',
summary: '列出指定 OpenCode 会话的所有消息记录',
args: {
sessionId: tool.schema.string().describe('Session ID'),
port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 4096'),
}
})
}
}).define(async (ctx) => {
const { sessionId, port } = ctx.query;
if (!sessionId) {
ctx.throw(400, 'Session ID 不能为空');
return;
}
const client = await opencodeManager.getClient({ port });
const result = await client.session.messages({ path: { id: sessionId } });
ctx.body = { data: result.data, content: `Session ${sessionId}${(result.data as any[])?.length ?? 0} 条消息` };
}).addTo(app);
// 新增 - 创建 session
app.route({
path: 'opencode-session',
key: 'create',
middleware: ['auth-admin'],
description: '创建 OpenCode Session',
metadata: {
tags: ['session'],
...createSkill({
skill: 'create-opencode-session',
title: '创建 Session',
summary: '在指定目录创建一个新的 OpenCode 会话',
args: {
directory: tool.schema.string().optional().describe('工作目录,默认为 /workspace'),
port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 4096'),
}
})
}
}).define(async (ctx) => {
const { directory = '/workspace', port } = ctx.query;
const client = await opencodeManager.getClient({ port });
const result = await client.session.create({ query: { directory } });
ctx.body = { data: result.data, content: `Session 已创建ID: ${(result.data as any)?.id}` };
}).addTo(app);
// 修改 - 更新 session
app.route({
path: 'opencode-session',
key: 'update',
middleware: ['auth-admin'],
description: '更新 OpenCode Session',
metadata: {
tags: ['session'],
...createSkill({
skill: 'update-opencode-session',
title: '更新 Session',
summary: '更新指定 OpenCode 会话的属性,如标题',
args: {
sessionId: tool.schema.string().describe('Session ID'),
title: tool.schema.string().optional().describe('新的会话标题'),
port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 4096'),
}
})
}
}).define(async (ctx) => {
const { sessionId, title, port } = ctx.query;
if (!sessionId) {
ctx.throw(400, 'Session ID 不能为空');
return;
}
const client = await opencodeManager.getClient({ port });
const result = await client.session.update({ path: { id: sessionId }, body: { title } });
ctx.body = { data: result.data, content: `Session ${sessionId} 已更新` };
}).addTo(app);
// 删除 - 删除 session
app.route({
path: 'opencode-session',
key: 'delete',
middleware: ['auth-admin'],
description: '删除 OpenCode Session',
metadata: {
tags: ['session'],
...createSkill({
skill: 'delete-opencode-session',
title: '删除 Session',
summary: '根据 ID 删除指定的 OpenCode 会话及其所有数据',
args: {
sessionId: tool.schema.string().describe('Session ID'),
port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 4096'),
}
})
}
}).define(async (ctx) => {
const { sessionId, port } = ctx.query;
if (!sessionId) {
ctx.throw(400, 'Session ID 不能为空');
return;
}
const client = await opencodeManager.getClient({ port });
await client.session.delete({ path: { id: sessionId } });
ctx.body = { content: `Session ${sessionId} 已删除` };
}).addTo(app);
// 操作 - 中止 session
app.route({
path: 'opencode-session',
key: 'abort',
middleware: ['auth-admin'],
description: '中止 OpenCode Session',
metadata: {
tags: ['session'],
...createSkill({
skill: 'abort-opencode-session',
title: '中止 Session',
summary: '中止正在运行的 OpenCode 会话',
args: {
sessionId: tool.schema.string().describe('Session ID'),
port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 4096'),
}
})
}
}).define(async (ctx) => {
const { sessionId, port } = ctx.query;
if (!sessionId) {
ctx.throw(400, 'Session ID 不能为空');
return;
}
const client = await opencodeManager.getClient({ port });
await client.session.abort({ path: { id: sessionId } });
ctx.body = { content: `Session ${sessionId} 已中止` };
}).addTo(app);
// 操作 - Fork session
app.route({
path: 'opencode-session',
key: 'fork',
middleware: ['auth-admin'],
description: 'Fork OpenCode Session',
metadata: {
tags: ['session'],
...createSkill({
skill: 'fork-opencode-session',
title: 'Fork Session',
summary: '从指定消息处 Fork 一个 OpenCode 会话',
args: {
sessionId: tool.schema.string().describe('Session ID'),
messageId: tool.schema.string().describe('从该消息处开始 Fork'),
port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 4096'),
}
})
}
}).define(async (ctx) => {
const { sessionId, messageId, port } = ctx.query;
if (!sessionId || !messageId) {
ctx.throw(400, 'Session ID 和 messageId 不能为空');
return;
}
const client = await opencodeManager.getClient({ port });
const result = await client.session.fork({ path: { id: sessionId }, body: { messageID: messageId } });
ctx.body = { data: result.data, content: `Session ${sessionId} 已从消息 ${messageId} Fork` };
}).addTo(app);
// 操作 - 总结 session
app.route({
path: 'opencode-session',
key: 'summarize',
middleware: ['auth-admin'],
description: '总结 OpenCode Session',
metadata: {
tags: ['session'],
...createSkill({
skill: 'summarize-opencode-session',
title: '总结 Session',
summary: '对指定的 OpenCode 会话进行内容总结',
args: {
sessionId: tool.schema.string().describe('Session ID'),
port: tool.schema.number().optional().describe('OpenCode 服务端口,默认为 4096'),
}
})
}
}).define(async (ctx) => {
const { sessionId, port } = ctx.query;
if (!sessionId) {
ctx.throw(400, 'Session ID 不能为空');
return;
}
const client = await opencodeManager.getClient({ port });
const result = await client.session.summarize({ path: { id: sessionId } });
ctx.body = { data: result.data, content: `Session ${sessionId} 总结完成` };
}).addTo(app);