update
This commit is contained in:
1
agent/routes/cnb-env/index.ts
Normal file
1
agent/routes/cnb-env/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
// 根据环境变量获取当前的 cnb 启动环境
|
||||
36
agent/routes/index.ts
Normal file
36
agent/routes/index.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { app, appId } from '@/agent/app.ts';
|
||||
import './user/check.ts'
|
||||
|
||||
const checkAppId = (ctx: any, appId: string) => {
|
||||
const _appId = ctx?.appId;
|
||||
if (_appId) {
|
||||
if (_appId !== appId) {
|
||||
ctx.throw(403, 'Invalid App ID');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!app.hasRoute('auth')) {
|
||||
app.route({
|
||||
id: 'auth',
|
||||
path: 'auth',
|
||||
}).define(async (ctx) => {
|
||||
// ctx.body = 'Auth Route';
|
||||
if (checkAppId(ctx, appId)) {
|
||||
return;
|
||||
}
|
||||
}).addTo(app);
|
||||
|
||||
app.route({
|
||||
id: 'admin-auth',
|
||||
path: 'admin-auth',
|
||||
middleware: ['auth'],
|
||||
}).define(async (ctx) => {
|
||||
// ctx.body = 'Admin Auth Route';
|
||||
if (checkAppId(ctx, appId)) {
|
||||
return;
|
||||
}
|
||||
}).addTo(app);
|
||||
}
|
||||
53
agent/routes/repo/index.ts
Normal file
53
agent/routes/repo/index.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { app, cnb } from '@/agent/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'cnb',
|
||||
key: 'repo-create',
|
||||
description: '创建代码仓库, 参数name, visibility, description',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
tags: ['opencode']
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const name = ctx.query?.name;
|
||||
const visibility = ctx.query?.visibility ?? 'private';
|
||||
const description = ctx.query?.description ?? '';
|
||||
|
||||
if (!name) {
|
||||
ctx.throw(400, '缺少参数 name');
|
||||
}
|
||||
|
||||
const res = await cnb.repo.createRepo(cnb.group, {
|
||||
name,
|
||||
visibility,
|
||||
description,
|
||||
});
|
||||
ctx.forward(res);
|
||||
}).addTo(app);
|
||||
|
||||
app.route({
|
||||
path: 'cnb',
|
||||
key: 'repo-create-file',
|
||||
description: '在代码仓库中创建文件, 参数repoName, path, content, encoding',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
tags: ['opencode']
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const repoName = ctx.query?.repoName;
|
||||
const path = ctx.query?.path;
|
||||
const content = ctx.query?.content;
|
||||
const encoding = ctx.query?.encoding ?? 'raw';
|
||||
|
||||
if (!repoName || !path || !content) {
|
||||
ctx.throw(400, '缺少参数 repoName, path 或 content');
|
||||
}
|
||||
|
||||
const res = await cnb.repo.createCommit(repoName, {
|
||||
message: `添加文件 ${path} 通过 API `,
|
||||
files: [
|
||||
{ path, content, encoding },
|
||||
],
|
||||
});
|
||||
ctx.forward(res);
|
||||
}).addTo(app);
|
||||
33
agent/routes/user/check.ts
Normal file
33
agent/routes/user/check.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { app, cnb } from '@/agent/app.ts';
|
||||
|
||||
|
||||
app.route({
|
||||
path: 'cnb',
|
||||
key: 'user-check',
|
||||
description: '检查用户登录状态,参数checkToken,default true; checkCookie, default false',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
tags: ['opencode']
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const checkToken = ctx.query?.checkToken ?? true;
|
||||
const checkCookie = ctx.query?.checkCookie ?? false;
|
||||
let output = '';
|
||||
if (checkToken) {
|
||||
const res = await cnb.user.getUser();
|
||||
if (res?.code !== 200) {
|
||||
output += `Token 无效,请检查 CNB_TOKEN 配置。\n`;
|
||||
} else {
|
||||
output += `Token 有效,Token用户昵称:${res.data?.nickname}\n`;
|
||||
}
|
||||
}
|
||||
if (checkCookie) {
|
||||
const res = await cnb.user.getCurrentUser();
|
||||
if (res?.code !== 200) {
|
||||
output += `Cookie 无效,请检查 CNB_COOKIE 配置。\n`;
|
||||
} else {
|
||||
output += `Cookie 有效,当前Cookie用户:${res.data?.nickname}\n`;
|
||||
}
|
||||
}
|
||||
ctx.body = { output };
|
||||
}).addTo(app);
|
||||
23
agent/routes/workspace/index.ts
Normal file
23
agent/routes/workspace/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { app, cnb } from '@/agent/app.ts';
|
||||
|
||||
app.route({
|
||||
path: 'cnb',
|
||||
key: 'start-workspace',
|
||||
description: '启动开发工作空间, 参数 repo',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
tags: ['opencode']
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
const repo = ctx.query?.repo;
|
||||
const branch = ctx.query?.branch;
|
||||
const ref = ctx.query?.ref;
|
||||
if (!repo) {
|
||||
ctx.throw(400, '缺少参数 repo');
|
||||
}
|
||||
const res = await cnb.workspace.startWorkspace(repo, {
|
||||
branch,
|
||||
ref
|
||||
});
|
||||
ctx.forward(res);
|
||||
}).addTo(app);
|
||||
Reference in New Issue
Block a user