@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,449 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 知识库:库本身、文档(vault)、目录、飞书同步源、授权、分享。
5
+ * 端点与 /api/open/knowledge-bases/** 一一对应;工作空间令牌与登录态都能访问(服务端双鉴权路径)。
6
+ */
7
+
8
+ const http = require('../http');
9
+ const dataInput = require('../data');
10
+ const output = require('../output');
11
+ const { EXIT } = output;
12
+
13
+ const KB_COLUMNS = [
14
+ { key: 'id', label: 'ID' },
15
+ { key: 'name', label: '名称' },
16
+ { key: 'scope', label: '范围' },
17
+ { key: 'documentCount', label: '文档数' },
18
+ { key: 'description', label: '简介' },
19
+ ];
20
+
21
+ const DOC_COLUMNS = [
22
+ { key: 'id', label: 'ID' },
23
+ { key: 'title', label: '标题' },
24
+ { key: 'fileType', label: '类型' },
25
+ { key: 'status', label: '状态' },
26
+ { key: 'updatedAt', label: '更新时间' },
27
+ ];
28
+
29
+ const kbArg = { name: 'kbId', required: true, summary: '知识库 id' };
30
+
31
+ /** 简化声明:大多数子命令就是「打某个路径、按某组列渲染」。 */
32
+ function simple({ name, summary, method = 'GET', path: pathBuilder, args = [], flags = {}, columns, examples, body, query, empty }) {
33
+ return {
34
+ name,
35
+ summary,
36
+ args,
37
+ flags,
38
+ examples,
39
+ async run(ctx) {
40
+ const result = await http.request(ctx, {
41
+ method,
42
+ path: pathBuilder(ctx),
43
+ query: query ? query(ctx) : undefined,
44
+ body: body ? body(ctx) : undefined,
45
+ });
46
+ output.print(result, { flags: ctx.flags, columns, empty });
47
+ return EXIT.OK;
48
+ },
49
+ };
50
+ }
51
+
52
+ /** 删除类:交互式二次确认,--yes 跳过。 */
53
+ function destructive({ name, summary, path: pathBuilder, args, confirmText, examples, method = 'DELETE', query }) {
54
+ return {
55
+ name,
56
+ summary,
57
+ args,
58
+ examples,
59
+ async run(ctx) {
60
+ const ok = await dataInput.confirm(confirmText(ctx), ctx.flags);
61
+ if (!ok) {
62
+ output.info('已取消', ctx.flags);
63
+ return EXIT.OK;
64
+ }
65
+ await http.request(ctx, { method, path: pathBuilder(ctx), query: query ? query(ctx) : undefined });
66
+ output.success('已删除', ctx.flags);
67
+ return EXIT.OK;
68
+ },
69
+ };
70
+ }
71
+
72
+ const commands = [
73
+ simple({
74
+ name: 'list',
75
+ summary: '列出可见知识库',
76
+ path: () => '/knowledge-bases',
77
+ columns: KB_COLUMNS,
78
+ examples: ['wd-cli kb list'],
79
+ empty: '当前工作空间还没有可见的知识库',
80
+ }),
81
+ simple({
82
+ name: 'get',
83
+ summary: '查看知识库详情',
84
+ args: [kbArg],
85
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}`,
86
+ examples: ['wd-cli kb get 12'],
87
+ }),
88
+ {
89
+ name: 'search',
90
+ summary: '在知识库内检索',
91
+ args: [kbArg],
92
+ flags: {
93
+ query: { type: 'string', summary: '检索词(必填)' },
94
+ 'top-k': { type: 'number', summary: '返回条数' },
95
+ catalog: { type: 'string', summary: '限定在某个目录下检索' },
96
+ },
97
+ examples: ['wd-cli kb search 12 --query 报销流程 --top-k 5'],
98
+ async run(ctx) {
99
+ const kbId = ctx.args[0];
100
+ const query = ctx.flags.query;
101
+ if (!query) {
102
+ throw new output.CliError('缺少 --query', EXIT.USAGE, { hint: '例:wd-cli kb search 12 --query 报销流程' });
103
+ }
104
+ const path = ctx.flags.catalog
105
+ ? `/knowledge-bases/${kbId}/catalogs/${ctx.flags.catalog}/search`
106
+ : `/knowledge-bases/${kbId}/search`;
107
+ const result = await http.request(ctx, {
108
+ method: 'POST',
109
+ path,
110
+ body: { query, topK: ctx.flags.topK },
111
+ });
112
+ output.print(result, {
113
+ flags: ctx.flags,
114
+ columns: [
115
+ { key: 'score', label: '相关度' },
116
+ { key: 'title', label: '标题' },
117
+ { key: 'content', label: '片段' },
118
+ ],
119
+ empty: '没有命中任何内容',
120
+ });
121
+ return EXIT.OK;
122
+ },
123
+ },
124
+ simple({
125
+ name: 'tags',
126
+ summary: '列出知识库标签',
127
+ args: [kbArg],
128
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/tags`,
129
+ examples: ['wd-cli kb tags 12'],
130
+ }),
131
+
132
+ // ---- 文档(vault) ----
133
+ simple({
134
+ name: 'doc list',
135
+ summary: '列出知识库文档',
136
+ args: [kbArg],
137
+ flags: { page: { type: 'number', summary: '页码' }, 'page-size': { type: 'number', summary: '每页条数' } },
138
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/vault/documents`,
139
+ query: (ctx) => ({ pageNum: ctx.flags.page, pageSize: ctx.flags.pageSize }),
140
+ columns: DOC_COLUMNS,
141
+ examples: ['wd-cli kb doc list 12'],
142
+ empty: '该知识库还没有文档',
143
+ }),
144
+ simple({
145
+ name: 'doc get',
146
+ summary: '查看文档元信息',
147
+ args: [kbArg, { name: 'vaultId', required: true, summary: '文档 id' }],
148
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/vault/documents/${ctx.args[1]}`,
149
+ examples: ['wd-cli kb doc get 12 99'],
150
+ }),
151
+ simple({
152
+ name: 'doc content',
153
+ summary: '读取文档正文',
154
+ args: [kbArg, { name: 'vaultId', required: true, summary: '文档 id' }],
155
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/vault/documents/${ctx.args[1]}/content`,
156
+ examples: ['wd-cli kb doc content 12 99'],
157
+ }),
158
+ {
159
+ name: 'doc download',
160
+ summary: '下载文档原文件',
161
+ args: [kbArg, { name: 'vaultId', required: true, summary: '文档 id' }],
162
+ flags: { out: { type: 'string', short: 'O', summary: '输出文件或目录,- 表示标准输出' } },
163
+ examples: ['wd-cli kb doc download 12 99 -O ./doc.pdf'],
164
+ async run(ctx) {
165
+ const saved = await http.download(ctx, {
166
+ path: `/knowledge-bases/${ctx.args[0]}/vault/documents/${ctx.args[1]}/download`,
167
+ outPath: ctx.flags.out,
168
+ defaultName: `document-${ctx.args[1]}`,
169
+ });
170
+ if (saved.path !== '-') output.success(`已下载:${saved.path}(${saved.bytes} 字节)`, ctx.flags);
171
+ return EXIT.OK;
172
+ },
173
+ },
174
+ {
175
+ name: 'doc upload',
176
+ summary: '上传一个或多个文档',
177
+ args: [kbArg, { name: 'file...', required: true, summary: '本地文件路径,可多个' }],
178
+ flags: { catalog: { type: 'string', summary: '放入指定目录' } },
179
+ examples: ['wd-cli kb doc upload 12 ./a.pdf ./b.md --catalog 3'],
180
+ async run(ctx) {
181
+ const [kbId, ...files] = ctx.args;
182
+ const results = [];
183
+ let failed = 0;
184
+ for (const file of files) {
185
+ try {
186
+ const form = http.buildFormData({ catalogId: ctx.flags.catalog }, [{ name: 'file', path: file }]);
187
+ const created = await http.request(ctx, {
188
+ method: 'POST',
189
+ path: `/knowledge-bases/${kbId}/vault/documents/upload`,
190
+ form,
191
+ });
192
+ results.push({ file, status: 'ok', id: created && created.id });
193
+ } catch (error) {
194
+ // 单个失败不中断其余文件:批量上传里最烦的就是第三个挂了前两个白传
195
+ failed += 1;
196
+ results.push({ file, status: 'failed', error: error.message });
197
+ }
198
+ }
199
+ output.print(results, {
200
+ flags: ctx.flags,
201
+ columns: [
202
+ { key: 'file', label: '文件' },
203
+ { key: 'status', label: '结果' },
204
+ { key: 'id', label: '文档 id' },
205
+ { key: 'error', label: '错误' },
206
+ ],
207
+ });
208
+ return failed ? EXIT.BUSINESS : EXIT.OK;
209
+ },
210
+ },
211
+ destructive({
212
+ name: 'doc delete',
213
+ summary: '删除文档',
214
+ args: [kbArg, { name: 'vaultId', required: true, summary: '文档 id' }],
215
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/vault/documents/${ctx.args[1]}`,
216
+ confirmText: (ctx) => `确认删除知识库 ${ctx.args[0]} 的文档 ${ctx.args[1]}?`,
217
+ examples: ['wd-cli kb doc delete 12 99 --yes'],
218
+ }),
219
+ simple({
220
+ name: 'doc reindex',
221
+ summary: '重新索引文档',
222
+ method: 'POST',
223
+ args: [kbArg, { name: 'vaultId', required: true, summary: '文档 id' }],
224
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/vault/documents/${ctx.args[1]}/reindex`,
225
+ examples: ['wd-cli kb doc reindex 12 99'],
226
+ }),
227
+ simple({
228
+ name: 'doc catalog-set',
229
+ summary: '把文档归入目录',
230
+ method: 'POST',
231
+ args: [kbArg, { name: 'vaultId', required: true, summary: '文档 id' }],
232
+ flags: { catalog: { type: 'string', summary: '目录 id' } },
233
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/documents/${ctx.args[1]}/doc-catalogs`,
234
+ body: (ctx) => ({ catalogId: ctx.flags.catalog }),
235
+ examples: ['wd-cli kb doc catalog-set 12 99 --catalog 3'],
236
+ }),
237
+ simple({
238
+ name: 'doc catalog-unset',
239
+ summary: '把文档移出目录',
240
+ method: 'DELETE',
241
+ args: [kbArg, { name: 'vaultId', required: true, summary: '文档 id' }],
242
+ flags: { catalog: { type: 'string', summary: '目录 id' } },
243
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/documents/${ctx.args[1]}/doc-catalogs`,
244
+ query: (ctx) => ({ catalogId: ctx.flags.catalog }),
245
+ examples: ['wd-cli kb doc catalog-unset 12 99 --catalog 3'],
246
+ }),
247
+
248
+ // ---- 目录 ----
249
+ simple({
250
+ name: 'catalog tree',
251
+ summary: '查看目录树',
252
+ args: [kbArg],
253
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/doc-catalogs/tree`,
254
+ examples: ['wd-cli kb catalog tree 12'],
255
+ }),
256
+ simple({
257
+ name: 'catalog create',
258
+ summary: '新建目录',
259
+ method: 'POST',
260
+ args: [kbArg],
261
+ flags: { name: { type: 'string', summary: '目录名' }, parent: { type: 'string', summary: '父目录 id' } },
262
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/doc-catalogs`,
263
+ body: (ctx) => ({ name: ctx.flags.name, parentId: ctx.flags.parent }),
264
+ examples: ['wd-cli kb catalog create 12 --name 制度'],
265
+ }),
266
+ simple({
267
+ name: 'catalog rename',
268
+ summary: '重命名目录',
269
+ method: 'PUT',
270
+ args: [kbArg, { name: 'catalogId', required: true, summary: '目录 id' }],
271
+ flags: { name: { type: 'string', summary: '新名称' } },
272
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/doc-catalogs/${ctx.args[1]}/rename`,
273
+ body: (ctx) => ({ name: ctx.flags.name }),
274
+ examples: ['wd-cli kb catalog rename 12 3 --name 新名称'],
275
+ }),
276
+ simple({
277
+ name: 'catalog move',
278
+ summary: '移动目录',
279
+ method: 'PUT',
280
+ args: [kbArg, { name: 'catalogId', required: true, summary: '目录 id' }],
281
+ flags: { parent: { type: 'string', summary: '新父目录 id' } },
282
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/doc-catalogs/${ctx.args[1]}/move`,
283
+ body: (ctx) => ({ parentId: ctx.flags.parent }),
284
+ examples: ['wd-cli kb catalog move 12 3 --parent 1'],
285
+ }),
286
+ destructive({
287
+ name: 'catalog delete',
288
+ summary: '删除目录',
289
+ args: [kbArg, { name: 'catalogId', required: true, summary: '目录 id' }],
290
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/doc-catalogs/${ctx.args[1]}`,
291
+ confirmText: (ctx) => `确认删除知识库 ${ctx.args[0]} 的目录 ${ctx.args[1]}?`,
292
+ examples: ['wd-cli kb catalog delete 12 3 --yes'],
293
+ }),
294
+ simple({
295
+ name: 'catalog contents',
296
+ summary: '列出目录下的内容',
297
+ args: [kbArg, { name: 'catalogId', required: true, summary: '目录 id' }],
298
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/catalogs/${ctx.args[1]}/contents`,
299
+ examples: ['wd-cli kb catalog contents 12 3'],
300
+ }),
301
+
302
+ // ---- 飞书文档 / 文件夹同步源 ----
303
+ simple({
304
+ name: 'lark import',
305
+ summary: '导入飞书文档',
306
+ method: 'POST',
307
+ args: [kbArg],
308
+ flags: { url: { type: 'string', summary: '飞书文档地址' }, catalog: { type: 'string', summary: '放入目录' } },
309
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/documents/lark`,
310
+ body: (ctx) => ({ url: ctx.flags.url, catalogId: ctx.flags.catalog }),
311
+ examples: ['wd-cli kb lark import 12 --url https://xxx.feishu.cn/docx/abc'],
312
+ }),
313
+ simple({
314
+ name: 'lark sources',
315
+ summary: '列出飞书文档同步源',
316
+ args: [kbArg],
317
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/documents/lark/sources`,
318
+ examples: ['wd-cli kb lark sources 12'],
319
+ }),
320
+ simple({
321
+ name: 'lark sync',
322
+ summary: '强制同步某个飞书文档源',
323
+ method: 'POST',
324
+ args: [kbArg, { name: 'sourceId', required: true, summary: '同步源 id' }],
325
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/documents/lark/${ctx.args[1]}/force-sync`,
326
+ examples: ['wd-cli kb lark sync 12 7'],
327
+ }),
328
+ simple({
329
+ name: 'folder add',
330
+ summary: '添加飞书文件夹同步源',
331
+ method: 'POST',
332
+ args: [kbArg],
333
+ flags: { url: { type: 'string', summary: '文件夹地址' }, policy: { type: 'string', summary: '同步策略 JSON' } },
334
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/lark/folder/sources`,
335
+ body: (ctx) => ({ url: ctx.flags.url, policy: ctx.flags.policy ? JSON.parse(ctx.flags.policy) : undefined }),
336
+ examples: ['wd-cli kb folder add 12 --url https://xxx.feishu.cn/drive/folder/abc'],
337
+ }),
338
+ simple({
339
+ name: 'folder list',
340
+ summary: '列出文件夹同步源',
341
+ args: [kbArg],
342
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/lark/folder/sources`,
343
+ examples: ['wd-cli kb folder list 12'],
344
+ }),
345
+ simple({
346
+ name: 'folder sync',
347
+ summary: '同步某个文件夹源',
348
+ method: 'POST',
349
+ args: [kbArg, { name: 'folderSourceId', required: true, summary: '文件夹源 id' }],
350
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/lark/folder/${ctx.args[1]}/sync`,
351
+ examples: ['wd-cli kb folder sync 12 5'],
352
+ }),
353
+ {
354
+ name: 'folder policy',
355
+ summary: '更新文件夹源的同步策略',
356
+ args: [kbArg, { name: 'folderSourceId', required: true, summary: '文件夹源 id' }],
357
+ flags: { data: { type: 'string', summary: '策略 JSON:内联 / @file / -' } },
358
+ examples: ['wd-cli kb folder policy 12 5 --data @policy.json'],
359
+ async run(ctx) {
360
+ const body = dataInput.requireJsonData(ctx.flags.data);
361
+ const result = await http.request(ctx, {
362
+ method: 'PUT',
363
+ path: `/knowledge-bases/${ctx.args[0]}/lark/folder/${ctx.args[1]}/policy`,
364
+ body,
365
+ });
366
+ output.print(result, { flags: ctx.flags });
367
+ return EXIT.OK;
368
+ },
369
+ },
370
+ destructive({
371
+ name: 'folder delete',
372
+ summary: '删除文件夹同步源',
373
+ args: [kbArg, { name: 'folderSourceId', required: true, summary: '文件夹源 id' }],
374
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/lark/folder/${ctx.args[1]}`,
375
+ confirmText: (ctx) => `确认删除知识库 ${ctx.args[0]} 的文件夹同步源 ${ctx.args[1]}?`,
376
+ examples: ['wd-cli kb folder delete 12 5 --yes'],
377
+ }),
378
+
379
+ // ---- 授权与分享 ----
380
+ simple({
381
+ name: 'acl list',
382
+ summary: '查看知识库授权',
383
+ args: [kbArg],
384
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/acl`,
385
+ examples: ['wd-cli kb acl list 12'],
386
+ }),
387
+ {
388
+ name: 'acl grant',
389
+ summary: '授予访问权限',
390
+ args: [kbArg],
391
+ flags: { data: { type: 'string', summary: '授权体 JSON:内联 / @file / -' } },
392
+ examples: ['wd-cli kb acl grant 12 --data \'{"userIds":[7]}\''],
393
+ async run(ctx) {
394
+ const body = dataInput.requireJsonData(ctx.flags.data);
395
+ const result = await http.request(ctx, { method: 'POST', path: `/knowledge-bases/${ctx.args[0]}/acl`, body });
396
+ output.print(result, { flags: ctx.flags });
397
+ output.success('已授权', ctx.flags);
398
+ return EXIT.OK;
399
+ },
400
+ },
401
+ {
402
+ name: 'acl revoke',
403
+ summary: '撤销访问权限',
404
+ args: [kbArg],
405
+ flags: { data: { type: 'string', summary: '撤销体 JSON:内联 / @file / -' } },
406
+ examples: ['wd-cli kb acl revoke 12 --data \'{"userIds":[7]}\''],
407
+ async run(ctx) {
408
+ const body = dataInput.requireJsonData(ctx.flags.data);
409
+ const ok = await dataInput.confirm(`确认撤销知识库 ${ctx.args[0]} 的相关授权?`, ctx.flags);
410
+ if (!ok) {
411
+ output.info('已取消', ctx.flags);
412
+ return EXIT.OK;
413
+ }
414
+ await http.request(ctx, { method: 'DELETE', path: `/knowledge-bases/${ctx.args[0]}/acl`, body });
415
+ output.success('已撤销', ctx.flags);
416
+ return EXIT.OK;
417
+ },
418
+ },
419
+ simple({
420
+ name: 'acl external',
421
+ summary: '列出外部用户授权',
422
+ args: [kbArg],
423
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/acl/external-users`,
424
+ examples: ['wd-cli kb acl external 12'],
425
+ }),
426
+ simple({
427
+ name: 'share create',
428
+ summary: '创建文档分享',
429
+ method: 'POST',
430
+ args: [kbArg, { name: 'vaultId', required: true, summary: '文档 id' }],
431
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/vault/documents/${ctx.args[1]}/share`,
432
+ examples: ['wd-cli kb share create 12 99'],
433
+ }),
434
+ simple({
435
+ name: 'share list',
436
+ summary: '列出文档的分享记录',
437
+ args: [kbArg, { name: 'vaultId', required: true, summary: '文档 id' }],
438
+ path: (ctx) => `/knowledge-bases/${ctx.args[0]}/vault/documents/${ctx.args[1]}/shares`,
439
+ examples: ['wd-cli kb share list 12 99'],
440
+ }),
441
+ ];
442
+
443
+ module.exports = {
444
+ id: 'kb',
445
+ summary: '知识库:检索 / 文档 / 目录 / 同步源 / 授权',
446
+ description: '令牌直连模式下工作空间令牌与 Agent 访问密钥都可用(服务端双鉴权路径)。',
447
+ principals: ['workspace', 'agent'],
448
+ commands,
449
+ };
@@ -0,0 +1,140 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * 可观测:trace / spans / sessions / metrics / logs / summary / 评分。
5
+ * 三个 topic 共用同一套查询参数,端点都在 /api/open/observability/**。
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
+ /** 时间范围与分页:各端点参数名一致,统一在这里声明。 */
16
+ const RANGE_FLAGS = {
17
+ from: { type: 'string', summary: '起始时间(ISO 或毫秒时间戳)' },
18
+ to: { type: 'string', summary: '结束时间' },
19
+ page: { type: 'number', summary: '页码' },
20
+ 'page-size': { type: 'number', summary: '每页条数' },
21
+ };
22
+
23
+ function rangeQuery(ctx) {
24
+ return {
25
+ from: ctx.flags.from,
26
+ to: ctx.flags.to,
27
+ startTime: ctx.flags.from,
28
+ endTime: ctx.flags.to,
29
+ pageNum: ctx.flags.page,
30
+ pageSize: ctx.flags.pageSize,
31
+ };
32
+ }
33
+
34
+ function obsCommand(name, summary, suffix, columns, examples) {
35
+ return {
36
+ name,
37
+ summary,
38
+ args: [agentArg],
39
+ flags: RANGE_FLAGS,
40
+ examples,
41
+ async run(ctx) {
42
+ const result = await http.request(ctx, {
43
+ path: `/observability/${encodeURIComponent(ctx.args[0])}/${suffix}`,
44
+ query: rangeQuery(ctx),
45
+ });
46
+ const rows = Array.isArray(result) ? result : result && result.list ? result.list : result;
47
+ output.print(rows, { flags: ctx.flags, columns, empty: '这个时间范围内没有数据' });
48
+ return EXIT.OK;
49
+ },
50
+ };
51
+ }
52
+
53
+ const traceTopic = {
54
+ id: 'trace',
55
+ summary: '调用链:列出 / 查看单条',
56
+ principals: ['agent'],
57
+ commands: [
58
+ obsCommand(
59
+ 'list',
60
+ '列出调用链',
61
+ 'traces',
62
+ [
63
+ { key: 'traceId', label: 'TRACE ID' },
64
+ { key: 'name', label: '名称' },
65
+ { key: 'status', label: '状态' },
66
+ { key: 'durationMs', label: '耗时(ms)' },
67
+ { key: 'startTime', label: '开始时间' },
68
+ ],
69
+ ['wd-cli trace list 66 --from 2026-09-01']
70
+ ),
71
+ {
72
+ name: 'get',
73
+ summary: '查看单条调用链',
74
+ args: [agentArg, { name: 'traceId', required: true, summary: 'trace id' }],
75
+ examples: ['wd-cli trace get 66 abc123'],
76
+ async run(ctx) {
77
+ const result = await http.request(ctx, {
78
+ path: `/observability/${encodeURIComponent(ctx.args[0])}/traces/${encodeURIComponent(ctx.args[1])}`,
79
+ });
80
+ output.print(result, { flags: ctx.flags });
81
+ return EXIT.OK;
82
+ },
83
+ },
84
+ ],
85
+ };
86
+
87
+ const obsTopic = {
88
+ id: 'obs',
89
+ summary: '可观测:spans / sessions / metrics / logs / summary',
90
+ principals: ['agent'],
91
+ commands: [
92
+ obsCommand('spans', '列出 span', 'spans', null, ['wd-cli obs spans 66']),
93
+ obsCommand('sessions', '列出会话维度统计', 'sessions', null, ['wd-cli obs sessions 66']),
94
+ obsCommand('metrics', '查看指标', 'metrics', null, ['wd-cli obs metrics 66 --from 2026-09-01']),
95
+ obsCommand('logs', '查看日志', 'logs', null, ['wd-cli obs logs 66']),
96
+ obsCommand('summary', '查看汇总', 'summary', null, ['wd-cli obs summary 66']),
97
+ ],
98
+ };
99
+
100
+ const scoreTopic = {
101
+ id: 'score',
102
+ summary: '评分:列出 / 打分',
103
+ principals: ['agent'],
104
+ commands: [
105
+ obsCommand(
106
+ 'list',
107
+ '列出评分',
108
+ 'scores',
109
+ [
110
+ { key: 'traceId', label: 'TRACE ID' },
111
+ { key: 'name', label: '指标' },
112
+ { key: 'value', label: '分值' },
113
+ { key: 'createdAt', label: '时间' },
114
+ ],
115
+ ['wd-cli score list 66']
116
+ ),
117
+ {
118
+ name: 'add',
119
+ summary: '写入一条评分',
120
+ args: [agentArg],
121
+ flags: { data: { type: 'string', summary: '评分体 JSON:内联 / @file / -' } },
122
+ examples: ['wd-cli score add 66 --data \'{"traceId":"abc","name":"helpful","value":1}\''],
123
+ async run(ctx) {
124
+ const body = dataInput.requireJsonData(ctx.flags.data);
125
+ const result = await http.request(ctx, {
126
+ method: 'POST',
127
+ path: `/observability/${encodeURIComponent(ctx.args[0])}/scores`,
128
+ body,
129
+ });
130
+ output.print(result, { flags: ctx.flags });
131
+ output.success('已写入评分', ctx.flags);
132
+ return EXIT.OK;
133
+ },
134
+ },
135
+ ],
136
+ };
137
+
138
+ // 一个模块导出三个 topic:registry 展开后各自独立
139
+ module.exports = traceTopic;
140
+ module.exports.extraTopics = [obsTopic, scoreTopic];