agentgui 1.0.691 → 1.0.693
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/acp-queries.js +182 -0
- package/lib/claude-runner.js +2 -1
- package/package.json +1 -1
- package/server.js +18 -0
package/acp-queries.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
const gid = (p) => `${p}-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
3
|
+
const uuid = () => randomUUID();
|
|
4
|
+
const iso = (t) => new Date(t).toISOString();
|
|
5
|
+
const j = (o) => JSON.stringify(o);
|
|
6
|
+
const jp = (s) => { try { return JSON.parse(s); } catch { return {}; } };
|
|
7
|
+
|
|
8
|
+
export function createACPQueries(db, prep) {
|
|
9
|
+
return {
|
|
10
|
+
createThread(metadata = {}) {
|
|
11
|
+
const id = uuid(), now = Date.now();
|
|
12
|
+
prep('INSERT INTO conversations (id, agentId, title, created_at, updated_at, status, metadata) VALUES (?, ?, ?, ?, ?, ?, ?)').run(id, 'unknown', null, now, now, 'idle', j(metadata));
|
|
13
|
+
return { thread_id: id, created_at: iso(now), updated_at: iso(now), metadata, status: 'idle' };
|
|
14
|
+
},
|
|
15
|
+
getThread(tid) {
|
|
16
|
+
const r = prep('SELECT * FROM conversations WHERE id = ?').get(tid);
|
|
17
|
+
if (!r) return null;
|
|
18
|
+
return { thread_id: r.id, created_at: iso(r.created_at), updated_at: iso(r.updated_at), metadata: jp(r.metadata), status: r.status || 'idle' };
|
|
19
|
+
},
|
|
20
|
+
patchThread(tid, upd) {
|
|
21
|
+
const t = this.getThread(tid);
|
|
22
|
+
if (!t) throw new Error('Thread not found');
|
|
23
|
+
const now = Date.now(), meta = upd.metadata !== undefined ? upd.metadata : t.metadata, stat = upd.status !== undefined ? upd.status : t.status;
|
|
24
|
+
prep('UPDATE conversations SET metadata = ?, status = ?, updated_at = ? WHERE id = ?').run(j(meta), stat, now, tid);
|
|
25
|
+
return { thread_id: tid, created_at: t.created_at, updated_at: iso(now), metadata: meta, status: stat };
|
|
26
|
+
},
|
|
27
|
+
deleteThread(tid) {
|
|
28
|
+
const pr = prep('SELECT COUNT(*) as count FROM run_metadata WHERE thread_id = ? AND status = ?').get(tid, 'pending');
|
|
29
|
+
if (pr && pr.count > 0) throw new Error('Cannot delete thread with pending runs');
|
|
30
|
+
db.transaction(() => {
|
|
31
|
+
prep('DELETE FROM thread_states WHERE thread_id = ?').run(tid);
|
|
32
|
+
prep('DELETE FROM checkpoints WHERE thread_id = ?').run(tid);
|
|
33
|
+
prep('DELETE FROM run_metadata WHERE thread_id = ?').run(tid);
|
|
34
|
+
prep('DELETE FROM sessions WHERE conversationId = ?').run(tid);
|
|
35
|
+
prep('DELETE FROM messages WHERE conversationId = ?').run(tid);
|
|
36
|
+
prep('DELETE FROM chunks WHERE conversationId = ?').run(tid);
|
|
37
|
+
prep('DELETE FROM events WHERE conversationId = ?').run(tid);
|
|
38
|
+
prep('DELETE FROM conversations WHERE id = ?').run(tid);
|
|
39
|
+
})();
|
|
40
|
+
return true;
|
|
41
|
+
},
|
|
42
|
+
saveThreadState(tid, cid, sd) {
|
|
43
|
+
const id = gid('state'), now = Date.now();
|
|
44
|
+
prep('INSERT INTO thread_states (id, thread_id, checkpoint_id, state_data, created_at) VALUES (?, ?, ?, ?, ?)').run(id, tid, cid, j(sd), now);
|
|
45
|
+
return { id, thread_id: tid, checkpoint_id: cid, created_at: iso(now) };
|
|
46
|
+
},
|
|
47
|
+
getThreadState(tid, cid = null) {
|
|
48
|
+
const r = cid ? prep('SELECT * FROM thread_states WHERE thread_id = ? AND checkpoint_id = ? ORDER BY created_at DESC LIMIT 1').get(tid, cid) : prep('SELECT * FROM thread_states WHERE thread_id = ? ORDER BY created_at DESC LIMIT 1').get(tid);
|
|
49
|
+
if (!r) return null;
|
|
50
|
+
const sd = jp(r.state_data);
|
|
51
|
+
return { checkpoint: { checkpoint_id: r.checkpoint_id }, values: sd.values || {}, messages: sd.messages || [], metadata: sd.metadata || {} };
|
|
52
|
+
},
|
|
53
|
+
getThreadHistory(tid, lim = 50, off = 0) {
|
|
54
|
+
const tot = prep('SELECT COUNT(*) as count FROM thread_states WHERE thread_id = ?').get(tid).count;
|
|
55
|
+
const rows = prep('SELECT * FROM thread_states WHERE thread_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?').all(tid, lim, off);
|
|
56
|
+
const states = rows.map(r => { const sd = jp(r.state_data); return { checkpoint: { checkpoint_id: r.checkpoint_id }, values: sd.values || {}, messages: sd.messages || [], metadata: sd.metadata || {} }; });
|
|
57
|
+
return { states, total: tot, limit: lim, offset: off, hasMore: off + lim < tot };
|
|
58
|
+
},
|
|
59
|
+
copyThread(stid) {
|
|
60
|
+
const st = this.getThread(stid);
|
|
61
|
+
if (!st) throw new Error('Source thread not found');
|
|
62
|
+
const ntid = uuid(), now = Date.now();
|
|
63
|
+
db.transaction(() => {
|
|
64
|
+
prep('INSERT INTO conversations (id, agentId, title, created_at, updated_at, status, metadata, workingDirectory) SELECT ?, agentId, title || \' (copy)\', ?, ?, status, metadata, workingDirectory FROM conversations WHERE id = ?').run(ntid, now, now, stid);
|
|
65
|
+
const cps = prep('SELECT * FROM checkpoints WHERE thread_id = ? ORDER BY sequence ASC').all(stid);
|
|
66
|
+
cps.forEach(cp => prep('INSERT INTO checkpoints (id, thread_id, checkpoint_name, sequence, created_at) VALUES (?, ?, ?, ?, ?)').run(uuid(), ntid, cp.checkpoint_name, cp.sequence, now));
|
|
67
|
+
const sts = prep('SELECT * FROM thread_states WHERE thread_id = ? ORDER BY created_at ASC').all(stid);
|
|
68
|
+
sts.forEach(s => prep('INSERT INTO thread_states (id, thread_id, checkpoint_id, state_data, created_at) VALUES (?, ?, ?, ?, ?)').run(gid('state'), ntid, s.checkpoint_id, s.state_data, now));
|
|
69
|
+
const msgs = prep('SELECT * FROM messages WHERE conversationId = ? ORDER BY created_at ASC').all(stid);
|
|
70
|
+
msgs.forEach(m => prep('INSERT INTO messages (id, conversationId, role, content, created_at) VALUES (?, ?, ?, ?, ?)').run(gid('msg'), ntid, m.role, m.content, now));
|
|
71
|
+
})();
|
|
72
|
+
return this.getThread(ntid);
|
|
73
|
+
},
|
|
74
|
+
createCheckpoint(tid, name = null) {
|
|
75
|
+
const id = uuid(), now = Date.now();
|
|
76
|
+
const ms = prep('SELECT MAX(sequence) as max FROM checkpoints WHERE thread_id = ?').get(tid);
|
|
77
|
+
const seq = (ms?.max ?? -1) + 1;
|
|
78
|
+
prep('INSERT INTO checkpoints (id, thread_id, checkpoint_name, sequence, created_at) VALUES (?, ?, ?, ?, ?)').run(id, tid, name, seq, now);
|
|
79
|
+
return { checkpoint_id: id, thread_id: tid, checkpoint_name: name, sequence: seq, created_at: iso(now) };
|
|
80
|
+
},
|
|
81
|
+
getCheckpoint(cid) {
|
|
82
|
+
const r = prep('SELECT * FROM checkpoints WHERE id = ?').get(cid);
|
|
83
|
+
if (!r) return null;
|
|
84
|
+
return { checkpoint_id: r.id, thread_id: r.thread_id, checkpoint_name: r.checkpoint_name, sequence: r.sequence, created_at: iso(r.created_at) };
|
|
85
|
+
},
|
|
86
|
+
listCheckpoints(tid, lim = 50, off = 0) {
|
|
87
|
+
const tot = prep('SELECT COUNT(*) as count FROM checkpoints WHERE thread_id = ?').get(tid).count;
|
|
88
|
+
const rows = prep('SELECT * FROM checkpoints WHERE thread_id = ? ORDER BY sequence DESC LIMIT ? OFFSET ?').all(tid, lim, off);
|
|
89
|
+
const cps = rows.map(r => ({ checkpoint_id: r.id, thread_id: r.thread_id, checkpoint_name: r.checkpoint_name, sequence: r.sequence, created_at: iso(r.created_at) }));
|
|
90
|
+
return { checkpoints: cps, total: tot, limit: lim, offset: off, hasMore: off + lim < tot };
|
|
91
|
+
},
|
|
92
|
+
createRun(aid, tid = null, inp = null, cfg = null, wh = null) {
|
|
93
|
+
const rid = uuid(), now = Date.now(), mid = gid('runmeta');
|
|
94
|
+
let atid = tid;
|
|
95
|
+
if (!tid) {
|
|
96
|
+
atid = uuid();
|
|
97
|
+
prep('INSERT INTO conversations (id, agentId, title, created_at, updated_at, status, metadata) VALUES (?, ?, ?, ?, ?, ?, ?)').run(atid, aid, 'Stateless Run', now, now, 'idle', '{"stateless":true}');
|
|
98
|
+
}
|
|
99
|
+
prep('INSERT INTO sessions (id, conversationId, status, started_at, completed_at, response, error) VALUES (?, ?, ?, ?, ?, ?, ?)').run(rid, atid, 'pending', now, null, null, null);
|
|
100
|
+
prep('INSERT INTO run_metadata (id, run_id, thread_id, agent_id, status, input, config, webhook_url, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)').run(mid, rid, tid, aid, 'pending', inp ? j(inp) : null, cfg ? j(cfg) : null, wh, now, now);
|
|
101
|
+
return { run_id: rid, thread_id: tid, agent_id: aid, status: 'pending', created_at: iso(now), updated_at: iso(now) };
|
|
102
|
+
},
|
|
103
|
+
getRun(rid) {
|
|
104
|
+
const r = prep('SELECT * FROM run_metadata WHERE run_id = ?').get(rid);
|
|
105
|
+
if (!r) return null;
|
|
106
|
+
return { run_id: r.run_id, thread_id: r.thread_id, agent_id: r.agent_id, status: r.status, created_at: iso(r.created_at), updated_at: iso(r.updated_at) };
|
|
107
|
+
},
|
|
108
|
+
updateRunStatus(rid, stat) {
|
|
109
|
+
const now = Date.now();
|
|
110
|
+
prep('UPDATE run_metadata SET status = ?, updated_at = ? WHERE run_id = ?').run(stat, now, rid);
|
|
111
|
+
prep('UPDATE sessions SET status = ? WHERE id = ?').run(stat, rid);
|
|
112
|
+
return this.getRun(rid);
|
|
113
|
+
},
|
|
114
|
+
cancelRun(rid) {
|
|
115
|
+
const r = this.getRun(rid);
|
|
116
|
+
if (!r) throw new Error('Run not found');
|
|
117
|
+
if (['success', 'error', 'cancelled'].includes(r.status)) throw new Error('Run already completed or cancelled');
|
|
118
|
+
return this.updateRunStatus(rid, 'cancelled');
|
|
119
|
+
},
|
|
120
|
+
deleteRun(rid) {
|
|
121
|
+
db.transaction(() => {
|
|
122
|
+
prep('DELETE FROM chunks WHERE sessionId = ?').run(rid);
|
|
123
|
+
prep('DELETE FROM events WHERE sessionId = ?').run(rid);
|
|
124
|
+
prep('DELETE FROM run_metadata WHERE run_id = ?').run(rid);
|
|
125
|
+
prep('DELETE FROM sessions WHERE id = ?').run(rid);
|
|
126
|
+
})();
|
|
127
|
+
return true;
|
|
128
|
+
},
|
|
129
|
+
getThreadRuns(tid, lim = 50, off = 0) {
|
|
130
|
+
const tot = prep('SELECT COUNT(*) as count FROM run_metadata WHERE thread_id = ?').get(tid).count;
|
|
131
|
+
const rows = prep('SELECT * FROM run_metadata WHERE thread_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?').all(tid, lim, off);
|
|
132
|
+
const runs = rows.map(r => ({ run_id: r.run_id, thread_id: r.thread_id, agent_id: r.agent_id, status: r.status, created_at: iso(r.created_at), updated_at: iso(r.updated_at) }));
|
|
133
|
+
return { runs, total: tot, limit: lim, offset: off, hasMore: off + lim < tot };
|
|
134
|
+
},
|
|
135
|
+
searchThreads(flt = {}) {
|
|
136
|
+
const { metadata, status, dateRange, limit = 50, offset = 0 } = flt;
|
|
137
|
+
let wh = "status != 'deleted'", prm = [];
|
|
138
|
+
if (status) { wh += ' AND status = ?'; prm.push(status); }
|
|
139
|
+
if (dateRange?.start) { wh += ' AND created_at >= ?'; prm.push(new Date(dateRange.start).getTime()); }
|
|
140
|
+
if (dateRange?.end) { wh += ' AND created_at <= ?'; prm.push(new Date(dateRange.end).getTime()); }
|
|
141
|
+
if (metadata) { for (const [k, v] of Object.entries(metadata)) { wh += ' AND metadata LIKE ?'; prm.push(`%"${k}":"${v}"%`); } }
|
|
142
|
+
const tot = prep(`SELECT COUNT(*) as count FROM conversations WHERE ${wh}`).get(...prm).count;
|
|
143
|
+
const rows = prep(`SELECT * FROM conversations WHERE ${wh} ORDER BY updated_at DESC LIMIT ? OFFSET ?`).all(...prm, limit, offset);
|
|
144
|
+
const ths = rows.map(r => ({ thread_id: r.id, created_at: iso(r.created_at), updated_at: iso(r.updated_at), metadata: jp(r.metadata), status: r.status || 'idle' }));
|
|
145
|
+
return { threads: ths, total: tot, limit, offset, hasMore: offset + limit < tot };
|
|
146
|
+
},
|
|
147
|
+
searchAgents(agents, flt = {}) {
|
|
148
|
+
const { name, version, capabilities, limit = 50, offset = 0 } = flt;
|
|
149
|
+
let results = agents;
|
|
150
|
+
if (name) {
|
|
151
|
+
const n = name.toLowerCase();
|
|
152
|
+
results = results.filter(a => a.name.toLowerCase().includes(n) || a.id.toLowerCase().includes(n));
|
|
153
|
+
}
|
|
154
|
+
if (capabilities) {
|
|
155
|
+
results = results.filter(a => {
|
|
156
|
+
const desc = this.getAgentDescriptor ? this.getAgentDescriptor(a.id) : null;
|
|
157
|
+
if (!desc) return false;
|
|
158
|
+
const caps = desc.specs?.capabilities || {};
|
|
159
|
+
if (capabilities.streaming !== undefined && !caps.streaming) return false;
|
|
160
|
+
if (capabilities.threads !== undefined && caps.threads !== capabilities.threads) return false;
|
|
161
|
+
if (capabilities.interrupts !== undefined && caps.interrupts !== capabilities.interrupts) return false;
|
|
162
|
+
return true;
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
const total = results.length;
|
|
166
|
+
const paginated = results.slice(offset, offset + limit);
|
|
167
|
+
const agents_list = paginated.map(a => ({ agent_id: a.id, name: a.name, version: version || '1.0.0', path: a.path }));
|
|
168
|
+
return { agents: agents_list, total, limit, offset, hasMore: offset + limit < total };
|
|
169
|
+
},
|
|
170
|
+
searchRuns(flt = {}) {
|
|
171
|
+
const { agent_id, thread_id, status, limit = 50, offset = 0 } = flt;
|
|
172
|
+
let wh = '1=1', prm = [];
|
|
173
|
+
if (agent_id) { wh += ' AND agent_id = ?'; prm.push(agent_id); }
|
|
174
|
+
if (thread_id) { wh += ' AND thread_id = ?'; prm.push(thread_id); }
|
|
175
|
+
if (status) { wh += ' AND status = ?'; prm.push(status); }
|
|
176
|
+
const tot = prep(`SELECT COUNT(*) as count FROM run_metadata WHERE ${wh}`).get(...prm).count;
|
|
177
|
+
const rows = prep(`SELECT * FROM run_metadata WHERE ${wh} ORDER BY created_at DESC LIMIT ? OFFSET ?`).all(...prm, limit, offset);
|
|
178
|
+
const runs = rows.map(r => ({ run_id: r.run_id, thread_id: r.thread_id, agent_id: r.agent_id, status: r.status, created_at: iso(r.created_at), updated_at: iso(r.updated_at) }));
|
|
179
|
+
return { runs, total: tot, limit, offset, hasMore: offset + limit < tot };
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
package/lib/claude-runner.js
CHANGED
|
@@ -255,7 +255,8 @@ class AgentRunner {
|
|
|
255
255
|
resolve({ outputs, sessionId });
|
|
256
256
|
} else {
|
|
257
257
|
const stderrHint = stderrBuffer.trim() ? `: ${stderrBuffer.trim().slice(0, 200)}` : '';
|
|
258
|
-
|
|
258
|
+
const codeHint = code === 143 ? ' (SIGTERM - process was killed)' : code === 137 ? ' (SIGKILL - out of memory or force-killed)' : '';
|
|
259
|
+
reject(new Error(`${this.name} exited with code ${code}${codeHint}${stderrHint}`));
|
|
259
260
|
}
|
|
260
261
|
});
|
|
261
262
|
|
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -4437,14 +4437,32 @@ if (watch) {
|
|
|
4437
4437
|
} catch (e) { console.error('Watch error:', e.message); }
|
|
4438
4438
|
}
|
|
4439
4439
|
|
|
4440
|
+
function killActiveExecutions() {
|
|
4441
|
+
for (const [convId, entry] of activeExecutions.entries()) {
|
|
4442
|
+
if (entry.pid) {
|
|
4443
|
+
try { process.kill(-entry.pid, 'SIGTERM'); } catch { try { process.kill(entry.pid, 'SIGTERM'); } catch (_) {} }
|
|
4444
|
+
}
|
|
4445
|
+
if (entry.proc) {
|
|
4446
|
+
try { entry.proc.kill('SIGTERM'); } catch (_) {}
|
|
4447
|
+
}
|
|
4448
|
+
}
|
|
4449
|
+
activeExecutions.clear();
|
|
4450
|
+
}
|
|
4451
|
+
|
|
4440
4452
|
process.on('SIGTERM', () => {
|
|
4441
4453
|
console.log('[SIGNAL] SIGTERM received - graceful shutdown');
|
|
4454
|
+
killActiveExecutions();
|
|
4442
4455
|
try { pm2Manager.disconnect(); } catch (_) {}
|
|
4443
4456
|
stopACPTools().catch(() => {}).finally(() => {
|
|
4444
4457
|
try { wss.close(() => server.close(() => process.exit(0))); } catch (_) { process.exit(0); }
|
|
4445
4458
|
});
|
|
4446
4459
|
});
|
|
4447
4460
|
|
|
4461
|
+
process.on('SIGINT', () => {
|
|
4462
|
+
killActiveExecutions();
|
|
4463
|
+
process.exit(0);
|
|
4464
|
+
});
|
|
4465
|
+
|
|
4448
4466
|
server.on('error', (err) => {
|
|
4449
4467
|
if (err.code === 'EADDRINUSE') {
|
|
4450
4468
|
console.error(`Port ${PORT} already in use. Waiting 3 seconds before retry...`);
|