multiagents 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ #!/usr/bin/env bun
2
+ // ============================================================================
3
+ // multiagents — CLI Command Router
4
+ // ============================================================================
5
+
6
+ import { DEFAULT_BROKER_PORT, BROKER_HOSTNAME } from "../shared/constants.ts";
7
+ import { BrokerClient } from "../shared/broker-client.ts";
8
+
9
+ const BROKER_PORT = parseInt(process.env.MULTIAGENTS_PORT ?? String(DEFAULT_BROKER_PORT), 10);
10
+ const BROKER_URL = `http://${BROKER_HOSTNAME}:${BROKER_PORT}`;
11
+
12
+ function printUsage(): void {
13
+ console.log(`multiagents CLI
14
+
15
+ Usage:
16
+ multiagents setup Interactive setup wizard
17
+ multiagents dashboard [session-id] TUI dashboard
18
+ multiagents session create <name> Create a new session
19
+ multiagents session list List all sessions
20
+ multiagents session resume [session-id] Resume a paused session
21
+ multiagents session pause [session-id] Pause a session
22
+ multiagents session archive <session-id> Archive a session
23
+ multiagents session delete <session-id> Delete a session
24
+ multiagents session export <session-id> Export session transcript
25
+ multiagents send <target> <message> Send message to peer/slot
26
+ multiagents peers List connected peers
27
+ multiagents status Broker health + peers summary
28
+ multiagents broker start|stop|status Manage broker daemon
29
+ multiagents mcp-server [--agent-type <t>] Run MCP server
30
+ multiagents orchestrator Run orchestrator server
31
+ multiagents help Show this help`);
32
+ }
33
+
34
+ export async function runCli(args: string[]): Promise<void> {
35
+ const command = args[0];
36
+
37
+ switch (command) {
38
+ case "setup": {
39
+ const { setup } = await import("./setup.ts");
40
+ await setup();
41
+ break;
42
+ }
43
+
44
+ case "dashboard": {
45
+ const { dashboard } = await import("./dashboard.ts");
46
+ await dashboard(args[1]);
47
+ break;
48
+ }
49
+
50
+ case "session": {
51
+ const { sessionCommand } = await import("./session.ts");
52
+ await sessionCommand(args.slice(1));
53
+ break;
54
+ }
55
+
56
+ case "send": {
57
+ const target = args[1];
58
+ const msg = args.slice(2).join(" ");
59
+ if (!target || !msg) {
60
+ console.error("Usage: multiagents send <peer-id|slot-id> <message>");
61
+ process.exit(1);
62
+ }
63
+ const client = new BrokerClient(BROKER_URL);
64
+ const isSlot = /^\d+$/.test(target);
65
+ const result = await client.sendMessage({
66
+ from_id: "cli",
67
+ ...(isSlot ? { to_slot_id: parseInt(target, 10) } : { to_id: target }),
68
+ text: msg,
69
+ });
70
+ if (result.ok) {
71
+ console.log(`Message sent to ${target}`);
72
+ } else {
73
+ console.error(`Failed: ${result.error}`);
74
+ process.exit(1);
75
+ }
76
+ break;
77
+ }
78
+
79
+ case "peers": {
80
+ const client = new BrokerClient(BROKER_URL);
81
+ try {
82
+ const peers = await client.listPeers({ scope: "machine", cwd: "/", git_root: null });
83
+ if (peers.length === 0) {
84
+ console.log("No peers registered.");
85
+ } else {
86
+ for (const p of peers) {
87
+ console.log(` ${p.id} PID:${p.pid} [${p.agent_type}] ${p.cwd}`);
88
+ if (p.summary) console.log(` ${p.summary}`);
89
+ if (p.tty) console.log(` TTY: ${p.tty}`);
90
+ console.log(` Last seen: ${p.last_seen}`);
91
+ }
92
+ }
93
+ } catch {
94
+ console.log("Broker is not running.");
95
+ }
96
+ break;
97
+ }
98
+
99
+ case "status": {
100
+ const client = new BrokerClient(BROKER_URL);
101
+ try {
102
+ const alive = await client.isAlive();
103
+ if (!alive) {
104
+ console.log("Broker is not running.");
105
+ break;
106
+ }
107
+ const peers = await client.listPeers({ scope: "machine", cwd: "/", git_root: null });
108
+ const sessions = await client.listSessions();
109
+ console.log(`Broker: running on ${BROKER_URL}`);
110
+ console.log(`Sessions: ${sessions.length} (${sessions.filter(s => s.status === "active").length} active)`);
111
+ console.log(`Peers: ${peers.length} registered`);
112
+ if (peers.length > 0) {
113
+ console.log("\nConnected agents:");
114
+ for (const p of peers) {
115
+ console.log(` ${p.id} PID:${p.pid} [${p.agent_type}] ${p.cwd}`);
116
+ }
117
+ }
118
+ } catch {
119
+ console.log("Broker is not running.");
120
+ }
121
+ break;
122
+ }
123
+
124
+ case "broker": {
125
+ const sub = args[1];
126
+ if (sub === "start") {
127
+ const proc = Bun.spawn(["bun", `${import.meta.dir}/../broker.ts`], {
128
+ stdio: ["ignore", "ignore", "ignore"],
129
+ });
130
+ proc.unref();
131
+ // Wait for broker to come up
132
+ const client = new BrokerClient(BROKER_URL);
133
+ for (let i = 0; i < 30; i++) {
134
+ if (await client.isAlive()) {
135
+ console.log(`Broker started on ${BROKER_URL} (PID ${proc.pid})`);
136
+ return;
137
+ }
138
+ await Bun.sleep(200);
139
+ }
140
+ console.error("Broker failed to start within 6s.");
141
+ process.exit(1);
142
+ } else if (sub === "stop") {
143
+ const proc = Bun.spawnSync(["lsof", "-ti", `:${BROKER_PORT}`]);
144
+ const pids = new TextDecoder().decode(proc.stdout).trim().split("\n").filter(Boolean);
145
+ if (pids.length === 0) {
146
+ console.log("Broker is not running.");
147
+ } else {
148
+ for (const pid of pids) process.kill(parseInt(pid), "SIGTERM");
149
+ console.log("Broker stopped.");
150
+ }
151
+ } else if (sub === "status") {
152
+ const client = new BrokerClient(BROKER_URL);
153
+ const alive = await client.isAlive();
154
+ console.log(alive ? `Broker is running on ${BROKER_URL}` : "Broker is not running.");
155
+ } else {
156
+ console.error("Usage: multiagents broker start|stop|status");
157
+ process.exit(1);
158
+ }
159
+ break;
160
+ }
161
+
162
+ case "mcp-server": {
163
+ const typeIdx = args.indexOf("--agent-type");
164
+ const agentType = typeIdx !== -1 ? args[typeIdx + 1] : undefined;
165
+ if (agentType) process.env.AGENT_TYPE = agentType;
166
+ await import("../server.ts");
167
+ break;
168
+ }
169
+
170
+ case "orchestrator": {
171
+ await import("../orchestrator/orchestrator-server.ts");
172
+ break;
173
+ }
174
+
175
+ // Legacy aliases from old cli.ts
176
+ case "kill-broker": {
177
+ await runCli(["broker", "stop"]);
178
+ break;
179
+ }
180
+
181
+ case "help":
182
+ case "--help":
183
+ case "-h":
184
+ case undefined: {
185
+ printUsage();
186
+ break;
187
+ }
188
+
189
+ default:
190
+ console.error(`Unknown command: ${command}`);
191
+ printUsage();
192
+ process.exit(1);
193
+ }
194
+ }