-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscovery.js
More file actions
146 lines (133 loc) · 3.6 KB
/
Copy pathdiscovery.js
File metadata and controls
146 lines (133 loc) · 3.6 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
68
69
70
71
72
73
74
75
76
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// ==========================================
// Agent Discovery — find and register AI agents on the network
// ==========================================
// Maintains a registry of known agents. Agents can announce themselves
// and discover other agents by querying the local registry or a
// shared discovery server.
import config from './config.js';
import { JsonStore } from './storage.js';
import { createAgentMessage } from './protocol.js';
import { auditLog } from './audit-log.js';
const registry = new JsonStore('agent-registry.json');
/**
* Register an agent in the local directory.
*/
export function registerAgent({ agentId, agentName, phone, providers = [], capabilities = [] }) {
const entry = {
agentId,
agentName,
phone,
providers, // e.g. ['openai', 'anthropic']
capabilities, // e.g. ['chat', 'code', 'research']
registeredAt: new Date().toISOString(),
lastSeenAt: new Date().toISOString(),
status: 'online',
};
registry.set(agentId, entry);
auditLog('INFO', 'agent-registered', { agentId, agentName });
return entry;
}
/**
* Update an agent's last-seen timestamp.
*/
export function markAgentSeen(agentId) {
const agent = registry.get(agentId);
if (agent) {
agent.lastSeenAt = new Date().toISOString();
agent.status = 'online';
registry.set(agentId, agent);
}
}
/**
* Mark an agent as offline.
*/
export function markAgentOffline(agentId) {
const agent = registry.get(agentId);
if (agent) {
agent.status = 'offline';
registry.set(agentId, agent);
}
}
/**
* Get all known agents.
*/
export function listAgents() {
return registry.values();
}
/**
* Get a specific agent by ID.
*/
export function getAgent(agentId) {
return registry.get(agentId);
}
/**
* Find agents by capability.
*/
export function findAgentsByCapability(capability) {
return registry.values().filter(a =>
a.capabilities.includes(capability) && a.status === 'online'
);
}
/**
* Find agents by provider.
*/
export function findAgentsByProvider(provider) {
return registry.values().filter(a =>
a.providers.includes(provider) && a.status === 'online'
);
}
/**
* Remove an agent from the registry.
*/
export function unregisterAgent(agentId) {
registry.delete(agentId);
auditLog('INFO', 'agent-unregistered', { agentId });
}
/**
* Register this agent (self) on startup.
*/
export function registerSelf() {
return registerAgent({
agentId: config.agent.id,
agentName: config.agent.name,
phone: 'self',
providers: [config.aiProvider],
capabilities: ['chat', 'groups'],
});
}
/**
* Create an announcement message for other agents.
* Send this to new contacts to introduce yourself.
*/
export function createAnnouncement() {
return createAgentMessage({
from: {
agentId: config.agent.id,
agentName: config.agent.name,
},
to: { agentId: 'broadcast' },
intent: 'announce',
payload: {
agentId: config.agent.id,
agentName: config.agent.name,
providers: [config.aiProvider],
capabilities: ['chat', 'groups'],
message: `Hello! I am ${config.agent.name}, an AI agent on AI COMMS.`,
},
});
}
/**
* Handle an incoming announcement from another agent.
*/
export function handleAnnouncement(agentMsg) {
if (agentMsg.intent !== 'announce') return false;
const payload = agentMsg.payload;
registerAgent({
agentId: payload.agentId || agentMsg.from?.agentId,
agentName: payload.agentName || agentMsg.from?.agentName,
phone: agentMsg.from?.phone || '',
providers: payload.providers || [],
capabilities: payload.capabilities || [],
});
return true;
}