@johnhenry/browsermesh-apps 0.0.0
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 +21 -0
- package/README.md +63 -0
- package/package.json +36 -0
- package/src/apps.mjs +1351 -0
- package/src/audit.mjs +622 -0
- package/src/chat.mjs +595 -0
- package/src/compat.mjs +30 -0
- package/src/consensus.mjs +784 -0
- package/src/devtools.mjs +255 -0
- package/src/gpu.mjs +732 -0
- package/src/index.mjs +44 -0
- package/src/marketplace-ui.mjs +245 -0
- package/src/marketplace.mjs +754 -0
- package/src/orchestrator.mjs +1575 -0
- package/src/payments.mjs +909 -0
- package/src/peer-agent-swarm.mjs +569 -0
- package/src/peer-agent.mjs +547 -0
- package/src/peer-chat.mjs +381 -0
- package/src/peer-compute.mjs +687 -0
- package/src/peer-encrypted-store.mjs +514 -0
- package/src/peer-escrow.mjs +573 -0
- package/src/peer-files.mjs +470 -0
- package/src/peer-health.mjs +727 -0
- package/src/peer-ipfs.mjs +374 -0
- package/src/peer-node.mjs +793 -0
- package/src/peer-payments.mjs +613 -0
- package/src/peer-registry.mjs +595 -0
- package/src/peer-routing.mjs +555 -0
- package/src/peer-services.mjs +478 -0
- package/src/peer-session.mjs +917 -0
- package/src/peer-terminal.mjs +500 -0
- package/src/peer-timestamp.mjs +403 -0
- package/src/peer-torrent.mjs +628 -0
- package/src/peer-verification.mjs +522 -0
- package/src/quotas.mjs +621 -0
- package/src/resources.mjs +679 -0
- package/src/scheduler.mjs +846 -0
- package/src/silent-catch.mjs +55 -0
- package/src/tools.mjs +668 -0
- package/src/visualizations.mjs +902 -0
package/src/tools.mjs
ADDED
|
@@ -0,0 +1,668 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clawser Mesh Tools
|
|
3
|
+
*
|
|
4
|
+
* BrowserTool subclasses exposing mesh stream and file transfer operations
|
|
5
|
+
* to the AI agent. Follows the pattern in clawser-wsh-tools.js.
|
|
6
|
+
*
|
|
7
|
+
* @module clawser-mesh-tools
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { BrowserTool } from './compat.mjs';
|
|
11
|
+
|
|
12
|
+
// ── Shared state ─────────────────────────────────────────────────────
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Holds references to the StreamMultiplexer and MeshFileTransfer instances
|
|
16
|
+
* so tools can access them without import cycles.
|
|
17
|
+
*/
|
|
18
|
+
export class MeshToolsContext {
|
|
19
|
+
#multiplexer = null;
|
|
20
|
+
#fileTransfer = null;
|
|
21
|
+
#dhtNode = null;
|
|
22
|
+
#trainingOrchestrator = null;
|
|
23
|
+
#iotBridge = null;
|
|
24
|
+
#iotTelemetry = null;
|
|
25
|
+
|
|
26
|
+
setMultiplexer(mux) { this.#multiplexer = mux; }
|
|
27
|
+
getMultiplexer() { return this.#multiplexer; }
|
|
28
|
+
|
|
29
|
+
setFileTransfer(ft) { this.#fileTransfer = ft; }
|
|
30
|
+
getFileTransfer() { return this.#fileTransfer; }
|
|
31
|
+
|
|
32
|
+
setDhtNode(node) { this.#dhtNode = node; }
|
|
33
|
+
getDhtNode() { return this.#dhtNode; }
|
|
34
|
+
|
|
35
|
+
setTrainingOrchestrator(orch) { this.#trainingOrchestrator = orch; }
|
|
36
|
+
getTrainingOrchestrator() { return this.#trainingOrchestrator; }
|
|
37
|
+
|
|
38
|
+
setIoTBridge(bridge) { this.#iotBridge = bridge; }
|
|
39
|
+
getIoTBridge() { return this.#iotBridge; }
|
|
40
|
+
|
|
41
|
+
setIoTTelemetry(telemetry) { this.#iotTelemetry = telemetry; }
|
|
42
|
+
getIoTTelemetry() { return this.#iotTelemetry; }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Singleton context for mesh tools. */
|
|
46
|
+
export const meshToolsContext = new MeshToolsContext();
|
|
47
|
+
|
|
48
|
+
// ── mesh_stream_open ─────────────────────────────────────────────────
|
|
49
|
+
|
|
50
|
+
export class MeshStreamOpenTool extends BrowserTool {
|
|
51
|
+
get name() { return 'mesh_stream_open'; }
|
|
52
|
+
get description() {
|
|
53
|
+
return 'Open a multiplexed data stream to a peer. Returns the stream ID for subsequent read/write operations.';
|
|
54
|
+
}
|
|
55
|
+
get parameters() {
|
|
56
|
+
return {
|
|
57
|
+
type: 'object',
|
|
58
|
+
properties: {
|
|
59
|
+
peerId: { type: 'string', description: 'Target peer identity' },
|
|
60
|
+
method: { type: 'string', description: 'Stream purpose (e.g., "chat", "storage/upload")' },
|
|
61
|
+
ordered: { type: 'boolean', description: 'Whether delivery must be ordered (default: true)' },
|
|
62
|
+
encrypted: { type: 'boolean', description: 'Whether to use per-stream encryption (default: false)' },
|
|
63
|
+
},
|
|
64
|
+
required: ['peerId', 'method'],
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
get permission() { return 'network'; }
|
|
68
|
+
|
|
69
|
+
async execute({ peerId, method, ordered, encrypted }) {
|
|
70
|
+
try {
|
|
71
|
+
const mux = meshToolsContext.getMultiplexer();
|
|
72
|
+
if (!mux) {
|
|
73
|
+
return { success: false, output: '', error: 'Stream multiplexer not initialized. Start a mesh session first.' };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const stream = mux.open(method, { ordered, encrypted });
|
|
77
|
+
return {
|
|
78
|
+
success: true,
|
|
79
|
+
output: `Stream opened: ${stream.hexId} (method: ${method}, peer: ${peerId}, ordered: ${stream.ordered}, encrypted: ${stream.encrypted})`,
|
|
80
|
+
};
|
|
81
|
+
} catch (err) {
|
|
82
|
+
return { success: false, output: '', error: `Stream open failed: ${err.message}` };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ── mesh_stream_close ────────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
export class MeshStreamCloseTool extends BrowserTool {
|
|
90
|
+
get name() { return 'mesh_stream_close'; }
|
|
91
|
+
get description() {
|
|
92
|
+
return 'Close an open data stream by ID.';
|
|
93
|
+
}
|
|
94
|
+
get parameters() {
|
|
95
|
+
return {
|
|
96
|
+
type: 'object',
|
|
97
|
+
properties: {
|
|
98
|
+
streamId: { type: 'string', description: 'Hex stream ID to close' },
|
|
99
|
+
},
|
|
100
|
+
required: ['streamId'],
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
get permission() { return 'network'; }
|
|
104
|
+
|
|
105
|
+
async execute({ streamId }) {
|
|
106
|
+
try {
|
|
107
|
+
const mux = meshToolsContext.getMultiplexer();
|
|
108
|
+
if (!mux) {
|
|
109
|
+
return { success: false, output: '', error: 'Stream multiplexer not initialized.' };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const stream = mux.getStream(streamId);
|
|
113
|
+
if (!stream) {
|
|
114
|
+
return { success: false, output: '', error: `Stream ${streamId} not found.` };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
mux.close(streamId);
|
|
118
|
+
return { success: true, output: `Stream ${streamId} closed.` };
|
|
119
|
+
} catch (err) {
|
|
120
|
+
return { success: false, output: '', error: `Stream close failed: ${err.message}` };
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ── mesh_stream_list ─────────────────────────────────────────────────
|
|
126
|
+
|
|
127
|
+
export class MeshStreamListTool extends BrowserTool {
|
|
128
|
+
get name() { return 'mesh_stream_list'; }
|
|
129
|
+
get description() {
|
|
130
|
+
return 'List active mesh streams, optionally filtered by peer.';
|
|
131
|
+
}
|
|
132
|
+
get parameters() {
|
|
133
|
+
return {
|
|
134
|
+
type: 'object',
|
|
135
|
+
properties: {
|
|
136
|
+
peerId: { type: 'string', description: 'Filter by peer identity (optional)' },
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
get permission() { return 'read'; }
|
|
141
|
+
|
|
142
|
+
async execute({ peerId } = {}) {
|
|
143
|
+
try {
|
|
144
|
+
const mux = meshToolsContext.getMultiplexer();
|
|
145
|
+
if (!mux) {
|
|
146
|
+
return { success: true, output: 'No active streams (multiplexer not initialized).' };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const streams = mux.listStreams();
|
|
150
|
+
if (streams.length === 0) {
|
|
151
|
+
return { success: true, output: 'No active streams.' };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const lines = streams.map(s => {
|
|
155
|
+
const json = s.toJSON();
|
|
156
|
+
return `${json.id} | ${json.method} | ${json.state} | sent:${json.bytesSent} recv:${json.bytesReceived}`;
|
|
157
|
+
});
|
|
158
|
+
return {
|
|
159
|
+
success: true,
|
|
160
|
+
output: `ID | METHOD | STATE | BYTES\n${lines.join('\n')}`,
|
|
161
|
+
};
|
|
162
|
+
} catch (err) {
|
|
163
|
+
return { success: false, output: '', error: err.message };
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ── mesh_file_send ───────────────────────────────────────────────────
|
|
169
|
+
|
|
170
|
+
export class MeshFileSendTool extends BrowserTool {
|
|
171
|
+
get name() { return 'mesh_file_send'; }
|
|
172
|
+
get description() {
|
|
173
|
+
return 'Send files to a peer. Creates a transfer offer with file metadata. The peer must accept before data is sent.';
|
|
174
|
+
}
|
|
175
|
+
get parameters() {
|
|
176
|
+
return {
|
|
177
|
+
type: 'object',
|
|
178
|
+
properties: {
|
|
179
|
+
peerId: { type: 'string', description: 'Recipient peer identity' },
|
|
180
|
+
files: {
|
|
181
|
+
type: 'array',
|
|
182
|
+
description: 'Files to send: [{ name, size, mimeType? }]',
|
|
183
|
+
items: {
|
|
184
|
+
type: 'object',
|
|
185
|
+
properties: {
|
|
186
|
+
name: { type: 'string' },
|
|
187
|
+
size: { type: 'number' },
|
|
188
|
+
mimeType: { type: 'string' },
|
|
189
|
+
},
|
|
190
|
+
required: ['name', 'size'],
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
required: ['peerId', 'files'],
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
get permission() { return 'approve'; }
|
|
198
|
+
|
|
199
|
+
async execute({ peerId, files }) {
|
|
200
|
+
try {
|
|
201
|
+
const ft = meshToolsContext.getFileTransfer();
|
|
202
|
+
if (!ft) {
|
|
203
|
+
return { success: false, output: '', error: 'File transfer not initialized. Start a mesh session first.' };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const offer = ft.createOffer(peerId, files);
|
|
207
|
+
const fileList = offer.files.map(f => ` ${f.name} (${f.size} bytes)`).join('\n');
|
|
208
|
+
return {
|
|
209
|
+
success: true,
|
|
210
|
+
output: `Transfer offer created: ${offer.transferId}\nRecipient: ${peerId}\nFiles:\n${fileList}\nTotal: ${offer.totalSize} bytes\nWaiting for acceptance...`,
|
|
211
|
+
};
|
|
212
|
+
} catch (err) {
|
|
213
|
+
return { success: false, output: '', error: `File send failed: ${err.message}` };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// ── mesh_file_accept ─────────────────────────────────────────────────
|
|
219
|
+
|
|
220
|
+
export class MeshFileAcceptTool extends BrowserTool {
|
|
221
|
+
get name() { return 'mesh_file_accept'; }
|
|
222
|
+
get description() {
|
|
223
|
+
return 'Accept an incoming file transfer offer.';
|
|
224
|
+
}
|
|
225
|
+
get parameters() {
|
|
226
|
+
return {
|
|
227
|
+
type: 'object',
|
|
228
|
+
properties: {
|
|
229
|
+
transferId: { type: 'string', description: 'Transfer ID from the incoming offer' },
|
|
230
|
+
},
|
|
231
|
+
required: ['transferId'],
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
get permission() { return 'approve'; }
|
|
235
|
+
|
|
236
|
+
async execute({ transferId }) {
|
|
237
|
+
try {
|
|
238
|
+
const ft = meshToolsContext.getFileTransfer();
|
|
239
|
+
if (!ft) {
|
|
240
|
+
return { success: false, output: '', error: 'File transfer not initialized.' };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const offer = ft.getOffer(transferId);
|
|
244
|
+
if (!offer) {
|
|
245
|
+
return { success: false, output: '', error: `Transfer offer ${transferId} not found.` };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const state = ft.acceptOffer(offer);
|
|
249
|
+
return {
|
|
250
|
+
success: true,
|
|
251
|
+
output: `Transfer ${transferId} accepted. Status: ${state.status}`,
|
|
252
|
+
};
|
|
253
|
+
} catch (err) {
|
|
254
|
+
return { success: false, output: '', error: `Accept failed: ${err.message}` };
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// ── mesh_file_list ───────────────────────────────────────────────────
|
|
260
|
+
|
|
261
|
+
export class MeshFileListTool extends BrowserTool {
|
|
262
|
+
get name() { return 'mesh_file_list'; }
|
|
263
|
+
get description() {
|
|
264
|
+
return 'List file transfers, optionally filtered by status or peer.';
|
|
265
|
+
}
|
|
266
|
+
get parameters() {
|
|
267
|
+
return {
|
|
268
|
+
type: 'object',
|
|
269
|
+
properties: {
|
|
270
|
+
status: { type: 'string', description: 'Filter by status: offered, accepted, transferring, completed, failed, cancelled' },
|
|
271
|
+
peerId: { type: 'string', description: 'Filter by peer identity (optional)' },
|
|
272
|
+
},
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
get permission() { return 'read'; }
|
|
276
|
+
|
|
277
|
+
async execute({ status, peerId } = {}) {
|
|
278
|
+
try {
|
|
279
|
+
const ft = meshToolsContext.getFileTransfer();
|
|
280
|
+
if (!ft) {
|
|
281
|
+
return { success: true, output: 'No transfers (file transfer not initialized).' };
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const transfers = ft.listTransfers({ status, peerId });
|
|
285
|
+
if (transfers.length === 0) {
|
|
286
|
+
return { success: true, output: 'No transfers found.' };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const lines = transfers.map(t => {
|
|
290
|
+
const s = t.state;
|
|
291
|
+
const fileNames = t.offer?.files?.map(f => f.name).join(', ') || '?';
|
|
292
|
+
return `${s.transferId} | ${s.status} | ${fileNames} | ${s.bytesTransferred}/${s.totalSize} (${Math.round(s.percentComplete)}%)`;
|
|
293
|
+
});
|
|
294
|
+
return {
|
|
295
|
+
success: true,
|
|
296
|
+
output: `ID | STATUS | FILES | PROGRESS\n${lines.join('\n')}`,
|
|
297
|
+
};
|
|
298
|
+
} catch (err) {
|
|
299
|
+
return { success: false, output: '', error: err.message };
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// ── mesh_file_cancel ─────────────────────────────────────────────────
|
|
305
|
+
|
|
306
|
+
export class MeshFileCancelTool extends BrowserTool {
|
|
307
|
+
get name() { return 'mesh_file_cancel'; }
|
|
308
|
+
get description() {
|
|
309
|
+
return 'Cancel an in-progress file transfer.';
|
|
310
|
+
}
|
|
311
|
+
get parameters() {
|
|
312
|
+
return {
|
|
313
|
+
type: 'object',
|
|
314
|
+
properties: {
|
|
315
|
+
transferId: { type: 'string', description: 'Transfer ID to cancel' },
|
|
316
|
+
reason: { type: 'string', description: 'Cancellation reason (optional)' },
|
|
317
|
+
},
|
|
318
|
+
required: ['transferId'],
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
get permission() { return 'write'; }
|
|
322
|
+
|
|
323
|
+
async execute({ transferId, reason }) {
|
|
324
|
+
try {
|
|
325
|
+
const ft = meshToolsContext.getFileTransfer();
|
|
326
|
+
if (!ft) {
|
|
327
|
+
return { success: false, output: '', error: 'File transfer not initialized.' };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
ft.cancelTransfer(transferId, reason);
|
|
331
|
+
return {
|
|
332
|
+
success: true,
|
|
333
|
+
output: `Transfer ${transferId} cancelled${reason ? ': ' + reason : ''}.`,
|
|
334
|
+
};
|
|
335
|
+
} catch (err) {
|
|
336
|
+
return { success: false, output: '', error: `Cancel failed: ${err.message}` };
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// ── dht_store ────────────────────────────────────────────────────────
|
|
342
|
+
|
|
343
|
+
export class DhtStoreTool extends BrowserTool {
|
|
344
|
+
get name() { return 'dht_store'; }
|
|
345
|
+
get description() {
|
|
346
|
+
return 'Store a key-value pair in the distributed hash table (DHT).';
|
|
347
|
+
}
|
|
348
|
+
get parameters() {
|
|
349
|
+
return {
|
|
350
|
+
type: 'object',
|
|
351
|
+
properties: {
|
|
352
|
+
key: { type: 'string', description: 'Key to store' },
|
|
353
|
+
value: { type: 'string', description: 'Value to store' },
|
|
354
|
+
ttl: { type: 'number', description: 'Time-to-live in milliseconds (optional)' },
|
|
355
|
+
},
|
|
356
|
+
required: ['key', 'value'],
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
get permission() { return 'network'; }
|
|
360
|
+
|
|
361
|
+
async execute({ key, value, ttl }) {
|
|
362
|
+
try {
|
|
363
|
+
const dht = meshToolsContext.getDhtNode();
|
|
364
|
+
if (!dht) {
|
|
365
|
+
return { success: false, output: '', error: 'DHT node not initialized. Start a mesh session first.' };
|
|
366
|
+
}
|
|
367
|
+
dht.store(key, value, ttl);
|
|
368
|
+
return { success: true, output: `Stored key "${key}" in DHT${ttl ? ` (TTL: ${ttl}ms)` : ''}.` };
|
|
369
|
+
} catch (err) {
|
|
370
|
+
return { success: false, output: '', error: `DHT store failed: ${err.message}` };
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// ── dht_lookup ───────────────────────────────────────────────────────
|
|
376
|
+
|
|
377
|
+
export class DhtLookupTool extends BrowserTool {
|
|
378
|
+
get name() { return 'dht_lookup'; }
|
|
379
|
+
get description() {
|
|
380
|
+
return 'Look up a value by key in the distributed hash table (DHT).';
|
|
381
|
+
}
|
|
382
|
+
get parameters() {
|
|
383
|
+
return {
|
|
384
|
+
type: 'object',
|
|
385
|
+
properties: {
|
|
386
|
+
key: { type: 'string', description: 'Key to look up' },
|
|
387
|
+
},
|
|
388
|
+
required: ['key'],
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
get permission() { return 'read'; }
|
|
392
|
+
|
|
393
|
+
async execute({ key }) {
|
|
394
|
+
try {
|
|
395
|
+
const dht = meshToolsContext.getDhtNode();
|
|
396
|
+
if (!dht) {
|
|
397
|
+
return { success: false, output: '', error: 'DHT node not initialized. Start a mesh session first.' };
|
|
398
|
+
}
|
|
399
|
+
const result = dht.findValue(key);
|
|
400
|
+
if (result.found) {
|
|
401
|
+
return { success: true, output: `Key "${key}" = ${JSON.stringify(result.value)}` };
|
|
402
|
+
}
|
|
403
|
+
return { success: true, output: `Key "${key}" not found. Closest nodes: ${result.closest.map(c => c.podId).join(', ')}` };
|
|
404
|
+
} catch (err) {
|
|
405
|
+
return { success: false, output: '', error: `DHT lookup failed: ${err.message}` };
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// ── dht_peers ────────────────────────────────────────────────────────
|
|
411
|
+
|
|
412
|
+
export class DhtPeersTool extends BrowserTool {
|
|
413
|
+
get name() { return 'dht_peers'; }
|
|
414
|
+
get description() {
|
|
415
|
+
return 'List peers in the DHT routing table.';
|
|
416
|
+
}
|
|
417
|
+
get parameters() {
|
|
418
|
+
return {
|
|
419
|
+
type: 'object',
|
|
420
|
+
properties: {
|
|
421
|
+
count: { type: 'number', description: 'Max number of peers to return (default: 20)' },
|
|
422
|
+
},
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
get permission() { return 'read'; }
|
|
426
|
+
|
|
427
|
+
async execute({ count = 20 } = {}) {
|
|
428
|
+
try {
|
|
429
|
+
const dht = meshToolsContext.getDhtNode();
|
|
430
|
+
if (!dht) {
|
|
431
|
+
return { success: true, output: 'DHT node not initialized.' };
|
|
432
|
+
}
|
|
433
|
+
const peers = dht.findNode(dht.localId || 'self').slice(0, count);
|
|
434
|
+
if (peers.length === 0) {
|
|
435
|
+
return { success: true, output: 'No peers in routing table.' };
|
|
436
|
+
}
|
|
437
|
+
const lines = peers.map(p => `${p.podId}${p.address ? ' @ ' + p.address : ''}`);
|
|
438
|
+
return { success: true, output: `DHT peers (${peers.length}):\n${lines.join('\n')}` };
|
|
439
|
+
} catch (err) {
|
|
440
|
+
return { success: false, output: '', error: `DHT peers failed: ${err.message}` };
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
// ── gpu_train_start ──────────────────────────────────────────────────
|
|
446
|
+
|
|
447
|
+
export class GpuTrainStartTool extends BrowserTool {
|
|
448
|
+
get name() { return 'gpu_train_start'; }
|
|
449
|
+
get description() {
|
|
450
|
+
return 'Start a distributed GPU training job across mesh peers.';
|
|
451
|
+
}
|
|
452
|
+
get parameters() {
|
|
453
|
+
return {
|
|
454
|
+
type: 'object',
|
|
455
|
+
properties: {
|
|
456
|
+
jobId: { type: 'string', description: 'Unique job identifier' },
|
|
457
|
+
modelConfig: { type: 'object', description: 'Model architecture configuration' },
|
|
458
|
+
datasetRef: { type: 'string', description: 'Reference to training dataset' },
|
|
459
|
+
epochs: { type: 'number', description: 'Number of training epochs (default: 1)' },
|
|
460
|
+
batchSize: { type: 'number', description: 'Batch size (default: 32)' },
|
|
461
|
+
learningRate: { type: 'number', description: 'Learning rate (default: 0.001)' },
|
|
462
|
+
strategy: { type: 'string', description: 'Training strategy: sync_allreduce, async_parameter_server, or federated_avg' },
|
|
463
|
+
shardCount: { type: 'number', description: 'Number of training shards (default: 1)' },
|
|
464
|
+
},
|
|
465
|
+
required: ['jobId', 'modelConfig', 'datasetRef'],
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
get permission() { return 'approve'; }
|
|
469
|
+
|
|
470
|
+
async execute({ jobId, modelConfig, datasetRef, epochs, batchSize, learningRate, strategy, shardCount }) {
|
|
471
|
+
try {
|
|
472
|
+
const orch = meshToolsContext.getTrainingOrchestrator();
|
|
473
|
+
if (!orch) {
|
|
474
|
+
return { success: false, output: '', error: 'Training orchestrator not initialized. Start a mesh session first.' };
|
|
475
|
+
}
|
|
476
|
+
const { TrainingSpec } = await import('./gpu.mjs');
|
|
477
|
+
const spec = new TrainingSpec({ jobId, modelConfig, datasetRef, epochs, batchSize, learningRate, strategy, shardCount });
|
|
478
|
+
const id = orch.startJob(spec);
|
|
479
|
+
return { success: true, output: `Training job started: ${id}` };
|
|
480
|
+
} catch (err) {
|
|
481
|
+
return { success: false, output: '', error: `Training start failed: ${err.message}` };
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// ── gpu_train_status ─────────────────────────────────────────────────
|
|
487
|
+
|
|
488
|
+
export class GpuTrainStatusTool extends BrowserTool {
|
|
489
|
+
get name() { return 'gpu_train_status'; }
|
|
490
|
+
get description() {
|
|
491
|
+
return 'Check the status of a distributed GPU training job.';
|
|
492
|
+
}
|
|
493
|
+
get parameters() {
|
|
494
|
+
return {
|
|
495
|
+
type: 'object',
|
|
496
|
+
properties: {
|
|
497
|
+
jobId: { type: 'string', description: 'Job identifier to check' },
|
|
498
|
+
},
|
|
499
|
+
required: ['jobId'],
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
get permission() { return 'read'; }
|
|
503
|
+
|
|
504
|
+
async execute({ jobId }) {
|
|
505
|
+
try {
|
|
506
|
+
const orch = meshToolsContext.getTrainingOrchestrator();
|
|
507
|
+
if (!orch) {
|
|
508
|
+
return { success: false, output: '', error: 'Training orchestrator not initialized.' };
|
|
509
|
+
}
|
|
510
|
+
const status = orch.getJobStatus(jobId);
|
|
511
|
+
if (!status) {
|
|
512
|
+
return { success: false, output: '', error: `Job ${jobId} not found.` };
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
success: true,
|
|
516
|
+
output: `Job: ${status.jobId} | Status: ${status.status} | Shards: ${status.completedShards}/${status.shardCount}${status.aggregated ? ' | Aggregated' : ''}`,
|
|
517
|
+
};
|
|
518
|
+
} catch (err) {
|
|
519
|
+
return { success: false, output: '', error: `Status check failed: ${err.message}` };
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// ── iot_list ─────────────────────────────────────────────────────────
|
|
525
|
+
|
|
526
|
+
export class IoTListTool extends BrowserTool {
|
|
527
|
+
get name() { return 'iot_list'; }
|
|
528
|
+
get description() {
|
|
529
|
+
return 'List registered IoT devices, optionally filtered by protocol or capability.';
|
|
530
|
+
}
|
|
531
|
+
get parameters() {
|
|
532
|
+
return {
|
|
533
|
+
type: 'object',
|
|
534
|
+
properties: {
|
|
535
|
+
protocol: { type: 'string', description: 'Filter by protocol: mqtt, http, direct, coap' },
|
|
536
|
+
capability: { type: 'string', description: 'Filter by capability: read, write, stream, command' },
|
|
537
|
+
},
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
get permission() { return 'read'; }
|
|
541
|
+
|
|
542
|
+
async execute({ protocol, capability } = {}) {
|
|
543
|
+
try {
|
|
544
|
+
const bridge = meshToolsContext.getIoTBridge();
|
|
545
|
+
if (!bridge) {
|
|
546
|
+
return { success: true, output: 'IoT bridge not initialized.' };
|
|
547
|
+
}
|
|
548
|
+
const filter = {};
|
|
549
|
+
if (protocol) filter.protocol = protocol;
|
|
550
|
+
if (capability) filter.capability = capability;
|
|
551
|
+
const devices = bridge.listDevices(Object.keys(filter).length ? filter : undefined);
|
|
552
|
+
if (devices.length === 0) {
|
|
553
|
+
return { success: true, output: 'No IoT devices found.' };
|
|
554
|
+
}
|
|
555
|
+
const lines = devices.map(d => `${d.deviceId} | ${d.name || '(unnamed)'} | ${d.protocol} | [${d.capabilities.join(',')}]`);
|
|
556
|
+
return { success: true, output: `ID | NAME | PROTOCOL | CAPABILITIES\n${lines.join('\n')}` };
|
|
557
|
+
} catch (err) {
|
|
558
|
+
return { success: false, output: '', error: err.message };
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
// ── iot_send ─────────────────────────────────────────────────────────
|
|
564
|
+
|
|
565
|
+
export class IoTSendTool extends BrowserTool {
|
|
566
|
+
get name() { return 'iot_send'; }
|
|
567
|
+
get description() {
|
|
568
|
+
return 'Send a command or payload to an IoT device.';
|
|
569
|
+
}
|
|
570
|
+
get parameters() {
|
|
571
|
+
return {
|
|
572
|
+
type: 'object',
|
|
573
|
+
properties: {
|
|
574
|
+
deviceId: { type: 'string', description: 'Target device ID' },
|
|
575
|
+
payload: { type: 'object', description: 'Data payload to send' },
|
|
576
|
+
},
|
|
577
|
+
required: ['deviceId', 'payload'],
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
get permission() { return 'approve'; }
|
|
581
|
+
|
|
582
|
+
async execute({ deviceId, payload }) {
|
|
583
|
+
try {
|
|
584
|
+
const bridge = meshToolsContext.getIoTBridge();
|
|
585
|
+
if (!bridge) {
|
|
586
|
+
return { success: false, output: '', error: 'IoT bridge not initialized.' };
|
|
587
|
+
}
|
|
588
|
+
await bridge.send(deviceId, payload);
|
|
589
|
+
return { success: true, output: `Payload sent to device ${deviceId}.` };
|
|
590
|
+
} catch (err) {
|
|
591
|
+
return { success: false, output: '', error: `IoT send failed: ${err.message}` };
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// ── iot_telemetry ────────────────────────────────────────────────────
|
|
597
|
+
|
|
598
|
+
export class IoTTelemetryTool extends BrowserTool {
|
|
599
|
+
get name() { return 'iot_telemetry'; }
|
|
600
|
+
get description() {
|
|
601
|
+
return 'Query telemetry data from an IoT device.';
|
|
602
|
+
}
|
|
603
|
+
get parameters() {
|
|
604
|
+
return {
|
|
605
|
+
type: 'object',
|
|
606
|
+
properties: {
|
|
607
|
+
deviceId: { type: 'string', description: 'Device ID to query' },
|
|
608
|
+
since: { type: 'number', description: 'Start timestamp (optional)' },
|
|
609
|
+
until: { type: 'number', description: 'End timestamp (optional)' },
|
|
610
|
+
stats: { type: 'boolean', description: 'Return statistics instead of raw data (default: false)' },
|
|
611
|
+
},
|
|
612
|
+
required: ['deviceId'],
|
|
613
|
+
};
|
|
614
|
+
}
|
|
615
|
+
get permission() { return 'read'; }
|
|
616
|
+
|
|
617
|
+
async execute({ deviceId, since, until, stats }) {
|
|
618
|
+
try {
|
|
619
|
+
const telemetry = meshToolsContext.getIoTTelemetry();
|
|
620
|
+
if (!telemetry) {
|
|
621
|
+
return { success: false, output: '', error: 'IoT telemetry not initialized.' };
|
|
622
|
+
}
|
|
623
|
+
if (stats) {
|
|
624
|
+
const s = telemetry.getStats(deviceId);
|
|
625
|
+
if (!s) {
|
|
626
|
+
return { success: true, output: `No telemetry data for device ${deviceId}.` };
|
|
627
|
+
}
|
|
628
|
+
return { success: true, output: `Device ${deviceId} stats: min=${s.min} max=${s.max} avg=${s.avg.toFixed(2)} count=${s.count} last=${s.last}` };
|
|
629
|
+
}
|
|
630
|
+
const samples = telemetry.query(deviceId, since, until);
|
|
631
|
+
if (samples.length === 0) {
|
|
632
|
+
return { success: true, output: `No telemetry data for device ${deviceId}.` };
|
|
633
|
+
}
|
|
634
|
+
return { success: true, output: `${samples.length} samples for ${deviceId}:\n${samples.map(s => `${new Date(s.ts).toISOString()}: ${s.value}`).join('\n')}` };
|
|
635
|
+
} catch (err) {
|
|
636
|
+
return { success: false, output: '', error: `Telemetry query failed: ${err.message}` };
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
// ── Registry helper ──────────────────────────────────────────────────
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Register all mesh stream/file tools with a BrowserToolRegistry.
|
|
645
|
+
* @param {import('./compat.mjs').BrowserToolRegistry} registry
|
|
646
|
+
* @param {import('@johnhenry/browsermesh-transport').StreamMultiplexer} [multiplexer]
|
|
647
|
+
* @param {import('@johnhenry/browsermesh-sync').MeshFileTransfer} [fileTransfer]
|
|
648
|
+
*/
|
|
649
|
+
export function registerMeshTools(registry, multiplexer, fileTransfer) {
|
|
650
|
+
if (multiplexer) meshToolsContext.setMultiplexer(multiplexer);
|
|
651
|
+
if (fileTransfer) meshToolsContext.setFileTransfer(fileTransfer);
|
|
652
|
+
|
|
653
|
+
registry.register(new MeshStreamOpenTool());
|
|
654
|
+
registry.register(new MeshStreamCloseTool());
|
|
655
|
+
registry.register(new MeshStreamListTool());
|
|
656
|
+
registry.register(new MeshFileSendTool());
|
|
657
|
+
registry.register(new MeshFileAcceptTool());
|
|
658
|
+
registry.register(new MeshFileListTool());
|
|
659
|
+
registry.register(new MeshFileCancelTool());
|
|
660
|
+
registry.register(new DhtStoreTool());
|
|
661
|
+
registry.register(new DhtLookupTool());
|
|
662
|
+
registry.register(new DhtPeersTool());
|
|
663
|
+
registry.register(new GpuTrainStartTool());
|
|
664
|
+
registry.register(new GpuTrainStatusTool());
|
|
665
|
+
registry.register(new IoTListTool());
|
|
666
|
+
registry.register(new IoTSendTool());
|
|
667
|
+
registry.register(new IoTTelemetryTool());
|
|
668
|
+
}
|