-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.js
More file actions
67 lines (62 loc) · 2.01 KB
/
agents.js
File metadata and controls
67 lines (62 loc) · 2.01 KB
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
58
59
60
61
62
63
64
65
66
67
// Agent registry -- track agent status and capabilities
import { getDb } from './db.js';
/**
* Register or update an agent.
*/
export function upsertAgent(id, opts = {}) {
if (!id || typeof id !== 'string') throw new Error('Agent id must be a non-empty string');
const db = getDb();
db.prepare(`
INSERT INTO agents (id, name, status, session_key, capabilities)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
name = COALESCE(excluded.name, agents.name),
status = COALESCE(excluded.status, agents.status),
session_key = COALESCE(excluded.session_key, agents.session_key),
capabilities = COALESCE(excluded.capabilities, agents.capabilities),
last_seen_at = datetime('now')
`).run(
id,
opts.name || null,
opts.status || 'idle',
opts.session_key || null,
opts.capabilities ? JSON.stringify(opts.capabilities) : null
);
return getAgent(id);
}
/**
* Get an agent by ID.
*/
export function getAgent(id) {
const agent = getDb().prepare('SELECT * FROM agents WHERE id = ?').get(id);
if (agent && agent.capabilities) {
try { agent.capabilities = JSON.parse(agent.capabilities); } catch (e) { process.stderr.write('Warning: failed to parse capabilities JSON: ' + e.message + '\n'); }
}
return agent;
}
/**
* List all agents.
*/
export function listAgents() {
return getDb().prepare('SELECT * FROM agents ORDER BY id').all().map(a => {
if (a.capabilities) try { a.capabilities = JSON.parse(a.capabilities); } catch (e) { process.stderr.write('Warning: failed to parse capabilities JSON: ' + e.message + '\n'); }
return a;
});
}
/**
* Update agent status.
*/
export function setAgentStatus(id, status, sessionKey) {
getDb().prepare(`
UPDATE agents SET status = ?, session_key = ?, last_seen_at = datetime('now')
WHERE id = ?
`).run(status, sessionKey || null, id);
}
/**
* Mark agent as seen (heartbeat).
*/
export function touchAgent(id) {
getDb().prepare(`
UPDATE agents SET last_seen_at = datetime('now') WHERE id = ?
`).run(id);
}