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
68
69
70
71
72
73
74
75
76
77
78
79
|
// module-agent-registry.js — VFS-backed agent directory
// Command: agent-list, agent-register, agent-unregister, agent-status
class AgentRegistryModule {
constructor() {
this.commands = {};
this.terminal = null;
}
registerCommands() {
this.commands['agent-list'] = () => this.listAgents();
this.commands['agent-status'] = (args) => this.showAgentStatus(args[0]);
this.commands['agent-register'] = (args) => this.registerAgent(args);
this.commands['agent-unregister'] = (args) => this.unregisterAgent(args[0]);
}
init(terminal) {
this.terminal = terminal;
if (!this.terminal.fs.readFile('/agents/')) {
this.terminal.fs.mkdir('agents');
}
this.terminal.print('📋 Agent Registry loaded', 'info');
}
listAgents() {
const agents = this.terminal.fs.ls('/agents/');
if (!agents || agents.length === 0) {
return this.terminal.print('No agents registered', 'info');
}
this.terminal.print(`📋 Registered Agents (${agents.length}):`, 'header');
agents.forEach(a => {
if (a.type === 'dir' && a.name !== 'configured') {
const data = this.terminal.fs.readFile(`/agents/${a.name}/status.json`);
const s = data ? JSON.parse(data) : { status: 'unknown', capabilities: [] };
this.terminal.print(` ${a.name} — ${s.status} (${(s.capabilities || []).length} capabilities)`, 'info');
}
});
}
registerAgent(args) {
const id = args[1] || `agent-${Date.now()}`;
const capabilities = args.slice(2).length ? args.slice(2) : ['list_files', 'read_file'];
const agentDir = `agents/${id}`;
if (!this.terminal.fs.readFile('/' + agentDir + '/')) this.terminal.fs.mkdir(agentDir);
this.terminal.fs.writeFile(`${agentDir}/status.json`, JSON.stringify({
id,
status: 'idle',
capabilities,
registered: new Date().toISOString(),
lastHeartbeat: new Date().toISOString()
}));
this.terminal.print(`✅ Agent '${id}' registered with ${capabilities.length} capabilities`, 'success');
}
unregisterAgent(id) {
const ok = this.terminal.fs.deleteFile(`agents/${id}`);
if (ok) {
this.terminal.print(`✅ Agent '${id}' unregistered`, 'success');
} else {
this.terminal.print(`❌ Agent '${id}' not found`, 'error');
}
}
showAgentStatus(id) {
if (!id) return this.terminal.print('Usage: agent-status <agent-id>', 'error');
const data = this.terminal.fs.readFile(`/agents/${id}/status.json`);
if (!data) return this.terminal.print(`❌ Agent '${id}' not found`, 'error');
const s = JSON.parse(data);
this.terminal.print(`Agent: ${s.id}`, 'header');
this.terminal.print(` Status: ${s.status}`, 'info');
this.terminal.print(` Capabilities: ${(s.capabilities || []).join(', ')}`, 'info');
this.terminal.print(` Registered: ${s.registered}`, 'info');
this.terminal.print(` Last Heartbeat: ${s.lastHeartbeat}`, 'info');
}
getAgent(id) {
const data = this.terminal.fs.readFile(`/agents/${id}/status.json`);
return data ? JSON.parse(data) : null;
}
updateHeartbeat(id) {
const agent = this.getAgent(id);
if (!agent) return false;
agent.lastHeartbeat = new Date().toISOString();
this.terminal.fs.writeFile(`agents/${id}/status.json`, JSON.stringify(agent));
return true;
}
}
|