open-agentic-web 0.0.0-stage → 1.0.1
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/LICENSE +17 -0
- package/README.md +286 -2
- package/bin/cli.mjs +351 -0
- package/package.json +48 -4
- package/src/actions.js +271 -0
- package/src/bridge.js +278 -0
- package/src/federation.js +340 -0
- package/src/index.js +10 -0
- package/src/validator.js +442 -0
- package/test/actions.test.mjs +140 -0
- package/test/federation.test.mjs +121 -0
- package/test/fixtures/hub-node/agent-manifest.json +20 -0
- package/test/fixtures/hub-node/bridge/action-bridge.html +1 -0
- package/test/fixtures/hub-node/hub.network.json +15 -0
- package/test/fixtures/hub-node/links.json +7 -0
- package/test/fixtures/hub-node/site-actions.js +22 -0
- package/test/fixtures/peer-node/agent-manifest.json +20 -0
- package/test/fixtures/peer-node/bridge/action-bridge.html +1 -0
- package/test/fixtures/peer-node/links.json +5 -0
- package/test/fixtures/peer-node/peer.network.json +15 -0
- package/test/fixtures/peer-node/site-actions.js +11 -0
- package/test/validator.test.mjs +93 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Open Agentic Web — Dynamic Swarm & Federation Engine
|
|
3
|
+
* Handles in-browser discovery, permission-checked mounting, and unmounting
|
|
4
|
+
* of peer agentic web nodes on the fly with 100% client-side privacy.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export class AgentSwarm {
|
|
8
|
+
constructor({ defaultRouter = "https://api.otari.ai/api/v1", defaultModel = "alibaba/qwen3.8-flash" } = {}) {
|
|
9
|
+
this.defaultRouter = defaultRouter;
|
|
10
|
+
this.defaultModel = defaultModel;
|
|
11
|
+
this.nodes = new Map();
|
|
12
|
+
this.listeners = new Set();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Register a new agentic node into the active swarm.
|
|
17
|
+
*/
|
|
18
|
+
registerNode(node) {
|
|
19
|
+
if (!node || !node.id) {
|
|
20
|
+
throw new Error("Node configuration must declare a unique 'id'.");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const normalizedNode = {
|
|
24
|
+
id: node.id,
|
|
25
|
+
name: node.name || node.id,
|
|
26
|
+
category: node.category || "General",
|
|
27
|
+
front_man: node.front_man || "agent",
|
|
28
|
+
node_url: node.node_url || "",
|
|
29
|
+
tools: Array.isArray(node.tools) ? node.tools : [],
|
|
30
|
+
icon: node.icon || "🌐",
|
|
31
|
+
description: node.description || "",
|
|
32
|
+
connectedAt: Date.now(),
|
|
33
|
+
status: "connected",
|
|
34
|
+
...node
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
this.nodes.set(normalizedNode.id, normalizedNode);
|
|
38
|
+
this._notify();
|
|
39
|
+
return normalizedNode;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Unregister / unlink an agent node from the swarm.
|
|
44
|
+
*/
|
|
45
|
+
unregisterNode(id) {
|
|
46
|
+
const existed = this.nodes.delete(id);
|
|
47
|
+
if (existed) this._notify();
|
|
48
|
+
return existed;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Search connected or catalog nodes by keyword or category.
|
|
53
|
+
*/
|
|
54
|
+
search({ query = "", category = "all" } = {}) {
|
|
55
|
+
const q = query.trim().toLowerCase();
|
|
56
|
+
const cat = category.trim().toLowerCase();
|
|
57
|
+
|
|
58
|
+
return Array.from(this.nodes.values()).filter(node => {
|
|
59
|
+
const matchCat = !cat || cat === "all" || node.category.toLowerCase() === cat;
|
|
60
|
+
const matchQuery = !q ||
|
|
61
|
+
node.name.toLowerCase().includes(q) ||
|
|
62
|
+
node.description.toLowerCase().includes(q) ||
|
|
63
|
+
node.category.toLowerCase().includes(q) ||
|
|
64
|
+
node.tools.some(t => t.toLowerCase().includes(q));
|
|
65
|
+
|
|
66
|
+
return matchCat && matchQuery;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Get all registered nodes.
|
|
72
|
+
*/
|
|
73
|
+
getNodes() {
|
|
74
|
+
return Array.from(this.nodes.values());
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Export dynamic links.json structure.
|
|
79
|
+
*/
|
|
80
|
+
exportLinks(hubNetworkName = "hub_orchestrator") {
|
|
81
|
+
const links = {
|
|
82
|
+
networks: {
|
|
83
|
+
[hubNetworkName]: {}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
for (const [id, node] of this.nodes.entries()) {
|
|
88
|
+
links.networks[hubNetworkName][id] = node.node_url;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return links;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Mount a remote conforming agentic web node from its public URL.
|
|
96
|
+
* If onConsent hook is provided, requests user approval before registering.
|
|
97
|
+
*/
|
|
98
|
+
async mountFromUrl(nodeUrl, { onConsent = null, fetchFn = globalThis.fetch } = {}) {
|
|
99
|
+
if (!fetchFn) {
|
|
100
|
+
throw new Error("fetch is required to mount remote agentic web nodes.");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const cleanUrl = nodeUrl.replace(/\/$/, "");
|
|
104
|
+
const manifestEndpoint = cleanUrl.endsWith(".json")
|
|
105
|
+
? cleanUrl
|
|
106
|
+
: `${cleanUrl}/agent-manifest.json`;
|
|
107
|
+
|
|
108
|
+
const response = await fetchFn(manifestEndpoint);
|
|
109
|
+
if (!response.ok) {
|
|
110
|
+
throw new Error(`Failed to fetch manifest from '${manifestEndpoint}' (HTTP ${response.status})`);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const manifest = await response.json();
|
|
114
|
+
const network = manifest.networks?.[0];
|
|
115
|
+
if (!network) {
|
|
116
|
+
throw new Error(`Manifest at '${manifestEndpoint}' does not declare any networks.`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const candidateNode = {
|
|
120
|
+
id: manifest.name || network.name || `node_${Date.now()}`,
|
|
121
|
+
name: manifest.brand || manifest.description || manifest.name || network.name,
|
|
122
|
+
front_man: network.front_man,
|
|
123
|
+
node_url: cleanUrl,
|
|
124
|
+
category: manifest.category || (network.tags && network.tags[0]) || "Specialist",
|
|
125
|
+
description: manifest.description || network.description || "",
|
|
126
|
+
tools: manifest.actions?.agentTools || []
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// User consent check
|
|
130
|
+
if (typeof onConsent === "function") {
|
|
131
|
+
const approved = await onConsent({
|
|
132
|
+
candidate: candidateNode,
|
|
133
|
+
manifest,
|
|
134
|
+
origin: new URL(cleanUrl).origin
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
if (!approved) {
|
|
138
|
+
throw new Error(`Mounting of node '${candidateNode.name}' was declined by user consent.`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return this.registerNode(candidateNode);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Export all registered tools across connected nodes as OpenAI-compatible function calling schemas.
|
|
147
|
+
*/
|
|
148
|
+
getOpenAITools(toolSubset = null) {
|
|
149
|
+
const formatted = [];
|
|
150
|
+
const allowed = toolSubset ? new Set(toolSubset) : null;
|
|
151
|
+
|
|
152
|
+
for (const node of this.nodes.values()) {
|
|
153
|
+
for (const tool of (node.tools || [])) {
|
|
154
|
+
const name = typeof tool === "string" ? tool : tool.name;
|
|
155
|
+
if (allowed && !allowed.has(name)) continue;
|
|
156
|
+
|
|
157
|
+
const description = typeof tool === "object" && tool.description
|
|
158
|
+
? tool.description
|
|
159
|
+
: `Action provided by specialist agent '${node.name}' (${node.category})`;
|
|
160
|
+
|
|
161
|
+
const parameters = typeof tool === "object" && tool.inputSchema
|
|
162
|
+
? tool.inputSchema
|
|
163
|
+
: { type: "object", properties: {}, additionalProperties: true };
|
|
164
|
+
|
|
165
|
+
formatted.push({
|
|
166
|
+
type: "function",
|
|
167
|
+
function: {
|
|
168
|
+
name,
|
|
169
|
+
description,
|
|
170
|
+
parameters
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return formatted;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Execute an in-browser agent turn with BYOK LLM inference and autonomous tool execution.
|
|
180
|
+
* Compatible with Mozilla Otari, OpenRouter, and standard OpenAI-compatible endpoints.
|
|
181
|
+
*/
|
|
182
|
+
async runTurn({
|
|
183
|
+
prompt,
|
|
184
|
+
messages = null,
|
|
185
|
+
systemInstructions = "You are a helpful autonomous concierge on the Open Agentic Web.",
|
|
186
|
+
model = this.defaultModel,
|
|
187
|
+
routerUrl = this.defaultRouter,
|
|
188
|
+
apiKey = null,
|
|
189
|
+
actionExecutor = null,
|
|
190
|
+
maxSteps = 5,
|
|
191
|
+
onStep = null,
|
|
192
|
+
fetchFn = globalThis.fetch
|
|
193
|
+
}) {
|
|
194
|
+
if (!fetchFn) {
|
|
195
|
+
throw new Error("fetch is required to execute LLM turns.");
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const conversation = messages
|
|
199
|
+
? [...messages]
|
|
200
|
+
: [
|
|
201
|
+
{ role: "system", content: systemInstructions },
|
|
202
|
+
{ role: "user", content: prompt }
|
|
203
|
+
];
|
|
204
|
+
|
|
205
|
+
const tools = this.getOpenAITools();
|
|
206
|
+
let step = 0;
|
|
207
|
+
let toolCallsCount = 0;
|
|
208
|
+
|
|
209
|
+
// Offline / Mock mode support for testing and airgapped environments
|
|
210
|
+
if (apiKey === "mock" || routerUrl === "mock") {
|
|
211
|
+
const mockToolCall = tools.length > 0 && prompt?.toLowerCase().includes("book")
|
|
212
|
+
? { id: "call_mock_1", function: { name: tools[0].function.name, arguments: "{}" } }
|
|
213
|
+
: null;
|
|
214
|
+
|
|
215
|
+
if (mockToolCall && actionExecutor) {
|
|
216
|
+
const result = await actionExecutor(mockToolCall.function.name, {});
|
|
217
|
+
conversation.push({ role: "assistant", tool_calls: [mockToolCall], content: null });
|
|
218
|
+
conversation.push({ role: "tool", tool_call_id: mockToolCall.id, name: mockToolCall.function.name, content: JSON.stringify(result) });
|
|
219
|
+
conversation.push({ role: "assistant", content: `Mock synthesis completed for ${mockToolCall.function.name}.` });
|
|
220
|
+
return { ok: true, text: `Mock synthesis completed for ${mockToolCall.function.name}.`, messages: conversation, stepsCount: 2, toolCallsCount: 1 };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const reply = `Mock offline response for: ${prompt}`;
|
|
224
|
+
conversation.push({ role: "assistant", content: reply });
|
|
225
|
+
return { ok: true, text: reply, messages: conversation, stepsCount: 1, toolCallsCount: 0 };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const endpoint = routerUrl.endsWith("/chat/completions")
|
|
229
|
+
? routerUrl
|
|
230
|
+
: `${routerUrl.replace(/\/$/, "")}/chat/completions`;
|
|
231
|
+
|
|
232
|
+
while (step < maxSteps) {
|
|
233
|
+
step++;
|
|
234
|
+
|
|
235
|
+
const payload = {
|
|
236
|
+
model,
|
|
237
|
+
messages: conversation,
|
|
238
|
+
tools: tools.length > 0 ? tools : undefined
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const headers = {
|
|
242
|
+
"Content-Type": "application/json"
|
|
243
|
+
};
|
|
244
|
+
if (apiKey) {
|
|
245
|
+
headers["Authorization"] = `Bearer ${apiKey}`;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const res = await fetchFn(endpoint, {
|
|
249
|
+
method: "POST",
|
|
250
|
+
headers,
|
|
251
|
+
body: JSON.stringify(payload)
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
if (!res.ok) {
|
|
255
|
+
const errText = await res.text().catch(() => "");
|
|
256
|
+
throw new Error(`LLM inference failed at '${endpoint}' (HTTP ${res.status}): ${errText}`);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const data = await res.json();
|
|
260
|
+
const choice = data.choices?.[0];
|
|
261
|
+
const message = choice?.message;
|
|
262
|
+
|
|
263
|
+
if (!message) {
|
|
264
|
+
throw new Error("Invalid response format from LLM router: missing choices[0].message");
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
conversation.push(message);
|
|
268
|
+
|
|
269
|
+
// Check if model called tools
|
|
270
|
+
if (message.tool_calls && message.tool_calls.length > 0) {
|
|
271
|
+
for (const call of message.tool_calls) {
|
|
272
|
+
toolCallsCount++;
|
|
273
|
+
const toolName = call.function.name;
|
|
274
|
+
let args = {};
|
|
275
|
+
try {
|
|
276
|
+
args = JSON.parse(call.function.arguments || "{}");
|
|
277
|
+
} catch {
|
|
278
|
+
args = {};
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
let result;
|
|
282
|
+
if (typeof actionExecutor === "function") {
|
|
283
|
+
try {
|
|
284
|
+
result = await actionExecutor(toolName, args);
|
|
285
|
+
} catch (err) {
|
|
286
|
+
result = { ok: false, error: err.message };
|
|
287
|
+
}
|
|
288
|
+
} else {
|
|
289
|
+
result = { ok: true, message: `Tool '${toolName}' executed in swarm context.` };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (typeof onStep === "function") {
|
|
293
|
+
try {
|
|
294
|
+
onStep({ step, tool: toolName, arguments: args, result });
|
|
295
|
+
} catch {}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
conversation.push({
|
|
299
|
+
role: "tool",
|
|
300
|
+
tool_call_id: call.id,
|
|
301
|
+
name: toolName,
|
|
302
|
+
content: typeof result === "string" ? result : JSON.stringify(result)
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
} else {
|
|
306
|
+
// Model provided final answer
|
|
307
|
+
return {
|
|
308
|
+
ok: true,
|
|
309
|
+
text: message.content || "",
|
|
310
|
+
messages: conversation,
|
|
311
|
+
stepsCount: step,
|
|
312
|
+
toolCallsCount
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return {
|
|
318
|
+
ok: true,
|
|
319
|
+
text: conversation[conversation.length - 1]?.content || "Turn reached max steps.",
|
|
320
|
+
messages: conversation,
|
|
321
|
+
stepsCount: step,
|
|
322
|
+
toolCallsCount
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Subscribe to swarm changes.
|
|
328
|
+
*/
|
|
329
|
+
subscribe(callback) {
|
|
330
|
+
this.listeners.add(callback);
|
|
331
|
+
return () => this.listeners.delete(callback);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
_notify() {
|
|
335
|
+
const nodes = this.getNodes();
|
|
336
|
+
for (const cb of this.listeners) {
|
|
337
|
+
try { cb(nodes); } catch (e) { console.error("Swarm listener error:", e); }
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Open Agentic Web SDK & Conformance Toolkit
|
|
3
|
+
* Official developer library for building, validating, and federating
|
|
4
|
+
* autonomous web agents adhering to Cognizant / Mozilla / MIT NANDA MVP Profile 0.3.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export * from "./actions.js";
|
|
8
|
+
export * from "./bridge.js";
|
|
9
|
+
export * from "./validator.js";
|
|
10
|
+
export * from "./federation.js";
|