peer-bridge 0.0.0-stage → 0.7.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/LICENSE +21 -0
- package/README.ko.md +202 -0
- package/README.md +242 -2
- package/adapters/antigravity/bin/agy-hook.mjs +24 -0
- package/adapters/antigravity/hooks.json +23 -0
- package/adapters/antigravity/mcp_config.json +8 -0
- package/adapters/antigravity/plugin.json +4 -0
- package/adapters/antigravity/rules/AGENTS.md +6 -0
- package/adapters/antigravity/skills/peer-bridge/SKILL.md +19 -0
- package/adapters/claude/.claude-plugin/plugin.json +5 -0
- package/adapters/claude/.mcp.json +8 -0
- package/adapters/claude/bin/cc-hook.mjs +20 -0
- package/adapters/claude/hooks/hooks.json +46 -0
- package/adapters/claude/skills/peer-bridge/SKILL.md +19 -0
- package/adapters/codex/bin/codex-hook.mjs +10 -0
- package/adapters/codex/config.toml.example +5 -0
- package/adapters/codex/hooks.json +44 -0
- package/adapters/codex/skills/peer-bridge/SKILL.md +12 -0
- package/adapters/gemini/bin/gemini-hook.mjs +10 -0
- package/adapters/gemini/settings.json +51 -0
- package/adapters/gemini/skills/peer-bridge/SKILL.md +12 -0
- package/adapters/paseo/SKILL.md +24 -0
- package/bin/pbridge-mcp.mjs +37 -0
- package/bin/pbridge-tui.mjs +7 -0
- package/bin/pbridge.mjs +5 -0
- package/bin/pbridged.mjs +16 -0
- package/package.json +57 -4
- package/scripts/postinstall.mjs +25 -0
- package/scripts/preuninstall.mjs +20 -0
- package/skills/peer-bridge/SKILL.md +70 -0
- package/src/adapters/antigravity/hooks.mjs +207 -0
- package/src/adapters/claude/hooks.mjs +204 -0
- package/src/adapters/codex/hooks.mjs +142 -0
- package/src/adapters/gemini/hooks.mjs +159 -0
- package/src/adapters/installer.mjs +328 -0
- package/src/cli/main.mjs +785 -0
- package/src/client/rpc.mjs +243 -0
- package/src/config.mjs +154 -0
- package/src/daemon/broker.mjs +1799 -0
- package/src/daemon/resolver.mjs +200 -0
- package/src/daemon/server.mjs +221 -0
- package/src/index.mjs +60 -0
- package/src/mcp/server.mjs +668 -0
- package/src/orchestrator/workflows.mjs +496 -0
- package/src/project.mjs +81 -0
- package/src/protocol.mjs +203 -0
- package/src/runtime/conv-api.mjs +101 -0
- package/src/runtime/host-discovery.mjs +635 -0
- package/src/runtime/native-claude.mjs +88 -0
- package/src/runtime/paseo.mjs +289 -0
- package/src/runtime/workers.mjs +751 -0
- package/src/skills.mjs +115 -0
- package/src/storage/db.mjs +704 -0
- package/src/tui/render.mjs +472 -0
- package/src/tui/viewer.mjs +443 -0
|
@@ -0,0 +1,704 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import { ensureHomeDir } from '../config.mjs';
|
|
3
|
+
import { isSameProjectScope } from '../project.mjs';
|
|
4
|
+
import { makeHandle, makeId, nowIso } from '../protocol.mjs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
|
|
7
|
+
const SCHEMA_SQL = `
|
|
8
|
+
CREATE TABLE IF NOT EXISTS peers (
|
|
9
|
+
peer_id TEXT PRIMARY KEY,
|
|
10
|
+
runtime TEXT NOT NULL,
|
|
11
|
+
name TEXT NOT NULL,
|
|
12
|
+
session_id TEXT,
|
|
13
|
+
origin TEXT NOT NULL,
|
|
14
|
+
spawned_by TEXT,
|
|
15
|
+
exec_mode TEXT NOT NULL,
|
|
16
|
+
state TEXT NOT NULL,
|
|
17
|
+
cwd TEXT,
|
|
18
|
+
model TEXT,
|
|
19
|
+
pid INTEGER,
|
|
20
|
+
pane_id TEXT,
|
|
21
|
+
ls_address TEXT,
|
|
22
|
+
csrf_token TEXT,
|
|
23
|
+
project_id TEXT,
|
|
24
|
+
metadata_json TEXT,
|
|
25
|
+
created_at TEXT NOT NULL,
|
|
26
|
+
updated_at TEXT NOT NULL,
|
|
27
|
+
closed_at TEXT
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
CREATE INDEX IF NOT EXISTS idx_peers_runtime_name ON peers(runtime, name);
|
|
31
|
+
CREATE INDEX IF NOT EXISTS idx_peers_session_id ON peers(session_id);
|
|
32
|
+
CREATE INDEX IF NOT EXISTS idx_peers_state ON peers(state);
|
|
33
|
+
|
|
34
|
+
CREATE TABLE IF NOT EXISTS messages (
|
|
35
|
+
msg_id TEXT PRIMARY KEY,
|
|
36
|
+
thread_id TEXT NOT NULL,
|
|
37
|
+
from_peer_id TEXT,
|
|
38
|
+
from_runtime TEXT,
|
|
39
|
+
from_name TEXT,
|
|
40
|
+
to_raw TEXT NOT NULL,
|
|
41
|
+
to_peer_id TEXT NOT NULL,
|
|
42
|
+
kind TEXT NOT NULL,
|
|
43
|
+
body TEXT NOT NULL,
|
|
44
|
+
reply_expected INTEGER NOT NULL,
|
|
45
|
+
hop_count INTEGER NOT NULL,
|
|
46
|
+
max_hops INTEGER NOT NULL,
|
|
47
|
+
delivery_mode TEXT NOT NULL,
|
|
48
|
+
status TEXT NOT NULL,
|
|
49
|
+
error TEXT,
|
|
50
|
+
created_at TEXT NOT NULL
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
CREATE INDEX IF NOT EXISTS idx_messages_thread ON messages(thread_id, created_at);
|
|
54
|
+
CREATE INDEX IF NOT EXISTS idx_messages_to ON messages(to_peer_id, status, created_at);
|
|
55
|
+
|
|
56
|
+
CREATE TABLE IF NOT EXISTS deliveries (
|
|
57
|
+
delivery_id TEXT PRIMARY KEY,
|
|
58
|
+
msg_id TEXT NOT NULL,
|
|
59
|
+
peer_id TEXT NOT NULL,
|
|
60
|
+
channel TEXT NOT NULL,
|
|
61
|
+
status TEXT NOT NULL,
|
|
62
|
+
attempt INTEGER NOT NULL,
|
|
63
|
+
error TEXT,
|
|
64
|
+
response_excerpt TEXT,
|
|
65
|
+
created_at TEXT NOT NULL,
|
|
66
|
+
delivered_at TEXT,
|
|
67
|
+
FOREIGN KEY (msg_id) REFERENCES messages(msg_id) ON DELETE CASCADE
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
CREATE INDEX IF NOT EXISTS idx_deliveries_peer_status ON deliveries(peer_id, status);
|
|
71
|
+
CREATE INDEX IF NOT EXISTS idx_deliveries_msg ON deliveries(msg_id);
|
|
72
|
+
|
|
73
|
+
CREATE TABLE IF NOT EXISTS thread_state (
|
|
74
|
+
thread_id TEXT PRIMARY KEY,
|
|
75
|
+
last_hop INTEGER NOT NULL,
|
|
76
|
+
max_hops INTEGER NOT NULL,
|
|
77
|
+
no_reply_from_peer TEXT,
|
|
78
|
+
blocked_reason TEXT,
|
|
79
|
+
updated_at TEXT NOT NULL
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
CREATE TABLE IF NOT EXISTS tasks (
|
|
83
|
+
request_id TEXT PRIMARY KEY,
|
|
84
|
+
workflow_id TEXT,
|
|
85
|
+
step_key TEXT,
|
|
86
|
+
thread_id TEXT NOT NULL,
|
|
87
|
+
msg_id TEXT,
|
|
88
|
+
from_handle TEXT,
|
|
89
|
+
callback_to TEXT,
|
|
90
|
+
to_peer_id TEXT NOT NULL,
|
|
91
|
+
to_handle TEXT NOT NULL,
|
|
92
|
+
cwd TEXT,
|
|
93
|
+
title TEXT,
|
|
94
|
+
body TEXT NOT NULL,
|
|
95
|
+
expected_artifacts_json TEXT,
|
|
96
|
+
acceptance_criteria_json TEXT,
|
|
97
|
+
require_artifacts INTEGER NOT NULL DEFAULT 0,
|
|
98
|
+
notify_on_finish INTEGER NOT NULL DEFAULT 1,
|
|
99
|
+
callback_sent INTEGER NOT NULL DEFAULT 0,
|
|
100
|
+
status TEXT NOT NULL,
|
|
101
|
+
result_summary TEXT,
|
|
102
|
+
artifact_report_json TEXT,
|
|
103
|
+
error TEXT,
|
|
104
|
+
created_at TEXT NOT NULL,
|
|
105
|
+
updated_at TEXT NOT NULL,
|
|
106
|
+
completed_at TEXT
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
CREATE INDEX IF NOT EXISTS idx_tasks_workflow ON tasks(workflow_id);
|
|
110
|
+
CREATE INDEX IF NOT EXISTS idx_tasks_to_status ON tasks(to_peer_id, status);
|
|
111
|
+
|
|
112
|
+
CREATE TABLE IF NOT EXISTS workflows (
|
|
113
|
+
workflow_id TEXT PRIMARY KEY,
|
|
114
|
+
template TEXT NOT NULL,
|
|
115
|
+
name TEXT,
|
|
116
|
+
status TEXT NOT NULL,
|
|
117
|
+
coordinator_handle TEXT,
|
|
118
|
+
spec_json TEXT NOT NULL,
|
|
119
|
+
state_json TEXT NOT NULL,
|
|
120
|
+
result_json TEXT,
|
|
121
|
+
error TEXT,
|
|
122
|
+
created_at TEXT NOT NULL,
|
|
123
|
+
updated_at TEXT NOT NULL,
|
|
124
|
+
completed_at TEXT
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
CREATE INDEX IF NOT EXISTS idx_workflows_status ON workflows(status, created_at);
|
|
128
|
+
`;
|
|
129
|
+
|
|
130
|
+
function parseJsonSafe(raw, fallback = null) {
|
|
131
|
+
if (!raw) return fallback;
|
|
132
|
+
try {
|
|
133
|
+
return JSON.parse(raw);
|
|
134
|
+
} catch {
|
|
135
|
+
return fallback;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function deserializeTask(row) {
|
|
140
|
+
if (!row) return null;
|
|
141
|
+
return {
|
|
142
|
+
request_id: row.request_id,
|
|
143
|
+
workflow_id: row.workflow_id || null,
|
|
144
|
+
step_key: row.step_key || null,
|
|
145
|
+
thread_id: row.thread_id,
|
|
146
|
+
msg_id: row.msg_id || null,
|
|
147
|
+
from_handle: row.from_handle || null,
|
|
148
|
+
callback_to: row.callback_to || null,
|
|
149
|
+
to_peer_id: row.to_peer_id,
|
|
150
|
+
to_handle: row.to_handle,
|
|
151
|
+
cwd: row.cwd || null,
|
|
152
|
+
title: row.title || null,
|
|
153
|
+
body: row.body,
|
|
154
|
+
expected_artifacts: parseJsonSafe(row.expected_artifacts_json, []),
|
|
155
|
+
acceptance_criteria: parseJsonSafe(row.acceptance_criteria_json, []),
|
|
156
|
+
require_artifacts: Boolean(row.require_artifacts),
|
|
157
|
+
notify_on_finish: Boolean(row.notify_on_finish),
|
|
158
|
+
callback_sent: Boolean(row.callback_sent),
|
|
159
|
+
status: row.status,
|
|
160
|
+
result_summary: row.result_summary || null,
|
|
161
|
+
artifact_report: parseJsonSafe(row.artifact_report_json, null),
|
|
162
|
+
error: row.error || null,
|
|
163
|
+
created_at: row.created_at,
|
|
164
|
+
updated_at: row.updated_at,
|
|
165
|
+
completed_at: row.completed_at || null,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function deserializeWorkflow(row) {
|
|
170
|
+
if (!row) return null;
|
|
171
|
+
return {
|
|
172
|
+
workflow_id: row.workflow_id,
|
|
173
|
+
template: row.template,
|
|
174
|
+
name: row.name || null,
|
|
175
|
+
status: row.status,
|
|
176
|
+
coordinator_handle: row.coordinator_handle || null,
|
|
177
|
+
spec: parseJsonSafe(row.spec_json, {}),
|
|
178
|
+
state: parseJsonSafe(row.state_json, {}),
|
|
179
|
+
result: parseJsonSafe(row.result_json, null),
|
|
180
|
+
error: row.error || null,
|
|
181
|
+
created_at: row.created_at,
|
|
182
|
+
updated_at: row.updated_at,
|
|
183
|
+
completed_at: row.completed_at || null,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function deserializePeer(row) {
|
|
188
|
+
if (!row) return null;
|
|
189
|
+
let metadata = {};
|
|
190
|
+
if (row.metadata_json) {
|
|
191
|
+
try {
|
|
192
|
+
metadata = JSON.parse(row.metadata_json);
|
|
193
|
+
} catch {
|
|
194
|
+
metadata = {};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const peer = {
|
|
198
|
+
peer_id: row.peer_id,
|
|
199
|
+
handle: makeHandle(row),
|
|
200
|
+
runtime: row.runtime,
|
|
201
|
+
name: row.name,
|
|
202
|
+
session_id: row.session_id || null,
|
|
203
|
+
origin: row.origin,
|
|
204
|
+
spawned_by: row.spawned_by || null,
|
|
205
|
+
exec_mode: row.exec_mode,
|
|
206
|
+
state: row.state,
|
|
207
|
+
cwd: row.cwd || null,
|
|
208
|
+
model: row.model || null,
|
|
209
|
+
pid: row.pid ?? null,
|
|
210
|
+
pane_id: row.pane_id || null,
|
|
211
|
+
ls_address: row.ls_address || null,
|
|
212
|
+
csrf_token: row.csrf_token || null,
|
|
213
|
+
project_id: row.project_id || null,
|
|
214
|
+
metadata,
|
|
215
|
+
created_at: row.created_at,
|
|
216
|
+
updated_at: row.updated_at,
|
|
217
|
+
closed_at: row.closed_at || null,
|
|
218
|
+
};
|
|
219
|
+
return peer;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function sanitizePeerForClient(peer) {
|
|
223
|
+
if (!peer) return null;
|
|
224
|
+
const { csrf_token: _secret, ...pub } = peer;
|
|
225
|
+
return { ...pub, has_ls_credentials: Boolean(peer.ls_address && peer.csrf_token) };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export class StateDB {
|
|
229
|
+
constructor(dbPath) {
|
|
230
|
+
ensureHomeDir(path.dirname(dbPath));
|
|
231
|
+
this.dbPath = dbPath;
|
|
232
|
+
this.db = new DatabaseSync(dbPath);
|
|
233
|
+
this.db.exec('PRAGMA journal_mode = WAL;');
|
|
234
|
+
this.db.exec('PRAGMA synchronous = NORMAL;');
|
|
235
|
+
this.db.exec('PRAGMA foreign_keys = ON;');
|
|
236
|
+
this.db.exec('PRAGMA busy_timeout = 5000;');
|
|
237
|
+
this.db.exec(SCHEMA_SQL);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
close() {
|
|
241
|
+
this.db.close();
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
upsertPeer(input) {
|
|
245
|
+
const ts = nowIso();
|
|
246
|
+
let existing = null;
|
|
247
|
+
if (input.peer_id) {
|
|
248
|
+
existing = this.getPeer(input.peer_id);
|
|
249
|
+
} else if (input.session_id) {
|
|
250
|
+
existing = this.getPeerBySession(input.runtime, input.session_id);
|
|
251
|
+
} else if (input.runtime && input.name) {
|
|
252
|
+
existing = this.getActivePeerByName(input.runtime, input.name, input.cwd || null);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const peerId = existing?.peer_id || input.peer_id || makeId('peer');
|
|
256
|
+
const createdAt = existing?.created_at || ts;
|
|
257
|
+
const mergedMeta = { ...(existing?.metadata || {}), ...(input.metadata || {}) };
|
|
258
|
+
|
|
259
|
+
const row = {
|
|
260
|
+
peer_id: peerId,
|
|
261
|
+
runtime: input.runtime || existing?.runtime,
|
|
262
|
+
name: input.name || existing?.name || peerId.slice(0, 12),
|
|
263
|
+
session_id: input.session_id ?? existing?.session_id ?? null,
|
|
264
|
+
origin: input.origin || existing?.origin || 'attached',
|
|
265
|
+
spawned_by: input.spawned_by ?? existing?.spawned_by ?? null,
|
|
266
|
+
exec_mode: input.exec_mode || existing?.exec_mode || 'interactive',
|
|
267
|
+
state: input.state || existing?.state || 'idle',
|
|
268
|
+
cwd: input.cwd ?? existing?.cwd ?? null,
|
|
269
|
+
model: input.model ?? existing?.model ?? null,
|
|
270
|
+
pid: input.pid ?? existing?.pid ?? null,
|
|
271
|
+
pane_id: input.pane_id ?? existing?.pane_id ?? null,
|
|
272
|
+
ls_address: input.ls_address ?? existing?.ls_address ?? null,
|
|
273
|
+
csrf_token: input.csrf_token ?? existing?.csrf_token ?? null,
|
|
274
|
+
project_id: input.project_id ?? existing?.project_id ?? null,
|
|
275
|
+
metadata_json: JSON.stringify(mergedMeta),
|
|
276
|
+
created_at: createdAt,
|
|
277
|
+
updated_at: ts,
|
|
278
|
+
closed_at: input.closed_at ?? (['archived', 'closed', 'deleted', 'detached'].includes(input.state) ? ts : null),
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const stmt = this.db.prepare(`
|
|
282
|
+
INSERT INTO peers (
|
|
283
|
+
peer_id, runtime, name, session_id, origin, spawned_by, exec_mode, state,
|
|
284
|
+
cwd, model, pid, pane_id, ls_address, csrf_token, project_id, metadata_json,
|
|
285
|
+
created_at, updated_at, closed_at
|
|
286
|
+
) VALUES (
|
|
287
|
+
$peer_id, $runtime, $name, $session_id, $origin, $spawned_by, $exec_mode, $state,
|
|
288
|
+
$cwd, $model, $pid, $pane_id, $ls_address, $csrf_token, $project_id, $metadata_json,
|
|
289
|
+
$created_at, $updated_at, $closed_at
|
|
290
|
+
)
|
|
291
|
+
ON CONFLICT(peer_id) DO UPDATE SET
|
|
292
|
+
runtime = excluded.runtime,
|
|
293
|
+
name = excluded.name,
|
|
294
|
+
session_id = excluded.session_id,
|
|
295
|
+
origin = excluded.origin,
|
|
296
|
+
spawned_by = excluded.spawned_by,
|
|
297
|
+
exec_mode = excluded.exec_mode,
|
|
298
|
+
state = excluded.state,
|
|
299
|
+
cwd = excluded.cwd,
|
|
300
|
+
model = excluded.model,
|
|
301
|
+
pid = excluded.pid,
|
|
302
|
+
pane_id = excluded.pane_id,
|
|
303
|
+
ls_address = excluded.ls_address,
|
|
304
|
+
csrf_token = excluded.csrf_token,
|
|
305
|
+
project_id = excluded.project_id,
|
|
306
|
+
metadata_json = excluded.metadata_json,
|
|
307
|
+
updated_at = excluded.updated_at,
|
|
308
|
+
closed_at = excluded.closed_at
|
|
309
|
+
`);
|
|
310
|
+
stmt.run(row);
|
|
311
|
+
return this.getPeer(peerId);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
getPeer(peerId) {
|
|
315
|
+
const row = this.db.prepare('SELECT * FROM peers WHERE peer_id = ?').get(peerId);
|
|
316
|
+
return deserializePeer(row);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
getPeerBySession(runtime, sessionId) {
|
|
320
|
+
if (!sessionId) return null;
|
|
321
|
+
const row = runtime
|
|
322
|
+
? this.db
|
|
323
|
+
.prepare('SELECT * FROM peers WHERE runtime = ? AND session_id = ? ORDER BY updated_at DESC LIMIT 1')
|
|
324
|
+
.get(runtime, sessionId)
|
|
325
|
+
: this.db
|
|
326
|
+
.prepare('SELECT * FROM peers WHERE session_id = ? ORDER BY updated_at DESC LIMIT 1')
|
|
327
|
+
.get(sessionId);
|
|
328
|
+
return deserializePeer(row);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
getActivePeersByName(runtime, name) {
|
|
332
|
+
const rows = this.db
|
|
333
|
+
.prepare(
|
|
334
|
+
`SELECT * FROM peers
|
|
335
|
+
WHERE runtime = ? AND name = ? AND state NOT IN ('archived', 'closed', 'deleted', 'detached')
|
|
336
|
+
ORDER BY updated_at DESC`,
|
|
337
|
+
)
|
|
338
|
+
.all(runtime, name);
|
|
339
|
+
return rows.map(deserializePeer);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
getActivePeerByName(runtime, name, cwd = null) {
|
|
343
|
+
const peers = this.getActivePeersByName(runtime, name);
|
|
344
|
+
if (peers.length === 0) return null;
|
|
345
|
+
if (!cwd) return peers[0];
|
|
346
|
+
return peers.find((p) => p.cwd && isSameProjectScope(p.cwd, cwd)) || null;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
listPeers({ runtime = null, includeOffline = false } = {}) {
|
|
350
|
+
const clauses = [];
|
|
351
|
+
const params = [];
|
|
352
|
+
if (runtime && runtime !== 'all') {
|
|
353
|
+
clauses.push('runtime = ?');
|
|
354
|
+
params.push(runtime);
|
|
355
|
+
}
|
|
356
|
+
if (!includeOffline) {
|
|
357
|
+
clauses.push(`state IN ('starting', 'idle', 'busy', 'waiting')`);
|
|
358
|
+
}
|
|
359
|
+
const where = clauses.length ? `WHERE ${clauses.join(' AND ')}` : '';
|
|
360
|
+
const rows = this.db
|
|
361
|
+
.prepare(`SELECT * FROM peers ${where} ORDER BY created_at ASC`)
|
|
362
|
+
.all(...params);
|
|
363
|
+
return rows.map(deserializePeer);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
updatePeerState(peerId, state, extra = {}) {
|
|
367
|
+
const existing = this.getPeer(peerId);
|
|
368
|
+
if (!existing) return null;
|
|
369
|
+
return this.upsertPeer({ ...existing, ...extra, peer_id: peerId, state });
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
getThreadState(threadId) {
|
|
373
|
+
return this.db.prepare('SELECT * FROM thread_state WHERE thread_id = ?').get(threadId) || null;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
upsertThreadState({ thread_id, last_hop, max_hops, no_reply_from_peer = null, blocked_reason = null }) {
|
|
377
|
+
const ts = nowIso();
|
|
378
|
+
this.db
|
|
379
|
+
.prepare(`
|
|
380
|
+
INSERT INTO thread_state (thread_id, last_hop, max_hops, no_reply_from_peer, blocked_reason, updated_at)
|
|
381
|
+
VALUES ($thread_id, $last_hop, $max_hops, $no_reply_from_peer, $blocked_reason, $updated_at)
|
|
382
|
+
ON CONFLICT(thread_id) DO UPDATE SET
|
|
383
|
+
last_hop = excluded.last_hop,
|
|
384
|
+
max_hops = excluded.max_hops,
|
|
385
|
+
no_reply_from_peer = excluded.no_reply_from_peer,
|
|
386
|
+
blocked_reason = excluded.blocked_reason,
|
|
387
|
+
updated_at = excluded.updated_at
|
|
388
|
+
`)
|
|
389
|
+
.run({ thread_id, last_hop, max_hops, no_reply_from_peer, blocked_reason, updated_at: ts });
|
|
390
|
+
return this.getThreadState(thread_id);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
insertMessage(msg) {
|
|
394
|
+
const ts = msg.created_at || nowIso();
|
|
395
|
+
const record = {
|
|
396
|
+
msg_id: msg.msg_id || makeId('msg'),
|
|
397
|
+
thread_id: msg.thread_id || makeId('thr'),
|
|
398
|
+
from_peer_id: msg.from_peer_id || null,
|
|
399
|
+
from_runtime: msg.from_runtime || null,
|
|
400
|
+
from_name: msg.from_name || null,
|
|
401
|
+
to_raw: msg.to_raw,
|
|
402
|
+
to_peer_id: msg.to_peer_id,
|
|
403
|
+
kind: msg.kind || 'prompt',
|
|
404
|
+
body: msg.body,
|
|
405
|
+
reply_expected: msg.reply_expected ? 1 : 0,
|
|
406
|
+
hop_count: msg.hop_count,
|
|
407
|
+
max_hops: msg.max_hops,
|
|
408
|
+
delivery_mode: msg.delivery_mode || 'immediate',
|
|
409
|
+
status: msg.status || 'queued',
|
|
410
|
+
error: msg.error || null,
|
|
411
|
+
created_at: ts,
|
|
412
|
+
};
|
|
413
|
+
this.db
|
|
414
|
+
.prepare(`
|
|
415
|
+
INSERT INTO messages (
|
|
416
|
+
msg_id, thread_id, from_peer_id, from_runtime, from_name,
|
|
417
|
+
to_raw, to_peer_id, kind, body, reply_expected, hop_count, max_hops,
|
|
418
|
+
delivery_mode, status, error, created_at
|
|
419
|
+
) VALUES (
|
|
420
|
+
$msg_id, $thread_id, $from_peer_id, $from_runtime, $from_name,
|
|
421
|
+
$to_raw, $to_peer_id, $kind, $body, $reply_expected, $hop_count, $max_hops,
|
|
422
|
+
$delivery_mode, $status, $error, $created_at
|
|
423
|
+
)
|
|
424
|
+
`)
|
|
425
|
+
.run(record);
|
|
426
|
+
return { ...record, reply_expected: Boolean(record.reply_expected) };
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
updateMessageStatus(msgId, status, error = null) {
|
|
430
|
+
this.db
|
|
431
|
+
.prepare('UPDATE messages SET status = ?, error = ? WHERE msg_id = ?')
|
|
432
|
+
.run(status, error, msgId);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
insertDelivery({ msg_id, peer_id, channel, status = 'queued', attempt = 1, error = null, response_excerpt = null }) {
|
|
436
|
+
const ts = nowIso();
|
|
437
|
+
const delivery_id = makeId('dlv');
|
|
438
|
+
const delivered_at = status === 'delivered' ? ts : null;
|
|
439
|
+
this.db
|
|
440
|
+
.prepare(`
|
|
441
|
+
INSERT INTO deliveries (
|
|
442
|
+
delivery_id, msg_id, peer_id, channel, status, attempt, error, response_excerpt, created_at, delivered_at
|
|
443
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
444
|
+
`)
|
|
445
|
+
.run(delivery_id, msg_id, peer_id, channel, status, attempt, error, response_excerpt, ts, delivered_at);
|
|
446
|
+
return { delivery_id, msg_id, peer_id, channel, status, attempt, error, response_excerpt, created_at: ts, delivered_at };
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
claimPendingMessages(peerId, limit = 20) {
|
|
450
|
+
const rows = this.db
|
|
451
|
+
.prepare(`
|
|
452
|
+
SELECT * FROM messages
|
|
453
|
+
WHERE to_peer_id = ? AND status = 'queued'
|
|
454
|
+
ORDER BY created_at ASC
|
|
455
|
+
LIMIT ?
|
|
456
|
+
`)
|
|
457
|
+
.all(peerId, limit);
|
|
458
|
+
return rows.map((r) => ({ ...r, reply_expected: Boolean(r.reply_expected) }));
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
tailLogs({ threadId = null, peerId = null, limit = 20 } = {}) {
|
|
462
|
+
const clauses = [];
|
|
463
|
+
const params = [];
|
|
464
|
+
if (threadId) {
|
|
465
|
+
clauses.push('m.thread_id = ?');
|
|
466
|
+
params.push(threadId);
|
|
467
|
+
}
|
|
468
|
+
if (peerId) {
|
|
469
|
+
clauses.push('(m.to_peer_id = ? OR m.from_peer_id = ?)');
|
|
470
|
+
params.push(peerId, peerId);
|
|
471
|
+
}
|
|
472
|
+
const where = clauses.length ? `WHERE ${clauses.join(' AND ')}` : '';
|
|
473
|
+
params.push(Math.max(1, Math.min(200, Number(limit) || 20)));
|
|
474
|
+
const rows = this.db
|
|
475
|
+
.prepare(`
|
|
476
|
+
SELECT
|
|
477
|
+
m.*,
|
|
478
|
+
(SELECT d.channel FROM deliveries d WHERE d.msg_id = m.msg_id ORDER BY d.created_at DESC LIMIT 1) AS last_channel,
|
|
479
|
+
(SELECT d.status FROM deliveries d WHERE d.msg_id = m.msg_id ORDER BY d.created_at DESC LIMIT 1) AS last_delivery_status,
|
|
480
|
+
(SELECT d.delivered_at FROM deliveries d WHERE d.msg_id = m.msg_id ORDER BY d.created_at DESC LIMIT 1) AS delivered_at
|
|
481
|
+
FROM messages m
|
|
482
|
+
${where}
|
|
483
|
+
ORDER BY m.created_at DESC
|
|
484
|
+
LIMIT ?
|
|
485
|
+
`)
|
|
486
|
+
.all(...params);
|
|
487
|
+
return rows.reverse().map((r) => ({ ...r, reply_expected: Boolean(r.reply_expected) }));
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
insertTask(task) {
|
|
491
|
+
const now = nowIso();
|
|
492
|
+
const requestId = task.request_id || makeId('req');
|
|
493
|
+
this.db
|
|
494
|
+
.prepare(`
|
|
495
|
+
INSERT INTO tasks (
|
|
496
|
+
request_id, workflow_id, step_key, thread_id, msg_id,
|
|
497
|
+
from_handle, callback_to, to_peer_id, to_handle, cwd,
|
|
498
|
+
title, body, expected_artifacts_json, acceptance_criteria_json,
|
|
499
|
+
require_artifacts, notify_on_finish, callback_sent,
|
|
500
|
+
status, result_summary, artifact_report_json, error,
|
|
501
|
+
created_at, updated_at, completed_at
|
|
502
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
503
|
+
`)
|
|
504
|
+
.run(
|
|
505
|
+
requestId,
|
|
506
|
+
task.workflow_id || null,
|
|
507
|
+
task.step_key || null,
|
|
508
|
+
task.thread_id,
|
|
509
|
+
task.msg_id || null,
|
|
510
|
+
task.from_handle || null,
|
|
511
|
+
task.callback_to || null,
|
|
512
|
+
task.to_peer_id,
|
|
513
|
+
task.to_handle,
|
|
514
|
+
task.cwd || null,
|
|
515
|
+
task.title || null,
|
|
516
|
+
task.body,
|
|
517
|
+
JSON.stringify(task.expected_artifacts || []),
|
|
518
|
+
JSON.stringify(task.acceptance_criteria || []),
|
|
519
|
+
task.require_artifacts ? 1 : 0,
|
|
520
|
+
task.notify_on_finish === false ? 0 : 1,
|
|
521
|
+
task.callback_sent ? 1 : 0,
|
|
522
|
+
task.status || 'pending',
|
|
523
|
+
task.result_summary || null,
|
|
524
|
+
task.artifact_report ? JSON.stringify(task.artifact_report) : null,
|
|
525
|
+
task.error || null,
|
|
526
|
+
now,
|
|
527
|
+
now,
|
|
528
|
+
task.completed_at || null,
|
|
529
|
+
);
|
|
530
|
+
return this.getTask(requestId);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
getTask(requestId) {
|
|
534
|
+
const row = this.db.prepare('SELECT * FROM tasks WHERE request_id = ?').get(requestId);
|
|
535
|
+
return deserializeTask(row);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
getTaskByMsgId(msgId) {
|
|
539
|
+
if (!msgId) return null;
|
|
540
|
+
const row = this.db.prepare('SELECT * FROM tasks WHERE msg_id = ?').get(msgId);
|
|
541
|
+
return deserializeTask(row);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
updateTask(requestId, patch = {}) {
|
|
545
|
+
const existing = this.getTask(requestId);
|
|
546
|
+
if (!existing) return null;
|
|
547
|
+
const now = nowIso();
|
|
548
|
+
const nextStatus = patch.status ?? existing.status;
|
|
549
|
+
const isTerminal = ['completed', 'failed', 'cancelled'].includes(nextStatus);
|
|
550
|
+
const completedAt =
|
|
551
|
+
patch.completed_at !== undefined
|
|
552
|
+
? patch.completed_at
|
|
553
|
+
: isTerminal && !existing.completed_at
|
|
554
|
+
? now
|
|
555
|
+
: existing.completed_at;
|
|
556
|
+
this.db
|
|
557
|
+
.prepare(`
|
|
558
|
+
UPDATE tasks SET
|
|
559
|
+
msg_id = ?,
|
|
560
|
+
status = ?,
|
|
561
|
+
result_summary = ?,
|
|
562
|
+
artifact_report_json = ?,
|
|
563
|
+
error = ?,
|
|
564
|
+
callback_sent = ?,
|
|
565
|
+
updated_at = ?,
|
|
566
|
+
completed_at = ?
|
|
567
|
+
WHERE request_id = ?
|
|
568
|
+
`)
|
|
569
|
+
.run(
|
|
570
|
+
patch.msg_id !== undefined ? patch.msg_id : existing.msg_id,
|
|
571
|
+
nextStatus,
|
|
572
|
+
patch.result_summary !== undefined ? patch.result_summary : existing.result_summary,
|
|
573
|
+
patch.artifact_report !== undefined
|
|
574
|
+
? patch.artifact_report
|
|
575
|
+
? JSON.stringify(patch.artifact_report)
|
|
576
|
+
: null
|
|
577
|
+
: existing.artifact_report
|
|
578
|
+
? JSON.stringify(existing.artifact_report)
|
|
579
|
+
: null,
|
|
580
|
+
patch.error !== undefined ? patch.error : existing.error,
|
|
581
|
+
patch.callback_sent !== undefined ? (patch.callback_sent ? 1 : 0) : existing.callback_sent ? 1 : 0,
|
|
582
|
+
now,
|
|
583
|
+
completedAt,
|
|
584
|
+
requestId,
|
|
585
|
+
);
|
|
586
|
+
return this.getTask(requestId);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
listTasks({ workflowId = null, status = null, toPeerId = null, limit = 50 } = {}) {
|
|
590
|
+
const clauses = [];
|
|
591
|
+
const params = [];
|
|
592
|
+
if (workflowId) {
|
|
593
|
+
clauses.push('workflow_id = ?');
|
|
594
|
+
params.push(workflowId);
|
|
595
|
+
}
|
|
596
|
+
if (status) {
|
|
597
|
+
clauses.push('status = ?');
|
|
598
|
+
params.push(status);
|
|
599
|
+
}
|
|
600
|
+
if (toPeerId) {
|
|
601
|
+
clauses.push('to_peer_id = ?');
|
|
602
|
+
params.push(toPeerId);
|
|
603
|
+
}
|
|
604
|
+
const where = clauses.length ? `WHERE ${clauses.join(' AND ')}` : '';
|
|
605
|
+
params.push(Math.max(1, Math.min(200, Number(limit) || 50)));
|
|
606
|
+
const rows = this.db
|
|
607
|
+
.prepare(`SELECT * FROM tasks ${where} ORDER BY created_at DESC LIMIT ?`)
|
|
608
|
+
.all(...params);
|
|
609
|
+
return rows.map(deserializeTask);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
insertWorkflow(wf) {
|
|
613
|
+
const now = nowIso();
|
|
614
|
+
const workflowId = wf.workflow_id || makeId('wf');
|
|
615
|
+
this.db
|
|
616
|
+
.prepare(`
|
|
617
|
+
INSERT INTO workflows (
|
|
618
|
+
workflow_id, template, name, status, coordinator_handle,
|
|
619
|
+
spec_json, state_json, result_json, error,
|
|
620
|
+
created_at, updated_at, completed_at
|
|
621
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
622
|
+
`)
|
|
623
|
+
.run(
|
|
624
|
+
workflowId,
|
|
625
|
+
wf.template,
|
|
626
|
+
wf.name || null,
|
|
627
|
+
wf.status || 'running',
|
|
628
|
+
wf.coordinator_handle || null,
|
|
629
|
+
JSON.stringify(wf.spec || {}),
|
|
630
|
+
JSON.stringify(wf.state || {}),
|
|
631
|
+
wf.result ? JSON.stringify(wf.result) : null,
|
|
632
|
+
wf.error || null,
|
|
633
|
+
now,
|
|
634
|
+
now,
|
|
635
|
+
wf.completed_at || null,
|
|
636
|
+
);
|
|
637
|
+
return this.getWorkflow(workflowId);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
getWorkflow(workflowId) {
|
|
641
|
+
const row = this.db.prepare('SELECT * FROM workflows WHERE workflow_id = ?').get(workflowId);
|
|
642
|
+
return deserializeWorkflow(row);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
updateWorkflow(workflowId, patch = {}) {
|
|
646
|
+
const existing = this.getWorkflow(workflowId);
|
|
647
|
+
if (!existing) return null;
|
|
648
|
+
const now = nowIso();
|
|
649
|
+
const nextStatus = patch.status ?? existing.status;
|
|
650
|
+
const isTerminal = ['completed', 'failed', 'cancelled'].includes(nextStatus);
|
|
651
|
+
const completedAt =
|
|
652
|
+
patch.completed_at !== undefined
|
|
653
|
+
? patch.completed_at
|
|
654
|
+
: isTerminal && !existing.completed_at
|
|
655
|
+
? now
|
|
656
|
+
: existing.completed_at;
|
|
657
|
+
this.db
|
|
658
|
+
.prepare(`
|
|
659
|
+
UPDATE workflows SET
|
|
660
|
+
status = ?,
|
|
661
|
+
state_json = ?,
|
|
662
|
+
result_json = ?,
|
|
663
|
+
error = ?,
|
|
664
|
+
updated_at = ?,
|
|
665
|
+
completed_at = ?
|
|
666
|
+
WHERE workflow_id = ?
|
|
667
|
+
`)
|
|
668
|
+
.run(
|
|
669
|
+
nextStatus,
|
|
670
|
+
JSON.stringify(patch.state !== undefined ? patch.state : existing.state),
|
|
671
|
+
patch.result !== undefined
|
|
672
|
+
? patch.result
|
|
673
|
+
? JSON.stringify(patch.result)
|
|
674
|
+
: null
|
|
675
|
+
: existing.result
|
|
676
|
+
? JSON.stringify(existing.result)
|
|
677
|
+
: null,
|
|
678
|
+
patch.error !== undefined ? patch.error : existing.error,
|
|
679
|
+
now,
|
|
680
|
+
completedAt,
|
|
681
|
+
workflowId,
|
|
682
|
+
);
|
|
683
|
+
return this.getWorkflow(workflowId);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
listWorkflows({ status = null, template = null, limit = 50 } = {}) {
|
|
687
|
+
const clauses = [];
|
|
688
|
+
const params = [];
|
|
689
|
+
if (status) {
|
|
690
|
+
clauses.push('status = ?');
|
|
691
|
+
params.push(status);
|
|
692
|
+
}
|
|
693
|
+
if (template) {
|
|
694
|
+
clauses.push('template = ?');
|
|
695
|
+
params.push(template);
|
|
696
|
+
}
|
|
697
|
+
const where = clauses.length ? `WHERE ${clauses.join(' AND ')}` : '';
|
|
698
|
+
params.push(Math.max(1, Math.min(200, Number(limit) || 50)));
|
|
699
|
+
const rows = this.db
|
|
700
|
+
.prepare(`SELECT * FROM workflows ${where} ORDER BY created_at DESC LIMIT ?`)
|
|
701
|
+
.all(...params);
|
|
702
|
+
return rows.map(deserializeWorkflow);
|
|
703
|
+
}
|
|
704
|
+
}
|