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
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
// murmur adapter for opencode: joins each opencode session to the murmur bus.
|
|
2
|
+
// One long-lived socket per session, push events become promptAsync calls,
|
|
3
|
+
// and the murmur_* tools talk over the calling session's connection.
|
|
4
|
+
import { tool, type Plugin } from '@opencode-ai/plugin';
|
|
5
|
+
import { execFileSync } from 'node:child_process';
|
|
6
|
+
import { readFileSync } from 'node:fs';
|
|
7
|
+
import { connect, type Socket } from 'node:net';
|
|
8
|
+
import { homedir } from 'node:os';
|
|
9
|
+
import { join } from 'node:path';
|
|
10
|
+
|
|
11
|
+
const HOME = process.env.MURMUR_HOME || join(homedir(), '.murmur');
|
|
12
|
+
const RETRY_MS = 5000;
|
|
13
|
+
|
|
14
|
+
let hostStart = '';
|
|
15
|
+
try {
|
|
16
|
+
hostStart = execFileSync('ps', ['-o', 'lstart=', '-p', String(process.pid)], {
|
|
17
|
+
env: { ...process.env, LC_ALL: 'C' },
|
|
18
|
+
encoding: 'utf8',
|
|
19
|
+
}).trim();
|
|
20
|
+
} catch {
|
|
21
|
+
// leave empty; the broker will refuse the announce and we retry quietly
|
|
22
|
+
}
|
|
23
|
+
const host = { hostPid: process.pid, hostStart };
|
|
24
|
+
|
|
25
|
+
type Pending = { resolve: (v: any) => void; reject: (e: Error) => void; timer: ReturnType<typeof setTimeout> };
|
|
26
|
+
|
|
27
|
+
interface Conn {
|
|
28
|
+
sid: string;
|
|
29
|
+
cwd: string;
|
|
30
|
+
alive: boolean;
|
|
31
|
+
busy: boolean;
|
|
32
|
+
handle: string | null;
|
|
33
|
+
sock: Socket | null;
|
|
34
|
+
buf: string;
|
|
35
|
+
nextId: number;
|
|
36
|
+
pending: Map<number, Pending>;
|
|
37
|
+
retry: ReturnType<typeof setTimeout> | null;
|
|
38
|
+
ready: Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const plugin: Plugin = async ({ client, directory }) => {
|
|
42
|
+
const conns = new Map<string, Conn>();
|
|
43
|
+
|
|
44
|
+
const fail = (p: Pending, msg: string) => {
|
|
45
|
+
clearTimeout(p.timer);
|
|
46
|
+
p.reject(new Error(msg));
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const drop = (c: Conn) => {
|
|
50
|
+
for (const p of c.pending.values()) fail(p, 'murmur connection closed');
|
|
51
|
+
c.pending.clear();
|
|
52
|
+
c.sock?.destroy();
|
|
53
|
+
c.sock = null;
|
|
54
|
+
if (c.alive && !c.retry) {
|
|
55
|
+
c.retry = setTimeout(() => {
|
|
56
|
+
c.retry = null;
|
|
57
|
+
if (c.alive) c.ready = open(c);
|
|
58
|
+
}, RETRY_MS);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const rpc = (c: Conn, msg: Record<string, unknown>, timeoutMs = 15000): Promise<any> =>
|
|
63
|
+
new Promise((resolve, reject) => {
|
|
64
|
+
if (!c.sock) return reject(new Error('murmur broker not connected'));
|
|
65
|
+
const id = c.nextId++;
|
|
66
|
+
const timer = setTimeout(() => {
|
|
67
|
+
c.pending.delete(id);
|
|
68
|
+
reject(new Error('murmur request timed out'));
|
|
69
|
+
}, timeoutMs);
|
|
70
|
+
c.pending.set(id, { resolve, reject, timer });
|
|
71
|
+
c.sock.write(JSON.stringify({ v: 2, id, ...msg }) + '\n');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const lifecycle = (c: Conn, event: string) =>
|
|
75
|
+
rpc(c, {
|
|
76
|
+
type: 'session',
|
|
77
|
+
event,
|
|
78
|
+
client: 'opencode',
|
|
79
|
+
cliSessionId: c.sid,
|
|
80
|
+
host,
|
|
81
|
+
cwd: c.cwd,
|
|
82
|
+
listen: true,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const post = async (c: Conn, text: string) => {
|
|
86
|
+
const r: any = await client.session.promptAsync({
|
|
87
|
+
path: { id: c.sid },
|
|
88
|
+
body: { parts: [{ type: 'text', text }] },
|
|
89
|
+
});
|
|
90
|
+
if (r && r.error) throw new Error('promptAsync failed');
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const onEvent = async (c: Conn, m: any) => {
|
|
94
|
+
if (m.event === 'push') {
|
|
95
|
+
const text = m.payload?.text;
|
|
96
|
+
const mid = m.payload?.message?.id;
|
|
97
|
+
if (typeof text !== 'string') return;
|
|
98
|
+
await post(c, text);
|
|
99
|
+
if (typeof mid === 'number') await rpc(c, { type: 'delivered', ids: [mid] });
|
|
100
|
+
} else if (m.event === 'notification' && !c.busy && Array.isArray(m.payload)) {
|
|
101
|
+
const details = m.payload.map((n: any) => n?.detail).filter(Boolean);
|
|
102
|
+
if (details.length) await post(c, '[murmur notice] ' + details.join('; '));
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const onLine = (c: Conn, line: string) => {
|
|
107
|
+
let m: any;
|
|
108
|
+
try {
|
|
109
|
+
m = JSON.parse(line);
|
|
110
|
+
} catch {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (m.type === 'event') {
|
|
114
|
+
onEvent(c, m).catch(() => {});
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const p = c.pending.get(m.id);
|
|
118
|
+
if (!p) return;
|
|
119
|
+
c.pending.delete(m.id);
|
|
120
|
+
clearTimeout(p.timer);
|
|
121
|
+
if (m.ok) p.resolve(m.result);
|
|
122
|
+
else p.reject(new Error(m.error?.message ?? 'murmur error'));
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const open = async (c: Conn): Promise<void> => {
|
|
126
|
+
try {
|
|
127
|
+
const token = readFileSync(join(HOME, 'token'), 'utf8').trim();
|
|
128
|
+
const sock = await new Promise<Socket>((resolve, reject) => {
|
|
129
|
+
const s = connect(join(HOME, 'bus.sock'));
|
|
130
|
+
s.once('connect', () => resolve(s));
|
|
131
|
+
s.once('error', reject);
|
|
132
|
+
});
|
|
133
|
+
c.sock = sock;
|
|
134
|
+
c.buf = '';
|
|
135
|
+
c.nextId = 1;
|
|
136
|
+
sock.setEncoding('utf8');
|
|
137
|
+
sock.on('data', (d: string) => {
|
|
138
|
+
c.buf += d;
|
|
139
|
+
let i;
|
|
140
|
+
while ((i = c.buf.indexOf('\n')) >= 0) {
|
|
141
|
+
const line = c.buf.slice(0, i);
|
|
142
|
+
c.buf = c.buf.slice(i + 1);
|
|
143
|
+
if (line) onLine(c, line);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
sock.on('error', () => {});
|
|
147
|
+
sock.on('close', () => {
|
|
148
|
+
if (c.sock === sock) drop(c);
|
|
149
|
+
});
|
|
150
|
+
await rpc(c, { type: 'auth', token });
|
|
151
|
+
const r = await lifecycle(c, 'session-start');
|
|
152
|
+
c.handle = r?.handle ?? c.handle;
|
|
153
|
+
} catch {
|
|
154
|
+
drop(c);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const ensure = (sid: string, cwd?: string): Conn => {
|
|
159
|
+
let c = conns.get(sid);
|
|
160
|
+
if (!c) {
|
|
161
|
+
c = {
|
|
162
|
+
sid, cwd: cwd || directory, alive: true, busy: false, handle: null, sock: null,
|
|
163
|
+
buf: '', nextId: 1, pending: new Map(), retry: null, ready: Promise.resolve(),
|
|
164
|
+
};
|
|
165
|
+
conns.set(sid, c);
|
|
166
|
+
c.ready = open(c);
|
|
167
|
+
}
|
|
168
|
+
return c;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const send = (sid: string, event: string) => {
|
|
172
|
+
const c = conns.get(sid);
|
|
173
|
+
if (c?.sock) lifecycle(c, event).catch(() => {});
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const setBusy = (sid: string, cwd?: string) => {
|
|
177
|
+
const c = ensure(sid, cwd);
|
|
178
|
+
if (c.busy) return;
|
|
179
|
+
c.busy = true;
|
|
180
|
+
send(sid, 'prompt');
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const call = async (sid: string, cwd: string, msg: Record<string, unknown>, timeoutMs?: number) => {
|
|
184
|
+
try {
|
|
185
|
+
const c = ensure(sid, cwd);
|
|
186
|
+
await c.ready;
|
|
187
|
+
return JSON.stringify(await rpc(c, msg, timeoutMs));
|
|
188
|
+
} catch (e) {
|
|
189
|
+
return JSON.stringify({ error: e instanceof Error ? e.message : String(e) });
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const s = tool.schema;
|
|
194
|
+
const refs = s.array(s.string()).optional();
|
|
195
|
+
const ctxCall = (msg: Record<string, unknown>, ctx: { sessionID: string; directory: string }, t?: number) =>
|
|
196
|
+
call(ctx.sessionID, ctx.directory, msg, t);
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
event: async ({ event }) => {
|
|
200
|
+
try {
|
|
201
|
+
const e: any = event;
|
|
202
|
+
const p = e.properties ?? {};
|
|
203
|
+
switch (e.type) {
|
|
204
|
+
case 'session.created':
|
|
205
|
+
ensure(p.info.id, p.info.directory);
|
|
206
|
+
break;
|
|
207
|
+
case 'session.status':
|
|
208
|
+
if (p.status?.type === 'busy') setBusy(p.sessionID);
|
|
209
|
+
break;
|
|
210
|
+
case 'session.idle': {
|
|
211
|
+
const c = ensure(p.sessionID);
|
|
212
|
+
c.busy = false;
|
|
213
|
+
send(p.sessionID, 'stop');
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
case 'session.deleted': {
|
|
217
|
+
const c = conns.get(p.info.id);
|
|
218
|
+
if (!c) break;
|
|
219
|
+
c.alive = false;
|
|
220
|
+
if (c.retry) clearTimeout(c.retry);
|
|
221
|
+
const end = c.sock ? lifecycle(c, 'session-end').catch(() => {}) : Promise.resolve();
|
|
222
|
+
end.finally(() => drop(c));
|
|
223
|
+
conns.delete(p.info.id);
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
} catch {
|
|
228
|
+
// never throw out of a hook
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
'chat.message': async ({ sessionID }) => {
|
|
232
|
+
try {
|
|
233
|
+
setBusy(sessionID);
|
|
234
|
+
} catch {
|
|
235
|
+
// never throw out of a hook
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
tool: {
|
|
239
|
+
murmur_send: tool({
|
|
240
|
+
description: 'Send a message to murmur peers or groups, optionally into an existing thread.',
|
|
241
|
+
args: {
|
|
242
|
+
to: s.array(s.string()),
|
|
243
|
+
body: s.string(),
|
|
244
|
+
threadId: s.number().optional(),
|
|
245
|
+
references: refs,
|
|
246
|
+
mode: s.enum(['push', 'notify', 'silent']).optional(),
|
|
247
|
+
},
|
|
248
|
+
execute: (a, ctx) =>
|
|
249
|
+
ctxCall({ type: 'send', threadId: a.threadId ?? null, to: a.to, body: a.body, references: a.references, mode: a.mode }, ctx),
|
|
250
|
+
}),
|
|
251
|
+
murmur_reply: tool({
|
|
252
|
+
description: 'Reply to a murmur message; the reply reaches everyone in the thread, so start the body with AGREE, DISAGREE or INFO.',
|
|
253
|
+
args: {
|
|
254
|
+
messageId: s.number(),
|
|
255
|
+
body: s.string(),
|
|
256
|
+
verdict: s.enum(['agree', 'disagree', 'info']).optional(),
|
|
257
|
+
references: refs,
|
|
258
|
+
},
|
|
259
|
+
execute: (a, ctx) =>
|
|
260
|
+
ctxCall({ type: 'reply', messageId: a.messageId, body: a.body, verdict: a.verdict, references: a.references }, ctx),
|
|
261
|
+
}),
|
|
262
|
+
murmur_thread: tool({
|
|
263
|
+
description: 'Read a murmur thread, optionally only messages after a sequence number.',
|
|
264
|
+
args: { threadId: s.number(), sinceSeq: s.number().optional(), full: s.boolean().optional() },
|
|
265
|
+
execute: (a, ctx) =>
|
|
266
|
+
ctxCall({ type: 'thread', threadId: a.threadId, sinceSeq: a.sinceSeq ?? null, full: a.full ?? false }, ctx),
|
|
267
|
+
}),
|
|
268
|
+
murmur_wait: tool({
|
|
269
|
+
description: 'Wait until all or any recipients of a murmur thread have replied.',
|
|
270
|
+
args: {
|
|
271
|
+
threadId: s.number(),
|
|
272
|
+
until: s.enum(['all_replied', 'any_replied']),
|
|
273
|
+
timeoutMs: s.number().optional(),
|
|
274
|
+
},
|
|
275
|
+
execute: (a, ctx) => {
|
|
276
|
+
const timeoutMs = Math.min(Math.max(a.timeoutMs ?? 120000, 0), 300000);
|
|
277
|
+
return ctxCall({ type: 'wait', threadId: a.threadId, until: a.until, timeoutMs }, ctx, timeoutMs + 10000);
|
|
278
|
+
},
|
|
279
|
+
}),
|
|
280
|
+
murmur_status: tool({
|
|
281
|
+
description: 'Show murmur peers, presence and delivery status.',
|
|
282
|
+
args: {},
|
|
283
|
+
execute: (_a, ctx) => ctxCall({ type: 'status' }, ctx),
|
|
284
|
+
}),
|
|
285
|
+
murmur_groups: tool({
|
|
286
|
+
description: 'Create, list, change or delete murmur groups.',
|
|
287
|
+
args: {
|
|
288
|
+
action: s.enum(['create', 'list', 'add', 'remove', 'delete']),
|
|
289
|
+
name: s.string().optional(),
|
|
290
|
+
members: s.array(s.string()).optional(),
|
|
291
|
+
},
|
|
292
|
+
execute: (a, ctx) => ctxCall({ type: 'groups', action: a.action, name: a.name, members: a.members }, ctx),
|
|
293
|
+
}),
|
|
294
|
+
murmur_name: tool({
|
|
295
|
+
description: 'Rename this session on the murmur bus.',
|
|
296
|
+
args: { handle: s.string() },
|
|
297
|
+
execute: (a, ctx) =>
|
|
298
|
+
ctxCall({ type: 'register', clientKind: 'adapter', host, handle: a.handle }, ctx),
|
|
299
|
+
}),
|
|
300
|
+
},
|
|
301
|
+
};
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
export default plugin;
|
package/bin/murmur.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Wrapper entry so the npm-link symlink always works: node resolves argv[1]
|
|
3
|
+
// through the symlink while import.meta.url keeps the real path, which breaks
|
|
4
|
+
// the in-module entry guard of dist/cli.js. This wrapper calls runCli
|
|
5
|
+
// explicitly instead of relying on that guard.
|
|
6
|
+
// The real command may start the broker on demand; tests and library
|
|
7
|
+
// callers never set this, so they never spawn one.
|
|
8
|
+
process.env.MURMUR_AUTOSTART ??= '1';
|
|
9
|
+
import { runCli } from '../dist/cli.js';
|
|
10
|
+
const code = await runCli(process.argv.slice(2));
|
|
11
|
+
process.exit(code);
|