@kevisual/cnb 0.0.36 → 0.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import { createSkill, tool } from '@kevisual/router';
2
- import { app, cnb } from '../../app.ts';
2
+ import { app, cnbManager, notCNBCheck } from '../../app.ts';
3
3
  import z from 'zod';
4
4
  import './skills.ts';
5
5
  import './keep.ts';
@@ -9,7 +9,7 @@ app.route({
9
9
  path: 'cnb',
10
10
  key: 'start-workspace',
11
11
  description: '启动开发工作空间, 参数 repo',
12
- middleware: ['auth-admin'],
12
+ middleware: ['auth'],
13
13
  metadata: {
14
14
  tags: ['opencode'],
15
15
  ...createSkill({
@@ -24,6 +24,7 @@ app.route({
24
24
  })
25
25
  }
26
26
  }).define(async (ctx) => {
27
+ const cnb = await cnbManager.getContext(ctx);
27
28
  const repo = ctx.query?.repo;
28
29
  const branch = ctx.query?.branch;
29
30
  const ref = ctx.query?.ref;
@@ -42,7 +43,7 @@ app.route({
42
43
  path: 'cnb',
43
44
  key: 'list-workspace',
44
45
  description: '获取cnb开发工作空间列表,可选参数 status=running 获取运行中的环境',
45
- middleware: ['auth-admin'],
46
+ middleware: ['auth'],
46
47
  metadata: {
47
48
  tags: ['opencode'],
48
49
  ...createSkill({
@@ -59,6 +60,7 @@ app.route({
59
60
  })
60
61
  }
61
62
  }).define(async (ctx) => {
63
+ const cnb = await cnbManager.getContext(ctx);
62
64
  const { status = 'running', page, pageSize, slug, branch } = ctx.query || {};
63
65
  const res = await cnb.workspace.list({
64
66
  status: status as 'running' | 'closed' | undefined,
@@ -73,7 +75,7 @@ app.route({
73
75
  path: 'cnb',
74
76
  key: 'get-workspace',
75
77
  description: '获取工作空间详情,通过 repo 和 sn 获取',
76
- middleware: ['auth-admin'],
78
+ middleware: ['auth'],
77
79
  metadata: {
78
80
  tags: ['opencode'],
79
81
  ...createSkill({
@@ -87,6 +89,7 @@ app.route({
87
89
  })
88
90
  }
89
91
  }).define(async (ctx) => {
92
+ const cnb = await cnbManager.getContext(ctx);
90
93
  const repo = ctx.query?.repo;
91
94
  const sn = ctx.query?.sn;
92
95
  if (!repo) {
@@ -104,7 +107,7 @@ app.route({
104
107
  path: 'cnb',
105
108
  key: 'delete-workspace',
106
109
  description: '删除工作空间,通过 pipelineId 或 sn',
107
- middleware: ['auth-admin'],
110
+ middleware: ['auth'],
108
111
  metadata: {
109
112
  tags: ['opencode'],
110
113
  ...createSkill({
@@ -119,6 +122,7 @@ app.route({
119
122
  })
120
123
  }
121
124
  }).define(async (ctx) => {
125
+ const cnb = await cnbManager.getContext(ctx);
122
126
  const pipelineId = ctx.query?.pipelineId;
123
127
  const sn = ctx.query?.sn;
124
128
  const sns = ctx.query?.sns;
@@ -143,7 +147,7 @@ app.route({
143
147
  path: 'cnb',
144
148
  key: 'stop-workspace',
145
149
  description: '停止工作空间,通过 pipelineId 或 sn',
146
- middleware: ['auth-admin'],
150
+ middleware: ['auth'],
147
151
  metadata: {
148
152
  tags: ['opencode'],
149
153
  ...createSkill({
@@ -157,6 +161,8 @@ app.route({
157
161
  })
158
162
  }
159
163
  }).define(async (ctx) => {
164
+ if (notCNBCheck(ctx)) { return; }
165
+ const cnb = await cnbManager.getContext(ctx);
160
166
  const pipelineId = ctx.query?.pipelineId;
161
167
  const sn = ctx.query?.sn;
162
168
  if (!pipelineId && !sn) {
@@ -1,5 +1,5 @@
1
1
  import { tool } from '@kevisual/router';
2
- import { app, cnb } from '../../app.ts';
2
+ import { app, cnbManager, notCNBCheck } from '../../app.ts';
3
3
  import { addKeepAliveData, KeepAliveData, removeKeepAliveData, createLiveData } from '../../../src/workspace/keep-file-live.ts';
4
4
  import { useKey } from '@kevisual/context';
5
5
 
@@ -8,7 +8,7 @@ app.route({
8
8
  path: 'cnb',
9
9
  key: 'keep-workspace-alive',
10
10
  description: '保持工作空间存活技能,参数repo:代码仓库路径,例如 user/repo,pipelineId:流水线ID,例如 cnb-708-1ji9sog7o-001',
11
- middleware: ['auth-admin'],
11
+ middleware: ['auth'],
12
12
  metadata: {
13
13
  tags: [],
14
14
  ...({
@@ -19,9 +19,11 @@ app.route({
19
19
  })
20
20
  }
21
21
  }).define(async (ctx) => {
22
+
23
+ const cnb = await cnbManager.getContext(ctx);
22
24
  const repo = ctx.query?.repo as string;
23
25
  const pipelineId = ctx.query?.pipelineId as string;
24
-
26
+ if (notCNBCheck(ctx)) return;
25
27
  if (!repo || !pipelineId) {
26
28
  ctx.throw(400, '缺少参数 repo 或 pipelineId');
27
29
  }
@@ -30,21 +32,20 @@ app.route({
30
32
  ctx.throw(401, 'CNB_COOKIE 环境变量无效或已过期,请重新登录获取新的cookie');
31
33
  }
32
34
  const res = await cnb.workspace.getWorkspaceCookie(repo, pipelineId);
33
- let wsUrl = `wss://${pipelineId}.cnb.space:443?skipWebSocketFrames=false`;
34
- let cookie = '';
35
- if (res.code === 200) {
36
- cookie = res.data.value;
37
- console.log(`启动保持工作空间 ${wsUrl} 存活的任务`);
38
- } else {
39
- ctx.throw(500, `获取工作空间访问cookie失败: ${res.message}`);
35
+ if (res.code !== 200 || !res.data?.cookie) {
36
+ ctx.throw(500, `获取工作空间 Cookie 失败: ${res.message}`);
40
37
  }
41
38
 
42
- console.log(`启动保持工作空间 ${wsUrl} 存活的任务`);
43
-
44
- const config: KeepAliveData = createLiveData({ cookie, repo, pipelineId });
45
- addKeepAliveData(config);
39
+ // 添加保活数据
40
+ const liveData = createLiveData({
41
+ repo,
42
+ pipelineId,
43
+ cookie: res.data.cookie
44
+ });
45
+ addKeepAliveData(liveData);
46
+ console.log('已添加 keep-alive 数据');
46
47
 
47
- ctx.body = { content: `已启动保持工作空间 ${wsUrl} 存活的任务`, data: config };
48
+ ctx.body = { content: `已启动保持工作空间 ${repo}/${pipelineId} 存活的任务`, data: liveData };
48
49
  }).addTo(app);
49
50
 
50
51
  // 停止保持工作空间存活技能
@@ -52,7 +53,7 @@ app.route({
52
53
  path: 'cnb',
53
54
  key: 'stop-keep-workspace-alive',
54
55
  description: '停止保持工作空间存活技能, 参数repo:代码仓库路径,例如 user/repo,pipelineId:流水线ID,例如 cnb-708-1ji9sog7o-001',
55
- middleware: ['auth-admin'],
56
+ middleware: ['auth'],
56
57
  metadata: {
57
58
  tags: [],
58
59
  ...({
@@ -63,6 +64,7 @@ app.route({
63
64
  })
64
65
  }
65
66
  }).define(async (ctx) => {
67
+ if (notCNBCheck(ctx)) return;
66
68
  const repo = ctx.query?.repo as string;
67
69
  const pipelineId = ctx.query?.pipelineId as string;
68
70
 
@@ -80,7 +82,7 @@ app.route({
80
82
  path: 'cnb',
81
83
  key: 'keep-alive-current-workspace',
82
84
  description: '保持当前工作空间存活技能',
83
- middleware: ['auth-admin'],
85
+ middleware: ['auth'],
84
86
  metadata: {
85
87
  tags: ['opencode'],
86
88
  skill: 'keep-alive-current-workspace',
@@ -88,6 +90,7 @@ app.route({
88
90
  summary: '保持当前工作空间存活,防止被关闭或释放资源',
89
91
  }
90
92
  }).define(async (ctx) => {
93
+ if (notCNBCheck(ctx)) return;
91
94
  const pipelineId = useKey('CNB_PIPELINE_ID');
92
95
  const repo = useKey('CNB_REPO_SLUG_LOWERCASE');
93
96
  if (!pipelineId || !repo) {
@@ -1,5 +1,5 @@
1
1
  import { createSkill, tool } from '@kevisual/router';
2
- import { app, cnb } from '../../app.ts';
2
+ import { app, cnbManager } from '../../app.ts';
3
3
 
4
4
  // 批量删除已停止的cnb工作空间
5
5
  // app.route({
@@ -35,7 +35,7 @@ app.route({
35
35
  path: 'cnb',
36
36
  key: 'clean-closed-workspace',
37
37
  description: '批量删除已停止的cnb工作空间',
38
- middleware: ['auth-admin'],
38
+ middleware: ['auth'],
39
39
  metadata: {
40
40
  tags: ['opencode'],
41
41
  ...createSkill({
@@ -45,6 +45,7 @@ app.route({
45
45
  })
46
46
  }
47
47
  }).define(async (ctx) => {
48
+ const cnb = await cnbManager.getContext(ctx);
48
49
  const closedWorkspaces = await cnb.workspace.list({ status: 'closed', pageSize: 100 });
49
50
  if (closedWorkspaces.code !== 200) {
50
51
  ctx.throw(500, '获取已关闭工作空间列表失败');