Files
cnb/agent/routes/repo/index.ts

103 lines
2.8 KiB
TypeScript

import { app, cnb } from '../../app.ts';
import { createSkill, Skill } from '@kevisual/router'
import { tool } from "@opencode-ai/plugin/tool"
app.route({
path: 'cnb',
key: 'create-repo',
description: '创建代码仓库, 参数name, visibility, description',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'create-repo',
title: '创建代码仓库',
args: {
name: tool.schema.string().describe('代码仓库名称, 如 my-user/my-repo'),
visibility: tool.schema.string().describe('代码仓库可见性, public 或 private').default('public'),
description: tool.schema.string().describe('代码仓库描述'),
},
summary: '创建一个新的代码仓库',
})
}
}).define(async (ctx) => {
const name = ctx.query?.name;
const visibility = ctx.query?.visibility ?? 'public';
const description = ctx.query?.description ?? '';
if (!name) {
ctx.throw(400, '缺少参数 name');
}
const res = await cnb.repo.createRepo({
name,
visibility,
description,
});
ctx.forward(res);
}).addTo(app);
app.route({
path: 'cnb',
key: 'create-repo-file',
description: '在代码仓库中创建文件, name, path, content, encoding',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'create-repo-file',
title: '在代码仓库中创建文件',
args: {
name: tool.schema.string().describe('代码仓库名称'),
path: tool.schema.string().describe('文件路径, 如 src/index.ts'),
content: tool.schema.string().describe('文件内容'),
encoding: tool.schema.string().describe('编码方式, 默认为 raw').optional(),
},
summary: '在代码仓库中创建文件',
})
}
}).define(async (ctx) => {
const name = ctx.query?.name;
const path = ctx.query?.path;
const content = ctx.query?.content;
const encoding = ctx.query?.encoding ?? 'raw';
if (!name || !path || !content) {
ctx.throw(400, '缺少参数 name, path 或 content');
}
const res = await cnb.repo.createCommit(name, {
message: `添加文件 ${path} 通过 API `,
files: [
{ path, content, encoding },
],
});
ctx.forward(res);
}).addTo(app);
app.route({
path: 'cnb',
key: 'delete-repo',
description: '删除代码仓库, 参数name',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'delete-repo',
title: '删除代码仓库',
args: {
name: tool.schema.string().describe('代码仓库名称'),
},
summary: '删除一个代码仓库',
})
}
}).define(async (ctx) => {
const name = ctx.query?.name;
if (!name) {
ctx.throw(400, '缺少参数 name');
}
const res = await cnb.repo.deleteRepo(name);
ctx.forward(res);
}).addTo(app);