@kevisual/cnb 0.0.2 → 0.0.4

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.
package/package.json CHANGED
@@ -1,51 +1,46 @@
1
1
  {
2
2
  "name": "@kevisual/cnb",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
- "main": "mod.ts",
5
+ "main": "index.js",
6
6
  "scripts": {
7
- "code": "opencode web --hostname 0.0.0.0"
7
+ "build": "bun run bun.config.ts"
8
8
  },
9
9
  "keywords": [],
10
10
  "files": [
11
+ "dist",
11
12
  "src",
12
13
  "mod.ts",
13
14
  "agent"
14
15
  ],
15
16
  "author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
16
17
  "license": "MIT",
17
- "packageManager": "pnpm@10.28.0",
18
+ "packageManager": "pnpm@10.28.2",
18
19
  "type": "module",
19
20
  "devDependencies": {
21
+ "@kevisual/ai": "^0.0.24",
20
22
  "@kevisual/context": "^0.0.4",
21
- "@kevisual/types": "^0.0.10",
22
- "@opencode-ai/plugin": "^1.1.13",
23
- "@types/bun": "^1.3.5",
24
- "@types/node": "^25.0.6",
23
+ "@kevisual/types": "^0.0.12",
24
+ "@opencode-ai/plugin": "^1.1.39",
25
+ "@types/bun": "^1.3.7",
26
+ "@types/node": "^25.1.0",
25
27
  "dotenv": "^17.2.3"
26
28
  },
27
29
  "publishConfig": {
28
30
  "access": "public"
29
31
  },
30
- "exports": {
31
- ".": {
32
- "import": "./mod.ts",
33
- "types": "./mod.d.ts"
34
- },
35
- "./agent": {
36
- "import": "./agent/index.ts",
37
- "types": "./agent/index.d.ts"
38
- },
39
- "./opencode": {
40
- "import": "./agent/opencode-plugin.ts",
41
- "types": "./agent/opencode-plugin.d.ts"
42
- }
43
- },
44
32
  "dependencies": {
45
- "@kevisual/query": "^0.0.35",
46
- "@kevisual/router": "^0.0.52",
47
- "@kevisual/use-config": "^1.0.24",
48
- "es-toolkit": "^1.43.0",
49
- "nanoid": "^5.1.6"
33
+ "@kevisual/query": "^0.0.38",
34
+ "@kevisual/router": "^0.0.63",
35
+ "@kevisual/use-config": "^1.0.28",
36
+ "es-toolkit": "^1.44.0",
37
+ "nanoid": "^5.1.6",
38
+ "zod": "^4.3.6"
39
+ },
40
+ "exports": {
41
+ ".": "./mod.ts",
42
+ "./opencode": "./dist/opencode.js",
43
+ "./src/*": "./src/*",
44
+ "./agent/*": "./agent/*"
50
45
  }
51
46
  }
package/src/cnb-core.ts CHANGED
@@ -69,12 +69,12 @@ export class CNBCore {
69
69
  headers: _headers,
70
70
  body: _body,
71
71
  });
