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

183 lines
5.3 KiB
TypeScript

import { app, cnbManager } from '../../app.ts';
import { createSkill, Skill, tool } from '@kevisual/router'
// 创建一个仓库 kevisual/test-repo
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 cnb = await cnbManager.getContext(ctx);
const name = ctx.query?.name;
const visibility = ctx.query?.visibility ?? 'public';
const description = ctx.query?.description ?? '';
if (!name) {
ctx.throw(400, '缺少参数 name');
}
try {
const res = await cnb.repo.createRepo({
name,
visibility,
description,
});
ctx.forward(res);
} catch (error) {
ctx.code = 200
ctx.body = { content: 'JS仓库可能已存在' }
}
}).addTo(app);
app.route({
path: 'cnb',
key: 'get-repo',
description: '获取代码仓库详情, 参数name',
middleware: ['auth'],
metadata: {
args: {
name: tool.schema.string().describe('代码仓库名称, 如 my-user/my-repo'),
}
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
const name = ctx.query?.name;
if (!name) {
ctx.throw(400, '缺少参数 name');
}
const res = await cnb.repo.getRepo(name);
ctx.forward(res);
}).addTo(app);
app.route({
path: 'cnb',
key: 'create-repo-file',
description: '在代码仓库中创建文件, repoName, filePath, content, encoding。使用CNB_COOKIE进行鉴权',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'create-repo-file',
title: '在代码仓库中创建文件',
summary: `在代码仓库中创建文件, encoding 可选,默认 raw`,
args: {
repoName: tool.schema.string().describe('代码仓库名称, 如 my-user/my-repo'),
filePath: tool.schema.string().describe('文件路径, 如 src/index.ts'),
content: tool.schema.string().describe('文本的字符串的内容'),
encoding: tool.schema.string().describe('编码方式,如 raw').optional(),
},
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
const repoName = ctx.query?.repoName;
const filePath = ctx.query?.filePath;
const content = ctx.query?.content;
const encoding = ctx.query?.encoding ?? 'raw';
if (!repoName || !filePath || !content) {
ctx.throw(400, '缺少参数 repoName, filePath 或 content');
}
const res = await cnb.repo.createCommit(repoName, {
message: `添加文件 ${filePath} 通过 API `,
files: [
{ path: filePath, content, encoding },
],
});
ctx.forward(res);
}).addTo(app);
// 删除一个仓库 kevisual/test-repo
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 cnb = await cnbManager.getContext(ctx);
const name = ctx.query?.name;
if (!name) {
ctx.throw(400, '缺少参数 name');
}
try {
const resCookie = await cnb.user.checkCookieValid()
if (resCookie.code !== 200) {
ctx.throw(401, 'Cookie 无效或已过期');
}
const res = await cnb.repo.deleteRepoCookie(name);
ctx.forward(res);
} catch (error) {
ctx.code = 200
ctx.body = { content: '已经删除' }
}
}).addTo(app);
app.route({
path: 'cnb',
key: 'update-repo-info',
description: '更新代码仓库信息, 参数name, description',
middleware: ['auth'],
metadata: {
tags: ['opencode'],
...createSkill({
skill: 'update-repo-info',
title: '更新代码仓库信息',
args: {
name: tool.schema.string().describe('代码仓库名称'),
description: tool.schema.string().describe('代码仓库描述'),
license: tool.schema.string().describe('代码仓库许可证类型,如 MIT').optional(),
site: tool.schema.string().describe('代码仓库主页链接').optional(),
topics: tool.schema.array(tool.schema.string()).describe('代码仓库话题标签列表').optional(),
},
summary: '更新代码仓库的信息',
})
}
}).define(async (ctx) => {
const cnb = await cnbManager.getContext(ctx);
const name = ctx.query?.name;
const description = ctx.query?.description;
const license = ctx.query?.license;
const site = ctx.query?.site;
const topics = ctx.query?.topics;
if (!name) {
ctx.throw(400, '缺少参数 name');
}
if (!description) {
ctx.throw(400, '缺少参数 description');
}
const res = await cnb.repo.updateRepoInfo(name, { description, license, site, topics });
ctx.forward(res);
}).addTo(app);