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/store.js
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
import { chmodSync, existsSync } from 'node:fs';
|
|
2
|
+
import Database from 'better-sqlite3';
|
|
3
|
+
const SCHEMA_SQL = `
|
|
4
|
+
CREATE TABLE IF NOT EXISTS peers (
|
|
5
|
+
handle TEXT PRIMARY KEY,
|
|
6
|
+
display TEXT,
|
|
7
|
+
role TEXT,
|
|
8
|
+
is_owner INTEGER NOT NULL DEFAULT 0,
|
|
9
|
+
created_at INTEGER NOT NULL,
|
|
10
|
+
last_seen_at INTEGER,
|
|
11
|
+
last_hook_activity_at INTEGER,
|
|
12
|
+
hook_state TEXT
|
|
13
|
+
);
|
|
14
|
+
CREATE TABLE IF NOT EXISTS groups (
|
|
15
|
+
name TEXT PRIMARY KEY,
|
|
16
|
+
created_at INTEGER NOT NULL
|
|
17
|
+
);
|
|
18
|
+
CREATE TABLE IF NOT EXISTS group_members (
|
|
19
|
+
group_name TEXT NOT NULL,
|
|
20
|
+
handle TEXT NOT NULL,
|
|
21
|
+
added_at INTEGER NOT NULL,
|
|
22
|
+
PRIMARY KEY (group_name, handle)
|
|
23
|
+
);
|
|
24
|
+
CREATE TABLE IF NOT EXISTS threads (
|
|
25
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
26
|
+
created_at INTEGER NOT NULL,
|
|
27
|
+
created_by TEXT NOT NULL
|
|
28
|
+
);
|
|
29
|
+
CREATE TABLE IF NOT EXISTS messages (
|
|
30
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
31
|
+
thread_id INTEGER NOT NULL,
|
|
32
|
+
seq INTEGER NOT NULL,
|
|
33
|
+
sender TEXT NOT NULL,
|
|
34
|
+
body TEXT NOT NULL,
|
|
35
|
+
delivered_body TEXT NOT NULL,
|
|
36
|
+
truncated INTEGER NOT NULL,
|
|
37
|
+
refs_json TEXT NOT NULL,
|
|
38
|
+
mode TEXT NOT NULL,
|
|
39
|
+
is_reply INTEGER NOT NULL,
|
|
40
|
+
in_reply_to INTEGER,
|
|
41
|
+
verdict TEXT,
|
|
42
|
+
created_at INTEGER NOT NULL,
|
|
43
|
+
UNIQUE (thread_id, seq)
|
|
44
|
+
);
|
|
45
|
+
CREATE TABLE IF NOT EXISTS deliveries (
|
|
46
|
+
message_id INTEGER NOT NULL,
|
|
47
|
+
recipient TEXT NOT NULL,
|
|
48
|
+
state TEXT NOT NULL,
|
|
49
|
+
sent_at INTEGER,
|
|
50
|
+
delivered_at INTEGER,
|
|
51
|
+
acked_at INTEGER,
|
|
52
|
+
replied_at INTEGER,
|
|
53
|
+
latency_ms INTEGER,
|
|
54
|
+
PRIMARY KEY (message_id, recipient)
|
|
55
|
+
);
|
|
56
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
57
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
58
|
+
client TEXT NOT NULL,
|
|
59
|
+
cli_session_id TEXT,
|
|
60
|
+
handle TEXT NOT NULL,
|
|
61
|
+
host_pid INTEGER NOT NULL,
|
|
62
|
+
host_start TEXT NOT NULL,
|
|
63
|
+
transcript_path TEXT,
|
|
64
|
+
cwd TEXT,
|
|
65
|
+
state TEXT,
|
|
66
|
+
state_at INTEGER,
|
|
67
|
+
started_at INTEGER NOT NULL,
|
|
68
|
+
updated_at INTEGER NOT NULL,
|
|
69
|
+
UNIQUE (client, cli_session_id)
|
|
70
|
+
);
|
|
71
|
+
CREATE TABLE IF NOT EXISTS native_transports (
|
|
72
|
+
owner TEXT PRIMARY KEY,
|
|
73
|
+
socket_path TEXT NOT NULL,
|
|
74
|
+
token TEXT NOT NULL,
|
|
75
|
+
registered_at INTEGER NOT NULL
|
|
76
|
+
);
|
|
77
|
+
CREATE INDEX IF NOT EXISTS idx_deliveries_recipient_state ON deliveries (recipient, state);
|
|
78
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_handle ON sessions (handle, updated_at, id);
|
|
79
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_host ON sessions (host_pid, host_start, updated_at, id);
|
|
80
|
+
`;
|
|
81
|
+
// Delivery states that still count as unread for inbox purposes.
|
|
82
|
+
const UNREAD_STATES_SQL = "('queued','sent','delivered')";
|
|
83
|
+
function toPeerRow(r) {
|
|
84
|
+
return {
|
|
85
|
+
handle: r.handle,
|
|
86
|
+
display: r.display,
|
|
87
|
+
role: r.role,
|
|
88
|
+
isOwner: r.is_owner === 1,
|
|
89
|
+
createdAt: r.created_at,
|
|
90
|
+
lastSeenAt: r.last_seen_at,
|
|
91
|
+
lastHookActivityAt: r.last_hook_activity_at,
|
|
92
|
+
hookState: r.hook_state,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function toSessionRow(r) {
|
|
96
|
+
return {
|
|
97
|
+
id: r.id,
|
|
98
|
+
client: r.client,
|
|
99
|
+
cliSessionId: r.cli_session_id,
|
|
100
|
+
handle: r.handle,
|
|
101
|
+
hostPid: r.host_pid,
|
|
102
|
+
hostStart: r.host_start,
|
|
103
|
+
transcriptPath: r.transcript_path,
|
|
104
|
+
cwd: r.cwd,
|
|
105
|
+
state: r.state,
|
|
106
|
+
stateAt: r.state_at,
|
|
107
|
+
startedAt: r.started_at,
|
|
108
|
+
updatedAt: r.updated_at,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function toMessageRow(r) {
|
|
112
|
+
return {
|
|
113
|
+
id: r.id,
|
|
114
|
+
threadId: r.thread_id,
|
|
115
|
+
seq: r.seq,
|
|
116
|
+
sender: r.sender,
|
|
117
|
+
body: r.body,
|
|
118
|
+
deliveredBody: r.delivered_body,
|
|
119
|
+
truncated: r.truncated === 1,
|
|
120
|
+
refsJson: r.refs_json,
|
|
121
|
+
mode: r.mode,
|
|
122
|
+
isReply: r.is_reply === 1,
|
|
123
|
+
inReplyTo: r.in_reply_to,
|
|
124
|
+
verdict: r.verdict,
|
|
125
|
+
createdAt: r.created_at,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function toDeliveryRow(r) {
|
|
129
|
+
return {
|
|
130
|
+
messageId: r.message_id,
|
|
131
|
+
recipient: r.recipient,
|
|
132
|
+
state: r.state,
|
|
133
|
+
sentAt: r.sent_at,
|
|
134
|
+
deliveredAt: r.delivered_at,
|
|
135
|
+
ackedAt: r.acked_at,
|
|
136
|
+
repliedAt: r.replied_at,
|
|
137
|
+
latencyMs: r.latency_ms,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function placeholders(n) {
|
|
141
|
+
// Array.from with length 0 yields an empty list and join yields '', so the
|
|
142
|
+
// empty case needs no separate branch.
|
|
143
|
+
return Array.from({ length: n }, () => '?').join(', ');
|
|
144
|
+
}
|
|
145
|
+
export class Store {
|
|
146
|
+
db;
|
|
147
|
+
// Serialized once in the constructor: appendMessage runs inside a
|
|
148
|
+
// transaction that computes the per-thread seq.
|
|
149
|
+
appendMessageTx;
|
|
150
|
+
addDeliveriesTx;
|
|
151
|
+
// Prepared once per SQL text. Preparing costs about as much as running a
|
|
152
|
+
// point query, and a send runs dozens of them.
|
|
153
|
+
statements = new Map();
|
|
154
|
+
constructor(db) {
|
|
155
|
+
this.db = db;
|
|
156
|
+
this.appendMessageTx = db.transaction((arg) => this.appendMessageInner(arg));
|
|
157
|
+
this.addDeliveriesTx = db.transaction((messageId, recipients) => {
|
|
158
|
+
const insert = this.stmt(`INSERT INTO deliveries (message_id, recipient, state, sent_at, delivered_at, acked_at, replied_at, latency_ms)
|
|
159
|
+
VALUES (?, ?, 'queued', NULL, NULL, NULL, NULL, NULL)`);
|
|
160
|
+
for (const recipient of recipients) {
|
|
161
|
+
insert.run(messageId, recipient);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
stmt(sql) {
|
|
166
|
+
let statement = this.statements.get(sql);
|
|
167
|
+
if (statement === undefined) {
|
|
168
|
+
statement = this.db.prepare(sql);
|
|
169
|
+
this.statements.set(sql, statement);
|
|
170
|
+
}
|
|
171
|
+
return statement;
|
|
172
|
+
}
|
|
173
|
+
static open(dbPath) {
|
|
174
|
+
const db = new Database(dbPath);
|
|
175
|
+
db.pragma('journal_mode = WAL');
|
|
176
|
+
db.pragma('foreign_keys = ON');
|
|
177
|
+
db.exec(SCHEMA_SQL);
|
|
178
|
+
// The database holds every message and each session's native messaging
|
|
179
|
+
// token: owner-only, like the token file.
|
|
180
|
+
for (const file of [dbPath, dbPath + '-wal', dbPath + '-shm']) {
|
|
181
|
+
if (existsSync(file)) {
|
|
182
|
+
chmodSync(file, 0o600);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return new Store(db);
|
|
186
|
+
}
|
|
187
|
+
close() {
|
|
188
|
+
this.db.close();
|
|
189
|
+
}
|
|
190
|
+
// Inserts the peer or refreshes last_seen_at on the existing row; a null
|
|
191
|
+
// display or role keeps the stored one. is_owner can only flip false to
|
|
192
|
+
// true, never back.
|
|
193
|
+
upsertPeer(handle, display, role, isOwner, now) {
|
|
194
|
+
this.stmt(`INSERT INTO peers (handle, display, role, is_owner, created_at, last_seen_at)
|
|
195
|
+
VALUES (?, ?, ?, ?, ?, ?)
|
|
196
|
+
ON CONFLICT(handle) DO UPDATE SET
|
|
197
|
+
display = COALESCE(excluded.display, peers.display),
|
|
198
|
+
role = COALESCE(excluded.role, peers.role),
|
|
199
|
+
last_seen_at = excluded.last_seen_at,
|
|
200
|
+
is_owner = MAX(peers.is_owner, excluded.is_owner)`).run(handle, display, role, isOwner ? 1 : 0, now, now);
|
|
201
|
+
}
|
|
202
|
+
// A handle is a name for one identity, so a rename moves every reference
|
|
203
|
+
// with it (peer row, sessions, native transport, group membership,
|
|
204
|
+
// deliveries, sent messages, created threads), atomically. Throws a plain
|
|
205
|
+
// Error when a peer already occupies the target handle.
|
|
206
|
+
renamePeer(oldHandle, newHandle) {
|
|
207
|
+
if (oldHandle !== newHandle) {
|
|
208
|
+
const dup = this.stmt('SELECT handle FROM peers WHERE handle = ?').get(newHandle);
|
|
209
|
+
if (dup !== undefined) {
|
|
210
|
+
throw new Error('peer handle already exists: ' + newHandle);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const move = this.db.transaction(() => {
|
|
214
|
+
this.stmt('UPDATE peers SET handle = ? WHERE handle = ?').run(newHandle, oldHandle);
|
|
215
|
+
this.stmt('UPDATE native_transports SET owner = ? WHERE owner = ?').run(newHandle, oldHandle);
|
|
216
|
+
this.stmt('UPDATE sessions SET handle = ? WHERE handle = ?').run(newHandle, oldHandle);
|
|
217
|
+
this.stmt('UPDATE group_members SET handle = ? WHERE handle = ?').run(newHandle, oldHandle);
|
|
218
|
+
this.stmt('UPDATE deliveries SET recipient = ? WHERE recipient = ?').run(newHandle, oldHandle);
|
|
219
|
+
this.stmt('UPDATE messages SET sender = ? WHERE sender = ?').run(newHandle, oldHandle);
|
|
220
|
+
this.stmt('UPDATE threads SET created_by = ? WHERE created_by = ?').run(newHandle, oldHandle);
|
|
221
|
+
});
|
|
222
|
+
move();
|
|
223
|
+
}
|
|
224
|
+
getPeer(handle) {
|
|
225
|
+
const rec = this.stmt('SELECT * FROM peers WHERE handle = ?').get(handle);
|
|
226
|
+
return rec === undefined ? null : toPeerRow(rec);
|
|
227
|
+
}
|
|
228
|
+
listPeers() {
|
|
229
|
+
const recs = this.stmt('SELECT * FROM peers ORDER BY handle ASC').all();
|
|
230
|
+
return recs.map(toPeerRow);
|
|
231
|
+
}
|
|
232
|
+
createGroup(name, now) {
|
|
233
|
+
this.stmt('INSERT INTO groups (name, created_at) VALUES (?, ?)').run(name, now);
|
|
234
|
+
}
|
|
235
|
+
// Deletes the group and its membership rows together so a recreated group
|
|
236
|
+
// of the same name does not inherit stale members.
|
|
237
|
+
deleteGroup(name) {
|
|
238
|
+
const drop = this.db.transaction(() => {
|
|
239
|
+
this.stmt('DELETE FROM group_members WHERE group_name = ?').run(name);
|
|
240
|
+
this.stmt('DELETE FROM groups WHERE name = ?').run(name);
|
|
241
|
+
});
|
|
242
|
+
drop();
|
|
243
|
+
}
|
|
244
|
+
addMember(group, handle, now) {
|
|
245
|
+
this.stmt('INSERT INTO group_members (group_name, handle, added_at) VALUES (?, ?, ?)').run(group, handle, now);
|
|
246
|
+
}
|
|
247
|
+
removeMember(group, handle) {
|
|
248
|
+
this.stmt('DELETE FROM group_members WHERE group_name = ? AND handle = ?').run(group, handle);
|
|
249
|
+
}
|
|
250
|
+
listGroups() {
|
|
251
|
+
const groups = this.stmt('SELECT name, created_at FROM groups ORDER BY name ASC').all();
|
|
252
|
+
const membersStmt = this.stmt('SELECT handle FROM group_members WHERE group_name = ? ORDER BY added_at ASC, handle ASC');
|
|
253
|
+
return groups.map((g) => {
|
|
254
|
+
const members = membersStmt.all(g.name);
|
|
255
|
+
return { name: g.name, createdAt: g.created_at, members: members.map((m) => m.handle) };
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
createThread(createdBy, now) {
|
|
259
|
+
const info = this.stmt('INSERT INTO threads (created_at, created_by) VALUES (?, ?)').run(now, createdBy);
|
|
260
|
+
return Number(info.lastInsertRowid);
|
|
261
|
+
}
|
|
262
|
+
appendMessage(arg) {
|
|
263
|
+
return this.appendMessageTx(arg);
|
|
264
|
+
}
|
|
265
|
+
appendMessageInner(arg) {
|
|
266
|
+
const seqRec = this.stmt('SELECT COALESCE(MAX(seq), -1) + 1 AS next_seq FROM messages WHERE thread_id = ?')
|
|
267
|
+
.get(arg.threadId);
|
|
268
|
+
const seq = seqRec === undefined ? 0 : seqRec.next_seq;
|
|
269
|
+
const info = this.stmt(`INSERT INTO messages (thread_id, seq, sender, body, delivered_body, truncated, refs_json, mode, is_reply, in_reply_to, verdict, created_at)
|
|
270
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`).run(arg.threadId, seq, arg.sender, arg.body, arg.deliveredBody, arg.truncated ? 1 : 0, JSON.stringify(arg.refs), arg.mode, arg.isReply ? 1 : 0, arg.inReplyTo, arg.verdict, arg.createdAt);
|
|
271
|
+
return { id: Number(info.lastInsertRowid), seq };
|
|
272
|
+
}
|
|
273
|
+
addDeliveries(messageId, recipients, now) {
|
|
274
|
+
// now is accepted for interface symmetry; queued rows carry no timestamp.
|
|
275
|
+
void now;
|
|
276
|
+
this.addDeliveriesTx(messageId, recipients);
|
|
277
|
+
}
|
|
278
|
+
// Applies only the keys present in the patch; absent keys keep their
|
|
279
|
+
// current values.
|
|
280
|
+
updateDelivery(messageId, recipient, patch) {
|
|
281
|
+
const sets = [];
|
|
282
|
+
const params = [];
|
|
283
|
+
if (patch.state !== undefined) {
|
|
284
|
+
sets.push('state = ?');
|
|
285
|
+
params.push(patch.state);
|
|
286
|
+
}
|
|
287
|
+
if (patch.sentAt !== undefined) {
|
|
288
|
+
sets.push('sent_at = ?');
|
|
289
|
+
params.push(patch.sentAt);
|
|
290
|
+
}
|
|
291
|
+
if (patch.deliveredAt !== undefined) {
|
|
292
|
+
sets.push('delivered_at = ?');
|
|
293
|
+
params.push(patch.deliveredAt);
|
|
294
|
+
}
|
|
295
|
+
if (patch.ackedAt !== undefined) {
|
|
296
|
+
sets.push('acked_at = ?');
|
|
297
|
+
params.push(patch.ackedAt);
|
|
298
|
+
}
|
|
299
|
+
if (patch.repliedAt !== undefined) {
|
|
300
|
+
sets.push('replied_at = ?');
|
|
301
|
+
params.push(patch.repliedAt);
|
|
302
|
+
}
|
|
303
|
+
if (patch.latencyMs !== undefined) {
|
|
304
|
+
sets.push('latency_ms = ?');
|
|
305
|
+
params.push(patch.latencyMs);
|
|
306
|
+
}
|
|
307
|
+
if (sets.length === 0) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
params.push(messageId, recipient);
|
|
311
|
+
this.stmt('UPDATE deliveries SET ' + sets.join(', ') + ' WHERE message_id = ? AND recipient = ?')
|
|
312
|
+
.run(...params);
|
|
313
|
+
}
|
|
314
|
+
getDeliveries(messageId) {
|
|
315
|
+
const recs = this.stmt('SELECT * FROM deliveries WHERE message_id = ? ORDER BY recipient ASC')
|
|
316
|
+
.all(messageId);
|
|
317
|
+
return recs.map(toDeliveryRow);
|
|
318
|
+
}
|
|
319
|
+
threadMessages(threadId, sinceSeq) {
|
|
320
|
+
const sql = 'SELECT * FROM messages WHERE thread_id = ?' +
|
|
321
|
+
(sinceSeq === null ? '' : ' AND seq > ?') +
|
|
322
|
+
' ORDER BY seq ASC';
|
|
323
|
+
const stmt = this.stmt(sql);
|
|
324
|
+
const recs = (sinceSeq === null ? stmt.all(threadId) : stmt.all(threadId, sinceSeq));
|
|
325
|
+
return recs.map(toMessageRow);
|
|
326
|
+
}
|
|
327
|
+
latestAsk(threadId) {
|
|
328
|
+
const rec = this.stmt('SELECT * FROM messages WHERE thread_id = ? AND is_reply = 0 ORDER BY seq DESC LIMIT 1')
|
|
329
|
+
.get(threadId);
|
|
330
|
+
return rec === undefined ? null : toMessageRow(rec);
|
|
331
|
+
}
|
|
332
|
+
messageById(id) {
|
|
333
|
+
const rec = this.stmt('SELECT * FROM messages WHERE id = ?').get(id);
|
|
334
|
+
return rec === undefined ? null : toMessageRow(rec);
|
|
335
|
+
}
|
|
336
|
+
messageCount(threadId) {
|
|
337
|
+
const rec = this.stmt('SELECT COUNT(*) AS n FROM messages WHERE thread_id = ?').get(threadId);
|
|
338
|
+
return rec === undefined ? 0 : rec.n;
|
|
339
|
+
}
|
|
340
|
+
markDelivered(messageId, recipient, now) {
|
|
341
|
+
this.stmt("UPDATE deliveries SET state = 'delivered', delivered_at = ? WHERE message_id = ? AND recipient = ?")
|
|
342
|
+
.run(now, messageId, recipient);
|
|
343
|
+
}
|
|
344
|
+
markAcked(messageId, recipient, now) {
|
|
345
|
+
this.stmt("UPDATE deliveries SET state = 'acknowledged', acked_at = ? WHERE message_id = ? AND recipient = ?")
|
|
346
|
+
.run(now, messageId, recipient);
|
|
347
|
+
}
|
|
348
|
+
markReplied(messageId, recipient, now, latencyMs) {
|
|
349
|
+
this.stmt("UPDATE deliveries SET state = 'replied', replied_at = ?, latency_ms = ? WHERE message_id = ? AND recipient = ?").run(now, latencyMs, messageId, recipient);
|
|
350
|
+
}
|
|
351
|
+
// Messages delivered to this handle. unreadTotal is the handle-wide count
|
|
352
|
+
// of unread deliveries; grouping into threads is the caller's job.
|
|
353
|
+
inboxFor(handle, unreadOnly) {
|
|
354
|
+
const unreadTotal = this.deliveryStatesForRecipient(handle, ['queued', 'sent', 'delivered']);
|
|
355
|
+
const sql = 'SELECT m.* FROM messages m JOIN deliveries d ON d.message_id = m.id WHERE d.recipient = ?' +
|
|
356
|
+
(unreadOnly ? ' AND d.state IN ' + UNREAD_STATES_SQL : '') +
|
|
357
|
+
' ORDER BY m.created_at ASC, m.id ASC';
|
|
358
|
+
const recs = this.stmt(sql).all(handle);
|
|
359
|
+
return { messages: recs.map(toMessageRow), unreadTotal };
|
|
360
|
+
}
|
|
361
|
+
// Returns only the requested messages that also have a delivery row for
|
|
362
|
+
// this recipient, so a peer cannot read messages it was not addressed in.
|
|
363
|
+
readMessages(handle, ids) {
|
|
364
|
+
if (ids.length === 0) {
|
|
365
|
+
return [];
|
|
366
|
+
}
|
|
367
|
+
// The SQL text varies with the id count, so it is not cached.
|
|
368
|
+
const recs = this.db.prepare('SELECT m.* FROM messages m JOIN deliveries d ON d.message_id = m.id ' +
|
|
369
|
+
'WHERE d.recipient = ? AND m.id IN (' + placeholders(ids.length) + ') ORDER BY m.id ASC').all(handle, ...ids);
|
|
370
|
+
return recs.map(toMessageRow);
|
|
371
|
+
}
|
|
372
|
+
replyLatencies(handle) {
|
|
373
|
+
const recs = this.stmt('SELECT latency_ms FROM deliveries WHERE recipient = ? AND latency_ms IS NOT NULL')
|
|
374
|
+
.all(handle);
|
|
375
|
+
return recs.map((r) => r.latency_ms).filter((v) => v !== null);
|
|
376
|
+
}
|
|
377
|
+
// Queued push-mode messages for this recipient, newest first. A sent
|
|
378
|
+
// delivery already left through a transport, so pulling it again would
|
|
379
|
+
// put it in the session's context twice.
|
|
380
|
+
pendingPushFor(handle, limit) {
|
|
381
|
+
const recs = this.stmt("SELECT m.* FROM messages m JOIN deliveries d ON d.message_id = m.id " +
|
|
382
|
+
"WHERE d.recipient = ? AND d.state = 'queued' AND m.mode = 'push' " +
|
|
383
|
+
'ORDER BY m.created_at DESC, m.id DESC LIMIT ?').all(handle, limit);
|
|
384
|
+
return recs.map((r) => ({ message: toMessageRow(r) }));
|
|
385
|
+
}
|
|
386
|
+
deliveryStatesForRecipient(recipient, states) {
|
|
387
|
+
if (states.length === 0) {
|
|
388
|
+
return 0;
|
|
389
|
+
}
|
|
390
|
+
const rec = this.stmt('SELECT COUNT(*) AS n FROM deliveries WHERE recipient = ? AND state IN (' + placeholders(states.length) + ')').get(recipient, ...states);
|
|
391
|
+
return rec === undefined ? 0 : rec.n;
|
|
392
|
+
}
|
|
393
|
+
// Every recipient that has a delivery row for any message in the thread.
|
|
394
|
+
threadRecipientHandles(threadId) {
|
|
395
|
+
const recs = this.stmt('SELECT DISTINCT d.recipient FROM deliveries d JOIN messages m ON m.id = d.message_id ' +
|
|
396
|
+
'WHERE m.thread_id = ? ORDER BY d.recipient ASC').all(threadId);
|
|
397
|
+
return recs.map((r) => r.recipient);
|
|
398
|
+
}
|
|
399
|
+
sessionByCliId(client, cliSessionId) {
|
|
400
|
+
const rec = this.stmt('SELECT * FROM sessions WHERE client = ? AND cli_session_id = ?')
|
|
401
|
+
.get(client, cliSessionId);
|
|
402
|
+
return rec === undefined ? null : toSessionRow(rec);
|
|
403
|
+
}
|
|
404
|
+
// The newest session recorded for this exact host process.
|
|
405
|
+
sessionByHost(hostPid, hostStart) {
|
|
406
|
+
const rec = this.stmt('SELECT * FROM sessions WHERE host_pid = ? AND host_start = ? ORDER BY updated_at DESC, id DESC LIMIT 1')
|
|
407
|
+
.get(hostPid, hostStart);
|
|
408
|
+
return rec === undefined ? null : toSessionRow(rec);
|
|
409
|
+
}
|
|
410
|
+
// Points one session at another, existing handle and moves its native
|
|
411
|
+
// transport along.
|
|
412
|
+
reassignSession(id, fromHandle, toHandle) {
|
|
413
|
+
const move = this.db.transaction(() => {
|
|
414
|
+
this.stmt('UPDATE sessions SET handle = ? WHERE id = ?').run(toHandle, id);
|
|
415
|
+
this.stmt('DELETE FROM native_transports WHERE owner = ?').run(toHandle);
|
|
416
|
+
this.stmt('UPDATE native_transports SET owner = ? WHERE owner = ?').run(toHandle, fromHandle);
|
|
417
|
+
});
|
|
418
|
+
move();
|
|
419
|
+
}
|
|
420
|
+
// True when anything still references the handle besides its peer row and
|
|
421
|
+
// sessions: messages, deliveries or group membership.
|
|
422
|
+
peerHasHistory(handle) {
|
|
423
|
+
const rec = this.stmt(`SELECT (SELECT COUNT(*) FROM messages WHERE sender = ?)
|
|
424
|
+
+ (SELECT COUNT(*) FROM deliveries WHERE recipient = ?)
|
|
425
|
+
+ (SELECT COUNT(*) FROM group_members WHERE handle = ?) AS n`).get(handle, handle, handle);
|
|
426
|
+
return rec.n > 0;
|
|
427
|
+
}
|
|
428
|
+
deletePeer(handle) {
|
|
429
|
+
const drop = this.db.transaction(() => {
|
|
430
|
+
this.stmt('DELETE FROM native_transports WHERE owner = ?').run(handle);
|
|
431
|
+
this.stmt('DELETE FROM peers WHERE handle = ?').run(handle);
|
|
432
|
+
});
|
|
433
|
+
drop();
|
|
434
|
+
}
|
|
435
|
+
sessionById(id) {
|
|
436
|
+
const rec = this.stmt('SELECT * FROM sessions WHERE id = ?').get(id);
|
|
437
|
+
return rec === undefined ? null : toSessionRow(rec);
|
|
438
|
+
}
|
|
439
|
+
sessionsForHandle(handle) {
|
|
440
|
+
const recs = this.stmt('SELECT * FROM sessions WHERE handle = ? ORDER BY updated_at DESC, id DESC')
|
|
441
|
+
.all(handle);
|
|
442
|
+
return recs.map(toSessionRow);
|
|
443
|
+
}
|
|
444
|
+
listSessions() {
|
|
445
|
+
const recs = this.stmt('SELECT * FROM sessions ORDER BY updated_at DESC, id DESC').all();
|
|
446
|
+
return recs.map(toSessionRow);
|
|
447
|
+
}
|
|
448
|
+
insertSession(arg) {
|
|
449
|
+
const info = this.stmt(`INSERT INTO sessions (client, cli_session_id, handle, host_pid, host_start, transcript_path, cwd, state, state_at, started_at, updated_at)
|
|
450
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, NULL, NULL, ?, ?)`).run(arg.client, arg.cliSessionId, arg.handle, arg.hostPid, arg.hostStart, arg.transcriptPath, arg.cwd, arg.now, arg.now);
|
|
451
|
+
return Number(info.lastInsertRowid);
|
|
452
|
+
}
|
|
453
|
+
// Applies only the keys present in the patch.
|
|
454
|
+
updateSession(id, patch) {
|
|
455
|
+
const columns = [
|
|
456
|
+
['client', 'client'],
|
|
457
|
+
['cliSessionId', 'cli_session_id'],
|
|
458
|
+
['hostPid', 'host_pid'],
|
|
459
|
+
['hostStart', 'host_start'],
|
|
460
|
+
['transcriptPath', 'transcript_path'],
|
|
461
|
+
['cwd', 'cwd'],
|
|
462
|
+
['state', 'state'],
|
|
463
|
+
['stateAt', 'state_at'],
|
|
464
|
+
['updatedAt', 'updated_at'],
|
|
465
|
+
];
|
|
466
|
+
const sets = [];
|
|
467
|
+
const params = [];
|
|
468
|
+
for (const [key, column] of columns) {
|
|
469
|
+
const value = patch[key];
|
|
470
|
+
if (value !== undefined) {
|
|
471
|
+
sets.push(column + ' = ?');
|
|
472
|
+
params.push(value);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
if (sets.length === 0) {
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
this.stmt('UPDATE sessions SET ' + sets.join(', ') + ' WHERE id = ?').run(...params, id);
|
|
479
|
+
}
|
|
480
|
+
upsertNativeTransport(owner, socketPath, token, now) {
|
|
481
|
+
this.stmt(`INSERT INTO native_transports (owner, socket_path, token, registered_at)
|
|
482
|
+
VALUES (?, ?, ?, ?)
|
|
483
|
+
ON CONFLICT(owner) DO UPDATE SET
|
|
484
|
+
socket_path = excluded.socket_path,
|
|
485
|
+
token = excluded.token,
|
|
486
|
+
registered_at = excluded.registered_at`).run(owner, socketPath, token, now);
|
|
487
|
+
}
|
|
488
|
+
getNativeTransport(owner) {
|
|
489
|
+
const rec = this.stmt('SELECT owner, socket_path, token FROM native_transports WHERE owner = ?')
|
|
490
|
+
.get(owner);
|
|
491
|
+
return rec === undefined ? null : { owner: rec.owner, socketPath: rec.socket_path, token: rec.token };
|
|
492
|
+
}
|
|
493
|
+
deleteNativeTransport(owner) {
|
|
494
|
+
this.stmt('DELETE FROM native_transports WHERE owner = ?').run(owner);
|
|
495
|
+
}
|
|
496
|
+
}
|
package/dist/wake.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
export const defaultWake = (session, text) => {
|
|
3
|
+
if (session.client !== 'codex' || session.cliSessionId === null) {
|
|
4
|
+
return Promise.resolve(false);
|
|
5
|
+
}
|
|
6
|
+
const args = ['queue', '--thread', session.cliSessionId, '--message', text];
|
|
7
|
+
return new Promise((resolve) => {
|
|
8
|
+
execFile('codex', args, { timeout: 10000 }, (err) => {
|
|
9
|
+
resolve(err === null);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "murmur-it",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Local, client-agnostic message bus for AI CLI sessions with group threads",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Renn F",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/rennf93/murmur.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://rennf93.github.io/murmur/",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/rennf93/murmur/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"ai",
|
|
17
|
+
"cli",
|
|
18
|
+
"agents",
|
|
19
|
+
"message-bus",
|
|
20
|
+
"mcp",
|
|
21
|
+
"claude-code",
|
|
22
|
+
"codex",
|
|
23
|
+
"gemini-cli",
|
|
24
|
+
"opencode",
|
|
25
|
+
"multi-agent"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"bin": {
|
|
29
|
+
"murmur": "bin/murmur.js"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"bin",
|
|
34
|
+
"adapters",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE",
|
|
37
|
+
"CHANGELOG.md"
|
|
38
|
+
],
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=20"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public",
|
|
44
|
+
"provenance": true
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsc -p tsconfig.json",
|
|
48
|
+
"test": "vitest run",
|
|
49
|
+
"coverage": "vitest run --coverage",
|
|
50
|
+
"prepublishOnly": "npm run build"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@modelcontextprotocol/sdk": "^1.12.0",
|
|
54
|
+
"better-sqlite3": "^12.2.0",
|
|
55
|
+
"zod": "^3.24.0"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
59
|
+
"@types/node": "^24.0.0",
|
|
60
|
+
"@vitest/coverage-v8": "^3.2.0",
|
|
61
|
+
"tsx": "^4.20.0",
|
|
62
|
+
"typescript": "^5.8.0",
|
|
63
|
+
"vitest": "^3.2.0"
|
|
64
|
+
}
|
|
65
|
+
}
|