@dmall-inc/wd-cli 0.0.0-stage → 0.2.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.
- package/README.md +401 -2
- package/bin/wd-cli.js +106 -0
- package/docs/design-references.md +75 -0
- package/lib/args.js +299 -0
- package/lib/auth.js +77 -0
- package/lib/commands/agent.js +784 -0
- package/lib/commands/api.js +122 -0
- package/lib/commands/auth.js +314 -0
- package/lib/commands/automation.js +92 -0
- package/lib/commands/completion.js +141 -0
- package/lib/commands/config.js +120 -0
- package/lib/commands/credential.js +108 -0
- package/lib/commands/dataset.js +144 -0
- package/lib/commands/evaluate.js +170 -0
- package/lib/commands/help.js +81 -0
- package/lib/commands/index.js +46 -0
- package/lib/commands/kb.js +449 -0
- package/lib/commands/observability.js +381 -0
- package/lib/commands/session.js +189 -0
- package/lib/commands/skill.js +463 -0
- package/lib/commands/workspace.js +110 -0
- package/lib/config.js +269 -0
- package/lib/data.js +128 -0
- package/lib/http.js +402 -0
- package/lib/jq.js +342 -0
- package/lib/keyring.js +125 -0
- package/lib/oidc.js +344 -0
- package/lib/output.js +249 -0
- package/package.json +22 -4
|
@@ -0,0 +1,381 @@
|
|
|
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
|
+
name: 'summary',
|
|
86
|
+
summary: '查看单条调用链的诊断摘要(提炼 CoT 推理、工具调用与异常)',
|
|
87
|
+
args: [agentArg, { name: 'traceId', required: true, summary: 'trace id' }],
|
|
88
|
+
flags: {
|
|
89
|
+
format: { type: 'string', summary: '输出格式:markdown(缺省)| json | text' },
|
|
90
|
+
},
|
|
91
|
+
examples: [
|
|
92
|
+
'wd-cli trace summary 66 abc123',
|
|
93
|
+
'wd-cli trace summary 66 abc123 --format json',
|
|
94
|
+
],
|
|
95
|
+
async run(ctx) {
|
|
96
|
+
const agentCode = ctx.args[0];
|
|
97
|
+
const traceId = ctx.args[1];
|
|
98
|
+
const spans = await http.request(ctx, {
|
|
99
|
+
path: `/observability/${encodeURIComponent(agentCode)}/traces/${encodeURIComponent(traceId)}`,
|
|
100
|
+
});
|
|
101
|
+
const summary = analyzeTraceSpans(traceId, spans);
|
|
102
|
+
const format = ctx.flags.format || (ctx.flags.output === 'json' || ctx.flags.json ? 'json' : 'markdown');
|
|
103
|
+
if (format === 'json') {
|
|
104
|
+
output.print(summary, { flags: ctx.flags });
|
|
105
|
+
} else if (format === 'text') {
|
|
106
|
+
process.stdout.write(`${renderSummaryText(summary)}\n`);
|
|
107
|
+
} else {
|
|
108
|
+
process.stdout.write(`${renderSummaryMarkdown(summary)}\n`);
|
|
109
|
+
}
|
|
110
|
+
return EXIT.OK;
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const obsTopic = {
|
|
117
|
+
id: 'obs',
|
|
118
|
+
summary: '可观测:spans / sessions / metrics / logs / summary',
|
|
119
|
+
principals: ['agent'],
|
|
120
|
+
commands: [
|
|
121
|
+
obsCommand('spans', '列出 span', 'spans', null, ['wd-cli obs spans 66']),
|
|
122
|
+
obsCommand('sessions', '列出会话维度统计', 'sessions', null, ['wd-cli obs sessions 66']),
|
|
123
|
+
obsCommand('metrics', '查看指标', 'metrics', null, ['wd-cli obs metrics 66 --from 2026-09-01']),
|
|
124
|
+
obsCommand('logs', '查看日志', 'logs', null, ['wd-cli obs logs 66']),
|
|
125
|
+
obsCommand('summary', '查看汇总', 'summary', null, ['wd-cli obs summary 66']),
|
|
126
|
+
],
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const scoreTopic = {
|
|
130
|
+
id: 'score',
|
|
131
|
+
summary: '评分:列出 / 打分',
|
|
132
|
+
principals: ['agent'],
|
|
133
|
+
commands: [
|
|
134
|
+
obsCommand(
|
|
135
|
+
'list',
|
|
136
|
+
'列出评分',
|
|
137
|
+
'scores',
|
|
138
|
+
[
|
|
139
|
+
{ key: 'traceId', label: 'TRACE ID' },
|
|
140
|
+
{ key: 'name', label: '指标' },
|
|
141
|
+
{ key: 'value', label: '分值' },
|
|
142
|
+
{ key: 'createdAt', label: '时间' },
|
|
143
|
+
],
|
|
144
|
+
['wd-cli score list 66']
|
|
145
|
+
),
|
|
146
|
+
{
|
|
147
|
+
name: 'add',
|
|
148
|
+
summary: '写入一条评分',
|
|
149
|
+
args: [agentArg],
|
|
150
|
+
flags: { data: { type: 'string', summary: '评分体 JSON:内联 / @file / -' } },
|
|
151
|
+
examples: ['wd-cli score add 66 --data \'{"traceId":"abc","name":"helpful","value":1}\''],
|
|
152
|
+
async run(ctx) {
|
|
153
|
+
const body = dataInput.requireJsonData(ctx.flags.data);
|
|
154
|
+
const result = await http.request(ctx, {
|
|
155
|
+
method: 'POST',
|
|
156
|
+
path: `/observability/${encodeURIComponent(ctx.args[0])}/scores`,
|
|
157
|
+
body,
|
|
158
|
+
});
|
|
159
|
+
output.print(result, { flags: ctx.flags });
|
|
160
|
+
output.success('已写入评分', ctx.flags);
|
|
161
|
+
return EXIT.OK;
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
function parseAttributes(attr) {
|
|
168
|
+
if (!attr) return {};
|
|
169
|
+
let current = attr;
|
|
170
|
+
for (let i = 0; i < 5 && typeof current === 'string'; i++) {
|
|
171
|
+
try {
|
|
172
|
+
current = JSON.parse(current);
|
|
173
|
+
} catch {
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (Array.isArray(current)) {
|
|
178
|
+
const res = {};
|
|
179
|
+
for (const item of current) {
|
|
180
|
+
if (item && typeof item === 'object') {
|
|
181
|
+
const k = item.key || item.Key || item.name;
|
|
182
|
+
const v = item.value !== undefined ? item.value : (item.Value !== undefined ? item.Value : item);
|
|
183
|
+
if (k) res[k] = v;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return res;
|
|
187
|
+
}
|
|
188
|
+
if (current && typeof current === 'object') {
|
|
189
|
+
return current;
|
|
190
|
+
}
|
|
191
|
+
return {};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function safeJsonOrString(val) {
|
|
195
|
+
if (val === undefined || val === null) return '';
|
|
196
|
+
if (typeof val === 'object') return val;
|
|
197
|
+
try {
|
|
198
|
+
return JSON.parse(val);
|
|
199
|
+
} catch {
|
|
200
|
+
return val;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function analyzeTraceSpans(traceId, rawSpans) {
|
|
205
|
+
const spans = Array.isArray(rawSpans) ? rawSpans : [];
|
|
206
|
+
let overallStatus = 'OK';
|
|
207
|
+
let totalDurationMs = 0;
|
|
208
|
+
const thoughts = [];
|
|
209
|
+
const toolCalls = [];
|
|
210
|
+
const errors = [];
|
|
211
|
+
|
|
212
|
+
for (const span of spans) {
|
|
213
|
+
const isError =
|
|
214
|
+
span.statusCode === 'ERROR' ||
|
|
215
|
+
span.statusCode === 'STATUS_CODE_ERROR' ||
|
|
216
|
+
(span.statusCode !== 'OK' &&
|
|
217
|
+
span.statusCode !== 'STATUS_CODE_OK' &&
|
|
218
|
+
span.statusMessage &&
|
|
219
|
+
/error|failed|exception/i.test(span.statusMessage));
|
|
220
|
+
if (isError) {
|
|
221
|
+
overallStatus = 'ERROR';
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const durMs = span.duration ? Math.round(Number(span.duration) / 1_000_000) : 0;
|
|
225
|
+
if (!span.parentSpanId && durMs > totalDurationMs) {
|
|
226
|
+
totalDurationMs = durMs;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const attrs = parseAttributes(span.spanAttributes);
|
|
230
|
+
|
|
231
|
+
// 提炼 CoT 推理(Thinking / Reasoning)
|
|
232
|
+
const thoughtVal =
|
|
233
|
+
attrs['llm.thought'] ||
|
|
234
|
+
attrs.thought ||
|
|
235
|
+
attrs.thinking ||
|
|
236
|
+
attrs.reasoning ||
|
|
237
|
+
attrs.reasoning_content ||
|
|
238
|
+
attrs['openinference.thought'] ||
|
|
239
|
+
attrs['gen_ai.thought'];
|
|
240
|
+
if (thoughtVal) {
|
|
241
|
+
const thoughtStr = typeof thoughtVal === 'object' ? JSON.stringify(thoughtVal) : String(thoughtVal);
|
|
242
|
+
const trimmed = thoughtStr.trim();
|
|
243
|
+
if (trimmed && !thoughts.includes(trimmed)) {
|
|
244
|
+
thoughts.push(trimmed);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 提炼工具调用(Tool Calls)
|
|
249
|
+
const isTool =
|
|
250
|
+
String(span.spanKind).toLowerCase() === 'tool' ||
|
|
251
|
+
String(attrs['openinference.span.kind']).toUpperCase() === 'TOOL' ||
|
|
252
|
+
String(attrs['span.kind']).toUpperCase() === 'TOOL' ||
|
|
253
|
+
(span.spanName && (span.spanName.startsWith('execute_tool') || span.spanName.includes('tool'))) ||
|
|
254
|
+
attrs['tool.name'] ||
|
|
255
|
+
attrs['gen_ai.tool.name'];
|
|
256
|
+
|
|
257
|
+
if (isTool) {
|
|
258
|
+
const toolName =
|
|
259
|
+
attrs['tool.name'] ||
|
|
260
|
+
attrs['gen_ai.tool.name'] ||
|
|
261
|
+
(span.spanName ? span.spanName.replace(/^execute_tool:?/, '').trim() : 'unknown');
|
|
262
|
+
const toolArgsRaw =
|
|
263
|
+
attrs['tool.parameters'] ||
|
|
264
|
+
attrs['tool.arguments'] ||
|
|
265
|
+
attrs['gen_ai.tool.call.arguments'] ||
|
|
266
|
+
attrs.input ||
|
|
267
|
+
attrs['input.value'] ||
|
|
268
|
+
attrs['gen_ai.task.input'];
|
|
269
|
+
const toolArgs = safeJsonOrString(toolArgsRaw);
|
|
270
|
+
|
|
271
|
+
const toolResultRaw =
|
|
272
|
+
attrs['tool.result'] ||
|
|
273
|
+
attrs.output ||
|
|
274
|
+
attrs['output.value'] ||
|
|
275
|
+
attrs['gen_ai.task.output'] ||
|
|
276
|
+
span.statusMessage ||
|
|
277
|
+
'';
|
|
278
|
+
|
|
279
|
+
const toolError =
|
|
280
|
+
span.statusCode === 'ERROR' || span.statusCode === 'STATUS_CODE_ERROR'
|
|
281
|
+
? (span.statusMessage || attrs.error || attrs['exception.message'] || 'Tool execution failed')
|
|
282
|
+
: (attrs.error || null);
|
|
283
|
+
|
|
284
|
+
toolCalls.push({
|
|
285
|
+
name: toolName,
|
|
286
|
+
arguments: toolArgs,
|
|
287
|
+
durationMs: durMs,
|
|
288
|
+
status: isError ? 'ERROR' : 'OK',
|
|
289
|
+
result: typeof toolResultRaw === 'object' ? JSON.stringify(toolResultRaw) : String(toolResultRaw),
|
|
290
|
+
error: toolError ? String(toolError) : null,
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// 提炼异常
|
|
295
|
+
if (isError) {
|
|
296
|
+
const errMsg =
|
|
297
|
+
span.statusMessage ||
|
|
298
|
+
attrs['exception.message'] ||
|
|
299
|
+
attrs['exception.stacktrace'] ||
|
|
300
|
+
attrs.error;
|
|
301
|
+
if (errMsg) {
|
|
302
|
+
const str = String(errMsg).trim();
|
|
303
|
+
if (!errors.includes(str)) errors.push(str);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (totalDurationMs === 0 && spans.length > 0) {
|
|
309
|
+
const maxDur = Math.max(...spans.map((s) => (s.duration ? Math.round(Number(s.duration) / 1_000_000) : 0)));
|
|
310
|
+
totalDurationMs = maxDur;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
traceId,
|
|
315
|
+
status: overallStatus,
|
|
316
|
+
durationMs: totalDurationMs,
|
|
317
|
+
thoughts,
|
|
318
|
+
toolCalls,
|
|
319
|
+
errors,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function renderSummaryMarkdown(summary) {
|
|
324
|
+
const lines = [
|
|
325
|
+
`# Trace Summary: ${summary.traceId}`,
|
|
326
|
+
`- **Status**: ${summary.status}`,
|
|
327
|
+
`- **Duration**: ${summary.durationMs}ms`,
|
|
328
|
+
'',
|
|
329
|
+
'## Thoughts (CoT)',
|
|
330
|
+
];
|
|
331
|
+
|
|
332
|
+
if (summary.thoughts.length === 0) {
|
|
333
|
+
lines.push('_No thought/reasoning spans recorded._');
|
|
334
|
+
} else {
|
|
335
|
+
for (const t of summary.thoughts) {
|
|
336
|
+
lines.push(`> ${t.replace(/\n/g, '\n> ')}`);
|
|
337
|
+
lines.push('');
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
lines.push('');
|
|
342
|
+
lines.push('## Tool Invocations');
|
|
343
|
+
if (summary.toolCalls.length === 0) {
|
|
344
|
+
lines.push('_No tool invocations recorded._');
|
|
345
|
+
} else {
|
|
346
|
+
summary.toolCalls.forEach((tc, idx) => {
|
|
347
|
+
lines.push(`### ${idx + 1}. \`${tc.name}\` (${tc.durationMs}ms) - [${tc.status}]`);
|
|
348
|
+
lines.push(`- **Arguments**: \`${typeof tc.arguments === 'object' ? JSON.stringify(tc.arguments) : tc.arguments}\``);
|
|
349
|
+
if (tc.result) {
|
|
350
|
+
const resPreview = tc.result.length > 500 ? tc.result.slice(0, 500) + '...' : tc.result;
|
|
351
|
+
lines.push(`- **Result**: \`${resPreview}\``);
|
|
352
|
+
}
|
|
353
|
+
if (tc.error) {
|
|
354
|
+
lines.push(`- **Error**: ${tc.error}`);
|
|
355
|
+
}
|
|
356
|
+
lines.push('');
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
lines.push('## Errors');
|
|
361
|
+
if (summary.errors.length === 0) {
|
|
362
|
+
lines.push('_No errors recorded._');
|
|
363
|
+
} else {
|
|
364
|
+
for (const err of summary.errors) {
|
|
365
|
+
lines.push(`- ${err}`);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
return lines.join('\n');
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function renderSummaryText(summary) {
|
|
373
|
+
return renderSummaryMarkdown(summary);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// 一个模块导出三个 topic:registry 展开后各自独立
|
|
377
|
+
module.exports = traceTopic;
|
|
378
|
+
module.exports.extraTopics = [obsTopic, scoreTopic];
|
|
379
|
+
module.exports.analyzeTraceSpans = analyzeTraceSpans;
|
|
380
|
+
module.exports.renderSummaryMarkdown = renderSummaryMarkdown;
|
|
381
|
+
module.exports.renderSummaryText = renderSummaryText;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 会话历史:列出 / 查看 / 改名 / 删除 / 消息记录 / 存在性 / 清空上下文。
|
|
5
|
+
* 端点对应 /api/open/chat-history/**(销毁会话在 server/gateway 的 dropsession)。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const http = require('../http');
|
|
9
|
+
const dataInput = require('../data');
|
|
10
|
+
const output = require('../output');
|
|
11
|
+
const { CliError, EXIT } = output;
|
|
12
|
+
|
|
13
|
+
const agentArg = { name: 'agentCode', required: true, summary: '应用标识' };
|
|
14
|
+
const sessionArg = { name: 'sessionId', required: true, summary: '会话 id' };
|
|
15
|
+
|
|
16
|
+
const SESSION_COLUMNS = [
|
|
17
|
+
{ key: 'sessionId', label: '会话 id' },
|
|
18
|
+
{ key: 'title', label: '标题' },
|
|
19
|
+
{ key: 'userId', label: '用户' },
|
|
20
|
+
{ key: 'updatedAt', label: '更新时间' },
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
function sessionPath(ctx, suffix) {
|
|
24
|
+
return `/chat-history/${encodeURIComponent(ctx.args[0])}/${encodeURIComponent(ctx.args[1])}${suffix}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const list = {
|
|
28
|
+
name: 'list',
|
|
29
|
+
summary: '列出会话',
|
|
30
|
+
args: [agentArg],
|
|
31
|
+
flags: {
|
|
32
|
+
'user-id': { type: 'string', summary: '只看某个用户的会话' },
|
|
33
|
+
page: { type: 'number', summary: '页码' },
|
|
34
|
+
'page-size': { type: 'number', summary: '每页条数' },
|
|
35
|
+
paginate: { type: 'boolean', summary: '自动翻页取全部' },
|
|
36
|
+
},
|
|
37
|
+
examples: ['wd-cli session list 66', 'wd-cli session list 66 --user-id 7 --paginate'],
|
|
38
|
+
async run(ctx) {
|
|
39
|
+
const options = {
|
|
40
|
+
path: `/chat-history/${encodeURIComponent(ctx.args[0])}/sessions`,
|
|
41
|
+
query: { userId: ctx.flags.userId, pageNum: ctx.flags.page, pageSize: ctx.flags.pageSize, agentCode: ctx.args[0] },
|
|
42
|
+
};
|
|
43
|
+
const result = ctx.flags.paginate ? await http.requestAllPages(ctx, options) : await http.request(ctx, options);
|
|
44
|
+
const rows = Array.isArray(result) ? result : result && result.list ? result.list : result;
|
|
45
|
+
output.print(rows, { flags: ctx.flags, columns: SESSION_COLUMNS, empty: '还没有会话' });
|
|
46
|
+
return EXIT.OK;
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const get = {
|
|
51
|
+
name: 'get',
|
|
52
|
+
summary: '查看会话详情',
|
|
53
|
+
args: [agentArg, sessionArg],
|
|
54
|
+
examples: ['wd-cli session get 66 s-1'],
|
|
55
|
+
async run(ctx) {
|
|
56
|
+
const result = await http.request(ctx, { path: sessionPath(ctx, '/session'), query: { agentCode: ctx.args[0] } });
|
|
57
|
+
output.print(result, { flags: ctx.flags });
|
|
58
|
+
return EXIT.OK;
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const rename = {
|
|
63
|
+
name: 'rename',
|
|
64
|
+
summary: '修改会话标题',
|
|
65
|
+
args: [agentArg, sessionArg],
|
|
66
|
+
flags: { title: { type: 'string', summary: '新标题' } },
|
|
67
|
+
examples: ['wd-cli session rename 66 s-1 --title 季度复盘'],
|
|
68
|
+
async run(ctx) {
|
|
69
|
+
if (!ctx.flags.title) throw new CliError('缺少 --title', EXIT.USAGE);
|
|
70
|
+
const result = await http.request(ctx, {
|
|
71
|
+
method: 'PATCH',
|
|
72
|
+
path: sessionPath(ctx, '/session'),
|
|
73
|
+
query: { agentCode: ctx.args[0] },
|
|
74
|
+
body: { title: ctx.flags.title },
|
|
75
|
+
});
|
|
76
|
+
output.print(result, { flags: ctx.flags });
|
|
77
|
+
output.success('已更新会话标题', ctx.flags);
|
|
78
|
+
return EXIT.OK;
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const remove = {
|
|
83
|
+
name: 'delete',
|
|
84
|
+
summary: '删除会话',
|
|
85
|
+
args: [agentArg, sessionArg],
|
|
86
|
+
examples: ['wd-cli session delete 66 s-1 --yes'],
|
|
87
|
+
async run(ctx) {
|
|
88
|
+
const ok = await dataInput.confirm(`确认删除会话 ${ctx.args[1]}?`, ctx.flags);
|
|
89
|
+
if (!ok) {
|
|
90
|
+
output.info('已取消', ctx.flags);
|
|
91
|
+
return EXIT.OK;
|
|
92
|
+
}
|
|
93
|
+
await http.request(ctx, {
|
|
94
|
+
method: 'DELETE',
|
|
95
|
+
path: sessionPath(ctx, '/session'),
|
|
96
|
+
query: { agentCode: ctx.args[0] },
|
|
97
|
+
});
|
|
98
|
+
output.success('已删除会话', ctx.flags);
|
|
99
|
+
return EXIT.OK;
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const history = {
|
|
104
|
+
name: 'history',
|
|
105
|
+
summary: '查看会话消息记录',
|
|
106
|
+
args: [agentArg, sessionArg],
|
|
107
|
+
flags: {
|
|
108
|
+
page: { type: 'number', summary: '页码' },
|
|
109
|
+
'page-size': { type: 'number', summary: '每页条数' },
|
|
110
|
+
paginate: { type: 'boolean', summary: '自动翻页取全部' },
|
|
111
|
+
},
|
|
112
|
+
examples: ['wd-cli session history 66 s-1'],
|
|
113
|
+
async run(ctx) {
|
|
114
|
+
const options = {
|
|
115
|
+
path: sessionPath(ctx, '/history'),
|
|
116
|
+
query: { pageNum: ctx.flags.page, pageSize: ctx.flags.pageSize, agentCode: ctx.args[0] },
|
|
117
|
+
};
|
|
118
|
+
const result = ctx.flags.paginate ? await http.requestAllPages(ctx, options) : await http.request(ctx, options);
|
|
119
|
+
const rows = Array.isArray(result) ? result : result && result.list ? result.list : result;
|
|
120
|
+
output.print(rows, {
|
|
121
|
+
flags: ctx.flags,
|
|
122
|
+
columns: [
|
|
123
|
+
{ key: 'role', label: '角色' },
|
|
124
|
+
{ key: 'content', label: '内容' },
|
|
125
|
+
{ key: 'createdAt', label: '时间' },
|
|
126
|
+
],
|
|
127
|
+
empty: '该会话还没有消息',
|
|
128
|
+
});
|
|
129
|
+
return EXIT.OK;
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const exists = {
|
|
134
|
+
name: 'exists',
|
|
135
|
+
summary: '判断会话是否存在(存在退出 0,不存在退出 1)',
|
|
136
|
+
args: [agentArg, sessionArg],
|
|
137
|
+
examples: ['wd-cli session exists 66 s-1 && echo 有'],
|
|
138
|
+
async run(ctx) {
|
|
139
|
+
const result = await http.request(ctx, { path: sessionPath(ctx, '/exists'), query: { agentCode: ctx.args[0] } });
|
|
140
|
+
const found = result === true || (result && result.exists === true);
|
|
141
|
+
process.stdout.write(`${found}\n`);
|
|
142
|
+
return found ? EXIT.OK : EXIT.BUSINESS;
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const infoCmd = {
|
|
147
|
+
name: 'info',
|
|
148
|
+
summary: '查看会话运行时信息',
|
|
149
|
+
args: [agentArg, sessionArg],
|
|
150
|
+
examples: ['wd-cli session info 66 s-1'],
|
|
151
|
+
async run(ctx) {
|
|
152
|
+
const result = await http.request(ctx, {
|
|
153
|
+
path: sessionPath(ctx, '/session-info'),
|
|
154
|
+
query: { sessionId: ctx.args[1], agentCode: ctx.args[0] },
|
|
155
|
+
});
|
|
156
|
+
output.print(result, { flags: ctx.flags });
|
|
157
|
+
return EXIT.OK;
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const drop = {
|
|
162
|
+
name: 'drop',
|
|
163
|
+
summary: '销毁会话上下文(运行时 + 历史)',
|
|
164
|
+
args: [agentArg, sessionArg],
|
|
165
|
+
examples: ['wd-cli session drop 66 s-1 --yes'],
|
|
166
|
+
async run(ctx) {
|
|
167
|
+
const ok = await dataInput.confirm(`确认销毁会话 ${ctx.args[1]} 的上下文?`, ctx.flags);
|
|
168
|
+
if (!ok) {
|
|
169
|
+
output.info('已取消', ctx.flags);
|
|
170
|
+
return EXIT.OK;
|
|
171
|
+
}
|
|
172
|
+
const result = await http.request(ctx, {
|
|
173
|
+
method: 'POST',
|
|
174
|
+
base: ctx.chatBase(),
|
|
175
|
+
path: `/${encodeURIComponent(ctx.args[0])}/dropsession`,
|
|
176
|
+
body: { sessionId: ctx.args[1] },
|
|
177
|
+
});
|
|
178
|
+
output.print(result, { flags: ctx.flags });
|
|
179
|
+
output.success('已销毁会话上下文', ctx.flags);
|
|
180
|
+
return EXIT.OK;
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
module.exports = {
|
|
185
|
+
id: 'session',
|
|
186
|
+
summary: '会话历史:列出 / 查看 / 改名 / 删除 / 消息 / 销毁',
|
|
187
|
+
principals: ['agent'],
|
|
188
|
+
commands: [list, get, rename, remove, history, exists, infoCmd, drop],
|
|
189
|
+
};
|