@dmall-inc/wd-cli 0.1.0 → 0.4.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 +126 -2
- package/docs/design-references.md +2 -0
- package/lib/args.js +2 -1
- package/lib/auth.js +1 -1
- package/lib/commands/agent.js +419 -20
- package/lib/commands/daemon.js +411 -0
- package/lib/commands/device.js +92 -0
- package/lib/commands/diwork.js +544 -0
- package/lib/commands/index.js +5 -0
- package/lib/commands/observability.js +241 -0
- package/lib/commands/open.js +89 -0
- package/lib/commands/skill.js +124 -2
- package/lib/commands/tool.js +327 -0
- package/lib/daemon/agui.js +63 -0
- package/lib/daemon/channel.js +183 -0
- package/lib/daemon/enroll.js +78 -0
- package/lib/daemon/executors/acp.js +480 -0
- package/lib/daemon/executors/pi-approval-extension.ts +57 -0
- package/lib/daemon/executors/pi.js +360 -0
- package/lib/daemon/index.js +146 -0
- package/lib/daemon/jsonrpc.js +127 -0
- package/lib/daemon/permission.js +96 -0
- package/lib/daemon/roots.js +161 -0
- package/lib/daemon/runtimes.js +238 -0
- package/lib/daemon/service.js +135 -0
- package/lib/daemon/state.js +188 -0
- package/lib/daemon/supervisor.js +244 -0
- package/lib/http.js +8 -2
- package/package.json +1 -1
package/lib/commands/agent.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* 拆开部署则用 profile 的 chatBaseUrl 指过去。
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
const fs = require('node:fs');
|
|
14
15
|
const readline = require('node:readline');
|
|
15
16
|
|
|
16
17
|
const http = require('../http');
|
|
@@ -34,6 +35,26 @@ function isFinished(payload) {
|
|
|
34
35
|
return /RUN_FINISHED|done|finish/i.test(type);
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
function extractTraceId(payload, headers) {
|
|
39
|
+
if (headers) {
|
|
40
|
+
const fromHeader = typeof headers.get === 'function' ? headers.get('x-trace-id') : headers['x-trace-id'];
|
|
41
|
+
if (fromHeader && String(fromHeader).trim() !== '00000000000000000000000000000000') {
|
|
42
|
+
return String(fromHeader).trim();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (!payload || typeof payload !== 'object') return null;
|
|
46
|
+
const tid =
|
|
47
|
+
payload.traceId ||
|
|
48
|
+
payload.trace_id ||
|
|
49
|
+
(payload.metadata && (payload.metadata.traceId || payload.metadata.trace_id)) ||
|
|
50
|
+
(payload.data && (payload.data.traceId || payload.data.trace_id)) ||
|
|
51
|
+
null;
|
|
52
|
+
if (tid && String(tid).trim() !== '00000000000000000000000000000000') {
|
|
53
|
+
return String(tid).trim();
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
37
58
|
const AGENT_COLUMNS = [
|
|
38
59
|
{ key: 'agentCode', label: 'ID' },
|
|
39
60
|
{ key: 'name', label: '名称' },
|
|
@@ -185,12 +206,14 @@ const model = {
|
|
|
185
206
|
* 发一轮对话,回复写标准输出。单次与连续对话共用这一条路径。
|
|
186
207
|
* 出错一律抛 CliError:单次模式直接决定退出码,连续对话里由调用方接住、只报一轮失败。
|
|
187
208
|
*/
|
|
188
|
-
async function sendTurn(ctx, { agentCode, sessionId, message, stream, asJson, declaredSessionId }) {
|
|
209
|
+
async function sendTurn(ctx, { agentCode, sessionId, message, stream, asJson, declaredSessionId, withTrace, traceOnly }) {
|
|
189
210
|
const path = `/${encodeURIComponent(agentCode)}/chat`;
|
|
190
211
|
const onError = (error) => {
|
|
191
212
|
throw explainMissingSession(error, declaredSessionId);
|
|
192
213
|
};
|
|
193
214
|
|
|
215
|
+
let traceId = null;
|
|
216
|
+
|
|
194
217
|
if (!stream) {
|
|
195
218
|
const result = await http
|
|
196
219
|
.request(ctx, {
|
|
@@ -198,29 +221,56 @@ async function sendTurn(ctx, { agentCode, sessionId, message, stream, asJson, de
|
|
|
198
221
|
base: ctx.chatBase(),
|
|
199
222
|
path,
|
|
200
223
|
body: { sessionId, message, mode: 'block' },
|
|
224
|
+
onResponse: (res) => {
|
|
225
|
+
const tid = extractTraceId(null, res.headers);
|
|
226
|
+
if (tid) traceId = tid;
|
|
227
|
+
},
|
|
201
228
|
})
|
|
202
229
|
.catch(onError);
|
|
230
|
+
|
|
231
|
+
if (!traceId) {
|
|
232
|
+
traceId = extractTraceId(result);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (traceOnly) {
|
|
236
|
+
process.stdout.write(`${traceId || ''}\n`);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
|
|
203
240
|
const text =
|
|
204
241
|
result && typeof result === 'object'
|
|
205
242
|
? result.reply ?? result.message ?? result.content ?? JSON.stringify(result)
|
|
206
243
|
: result;
|
|
244
|
+
|
|
245
|
+
if (asJson) {
|
|
246
|
+
output.print({ reply: text, sessionId, traceId: traceId || null }, { flags: ctx.flags });
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
|
|
207
250
|
output.print(text, { flags: ctx.flags });
|
|
251
|
+
if (withTrace && traceId) {
|
|
252
|
+
process.stdout.write(`[TraceId: ${traceId}]\n`);
|
|
253
|
+
}
|
|
208
254
|
return;
|
|
209
255
|
}
|
|
210
256
|
|
|
211
257
|
let failure = null;
|
|
212
258
|
let wroteAny = false;
|
|
259
|
+
let replyText = '';
|
|
213
260
|
await http
|
|
214
261
|
.stream(ctx, {
|
|
215
262
|
method: 'POST',
|
|
216
263
|
base: ctx.chatBase(),
|
|
217
264
|
path,
|
|
218
265
|
body: { sessionId, message, mode: 'stream' },
|
|
266
|
+
onResponse: (res) => {
|
|
267
|
+
const tid = extractTraceId(null, res.headers);
|
|
268
|
+
if (tid) traceId = tid;
|
|
269
|
+
},
|
|
219
270
|
onEvent: ({ data }) => {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
271
|
+
const tid = extractTraceId(data);
|
|
272
|
+
if (tid) traceId = tid;
|
|
273
|
+
|
|
224
274
|
const error = errorMessage(data);
|
|
225
275
|
if (error) {
|
|
226
276
|
failure = error;
|
|
@@ -228,14 +278,32 @@ async function sendTurn(ctx, { agentCode, sessionId, message, stream, asJson, de
|
|
|
228
278
|
}
|
|
229
279
|
const delta = extractDelta(data);
|
|
230
280
|
if (delta) {
|
|
231
|
-
|
|
232
|
-
|
|
281
|
+
replyText += String(delta);
|
|
282
|
+
if (!asJson && !traceOnly) {
|
|
283
|
+
process.stdout.write(String(delta));
|
|
284
|
+
wroteAny = true;
|
|
285
|
+
}
|
|
233
286
|
}
|
|
234
287
|
},
|
|
235
288
|
})
|
|
236
289
|
.catch(onError);
|
|
237
|
-
|
|
290
|
+
|
|
238
291
|
if (failure) throw new CliError(failure, EXIT.BUSINESS);
|
|
292
|
+
|
|
293
|
+
if (traceOnly) {
|
|
294
|
+
process.stdout.write(`${traceId || ''}\n`);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (asJson) {
|
|
299
|
+
output.print({ reply: replyText, sessionId, traceId: traceId || null }, { flags: ctx.flags });
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (wroteAny) process.stdout.write('\n');
|
|
304
|
+
if (withTrace && traceId) {
|
|
305
|
+
process.stdout.write(`[TraceId: ${traceId}]\n`);
|
|
306
|
+
}
|
|
239
307
|
}
|
|
240
308
|
|
|
241
309
|
/** 连续对话里可用的斜杠命令,供提示与补全。 */
|
|
@@ -253,7 +321,7 @@ function printReplHelp(ctx) {
|
|
|
253
321
|
* 提示符与所有提示走标准错误,回复走标准输出——这样 `wd-cli agent chat 66 > out.txt`
|
|
254
322
|
* 仍只把回复落进文件(clig.dev)。Ctrl-D(EOF)与 /exit 都能正常收尾。
|
|
255
323
|
*/
|
|
256
|
-
async function runInteractive(ctx, { agentCode, sessionId, stream, asJson }) {
|
|
324
|
+
async function runInteractive(ctx, { agentCode, sessionId, stream, asJson, withTrace, traceOnly }) {
|
|
257
325
|
const rl = readline.createInterface({
|
|
258
326
|
input: process.stdin,
|
|
259
327
|
output: process.stderr,
|
|
@@ -266,24 +334,30 @@ async function runInteractive(ctx, { agentCode, sessionId, stream, asJson }) {
|
|
|
266
334
|
// Ctrl-C 结束当前 REPL,而不是让 readline 默认继续等输入
|
|
267
335
|
rl.on('SIGINT', () => rl.close());
|
|
268
336
|
|
|
337
|
+
function safePrompt() {
|
|
338
|
+
try {
|
|
339
|
+
if (!rl.closed) rl.prompt();
|
|
340
|
+
} catch {}
|
|
341
|
+
}
|
|
342
|
+
|
|
269
343
|
output.info('连续对话已开始,/help 看可用命令,/exit 或 Ctrl-D 退出。', ctx.flags);
|
|
270
344
|
let current = sessionId;
|
|
271
|
-
|
|
345
|
+
safePrompt();
|
|
272
346
|
for await (const raw of rl) {
|
|
273
347
|
const line = String(raw).trim();
|
|
274
348
|
if (!line) {
|
|
275
|
-
|
|
349
|
+
safePrompt();
|
|
276
350
|
continue;
|
|
277
351
|
}
|
|
278
352
|
if (line === '/exit' || line === '/quit') break;
|
|
279
353
|
if (line === '/help') {
|
|
280
354
|
printReplHelp(ctx);
|
|
281
|
-
|
|
355
|
+
safePrompt();
|
|
282
356
|
continue;
|
|
283
357
|
}
|
|
284
358
|
if (line === '/session') {
|
|
285
359
|
output.info(`当前会话:${current}`, ctx.flags);
|
|
286
|
-
|
|
360
|
+
safePrompt();
|
|
287
361
|
continue;
|
|
288
362
|
}
|
|
289
363
|
if (line === '/new') {
|
|
@@ -293,18 +367,20 @@ async function runInteractive(ctx, { agentCode, sessionId, stream, asJson }) {
|
|
|
293
367
|
} catch (error) {
|
|
294
368
|
output.warn(`建会话失败:${error.message}`, ctx.flags);
|
|
295
369
|
}
|
|
296
|
-
|
|
370
|
+
safePrompt();
|
|
297
371
|
continue;
|
|
298
372
|
}
|
|
299
373
|
try {
|
|
300
374
|
// 会话是这里建/接手的,一轮失败不该把整个对话带走,故不传 declaredSessionId
|
|
301
|
-
await sendTurn(ctx, { agentCode, sessionId: current, message: line, stream, asJson });
|
|
375
|
+
await sendTurn(ctx, { agentCode, sessionId: current, message: line, stream, asJson, withTrace, traceOnly });
|
|
302
376
|
} catch (error) {
|
|
303
377
|
output.warn(error.message, ctx.flags);
|
|
304
378
|
}
|
|
305
|
-
|
|
379
|
+
safePrompt();
|
|
306
380
|
}
|
|
307
|
-
|
|
381
|
+
try {
|
|
382
|
+
if (!rl.closed) rl.close();
|
|
383
|
+
} catch {}
|
|
308
384
|
output.info(`对话结束。下次接着聊:wd-cli agent chat ${agentCode} --session-id ${current}`, ctx.flags);
|
|
309
385
|
}
|
|
310
386
|
|
|
@@ -317,6 +393,8 @@ const chat = {
|
|
|
317
393
|
'session-id': { type: 'string', summary: '接着已有会话聊;缺省自动新建一个并在标准错误提示 id' },
|
|
318
394
|
stream: { type: 'boolean', summary: '流式输出(消费 AG-UI 事件)' },
|
|
319
395
|
interactive: { type: 'boolean', short: 'i', summary: '发完 --message 后继续连续对话' },
|
|
396
|
+
'with-trace': { type: 'boolean', summary: '输出中附带本次会话关联的 traceId' },
|
|
397
|
+
'trace-only': { type: 'boolean', summary: '仅输出 traceId' },
|
|
320
398
|
},
|
|
321
399
|
examples: [
|
|
322
400
|
'wd-cli agent chat 66',
|
|
@@ -324,12 +402,16 @@ const chat = {
|
|
|
324
402
|
'wd-cli agent chat 66 -m "写一段介绍" --stream',
|
|
325
403
|
'wd-cli agent chat 66 -i --stream',
|
|
326
404
|
'cat prompt.txt | wd-cli agent chat 66 -m - --session-id s1',
|
|
405
|
+
'wd-cli agent chat 66 -m "你好" --with-trace',
|
|
406
|
+
'wd-cli agent chat 66 -m "你好" --trace-only',
|
|
327
407
|
],
|
|
328
408
|
async run(ctx) {
|
|
329
409
|
const agentCode = ctx.args[0];
|
|
330
410
|
const messageRaw = ctx.flags.message;
|
|
331
411
|
const stream = Boolean(ctx.flags.stream);
|
|
332
412
|
const asJson = ctx.flags.output === 'json' || ctx.flags.json || ctx.flags.jq;
|
|
413
|
+
const withTrace = Boolean(ctx.flags.withTrace);
|
|
414
|
+
const traceOnly = Boolean(ctx.flags.traceOnly);
|
|
333
415
|
|
|
334
416
|
// 不给 --message 就进连续对话;但标准输入不是终端时(脚本 / 管道)保持报错,
|
|
335
417
|
// 免得 CI 里漏传 -m 的命令挂在那儿等输入。
|
|
@@ -357,6 +439,8 @@ const chat = {
|
|
|
357
439
|
stream,
|
|
358
440
|
asJson,
|
|
359
441
|
declaredSessionId: ctx.flags.sessionId,
|
|
442
|
+
withTrace,
|
|
443
|
+
traceOnly,
|
|
360
444
|
});
|
|
361
445
|
} catch (error) {
|
|
362
446
|
// 单次模式下这一轮就是全部,错误直接决定退出码;
|
|
@@ -367,19 +451,334 @@ const chat = {
|
|
|
367
451
|
}
|
|
368
452
|
|
|
369
453
|
if (interactive) {
|
|
370
|
-
await runInteractive(ctx, { agentCode, sessionId, stream, asJson });
|
|
454
|
+
await runInteractive(ctx, { agentCode, sessionId, stream, asJson, withTrace, traceOnly });
|
|
371
455
|
}
|
|
372
456
|
return EXIT.OK;
|
|
373
457
|
},
|
|
374
458
|
};
|
|
375
459
|
|
|
460
|
+
function readFileOrText(val, label) {
|
|
461
|
+
if (val === undefined || val === null) return null;
|
|
462
|
+
const s = String(val);
|
|
463
|
+
if (s === '-') return dataInput.readDataText('-', label);
|
|
464
|
+
if (s.startsWith('@')) return dataInput.readDataText(s, label);
|
|
465
|
+
if (fs.existsSync(s) && fs.statSync(s).isFile()) {
|
|
466
|
+
return fs.readFileSync(s, 'utf8');
|
|
467
|
+
}
|
|
468
|
+
return dataInput.readDataText(s, label);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
async function doPublishAgent(ctx, agentCode, remark) {
|
|
472
|
+
const body = remark ? { remark } : {};
|
|
473
|
+
const res = await http.request(ctx, {
|
|
474
|
+
method: 'POST',
|
|
475
|
+
path: `/agent/${encodeURIComponent(agentCode)}/publish`,
|
|
476
|
+
body,
|
|
477
|
+
});
|
|
478
|
+
output.print(res, { flags: ctx.flags });
|
|
479
|
+
const ver = res && (res.versionNumber || (res.data && res.data.versionNumber));
|
|
480
|
+
output.success(`Agent ${agentCode} 已成功发布${ver ? `(版本 V${ver})` : ''}`, ctx.flags);
|
|
481
|
+
return res;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function parseSkillIds(raw) {
|
|
485
|
+
if (raw === undefined || raw === null) return undefined;
|
|
486
|
+
const list = Array.isArray(raw) ? raw : [raw];
|
|
487
|
+
const ids = [];
|
|
488
|
+
for (const item of list) {
|
|
489
|
+
for (const part of String(item).split(',')) {
|
|
490
|
+
const trimmed = part.trim();
|
|
491
|
+
if (!trimmed) continue;
|
|
492
|
+
const num = Number(trimmed);
|
|
493
|
+
if (!Number.isFinite(num) || num <= 0) {
|
|
494
|
+
throw new CliError(`非法的 skillId:${trimmed},须为正整数`, EXIT.USAGE);
|
|
495
|
+
}
|
|
496
|
+
ids.push(num);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
return ids.length > 0 ? ids : undefined;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
const create = {
|
|
503
|
+
name: 'create',
|
|
504
|
+
summary: '在当前工作空间新建 Agent(参数化或 YAML DSL 导入)',
|
|
505
|
+
principals: ['workspace'],
|
|
506
|
+
flags: {
|
|
507
|
+
name: { type: 'string', summary: 'Agent 名称(参数化创建时必填)' },
|
|
508
|
+
description: { type: 'string', summary: 'Agent 描述' },
|
|
509
|
+
type: { type: 'string', summary: '应用类型(缺省 agent)' },
|
|
510
|
+
prompt: { type: 'string', summary: 'System Prompt 提示词' },
|
|
511
|
+
'prompt-file': { type: 'string', summary: '从文件或标准输入(-)读取提示词' },
|
|
512
|
+
dsl: { type: 'string', summary: '从 YAML 文件或标准输入(-)读取完整 DSL 导入创建' },
|
|
513
|
+
'model-id': { type: 'number', summary: 'LLM 模型 ID' },
|
|
514
|
+
temperature: { type: 'number', summary: '模型采样温度 (0.0~2.0)' },
|
|
515
|
+
'max-tokens': { type: 'number', summary: '单次最大 Token 限制' },
|
|
516
|
+
'max-iterations': { type: 'number', summary: '最大迭代轮数' },
|
|
517
|
+
'max-iteration-count': { type: 'number', summary: '最大迭代轮数(等同 --max-iterations)' },
|
|
518
|
+
'skill-id': { type: 'string', repeat: true, summary: '初始绑定的技能 ID(可重复或逗号分隔)' },
|
|
519
|
+
'dry-run': { type: 'boolean', summary: '只打印将要提交的数据,不真正发起请求' },
|
|
520
|
+
publish: { type: 'boolean', summary: '创建成功后自动联动发布新版本' },
|
|
521
|
+
remark: { type: 'string', summary: '发布说明(配合 --publish 生效)' },
|
|
522
|
+
},
|
|
523
|
+
examples: [
|
|
524
|
+
'wd-cli agent create --name "数仓排障助手" --model-id 3 --prompt "你是数仓专家"',
|
|
525
|
+
'wd-cli agent create --name "数仓排障助手" --model-id 3 --prompt-file ./prompt.md --skill-id 128 --publish',
|
|
526
|
+
'wd-cli agent create --dsl ./agent.yaml --publish --remark "初始版本"',
|
|
527
|
+
],
|
|
528
|
+
async run(ctx) {
|
|
529
|
+
const {
|
|
530
|
+
name,
|
|
531
|
+
description,
|
|
532
|
+
type,
|
|
533
|
+
dsl,
|
|
534
|
+
prompt,
|
|
535
|
+
promptFile,
|
|
536
|
+
modelId,
|
|
537
|
+
temperature,
|
|
538
|
+
maxTokens,
|
|
539
|
+
maxIterations,
|
|
540
|
+
maxIterationCount,
|
|
541
|
+
skillId,
|
|
542
|
+
dryRun,
|
|
543
|
+
publish: shouldPublish,
|
|
544
|
+
remark: publishRemark,
|
|
545
|
+
} = ctx.flags;
|
|
546
|
+
|
|
547
|
+
if (dsl !== undefined) {
|
|
548
|
+
const yamlText = String(readFileOrText(dsl, '--dsl'));
|
|
549
|
+
if (dryRun) {
|
|
550
|
+
output.info('[dry-run] 将通过 DSL 新建 Agent' + (shouldPublish ? ' 并自动发布' : '') + ':\n' + yamlText, ctx.flags);
|
|
551
|
+
return EXIT.OK;
|
|
552
|
+
}
|
|
553
|
+
const created = await http.request(ctx, {
|
|
554
|
+
method: 'POST',
|
|
555
|
+
path: '/workspace-agents/dsl',
|
|
556
|
+
headers: { 'Content-Type': 'text/yaml' },
|
|
557
|
+
body: yamlText,
|
|
558
|
+
});
|
|
559
|
+
output.print(created, { flags: ctx.flags });
|
|
560
|
+
const newCode = created && (created.agentCode || created.agentId || created.id);
|
|
561
|
+
output.success(`已通过 DSL 创建 Agent:${newCode || ''}`, ctx.flags);
|
|
562
|
+
if (shouldPublish && newCode) {
|
|
563
|
+
await doPublishAgent(ctx, String(newCode), publishRemark);
|
|
564
|
+
}
|
|
565
|
+
return EXIT.OK;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
if (!name || !String(name).trim()) {
|
|
569
|
+
throw new CliError('必须指定 --name <名称> 或 --dsl <YAML文件>', EXIT.USAGE);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
let effectivePrompt = prompt;
|
|
573
|
+
if (promptFile !== undefined) {
|
|
574
|
+
effectivePrompt = String(readFileOrText(promptFile, '--prompt-file'));
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
const payload = { name: String(name).trim() };
|
|
578
|
+
if (description !== undefined) payload.description = String(description);
|
|
579
|
+
if (type !== undefined) payload.appType = String(type);
|
|
580
|
+
if (effectivePrompt !== undefined) payload.prompt = effectivePrompt;
|
|
581
|
+
if (modelId !== undefined) payload.modelId = Number(modelId);
|
|
582
|
+
if (temperature !== undefined) payload.temperature = Number(temperature);
|
|
583
|
+
if (maxTokens !== undefined) payload.maxTokens = Number(maxTokens);
|
|
584
|
+
const iter = maxIterations !== undefined ? maxIterations : maxIterationCount;
|
|
585
|
+
if (iter !== undefined) payload.maxIterationCount = Number(iter);
|
|
586
|
+
const skillIds = parseSkillIds(skillId);
|
|
587
|
+
if (skillIds !== undefined) payload.skillIds = skillIds;
|
|
588
|
+
|
|
589
|
+
if (dryRun) {
|
|
590
|
+
output.info('[dry-run] 将创建 Agent' + (shouldPublish ? ' 并自动发布' : '') + ':\n' + JSON.stringify(payload, null, 2), ctx.flags);
|
|
591
|
+
return EXIT.OK;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
const created = await http.request(ctx, {
|
|
595
|
+
method: 'POST',
|
|
596
|
+
path: '/workspace-agents',
|
|
597
|
+
body: payload,
|
|
598
|
+
});
|
|
599
|
+
output.print(created, { flags: ctx.flags });
|
|
600
|
+
const newCode = created && (created.agentCode || created.agentId || created.id);
|
|
601
|
+
output.success(`已创建 Agent:${newCode || payload.name}`, ctx.flags);
|
|
602
|
+
if (shouldPublish && newCode) {
|
|
603
|
+
await doPublishAgent(ctx, String(newCode), publishRemark);
|
|
604
|
+
}
|
|
605
|
+
return EXIT.OK;
|
|
606
|
+
},
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
const publish = {
|
|
610
|
+
name: 'publish',
|
|
611
|
+
summary: '发布 Agent 编排为正式生效版本',
|
|
612
|
+
principals: ['workspace'],
|
|
613
|
+
args: [{ name: 'agentCode', required: true, summary: 'Agent ID 或 agentId#versionId' }],
|
|
614
|
+
flags: {
|
|
615
|
+
remark: { type: 'string', summary: '发布说明(版本备注)' },
|
|
616
|
+
},
|
|
617
|
+
examples: [
|
|
618
|
+
'wd-cli agent publish 66',
|
|
619
|
+
'wd-cli agent publish 66 --remark "发布最新提示词与模型参数"',
|
|
620
|
+
],
|
|
621
|
+
async run(ctx) {
|
|
622
|
+
const agentCode = ctx.args[0];
|
|
623
|
+
await doPublishAgent(ctx, agentCode, ctx.flags.remark);
|
|
624
|
+
return EXIT.OK;
|
|
625
|
+
},
|
|
626
|
+
};
|
|
627
|
+
|
|
628
|
+
const update = {
|
|
629
|
+
name: 'update',
|
|
630
|
+
summary: '更新 Agent 编排(增量配置或全量 DSL)',
|
|
631
|
+
principals: ['workspace'],
|
|
632
|
+
args: [{ name: 'agentCode', required: true, summary: 'Agent ID 或 agentId#versionId' }],
|
|
633
|
+
flags: {
|
|
634
|
+
prompt: { type: 'string', summary: 'System Prompt 提示词' },
|
|
635
|
+
'prompt-file': { type: 'string', summary: '从文件或标准输入(-)读取提示词' },
|
|
636
|
+
dsl: { type: 'string', summary: '从 YAML 文件或标准输入(-)读取完整 DSL 覆盖更新' },
|
|
637
|
+
'model-id': { type: 'number', summary: 'LLM 模型 ID' },
|
|
638
|
+
temperature: { type: 'number', summary: '模型采样温度 (0.0~2.0)' },
|
|
639
|
+
'max-tokens': { type: 'number', summary: '单次最大 Token 限制' },
|
|
640
|
+
'max-iterations': { type: 'number', summary: '最大迭代轮数' },
|
|
641
|
+
'max-iteration-count': { type: 'number', summary: '最大迭代轮数(等同 --max-iterations)' },
|
|
642
|
+
'dry-run': { type: 'boolean', summary: '只打印将要提交的数据,不真正发起请求' },
|
|
643
|
+
publish: { type: 'boolean', summary: '更新成功后自动联动发布新版本' },
|
|
644
|
+
remark: { type: 'string', summary: '发布说明(配合 --publish 生效)' },
|
|
645
|
+
},
|
|
646
|
+
examples: [
|
|
647
|
+
'wd-cli agent update 66 --prompt "你是智能助手"',
|
|
648
|
+
'wd-cli agent update 66 --prompt-file ./prompt.txt --temperature 0.7',
|
|
649
|
+
'wd-cli agent update 66 --dsl ./agent.yaml',
|
|
650
|
+
'wd-cli agent update 66 --dsl - < ./agent.yaml --dry-run',
|
|
651
|
+
'wd-cli agent update 66 --dsl ./agent.yaml --publish --remark "发布更新"',
|
|
652
|
+
],
|
|
653
|
+
async run(ctx) {
|
|
654
|
+
const agentCode = ctx.args[0];
|
|
655
|
+
const { dsl, prompt, promptFile, modelId, temperature, maxTokens, maxIterations, maxIterationCount, dryRun, publish: shouldPublish, remark: publishRemark } = ctx.flags;
|
|
656
|
+
|
|
657
|
+
if (dsl !== undefined) {
|
|
658
|
+
const yamlText = String(readFileOrText(dsl, '--dsl'));
|
|
659
|
+
if (dryRun) {
|
|
660
|
+
output.info('[dry-run] 将通过 DSL 覆盖更新 Agent' + (shouldPublish ? ' 并自动发布' : '') + ':\n' + yamlText, ctx.flags);
|
|
661
|
+
return EXIT.OK;
|
|
662
|
+
}
|
|
663
|
+
const updated = await http.request(ctx, {
|
|
664
|
+
method: 'PUT',
|
|
665
|
+
path: `/agent/${encodeURIComponent(agentCode)}/dsl`,
|
|
666
|
+
headers: { 'Content-Type': 'text/yaml' },
|
|
667
|
+
body: yamlText,
|
|
668
|
+
});
|
|
669
|
+
output.print(updated, { flags: ctx.flags });
|
|
670
|
+
output.success(`Agent ${agentCode} DSL 已更新`, ctx.flags);
|
|
671
|
+
if (shouldPublish) {
|
|
672
|
+
await doPublishAgent(ctx, agentCode, publishRemark);
|
|
673
|
+
}
|
|
674
|
+
return EXIT.OK;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
let effectivePrompt = prompt;
|
|
678
|
+
if (promptFile !== undefined) {
|
|
679
|
+
effectivePrompt = String(readFileOrText(promptFile, '--prompt-file'));
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
const payload = {};
|
|
683
|
+
if (effectivePrompt !== undefined) payload.prompt = effectivePrompt;
|
|
684
|
+
if (modelId !== undefined) payload.modelId = Number(modelId);
|
|
685
|
+
if (temperature !== undefined) payload.temperature = Number(temperature);
|
|
686
|
+
if (maxTokens !== undefined) payload.maxTokens = Number(maxTokens);
|
|
687
|
+
const iter = maxIterations !== undefined ? maxIterations : maxIterationCount;
|
|
688
|
+
if (iter !== undefined) payload.maxIterationCount = Number(iter);
|
|
689
|
+
|
|
690
|
+
if (Object.keys(payload).length === 0) {
|
|
691
|
+
throw new CliError('未指定任何更新字段(需指定 --prompt, --prompt-file, --dsl, --model-id 等)', EXIT.USAGE);
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
if (dryRun) {
|
|
695
|
+
output.info('[dry-run] 将提交增量编排配置' + (shouldPublish ? ' 并自动发布' : '') + ':\n' + JSON.stringify(payload, null, 2), ctx.flags);
|
|
696
|
+
return EXIT.OK;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
const updated = await http.request(ctx, {
|
|
700
|
+
method: 'PUT',
|
|
701
|
+
path: `/agent/${encodeURIComponent(agentCode)}/orchestration`,
|
|
702
|
+
body: payload,
|
|
703
|
+
});
|
|
704
|
+
output.print(updated, { flags: ctx.flags });
|
|
705
|
+
output.success(`Agent ${agentCode} 编排配置已更新`, ctx.flags);
|
|
706
|
+
if (shouldPublish) {
|
|
707
|
+
await doPublishAgent(ctx, agentCode, publishRemark);
|
|
708
|
+
}
|
|
709
|
+
return EXIT.OK;
|
|
710
|
+
},
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
const bindSkill = {
|
|
714
|
+
name: 'bind-skill',
|
|
715
|
+
summary: '为 Agent 绑定工作空间技能',
|
|
716
|
+
principals: ['workspace'],
|
|
717
|
+
args: [
|
|
718
|
+
{ name: 'agentCode', required: true, summary: 'Agent ID' },
|
|
719
|
+
{ name: 'skillId', required: false, summary: '待绑定的技能 ID(也可通过 --skill-id 指定)' },
|
|
720
|
+
],
|
|
721
|
+
flags: {
|
|
722
|
+
'skill-id': { type: 'number', summary: '待绑定的技能 ID' },
|
|
723
|
+
},
|
|
724
|
+
examples: ['wd-cli agent bind-skill 66 128', 'wd-cli agent bind-skill 66 --skill-id 128'],
|
|
725
|
+
async run(ctx) {
|
|
726
|
+
const agentCode = ctx.args[0];
|
|
727
|
+
const skillIdRaw = ctx.flags?.skillId !== undefined ? ctx.flags.skillId : (ctx.flags?.['skill-id'] !== undefined ? ctx.flags['skill-id'] : ctx.args[1]);
|
|
728
|
+
const skillId = Number(skillIdRaw);
|
|
729
|
+
if (!skillId || Number.isNaN(skillId)) {
|
|
730
|
+
throw new CliError('必须指定合法的 skillId(位置参数或 --skill-id),须为正整数', EXIT.USAGE);
|
|
731
|
+
}
|
|
732
|
+
const res = await http.request(ctx, {
|
|
733
|
+
method: 'POST',
|
|
734
|
+
path: `/agent/${encodeURIComponent(agentCode)}/skills/bind`,
|
|
735
|
+
body: { skillId },
|
|
736
|
+
});
|
|
737
|
+
output.print(res, { flags: ctx.flags });
|
|
738
|
+
output.success(`已为 Agent ${agentCode} 绑定技能 ${skillId}`, ctx.flags);
|
|
739
|
+
return EXIT.OK;
|
|
740
|
+
},
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
const unbindSkill = {
|
|
744
|
+
name: 'unbind-skill',
|
|
745
|
+
summary: '从 Agent 解绑技能',
|
|
746
|
+
principals: ['workspace'],
|
|
747
|
+
args: [
|
|
748
|
+
{ name: 'agentCode', required: true, summary: 'Agent ID' },
|
|
749
|
+
{ name: 'skillId', required: false, summary: '待解绑的技能 ID(也可通过 --skill-id 指定)' },
|
|
750
|
+
],
|
|
751
|
+
flags: {
|
|
752
|
+
'skill-id': { type: 'number', summary: '待解绑的技能 ID' },
|
|
753
|
+
},
|
|
754
|
+
examples: ['wd-cli agent unbind-skill 66 128', 'wd-cli agent unbind-skill 66 --skill-id 128'],
|
|
755
|
+
async run(ctx) {
|
|
756
|
+
const agentCode = ctx.args[0];
|
|
757
|
+
const skillIdRaw = ctx.flags?.skillId !== undefined ? ctx.flags.skillId : (ctx.flags?.['skill-id'] !== undefined ? ctx.flags['skill-id'] : ctx.args[1]);
|
|
758
|
+
const skillId = Number(skillIdRaw);
|
|
759
|
+
if (!skillId || Number.isNaN(skillId)) {
|
|
760
|
+
throw new CliError('必须指定合法的 skillId(位置参数或 --skill-id),须为正整数', EXIT.USAGE);
|
|
761
|
+
}
|
|
762
|
+
const res = await http.request(ctx, {
|
|
763
|
+
method: 'POST',
|
|
764
|
+
path: `/agent/${encodeURIComponent(agentCode)}/skills/unbind`,
|
|
765
|
+
body: { skillId },
|
|
766
|
+
});
|
|
767
|
+
output.print(res, { flags: ctx.flags });
|
|
768
|
+
output.success(`已从 Agent ${agentCode} 解绑技能 ${skillId}`, ctx.flags);
|
|
769
|
+
return EXIT.OK;
|
|
770
|
+
},
|
|
771
|
+
};
|
|
772
|
+
|
|
376
773
|
module.exports = {
|
|
377
774
|
id: 'agent',
|
|
378
|
-
summary: '应用:列表 / 信息 / 元数据 / 对话',
|
|
775
|
+
summary: '应用:列表 / 信息 / 元数据 / 对话 / 编排维护',
|
|
379
776
|
description: '对话端点在 server/gateway;与其余 Open API 不同域名时用 wd-cli config set chatBaseUrl 指定。',
|
|
380
777
|
principals: ['agent'],
|
|
381
|
-
commands: [list, info, metadata, revision, model, chat],
|
|
778
|
+
commands: [list, create, info, metadata, revision, model, chat, update, publish, bindSkill, unbindSkill],
|
|
382
779
|
extractDelta,
|
|
383
780
|
isFinished,
|
|
384
781
|
errorMessage,
|
|
782
|
+
extractTraceId,
|
|
385
783
|
};
|
|
784
|
+
|