@dmall-inc/wd-cli 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,108 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 凭证取值(/api/open/credentials/**)。
5
+ *
6
+ * 明文默认不打印:列表与取值都只显示「已配置」标记,确实要拿明文才加 --reveal。
7
+ * 这样 `wd-cli credential get x` 贴进工单、粘进聊天窗口时不会顺手泄密。
8
+ */
9
+
10
+ const http = require('../http');
11
+ const output = require('../output');
12
+ const { CliError, EXIT } = output;
13
+
14
+ const MASK = '<configured>';
15
+
16
+ /**
17
+ * 登录态用 --agent-code 指明目标应用(服务端按用户身份解析凭证);
18
+ * 令牌直连(Agent 访问密钥)时主体自带应用,用 --session-id 定位发起用户。
19
+ */
20
+ function scopeQuery(ctx) {
21
+ const query = { mode: ctx.flags.mode };
22
+ if (ctx.flags.sessionId) query.sessionId = ctx.flags.sessionId;
23
+ if (ctx.flags.agentCode) query.agentCode = ctx.flags.agentCode;
24
+ if (ctx.principal === 'user' && !ctx.flags.agentCode) {
25
+ throw new CliError('缺少 --agent-code', EXIT.USAGE, {
26
+ hint: '登录态下需指明目标应用:--agent-code <agentId>',
27
+ });
28
+ }
29
+ if (ctx.principal !== 'user' && !ctx.flags.sessionId) {
30
+ throw new CliError('缺少 --session-id', EXIT.USAGE, {
31
+ hint: '访问密钥模式下按会话反解发起用户:--session-id <sessionId>',
32
+ });
33
+ }
34
+ return query;
35
+ }
36
+
37
+ const COMMON_FLAGS = {
38
+ 'session-id': { type: 'string', summary: '会话 id(访问密钥模式必填)' },
39
+ 'agent-code': { type: 'string', summary: '应用标识(登录态必填)' },
40
+ mode: { type: 'string', summary: '解析模式:USER_ONLY | AGENT_ONLY | AGENT_FIRST | USER_FIRST' },
41
+ };
42
+
43
+ const list = {
44
+ name: 'list',
45
+ summary: '列出可用凭证(脱敏)',
46
+ flags: COMMON_FLAGS,
47
+ examples: ['wd-cli credential list --agent-code 66'],
48
+ async run(ctx) {
49
+ const items = (await http.request(ctx, { path: '/credentials', query: scopeQuery(ctx) })) || [];
50
+ output.print(items, {
51
+ flags: ctx.flags,
52
+ columns: [
53
+ { key: 'bizKey', label: '标识' },
54
+ { key: 'name', label: '名称' },
55
+ { key: 'scope', label: '归属' },
56
+ { key: 'status', label: '状态' },
57
+ ],
58
+ empty: '没有可用凭证',
59
+ });
60
+ return EXIT.OK;
61
+ },
62
+ };
63
+
64
+ const get = {
65
+ name: 'get',
66
+ summary: '按标识取凭证(默认脱敏)',
67
+ args: [{ name: 'key', required: true, summary: '凭证标识(biz_key)' }],
68
+ flags: { ...COMMON_FLAGS, reveal: { type: 'boolean', summary: '输出明文(谨慎使用)' } },
69
+ examples: ['wd-cli credential get feishu-bot --agent-code 66', 'wd-cli credential get feishu-bot --agent-code 66 --reveal'],
70
+ async run(ctx) {
71
+ const result = await http.request(ctx, {
72
+ path: `/credentials/${encodeURIComponent(ctx.args[0])}`,
73
+ query: scopeQuery(ctx),
74
+ });
75
+ if (!ctx.flags.reveal && result && typeof result === 'object' && result.value !== undefined) {
76
+ output.print({ ...result, value: result.value ? MASK : '' }, { flags: ctx.flags });
77
+ output.info('明文已隐藏,确需输出请加 --reveal', ctx.flags);
78
+ return EXIT.OK;
79
+ }
80
+ output.print(result, { flags: ctx.flags });
81
+ return EXIT.OK;
82
+ },
83
+ };
84
+
85
+ const authorizeUrl = {
86
+ name: 'authorize-url',
87
+ summary: '获取凭证授权地址(供用户去完成授权)',
88
+ args: [{ name: 'key', required: true, summary: '凭证标识' }],
89
+ flags: COMMON_FLAGS,
90
+ examples: ['wd-cli credential authorize-url feishu-bot --agent-code 66'],
91
+ async run(ctx) {
92
+ const result = await http.request(ctx, {
93
+ path: `/credentials/${encodeURIComponent(ctx.args[0])}/authorize-url`,
94
+ query: scopeQuery(ctx),
95
+ });
96
+ output.print(result, { flags: ctx.flags });
97
+ return EXIT.OK;
98
+ },
99
+ };
100
+
101
+ module.exports = {
102
+ id: 'credential',
103
+ summary: '凭证:列出 / 取值 / 授权地址',
104
+ description: '明文默认不打印;批量脚本请确认输出去向后再用 --reveal。',
105
+ principals: ['agent'],
106
+ commands: [list, get, authorizeUrl],
107
+ MASK,
108
+ };
@@ -0,0 +1,144 @@
1
+ 'use strict';
2
+
3
+ /** 评测数据集(/api/open/datasets/**)。 */
4
+
5
+ const http = require('../http');
6
+ const dataInput = require('../data');
7
+ const output = require('../output');
8
+ const { EXIT } = output;
9
+
10
+ const agentArg = { name: 'agentCode', required: true, summary: '应用标识' };
11
+ const idArg = { name: 'id', required: true, summary: '数据集 id' };
12
+
13
+ function base(ctx) {
14
+ return `/datasets/${encodeURIComponent(ctx.args[0])}`;
15
+ }
16
+
17
+ const commands = [
18
+ {
19
+ name: 'list',
20
+ summary: '列出数据集',
21
+ args: [agentArg],
22
+ examples: ['wd-cli dataset list 66'],
23
+ async run(ctx) {
24
+ const result = await http.request(ctx, { path: base(ctx) });
25
+ output.print(result, {
26
+ flags: ctx.flags,
27
+ columns: [
28
+ { key: 'id', label: 'ID' },
29
+ { key: 'name', label: '名称' },
30
+ { key: 'itemCount', label: '条目数' },
31
+ { key: 'updatedAt', label: '更新时间' },
32
+ ],
33
+ empty: '还没有数据集',
34
+ });
35
+ return EXIT.OK;
36
+ },
37
+ },
38
+ {
39
+ name: 'create',
40
+ summary: '创建数据集',
41
+ args: [agentArg],
42
+ flags: { data: { type: 'string', summary: '数据集体 JSON:内联 / @file / -' } },
43
+ examples: ['wd-cli dataset create 66 --data \'{"name":"回归集"}\''],
44
+ async run(ctx) {
45
+ const body = dataInput.requireJsonData(ctx.flags.data);
46
+ const result = await http.request(ctx, { method: 'POST', path: base(ctx), body });
47
+ output.print(result, { flags: ctx.flags });
48
+ output.success('已创建数据集', ctx.flags);
49
+ return EXIT.OK;
50
+ },
51
+ },
52
+ {
53
+ name: 'get',
54
+ summary: '查看数据集',
55
+ args: [agentArg, idArg],
56
+ examples: ['wd-cli dataset get 66 3'],
57
+ async run(ctx) {
58
+ const result = await http.request(ctx, { path: `${base(ctx)}/${encodeURIComponent(ctx.args[1])}` });
59
+ output.print(result, { flags: ctx.flags });
60
+ return EXIT.OK;
61
+ },
62
+ },
63
+ {
64
+ name: 'update',
65
+ summary: '更新数据集',
66
+ args: [agentArg, idArg],
67
+ flags: { data: { type: 'string', summary: '更新体 JSON:内联 / @file / -' } },
68
+ examples: ['wd-cli dataset update 66 3 --data @dataset.json'],
69
+ async run(ctx) {
70
+ const body = dataInput.requireJsonData(ctx.flags.data);
71
+ const result = await http.request(ctx, {
72
+ method: 'PUT',
73
+ path: `${base(ctx)}/${encodeURIComponent(ctx.args[1])}`,
74
+ body,
75
+ });
76
+ output.print(result, { flags: ctx.flags });
77
+ output.success('已更新数据集', ctx.flags);
78
+ return EXIT.OK;
79
+ },
80
+ },
81
+ {
82
+ name: 'delete',
83
+ summary: '删除数据集',
84
+ args: [agentArg, idArg],
85
+ examples: ['wd-cli dataset delete 66 3 --yes'],
86
+ async run(ctx) {
87
+ const ok = await dataInput.confirm(`确认删除数据集 ${ctx.args[1]}?`, ctx.flags);
88
+ if (!ok) {
89
+ output.info('已取消', ctx.flags);
90
+ return EXIT.OK;
91
+ }
92
+ await http.request(ctx, { method: 'DELETE', path: `${base(ctx)}/${encodeURIComponent(ctx.args[1])}` });
93
+ output.success('已删除数据集', ctx.flags);
94
+ return EXIT.OK;
95
+ },
96
+ },
97
+ {
98
+ name: 'items',
99
+ summary: '列出数据集条目',
100
+ args: [agentArg, idArg],
101
+ examples: ['wd-cli dataset items 66 3'],
102
+ async run(ctx) {
103
+ const result = await http.request(ctx, { path: `${base(ctx)}/${encodeURIComponent(ctx.args[1])}/items` });
104
+ output.print(result, { flags: ctx.flags, empty: '该数据集还没有条目' });
105
+ return EXIT.OK;
106
+ },
107
+ },
108
+ {
109
+ name: 'items add',
110
+ summary: '追加数据集条目',
111
+ args: [agentArg, idArg],
112
+ flags: { data: { type: 'string', summary: '条目 JSON:内联 / @file / -' } },
113
+ examples: ['wd-cli dataset items add 66 3 --data @items.json'],
114
+ async run(ctx) {
115
+ const body = dataInput.requireJsonData(ctx.flags.data);
116
+ const result = await http.request(ctx, {
117
+ method: 'POST',
118
+ path: `${base(ctx)}/${encodeURIComponent(ctx.args[1])}/items`,
119
+ body,
120
+ });
121
+ output.print(result, { flags: ctx.flags });
122
+ output.success('已追加条目', ctx.flags);
123
+ return EXIT.OK;
124
+ },
125
+ },
126
+ {
127
+ name: 'test-cases',
128
+ summary: '列出可用测试用例',
129
+ args: [agentArg],
130
+ examples: ['wd-cli dataset test-cases 66'],
131
+ async run(ctx) {
132
+ const result = await http.request(ctx, { path: `${base(ctx)}/test-cases` });
133
+ output.print(result, { flags: ctx.flags, empty: '没有可用测试用例' });
134
+ return EXIT.OK;
135
+ },
136
+ },
137
+ ];
138
+
139
+ module.exports = {
140
+ id: 'dataset',
141
+ summary: '评测数据集:增删改查与条目',
142
+ principals: ['agent'],
143
+ commands,
144
+ };
@@ -0,0 +1,170 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 评测:计划(plan)、执行(execute)、评测任务(task)。
5
+ * 端点对应 /api/open/evaluate/** 与 /api/open/evaluations/**。
6
+ */
7
+
8
+ const http = require('../http');
9
+ const dataInput = require('../data');
10
+ const output = require('../output');
11
+ const { EXIT } = output;
12
+
13
+ const agentArg = { name: 'agentCode', required: true, summary: '应用标识' };
14
+
15
+ const commands = [
16
+ {
17
+ name: 'plan list',
18
+ summary: '列出评测计划',
19
+ flags: { 'agent-code': { type: 'string', summary: '应用标识(登录态必填)' } },
20
+ examples: ['wd-cli eval plan list --agent-code 66'],
21
+ async run(ctx) {
22
+ const result = await http.request(ctx, {
23
+ path: '/evaluate/plans',
24
+ query: { agentCode: ctx.flags.agentCode },
25
+ });
26
+ output.print(result, {
27
+ flags: ctx.flags,
28
+ columns: [
29
+ { key: 'id', label: 'ID' },
30
+ { key: 'name', label: '名称' },
31
+ { key: 'status', label: '状态' },
32
+ ],
33
+ empty: '没有评测计划',
34
+ });
35
+ return EXIT.OK;
36
+ },
37
+ },
38
+ {
39
+ name: 'plan items',
40
+ summary: '列出计划下的评测项',
41
+ args: [{ name: 'planId', required: true, summary: '计划 id' }],
42
+ flags: { 'agent-code': { type: 'string', summary: '应用标识(登录态必填)' } },
43
+ examples: ['wd-cli eval plan items 8 --agent-code 66'],
44
+ async run(ctx) {
45
+ const result = await http.request(ctx, {
46
+ path: `/evaluate/plans/${encodeURIComponent(ctx.args[0])}/items`,
47
+ query: { agentCode: ctx.flags.agentCode },
48
+ });
49
+ output.print(result, { flags: ctx.flags, empty: '该计划下没有评测项' });
50
+ return EXIT.OK;
51
+ },
52
+ },
53
+ {
54
+ name: 'plan update',
55
+ summary: '更新某个评测项',
56
+ args: [
57
+ { name: 'planId', required: true, summary: '计划 id' },
58
+ { name: 'itemId', required: true, summary: '评测项 id' },
59
+ ],
60
+ flags: {
61
+ data: { type: 'string', summary: '更新体 JSON:内联 / @file / -' },
62
+ 'agent-code': { type: 'string', summary: '应用标识(登录态必填)' },
63
+ },
64
+ examples: ['wd-cli eval plan update 8 15 --data @item.json --agent-code 66'],
65
+ async run(ctx) {
66
+ const body = dataInput.requireJsonData(ctx.flags.data);
67
+ const result = await http.request(ctx, {
68
+ method: 'PUT',
69
+ path: `/evaluate/plans/${encodeURIComponent(ctx.args[0])}/items/${encodeURIComponent(ctx.args[1])}`,
70
+ query: { agentCode: ctx.flags.agentCode },
71
+ body,
72
+ });
73
+ output.print(result, { flags: ctx.flags });
74
+ output.success('已更新评测项', ctx.flags);
75
+ return EXIT.OK;
76
+ },
77
+ },
78
+ {
79
+ name: 'execute',
80
+ summary: '触发一次评测执行',
81
+ flags: {
82
+ data: { type: 'string', summary: '执行参数 JSON:内联 / @file / -' },
83
+ 'agent-code': { type: 'string', summary: '应用标识(登录态必填)' },
84
+ },
85
+ examples: ['wd-cli eval execute --data @run.json --agent-code 66'],
86
+ async run(ctx) {
87
+ const body = dataInput.requireJsonData(ctx.flags.data);
88
+ const result = await http.request(ctx, {
89
+ method: 'POST',
90
+ path: '/evaluate/execute',
91
+ query: { agentCode: ctx.flags.agentCode },
92
+ body,
93
+ // 评测执行可能跑很久,给到与流式对话同级的上限
94
+ });
95
+ output.print(result, { flags: ctx.flags });
96
+ return EXIT.OK;
97
+ },
98
+ },
99
+ {
100
+ name: 'task list',
101
+ summary: '列出评测任务',
102
+ args: [agentArg],
103
+ examples: ['wd-cli eval task list 66'],
104
+ async run(ctx) {
105
+ const result = await http.request(ctx, { path: `/evaluations/${encodeURIComponent(ctx.args[0])}` });
106
+ output.print(result, {
107
+ flags: ctx.flags,
108
+ columns: [
109
+ { key: 'id', label: 'ID' },
110
+ { key: 'name', label: '名称' },
111
+ { key: 'status', label: '状态' },
112
+ { key: 'createdAt', label: '创建时间' },
113
+ ],
114
+ empty: '没有评测任务',
115
+ });
116
+ return EXIT.OK;
117
+ },
118
+ },
119
+ {
120
+ name: 'task create',
121
+ summary: '创建评测任务',
122
+ args: [agentArg],
123
+ flags: { data: { type: 'string', summary: '任务体 JSON:内联 / @file / -' } },
124
+ examples: ['wd-cli eval task create 66 --data @task.json'],
125
+ async run(ctx) {
126
+ const body = dataInput.requireJsonData(ctx.flags.data);
127
+ const result = await http.request(ctx, {
128
+ method: 'POST',
129
+ path: `/evaluations/${encodeURIComponent(ctx.args[0])}`,
130
+ body,
131
+ });
132
+ output.print(result, { flags: ctx.flags });
133
+ output.success('已创建评测任务', ctx.flags);
134
+ return EXIT.OK;
135
+ },
136
+ },
137
+ {
138
+ name: 'task get',
139
+ summary: '查看评测任务',
140
+ args: [agentArg, { name: 'id', required: true, summary: '任务 id' }],
141
+ examples: ['wd-cli eval task get 66 12'],
142
+ async run(ctx) {
143
+ const result = await http.request(ctx, {
144
+ path: `/evaluations/${encodeURIComponent(ctx.args[0])}/${encodeURIComponent(ctx.args[1])}`,
145
+ });
146
+ output.print(result, { flags: ctx.flags });
147
+ return EXIT.OK;
148
+ },
149
+ },
150
+ {
151
+ name: 'task items',
152
+ summary: '查看评测任务的明细项',
153
+ args: [agentArg, { name: 'id', required: true, summary: '任务 id' }],
154
+ examples: ['wd-cli eval task items 66 12'],
155
+ async run(ctx) {
156
+ const result = await http.request(ctx, {
157
+ path: `/evaluations/${encodeURIComponent(ctx.args[0])}/${encodeURIComponent(ctx.args[1])}/items`,
158
+ });
159
+ output.print(result, { flags: ctx.flags, empty: '该任务还没有明细' });
160
+ return EXIT.OK;
161
+ },
162
+ },
163
+ ];
164
+
165
+ module.exports = {
166
+ id: 'eval',
167
+ summary: '评测:计划 / 执行 / 任务',
168
+ principals: ['agent'],
169
+ commands,
170
+ };
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 文档主题(对标 gh help environment / gh help exit-codes):
5
+ * 这些不是能执行的命令,而是「一次说清楚」的参考页,省得用户翻 README。
6
+ */
7
+
8
+ const output = require('../output');
9
+ const { CliError, EXIT } = output;
10
+
11
+ const ENVIRONMENT = `环境变量
12
+
13
+ WD_CLI_BASE_URL 平台基址,等价于 --base-url
14
+ WD_CLI_TOKEN 直连令牌(wat_ 工作空间令牌或 Agent 访问密钥)。设置后压过登录态,
15
+ CI 里用它最省事;语义与 gh 的 GH_TOKEN 一致
16
+ WD_CLI_WORKSPACE 本次调用使用的工作空间 id,等价于 --workspace
17
+ WD_CLI_PROFILE 使用哪个 profile,等价于 --profile
18
+ WD_CLI_TIMEOUT_MS 请求超时毫秒数,等价于 --timeout-ms
19
+ WD_CLI_CHAT_BASE_URL 对话端点基址(与其余 Open API 不同域名时用)
20
+ WD_CLI_CONFIG_DIR 配置目录,覆盖默认的 XDG 位置
21
+ XDG_CONFIG_HOME 未设 WD_CLI_CONFIG_DIR 时,配置放在 $XDG_CONFIG_HOME/wd-cli
22
+ NO_COLOR 置任意非空值即关闭彩色输出
23
+
24
+ 配置文件
25
+
26
+ <配置目录>/config.json profile 的基址、认证中心、当前工作空间等(可进 dotfiles)
27
+ <配置目录>/hosts.json 登录令牌与直连令牌,权限 0600(不要提交到仓库)
28
+
29
+ 默认配置目录:~/.config/wd-cli(Windows 为 %AppData%\\wd-cli)
30
+ 用 wd-cli config path 看当前实际位置。`;
31
+
32
+ const EXIT_CODES = `退出码
33
+
34
+ 0 成功
35
+ 1 业务失败。服务端明确返回了错误,比如资源不存在、参数被拒
36
+ 2 用法错误。参数写错、缺必填项、当前凭据类型不对、还没选工作空间
37
+ 3 鉴权失败。令牌无效 / 过期 / 无权访问;登录态会先自动刷新一次,仍失败才报这个
38
+ 4 网络问题。连不上、超时,或响应不是能解析的内容
39
+
40
+ 脚本里这样用:
41
+
42
+ if wd-cli session exists 66 s-1 >/dev/null; then
43
+ echo "会话还在"
44
+ fi
45
+
46
+ wd-cli skill list --output json || case $? in
47
+ 3) echo "该重新登录了" ;;
48
+ 4) echo "网络不通" ;;
49
+ esac`;
50
+
51
+ const TOPICS = {
52
+ environment: ENVIRONMENT,
53
+ 'exit-codes': EXIT_CODES,
54
+ };
55
+
56
+ const run = {
57
+ name: '',
58
+ summary: '查看参考文档主题',
59
+ args: [{ name: 'topic', required: true, summary: `environment | exit-codes` }],
60
+ examples: ['wd-cli help environment', 'wd-cli help exit-codes'],
61
+ skipPrincipalCheck: true,
62
+ workspaceOptional: true,
63
+ async run(ctx) {
64
+ const topic = String(ctx.args[0]);
65
+ const text = TOPICS[topic];
66
+ if (!text) {
67
+ throw new CliError(`没有这个文档主题:${topic}`, EXIT.USAGE, {
68
+ hint: `可用主题:${Object.keys(TOPICS).join(' / ')}`,
69
+ });
70
+ }
71
+ process.stdout.write(`${text}\n`);
72
+ return EXIT.OK;
73
+ },
74
+ };
75
+
76
+ module.exports = {
77
+ id: 'help',
78
+ summary: '参考文档:环境变量 / 退出码',
79
+ commands: [run],
80
+ TOPICS,
81
+ };
@@ -0,0 +1,46 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 命令表(oclif 风格的声明式注册)。
5
+ *
6
+ * 每个 topic 一个模块,导出 { id, summary, description?, principals?, commands: [...] }。
7
+ * 新增资源组只需在这里加一行 require——帮助文本、参数解析、补全脚本都从命令表生成,
8
+ * 不需要在别处再登记一遍。
9
+ */
10
+
11
+ const TOPIC_MODULES = [
12
+ require('./auth'),
13
+ require('./workspace'),
14
+ require('./config'),
15
+ require('./skill'),
16
+ require('./kb'),
17
+ require('./agent'),
18
+ require('./session'),
19
+ require('./credential'),
20
+ require('./evaluate'),
21
+ require('./dataset'),
22
+ require('./observability'),
23
+ require('./automation'),
24
+ require('./api'),
25
+ require('./completion'),
26
+ require('./help'),
27
+ ];
28
+
29
+ /**
30
+ * 展开命令表。一个模块通常就是一个 topic;少数模块(如可观测)天然带出几个同源 topic,
31
+ * 用 extraTopics 一并导出,避免为 trace / obs / score 各写一份几乎相同的查询代码。
32
+ */
33
+ function buildRegistry() {
34
+ const topics = {};
35
+ const list = [];
36
+ for (const mod of TOPIC_MODULES) {
37
+ for (const topic of [mod, ...(mod.extraTopics || [])]) {
38
+ if (!topic || !topic.id) continue;
39
+ topics[topic.id] = topic;
40
+ list.push(topic);
41
+ }
42
+ }
43
+ return { topics, list };
44
+ }
45
+
46
+ module.exports = { buildRegistry, TOPIC_MODULES };