evolclaw 3.2.0 → 3.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.
Files changed (95) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/README.md +7 -4
  3. package/dist/agents/{resolve.js → baseagent.js} +34 -5
  4. package/dist/agents/claude-runner.js +120 -31
  5. package/dist/agents/codex-app-server-client.js +364 -0
  6. package/dist/agents/codex-runner.js +1152 -140
  7. package/dist/agents/gemini-runner.js +2 -2
  8. package/dist/agents/runner-types.js +58 -0
  9. package/dist/aun/aid/store.js +1 -1
  10. package/dist/aun/outbox.js +14 -2
  11. package/dist/aun/storage/download.js +1 -1
  12. package/dist/aun/storage/upload.js +13 -1
  13. package/dist/channels/aun.js +869 -358
  14. package/dist/channels/dingtalk.js +77 -140
  15. package/dist/channels/feishu.js +125 -154
  16. package/dist/channels/qqbot.js +75 -138
  17. package/dist/channels/wechat.js +75 -136
  18. package/dist/channels/wecom.js +75 -138
  19. package/dist/cli/agent-command.js +591 -0
  20. package/dist/cli/agent.js +23 -8
  21. package/dist/cli/aun-commands.js +1444 -0
  22. package/dist/cli/ctl-command.js +78 -0
  23. package/dist/cli/daemon-commands.js +2707 -0
  24. package/dist/cli/index.js +23 -4905
  25. package/dist/cli/init.js +33 -6
  26. package/dist/cli/model.js +1 -1
  27. package/dist/cli/restart-monitor.js +539 -0
  28. package/dist/cli/stats.js +558 -0
  29. package/dist/cli/version.js +87 -0
  30. package/dist/cli/watch-logs.js +33 -0
  31. package/dist/cli/watch-msg.js +5 -2
  32. package/dist/config-store.js +12 -6
  33. package/dist/core/channel-loader.js +88 -83
  34. package/dist/core/command/command-handler.js +1189 -0
  35. package/dist/core/command/menu-handler.js +1478 -0
  36. package/dist/core/command/slash-gate.js +142 -0
  37. package/dist/core/command/slash-handler.js +2090 -0
  38. package/dist/core/evolagent-registry.js +82 -0
  39. package/dist/core/evolagent.js +17 -1
  40. package/dist/core/interaction-router.js +8 -0
  41. package/dist/core/message/command-handler-agent-control.js +63 -1
  42. package/dist/core/message/im-renderer.js +91 -51
  43. package/dist/core/message/items-formatter.js +9 -1
  44. package/dist/core/message/message-bridge.js +73 -24
  45. package/dist/core/message/message-log.js +1 -0
  46. package/dist/core/message/message-processor.js +432 -94
  47. package/dist/core/message/message-queue.js +70 -2
  48. package/dist/core/message/pending-hints.js +232 -0
  49. package/dist/core/model/model-catalog.js +1 -1
  50. package/dist/core/model/model-scope.js +2 -2
  51. package/dist/core/permission.js +25 -12
  52. package/dist/core/relation/peer-identity.js +16 -1
  53. package/dist/core/session/adapters/codex-session-file-adapter.js +4 -2
  54. package/dist/core/session/session-manager.js +86 -26
  55. package/dist/core/session/session-title.js +26 -0
  56. package/dist/core/stats/billing.js +151 -0
  57. package/dist/core/stats/budget.js +93 -0
  58. package/dist/core/stats/db.js +334 -0
  59. package/dist/core/stats/eck-vars.js +84 -0
  60. package/dist/core/stats/index.js +10 -0
  61. package/dist/core/stats/normalizer.js +78 -0
  62. package/dist/core/stats/query.js +760 -0
  63. package/dist/core/stats/writer.js +115 -0
  64. package/dist/core/trigger/manager.js +34 -0
  65. package/dist/core/trigger/parser.js +9 -3
  66. package/dist/core/trigger/scheduler.js +20 -17
  67. package/dist/data/error-dict.json +7 -0
  68. package/dist/{agents → eck}/manifest-engine.js +20 -1
  69. package/dist/{agents → eck}/message-renderer.js +24 -1
  70. package/dist/index.js +174 -9
  71. package/dist/ipc.js +116 -1
  72. package/dist/utils/cross-platform.js +58 -5
  73. package/dist/utils/ecweb-launch.js +49 -0
  74. package/dist/utils/ecweb-pair.js +20 -0
  75. package/dist/utils/error-utils.js +18 -5
  76. package/dist/utils/npm-ops.js +38 -8
  77. package/dist/utils/stats.js +77 -6
  78. package/kits/docs/evolclaw/INDEX.md +3 -1
  79. package/kits/docs/evolclaw/fs-architecture.md +1215 -0
  80. package/kits/docs/evolclaw/fs.md +131 -0
  81. package/kits/docs/evolclaw/group-fs.md +209 -0
  82. package/kits/docs/evolclaw/stats.md +70 -0
  83. package/kits/docs/venues/aun-group.md +29 -6
  84. package/kits/docs/venues/group.md +5 -4
  85. package/kits/eck_message_manifest.json +30 -3
  86. package/kits/rules/05-venue.md +1 -1
  87. package/kits/templates/message-fragments/inject-default.md +2 -0
  88. package/package.json +5 -6
  89. package/dist/agents/baseagent-normalize.js +0 -19
  90. package/dist/core/command-handler.js +0 -3876
  91. package/dist/core/relation/peer-key.js +0 -16
  92. package/dist/evolclaw-config.js +0 -11
  93. package/dist/utils/channel-helpers.js +0 -46
  94. /package/dist/core/{cache/file-cache.js → daemon-file-cache.js} +0 -0
  95. /package/dist/{agents → eck}/kit-renderer.js +0 -0
