web-agent-bridge 2.2.0 → 2.3.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/README.ar.md +7 -0
- package/README.md +7 -0
- package/package.json +12 -4
- package/public/commander-dashboard.html +243 -0
- package/public/css/premium.css +317 -317
- package/public/demo.html +259 -259
- package/public/index.html +644 -592
- package/public/llms.txt +1 -0
- package/public/mesh-dashboard.html +328 -0
- package/public/premium-dashboard.html +2487 -2487
- package/public/premium.html +791 -791
- package/public/script/wab.min.js +181 -6
- package/script/ai-agent-bridge.js +196 -0
- package/sdk/agent-mesh.js +449 -0
- package/sdk/commander.js +262 -0
- package/sdk/index.js +260 -259
- package/sdk/package.json +1 -1
- package/server/index.js +13 -1
- package/server/migrations/002_premium_features.sql +418 -418
- package/server/models/db.js +24 -5
- package/server/routes/admin-premium.js +671 -671
- package/server/routes/commander.js +316 -0
- package/server/routes/mesh.js +469 -0
- package/server/routes/premium-v2.js +686 -686
- package/server/routes/premium.js +724 -724
- package/server/services/agent-learning.js +575 -0
- package/server/services/agent-memory.js +625 -625
- package/server/services/agent-mesh.js +539 -0
- package/server/services/agent-symphony.js +711 -0
- package/server/services/commander.js +738 -0
- package/server/services/edge-compute.js +440 -0
- package/server/services/local-ai.js +389 -0
- package/server/services/plugins.js +747 -747
- package/server/services/self-healing.js +843 -843
- package/server/services/swarm.js +788 -788
- package/server/services/vision.js +871 -871
- package/public/admin/dashboard.html +0 -848
- package/public/admin/login.html +0 -84
- package/public/video/tutorial.mp4 +0 -0
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WABAgentMesh — SDK Client for the Private Agent Mesh
|
|
3
|
+
*
|
|
4
|
+
* High-level client for agents to join the mesh, communicate,
|
|
5
|
+
* share knowledge, participate in votes, and learn from decisions.
|
|
6
|
+
* Includes automatic heartbeat, reconnection, and response validation.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
class WABAgentMesh {
|
|
10
|
+
/**
|
|
11
|
+
* @param {object} options
|
|
12
|
+
* @param {string} options.serverUrl - WAB server URL
|
|
13
|
+
* @param {string} options.role - Agent role (e.g., 'monitor', 'optimizer')
|
|
14
|
+
* @param {string} [options.displayName] - Human-readable agent name
|
|
15
|
+
* @param {string[]} [options.capabilities] - List of capabilities
|
|
16
|
+
* @param {string} [options.siteId] - Site identifier
|
|
17
|
+
* @param {number} [options.heartbeatInterval=30000] - Heartbeat interval in ms
|
|
18
|
+
*/
|
|
19
|
+
constructor(options = {}) {
|
|
20
|
+
this.serverUrl = (options.serverUrl || '').replace(/\/$/, '');
|
|
21
|
+
this.role = options.role || 'agent';
|
|
22
|
+
this.displayName = options.displayName || null;
|
|
23
|
+
this.capabilities = options.capabilities || [];
|
|
24
|
+
this.siteId = options.siteId || null;
|
|
25
|
+
this.heartbeatInterval = options.heartbeatInterval || 30000;
|
|
26
|
+
|
|
27
|
+
this.agentId = null;
|
|
28
|
+
this._heartbeatTimer = null;
|
|
29
|
+
this._retryCount = 0;
|
|
30
|
+
this._maxRetries = 5;
|
|
31
|
+
this._listeners = {};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// ─── Lifecycle ───────────────────────────────────────────────────
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Join the mesh. Registers the agent and starts heartbeat.
|
|
38
|
+
* @returns {Promise<object>} Registered agent data
|
|
39
|
+
*/
|
|
40
|
+
async join() {
|
|
41
|
+
const res = await this._post('/api/mesh/agents', {
|
|
42
|
+
siteId: this.siteId,
|
|
43
|
+
role: this.role,
|
|
44
|
+
displayName: this.displayName,
|
|
45
|
+
capabilities: this.capabilities,
|
|
46
|
+
});
|
|
47
|
+
const data = await this._json(res);
|
|
48
|
+
this.agentId = data.agent.id;
|
|
49
|
+
this._retryCount = 0;
|
|
50
|
+
this._startHeartbeat();
|
|
51
|
+
this._emit('joined', data.agent);
|
|
52
|
+
return data.agent;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Leave the mesh. Deregisters and stops heartbeat.
|
|
57
|
+
*/
|
|
58
|
+
async leave() {
|
|
59
|
+
this._stopHeartbeat();
|
|
60
|
+
if (this.agentId) {
|
|
61
|
+
try {
|
|
62
|
+
await this._delete(`/api/mesh/agents/${this.agentId}`);
|
|
63
|
+
} catch (_) { /* ignore errors during cleanup */ }
|
|
64
|
+
this._emit('left', { agentId: this.agentId });
|
|
65
|
+
this.agentId = null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Destroy the client — leave mesh and clean up all resources.
|
|
71
|
+
*/
|
|
72
|
+
async destroy() {
|
|
73
|
+
await this.leave();
|
|
74
|
+
this._listeners = {};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ─── Messaging ─────────────────────────────────────────────────
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Publish a message to a channel.
|
|
81
|
+
*/
|
|
82
|
+
async publish(type, subject, payload = {}, options = {}) {
|
|
83
|
+
this._requireJoined();
|
|
84
|
+
const res = await this._post('/api/mesh/messages', {
|
|
85
|
+
channelName: options.channel || 'general',
|
|
86
|
+
senderId: this.agentId,
|
|
87
|
+
targetId: options.targetId || null,
|
|
88
|
+
type,
|
|
89
|
+
subject,
|
|
90
|
+
payload,
|
|
91
|
+
priority: options.priority || 0,
|
|
92
|
+
ttl: options.ttl,
|
|
93
|
+
});
|
|
94
|
+
return (await this._json(res)).message;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Get messages for this agent.
|
|
99
|
+
*/
|
|
100
|
+
async getMessages(limit = 50) {
|
|
101
|
+
this._requireJoined();
|
|
102
|
+
const res = await this._get(`/api/mesh/messages?agentId=${this.agentId}&limit=${limit}`);
|
|
103
|
+
return (await this._json(res)).messages;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Acknowledge a message.
|
|
108
|
+
*/
|
|
109
|
+
async acknowledge(messageId) {
|
|
110
|
+
const res = await this._post(`/api/mesh/messages/${encodeURIComponent(messageId)}/acknowledge`);
|
|
111
|
+
return (await this._json(res));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Get unread count and breakdown by channel.
|
|
116
|
+
*/
|
|
117
|
+
async getUnread() {
|
|
118
|
+
this._requireJoined();
|
|
119
|
+
const res = await this._get(`/api/mesh/agents/${this.agentId}/unread`);
|
|
120
|
+
return await this._json(res);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Broadcast an alert to all agents.
|
|
125
|
+
*/
|
|
126
|
+
async alert(subject, details, priority = 2) {
|
|
127
|
+
this._requireJoined();
|
|
128
|
+
const res = await this._post('/api/mesh/alert', {
|
|
129
|
+
senderId: this.agentId, subject, details, priority,
|
|
130
|
+
});
|
|
131
|
+
return (await this._json(res)).message;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Share a tactic with the mesh.
|
|
136
|
+
*/
|
|
137
|
+
async shareTactic(name, tactic) {
|
|
138
|
+
this._requireJoined();
|
|
139
|
+
const res = await this._post('/api/mesh/tactic', {
|
|
140
|
+
senderId: this.agentId, name, tactic,
|
|
141
|
+
});
|
|
142
|
+
return (await this._json(res)).message;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Request help from other agents.
|
|
147
|
+
*/
|
|
148
|
+
async requestHelp(problem, context = {}) {
|
|
149
|
+
this._requireJoined();
|
|
150
|
+
const res = await this._post('/api/mesh/help', {
|
|
151
|
+
senderId: this.agentId, problem, context,
|
|
152
|
+
});
|
|
153
|
+
return (await this._json(res)).message;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ─── Knowledge ─────────────────────────────────────────────────
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Share knowledge with the mesh.
|
|
160
|
+
*/
|
|
161
|
+
async shareKnowledge(type, key, value, options = {}) {
|
|
162
|
+
this._requireJoined();
|
|
163
|
+
const res = await this._post('/api/mesh/knowledge', {
|
|
164
|
+
agentId: this.agentId,
|
|
165
|
+
type,
|
|
166
|
+
domain: options.domain,
|
|
167
|
+
key,
|
|
168
|
+
value,
|
|
169
|
+
confidence: options.confidence,
|
|
170
|
+
source: options.source,
|
|
171
|
+
});
|
|
172
|
+
return (await this._json(res)).knowledge;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Query knowledge by domain and/or type.
|
|
177
|
+
*/
|
|
178
|
+
async queryKnowledge(params = {}) {
|
|
179
|
+
const qs = new URLSearchParams();
|
|
180
|
+
if (params.domain) qs.set('domain', params.domain);
|
|
181
|
+
if (params.type) qs.set('type', params.type);
|
|
182
|
+
if (params.agentId) qs.set('agentId', params.agentId);
|
|
183
|
+
if (params.limit) qs.set('limit', params.limit);
|
|
184
|
+
const res = await this._get(`/api/mesh/knowledge?${qs.toString()}`);
|
|
185
|
+
return (await this._json(res)).knowledge;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Search knowledge by keyword.
|
|
190
|
+
*/
|
|
191
|
+
async searchKnowledge(query, limit = 20) {
|
|
192
|
+
const res = await this._get(`/api/mesh/knowledge/search/${encodeURIComponent(query)}?limit=${limit}`);
|
|
193
|
+
return (await this._json(res)).knowledge;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Get knowledge domains with counts.
|
|
198
|
+
*/
|
|
199
|
+
async getKnowledgeDomains() {
|
|
200
|
+
const res = await this._get('/api/mesh/knowledge-domains');
|
|
201
|
+
return (await this._json(res)).domains;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Verify a knowledge entry.
|
|
206
|
+
*/
|
|
207
|
+
async verifyKnowledge(knowledgeId, confidence) {
|
|
208
|
+
this._requireJoined();
|
|
209
|
+
const res = await this._post(`/api/mesh/knowledge/${encodeURIComponent(knowledgeId)}/verify`, {
|
|
210
|
+
verifierId: this.agentId, confidence,
|
|
211
|
+
});
|
|
212
|
+
return await this._json(res);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ─── Voting ────────────────────────────────────────────────────
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Create a vote for other agents to participate in.
|
|
219
|
+
*/
|
|
220
|
+
async createVote(subject, options, deadlineSeconds = 60) {
|
|
221
|
+
this._requireJoined();
|
|
222
|
+
const res = await this._post('/api/mesh/votes', {
|
|
223
|
+
senderId: this.agentId, subject, options, deadlineSeconds,
|
|
224
|
+
});
|
|
225
|
+
return (await this._json(res)).vote;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Cast a vote on an existing vote message.
|
|
230
|
+
*/
|
|
231
|
+
async castVote(voteMessageId, choice, weight = 1, reason = '') {
|
|
232
|
+
this._requireJoined();
|
|
233
|
+
const res = await this._post(`/api/mesh/votes/${encodeURIComponent(voteMessageId)}/cast`, {
|
|
234
|
+
voterId: this.agentId, choice, weight, reason,
|
|
235
|
+
});
|
|
236
|
+
return (await this._json(res)).result;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Get the tally for a vote.
|
|
241
|
+
*/
|
|
242
|
+
async tallyVote(voteMessageId) {
|
|
243
|
+
const res = await this._get(`/api/mesh/votes/${encodeURIComponent(voteMessageId)}/tally`);
|
|
244
|
+
return (await this._json(res)).tally;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// ─── Learning Integration ─────────────────────────────────────
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Record a decision for learning.
|
|
251
|
+
*/
|
|
252
|
+
async recordDecision(domain, action, context = {}, features = {}) {
|
|
253
|
+
const res = await this._post('/api/mesh/learning/decisions', {
|
|
254
|
+
siteId: this.siteId || 'default',
|
|
255
|
+
agentId: this.agentId || this.role,
|
|
256
|
+
domain, action, context, features,
|
|
257
|
+
});
|
|
258
|
+
return await this._json(res);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Provide feedback on a decision.
|
|
263
|
+
*/
|
|
264
|
+
async feedback(decisionId, outcome, reward) {
|
|
265
|
+
const res = await this._post('/api/mesh/learning/feedback', {
|
|
266
|
+
decisionId, outcome, reward,
|
|
267
|
+
});
|
|
268
|
+
return await this._json(res);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Get a recommendation for the best action.
|
|
273
|
+
*/
|
|
274
|
+
async recommend(domain, actions, context = {}) {
|
|
275
|
+
const res = await this._post('/api/mesh/learning/recommend', {
|
|
276
|
+
siteId: this.siteId || 'default',
|
|
277
|
+
agentId: this.agentId || this.role,
|
|
278
|
+
domain, actions, context,
|
|
279
|
+
});
|
|
280
|
+
return await this._json(res);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Get learning stats.
|
|
285
|
+
*/
|
|
286
|
+
async getLearningStats() {
|
|
287
|
+
const res = await this._get(`/api/mesh/learning/stats?siteId=${this.siteId || 'default'}&agentId=${this.agentId || this.role}`);
|
|
288
|
+
return (await this._json(res)).stats;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// ─── Symphony Integration ─────────────────────────────────────
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Execute a symphony composition.
|
|
295
|
+
*/
|
|
296
|
+
async compose(template, inputData = {}, schema = null) {
|
|
297
|
+
const res = await this._post('/api/mesh/symphony/compose', {
|
|
298
|
+
siteId: this.siteId || 'default',
|
|
299
|
+
template, inputData, schema,
|
|
300
|
+
});
|
|
301
|
+
return await this._json(res);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Get available symphony templates.
|
|
306
|
+
*/
|
|
307
|
+
async getTemplates() {
|
|
308
|
+
const res = await this._get('/api/mesh/symphony/templates');
|
|
309
|
+
return (await this._json(res)).templates;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// ─── Mesh Info ─────────────────────────────────────────────────
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Get all active agents in the mesh.
|
|
316
|
+
*/
|
|
317
|
+
async getAgents(role = null) {
|
|
318
|
+
const qs = role ? `?role=${encodeURIComponent(role)}` : '';
|
|
319
|
+
const res = await this._get(`/api/mesh/agents${qs}`);
|
|
320
|
+
return (await this._json(res)).agents;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Get mesh stats.
|
|
325
|
+
*/
|
|
326
|
+
async getStats() {
|
|
327
|
+
const res = await this._get('/api/mesh/stats');
|
|
328
|
+
return (await this._json(res)).stats;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Update own agent metadata.
|
|
333
|
+
*/
|
|
334
|
+
async updateMeta(metadata) {
|
|
335
|
+
this._requireJoined();
|
|
336
|
+
const res = await this._patch(`/api/mesh/agents/${this.agentId}/meta`, { metadata });
|
|
337
|
+
return await this._json(res);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Set own status.
|
|
342
|
+
*/
|
|
343
|
+
async setStatus(status) {
|
|
344
|
+
this._requireJoined();
|
|
345
|
+
const res = await this._put(`/api/mesh/agents/${this.agentId}/status`, { status });
|
|
346
|
+
return await this._json(res);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// ─── Events ────────────────────────────────────────────────────
|
|
350
|
+
|
|
351
|
+
on(event, fn) {
|
|
352
|
+
if (!this._listeners[event]) this._listeners[event] = [];
|
|
353
|
+
this._listeners[event].push(fn);
|
|
354
|
+
return this;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
off(event, fn) {
|
|
358
|
+
if (!this._listeners[event]) return this;
|
|
359
|
+
this._listeners[event] = this._listeners[event].filter((f) => f !== fn);
|
|
360
|
+
return this;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
_emit(event, data) {
|
|
364
|
+
if (this._listeners[event]) {
|
|
365
|
+
for (const fn of this._listeners[event]) {
|
|
366
|
+
try { fn(data); } catch (e) { console.error(`[WABAgentMesh] listener error on ${event}:`, e.message); }
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// ─── Internal ──────────────────────────────────────────────────
|
|
372
|
+
|
|
373
|
+
_requireJoined() {
|
|
374
|
+
if (!this.agentId) throw new Error('Agent not joined. Call join() first.');
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
_startHeartbeat() {
|
|
378
|
+
this._stopHeartbeat();
|
|
379
|
+
this._heartbeatTimer = setInterval(async () => {
|
|
380
|
+
try {
|
|
381
|
+
await this._post(`/api/mesh/agents/${this.agentId}/heartbeat`);
|
|
382
|
+
this._retryCount = 0;
|
|
383
|
+
} catch (e) {
|
|
384
|
+
this._retryCount++;
|
|
385
|
+
this._emit('heartbeat-error', { retryCount: this._retryCount, error: e.message });
|
|
386
|
+
if (this._retryCount >= this._maxRetries) {
|
|
387
|
+
this._stopHeartbeat();
|
|
388
|
+
this._emit('disconnected', { reason: 'heartbeat-failed', retries: this._retryCount });
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}, this.heartbeatInterval);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
_stopHeartbeat() {
|
|
395
|
+
if (this._heartbeatTimer) {
|
|
396
|
+
clearInterval(this._heartbeatTimer);
|
|
397
|
+
this._heartbeatTimer = null;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
async _post(path, body) {
|
|
402
|
+
const fetch = globalThis.fetch || require('node-fetch');
|
|
403
|
+
return fetch(`${this.serverUrl}${path}`, {
|
|
404
|
+
method: 'POST',
|
|
405
|
+
headers: { 'Content-Type': 'application/json' },
|
|
406
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
async _get(path) {
|
|
411
|
+
const fetch = globalThis.fetch || require('node-fetch');
|
|
412
|
+
return fetch(`${this.serverUrl}${path}`);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
async _put(path, body) {
|
|
416
|
+
const fetch = globalThis.fetch || require('node-fetch');
|
|
417
|
+
return fetch(`${this.serverUrl}${path}`, {
|
|
418
|
+
method: 'PUT',
|
|
419
|
+
headers: { 'Content-Type': 'application/json' },
|
|
420
|
+
body: JSON.stringify(body),
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
async _patch(path, body) {
|
|
425
|
+
const fetch = globalThis.fetch || require('node-fetch');
|
|
426
|
+
return fetch(`${this.serverUrl}${path}`, {
|
|
427
|
+
method: 'PATCH',
|
|
428
|
+
headers: { 'Content-Type': 'application/json' },
|
|
429
|
+
body: JSON.stringify(body),
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
async _delete(path) {
|
|
434
|
+
const fetch = globalThis.fetch || require('node-fetch');
|
|
435
|
+
return fetch(`${this.serverUrl}${path}`, { method: 'DELETE' });
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
async _json(res) {
|
|
439
|
+
if (!res.ok) {
|
|
440
|
+
const text = await res.text().catch(() => '');
|
|
441
|
+
let msg;
|
|
442
|
+
try { msg = JSON.parse(text).error; } catch (_) { msg = text || res.statusText; }
|
|
443
|
+
throw new Error(`WABAgentMesh HTTP ${res.status}: ${msg}`);
|
|
444
|
+
}
|
|
445
|
+
return res.json();
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
module.exports = { WABAgentMesh };
|
package/sdk/commander.js
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WABCommander — SDK Client for the Commander Agent System
|
|
3
|
+
*
|
|
4
|
+
* Provides a high-level API for launching missions, managing edge nodes,
|
|
5
|
+
* running local AI inference, and orchestrating distributed tasks.
|
|
6
|
+
* Works in both browser and Node.js environments.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
class WABCommander {
|
|
10
|
+
/**
|
|
11
|
+
* @param {object} options
|
|
12
|
+
* @param {string} options.serverUrl - WAB server URL
|
|
13
|
+
* @param {string} [options.siteId='default'] - Site identifier
|
|
14
|
+
*/
|
|
15
|
+
constructor(options = {}) {
|
|
16
|
+
this.serverUrl = (options.serverUrl || '').replace(/\/$/, '');
|
|
17
|
+
this.siteId = options.siteId || 'default';
|
|
18
|
+
this._base = `${this.serverUrl}/api/commander`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
22
|
+
// MISSIONS
|
|
23
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
24
|
+
|
|
25
|
+
/** Launch a mission (create + execute in one call). */
|
|
26
|
+
async launchMission(goal, options = {}) {
|
|
27
|
+
return this._post('/missions/launch', {
|
|
28
|
+
siteId: this.siteId, goal,
|
|
29
|
+
title: options.title, strategy: options.strategy,
|
|
30
|
+
priority: options.priority, context: options.context,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Create a mission plan without executing it. */
|
|
35
|
+
async createMission(goal, options = {}) {
|
|
36
|
+
return this._post('/missions', {
|
|
37
|
+
siteId: this.siteId, goal,
|
|
38
|
+
title: options.title, strategy: options.strategy,
|
|
39
|
+
priority: options.priority, context: options.context,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Execute an existing mission by ID. */
|
|
44
|
+
async executeMission(missionId) {
|
|
45
|
+
return this._post(`/missions/${missionId}/execute`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Get a mission by ID. */
|
|
49
|
+
async getMission(missionId) {
|
|
50
|
+
return this._get(`/missions/${missionId}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Get mission tasks. */
|
|
54
|
+
async getMissionTasks(missionId) {
|
|
55
|
+
return this._get(`/missions/${missionId}/tasks`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** List missions. */
|
|
59
|
+
async listMissions(options = {}) {
|
|
60
|
+
const params = new URLSearchParams({ siteId: this.siteId });
|
|
61
|
+
if (options.active) params.set('active', 'true');
|
|
62
|
+
if (options.limit) params.set('limit', String(options.limit));
|
|
63
|
+
return this._get(`/missions?${params}`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Get available decomposition strategies. */
|
|
67
|
+
async getStrategies() {
|
|
68
|
+
return this._get('/strategies');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
72
|
+
// AGENTS
|
|
73
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
74
|
+
|
|
75
|
+
/** Register a commander agent. */
|
|
76
|
+
async registerAgent(agentType, options = {}) {
|
|
77
|
+
return this._post('/agents', {
|
|
78
|
+
siteId: this.siteId, agentType,
|
|
79
|
+
displayName: options.displayName,
|
|
80
|
+
capabilities: options.capabilities,
|
|
81
|
+
modelInfo: options.modelInfo,
|
|
82
|
+
hardware: options.hardware,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** List commander agents. */
|
|
87
|
+
async listAgents(capability) {
|
|
88
|
+
const params = new URLSearchParams({ siteId: this.siteId });
|
|
89
|
+
if (capability) params.set('capability', capability);
|
|
90
|
+
return this._get(`/agents?${params}`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Send agent heartbeat. */
|
|
94
|
+
async agentHeartbeat(agentId) {
|
|
95
|
+
return this._post(`/agents/${agentId}/heartbeat`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
99
|
+
// EDGE COMPUTING
|
|
100
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
101
|
+
|
|
102
|
+
/** Register this device as an edge node. */
|
|
103
|
+
async registerEdgeNode(hostname, hardware = {}, capabilities = []) {
|
|
104
|
+
return this._post('/edge/nodes', {
|
|
105
|
+
siteId: this.siteId, hostname, hardware, capabilities,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** List edge nodes. */
|
|
110
|
+
async listEdgeNodes(availableOnly = false) {
|
|
111
|
+
const params = new URLSearchParams({ siteId: this.siteId });
|
|
112
|
+
if (availableOnly) params.set('available', 'true');
|
|
113
|
+
return this._get(`/edge/nodes?${params}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Send node heartbeat. */
|
|
117
|
+
async nodeHeartbeat(nodeId, currentLoad = 0) {
|
|
118
|
+
return this._post(`/edge/nodes/${nodeId}/heartbeat`, { currentLoad });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Remove an edge node. */
|
|
122
|
+
async removeEdgeNode(nodeId) {
|
|
123
|
+
return this._delete(`/edge/nodes/${nodeId}`);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Submit a task to the edge computing queue. */
|
|
127
|
+
async submitEdgeTask(taskType, payload, options = {}) {
|
|
128
|
+
return this._post('/edge/tasks', {
|
|
129
|
+
taskType, payload,
|
|
130
|
+
priority: options.priority,
|
|
131
|
+
missionId: options.missionId,
|
|
132
|
+
encrypt: options.encrypt,
|
|
133
|
+
encryptionKey: options.encryptionKey,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Distribute queued tasks to available nodes. */
|
|
138
|
+
async distributeEdgeTasks() {
|
|
139
|
+
return this._post('/edge/distribute', { siteId: this.siteId });
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Complete a task (called by executing node). */
|
|
143
|
+
async completeEdgeTask(taskId, result, success = true) {
|
|
144
|
+
return this._post(`/edge/tasks/${taskId}/complete`, { result, success });
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Get tasks for a specific node. */
|
|
148
|
+
async getNodeTasks(nodeId) {
|
|
149
|
+
return this._get(`/edge/tasks?nodeId=${nodeId}`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** List edge swarms. */
|
|
153
|
+
async listSwarms(capability) {
|
|
154
|
+
const params = new URLSearchParams({ siteId: this.siteId });
|
|
155
|
+
if (capability) params.set('capability', capability);
|
|
156
|
+
return this._get(`/edge/swarms?${params}`);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Get edge computing stats. */
|
|
160
|
+
async getEdgeStats() {
|
|
161
|
+
return this._get(`/edge/stats?siteId=${this.siteId}`);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
165
|
+
// LOCAL AI
|
|
166
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
167
|
+
|
|
168
|
+
/** Discover local AI models (Ollama, llama.cpp, etc.). */
|
|
169
|
+
async discoverModels(customEndpoints = []) {
|
|
170
|
+
return this._post('/local-ai/discover', {
|
|
171
|
+
siteId: this.siteId, customEndpoints,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Register a local model manually. */
|
|
176
|
+
async registerModel(provider, modelName, endpoint, capabilities, contextWindow) {
|
|
177
|
+
return this._post('/local-ai/models', {
|
|
178
|
+
siteId: this.siteId, provider, modelName, endpoint, capabilities, contextWindow,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Run local AI inference. */
|
|
183
|
+
async infer(prompt, options = {}) {
|
|
184
|
+
return this._post('/local-ai/infer', {
|
|
185
|
+
siteId: this.siteId, prompt,
|
|
186
|
+
capability: options.capability,
|
|
187
|
+
modelId: options.modelId,
|
|
188
|
+
systemPrompt: options.systemPrompt,
|
|
189
|
+
temperature: options.temperature,
|
|
190
|
+
maxTokens: options.maxTokens,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** List local models. */
|
|
195
|
+
async listModels(availableOnly = false) {
|
|
196
|
+
const params = new URLSearchParams({ siteId: this.siteId });
|
|
197
|
+
if (availableOnly) params.set('available', 'true');
|
|
198
|
+
return this._get(`/local-ai/models?${params}`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Update model status. */
|
|
202
|
+
async updateModelStatus(modelId, status) {
|
|
203
|
+
return this._patch(`/local-ai/models/${modelId}`, { status });
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Get local AI stats. */
|
|
207
|
+
async getLocalAIStats() {
|
|
208
|
+
return this._get(`/local-ai/stats?siteId=${this.siteId}`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
212
|
+
// UNIFIED STATS
|
|
213
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
214
|
+
|
|
215
|
+
/** Get unified stats across commander, edge, and local AI. */
|
|
216
|
+
async getStats() {
|
|
217
|
+
return this._get(`/stats?siteId=${this.siteId}`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
221
|
+
// HTTP helpers
|
|
222
|
+
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
223
|
+
|
|
224
|
+
async _get(path) {
|
|
225
|
+
const res = await fetch(`${this._base}${path}`);
|
|
226
|
+
if (!res.ok) throw new Error(`GET ${path} failed: ${res.status}`);
|
|
227
|
+
return res.json();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async _post(path, body = {}) {
|
|
231
|
+
const res = await fetch(`${this._base}${path}`, {
|
|
232
|
+
method: 'POST',
|
|
233
|
+
headers: { 'Content-Type': 'application/json' },
|
|
234
|
+
body: JSON.stringify(body),
|
|
235
|
+
});
|
|
236
|
+
if (!res.ok) throw new Error(`POST ${path} failed: ${res.status}`);
|
|
237
|
+
return res.json();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
async _patch(path, body = {}) {
|
|
241
|
+
const res = await fetch(`${this._base}${path}`, {
|
|
242
|
+
method: 'PATCH',
|
|
243
|
+
headers: { 'Content-Type': 'application/json' },
|
|
244
|
+
body: JSON.stringify(body),
|
|
245
|
+
});
|
|
246
|
+
if (!res.ok) throw new Error(`PATCH ${path} failed: ${res.status}`);
|
|
247
|
+
return res.json();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async _delete(path) {
|
|
251
|
+
const res = await fetch(`${this._base}${path}`, { method: 'DELETE' });
|
|
252
|
+
if (!res.ok) throw new Error(`DELETE ${path} failed: ${res.status}`);
|
|
253
|
+
return res.json();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Universal export
|
|
258
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
259
|
+
module.exports = WABCommander;
|
|
260
|
+
} else if (typeof window !== 'undefined') {
|
|
261
|
+
window.WABCommander = WABCommander;
|
|
262
|
+
}
|