murmur-it 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/LICENSE +21 -0
- package/README.md +125 -2
- package/adapters/hummin/murmur.ts +467 -0
- package/adapters/opencode/murmur.ts +304 -0
- package/bin/murmur.js +11 -0
- package/dist/broker.js +1050 -0
- package/dist/cli.js +588 -0
- package/dist/client.js +210 -0
- package/dist/config.js +83 -0
- package/dist/core.js +779 -0
- package/dist/hooks.js +120 -0
- package/dist/install.js +250 -0
- package/dist/mcp.js +217 -0
- package/dist/native.js +125 -0
- package/dist/proc.js +110 -0
- package/dist/proof.js +111 -0
- package/dist/protocol.js +92 -0
- package/dist/store.js +496 -0
- package/dist/wake.js +12 -0
- package/package.json +63 -4
package/dist/cli.js
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
import { connectBroker, isBrokerDown } from './client.js';
|
|
4
|
+
import { ensureHome, getPaths, readToken } from './config.js';
|
|
5
|
+
import { HOOK_CLIENTS, runHook } from './hooks.js';
|
|
6
|
+
import { INSTALL_CLIENTS, installClient } from './install.js';
|
|
7
|
+
import { currentHost } from './proc.js';
|
|
8
|
+
import { HANDLE_RE, MurmurError } from './protocol.js';
|
|
9
|
+
const OWNER_HANDLE = 'renn';
|
|
10
|
+
const OWNER_DISPLAY = 'renn (owner)';
|
|
11
|
+
const SEND_USAGE = 'usage: murmur send <targets> <body...> [--as <handle>] [--mode <mode>] [--refs <a,b>] [--thread <id>]';
|
|
12
|
+
const REPLY_USAGE = 'usage: murmur reply <message id> <body...> [--as <handle>] [--verdict agree|disagree|info] [--refs <a,b>]';
|
|
13
|
+
const NAME_USAGE = 'usage: murmur name <handle> (renames the agent session this runs in)';
|
|
14
|
+
const THREAD_USAGE = 'usage: murmur thread <id> [--since <n>] [--full]';
|
|
15
|
+
const GROUPS_USAGE = 'usage: murmur groups [list] | groups create <name> | groups add <name> <members...> | groups remove <name> <members...> | groups delete <name>';
|
|
16
|
+
const SLASH_USAGE = '/murmur <group | handle[,more]> <message> (run bare like this for peers and groups)';
|
|
17
|
+
export async function runCli(argv, io) {
|
|
18
|
+
// The default io is captured at call time and touches only the real
|
|
19
|
+
// process streams, so injected io replaces them completely.
|
|
20
|
+
const out = io?.out ?? ((s) => { process.stdout.write(s); });
|
|
21
|
+
const err = io?.err ?? ((s) => { process.stderr.write(s); });
|
|
22
|
+
try {
|
|
23
|
+
return await dispatch(argv, out, err);
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
if (isBrokerDown(e)) {
|
|
27
|
+
err('murmur broker is down; start it with: murmur serve\n');
|
|
28
|
+
return 2;
|
|
29
|
+
}
|
|
30
|
+
if (e instanceof MurmurError) {
|
|
31
|
+
err(e.message + '\n');
|
|
32
|
+
return 1;
|
|
33
|
+
}
|
|
34
|
+
if (e instanceof Error) {
|
|
35
|
+
err(e.message + '\n');
|
|
36
|
+
return 1;
|
|
37
|
+
}
|
|
38
|
+
throw e;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async function dispatch(argv, out, err) {
|
|
42
|
+
const command = argv[0];
|
|
43
|
+
if (command === undefined) {
|
|
44
|
+
printUsage(err);
|
|
45
|
+
return 1;
|
|
46
|
+
}
|
|
47
|
+
switch (command) {
|
|
48
|
+
case 'init':
|
|
49
|
+
return cmdInit(out);
|
|
50
|
+
case 'serve':
|
|
51
|
+
return cmdServe(argv.slice(1), out);
|
|
52
|
+
case 'mcp':
|
|
53
|
+
// The MCP SDK and zod cost more to load than everything else here, and
|
|
54
|
+
// only this command needs them; hooks and sends run on every prompt.
|
|
55
|
+
await (await import('./mcp.js')).runMcpServer();
|
|
56
|
+
return 0;
|
|
57
|
+
case 'hook':
|
|
58
|
+
return cmdHook(argv.slice(1), out, err);
|
|
59
|
+
case 'send':
|
|
60
|
+
return cmdSend(argv.slice(1), out, err, 'session');
|
|
61
|
+
case 'reply':
|
|
62
|
+
return cmdReply(argv.slice(1), out, err);
|
|
63
|
+
case 'name':
|
|
64
|
+
return cmdName(argv.slice(1), out, err);
|
|
65
|
+
case 'slash':
|
|
66
|
+
return cmdSlash(argv.slice(1), out, err);
|
|
67
|
+
case 'status':
|
|
68
|
+
return cmdStatus(out);
|
|
69
|
+
case 'thread':
|
|
70
|
+
return cmdThread(argv.slice(1), out, err);
|
|
71
|
+
case 'groups':
|
|
72
|
+
return cmdGroups(argv.slice(1), out, err);
|
|
73
|
+
case 'install':
|
|
74
|
+
return cmdInstall(argv.slice(1), out, err);
|
|
75
|
+
default:
|
|
76
|
+
err('murmur: unknown command: ' + command + '\n');
|
|
77
|
+
printUsage(err);
|
|
78
|
+
return 1;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function printUsage(err) {
|
|
82
|
+
const lines = [
|
|
83
|
+
'usage: murmur <command> [args]',
|
|
84
|
+
'',
|
|
85
|
+
'commands:',
|
|
86
|
+
' init create the murmur home and the owner peer',
|
|
87
|
+
' serve [--daemon] start the broker and print the socket path',
|
|
88
|
+
' mcp serve the murmur MCP server over stdio',
|
|
89
|
+
' hook <event> [--client <cli>] run one hook (session-start, prompt, stop, session-end)',
|
|
90
|
+
' send <targets> <body...> send a message; flags: --as, --mode, --refs, --thread',
|
|
91
|
+
' reply <id> <body...> reply into a thread; flags: --as, --verdict, --refs',
|
|
92
|
+
' name <handle> rename the agent session this runs in',
|
|
93
|
+
' slash [args...] the /murmur backing (args or stdin); bare prints peers and groups',
|
|
94
|
+
' status list peers with presence and unread counts',
|
|
95
|
+
' thread <id> [--since <n>] [--full]',
|
|
96
|
+
' groups [list] | groups create <name> | groups add <name> <members...>',
|
|
97
|
+
' | groups remove <name> <members...> | groups delete <name>',
|
|
98
|
+
' install <' + INSTALL_CLIENTS.join('|') + '>',
|
|
99
|
+
];
|
|
100
|
+
err(lines.join('\n') + '\n');
|
|
101
|
+
}
|
|
102
|
+
async function cmdInit(out) {
|
|
103
|
+
const { Store } = await import('./store.js');
|
|
104
|
+
const paths = getPaths();
|
|
105
|
+
ensureHome(paths);
|
|
106
|
+
const store = Store.open(paths.dbPath);
|
|
107
|
+
try {
|
|
108
|
+
store.upsertPeer(OWNER_HANDLE, null, null, true, Date.now());
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
store.close();
|
|
112
|
+
}
|
|
113
|
+
out(paths.home + '\n');
|
|
114
|
+
return 0;
|
|
115
|
+
}
|
|
116
|
+
async function cmdServe(args, out) {
|
|
117
|
+
if (args.includes('--daemon')) {
|
|
118
|
+
// Re-spawn this exact entry file without the flag, detached from the
|
|
119
|
+
// current terminal, then report success immediately.
|
|
120
|
+
const self = process.argv[1] ?? '';
|
|
121
|
+
const child = spawn(process.execPath, [self, 'serve'], { detached: true, stdio: 'ignore' });
|
|
122
|
+
child.unref();
|
|
123
|
+
return 0;
|
|
124
|
+
}
|
|
125
|
+
const [{ Store }, { MurmurCore }, { Broker }] = await Promise.all([
|
|
126
|
+
import('./store.js'),
|
|
127
|
+
import('./core.js'),
|
|
128
|
+
import('./broker.js'),
|
|
129
|
+
]);
|
|
130
|
+
const paths = getPaths();
|
|
131
|
+
ensureHome(paths);
|
|
132
|
+
const store = Store.open(paths.dbPath);
|
|
133
|
+
// An autostarted broker may run before murmur init ever did.
|
|
134
|
+
if (!store.listPeers().some((peer) => peer.isOwner)) {
|
|
135
|
+
store.upsertPeer(OWNER_HANDLE, OWNER_DISPLAY, null, true, Date.now());
|
|
136
|
+
}
|
|
137
|
+
const events = {
|
|
138
|
+
onAppend: () => { },
|
|
139
|
+
onStateChange: () => { },
|
|
140
|
+
};
|
|
141
|
+
const core = new MurmurCore(store, events);
|
|
142
|
+
const broker = await Broker.start({ paths, store, core });
|
|
143
|
+
out(paths.socketPath + '\n');
|
|
144
|
+
let stopping = false;
|
|
145
|
+
await new Promise((resolve) => {
|
|
146
|
+
const stop = () => {
|
|
147
|
+
if (stopping) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
stopping = true;
|
|
151
|
+
void broker
|
|
152
|
+
.stop()
|
|
153
|
+
.catch(() => undefined)
|
|
154
|
+
.then(() => {
|
|
155
|
+
store.close();
|
|
156
|
+
resolve();
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
process.on('SIGINT', stop);
|
|
160
|
+
process.on('SIGTERM', stop);
|
|
161
|
+
});
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
function isHookEvent(value) {
|
|
165
|
+
return value === 'session-start' || value === 'prompt' || value === 'stop' || value === 'session-end';
|
|
166
|
+
}
|
|
167
|
+
async function cmdHook(args, out, err) {
|
|
168
|
+
const event = args[0];
|
|
169
|
+
const clientFlag = args.indexOf('--client');
|
|
170
|
+
const client = clientFlag === -1 ? 'claude' : args[clientFlag + 1];
|
|
171
|
+
if (event === undefined || !isHookEvent(event) || !HOOK_CLIENTS.includes(client)) {
|
|
172
|
+
err('usage: murmur hook <session-start|prompt|stop|session-end> [--client ' + HOOK_CLIENTS.join('|') + ']\n');
|
|
173
|
+
return 1;
|
|
174
|
+
}
|
|
175
|
+
const stdinText = await readAllStdin();
|
|
176
|
+
const outcome = await runHook(event, stdinText, process.env, { client: client });
|
|
177
|
+
out(withNewline(outcome.stdout));
|
|
178
|
+
return outcome.exitCode;
|
|
179
|
+
}
|
|
180
|
+
function withNewline(text) {
|
|
181
|
+
if (text.length === 0 || text.endsWith('\n')) {
|
|
182
|
+
return text;
|
|
183
|
+
}
|
|
184
|
+
return text + '\n';
|
|
185
|
+
}
|
|
186
|
+
function readAllStdin() {
|
|
187
|
+
return new Promise((resolve, reject) => {
|
|
188
|
+
let text = '';
|
|
189
|
+
process.stdin.setEncoding('utf8');
|
|
190
|
+
process.stdin.on('data', (chunk) => {
|
|
191
|
+
text += chunk;
|
|
192
|
+
});
|
|
193
|
+
process.stdin.once('end', () => {
|
|
194
|
+
resolve(text);
|
|
195
|
+
});
|
|
196
|
+
process.stdin.once('error', (ioErr) => {
|
|
197
|
+
reject(ioErr);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
function identityMessage(asHandle, speaker) {
|
|
202
|
+
if (asHandle !== null) {
|
|
203
|
+
return { type: 'register', handle: asHandle, clientKind: 'cli' };
|
|
204
|
+
}
|
|
205
|
+
if (speaker === 'owner') {
|
|
206
|
+
return { type: 'register', owner: true, display: OWNER_DISPLAY, clientKind: 'cli' };
|
|
207
|
+
}
|
|
208
|
+
return { type: 'register', clientKind: 'cli', ancestors: currentHost()?.ancestors ?? [] };
|
|
209
|
+
}
|
|
210
|
+
// Splits --flag value pairs from positionals. Null (after printing why)
|
|
211
|
+
// when a flag is missing its value.
|
|
212
|
+
function parseFlags(args, flags, usage, name, err) {
|
|
213
|
+
const values = new Map();
|
|
214
|
+
const positionals = [];
|
|
215
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
216
|
+
const arg = args[i];
|
|
217
|
+
if (flags.includes(arg)) {
|
|
218
|
+
const value = args[i + 1];
|
|
219
|
+
if (value === undefined) {
|
|
220
|
+
err('murmur ' + name + ': flag ' + arg + ' needs a value\n' + usage + '\n');
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
values.set(arg, value);
|
|
224
|
+
i += 1;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
positionals.push(arg);
|
|
228
|
+
}
|
|
229
|
+
return { values, positionals };
|
|
230
|
+
}
|
|
231
|
+
function splitRefs(value) {
|
|
232
|
+
return value
|
|
233
|
+
?.split(',')
|
|
234
|
+
.map((part) => part.trim())
|
|
235
|
+
.filter((part) => part.length > 0);
|
|
236
|
+
}
|
|
237
|
+
function validAs(value, err) {
|
|
238
|
+
if (value === undefined) {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
if (!HANDLE_RE.test(value)) {
|
|
242
|
+
// Rejected locally with the same message the broker would produce,
|
|
243
|
+
// before any connection is made.
|
|
244
|
+
err('invalid handle: ' + value + '\n');
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
return value;
|
|
248
|
+
}
|
|
249
|
+
async function cmdSend(args, out, err, speaker) {
|
|
250
|
+
const parsed = parseFlags(args, ['--as', '--mode', '--refs', '--thread'], SEND_USAGE, 'send', err);
|
|
251
|
+
if (parsed === null) {
|
|
252
|
+
return 1;
|
|
253
|
+
}
|
|
254
|
+
let threadId = null;
|
|
255
|
+
const threadValue = parsed.values.get('--thread');
|
|
256
|
+
if (threadValue !== undefined) {
|
|
257
|
+
const value = Number(threadValue);
|
|
258
|
+
if (!Number.isInteger(value) || value < 0) {
|
|
259
|
+
err('murmur send: --thread needs a non-negative integer, got: ' + threadValue + '\n');
|
|
260
|
+
return 1;
|
|
261
|
+
}
|
|
262
|
+
threadId = value;
|
|
263
|
+
}
|
|
264
|
+
const asHandle = validAs(parsed.values.get('--as'), err);
|
|
265
|
+
if (asHandle === false) {
|
|
266
|
+
return 1;
|
|
267
|
+
}
|
|
268
|
+
const targetsArg = parsed.positionals[0];
|
|
269
|
+
if (targetsArg === undefined) {
|
|
270
|
+
err('murmur send: missing targets\n' + SEND_USAGE + '\n');
|
|
271
|
+
return 1;
|
|
272
|
+
}
|
|
273
|
+
const body = parsed.positionals.slice(1).join(' ');
|
|
274
|
+
if (body.length === 0) {
|
|
275
|
+
err('murmur send: missing body\n' + SEND_USAGE + '\n');
|
|
276
|
+
return 1;
|
|
277
|
+
}
|
|
278
|
+
return sendAs(identityMessage(asHandle, speaker), targetsArg, body, threadId, parsed.values, out);
|
|
279
|
+
}
|
|
280
|
+
async function sendAs(identity, targetsArg, body, threadId, flags, out) {
|
|
281
|
+
const to = targetsArg
|
|
282
|
+
.split(',')
|
|
283
|
+
.map((entry) => entry.trim())
|
|
284
|
+
.filter((entry) => entry.length > 0);
|
|
285
|
+
const client = await connectCli();
|
|
286
|
+
try {
|
|
287
|
+
await client.request(identity);
|
|
288
|
+
const msg = { type: 'send', threadId, to, body };
|
|
289
|
+
const mode = flags.get('--mode');
|
|
290
|
+
if (mode !== undefined) {
|
|
291
|
+
// The broker validates the mode value against the wire enum.
|
|
292
|
+
msg.mode = mode;
|
|
293
|
+
}
|
|
294
|
+
const refs = splitRefs(flags.get('--refs'));
|
|
295
|
+
if (refs !== undefined) {
|
|
296
|
+
msg.references = refs;
|
|
297
|
+
}
|
|
298
|
+
const result = await client.request(msg);
|
|
299
|
+
for (const delivery of result.deliveries) {
|
|
300
|
+
out(delivery.recipient + ': ' + delivery.state + '\n');
|
|
301
|
+
}
|
|
302
|
+
out('message ' + String(result.messageId) + ' thread ' + String(result.threadId) + ' seq ' + String(result.seq) + '\n');
|
|
303
|
+
return 0;
|
|
304
|
+
}
|
|
305
|
+
finally {
|
|
306
|
+
client.close();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
async function cmdReply(args, out, err) {
|
|
310
|
+
const parsed = parseFlags(args, ['--as', '--verdict', '--refs'], REPLY_USAGE, 'reply', err);
|
|
311
|
+
if (parsed === null) {
|
|
312
|
+
return 1;
|
|
313
|
+
}
|
|
314
|
+
const asHandle = validAs(parsed.values.get('--as'), err);
|
|
315
|
+
if (asHandle === false) {
|
|
316
|
+
return 1;
|
|
317
|
+
}
|
|
318
|
+
const idArg = parsed.positionals[0];
|
|
319
|
+
const messageId = Number(idArg);
|
|
320
|
+
const body = parsed.positionals.slice(1).join(' ');
|
|
321
|
+
if (idArg === undefined || !Number.isInteger(messageId) || messageId < 0 || body.length === 0) {
|
|
322
|
+
err('murmur reply: need a message id and a body\n' + REPLY_USAGE + '\n');
|
|
323
|
+
return 1;
|
|
324
|
+
}
|
|
325
|
+
const client = await connectCli();
|
|
326
|
+
try {
|
|
327
|
+
await client.request(identityMessage(asHandle, 'session'));
|
|
328
|
+
const msg = { type: 'reply', messageId, body };
|
|
329
|
+
const verdict = parsed.values.get('--verdict');
|
|
330
|
+
if (verdict !== undefined) {
|
|
331
|
+
// The broker validates the verdict against the wire enum.
|
|
332
|
+
msg.verdict = verdict;
|
|
333
|
+
}
|
|
334
|
+
const refs = splitRefs(parsed.values.get('--refs'));
|
|
335
|
+
if (refs !== undefined) {
|
|
336
|
+
msg.references = refs;
|
|
337
|
+
}
|
|
338
|
+
const result = await client.request(msg);
|
|
339
|
+
out('message ' + String(result.messageId) + ' thread ' + String(result.threadId) + ' seq ' + String(result.seq) + '\n');
|
|
340
|
+
return 0;
|
|
341
|
+
}
|
|
342
|
+
finally {
|
|
343
|
+
client.close();
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
// Renames the agent session this command runs in (its host process).
|
|
347
|
+
async function cmdName(args, out, err) {
|
|
348
|
+
const handle = args[0];
|
|
349
|
+
const host = currentHost(undefined, false);
|
|
350
|
+
if (handle === undefined || !HANDLE_RE.test(handle)) {
|
|
351
|
+
err('murmur name: need a valid handle (a-z, 0-9, -)\n' + NAME_USAGE + '\n');
|
|
352
|
+
return 1;
|
|
353
|
+
}
|
|
354
|
+
if (host === null) {
|
|
355
|
+
err('murmur name: cannot find the agent session this runs in\n');
|
|
356
|
+
return 1;
|
|
357
|
+
}
|
|
358
|
+
const client = await connectCli();
|
|
359
|
+
try {
|
|
360
|
+
const msg = {
|
|
361
|
+
type: 'register',
|
|
362
|
+
clientKind: 'cli',
|
|
363
|
+
handle,
|
|
364
|
+
host: { hostPid: host.hostPid, hostStart: host.hostStart },
|
|
365
|
+
};
|
|
366
|
+
const result = await client.request(msg);
|
|
367
|
+
out(result.handle + '\n');
|
|
368
|
+
return 0;
|
|
369
|
+
}
|
|
370
|
+
finally {
|
|
371
|
+
client.close();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
// Auth-only connection; each command registers the identity it needs.
|
|
375
|
+
async function connectCli() {
|
|
376
|
+
const paths = getPaths();
|
|
377
|
+
const token = readToken(paths);
|
|
378
|
+
return connectBroker({ socketPath: paths.socketPath, token });
|
|
379
|
+
}
|
|
380
|
+
// One formatted line per peer, exactly as the status view prints them. The
|
|
381
|
+
// slash discovery view reuses these so the two can never drift apart.
|
|
382
|
+
async function statusLines() {
|
|
383
|
+
const client = await connectCli();
|
|
384
|
+
try {
|
|
385
|
+
const result = await client.request({ type: 'status' });
|
|
386
|
+
return result.peers.map((peer) => {
|
|
387
|
+
let line = peer.handle + ' ' + peer.presence + (peer.client === null ? '' : ' ' + peer.client) + ' unread ' + String(peer.unread);
|
|
388
|
+
if (peer.display !== null) {
|
|
389
|
+
line += ' ' + peer.display;
|
|
390
|
+
}
|
|
391
|
+
return line;
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
finally {
|
|
395
|
+
client.close();
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
async function cmdStatus(out) {
|
|
399
|
+
const lines = await statusLines();
|
|
400
|
+
for (const line of lines) {
|
|
401
|
+
out(line + '\n');
|
|
402
|
+
}
|
|
403
|
+
return 0;
|
|
404
|
+
}
|
|
405
|
+
async function cmdThread(args, out, err) {
|
|
406
|
+
let full = false;
|
|
407
|
+
let sinceSeq = null;
|
|
408
|
+
const positionals = [];
|
|
409
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
410
|
+
const arg = args[i];
|
|
411
|
+
if (arg === undefined) {
|
|
412
|
+
break;
|
|
413
|
+
}
|
|
414
|
+
if (arg === '--full') {
|
|
415
|
+
full = true;
|
|
416
|
+
continue;
|
|
417
|
+
}
|
|
418
|
+
if (arg === '--since') {
|
|
419
|
+
const value = args[i + 1];
|
|
420
|
+
if (value === undefined) {
|
|
421
|
+
err('murmur thread: --since needs a value\n' + THREAD_USAGE + '\n');
|
|
422
|
+
return 1;
|
|
423
|
+
}
|
|
424
|
+
i += 1;
|
|
425
|
+
const parsed = Number(value);
|
|
426
|
+
if (!Number.isInteger(parsed) || parsed < 0) {
|
|
427
|
+
err('murmur thread: --since needs a non-negative integer, got: ' + value + '\n');
|
|
428
|
+
return 1;
|
|
429
|
+
}
|
|
430
|
+
sinceSeq = parsed;
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
positionals.push(arg);
|
|
434
|
+
}
|
|
435
|
+
const idArg = positionals[0];
|
|
436
|
+
if (idArg === undefined) {
|
|
437
|
+
err('murmur thread: missing thread id\n' + THREAD_USAGE + '\n');
|
|
438
|
+
return 1;
|
|
439
|
+
}
|
|
440
|
+
const threadId = Number(idArg);
|
|
441
|
+
if (!Number.isInteger(threadId) || threadId < 0) {
|
|
442
|
+
err('murmur thread: thread id must be a non-negative integer, got: ' + idArg + '\n');
|
|
443
|
+
return 1;
|
|
444
|
+
}
|
|
445
|
+
const client = await connectCli();
|
|
446
|
+
try {
|
|
447
|
+
const msg = { type: 'thread', threadId, sinceSeq, full };
|
|
448
|
+
const result = await client.request(msg);
|
|
449
|
+
for (const message of result.messages) {
|
|
450
|
+
const text = full ? message.body : message.firstLine;
|
|
451
|
+
out(String(message.seq) + ' ' + message.sender + ' ' + text + '\n');
|
|
452
|
+
}
|
|
453
|
+
return 0;
|
|
454
|
+
}
|
|
455
|
+
finally {
|
|
456
|
+
client.close();
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
// Runs one groups action and returns one formatted line per group, exactly as
|
|
460
|
+
// the groups view prints them; the slash discovery view reuses the list form.
|
|
461
|
+
async function groupsLines(action, name, members) {
|
|
462
|
+
const client = await connectCli();
|
|
463
|
+
try {
|
|
464
|
+
const msg = { type: 'groups', action };
|
|
465
|
+
if (name !== undefined) {
|
|
466
|
+
msg.name = name;
|
|
467
|
+
}
|
|
468
|
+
if (members !== undefined) {
|
|
469
|
+
msg.members = members;
|
|
470
|
+
}
|
|
471
|
+
const groups = await client.request(msg);
|
|
472
|
+
return groups.map((group) => {
|
|
473
|
+
const memberText = group.members.length > 0 ? ' ' + group.members.join(', ') : '';
|
|
474
|
+
return group.name + ':' + memberText;
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
finally {
|
|
478
|
+
client.close();
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
async function cmdGroups(args, out, err) {
|
|
482
|
+
const sub = args[0];
|
|
483
|
+
let action = 'list';
|
|
484
|
+
let name;
|
|
485
|
+
let members;
|
|
486
|
+
if (sub === undefined || sub === 'list') {
|
|
487
|
+
action = 'list';
|
|
488
|
+
}
|
|
489
|
+
else if (sub === 'create' || sub === 'delete') {
|
|
490
|
+
const value = args[1];
|
|
491
|
+
if (value === undefined) {
|
|
492
|
+
err('murmur groups ' + sub + ': missing group name\n' + GROUPS_USAGE + '\n');
|
|
493
|
+
return 1;
|
|
494
|
+
}
|
|
495
|
+
action = sub;
|
|
496
|
+
name = value;
|
|
497
|
+
}
|
|
498
|
+
else if (sub === 'add' || sub === 'remove') {
|
|
499
|
+
const value = args[1];
|
|
500
|
+
const rest = args.slice(2);
|
|
501
|
+
if (value === undefined || rest.length === 0) {
|
|
502
|
+
err('murmur groups ' + sub + ': need a group name and at least one member\n' + GROUPS_USAGE + '\n');
|
|
503
|
+
return 1;
|
|
504
|
+
}
|
|
505
|
+
action = sub;
|
|
506
|
+
name = value;
|
|
507
|
+
members = rest;
|
|
508
|
+
}
|
|
509
|
+
else {
|
|
510
|
+
err('murmur groups: unknown action: ' + sub + '\n' + GROUPS_USAGE + '\n');
|
|
511
|
+
return 1;
|
|
512
|
+
}
|
|
513
|
+
const lines = await groupsLines(action, name, members);
|
|
514
|
+
for (const line of lines) {
|
|
515
|
+
out(line + '\n');
|
|
516
|
+
}
|
|
517
|
+
return 0;
|
|
518
|
+
}
|
|
519
|
+
// The backing of the /murmur command, always speaking as the owner. The
|
|
520
|
+
// whole argument text arrives either on stdin (--stdin: the command file
|
|
521
|
+
// feeds it through a quoted heredoc so no shell ever parses the message)
|
|
522
|
+
// or as one shell-escaped word (gemini): the first word is the targets,
|
|
523
|
+
// the rest is the body, verbatim. Bare, it is the discovery surface:
|
|
524
|
+
// usage, then peers as status prints them, then groups as the groups list
|
|
525
|
+
// prints them.
|
|
526
|
+
async function cmdSlash(args, out, err) {
|
|
527
|
+
let words = args;
|
|
528
|
+
const oneWord = args.length === 1 && /\s/.test(args[0]);
|
|
529
|
+
if (args[0] === '--stdin' || oneWord) {
|
|
530
|
+
let text = (oneWord ? args[0] : await readAllStdin()).trim();
|
|
531
|
+
// Claude Code rewrites a "!" that starts a word in $ARGUMENTS to "\!"
|
|
532
|
+
// (its guard against injected shell blocks); undo exactly that.
|
|
533
|
+
if (args[1] === '--claude-escaped') {
|
|
534
|
+
text = text.replace(/(^|\s)\\!/g, '$1!');
|
|
535
|
+
}
|
|
536
|
+
const match = /^(\S+)\s+([\s\S]+)$/.exec(text);
|
|
537
|
+
if (match !== null) {
|
|
538
|
+
return sendAs(identityMessage(null, 'owner'), match[1], match[2], null, new Map(), out);
|
|
539
|
+
}
|
|
540
|
+
words = text.length === 0 ? [] : [text];
|
|
541
|
+
}
|
|
542
|
+
const sub = words[0];
|
|
543
|
+
if (sub === 'status') {
|
|
544
|
+
return cmdStatus(out);
|
|
545
|
+
}
|
|
546
|
+
if (sub === 'groups') {
|
|
547
|
+
return cmdGroups(words.slice(1), out, err);
|
|
548
|
+
}
|
|
549
|
+
if (sub !== undefined) {
|
|
550
|
+
return cmdSend(words, out, err, 'owner');
|
|
551
|
+
}
|
|
552
|
+
const peers = await statusLines();
|
|
553
|
+
const groups = await groupsLines('list');
|
|
554
|
+
out(SLASH_USAGE + '\n');
|
|
555
|
+
out('\npeers:\n');
|
|
556
|
+
for (const line of peers) {
|
|
557
|
+
out(line + '\n');
|
|
558
|
+
}
|
|
559
|
+
out('\ngroups:\n');
|
|
560
|
+
for (const line of groups) {
|
|
561
|
+
out(line + '\n');
|
|
562
|
+
}
|
|
563
|
+
return 0;
|
|
564
|
+
}
|
|
565
|
+
async function cmdInstall(args, out, err) {
|
|
566
|
+
const name = args[0];
|
|
567
|
+
if (name === undefined || !INSTALL_CLIENTS.includes(name)) {
|
|
568
|
+
err('usage: murmur install <' + INSTALL_CLIENTS.join('|') + '>\n');
|
|
569
|
+
return 1;
|
|
570
|
+
}
|
|
571
|
+
for (const line of await installClient(name)) {
|
|
572
|
+
out(line + '\n');
|
|
573
|
+
}
|
|
574
|
+
return 0;
|
|
575
|
+
}
|
|
576
|
+
// The entry guard lives in one function so every branch of it is attributed
|
|
577
|
+
// to a function body, which the v8 coverage provider credits reliably even
|
|
578
|
+
// when a second module instance (a dynamic re-import) runs the entry path.
|
|
579
|
+
function runWhenEntry() {
|
|
580
|
+
const isEntry = import.meta.url === pathToFileURL(process.argv[1] ?? '').href;
|
|
581
|
+
if (isEntry) {
|
|
582
|
+
void runCli(process.argv.slice(2)).then((code) => {
|
|
583
|
+
process.exit(code);
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
return isEntry;
|
|
587
|
+
}
|
|
588
|
+
runWhenEntry();
|