@@ -0,0 +1,1444 @@
1
+ import fs from 'fs';
2
+ import { agentMdPath, resolveRoot } from '../paths.js';
3
+ import { getArgValue, isHelpFlag, wantsHelp } from './help.js';
4
+ // ==================== AID ====================
5
+ function resolveAunPath(args) {
6
+ const idx = args.indexOf('--aun-path');
7
+ if (idx !== -1 && idx + 1 < args.length)
8
+ return args[idx + 1];
9
+ return process.env.AUN_HOME || undefined;
10
+ }
11
+ export async function cmdAid(args) {
12
+ const sub = args[0];
13
+ const formatJson = getArgValue(args, '--format') === 'json';
14
+ const aunPath = resolveAunPath(args);
15
+ if (!sub || isHelpFlag(sub)) {
16
+ console.log(`用法: evolclaw aid <command>
17
+
18
+ Commands:
19
+ list 列出本地所有 AID(实测 sign+verify)
20
+ show <aid> 查看本地 AID 详情(证书、私钥、签名能力)
21
+ new <aid> 创建新 AID 身份
22
+ delete <aid> 删除指定本地 AID(无网络注销)
23
+ delete --orphan 批量清理无私钥的外部 AID 缓存
24
+ delete --no-cert 批量清理无私钥也无公钥证书的孤儿目录
25
+ delete --unrecoverable 批量清理云端公钥已变更、本地不可恢复的 AID
26
+ 批量删除默认 dry-run,加 --yes 执行
27
+ lookup <aid> 远程探测 AID(是否存在 + 网关 + agent.md)
28
+ agentmd put <aid> 读本地 agent.md → 签名 → 上传
29
+ agentmd get <aid> 下载 agent.md → 验签 → 本地持久化
30
+
31
+ Options:
32
+ --format json 输出 JSON 格式
33
+ --help, -h 各子命令均支持,查看详细用法
34
+
35
+ 示例:
36
+ evolclaw aid list
37
+ evolclaw aid show toleiliang2.agentid.pub
38
+ evolclaw aid new reviewer.agentid.pub
39
+ evolclaw aid delete --help
40
+ evolclaw aid delete old.agentid.pub
41
+ evolclaw aid delete --orphan
42
+ evolclaw aid delete --unrecoverable --yes
43
+ evolclaw aid lookup someone.agentid.pub
44
+ evolclaw aid agentmd put mybot.agentid.pub
45
+ evolclaw aid agentmd get someone.agentid.pub`);
46
+ return;
47
+ }
48
+ const { aidList, aidListVerified, aidCreate, aidShow, aidDelete, aidLookup, agentmdPut, agentmdGet, buildInitialAgentMd, isValidAid } = await import('../aun/aid/index.js');
49
+ if (sub === 'list') {
50
+ if (wantsHelp(args)) {
51
+ console.log(`用法: evolclaw aid list [筛选选项] [--no-verify] [--format json]
52
+
53
+ 列出本地 AID 并跑 sign+verify 自检。
54
+
55
+ 筛选选项(可组合,不指定 = 列出 mine + broken + peer-cert):
56
+ --mine 仅本地可用身份(实测可签名+验签通过)
57
+ --broken 仅有私钥但不可用(公钥不匹配 / 证书过期 / sign 失败)
58
+ --peer-cert 仅对端 AID(无私钥,有公钥证书)
59
+ --no-cert 仅无私钥无证书的目录(默认隐藏,需用 aid delete --no-cert 清理)
60
+
61
+ 选项:
62
+ --no-verify 跳过 sign+verify 实测,仅静态扫描(更快,mine/broken 仅按静态判定近似)
63
+ --format json JSON 格式输出
64
+
65
+ 输出图标:
66
+ 🔑 有私钥
67
+ ✅ 实测可签名/验签
68
+ ❌ 不可签名(公钥不匹配 / sign 失败 / verify 失败等)
69
+ ⌛ 证书过期
70
+ 📜 有公钥证书
71
+ 📄 有 agent.md
72
+
73
+ 示例:
74
+ evolclaw aid list 列出 mine + broken + peer-cert
75
+ evolclaw aid list --mine 仅可用身份
76
+ evolclaw aid list --mine --broken 所有有私钥的 AID
77
+ evolclaw aid list --no-cert 仅无私钥无证书的孤儿目录
78
+ evolclaw aid list --no-verify 跳过实测,快速静态扫描`);
79
+ return;
80
+ }
81
+ const wantMine = args.includes('--mine');
82
+ const wantBroken = args.includes('--broken');
83
+ const wantPeerCert = args.includes('--peer-cert');
84
+ const wantNoCert = args.includes('--no-cert');
85
+ const noVerify = args.includes('--no-verify');
86
+ const anyFilter = wantMine || wantBroken || wantPeerCert || wantNoCert;
87
+ // 默认: mine + broken + peer-cert(隐藏 no-cert,需显式 --no-cert 才列)
88
+ const showMine = anyFilter ? wantMine : true;
89
+ const showBroken = anyFilter ? wantBroken : true;
90
+ const showPeerCert = anyFilter ? wantPeerCert : true;
91
+ const showNoCert = anyFilter ? wantNoCert : false;
92
+ const all = noVerify ? aidList(aunPath) : await aidListVerified(aunPath);
93
+ const aids = all.filter(a => (showMine && a.category === 'mine') ||
94
+ (showBroken && a.category === 'broken') ||
95
+ (showPeerCert && a.category === 'peer-cert') ||
96
+ (showNoCert && a.category === 'no-cert'));
97
+ if (formatJson) {
98
+ console.log(JSON.stringify(aids, null, 2));
99
+ return;
100
+ }
101
+ if (aids.length === 0) {
102
+ console.log('无匹配 AID');
103
+ return;
104
+ }
105
+ console.log(`本地 AID${noVerify ? '(静态扫描,未实测)' : ''}(${aunPath ?? resolveRoot()}):`);
106
+ for (const a of aids) {
107
+ const keyIcon = a.hasPrivateKey ? '🔑' : ' ';
108
+ let signIcon = ' ';
109
+ // --no-verify 时 signVerified 始终为 null,用 canSign 作为静态近似
110
+ const effectiveOk = noVerify ? a.canSign : a.signVerified === true;
111
+ const effectiveFail = noVerify ? (a.hasPrivateKey && !a.canSign) : (a.hasPrivateKey && a.signVerified === false);
112
+ if (effectiveOk)
113
+ signIcon = '✅';
114
+ else if (a.hasPrivateKey && a.certExpired)
115
+ signIcon = '⌛';
116
+ else if (effectiveFail)
117
+ signIcon = '❌';
118
+ const certIcon = a.hasCert ? '📜' : ' ';
119
+ const mdIcon = a.hasAgentMd ? '📄' : ' ';
120
+ const tail = !noVerify && a.signVerified === false && a.signError && !(a.keyMatchesCert === false || a.certExpired || !a.hasPrivateKey || !a.hasCert)
121
+ ? ` (${a.signError})` : '';
122
+ console.log(` ${keyIcon} ${signIcon} ${certIcon} ${mdIcon} ${a.aid}${tail}`);
123
+ }
124
+ console.log('\n🔑=私钥 ✅=可签名/验签 ❌=不可签名 ⌛=证书过期 📜=公钥证书 📄=agent.md');
125
+ return;
126
+ }
127
+ if (sub === 'show') {
128
+ if (wantsHelp(args)) {
129
+ console.log(`用法: evolclaw aid show <aid> [--format json]
130
+
131
+ 查看本地 AID 详情:私钥/证书/agent.md 状态、签名能力实测。`);
132
+ return;
133
+ }
134
+ const aid = args[1];
135
+ if (!aid) {
136
+ console.error('用法: evolclaw aid show <aid>');
137
+ process.exit(1);
138
+ }
139
+ const info = await aidShow(aid, { aunPath });
140
+ if (formatJson) {
141
+ console.log(JSON.stringify(info, null, 2));
142
+ return;
143
+ }
144
+ console.log(`AID: ${info.aid}`);
145
+ console.log(` 私钥: ${info.hasPrivateKey ? '有' : '无'}`);
146
+ console.log(` agent.md: ${info.hasAgentMd ? '有' : '无'}`);
147
+ if (info.hasAgentMd) {
148
+ const sigLabel = info.agentMdSignature === 'verified' ? '✓ 已验签'
149
+ : info.agentMdSignature === 'unsigned' ? '⚠ 未签名'
150
+ : info.agentMdSignature === 'invalid' ? `✗ 签名无效${info.agentMdSignatureReason ? ': ' + info.agentMdSignatureReason : ''}`
151
+ : '? 未知';
152
+ console.log(` 签名状态: ${sigLabel}`);
153
+ }
154
+ console.log(` 证书到期: ${info.certExpiresAt ?? '无证书'}${info.certExpired ? ' (已过期!)' : ''}`);
155
+ if (info.certSubject)
156
+ console.log(` 证书主体: ${info.certSubject}`);
157
+ if (info.keyMatchesCert === false)
158
+ console.log(` 密钥/证书: ✗ 公钥不匹配(cert.pem 与 key.json 公钥不一致)`);
159
+ else if (info.keyMatchesCert === true)
160
+ console.log(` 密钥/证书: ✓ 公钥一致`);
161
+ if (info.signVerified === true)
162
+ console.log(` 可签名/验签: ✓ 实测通过`);
163
+ else if (info.signVerified === false)
164
+ console.log(` 可签名/验签: ✗ 失败${info.signError ? `(${info.signError})` : ''}`);
165
+ else
166
+ console.log(` 可签名/验签: ? 未知`);
167
+ return;
168
+ }
169
+ if (sub === 'new') {
170
+ if (wantsHelp(args)) {
171
+ console.log(`用法: evolclaw aid new <完整AID> [--force]
172
+
173
+ 创建新 AID 身份:生成 ECDSA 密钥对、向 Issuer 申请证书、构建并上传初始 agent.md。
174
+
175
+ 选项:
176
+ --force 强制重新注册,覆盖已存在的身份(即使签名验证失败)
177
+
178
+ 例: evolclaw aid new reviewer.agentid.pub
179
+ evolclaw aid new reviewer.agentid.pub --force`);
180
+ return;
181
+ }
182
+ const aid = args[1];
183
+ const force = args.includes('--force');
184
+ if (!aid) {
185
+ console.error('用法: evolclaw aid new <完整AID> [--force]\n例: evolclaw aid new reviewer.agentid.pub');
186
+ process.exit(1);
187
+ }
188
+ if (!isValidAid(aid)) {
189
+ console.error(`❌ 无效 AID 格式: ${aid}`);
190
+ process.exit(1);
191
+ }
192
+ try {
193
+ const result = await aidCreate(aid, { aunPath, force });
194
+ if (!result.alreadyExisted) {
195
+ const content = buildInitialAgentMd({ aid });
196
+ try {
197
+ await agentmdPut(content, { aid, aunPath });
198
+ console.log('✓ agent.md 已发布');
199
+ }
200
+ catch (e) {
201
+ console.warn(`⚠ agent.md 发布失败(首次连接将自动重试): ${String(e.message || e).slice(0, 100)}`);
202
+ }
203
+ }
204
+ try {
205
+ await result.client.close();
206
+ }
207
+ catch { }
208
+ try {
209
+ result.store?.close();
210
+ }
211
+ catch { }
212
+ const verb = result.alreadyExisted ? '已存在且有效' : (force ? '已重新创建' : '已创建');
213
+ console.log(`✓ ${aid} ${verb}`);
214
+ console.log(' 如需上线 AUN 通道,运行 evolclaw agent new ' + aid);
215
+ }
216
+ catch (e) {
217
+ if (e.code === 'AID_INVALID') {
218
+ console.error(`❌ ${e.message}`);
219
+ process.exit(1);
220
+ }
221
+ if (e.code === -32052 || e.constructor?.name === 'IdentityConflictError') {
222
+ console.error(`❌ AID ${aid} 已在服务端注册,但本地密钥无法匹配。\n` +
223
+ `该 AID 可能由其他设备创建,无法在本地恢复。请选择其他名称。`);
224
+ process.exit(1);
225
+ }
226
+ throw e;
227
+ }
228
+ return;
229
+ }
230
+ if (sub === 'delete') {
231
+ if (wantsHelp(args)) {
232
+ console.log(`用法: evolclaw aid delete <子命令>
233
+
234
+ 单个删除:
235
+ evolclaw aid delete <aid> 删除指定 AID 的本地数据(无网络注销)
236
+
237
+ 批量删除(默认 dry-run,加 --yes 才真删):
238
+ evolclaw aid delete --orphan 删除所有"无私钥"的本地缓存(外部 AID)
239
+ evolclaw aid delete --no-cert 删除所有"无私钥也无公钥证书"的目录
240
+ 条件:!hasPrivateKey && !hasCert
241
+ 这些目录最多只剩 agent.md 或 SQLite 残留,
242
+ 对验签和加密通信都没用,删除安全。
243
+ evolclaw aid delete --unrecoverable 删除所有不可恢复的 AID
244
+ 条件:本地 sign+verify 实测失败
245
+ 且 PKI 探测确认云端公钥也不等本地 key.json
246
+
247
+ 选项:
248
+ --yes 跳过 dry-run,立即执行
249
+ --skip-pki --unrecoverable 时跳过 PKI 探测,仅依据本地 sign+verify 失败判断(危险,可能误删可恢复 AID)
250
+ --format json 输出 JSON 格式
251
+
252
+ 示例:
253
+ evolclaw aid delete old.agentid.pub
254
+ evolclaw aid delete --orphan 列出会被清理的孤儿
255
+ evolclaw aid delete --orphan --yes 实际清理
256
+ evolclaw aid delete --no-cert 列出无证书孤儿目录
257
+ evolclaw aid delete --no-cert --yes 实际清理
258
+ evolclaw aid delete --unrecoverable 联网探测后列出无救 AID
259
+ evolclaw aid delete --unrecoverable --yes`);
260
+ return;
261
+ }
262
+ const yes = args.includes('--yes');
263
+ const skipPki = args.includes('--skip-pki');
264
+ const orphan = args.includes('--orphan');
265
+ const noCert = args.includes('--no-cert');
266
+ const unrecoverable = args.includes('--unrecoverable');
267
+ const modes = [orphan, noCert, unrecoverable].filter(Boolean).length;
268
+ if (modes > 1) {
269
+ console.error('❌ --orphan / --no-cert / --unrecoverable 互斥,不能同时使用');
270
+ process.exit(1);
271
+ }
272
+ // 单个 aid 删除:保留原有行为
273
+ if (modes === 0) {
274
+ const aid = args[1];
275
+ if (!aid) {
276
+ console.error('用法: evolclaw aid delete <aid>\n evolclaw aid delete --orphan | --no-cert | --unrecoverable [--yes]\n evolclaw aid delete --help 查看完整用法');
277
+ process.exit(1);
278
+ }
279
+ const deleted = aidDelete(aid, { aunPath });
280
+ if (deleted) {
281
+ console.log(`✓ ${aid} 已删除`);
282
+ }
283
+ else {
284
+ console.error(`❌ 本地不存在: ${aid}`);
285
+ process.exit(1);
286
+ }
287
+ return;
288
+ }
289
+ // 批量模式:先选出候选
290
+ const { probePkiRecoverability } = await import('../aun/aid/index.js');
291
+ const candidates = [];
292
+ if (orphan) {
293
+ const aids = aidList(aunPath);
294
+ for (const a of aids) {
295
+ if (!a.hasPrivateKey)
296
+ candidates.push({ aid: a.aid, reason: 'no private key (external AID cache)' });
297
+ }
298
+ }
299
+ else if (noCert) {
300
+ const aids = aidList(aunPath);
301
+ for (const a of aids) {
302
+ if (!a.hasPrivateKey && !a.hasCert) {
303
+ const traits = [a.hasAgentMd ? 'agent.md' : null].filter(Boolean).join(', ');
304
+ candidates.push({ aid: a.aid, reason: `no private key, no cert${traits ? ` (only: ${traits})` : ''}` });
305
+ }
306
+ }
307
+ }
308
+ else {
309
+ // unrecoverable: 必须先做 sign+verify 实测
310
+ if (!formatJson)
311
+ console.log('扫描中: 本地签名/验签实测...');
312
+ const aids = await aidListVerified(aunPath);
313
+ const localBroken = aids.filter(a => a.hasPrivateKey && a.signVerified === false);
314
+ if (skipPki) {
315
+ for (const a of localBroken) {
316
+ candidates.push({ aid: a.aid, reason: `sign+verify failed (${a.signError ?? 'unknown'}) [--skip-pki: 未联网验证]` });
317
+ }
318
+ }
319
+ else {
320
+ if (!formatJson)
321
+ console.log(`扫描中: 对 ${localBroken.length} 个本地损坏 AID 做 PKI 探测...`);
322
+ for (const a of localBroken) {
323
+ const r = await probePkiRecoverability(a.aid, { aunPath });
324
+ if (r.kind === 'unrecoverable') {
325
+ candidates.push({ aid: a.aid, reason: `local broken; PKI: ${r.reason}`, pki: 'unrecoverable' });
326
+ }
327
+ else if (r.kind === 'no-server-record') {
328
+ candidates.push({ aid: a.aid, reason: `local broken; PKI: ${r.reason}`, pki: 'no-server-record' });
329
+ }
330
+ else {
331
+ // recoverable / no-key / unknown 一律保守不删
332
+ if (!formatJson)
333
+ console.log(` · 跳过 ${a.aid}: PKI=${r.kind}${('reason' in r) ? ' — ' + r.reason : ''}`);
334
+ }
335
+ }
336
+ }
337
+ }
338
+ if (formatJson) {
339
+ console.log(JSON.stringify({
340
+ mode: orphan ? 'orphan' : noCert ? 'no-cert' : 'unrecoverable',
341
+ dryRun: !yes,
342
+ skipPki: unrecoverable ? skipPki : undefined,
343
+ candidates,
344
+ }, null, 2));
345
+ if (yes) {
346
+ for (const c of candidates)
347
+ aidDelete(c.aid, { aunPath });
348
+ }
349
+ return;
350
+ }
351
+ if (candidates.length === 0) {
352
+ console.log(orphan ? '✓ 无孤儿 AID' : noCert ? '✓ 无无证书孤儿目录' : '✓ 无不可恢复 AID');
353
+ return;
354
+ }
355
+ console.log(`\n${yes ? '将删除' : '候选删除(dry-run)'}:${candidates.length} 个 AID`);
356
+ for (const c of candidates) {
357
+ console.log(` - ${c.aid}`);
358
+ console.log(` ${c.reason}`);
359
+ }
360
+ if (!yes) {
361
+ console.log('\n(dry-run,未真删除。加 --yes 执行真删。)');
362
+ return;
363
+ }
364
+ let ok = 0;
365
+ let fail = 0;
366
+ for (const c of candidates) {
367
+ const deleted = aidDelete(c.aid, { aunPath });
368
+ if (deleted) {
369
+ console.log(` ✓ 删除 ${c.aid}`);
370
+ ok++;
371
+ }
372
+ else {
373
+ console.log(` ✗ 失败 ${c.aid}(已不存在?)`);
374
+ fail++;
375
+ }
376
+ }
377
+ console.log(`\n完成:成功 ${ok},失败 ${fail}`);
378
+ return;
379
+ }
380
+ if (sub === 'lookup') {
381
+ if (wantsHelp(args)) {
382
+ console.log(`用法: evolclaw aid lookup <aid> [--format json]
383
+
384
+ 远程探测 AID:是否注册、所在网关、是否有 agent.md(不验签,仅获取)。`);
385
+ return;
386
+ }
387
+ const aid = args[1];
388
+ if (!aid) {
389
+ console.error('用法: evolclaw aid lookup <aid>');
390
+ process.exit(1);
391
+ }
392
+ if (!isValidAid(aid)) {
393
+ console.error(`❌ 无效 AID 格式: ${aid}`);
394
+ process.exit(1);
395
+ }
396
+ const result = await aidLookup(aid);
397
+ if (formatJson) {
398
+ console.log(JSON.stringify(result, null, 2));
399
+ return;
400
+ }
401
+ if (result.exists) {
402
+ console.log(`✓ ${aid} 已注册`);
403
+ if (result.gateway)
404
+ console.log(` 网关: ${result.gateway}`);
405
+ if (result.content) {
406
+ const hasSig = result.content.includes('AUN-SIGNATURE');
407
+ console.log(` 签名: ${hasSig ? '有(未验证,如需验证请用 evolclaw aid agentmd get ' + aid + ')' : '无'}`);
408
+ console.log('');
409
+ console.log(result.content);
410
+ }
411
+ }
412
+ else {
413
+ console.log(`✗ ${aid} 未注册`);
414
+ if (result.gateway)
415
+ console.log(` 网关: ${result.gateway}`);
416
+ if (result.error)
417
+ console.log(` 原因: ${result.error}`);
418
+ }
419
+ return;
420
+ }
421
+ if (sub === 'agentmd') {
422
+ const verb = args[1];
423
+ const aid = args[2];
424
+ if (!verb || isHelpFlag(verb) || wantsHelp(args)) {
425
+ console.log(`用法: evolclaw aid agentmd <put|get> <aid> [--format json]
426
+
427
+ put <aid> 读本地 agent.md → 用本地私钥签名 → 上传到 PKI
428
+ get <aid> 从 PKI 下载 agent.md → 验签 → 持久化到本地`);
429
+ return;
430
+ }
431
+ if (verb === 'put') {
432
+ if (!aid) {
433
+ console.error('用法: evolclaw aid agentmd put <aid>');
434
+ process.exit(1);
435
+ }
436
+ if (!isValidAid(aid)) {
437
+ console.error(`❌ 无效 AID 格式: ${aid}`);
438
+ process.exit(1);
439
+ }
440
+ const localPath = agentMdPath(aid);
441
+ if (!fs.existsSync(localPath)) {
442
+ console.error(`❌ 本地无 agent.md: ${aid}`);
443
+ process.exit(1);
444
+ }
445
+ const content = fs.readFileSync(localPath, 'utf-8');
446
+ await agentmdPut(content, { aid, aunPath });
447
+ console.log('✓ agent.md 已发布');
448
+ return;
449
+ }
450
+ if (verb === 'get') {
451
+ if (!aid) {
452
+ console.error('用法: evolclaw aid agentmd get <aid>');
453
+ process.exit(1);
454
+ }
455
+ if (!isValidAid(aid)) {
456
+ console.error(`❌ 无效 AID 格式: ${aid}`);
457
+ process.exit(1);
458
+ }
459
+ try {
460
+ const result = await agentmdGet(aid, { withVerification: true, aunPath });
461
+ if (!result.content || !result.content.trim()) {
462
+ console.log(`ℹ️ ${aid} 尚未设置 agent.md`);
463
+ return;
464
+ }
465
+ if (formatJson) {
466
+ console.log(JSON.stringify(result, null, 2));
467
+ }
468
+ else {
469
+ console.log(result.content);
470
+ const v = result.verification;
471
+ if (v.status === 'verified') {
472
+ console.error(`✓ 签名验证通过`);
473
+ }
474
+ else if (v.status === 'invalid') {
475
+ console.error(`⚠ 签名验证失败: ${v.reason ?? '未知原因'}`);
476
+ }
477
+ else {
478
+ console.error(`ℹ️ 未签名`);
479
+ }
480
+ }
481
+ }
482
+ catch (e) {
483
+ const msg = String(e.message || e);
484
+ if (msg.includes('not found') || msg.includes('404')) {
485
+ console.log(`ℹ️ ${aid} 尚未设置 agent.md`);
486
+ }
487
+ else {
488
+ console.error(`❌ 获取失败: ${msg.slice(0, 100)}`);
489
+ process.exit(1);
490
+ }
491
+ }
492
+ return;
493
+ }
494
+ console.error(`未知子命令: aid agentmd ${verb ?? ''}\n用法: evolclaw aid agentmd [put|get] <aid>`);
495
+ process.exit(1);
496
+ }
497
+ console.error(`未知子命令: ${sub}\n用法: evolclaw aid [list|show|new|delete|lookup|agentmd] <aid>`);
498
+ process.exit(1);
499
+ }
500
+ // ==================== RPC ====================
501
+ export async function cmdRpc(args) {
502
+ if (args.length === 0 || isHelpFlag(args[0])) {
503
+ console.log(`用法: evolclaw rpc --as <aid> --params <params>
504
+
505
+ 通用 AUN RPC 调用。
506
+
507
+ --params 自动判断输入形式:
508
+ 单行 JSON (以 { 开头) → 单次调用
509
+ 多行 JSONL → 逐行执行,失败即停
510
+ 文件路径 (文件存在) → 读取文件内容作为 JSONL
511
+
512
+ 每行 JSON 格式: {"method":"<namespace.method>","params":{...}}
513
+
514
+ Options:
515
+ --app <name> 指定应用 slot(独立消费通道)。仅对 message.pull / group.pull
516
+ 等消费类方法有意义——隔离 seq 游标与消息过滤;默认与 daemon 共享通道。
517
+
518
+ 示例:
519
+ evolclaw rpc --as alice.agentid.pub --params '{"method":"message.send","params":{"to":"bob.agentid.pub","payload":{"type":"text","text":"hello"}}}'
520
+ evolclaw rpc --as alice.agentid.pub --params calls.jsonl`);
521
+ return;
522
+ }
523
+ const asIdx = args.indexOf('--as');
524
+ const paramsIdx = args.indexOf('--params');
525
+ const aunPath = resolveAunPath(args);
526
+ const appSlot = getArgValue(args, '--app');
527
+ if (asIdx === -1 || asIdx + 1 >= args.length) {
528
+ console.error('❌ 缺少 --as <aid>');
529
+ process.exit(1);
530
+ }
531
+ if (paramsIdx === -1 || paramsIdx + 1 >= args.length) {
532
+ console.error('❌ 缺少 --params <params>');
533
+ process.exit(1);
534
+ }
535
+ const aid = args[asIdx + 1];
536
+ const paramsRaw = args[paramsIdx + 1];
537
+ const { isValidAid } = await import('../aun/aid/index.js');
538
+ if (!isValidAid(aid)) {
539
+ console.error(`❌ 无效 AID 格式: ${aid}`);
540
+ process.exit(1);
541
+ }
542
+ // Determine input: file, single JSON, or multi-line JSONL
543
+ let lines;
544
+ if (fs.existsSync(paramsRaw)) {
545
+ lines = fs.readFileSync(paramsRaw, 'utf-8').split('\n').filter(l => l.trim());
546
+ }
547
+ else if (paramsRaw.includes('\n')) {
548
+ lines = paramsRaw.split('\n').filter(l => l.trim());
549
+ }
550
+ else {
551
+ lines = [paramsRaw];
552
+ }
553
+ // Parse calls
554
+ const calls = [];
555
+ for (let i = 0; i < lines.length; i++) {
556
+ try {
557
+ const parsed = JSON.parse(lines[i]);
558
+ if (!parsed.method) {
559
+ console.error(`❌ 第 ${i + 1} 行缺少 "method" 字段`);
560
+ process.exit(1);
561
+ }
562
+ calls.push({ method: parsed.method, params: parsed.params ?? {} });
563
+ }
564
+ catch (e) {
565
+ console.error(`❌ 第 ${i + 1} 行 JSON 解析失败: ${e.message}`);
566
+ process.exit(1);
567
+ }
568
+ }
569
+ const { rpcCall, rpcBatch } = await import('../aun/rpc/index.js');
570
+ if (calls.length === 1) {
571
+ const result = await rpcCall(aid, calls[0].method, calls[0].params, { aunPath, slotId: appSlot });
572
+ console.log(JSON.stringify(result));
573
+ }
574
+ else {
575
+ const results = await rpcBatch(aid, calls, { aunPath, slotId: appSlot });
576
+ for (const r of results) {
577
+ console.log(JSON.stringify(r));
578
+ }
579
+ }
580
+ }
581
+ // ==================== Storage ====================
582
+ export async function cmdStorage(args) {
583
+ const sub = args[0];
584
+ const aunPath = resolveAunPath(args);
585
+ const formatJson = getArgValue(args, '--format') === 'json';
586
+ if (!sub || isHelpFlag(sub)) {
587
+ console.log(`用法: evolclaw storage <command> <aid> [options]
588
+
589
+ Commands:
590
+ upload <aid> <local-file> <remote-path> [--public] 上传文件(默认私有)
591
+ download <aid> <url> [local-path] 下载文件
592
+ ls <aid> [prefix] 列文件
593
+ rm <aid> <remote-path> 删文件
594
+ quota <aid> 查配额
595
+
596
+ <url> 格式: [https://]<owner-aid>/<path>
597
+
598
+ 示例:
599
+ evolclaw storage upload myaid.agentid.pub ./doc.txt notes/doc.txt
600
+ evolclaw storage upload myaid.agentid.pub ./pic.png images/pic.png --public
601
+ evolclaw storage download myaid.agentid.pub myaid.agentid.pub/notes/doc.txt ./doc.txt
602
+ evolclaw storage download myaid.agentid.pub bob.agentid.pub/public/file.pdf ./file.pdf
603
+ evolclaw storage ls myaid.agentid.pub notes/
604
+ evolclaw storage rm myaid.agentid.pub notes/doc.txt
605
+ evolclaw storage quota myaid.agentid.pub`);
606
+ return;
607
+ }
608
+ const aid = args[1];
609
+ if (!aid) {
610
+ console.error('❌ 缺少 <aid> 参数');
611
+ process.exit(1);
612
+ }
613
+ const { isValidAid } = await import('../aun/aid/index.js');
614
+ if (!isValidAid(aid)) {
615
+ console.error(`❌ 无效 AID 格式: ${aid}`);
616
+ process.exit(1);
617
+ }
618
+ const { storageUpload, storageDownload, storageLs, storageRm, storageQuota } = await import('../aun/storage/index.js');
619
+ if (sub === 'upload') {
620
+ const localFile = args[2];
621
+ const remotePath = args[3];
622
+ const isPublic = args.includes('--public');
623
+ if (!localFile || !remotePath) {
624
+ console.error('用法: evolclaw storage upload <aid> <local-file> <remote-path> [--public]');
625
+ process.exit(1);
626
+ }
627
+ if (!fs.existsSync(localFile)) {
628
+ console.error(`❌ 文件不存在: ${localFile}`);
629
+ process.exit(1);
630
+ }
631
+ const result = await storageUpload(aid, localFile, remotePath, { isPublic, aunPath });
632
+ if (!result.ok) {
633
+ if (formatJson) {
634
+ console.log(JSON.stringify({ ok: false, error: result.error }));
635
+ }
636
+ else {
637
+ console.error(`❌ 上传失败: ${result.error}`);
638
+ }
639
+ process.exit(1);
640
+ }
641
+ if (formatJson) {
642
+ console.log(JSON.stringify({ ok: true, objectKey: remotePath, isPublic, ref: `${aid}/${remotePath}`, publicUrl: result.publicUrl ?? null }));
643
+ }
644
+ else {
645
+ console.log(`✓ 已上传: ${remotePath}${isPublic ? ' (公开)' : ''}`);
646
+ if (result.publicUrl) {
647
+ console.log(` 🔗 访问: ${result.publicUrl}`);
648
+ }
649
+ else {
650
+ console.log(` 引用: ${aid}/${remotePath}`);
651
+ }
652
+ console.log(` 下载: evolclaw storage download ${aid} ${aid}/${remotePath}`);
653
+ }
654
+ return;
655
+ }
656
+ if (sub === 'download') {
657
+ const url = args[2];
658
+ const localPath = args[3];
659
+ if (!url) {
660
+ console.error('用法: evolclaw storage download <aid> <url> [local-path]');
661
+ process.exit(1);
662
+ }
663
+ const result = await storageDownload(aid, url, localPath, { aunPath });
664
+ if (!result.ok) {
665
+ if (formatJson) {
666
+ console.log(JSON.stringify({ ok: false, error: result.error }));
667
+ }
668
+ else {
669
+ console.error(`❌ 下载失败: ${result.error}`);
670
+ }
671
+ process.exit(1);
672
+ }
673
+ if (formatJson) {
674
+ console.log(JSON.stringify({ ok: true, localPath: result.localPath, size: result.size }));
675
+ }
676
+ else {
677
+ console.log(`✓ 已下载: ${result.localPath} (${result.size} bytes)`);
678
+ }
679
+ return;
680
+ }
681
+ if (sub === 'ls') {
682
+ const prefix = args[2] || '';
683
+ const result = await storageLs(aid, prefix, { aunPath });
684
+ if (!result.ok) {
685
+ console.error(`❌ 列文件失败: ${JSON.stringify(result.error)}`);
686
+ process.exit(1);
687
+ }
688
+ const objects = result.result?.objects || result.result || [];
689
+ if (Array.isArray(objects) && objects.length === 0) {
690
+ console.log('(空)');
691
+ }
692
+ else {
693
+ console.log(JSON.stringify(objects, null, 2));
694
+ }
695
+ return;
696
+ }
697
+ if (sub === 'rm') {
698
+ const remotePath = args[2];
699
+ if (!remotePath) {
700
+ console.error('用法: evolclaw storage rm <aid> <remote-path>');
701
+ process.exit(1);
702
+ }
703
+ const result = await storageRm(aid, remotePath, { aunPath });
704
+ if (!result.ok) {
705
+ if (formatJson) {
706
+ console.log(JSON.stringify({ ok: false, error: result.error }));
707
+ }
708
+ else {
709
+ console.error(`❌ 删除失败: ${JSON.stringify(result.error)}`);
710
+ }
711
+ process.exit(1);
712
+ }
713
+ if (formatJson) {
714
+ console.log(JSON.stringify({ ok: true, objectKey: remotePath }));
715
+ }
716
+ else {
717
+ console.log(`✓ 已删除: ${remotePath}`);
718
+ }
719
+ return;
720
+ }
721
+ if (sub === 'quota') {
722
+ const result = await storageQuota(aid, { aunPath });
723
+ if (!result.ok) {
724
+ console.error(`❌ 查询配额失败: ${JSON.stringify(result.error)}`);
725
+ process.exit(1);
726
+ }
727
+ console.log(JSON.stringify(result.result, null, 2));
728
+ return;
729
+ }
730
+ console.error(`未知子命令: ${sub}\n用法: evolclaw storage [upload|download|ls|rm|quota]`);
731
+ process.exit(1);
732
+ }
733
+ // ==================== Msg ====================
734
+ export async function cmdMsg(args) {
735
+ const sub = args[0];
736
+ const aunPath = resolveAunPath(args);
737
+ const formatJson = getArgValue(args, '--format') === 'json';
738
+ const appIdx = args.indexOf('--app');
739
+ const appSlot = appIdx >= 0 ? args[appIdx + 1] : undefined;
740
+ if (!sub || isHelpFlag(sub)) {
741
+ console.log(`用法: evolclaw msg <command> <from-aid> [args...] [options]
742
+
743
+ Commands:
744
+ send <from> <to> <text> 发送文本
745
+ send <from> <to> --file <path> [--as <type>] 发送文件(image|video|voice|file)
746
+ send <from> <to> --link <url> [--title T] 发送链接卡片
747
+ send <from> <to> --payload <json> 发送自定义 payload
748
+ pull <from> [--after-seq N] [--limit N] 拉取收件箱
749
+ ack <from> <seq> [--app <name>] 确认已读
750
+ recall <from> <message-id> [<message-id>...] 撤回消息
751
+ online <from> <target-aid> [<target-aid>...] 查询在线状态
752
+
753
+ Options:
754
+ --app <name> 指定应用 slot(独立消费通道,不影响 daemon)
755
+ --format json 输出 JSON 格式
756
+ --encrypt 启用端到端加密
757
+ --thread <id> 指定话题 ID(用于多话题路由)
758
+ --content-type <mime> 显式覆盖 MIME(仅 --file 模式)
759
+ --text <说明> 附件说明文字(仅 --file 模式)
760
+ --transcript <text> 语音转写(仅 --as voice)
761
+ -- end-of-options:其后所有参数按正文处理
762
+ (用于发送恰好等于某 flag 的文本,如 send a b -- --encrypt)
763
+
764
+ 示例:
765
+ evolclaw msg send alice.agentid.pub bob.agentid.pub "hello"
766
+ evolclaw msg send alice.agentid.pub bob.agentid.pub "讨论项目A" --thread "project-A"
767
+ evolclaw msg send alice.agentid.pub bob.agentid.pub --file ./pic.png
768
+ evolclaw msg send alice.agentid.pub bob.agentid.pub --file ./demo.mp4 --as video
769
+ evolclaw msg send alice.agentid.pub bob.agentid.pub --link https://example.com --title "AUN"
770
+ evolclaw msg pull alice.agentid.pub --app my-bot
771
+ evolclaw msg ack alice.agentid.pub 42 --app my-bot
772
+ evolclaw msg recall alice.agentid.pub msg-uuid-1 msg-uuid-2
773
+ evolclaw msg online alice.agentid.pub bob.agentid.pub carol.agentid.pub`);
774
+ return;
775
+ }
776
+ const from = args[1];
777
+ if (!from) {
778
+ console.error('❌ 缺少 <from-aid> 参数');
779
+ process.exit(1);
780
+ }
781
+ const { isValidAid } = await import('../aun/aid/index.js');
782
+ if (!isValidAid(from)) {
783
+ console.error(`❌ 无效 AID 格式: ${from}`);
784
+ process.exit(1);
785
+ }
786
+ const { msgSend, msgPull, msgAck, msgRecall, msgOnline } = await import('../aun/msg/index.js');
787
+ const commonOpts = { aunPath, slotId: appSlot };
788
+ if (sub === 'send') {
789
+ const to = args[2];
790
+ if (!to) {
791
+ console.error('用法: evolclaw msg send <from> <to> <text|--file ...|--link ...|--payload ...>');
792
+ process.exit(1);
793
+ }
794
+ if (!isValidAid(to)) {
795
+ console.error(`❌ 无效目标 AID: ${to}`);
796
+ process.exit(1);
797
+ }
798
+ const fileVal = getArgValue(args, '--file');
799
+ const linkVal = getArgValue(args, '--link');
800
+ const payloadVal = getArgValue(args, '--payload');
801
+ let body;
802
+ if (fileVal) {
803
+ body = {
804
+ mode: 'file',
805
+ filePath: fileVal,
806
+ as: getArgValue(args, '--as'),
807
+ contentType: getArgValue(args, '--content-type'),
808
+ text: getArgValue(args, '--text'),
809
+ transcript: getArgValue(args, '--transcript'),
810
+ };
811
+ }
812
+ else if (linkVal) {
813
+ body = {
814
+ mode: 'link',
815
+ url: linkVal,
816
+ title: getArgValue(args, '--title'),
817
+ description: getArgValue(args, '--description'),
818
+ };
819
+ }
820
+ else if (payloadVal) {
821
+ let parsed;
822
+ try {
823
+ parsed = JSON.parse(payloadVal);
824
+ }
825
+ catch (e) {
826
+ console.error(`❌ --payload 解析失败: ${e.message}`);
827
+ process.exit(1);
828
+ }
829
+ body = { mode: 'payload', payload: parsed };
830
+ }
831
+ else {
832
+ const text = collectPositional(args, 3).join(' ');
833
+ if (!text) {
834
+ console.error('❌ 缺少消息内容(文本或 --file/--link/--payload)');
835
+ process.exit(1);
836
+ }
837
+ body = { mode: 'text', text };
838
+ }
839
+ const encrypt = args.includes('--encrypt');
840
+ const thread = getArgValue(args, '--thread');
841
+ // 文件上传进度展示(非 JSON 输出时)。仅在大文件降级到 HTTP PUT 阶段会逐块更新。
842
+ let lastPctShown = -1;
843
+ const onUploadProgress = formatJson ? undefined : (info) => {
844
+ if (info.phase === 'inline')
845
+ return; // 内联阶段不分块,跳过
846
+ if (info.phase === 'http-put') {
847
+ const pct = info.total > 0 ? Math.floor((info.bytes / info.total) * 100) : 0;
848
+ if (pct === lastPctShown && info.bytes < info.total)
849
+ return;
850
+ lastPctShown = pct;
851
+ const mb = (n) => (n / 1024 / 1024).toFixed(2);
852
+ const eol = info.bytes >= info.total ? '\n' : '\r';
853
+ process.stderr.write(` ⏫ uploading: ${pct}% (${mb(info.bytes)}/${mb(info.total)} MB)${eol}`);
854
+ }
855
+ else if (info.phase === 'session-create') {
856
+ process.stderr.write(' ⏫ requesting upload session...\n');
857
+ }
858
+ else if (info.phase === 'session-complete') {
859
+ process.stderr.write(' ⏫ finalizing upload...\n');
860
+ }
861
+ };
862
+ const result = await msgSend({ from, to, body, encrypt, thread, onUploadProgress, ...commonOpts });
863
+ if (!result.ok) {
864
+ if (formatJson) {
865
+ console.log(JSON.stringify(result));
866
+ }
867
+ else {
868
+ console.error(`❌ 发送失败: ${result.error}`);
869
+ }
870
+ process.exit(1);
871
+ }
872
+ if (formatJson) {
873
+ console.log(JSON.stringify(result));
874
+ }
875
+ else {
876
+ console.log(`✓ 已发送 ${result.message_id ?? ''} seq=${result.seq ?? '-'} status=${result.status ?? '-'}`);
877
+ }
878
+ return;
879
+ }
880
+ if (sub === 'pull') {
881
+ if (!appSlot) {
882
+ console.warn('⚠ 警告: 未传 --app,当前与 daemon 共享 evolclaw 消费通道。pull 会看到/影响 daemon 的消息消费;如需独立消费请用 --app <name>');
883
+ }
884
+ const afterSeqStr = getArgValue(args, '--after-seq');
885
+ const limitStr = getArgValue(args, '--limit');
886
+ const afterSeq = afterSeqStr !== undefined ? Number(afterSeqStr) : undefined;
887
+ const limit = limitStr !== undefined ? Number(limitStr) : undefined;
888
+ if (afterSeq !== undefined && !Number.isFinite(afterSeq)) {
889
+ console.error(`❌ --after-seq 必须是数字: ${afterSeqStr}`);
890
+ process.exit(1);
891
+ }
892
+ if (limit !== undefined && !Number.isFinite(limit)) {
893
+ console.error(`❌ --limit 必须是数字: ${limitStr}`);
894
+ process.exit(1);
895
+ }
896
+ const result = await msgPull({ from, afterSeq, limit, ...commonOpts });
897
+ if (!result.ok) {
898
+ if (formatJson) {
899
+ console.log(JSON.stringify(result));
900
+ }
901
+ else {
902
+ console.error(`❌ 拉取失败: ${result.error}`);
903
+ }
904
+ process.exit(1);
905
+ }
906
+ if (formatJson) {
907
+ console.log(JSON.stringify(result));
908
+ }
909
+ else {
910
+ console.log(`✓ ${result.count} 条消息,latest_seq=${result.latest_seq}`);
911
+ for (const m of result.messages) {
912
+ const text = m.payload?.text ?? JSON.stringify(m.payload).slice(0, 80);
913
+ console.log(` [${m.seq}] ${m.from}: ${text}`);
914
+ }
915
+ if (result.ephemeral_dropped_count && result.ephemeral_dropped_count > 0) {
916
+ console.log(` (临时消息淘汰: ${result.ephemeral_dropped_count} 条)`);
917
+ }
918
+ }
919
+ return;
920
+ }
921
+ if (sub === 'ack') {
922
+ const seqStr = args[2];
923
+ if (!seqStr) {
924
+ console.error('用法: evolclaw msg ack <from> <seq> [--app <name>]');
925
+ process.exit(1);
926
+ }
927
+ const seq = Number(seqStr);
928
+ if (!Number.isFinite(seq)) {
929
+ console.error(`❌ seq 必须是数字: ${seqStr}`);
930
+ process.exit(1);
931
+ }
932
+ if (!appSlot) {
933
+ console.warn('⚠ 警告: 未传 --app,ack 将推进与 daemon 共享的 evolclaw 消费游标,可能影响 daemon 收消息;如需独立请用 --app <name>');
934
+ }
935
+ const result = await msgAck({ from, seq, ...commonOpts });
936
+ if (!result.ok) {
937
+ if (formatJson) {
938
+ console.log(JSON.stringify(result));
939
+ }
940
+ else {
941
+ console.error(`❌ ack 失败: ${result.error}`);
942
+ }
943
+ process.exit(1);
944
+ }
945
+ if (formatJson) {
946
+ console.log(JSON.stringify(result));
947
+ }
948
+ else {
949
+ console.log(`✓ ack_seq=${result.ack_seq}`);
950
+ }
951
+ return;
952
+ }
953
+ if (sub === 'recall') {
954
+ const messageIds = collectPositional(args, 2);
955
+ if (messageIds.length === 0) {
956
+ console.error('用法: evolclaw msg recall <from> <message-id> [<message-id>...]');
957
+ process.exit(1);
958
+ }
959
+ const result = await msgRecall({ from, messageIds, ...commonOpts });
960
+ if (!result.ok) {
961
+ if (formatJson) {
962
+ console.log(JSON.stringify(result));
963
+ }
964
+ else {
965
+ console.error(`❌ recall 失败: ${result.error}`);
966
+ }
967
+ process.exit(1);
968
+ }
969
+ if (formatJson) {
970
+ console.log(JSON.stringify(result));
971
+ }
972
+ else {
973
+ console.log(`✓ 受理 ${result.accepted},撤回 ${result.recalled}`);
974
+ if (result.errors && result.errors.length > 0) {
975
+ for (const e of result.errors) {
976
+ console.log(` 失败 ${e.message_id}: ${e.error}`);
977
+ }
978
+ }
979
+ }
980
+ return;
981
+ }
982
+ if (sub === 'online') {
983
+ const targets = collectPositional(args, 2);
984
+ if (targets.length === 0) {
985
+ console.error('用法: evolclaw msg online <from> <target-aid> [<target-aid>...]');
986
+ process.exit(1);
987
+ }
988
+ for (const t of targets) {
989
+ if (!isValidAid(t)) {
990
+ console.error(`❌ 无效 AID: ${t}`);
991
+ process.exit(1);
992
+ }
993
+ }
994
+ const result = await msgOnline({ from, targets, ...commonOpts });
995
+ if (!result.ok) {
996
+ if (formatJson) {
997
+ console.log(JSON.stringify(result));
998
+ }
999
+ else {
1000
+ console.error(`❌ 查询失败: ${result.error}`);
1001
+ }
1002
+ process.exit(1);
1003
+ }
1004
+ if (formatJson) {
1005
+ console.log(JSON.stringify(result));
1006
+ }
1007
+ else {
1008
+ for (const [aid, online] of Object.entries(result.online)) {
1009
+ console.log(` ${online ? '🟢' : '⚫'} ${aid}`);
1010
+ }
1011
+ }
1012
+ return;
1013
+ }
1014
+ console.error(`未知子命令: ${sub}\n用法: evolclaw msg [send|pull|ack|recall|online]`);
1015
+ process.exit(1);
1016
+ }
1017
+ // ==================== Group ====================
1018
+ export async function cmdGroup(args) {
1019
+ const sub = args[0];
1020
+ const aunPath = resolveAunPath(args);
1021
+ const formatJson = getArgValue(args, '--format') === 'json';
1022
+ const appIdx = args.indexOf('--app');
1023
+ const appSlot = appIdx >= 0 ? args[appIdx + 1] : undefined;
1024
+ if (!sub || isHelpFlag(sub)) {
1025
+ console.log(`用法: evolclaw group <command> <from-aid> [args...] [options]
1026
+
1027
+ 消息:
1028
+ send <from> <group-id> <text> 发送群文本
1029
+ send <from> <group-id> --file <path> [--as <type>] 发送群文件
1030
+ send <from> <group-id> --payload <json> 发送自定义 payload
1031
+ pull <from> <group-id> [--after-seq N] [--limit N] 拉取群消息
1032
+ ack <from> <group-id> <seq> [--app <name>] 确认已读
1033
+
1034
+ 群管理:
1035
+ create <from> <name> [--visibility public|private] [--description D] [--join-mode M] 创建群
1036
+ list <from> [--size N] 列出我加入的群
1037
+ info <from> <group-id> 查看群详情
1038
+ update <from> <group-id> [--name N] [--description D] 修改群信息
1039
+ dissolve <from> <group-id> 解散群
1040
+
1041
+ 成员:
1042
+ join <from> <group-id> [--message M] [--answer A] 申请加入
1043
+ leave <from> <group-id> 退出群
1044
+ invite <from> <group-id> <member-aid> [<member-aid>...] 邀请成员
1045
+ kick <from> <group-id> <member-aid> 踢出成员
1046
+ members <from> <group-id> [--page N] [--size N] 列出群成员
1047
+ online <from> <group-id> 查看在线成员
1048
+
1049
+ Options:
1050
+ --app <name> 指定应用 slot(独立消费通道,不影响 daemon)
1051
+ --format json 输出 JSON 格式
1052
+ --encrypt 启用端到端加密(仅 send)
1053
+ --mention <aid> 发送时 @ 某个成员(可多次,或用逗号分隔多个 aid)
1054
+ --mention-all 发送时 @ 所有人
1055
+ -- end-of-options:其后所有参数按正文处理
1056
+ (用于发送恰好等于某 flag 的文本,如 send a g -- --encrypt)
1057
+
1058
+ 示例:
1059
+ evolclaw group create alice.agentid.pub "Dev Team" --visibility private
1060
+ evolclaw group send alice.agentid.pub g-dev.agentid.pub "hello team"
1061
+ evolclaw group send alice.agentid.pub g-dev.agentid.pub "@bob 看下 PR" --mention bob.agentid.pub
1062
+ evolclaw group send alice.agentid.pub g-dev.agentid.pub --file ./arch.png
1063
+ evolclaw group invite alice.agentid.pub g-dev.agentid.pub bob.agentid.pub carol.agentid.pub
1064
+ evolclaw group members alice.agentid.pub g-dev.agentid.pub`);
1065
+ return;
1066
+ }
1067
+ const from = args[1];
1068
+ if (!from) {
1069
+ console.error('❌ 缺少 <from-aid> 参数');
1070
+ process.exit(1);
1071
+ }
1072
+ const { isValidAid } = await import('../aun/aid/index.js');
1073
+ if (!isValidAid(from)) {
1074
+ console.error(`❌ 无效 AID 格式: ${from}`);
1075
+ process.exit(1);
1076
+ }
1077
+ const { groupSend, groupPull, groupAck, groupCreate, groupInfo, groupList, groupUpdate, groupDissolve, groupJoin, groupLeave, groupInvite, groupKick, groupMembers, groupOnline, } = await import('../aun/msg/index.js');
1078
+ const commonOpts = { aunPath, slotId: appSlot };
1079
+ // 通用 group_id 提取(第三参数)
1080
+ const requireGroupId = () => {
1081
+ const gid = args[2];
1082
+ if (!gid) {
1083
+ console.error(`❌ 缺少 <group-id> 参数`);
1084
+ process.exit(1);
1085
+ }
1086
+ return gid;
1087
+ };
1088
+ // 收集 --mention(可多次;每次的值支持逗号分隔多个 aid)
1089
+ const collectMentions = () => {
1090
+ const mentions = [];
1091
+ for (let i = 0; i < args.length; i++) {
1092
+ if (args[i] !== '--mention')
1093
+ continue;
1094
+ const val = args[i + 1];
1095
+ if (val === undefined || val.startsWith('--')) {
1096
+ console.error(`❌ --mention 后面缺少 <aid>`);
1097
+ process.exit(1);
1098
+ }
1099
+ for (const aid of val.split(',').map(s => s.trim()).filter(Boolean)) {
1100
+ if (!isValidAid(aid)) {
1101
+ console.error(`❌ --mention 的 aid 无效: ${aid}`);
1102
+ process.exit(1);
1103
+ }
1104
+ mentions.push({ aid });
1105
+ }
1106
+ }
1107
+ if (args.includes('--mention-all')) {
1108
+ mentions.push({ scope: 'all' });
1109
+ }
1110
+ return mentions;
1111
+ };
1112
+ // 输出辅助
1113
+ const outputResult = (result, successHuman) => {
1114
+ if (!result.ok) {
1115
+ if (formatJson) {
1116
+ console.log(JSON.stringify(result));
1117
+ }
1118
+ else {
1119
+ console.error(`❌ ${result.error}`);
1120
+ }
1121
+ process.exit(1);
1122
+ }
1123
+ if (formatJson) {
1124
+ console.log(JSON.stringify(result));
1125
+ }
1126
+ else {
1127
+ successHuman();
1128
+ }
1129
+ };
1130
+ // ---- 消息 ----
1131
+ if (sub === 'send') {
1132
+ const groupId = requireGroupId();
1133
+ const fileVal = getArgValue(args, '--file');
1134
+ const payloadVal = getArgValue(args, '--payload');
1135
+ let body;
1136
+ if (fileVal) {
1137
+ body = {
1138
+ mode: 'file',
1139
+ filePath: fileVal,
1140
+ as: getArgValue(args, '--as'),
1141
+ contentType: getArgValue(args, '--content-type'),
1142
+ text: getArgValue(args, '--text'),
1143
+ transcript: getArgValue(args, '--transcript'),
1144
+ };
1145
+ }
1146
+ else if (payloadVal) {
1147
+ let parsed;
1148
+ try {
1149
+ parsed = JSON.parse(payloadVal);
1150
+ }
1151
+ catch (e) {
1152
+ console.error(`❌ --payload 解析失败: ${e.message}`);
1153
+ process.exit(1);
1154
+ }
1155
+ body = { mode: 'payload', payload: parsed };
1156
+ }
1157
+ else {
1158
+ const text = collectPositional(args, 3).join(' ');
1159
+ if (!text) {
1160
+ console.error('❌ 缺少消息内容(文本或 --file/--payload)');
1161
+ process.exit(1);
1162
+ }
1163
+ body = { mode: 'text', text };
1164
+ }
1165
+ const mentions = collectMentions();
1166
+ const encryptGroup = args.includes('--encrypt');
1167
+ const result = await groupSend({ from, groupId, body, mentions: mentions.length ? mentions : undefined, encrypt: encryptGroup, ...commonOpts });
1168
+ outputResult(result, () => {
1169
+ const r = result;
1170
+ console.log(`✓ 已发送 message_id=${r.message?.message_id ?? '-'} seq=${r.message?.seq ?? '-'}`);
1171
+ });
1172
+ return;
1173
+ }
1174
+ if (sub === 'pull') {
1175
+ const groupId = requireGroupId();
1176
+ if (!appSlot) {
1177
+ console.warn('⚠ 警告: 未传 --app,当前与 daemon 共享 evolclaw 消费通道。pull 会看到/影响 daemon 的消息消费;如需独立消费请用 --app <name>');
1178
+ }
1179
+ const afterSeqStr = getArgValue(args, '--after-seq');
1180
+ const limitStr = getArgValue(args, '--limit');
1181
+ const afterSeq = afterSeqStr !== undefined ? Number(afterSeqStr) : undefined;
1182
+ const limit = limitStr !== undefined ? Number(limitStr) : undefined;
1183
+ const result = await groupPull({ from, groupId, afterSeq, limit, ...commonOpts });
1184
+ outputResult(result, () => {
1185
+ const r = result;
1186
+ console.log(`✓ ${r.messages.length} 条消息,latest_seq=${r.latest_message_seq}${r.has_more ? '(还有更多)' : ''}`);
1187
+ for (const m of r.messages) {
1188
+ const text = m.payload?.text ?? JSON.stringify(m.payload).slice(0, 80);
1189
+ console.log(` [${m.seq}] ${m.sender_aid}: ${text}`);
1190
+ }
1191
+ });
1192
+ return;
1193
+ }
1194
+ if (sub === 'ack') {
1195
+ const groupId = requireGroupId();
1196
+ const seqStr = args[3];
1197
+ if (!seqStr) {
1198
+ console.error('用法: evolclaw group ack <from> <group-id> <seq> [--app <name>]');
1199
+ process.exit(1);
1200
+ }
1201
+ const seq = Number(seqStr);
1202
+ if (!Number.isFinite(seq)) {
1203
+ console.error(`❌ seq 必须是数字: ${seqStr}`);
1204
+ process.exit(1);
1205
+ }
1206
+ if (!appSlot) {
1207
+ console.warn('⚠ 警告: 未传 --app,ack 将推进与 daemon 共享的 evolclaw 消费游标,可能影响 daemon 收消息;如需独立请用 --app <name>');
1208
+ }
1209
+ const result = await groupAck({ from, groupId, seq, ...commonOpts });
1210
+ outputResult(result, () => {
1211
+ const r = result;
1212
+ console.log(`✓ ack_seq=${r.ack_seq}`);
1213
+ });
1214
+ return;
1215
+ }
1216
+ // ---- 群管理 ----
1217
+ if (sub === 'create') {
1218
+ const name = args[2];
1219
+ if (!name) {
1220
+ console.error('用法: evolclaw group create <from> <name> [--visibility ...] [--description ...]');
1221
+ process.exit(1);
1222
+ }
1223
+ const visibility = getArgValue(args, '--visibility');
1224
+ if (visibility && visibility !== 'public' && visibility !== 'private') {
1225
+ console.error(`❌ --visibility 必须是 public 或 private`);
1226
+ process.exit(1);
1227
+ }
1228
+ const result = await groupCreate({
1229
+ from,
1230
+ name,
1231
+ visibility,
1232
+ description: getArgValue(args, '--description'),
1233
+ joinMode: getArgValue(args, '--join-mode'),
1234
+ groupId: getArgValue(args, '--group-id'),
1235
+ ...commonOpts,
1236
+ });
1237
+ outputResult(result, () => {
1238
+ const r = result;
1239
+ console.log(`✓ 已创建群 ${r.group?.group_id}`);
1240
+ console.log(` 名称: ${r.group?.name}`);
1241
+ console.log(` 可见性: ${r.group?.visibility}`);
1242
+ });
1243
+ return;
1244
+ }
1245
+ if (sub === 'list') {
1246
+ const sizeStr = getArgValue(args, '--size');
1247
+ const size = sizeStr !== undefined ? Number(sizeStr) : undefined;
1248
+ const result = await groupList({ from, size, ...commonOpts });
1249
+ outputResult(result, () => {
1250
+ const r = result;
1251
+ if (r.items.length === 0) {
1252
+ console.log('(没有加入任何群)');
1253
+ return;
1254
+ }
1255
+ console.log(`共 ${r.total} 个群:`);
1256
+ for (const g of r.items) {
1257
+ console.log(` ${g.group_id} ${g.name} (${g.member_count ?? '?'} 人)`);
1258
+ }
1259
+ });
1260
+ return;
1261
+ }
1262
+ if (sub === 'info') {
1263
+ const groupId = requireGroupId();
1264
+ const result = await groupInfo({ from, groupId, ...commonOpts });
1265
+ outputResult(result, () => {
1266
+ const g = result.group;
1267
+ console.log(`Group: ${g.group_id}`);
1268
+ console.log(` 名称: ${g.name}`);
1269
+ console.log(` 群主: ${g.owner_aid}`);
1270
+ console.log(` 可见性: ${g.visibility ?? '-'}`);
1271
+ console.log(` 状态: ${g.status ?? '-'}`);
1272
+ console.log(` 成员数: ${g.member_count ?? '-'}`);
1273
+ console.log(` 最新 seq: ${g.message_seq ?? '-'}`);
1274
+ if (g.description)
1275
+ console.log(` 描述: ${g.description}`);
1276
+ });
1277
+ return;
1278
+ }
1279
+ if (sub === 'update') {
1280
+ const groupId = requireGroupId();
1281
+ const name = getArgValue(args, '--name');
1282
+ const description = getArgValue(args, '--description');
1283
+ if (name === undefined && description === undefined) {
1284
+ console.error('❌ 至少需要 --name 或 --description 之一');
1285
+ process.exit(1);
1286
+ }
1287
+ const result = await groupUpdate({ from, groupId, name, description, ...commonOpts });
1288
+ outputResult(result, () => {
1289
+ const g = result.group;
1290
+ console.log(`✓ 已更新 ${g.group_id}`);
1291
+ console.log(` 名称: ${g.name}`);
1292
+ });
1293
+ return;
1294
+ }
1295
+ if (sub === 'dissolve') {
1296
+ const groupId = requireGroupId();
1297
+ const result = await groupDissolve({ from, groupId, ...commonOpts });
1298
+ outputResult(result, () => {
1299
+ const r = result;
1300
+ console.log(`✓ 已解散 ${r.group_id} (${r.status})`);
1301
+ });
1302
+ return;
1303
+ }
1304
+ // ---- 成员 ----
1305
+ if (sub === 'join') {
1306
+ const groupId = requireGroupId();
1307
+ const result = await groupJoin({
1308
+ from, groupId,
1309
+ message: getArgValue(args, '--message'),
1310
+ answer: getArgValue(args, '--answer'),
1311
+ ...commonOpts,
1312
+ });
1313
+ outputResult(result, () => {
1314
+ console.log(`✓ 已提交入群申请`);
1315
+ });
1316
+ return;
1317
+ }
1318
+ if (sub === 'leave') {
1319
+ const groupId = requireGroupId();
1320
+ const result = await groupLeave({ from, groupId, ...commonOpts });
1321
+ outputResult(result, () => {
1322
+ console.log(`✓ 已退出 ${groupId}`);
1323
+ });
1324
+ return;
1325
+ }
1326
+ if (sub === 'invite') {
1327
+ const groupId = requireGroupId();
1328
+ const members = collectPositional(args, 3);
1329
+ if (members.length === 0) {
1330
+ console.error('用法: evolclaw group invite <from> <group-id> <member-aid> [<member-aid>...]');
1331
+ process.exit(1);
1332
+ }
1333
+ for (const m of members) {
1334
+ if (!isValidAid(m)) {
1335
+ console.error(`❌ 无效 AID: ${m}`);
1336
+ process.exit(1);
1337
+ }
1338
+ }
1339
+ const result = await groupInvite({ from, groupId, members, ...commonOpts });
1340
+ outputResult(result, () => {
1341
+ const r = result;
1342
+ console.log(`✓ 成功 ${r.added.length},失败 ${r.failed.length}`);
1343
+ for (const a of r.added)
1344
+ console.log(` + ${a}`);
1345
+ for (const f of r.failed)
1346
+ console.log(` ✗ ${f.aid}: ${f.error}`);
1347
+ });
1348
+ return;
1349
+ }
1350
+ if (sub === 'kick') {
1351
+ const groupId = requireGroupId();
1352
+ const memberAid = args[3];
1353
+ if (!memberAid) {
1354
+ console.error('用法: evolclaw group kick <from> <group-id> <member-aid>');
1355
+ process.exit(1);
1356
+ }
1357
+ const result = await groupKick({ from, groupId, memberAid, ...commonOpts });
1358
+ outputResult(result, () => {
1359
+ console.log(`✓ 已踢出 ${memberAid}`);
1360
+ });
1361
+ return;
1362
+ }
1363
+ if (sub === 'members') {
1364
+ const groupId = requireGroupId();
1365
+ const pageStr = getArgValue(args, '--page');
1366
+ const sizeStr = getArgValue(args, '--size');
1367
+ const result = await groupMembers({
1368
+ from, groupId,
1369
+ page: pageStr !== undefined ? Number(pageStr) : undefined,
1370
+ size: sizeStr !== undefined ? Number(sizeStr) : undefined,
1371
+ ...commonOpts,
1372
+ });
1373
+ outputResult(result, () => {
1374
+ const r = result;
1375
+ console.log(`共 ${r.total} 名成员(第 ${r.page} 页):`);
1376
+ for (const m of r.members) {
1377
+ console.log(` [${m.role}] ${m.aid}`);
1378
+ }
1379
+ });
1380
+ return;
1381
+ }
1382
+ if (sub === 'online') {
1383
+ const groupId = requireGroupId();
1384
+ const result = await groupOnline({ from, groupId, ...commonOpts });
1385
+ outputResult(result, () => {
1386
+ const r = result;
1387
+ console.log(`在线 ${r.online_count}/${r.total}:`);
1388
+ for (const m of r.members) {
1389
+ console.log(` 🟢 ${m.aid}`);
1390
+ }
1391
+ });
1392
+ return;
1393
+ }
1394
+ console.error(`未知子命令: ${sub}\n用法: evolclaw group [send|pull|ack|create|list|info|update|dissolve|join|leave|invite|kick|members|online]`);
1395
+ process.exit(1);
1396
+ }
1397
+ // ==================== Main ====================
1398
+ /**
1399
+ * 收集位置参数(从 startIdx 开始)。
1400
+ *
1401
+ * flag 判定采用**精确匹配已知 flag 集合**,而非 `startsWith('--')`——
1402
+ * 这样"正文恰好以 -- 开头"(如消息文本 `--file 坏了`)不会被误当 flag 吞掉。
1403
+ * 仅当 token 精确等于某个已知 flag 时才按 flag 处理:
1404
+ * - VALUE_FLAGS:消耗自身 + 下一个 arg(flag 的值)
1405
+ * - BOOLEAN_FLAGS:仅消耗自身
1406
+ * 其余以 -- 开头但不在集合中的 token,一律视为正文。
1407
+ *
1408
+ * 另支持 POSIX `--` end-of-options 分隔符:遇到单独的 `--` 后,
1409
+ * 其后所有 token 无条件按正文处理(用于发送精确等于某 flag 的文本,如 `-- --encrypt`)。
1410
+ */
1411
+ const VALUE_FLAGS = new Set([
1412
+ '--format', '--app', '--after-seq', '--limit', '--file', '--link',
1413
+ '--payload', '--title', '--description', '--text', '--transcript',
1414
+ '--as', '--content-type', '--mention', '--visibility', '--join-mode',
1415
+ '--group-id', '--name', '--message', '--answer', '--page', '--size',
1416
+ '--aun-path', '--thread',
1417
+ ]);
1418
+ const BOOLEAN_FLAGS = new Set([
1419
+ '--encrypt', '--mention-all',
1420
+ ]);
1421
+ function collectPositional(args, startIdx) {
1422
+ const out = [];
1423
+ let endOfFlags = false;
1424
+ for (let i = startIdx; i < args.length; i++) {
1425
+ const a = args[i];
1426
+ if (endOfFlags) {
1427
+ out.push(a);
1428
+ continue;
1429
+ }
1430
+ if (a === '--') {
1431
+ endOfFlags = true;
1432
+ continue;
1433
+ }
1434
+ if (VALUE_FLAGS.has(a)) {
1435
+ i++;
1436
+ continue;
1437
+ } // 精确匹配取值 flag:跳过其值
1438
+ if (BOOLEAN_FLAGS.has(a)) {
1439
+ continue;
1440
+ } // 精确匹配开关 flag:仅跳过自身
1441
+ out.push(a); // 其余(含以 -- 开头的未知 token)= 正文
1442
+ }
1443
+ return out;
1444
+ }