agentgui 1.0.381 → 1.0.382
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 +162 -550
- package/package.json +1 -1
package/acp-queries.js
CHANGED
|
@@ -1,550 +1,162 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return
|
|
113
|
-
},
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
let
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
},
|
|
164
|
-
|
|
165
|
-
getThreadHistory(threadId, limit = 50, offset = 0) {
|
|
166
|
-
const countStmt = prep('SELECT COUNT(*) as count FROM thread_states WHERE thread_id = ?');
|
|
167
|
-
const total = countStmt.get(threadId).count;
|
|
168
|
-
|
|
169
|
-
const stmt = prep(
|
|
170
|
-
`SELECT * FROM thread_states WHERE thread_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?`
|
|
171
|
-
);
|
|
172
|
-
const rows = stmt.all(threadId, limit, offset);
|
|
173
|
-
|
|
174
|
-
const states = rows.map(row => {
|
|
175
|
-
let stateData = {};
|
|
176
|
-
try {
|
|
177
|
-
stateData = JSON.parse(row.state_data);
|
|
178
|
-
} catch (e) {}
|
|
179
|
-
|
|
180
|
-
return {
|
|
181
|
-
checkpoint: { checkpoint_id: row.checkpoint_id },
|
|
182
|
-
values: stateData.values || {},
|
|
183
|
-
messages: stateData.messages || [],
|
|
184
|
-
metadata: stateData.metadata || {}
|
|
185
|
-
};
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
return {
|
|
189
|
-
states,
|
|
190
|
-
total,
|
|
191
|
-
limit,
|
|
192
|
-
offset,
|
|
193
|
-
hasMore: offset + limit < total
|
|
194
|
-
};
|
|
195
|
-
},
|
|
196
|
-
|
|
197
|
-
copyThread(sourceThreadId) {
|
|
198
|
-
const sourceThread = this.getThread(sourceThreadId);
|
|
199
|
-
if (!sourceThread) {
|
|
200
|
-
throw new Error('Source thread not found');
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
const newThreadId = generateUUID();
|
|
204
|
-
const now = Date.now();
|
|
205
|
-
|
|
206
|
-
const copyStmt = db.transaction(() => {
|
|
207
|
-
// Copy thread
|
|
208
|
-
prep(
|
|
209
|
-
`INSERT INTO conversations (id, agentId, title, created_at, updated_at, status, metadata, workingDirectory)
|
|
210
|
-
SELECT ?, agentId, title || ' (copy)', ?, ?, status, metadata, workingDirectory
|
|
211
|
-
FROM conversations WHERE id = ?`
|
|
212
|
-
).run(newThreadId, now, now, sourceThreadId);
|
|
213
|
-
|
|
214
|
-
// Copy checkpoints
|
|
215
|
-
const checkpoints = prep('SELECT * FROM checkpoints WHERE thread_id = ? ORDER BY sequence ASC').all(sourceThreadId);
|
|
216
|
-
for (const checkpoint of checkpoints) {
|
|
217
|
-
const newCheckpointId = generateUUID();
|
|
218
|
-
prep(
|
|
219
|
-
`INSERT INTO checkpoints (id, thread_id, checkpoint_name, sequence, created_at)
|
|
220
|
-
VALUES (?, ?, ?, ?, ?)`
|
|
221
|
-
).run(newCheckpointId, newThreadId, checkpoint.checkpoint_name, checkpoint.sequence, now);
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
// Copy thread states
|
|
225
|
-
const states = prep('SELECT * FROM thread_states WHERE thread_id = ? ORDER BY created_at ASC').all(sourceThreadId);
|
|
226
|
-
for (const state of states) {
|
|
227
|
-
prep(
|
|
228
|
-
`INSERT INTO thread_states (id, thread_id, checkpoint_id, state_data, created_at)
|
|
229
|
-
VALUES (?, ?, ?, ?, ?)`
|
|
230
|
-
).run(generateId('state'), newThreadId, state.checkpoint_id, state.state_data, now);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// Copy messages
|
|
234
|
-
const messages = prep('SELECT * FROM messages WHERE conversationId = ? ORDER BY created_at ASC').all(sourceThreadId);
|
|
235
|
-
for (const msg of messages) {
|
|
236
|
-
prep(
|
|
237
|
-
`INSERT INTO messages (id, conversationId, role, content, created_at)
|
|
238
|
-
VALUES (?, ?, ?, ?, ?)`
|
|
239
|
-
).run(generateId('msg'), newThreadId, msg.role, msg.content, now);
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
copyStmt();
|
|
244
|
-
return this.getThread(newThreadId);
|
|
245
|
-
},
|
|
246
|
-
|
|
247
|
-
// ============ CHECKPOINT FUNCTIONS ============
|
|
248
|
-
|
|
249
|
-
createCheckpoint(threadId, checkpointName = null) {
|
|
250
|
-
const id = generateUUID();
|
|
251
|
-
const now = Date.now();
|
|
252
|
-
|
|
253
|
-
// Get next sequence number
|
|
254
|
-
const maxSeq = prep('SELECT MAX(sequence) as max FROM checkpoints WHERE thread_id = ?').get(threadId);
|
|
255
|
-
const sequence = (maxSeq?.max ?? -1) + 1;
|
|
256
|
-
|
|
257
|
-
const stmt = prep(
|
|
258
|
-
`INSERT INTO checkpoints (id, thread_id, checkpoint_name, sequence, created_at)
|
|
259
|
-
VALUES (?, ?, ?, ?, ?)`
|
|
260
|
-
);
|
|
261
|
-
stmt.run(id, threadId, checkpointName, sequence, now);
|
|
262
|
-
|
|
263
|
-
return {
|
|
264
|
-
checkpoint_id: id,
|
|
265
|
-
thread_id: threadId,
|
|
266
|
-
checkpoint_name: checkpointName,
|
|
267
|
-
sequence,
|
|
268
|
-
created_at: toISOString(now)
|
|
269
|
-
};
|
|
270
|
-
},
|
|
271
|
-
|
|
272
|
-
getCheckpoint(checkpointId) {
|
|
273
|
-
const stmt = prep('SELECT * FROM checkpoints WHERE id = ?');
|
|
274
|
-
const row = stmt.get(checkpointId);
|
|
275
|
-
|
|
276
|
-
if (!row) return null;
|
|
277
|
-
|
|
278
|
-
return {
|
|
279
|
-
checkpoint_id: row.id,
|
|
280
|
-
thread_id: row.thread_id,
|
|
281
|
-
checkpoint_name: row.checkpoint_name,
|
|
282
|
-
sequence: row.sequence,
|
|
283
|
-
created_at: toISOString(row.created_at)
|
|
284
|
-
};
|
|
285
|
-
},
|
|
286
|
-
|
|
287
|
-
listCheckpoints(threadId, limit = 50, offset = 0) {
|
|
288
|
-
const countStmt = prep('SELECT COUNT(*) as count FROM checkpoints WHERE thread_id = ?');
|
|
289
|
-
const total = countStmt.get(threadId).count;
|
|
290
|
-
|
|
291
|
-
const stmt = prep(
|
|
292
|
-
`SELECT * FROM checkpoints WHERE thread_id = ? ORDER BY sequence DESC LIMIT ? OFFSET ?`
|
|
293
|
-
);
|
|
294
|
-
const rows = stmt.all(threadId, limit, offset);
|
|
295
|
-
|
|
296
|
-
const checkpoints = rows.map(row => ({
|
|
297
|
-
checkpoint_id: row.id,
|
|
298
|
-
thread_id: row.thread_id,
|
|
299
|
-
checkpoint_name: row.checkpoint_name,
|
|
300
|
-
sequence: row.sequence,
|
|
301
|
-
created_at: toISOString(row.created_at)
|
|
302
|
-
}));
|
|
303
|
-
|
|
304
|
-
return {
|
|
305
|
-
checkpoints,
|
|
306
|
-
total,
|
|
307
|
-
limit,
|
|
308
|
-
offset,
|
|
309
|
-
hasMore: offset + limit < total
|
|
310
|
-
};
|
|
311
|
-
},
|
|
312
|
-
|
|
313
|
-
// ============ RUN MANAGEMENT ============
|
|
314
|
-
|
|
315
|
-
createRun(agentId, threadId = null, input = null, config = null, webhookUrl = null) {
|
|
316
|
-
const runId = generateUUID();
|
|
317
|
-
const now = Date.now();
|
|
318
|
-
|
|
319
|
-
// Create session first
|
|
320
|
-
const sessionStmt = prep(
|
|
321
|
-
`INSERT INTO sessions (id, conversationId, status, started_at, completed_at, response, error)
|
|
322
|
-
VALUES (?, ?, ?, ?, ?, ?, ?)`
|
|
323
|
-
);
|
|
324
|
-
sessionStmt.run(runId, threadId || 'stateless', 'pending', now, null, null, null);
|
|
325
|
-
|
|
326
|
-
// Create run metadata
|
|
327
|
-
const runStmt = prep(
|
|
328
|
-
`INSERT INTO run_metadata (run_id, thread_id, agent_id, status, input, config, webhook_url, created_at, updated_at)
|
|
329
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
330
|
-
);
|
|
331
|
-
runStmt.run(
|
|
332
|
-
runId,
|
|
333
|
-
threadId,
|
|
334
|
-
agentId,
|
|
335
|
-
'pending',
|
|
336
|
-
input ? JSON.stringify(input) : null,
|
|
337
|
-
config ? JSON.stringify(config) : null,
|
|
338
|
-
webhookUrl,
|
|
339
|
-
now,
|
|
340
|
-
now
|
|
341
|
-
);
|
|
342
|
-
|
|
343
|
-
return {
|
|
344
|
-
run_id: runId,
|
|
345
|
-
thread_id: threadId,
|
|
346
|
-
agent_id: agentId,
|
|
347
|
-
status: 'pending',
|
|
348
|
-
created_at: toISOString(now),
|
|
349
|
-
updated_at: toISOString(now)
|
|
350
|
-
};
|
|
351
|
-
},
|
|
352
|
-
|
|
353
|
-
getRun(runId) {
|
|
354
|
-
const stmt = prep('SELECT * FROM run_metadata WHERE run_id = ?');
|
|
355
|
-
const row = stmt.get(runId);
|
|
356
|
-
|
|
357
|
-
if (!row) return null;
|
|
358
|
-
|
|
359
|
-
return {
|
|
360
|
-
run_id: row.run_id,
|
|
361
|
-
thread_id: row.thread_id,
|
|
362
|
-
agent_id: row.agent_id,
|
|
363
|
-
status: row.status,
|
|
364
|
-
created_at: toISOString(row.created_at),
|
|
365
|
-
updated_at: toISOString(row.updated_at)
|
|
366
|
-
};
|
|
367
|
-
},
|
|
368
|
-
|
|
369
|
-
updateRunStatus(runId, status) {
|
|
370
|
-
const now = Date.now();
|
|
371
|
-
|
|
372
|
-
const stmt = prep(
|
|
373
|
-
`UPDATE run_metadata SET status = ?, updated_at = ? WHERE run_id = ?`
|
|
374
|
-
);
|
|
375
|
-
stmt.run(status, now, runId);
|
|
376
|
-
|
|
377
|
-
// Also update session
|
|
378
|
-
prep('UPDATE sessions SET status = ? WHERE id = ?').run(status, runId);
|
|
379
|
-
|
|
380
|
-
return this.getRun(runId);
|
|
381
|
-
},
|
|
382
|
-
|
|
383
|
-
cancelRun(runId) {
|
|
384
|
-
const run = this.getRun(runId);
|
|
385
|
-
if (!run) {
|
|
386
|
-
throw new Error('Run not found');
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
if (['success', 'error', 'cancelled'].includes(run.status)) {
|
|
390
|
-
throw new Error('Run already completed or cancelled');
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
return this.updateRunStatus(runId, 'cancelled');
|
|
394
|
-
},
|
|
395
|
-
|
|
396
|
-
deleteRun(runId) {
|
|
397
|
-
const deleteStmt = db.transaction(() => {
|
|
398
|
-
prep('DELETE FROM chunks WHERE sessionId = ?').run(runId);
|
|
399
|
-
prep('DELETE FROM events WHERE sessionId = ?').run(runId);
|
|
400
|
-
prep('DELETE FROM run_metadata WHERE run_id = ?').run(runId);
|
|
401
|
-
prep('DELETE FROM sessions WHERE id = ?').run(runId);
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
deleteStmt();
|
|
405
|
-
return true;
|
|
406
|
-
},
|
|
407
|
-
|
|
408
|
-
getThreadRuns(threadId, limit = 50, offset = 0) {
|
|
409
|
-
const countStmt = prep('SELECT COUNT(*) as count FROM run_metadata WHERE thread_id = ?');
|
|
410
|
-
const total = countStmt.get(threadId).count;
|
|
411
|
-
|
|
412
|
-
const stmt = prep(
|
|
413
|
-
`SELECT * FROM run_metadata WHERE thread_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?`
|
|
414
|
-
);
|
|
415
|
-
const rows = stmt.all(threadId, limit, offset);
|
|
416
|
-
|
|
417
|
-
const runs = rows.map(row => ({
|
|
418
|
-
run_id: row.run_id,
|
|
419
|
-
thread_id: row.thread_id,
|
|
420
|
-
agent_id: row.agent_id,
|
|
421
|
-
status: row.status,
|
|
422
|
-
created_at: toISOString(row.created_at),
|
|
423
|
-
updated_at: toISOString(row.updated_at)
|
|
424
|
-
}));
|
|
425
|
-
|
|
426
|
-
return {
|
|
427
|
-
runs,
|
|
428
|
-
total,
|
|
429
|
-
limit,
|
|
430
|
-
offset,
|
|
431
|
-
hasMore: offset + limit < total
|
|
432
|
-
};
|
|
433
|
-
},
|
|
434
|
-
|
|
435
|
-
// ============ SEARCH FUNCTIONS ============
|
|
436
|
-
|
|
437
|
-
searchThreads(filters = {}) {
|
|
438
|
-
const { metadata, status, dateRange, limit = 50, offset = 0 } = filters;
|
|
439
|
-
|
|
440
|
-
let whereClause = "status != 'deleted'";
|
|
441
|
-
const params = [];
|
|
442
|
-
|
|
443
|
-
if (status) {
|
|
444
|
-
whereClause += ' AND status = ?';
|
|
445
|
-
params.push(status);
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
if (dateRange?.start) {
|
|
449
|
-
whereClause += ' AND created_at >= ?';
|
|
450
|
-
params.push(new Date(dateRange.start).getTime());
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
if (dateRange?.end) {
|
|
454
|
-
whereClause += ' AND created_at <= ?';
|
|
455
|
-
params.push(new Date(dateRange.end).getTime());
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
if (metadata) {
|
|
459
|
-
// Simple metadata filter - check if JSON contains key-value pairs
|
|
460
|
-
for (const [key, value] of Object.entries(metadata)) {
|
|
461
|
-
whereClause += ` AND metadata LIKE ?`;
|
|
462
|
-
params.push(`%"${key}":"${value}"%`);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
const countStmt = prep(`SELECT COUNT(*) as count FROM conversations WHERE ${whereClause}`);
|
|
467
|
-
const total = countStmt.get(...params).count;
|
|
468
|
-
|
|
469
|
-
const stmt = prep(
|
|
470
|
-
`SELECT * FROM conversations WHERE ${whereClause} ORDER BY updated_at DESC LIMIT ? OFFSET ?`
|
|
471
|
-
);
|
|
472
|
-
const rows = stmt.all(...params, limit, offset);
|
|
473
|
-
|
|
474
|
-
const threads = rows.map(row => {
|
|
475
|
-
let metadata = {};
|
|
476
|
-
if (row.metadata) {
|
|
477
|
-
try { metadata = JSON.parse(row.metadata); } catch (e) {}
|
|
478
|
-
}
|
|
479
|
-
return {
|
|
480
|
-
thread_id: row.id,
|
|
481
|
-
created_at: toISOString(row.created_at),
|
|
482
|
-
updated_at: toISOString(row.updated_at),
|
|
483
|
-
metadata,
|
|
484
|
-
status: row.status || 'idle'
|
|
485
|
-
};
|
|
486
|
-
});
|
|
487
|
-
|
|
488
|
-
return {
|
|
489
|
-
threads,
|
|
490
|
-
total,
|
|
491
|
-
limit,
|
|
492
|
-
offset,
|
|
493
|
-
hasMore: offset + limit < total
|
|
494
|
-
};
|
|
495
|
-
},
|
|
496
|
-
|
|
497
|
-
searchAgents(filters = {}) {
|
|
498
|
-
// This would integrate with the agent discovery system
|
|
499
|
-
// For now, return empty array as agents are discovered dynamically
|
|
500
|
-
return [];
|
|
501
|
-
},
|
|
502
|
-
|
|
503
|
-
searchRuns(filters = {}) {
|
|
504
|
-
const { agent_id, thread_id, status, limit = 50, offset = 0 } = filters;
|
|
505
|
-
|
|
506
|
-
let whereClause = '1=1';
|
|
507
|
-
const params = [];
|
|
508
|
-
|
|
509
|
-
if (agent_id) {
|
|
510
|
-
whereClause += ' AND agent_id = ?';
|
|
511
|
-
params.push(agent_id);
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
if (thread_id) {
|
|
515
|
-
whereClause += ' AND thread_id = ?';
|
|
516
|
-
params.push(thread_id);
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
if (status) {
|
|
520
|
-
whereClause += ' AND status = ?';
|
|
521
|
-
params.push(status);
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
const countStmt = prep(`SELECT COUNT(*) as count FROM run_metadata WHERE ${whereClause}`);
|
|
525
|
-
const total = countStmt.get(...params).count;
|
|
526
|
-
|
|
527
|
-
const stmt = prep(
|
|
528
|
-
`SELECT * FROM run_metadata WHERE ${whereClause} ORDER BY created_at DESC LIMIT ? OFFSET ?`
|
|
529
|
-
);
|
|
530
|
-
const rows = stmt.all(...params, limit, offset);
|
|
531
|
-
|
|
532
|
-
const runs = rows.map(row => ({
|
|
533
|
-
run_id: row.run_id,
|
|
534
|
-
thread_id: row.thread_id,
|
|
535
|
-
agent_id: row.agent_id,
|
|
536
|
-
status: row.status,
|
|
537
|
-
created_at: toISOString(row.created_at),
|
|
538
|
-
updated_at: toISOString(row.updated_at)
|
|
539
|
-
}));
|
|
540
|
-
|
|
541
|
-
return {
|
|
542
|
-
runs,
|
|
543
|
-
total,
|
|
544
|
-
limit,
|
|
545
|
-
offset,
|
|
546
|
-
hasMore: offset + limit < total
|
|
547
|
-
};
|
|
548
|
-
}
|
|
549
|
-
};
|
|
550
|
-
}
|
|
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(flt = {}) {
|
|
148
|
+
return [];
|
|
149
|
+
},
|
|
150
|
+
searchRuns(flt = {}) {
|
|
151
|
+
const { agent_id, thread_id, status, limit = 50, offset = 0 } = flt;
|
|
152
|
+
let wh = '1=1', prm = [];
|
|
153
|
+
if (agent_id) { wh += ' AND agent_id = ?'; prm.push(agent_id); }
|
|
154
|
+
if (thread_id) { wh += ' AND thread_id = ?'; prm.push(thread_id); }
|
|
155
|
+
if (status) { wh += ' AND status = ?'; prm.push(status); }
|
|
156
|
+
const tot = prep(`SELECT COUNT(*) as count FROM run_metadata WHERE ${wh}`).get(...prm).count;
|
|
157
|
+
const rows = prep(`SELECT * FROM run_metadata WHERE ${wh} ORDER BY created_at DESC LIMIT ? OFFSET ?`).all(...prm, limit, offset);
|
|
158
|
+
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) }));
|
|
159
|
+
return { runs, total: tot, limit, offset, hasMore: offset + limit < tot };
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
}
|