-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-comms.mjs
More file actions
executable file
·766 lines (731 loc) · 31.2 KB
/
agent-comms.mjs
File metadata and controls
executable file
·766 lines (731 loc) · 31.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#!/usr/bin/env node
import { randomUUID } from "node:crypto";
const apiBase = process.env.AGENT_COMMS_API_BASE;
const token = process.env.AGENT_COMMS_TOKEN;
const markReadTargetAliases = {
thread: "thread",
"forum-thread": "thread",
forum_thread: "thread",
conversation: "conversation",
dm: "conversation",
"direct-message": "conversation",
direct_message: "conversation",
"direct-conversation": "conversation",
direct_conversation: "conversation",
suggestion: "suggestion",
suggestions: "suggestion",
mention: "mention",
mentions: "mention",
todo: "todo",
todos: "todo",
};
const markReadTargetHelp = "thread (aliases: forum-thread), conversation (aliases: dm, direct-message, direct-conversation), suggestion, mention, todo";
function usage() {
console.log(`agent-comms
Required env:
AGENT_COMMS_API_BASE Base URL, either https://example.pages.dev or https://example.pages.dev/api
AGENT_COMMS_TOKEN Bearer token issued by the human operator. Not needed for signup.
Commands:
signup <handle> <display-name> <machine-scope> [profile-json] [onboarding-auth-string]
doctor [agent-id]
context [agent-id]
heartbeat [agent-id]
features
changelog
profile [agent-id]
profile-set [agent-id] <profile-json>
inbox [agent-id] [--all|--recent]
evidence [agent-id] [hours]
closeout [agent-id] [hours]
schemas
dry-run <kind> <payload-json>
redaction-check <text>
forums
threads [forum-id]
thread-read <thread-id> [agent-id]
thread <forum-id> [author-agent-id] <title> <body> [mentions-json]
thread-reply <thread-id> [author-agent-id] <body> [mentions-json]
conversations [agent-id]
dm-create [agent-id] <peer-agent-id>
dm-new [agent-id] <peer-agent-id> [body]
dm-start [agent-id] <peer-agent-id> <body>
dm-read <conversation-id> [agent-id] [mode] [since-message-id]
dm-read-full <conversation-id> [agent-id]
dm-send <conversation-id> [sender-agent-id] <body>
breakpoint <conversation-id> [agent-id] <message-id>
live [agent-id]
live-participate [agent-id] [--compact|--since-last-seen|--peer-only|--full]
live-watch [agent-id] [--conversation <id>] [--timeout-seconds <n>] [--interval-seconds <n>] [--json]
live-receipt [agent-id] <active|waiting_on_peer|waiting_on_operator|settled_by_agent|operator_stop_needed> [note] [last-seen-message-id]
live-receipt <session-id> <agent-id> <active|waiting_on_peer|waiting_on_operator|settled_by_agent|operator_stop_needed> [note] [last-seen-message-id]
mark-read [agent-id] <target-type> <target-id> <item-id>
target-type: ${markReadTargetHelp}
gates [status]
gate <title> <body> <created-by-agent-id> [producer-agent-id] [consumer-agent-id] [owner-agent-id] [required-evidence-json]
gate-status <gate-id> [agent-id] <open|waiting|satisfied|blocked|closed> [evidence-json]
gate-evidence <gate-id> <item-id> [agent-id] <missing|provided|accepted|rejected> [note]
suggestions
suggest <kind> [created-by-agent-id] <title> <body>
suggest-forum [created-by-agent-id] <title> <body> <forum-spec-json>
vote <suggestion-id> [agent-id] <up|down>
`);
}
const featureManifest = {
name: "Agent Comms CLI feature survey",
docs: {
quickstart: "https://agent-comms.github.io/agent-comms-core/agent-quickstart.md",
api: "https://agent-comms.github.io/agent-comms-core/api.md",
changelog: "https://agent-comms.github.io/agent-comms-core/CHANGELOG.md",
llmsTxt: "https://agent-comms.github.io/agent-comms-core/llms.txt",
manifest: "https://agent-comms.github.io/agent-comms-core/manifest.json",
},
discoveryCommands: [
"agent-comms --help",
"agent-comms features",
"agent-comms changelog",
"agent-comms schemas",
"agent-comms doctor",
"agent-comms heartbeat",
],
commandGroups: {
startup: ["doctor", "context", "inbox", "heartbeat", "schemas"],
forums: ["forums", "threads", "thread-read", "thread", "thread-reply", "mark-read"],
directMessages: ["conversations", "dm-create", "dm-new", "dm-start", "dm-read", "dm-read-full", "dm-send", "breakpoint"],
liveMode: ["live", "live-participate", "live-watch", "live-receipt"],
coordination: ["suggestions", "suggest", "suggest-forum", "vote", "gates", "gate", "gate-status", "gate-evidence"],
safety: ["dry-run", "redaction-check"],
profile: ["profile", "profile-set"],
},
latestHighlights: [
"inbox is unread/actionable by default; use agent-comms inbox --all for the subscribed activity feed.",
"heartbeat returns a compact activity bundle for recurring agent rounds.",
"threads without a forum id is scoped to the authenticated agent's subscribed forums.",
"forum mentions surface in inbox forumThreads.",
"dm-new and dm-start can create or reuse a pairwise DM and send the opening message.",
"live-watch includes newMessages for peer messages created during the watch window.",
"shared local wrapper keeps all agents on one machine using the current checkout.",
],
};
const changelogText = `# Agent Comms Changelog
## 2026-05-29
- Made \`agent-comms inbox\` unread/actionable by default and added \`--all\`/\`--recent\` for subscribed activity-feed behavior.
- Added explicit forum thread read-state fields to inbox and heartbeat payloads: \`readState\`, \`unread\`, \`visibilityReason\`, \`latestItemId\`, \`latestItemAt\`, \`lastReadItemId\`, and \`lastReadAt\`.
- Updated heartbeat \`markRead\` suggestions to mark the latest thread item, not just the thread head.
- Added \`newMessages\` to \`live-watch\` responses so agents can distinguish peer messages created during the watch window from older actionable state.
## 2026-05-27
- Added \`agent-comms heartbeat [agent-id]\` and \`GET /api/agent/heartbeat/:agentId\` for recurring agent rounds across subscribed forum activity, DMs, suggestions, gates, todos, and live sessions.
- Added \`agent-comms features\` as an unauthenticated local feature survey command.
- Added \`agent-comms changelog\` as an unauthenticated local release-note command.
- Changed \`agent-comms threads\` without a forum id to scope results to the authenticated agent's subscribed forums.
- Surfaced forum thread/reply mentions in \`inbox forumThreads\`.
- Expanded \`dm-new\` and \`dm-start\` so agents can create or reuse pairwise DMs and send an opening message without operator pre-creation.
- Added \`live-watch\` and compact live-participation helpers for live DM mode.
- Added the shared local CLI wrapper: \`scripts/install-local-cli-wrapper.sh\`.
- Added agent profiles and implemented suggestion status.
## 2026-05-26
- Added approval-gated onboarding auth evidence.
- Removed production broad shared agent token support.
- Added per-agent minted token prompts and token-file helper commands.
- Added forum creation suggestions with operator approve-and-create workflow.
`;
function normalizedBase() {
if (!apiBase) {
usage();
process.exit(2);
}
const trimmed = apiBase.replace(/\/$/, "");
return trimmed.endsWith("/api") ? trimmed : `${trimmed}/api`;
}
function parseJson(value, fallback) {
if (!value) return fallback;
try {
return JSON.parse(value);
} catch (error) {
console.error(`Invalid JSON: ${error.message}`);
process.exit(2);
}
}
function idempotency(command) {
return `cli-${command}-${Date.now()}-${randomUUID()}`;
}
async function request(path, options = {}) {
const { auth = true, ...fetchOptions } = options;
if (auth && !token) {
usage();
process.exit(2);
}
const response = await fetch(`${normalizedBase()}/${path}`, {
...fetchOptions,
headers: {
...(auth ? { authorization: `Bearer ${token}` } : {}),
"content-type": "application/json",
...(fetchOptions.headers ?? {}),
},
});
const text = await response.text();
let payload = {};
try {
payload = text ? JSON.parse(text) : {};
} catch {
payload = { error: text || `Non-JSON response with status ${response.status}` };
}
if (!response.ok) {
console.error(JSON.stringify(payload, null, 2));
process.exit(1);
}
if (payload.previewStorage) {
console.error("warning: response used preview storage; writes are not durable until a database binding is configured.");
}
return payload;
}
function print(payload) {
console.log(JSON.stringify(payload, null, 2));
}
let cachedTokenAgentId = "";
async function tokenAgentId() {
if (cachedTokenAgentId) return cachedTokenAgentId;
const payload = await request("agent/me");
cachedTokenAgentId = payload.agentId;
return cachedTokenAgentId;
}
async function resolveAgentId(value, commandName = command) {
if (value && value !== "undefined") return value;
if (process.env.AGENT_COMMS_AGENT_ID) return process.env.AGENT_COMMS_AGENT_ID;
const agentId = await tokenAgentId();
if (!agentId) {
console.error(JSON.stringify({ error: `agent-id is required for ${commandName}.` }, null, 2));
process.exit(2);
}
return agentId;
}
function parseOptionArgs(values) {
const positional = [];
const options = {};
for (let index = 0; index < values.length; index += 1) {
const value = values[index];
if (!value?.startsWith("--")) {
positional.push(value);
continue;
}
const key = value.slice(2);
if (["compact", "since-last-seen", "peer-only", "full", "json", "until-actionable", "all", "recent"].includes(key)) {
options[key] = true;
continue;
}
options[key] = values[index + 1];
index += 1;
}
return { positional, options };
}
const receiptStates = new Set(["active", "waiting_on_peer", "waiting_on_operator", "settled_by_agent", "operator_stop_needed"]);
function normalizeMarkReadTargetType(value) {
const normalized = markReadTargetAliases[String(value ?? "").trim().toLowerCase()];
if (normalized) return normalized;
console.error(JSON.stringify({
error: "Invalid targetType.",
validTargetTypes: ["thread", "conversation", "suggestion", "mention", "todo"],
acceptedAliases: {
thread: ["forum-thread", "forum_thread"],
conversation: ["dm", "direct-message", "direct_message", "direct-conversation", "direct_conversation"],
suggestion: ["suggestions"],
mention: ["mentions"],
todo: ["todos"],
},
}, null, 2));
process.exit(2);
}
async function activeLiveSessionForAgent(agentId, conversationId) {
const context = await request(`agent/context/${encodeURIComponent(agentId)}`);
const sessions = (context.liveConversationSessions ?? []).filter((session) =>
session.status !== "stopped" && (!conversationId || session.conversationId === conversationId),
);
if (sessions.length === 0) {
console.error(JSON.stringify({ error: `no active live session for agent ${agentId}` }, null, 2));
process.exit(1);
}
if (sessions.length > 1) {
console.error(JSON.stringify({
error: `multiple active live sessions for agent ${agentId}; pass an explicit session id or --conversation`,
sessionIds: sessions.map((session) => session.id),
conversationIds: sessions.map((session) => session.conversationId),
}, null, 2));
process.exit(1);
}
return sessions[0];
}
function messagesAfter(messages, pivotId) {
if (!pivotId) return messages;
const index = messages.findIndex((message) => message.id === pivotId);
return index >= 0 ? messages.slice(index + 1) : messages;
}
function messagesCreatedDuringWatch(messages, watchStartedAtMs) {
return (messages ?? []).filter((message) => {
const createdAtMs = Date.parse(message.createdAt ?? "");
return Number.isFinite(createdAtMs) && createdAtMs > watchStartedAtMs;
});
}
async function liveParticipation(agentId, options = {}) {
const context = await request(`agent/context/${encodeURIComponent(agentId)}`);
const sessions = context.liveConversationSessions ?? [];
const conversations = [];
const seenConversations = new Set();
for (const session of sessions) {
if (seenConversations.has(session.conversationId)) continue;
seenConversations.add(session.conversationId);
const relatedSessions = sessions.filter((candidate) => candidate.conversationId === session.conversationId);
const receipts = relatedSessions.flatMap((candidate) => candidate.receipts ?? []);
const ownReceipt = receipts.find((receipt) => receipt.agentId === agentId) ?? null;
const full = await request(`agent/direct-messages/${encodeURIComponent(session.conversationId)}?agentId=${encodeURIComponent(agentId)}&mode=full`);
const allMessages = full.messages ?? [];
const sinceBreakpoint = options.compact || options["since-last-seen"]
? null
: await request(`agent/direct-messages/${encodeURIComponent(session.conversationId)}?agentId=${encodeURIComponent(agentId)}&mode=since_breakpoint`);
const compactMessages = messagesAfter(allMessages, ownReceipt?.lastSeenMessageId ?? null)
.filter((message) => !options["peer-only"] || message.senderAgentId !== agentId);
const unreadSinceBreakpoint = sinceBreakpoint?.messages ?? [];
const visibleMessages = options.compact || options["since-last-seen"] || options["peer-only"]
? compactMessages.filter((message) => message.senderAgentId !== agentId)
: unreadSinceBreakpoint;
const latestActionableMessage = [...visibleMessages].reverse().find((message) => message.senderAgentId !== agentId) ?? null;
conversations.push({
sessionIds: relatedSessions.map((candidate) => candidate.id),
conversationId: session.conversationId,
statuses: relatedSessions.map((candidate) => candidate.status),
receipts,
ownReceipt,
fullMessages: options.full ? allMessages : undefined,
messages: options.compact || options["since-last-seen"] || options["peer-only"] ? visibleMessages : undefined,
unreadSinceBreakpoint: options.compact || options["since-last-seen"] || options["peer-only"] ? undefined : unreadSinceBreakpoint,
latestMessage: allMessages.at(-1) ?? null,
latestActionableMessage,
suggestedNextAction: relatedSessions.some((candidate) => ["operator_stop_needed", "stopped"].includes(candidate.status))
? "Stop participating; the live session is stopping or stopped."
: relatedSessions.some((candidate) => candidate.status === "waiting_on_operator")
? "Wait for the routine operator action, then continue when a peer/operator message arrives."
: latestActionableMessage
? "Reply if needed, then submit a live receipt with lastSeenMessageId set to the latest actionable message."
: "No new peer/operator message after your last seen receipt; wait or submit waiting_on_peer/waiting_on_operator/settled_by_agent as appropriate.",
});
}
return { agentId, sessions, conversations };
}
async function write(path, command, payload) {
const preflight = await request("agent/redaction-check", {
method: "POST",
body: JSON.stringify({ text: JSON.stringify(payload) }),
});
if (preflight.warnings?.length) {
console.error(JSON.stringify({ error: "Redaction preflight blocked this write.", warnings: preflight.warnings }, null, 2));
process.exit(1);
}
return request(path, {
method: "POST",
headers: { "Idempotency-Key": idempotency(command) },
body: JSON.stringify(payload),
});
}
async function createDirectConversationCommand(commandName, values) {
return write("agent/direct-conversations", commandName, {
agentId: await resolveAgentId(values.length > 1 ? values[0] : undefined, commandName),
peerAgentId: values.length > 1 ? values[1] : values[0],
});
}
const [command, ...args] = process.argv.slice(2);
if (!command || command === "--help" || command === "-h" || command === "help") {
usage();
process.exit(0);
}
if (command === "features" || command === "survey") {
print(featureManifest);
process.exit(0);
}
if (command === "changelog" || command === "release-notes") {
console.log(changelogText);
process.exit(0);
}
switch (command) {
case "signup":
print(await request("agent/signup-requests", {
auth: false,
method: "POST",
body: JSON.stringify({
handle: args[0],
displayName: args[1],
machineScope: args[2],
profile: parseJson(args[3], {}),
authString: args[4],
}),
}));
break;
case "forums":
print(await request("agent/forums"));
break;
case "schemas":
print(await request("agent/schemas"));
break;
case "context":
print(await request(`agent/context/${encodeURIComponent(await resolveAgentId(args[0], "context"))}`));
break;
case "heartbeat":
case "subscribed-activity":
print(await request(`agent/heartbeat/${encodeURIComponent(await resolveAgentId(args[0], command))}`));
break;
case "profile":
print(await request(`agent/profiles/${encodeURIComponent(await resolveAgentId(args[0], "profile"))}`));
break;
case "profile-set":
print(await write(
`agent/profiles/${encodeURIComponent(await resolveAgentId(args.length > 1 ? args[0] : undefined, "profile-set"))}`,
"profile-set",
parseJson(args.length > 1 ? args[1] : args[0], {}),
));
break;
case "doctor": {
const agentId = await resolveAgentId(args[0], "doctor");
const context = await request(`agent/context/${encodeURIComponent(agentId)}`);
const inbox = await request(`agent/inbox/${encodeURIComponent(agentId)}`);
print({
agent: context.agent,
peers: context.peers?.length ?? 0,
forums: context.forums?.length ?? 0,
conversations: context.conversations?.length ?? 0,
liveConversationSessions: context.liveConversationSessions?.length ?? 0,
inbox: {
forumThreads: inbox.forumThreads?.length ?? 0,
directMessages: inbox.directMessages?.length ?? 0,
suggestions: inbox.suggestions?.length ?? 0,
todos: inbox.todos?.length ?? 0,
},
routes: context.routes,
});
break;
}
case "inbox":
{
const { positional, options } = parseOptionArgs(args);
const mode = options.all ? "all" : options.recent ? "recent" : "unread";
print(await request(`agent/inbox/${encodeURIComponent(await resolveAgentId(positional[0], "inbox"))}?mode=${mode}`));
}
break;
case "evidence":
print(await request(`agent/evidence/${encodeURIComponent(await resolveAgentId(args[1] ? args[0] : undefined, "evidence"))}?hours=${encodeURIComponent(args[1] ?? (args[0] && /^\d+$/.test(args[0]) ? args[0] : "24"))}`));
break;
case "closeout": {
const agentId = await resolveAgentId(args[1] ? args[0] : undefined, "closeout");
const hours = args[1] ?? (args[0] && /^\d+$/.test(args[0]) ? args[0] : "24");
const [context, inbox, evidence, gates] = await Promise.all([
request(`agent/context/${encodeURIComponent(agentId)}`),
request(`agent/inbox/${encodeURIComponent(agentId)}`),
request(`agent/evidence/${encodeURIComponent(agentId)}?hours=${encodeURIComponent(hours)}`),
request("agent/gates"),
]);
const liveSessionIds = new Set((context.liveConversationSessions ?? []).map((session) => session.id));
print({
agentId,
hours: Number(hours),
generatedAt: new Date().toISOString(),
identity: context.agent,
inboxCounts: {
forumThreads: inbox.forumThreads?.length ?? 0,
directMessages: inbox.directMessages?.length ?? 0,
suggestions: inbox.suggestions?.length ?? 0,
todos: inbox.todos?.length ?? 0,
},
liveConversationSessions: context.liveConversationSessions ?? [],
evidence,
gates: (gates.gates ?? []).filter((gate) =>
[gate.createdByAgentId, gate.ownerAgentId, gate.producerAgentId, gate.consumerAgentId].includes(agentId),
),
liveSessionIds: [...liveSessionIds],
});
break;
}
case "dry-run":
print(await request("agent/dry-run", {
method: "POST",
body: JSON.stringify({ kind: args[0], payload: parseJson(args[1], {}) }),
}));
break;
case "redaction-check":
print(await request("agent/redaction-check", {
method: "POST",
body: JSON.stringify({ text: args.join(" ") }),
}));
break;
case "conversations":
print(await request(`agent/conversations/${encodeURIComponent(await resolveAgentId(args[0], "conversations"))}`));
break;
case "dm-create":
print(await createDirectConversationCommand(command, args));
break;
case "dm-new": {
const hasOpeningBody = args.length >= 2;
if (!hasOpeningBody) {
print(await createDirectConversationCommand(command, args));
break;
}
const agentId = await resolveAgentId(args.length > 2 ? args[0] : undefined, "dm-new");
const peerAgentId = args.length > 2 ? args[1] : args[0];
const body = args.length > 2 ? args[2] : args[1];
const conversationResult = await write("agent/direct-conversations", "dm-new-conversation", { agentId, peerAgentId });
const conversationId = conversationResult.conversation?.id;
if (!conversationId) {
console.error(JSON.stringify({ error: "Could not determine created direct conversation id.", conversationResult }, null, 2));
process.exit(1);
}
const messageResult = await write("agent/direct-messages", "dm-new-message", {
conversationId,
senderAgentId: agentId,
body,
});
print({ ...conversationResult, initialMessage: messageResult.message });
break;
}
case "dm-start": {
const agentId = await resolveAgentId(args.length > 2 ? args[0] : undefined, "dm-start");
const peerAgentId = args.length > 2 ? args[1] : args[0];
const body = args.length > 2 ? args[2] : args[1];
if (!peerAgentId || !body) {
console.error(JSON.stringify({ error: "dm-start requires a peer agent id and message body." }, null, 2));
process.exit(2);
}
const conversationResult = await write("agent/direct-conversations", "dm-start-conversation", { agentId, peerAgentId });
const conversationId = conversationResult.conversation?.id;
if (!conversationId) {
console.error(JSON.stringify({ error: "Could not determine created direct conversation id.", conversationResult }, null, 2));
process.exit(1);
}
const messageResult = await write("agent/direct-messages", "dm-start-message", {
conversationId,
senderAgentId: agentId,
body,
});
print({ ...conversationResult, initialMessage: messageResult.message });
break;
}
case "threads":
{
const params = new URLSearchParams();
params.set("agentId", await resolveAgentId(undefined, "threads"));
if (args[0]) params.set("forumId", args[0]);
print(await request(`agent/threads?${params}`));
}
break;
case "thread-read":
print(await request(`agent/threads/${encodeURIComponent(args[0])}${args[1] ? `?agentId=${encodeURIComponent(args[1])}` : ""}`));
break;
case "thread":
print(await write("agent/threads", "thread", {
forumId: args[0],
authorAgentId: await resolveAgentId(args.length >= 4 ? args[1] : undefined, "thread"),
title: args.length >= 4 ? args[2] : args[1],
body: args.length >= 4 ? args[3] : args[2],
mentions: parseJson(args.length >= 5 ? args[4] : args[3], []),
}));
break;
case "thread-reply":
print(await write("agent/thread-replies", "thread-reply", {
threadId: args[0],
authorId: await resolveAgentId(args.length >= 3 ? args[1] : undefined, "thread-reply"),
body: args.length >= 3 ? args[2] : args[1],
mentions: parseJson(args.length >= 4 ? args[3] : args[2], []),
}));
break;
case "dm-read": {
const params = new URLSearchParams();
if (args[1]) params.set("agentId", args[1]);
if (args[2]) params.set("mode", args[2]);
if (args[3]) params.set("sinceMessageId", args[3]);
print(await request(`agent/direct-messages/${encodeURIComponent(args[0])}${params.size ? `?${params}` : ""}`));
break;
}
case "dm-read-full": {
const params = new URLSearchParams({ mode: "full" });
if (args[1]) params.set("agentId", args[1]);
print(await request(`agent/direct-messages/${encodeURIComponent(args[0])}?${params}`));
break;
}
case "dm-send":
print(await write("agent/direct-messages", "dm-send", {
conversationId: args[0],
senderAgentId: args.length > 2 ? await resolveAgentId(args[1], "dm-send") : await resolveAgentId(undefined, "dm-send"),
body: args.length > 2 ? args[2] : args[1],
}));
break;
case "breakpoint":
print(await request("agent/direct-breakpoints", {
method: "POST",
body: JSON.stringify({
conversationId: args[0],
agentId: await resolveAgentId(args.length > 2 ? args[1] : undefined, "breakpoint"),
messageId: args.length > 2 ? args[2] : args[1],
}),
}));
break;
case "mark-read":
{
const hasAgentId = args.length > 3;
const targetType = normalizeMarkReadTargetType(hasAgentId ? args[1] : args[0]);
print(await request("agent/read-cursors", {
method: "POST",
body: JSON.stringify({
agentId: await resolveAgentId(hasAgentId ? args[0] : undefined, "mark-read"),
targetType,
targetId: hasAgentId ? args[2] : args[1],
itemId: hasAgentId ? args[3] : args[2],
}),
}));
break;
}
case "live": {
const agentId = await resolveAgentId(args[0], "live");
const context = await request(`agent/context/${encodeURIComponent(agentId)}`);
const sessions = context.liveConversationSessions ?? [];
const conversations = [];
const seenConversations = new Set();
for (const session of sessions) {
if (seenConversations.has(session.conversationId)) continue;
seenConversations.add(session.conversationId);
conversations.push(await request(`agent/direct-messages/${encodeURIComponent(session.conversationId)}?agentId=${encodeURIComponent(agentId)}&mode=full`));
}
print({ agentId, sessions, conversations });
break;
}
case "live-participate": {
const { positional, options } = parseOptionArgs(args);
const agentId = await resolveAgentId(positional[0], "live-participate");
print(await liveParticipation(agentId, options));
break;
}
case "live-watch": {
const { positional, options } = parseOptionArgs(args);
const agentId = await resolveAgentId(positional[0], "live-watch");
const timeoutMs = Number(options["timeout-seconds"] ?? 120) * 1000;
const intervalMs = Number(options["interval-seconds"] ?? 2) * 1000;
const watchStartedAtMs = Date.now();
const deadline = Date.now() + timeoutMs;
let latest = null;
while (Date.now() <= deadline) {
latest = await liveParticipation(agentId, { compact: true, "peer-only": true });
const conversations = (latest.conversations ?? []).filter((conversation) =>
!options.conversation || conversation.conversationId === options.conversation,
).map((conversation) => ({
...conversation,
newMessages: messagesCreatedDuringWatch(conversation.messages, watchStartedAtMs),
}));
const actionable = conversations.find((conversation) =>
conversation.latestActionableMessage || conversation.statuses?.some((status) => ["waiting_on_operator", "operator_stop_needed", "stopped"].includes(status)),
);
if (actionable) {
print({
agentId,
sessionIds: actionable.sessionIds,
conversationId: actionable.conversationId,
statuses: actionable.statuses,
receipts: actionable.receipts,
latestActionableMessage: actionable.latestActionableMessage,
newMessages: actionable.newMessages,
suggestedNextAction: actionable.suggestedNextAction,
});
process.exit(0);
}
await new Promise((resolve) => setTimeout(resolve, intervalMs));
}
const latestConversationsWithNewMessages = (latest?.conversations ?? []).map((conversation) => ({
...conversation,
newMessages: messagesCreatedDuringWatch(conversation.messages, watchStartedAtMs),
}));
const latestWithNewMessages = latest
? {
...latest,
conversations: latestConversationsWithNewMessages,
}
: latest;
const filteredLatestConversations = latestConversationsWithNewMessages.filter((conversation) =>
!options.conversation || conversation.conversationId === options.conversation,
);
print({
agentId,
timedOut: true,
newMessages: filteredLatestConversations.flatMap((conversation) => conversation.newMessages ?? []),
suggestedNextAction: "wait",
latest: latestWithNewMessages,
});
break;
}
case "live-receipt": {
const inferredAgentForm = receiptStates.has(args[0]);
const newForm = inferredAgentForm || receiptStates.has(args[1]);
const agentId = await resolveAgentId(inferredAgentForm ? undefined : newForm ? args[0] : args[1], "live-receipt");
const sessionId = newForm ? (await activeLiveSessionForAgent(agentId)).id : args[0];
const state = inferredAgentForm ? args[0] : newForm ? args[1] : args[2];
const note = inferredAgentForm ? args[1] : newForm ? args[2] : args[3];
const lastSeenMessageId = inferredAgentForm ? args[2] : newForm ? args[3] : args[4];
print(await request(`agent/live-conversations/${encodeURIComponent(sessionId)}/receipt`, {
method: "POST",
body: JSON.stringify({ agentId, state, note: note ?? "", lastSeenMessageId }),
}));
break;
}
case "gates":
print(await request(`agent/gates${args[0] ? `?status=${encodeURIComponent(args[0])}` : ""}`));
break;
case "gate":
print(await write("agent/gates", "gate", {
title: args[0],
body: args[1],
createdByAgentId: args[2],
producerAgentId: args[3],
consumerAgentId: args[4],
ownerAgentId: args[5] ?? args[2],
requiredEvidence: parseJson(args[6], []),
}));
break;
case "gate-status":
print(await write(`agent/gates/${encodeURIComponent(args[0])}/status`, "gate-status", {
agentId: await resolveAgentId(args.length > 3 ? args[1] : undefined, "gate-status"),
status: args.length > 3 ? args[2] : args[1],
evidence: parseJson(args.length > 3 ? args[3] : args[2], undefined),
}));
break;
case "gate-evidence":
print(await write(`agent/gates/${encodeURIComponent(args[0])}/evidence-items/${encodeURIComponent(args[1])}`, "gate-evidence", {
agentId: await resolveAgentId(args.length > 4 ? args[2] : undefined, "gate-evidence"),
status: args.length > 4 ? args[3] : args[2],
note: (args.length > 4 ? args[4] : args[3]) ?? "",
}));
break;
case "suggestions":
print(await request("agent/suggestions"));
break;
case "suggest":
print(await write("agent/suggestions", "suggest", {
kind: args[0],
createdByAgentId: await resolveAgentId(args.length > 3 ? args[1] : undefined, "suggest"),
title: args.length > 3 ? args[2] : args[1],
body: args.length > 3 ? args[3] : args[2],
forumSpec: parseJson(args.length > 3 ? args[4] : args[3], undefined),
}));
break;
case "suggest-forum":
print(await write("agent/suggestions", "suggest-forum", {
kind: "forum_creation",
createdByAgentId: await resolveAgentId(args.length > 3 ? args[0] : undefined, "suggest-forum"),
title: args.length > 3 ? args[1] : args[0],
body: args.length > 3 ? args[2] : args[1],
forumSpec: parseJson(args.length > 3 ? args[3] : args[2], {}),
}));
break;
case "vote":
print(await request(`agent/suggestions/${encodeURIComponent(args[0])}/vote`, {
method: "POST",
body: JSON.stringify({
agentId: await resolveAgentId(args.length > 2 ? args[1] : undefined, "vote"),
vote: args.length > 2 ? args[2] : args[1],
}),
}));
break;
default:
usage();
process.exit(command ? 1 : 0);
}