72
- const res = (data: any, message?: string) => {
72
+ const res = (data: any, message?: string, code?: number) => {
73
73
  if (useOrigin) {
74
74
  return data;
75
75
  }
76
76
  return {
77
- code: 200,
77
+ code: code ?? 200,
78
78
  message: message || 'success',
79
79
  data,
80
80
  };
@@ -83,7 +83,7 @@ export class CNBCore {
83
83
  const errorText = await response.text();
84
84
  if (useOrigin)
85
85
  throw new Error(`Request failed with status ${response.status}: ${errorText}`);
86
- return res(null, `Request failed with status ${response.status}: ${errorText}`);
86
+ return res(null, `Request failed with status ${response.status}: ${errorText}`, response.status);
87
87
  }
88
88
 
89
89
  const contentType = response.headers.get('Content-Type');
@@ -0,0 +1,57 @@
1
+
2
+ /**
3
+ * CNB 环境变量配置
4
+ * 该模块定义了 CNB (Cloud Native Build) 平台提供的所有环境变量
5
+ * 用于获取当前构建环境、仓库信息、运行器配置等元数据
6
+ */
7
+ import { useKey } from "@kevisual/use-config"
8
+
9
+ export const CNB_ENV = {
10
+ // 仓库相关配置
11
+ /** 仓库的 HTTPS 地址,如 "https://cnb.cool/kevisual/cnb" */
12
+ CNB_REPO_URL_HTTPS: useKey('CNB_REPO_URL_HTTPS'),
13
+
14
+ // 构建相关配置
15
+ /** 流水线 ID,唯一标识一次构建流水线,如 "cnb-upo-1jfth1771-001" */
16
+ CNB_PIPELINE_ID: useKey('CNB_PIPELINE_ID'),
17
+ /** 构建 ID,与流水线 ID 相关联,如 "cnb-upo-1jfth1771" */
18
+ CNB_BUILD_ID: useKey('CNB_BUILD_ID'),
19
+ /** 构建开始时间,ISO 8601 格式,如 "2026-01-13T07:58:41.825Z" */
20
+ CNB_BUILD_START_TIME: useKey('CNB_BUILD_START_TIME'),
21
+ /** 构建日志 Web 界面 URL,用于在浏览器中查看构建日志 */
22
+ CNB_BUILD_WEB_URL: useKey('CNB_BUILD_WEB_URL'),
23
+ /** 触发构建的事件类型,如 "vscode" 表示由 VS Code 触发 */
24
+ CNB_EVENT: useKey('CNB_EVENT'),
25
+ /** 当前构建对应的 Git 提交哈希值 */
26
+ CNB_COMMIT: useKey('CNB_COMMIT'),
27
+
28
+ // VS Code 相关配置
29
+ /** VS Code Web 界面的访问 URL,用于在浏览器中打开 VS Code,例如:'https://cnb.cool/kevisual/cnb/-/workspace/vscode-web/cnb-upo-1jfth1771-001/'*/
30
+ CNB_VSCODE_WEB_URL: useKey('CNB_VSCODE_WEB_URL'),
31
+ /** VS Code 代理 URI,用于端口转发,{{port}} 会被替换为实际端口号, 例如: "https://1wnts22gq3-{{port}}.cnb.run"*/
32
+ CNB_VSCODE_PROXY_URI: useKey('CNB_VSCODE_PROXY_URI'),
33
+ /**
34
+ * VS Code Remote SSH 连接字符串,例如: vscode://vscode-remote/ssh-remote+cnb-upo-1jfth1771-001.8939f3d1-de13-486e-921f-f07943fcfa28-qng@cnb.space/workspace/"
35
+ * 是CNB_PIPELINE_ID和CNB_VSCODE_SSH_TOKEN的组合
36
+ */
37
+ CNB_VSCODE_REMOTE_SSH_SCHEMA: useKey('CNB_VSCODE_REMOTE_SSH_SCHEMA'),
38
+ /**
39
+ * VS Code Remote SSH 连接的认证 Token 8939f3d1-de13-486e-921f-f07943fcfa28-qng
40
+ * 组装为ssh的链接字符串是 ssh CNB_PIPELINE_ID + '.' + CNB_VSCODE_SSH_TOKEN@cnb.space
41
+ */
42
+ CNB_VSCODE_SSH_TOKEN: useKey('CNB_VSCODE_SSH_TOKEN'),
43
+
44
+ // 仓库标识配置
45
+ /** 仓库标识符,格式为 "组名/仓库名",如 "kevisual/cnb" */
46
+ CNB_REPO_SLUG: useKey('CNB_REPO_SLUG'),
47
+ /** 组名/命名空间标识符,如 "kevisual" */
48
+ CNB_GROUP_SLUG: useKey('CNB_GROUP_SLUG'),
49
+
50
+ // 运行器资源配置
51
+ /** 运行器分配的 CPU 核心数,单位为核, 例如: "8"*/
52
+ CNB_CPUS: useKey('CNB_CPUS'),
53
+ /** 运行器分配的内存大小,单位为 GB, 例如: "16"*/
54
+ CNB_MEMORY: useKey('CNB_MEMORY'),
55
+ /** 运行器的 IP 地址,用于网络连接和调试 */
56
+ CNB_RUNNER_IP: useKey('CNB_RUNNER_IP'),
57
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { CNBCore, CNBCoreOptions } from "./cnb-core.ts";
2
- import { Workspace } from "./workspace.ts";
2
+ import { Workspace } from "./workspace/index.ts";
3
3
  import { KnowledgeBase } from "./knowledge/index.ts";
4
4
  import { Repo } from "./repo/index.ts";
5
5
  import { User } from "./user/index.ts";
@@ -9,7 +9,6 @@ import { Mission } from "./mission/index.ts";
9
9
  import { AiBase } from "./ai/index.ts";
10
10
 
11
11
  type CNBOptions = CNBCoreOptions<{
12
- group?: string;
13
12
  }>;
14
13
 
15
14
  export class CNB extends CNBCore {
@@ -21,7 +20,6 @@ export class CNB extends CNBCore {
21
20
  issue!: Issue;
22
21
  mission!: Mission;
23
22
  ai!: AiBase;
24
- group!: string;
25
23
  constructor(options: CNBOptions) {
26
24
  super({ token: options.token, cookie: options.cookie, cnb: options.cnb });
27
25
  this.init(options);
@@ -31,7 +29,6 @@ export class CNB extends CNBCore {
31
29
  const cookie = this.cookie;
32
30
  const options = { token, cookie };
33
31
  this.workspace = new Workspace(options.token);
34
- const group = cnbOptions?.group || '';
35
32
  this.knowledgeBase = new KnowledgeBase({ token: options.token, cookie: options.cookie });
36
33
  this.repo = new Repo({ token: options.token, cookie: options.cookie });
37
34
  this.user = new User({ token: options.token, cookie: options.cookie });
@@ -39,10 +36,6 @@ export class CNB extends CNBCore {
39
36
  this.issue = new Issue({ token: options.token, cookie: options.cookie });
40
37
  this.mission = new Mission({ token: options.token, cookie: options.cookie });
41
38
  this.ai = new AiBase({ token: options.token, cookie: options.cookie });
42
- this.group = group;
43
- }
44
- setGroup(group: string) {
45
- this.group = group;
46
39
  }
47
40
  setToken(token: string) {
48
41
  this.token = token;
@@ -64,7 +57,7 @@ export class CNB extends CNBCore {
64
57
  }
65
58
  }
66
59
 
67
- export * from './workspace.ts'
60
+ export * from './workspace/index.ts'
68
61
  export * from './cnb-core.ts'
69
62
  export * from './knowledge/index.ts'
70
63
  export * from './repo/index.ts'
@@ -26,6 +26,7 @@ export type IssueItem = {
26
26
  author: IssueAuthor;
27
27
  labels: IssueLabel[];
28
28
 
29
+ body: string;
29
30
  last_acted_at: string;
30
31
  number: string;
31
32
  priority: string;
@@ -10,7 +10,7 @@ export class KnowledgeBase extends CNBCore {
10
10
  score_threshold?: number,
11
11
  top_k?: number,
12
12
  metadata_filtering_conditions?: MetadataFilteringConditions
13
- }): Promise<any> {
13
+ }): Promise<Result<QueryRag[]>> {
14
14
  const url = `/${repo}/-/knowledge/base/query`;
15
15
  let postData = {
16
16
  query: data.query,
@@ -43,4 +43,17 @@ type MetadataFilteringConditions = {
43
43
  value?: string
44
44
  }>
45
45
  logical_operator?: 'adn' | 'or'
46
+ }
47
+
48
+ type QueryRag = {
49
+ chunk: string;
50
+ score: number;
51
+ metadata: {
52
+ hash: string;
53
+ name: string;
54
+ path: string;
55
+ position: string;
56
+ type: string; // code, text
57
+ url: string;
58
+ }
46
59
  }
package/src/repo/index.ts CHANGED
@@ -6,27 +6,32 @@ export class Repo extends CNBCore {
6
6
  }
7
7
  /**
8
8
  * 创建代码仓库
9
- * @param group e.g. my-group
10
9
  * @param data
11
10
  * @returns
12
11
  */
13
- createRepo(group: string, data: CreateRepoData): Promise<any> {
12
+ createRepo(data: CreateRepoData): Promise<any> {
13
+ const name = data.name;
14
+ const [group, repo] = name.includes('/') ? name.split('/') : ['', name];
14
15
  const url = `/${group}/-/repos`;
15
16
  let postData: CreateRepoData = {
16
17
  ...data,
17
18
  description: data.description || '',
18
- name: data.name,
19
+ name: repo,
19
20
  license: data.license || 'Unlicense',
20
21
  visibility: data.visibility || 'private',
21
22
  };
22
23
  return this.post({ url, data: postData });
23
24
  }
25
+ deleteRepo(name: string): Promise<any> {
26
+ const url = `https://cnb.cool/${name}`;
27
+ return this.delete({ url, useCookie: true });
28
+ }
24
29
  async createCommit(repo: string, data: CreateCommitData): Promise<any> {
25
30
  const commitList = await this.getCommitList(repo, {
26
31
  page: 1,
27
32
  page_size: 1,
28
33
  }, { useOrigin: true }).catch((err) => {
29
- console.error("Error fetching commit list:", err);
34
+ // console.error("Error fetching commit list:", err);
30
35
  return []
31
36
  });
32
37
  const preCommitSha = commitList.length > 0 ? commitList[0].sha : undefined;
@@ -46,7 +51,14 @@ export class Repo extends CNBCore {
46
51
  delete postData.parent_commit_sha;
47
52
  delete postData.base_branch;
48
53
  }
49
- return this.post({ url, data: postData, useCookie: true, });
54
+ return this.post({
55
+ url,
56
+ data: postData,
57
+ useCookie: true,
58
+ headers: {
59
+ 'Accept': 'application/vnd.cnb.web+json',
60
+ }
61
+ });
50
62
  }
51
63
  createBlobs(repo: string, data: { content: string, encoding?: 'utf-8' | 'base64' }): Promise<any> {
52
64
  const url = `/${repo}/-/git/blobs`;
@@ -77,10 +89,15 @@ export class Repo extends CNBCore {
77
89
  }): Promise<Result<RepoItem[]>> {
78
90
  const url = '/user/repos';
79
91
  let _params = {
92
+ role: 'developer',
93
+ status: 'active',
80
94
  ...params,
81
95
  page: params.page || 1,
82
96
  page_size: params.page_size || 999,
83
97
  }
98
+ if(!_params.search) {
99
+ delete _params.search;
100
+ }
84
101
  return this.get({ url, params: _params });
85
102
  }
86
103
  }
@@ -6,7 +6,7 @@
6
6
  */
7
7
 
8
8
  import { Result } from "@kevisual/query/query";
9
- import { CNBCore } from "./cnb-core.ts";
9
+ import { CNBCore } from "../cnb-core.ts";
10
10
 
11
11
  /**
12
12
  * 工作空间列表查询参数
@@ -54,7 +54,7 @@ export class Workspace extends CNBCore {
54
54
  return this.post({ url: '/workspace/delete', data });
55
55
  }
56
56
  /** 获取我的云原生开发环境列表 */
57
- async list(params?: ListParams): Promise<WorkspaceResult> {
57
+ async list(params?: ListParams): Promise<Result<WorkspaceResult>> {
58
58
  return this.get({ url: '/workspace/list', params });
59
59
  }
60
60
 
@@ -1,35 +0,0 @@
1
- import { tool } from "@opencode-ai/plugin/tool"
2
- import { type Plugin } from "@opencode-ai/plugin"
3
- import { app, cnb, appId } from './index.ts';
4
-
5
- // opencode run "请使用 cnb-login-verify 工具验证登录信信息,检查cookie"
6
- export const CnbPlugin: Plugin = async ({ project, client, $, directory, worktree }) => {
7
- return {
8
- 'tool': {
9
- "cnb-login-verify": {
10
- name: "CNB 登录验证信息",
11
- description: "验证 CNB 登录信息是否有效",
12
- args: {
13
- checkToken: tool.schema.boolean().describe("是否检查 Token 的有效性").default(true),
14
- checkCookie: tool.schema.boolean().describe("是否检查 Cookie 的有效性").default(false),
15
- },
16
- async execute(args) {
17
- const res = await app.run({
18
- path: 'cnb',
19
- key: 'user-check',
20
- payload: {
21
- ...args
22
- }
23
- }, { appId });
24
- if (res.code === 200) {
25
- return res.data?.output;
26
- }
27
- return '无法获取登录状态,请检查配置。';
28
- },
29
- },
30
- },
31
- 'tool.execute.before': async (opts) => {
32
- // console.log('CnbPlugin: tool.execute.before', opts.tool);
33
- }
34
- }
35
- }