agentxjs 0.1.9 → 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/dist/browser.cjs +495 -31
- package/dist/browser.cjs.map +1 -1
- package/dist/browser.js +495 -31
- package/dist/browser.js.map +1 -1
- package/dist/index.cjs +512 -121
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +512 -121
- package/dist/index.js.map +1 -1
- package/package.json +8 -6
package/dist/index.js
CHANGED
|
@@ -194,48 +194,476 @@ var init_dist = __esm({
|
|
|
194
194
|
}
|
|
195
195
|
});
|
|
196
196
|
|
|
197
|
+
// ../network/dist/chunk-63P5VUHB.js
|
|
198
|
+
async function createWebSocketClient(options) {
|
|
199
|
+
if (isBrowser) {
|
|
200
|
+
const client = new BrowserWebSocketClient(options);
|
|
201
|
+
await client.connect();
|
|
202
|
+
return client;
|
|
203
|
+
} else {
|
|
204
|
+
const client = new WebSocketClient(options);
|
|
205
|
+
await client.connect();
|
|
206
|
+
return client;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
var __defProp3, __defNormalProp2, __publicField2, logger, isBrowser, WebSocketClient, BrowserWebSocketClient;
|
|
210
|
+
var init_chunk_63P5VUHB = __esm({
|
|
211
|
+
"../network/dist/chunk-63P5VUHB.js"() {
|
|
212
|
+
init_dist();
|
|
213
|
+
__defProp3 = Object.defineProperty;
|
|
214
|
+
__defNormalProp2 = (obj, key, value) => key in obj ? __defProp3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
215
|
+
__publicField2 = (obj, key, value) => __defNormalProp2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
216
|
+
logger = createLogger("network/WebSocketClient");
|
|
217
|
+
isBrowser = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined" && typeof globalThis.window.WebSocket !== "undefined";
|
|
218
|
+
WebSocketClient = class {
|
|
219
|
+
constructor(options) {
|
|
220
|
+
__publicField2(this, "ws", null);
|
|
221
|
+
__publicField2(this, "serverUrl");
|
|
222
|
+
__publicField2(this, "messageHandlers", /* @__PURE__ */ new Set());
|
|
223
|
+
__publicField2(this, "openHandlers", /* @__PURE__ */ new Set());
|
|
224
|
+
__publicField2(this, "closeHandlers", /* @__PURE__ */ new Set());
|
|
225
|
+
__publicField2(this, "errorHandlers", /* @__PURE__ */ new Set());
|
|
226
|
+
if (isBrowser) {
|
|
227
|
+
throw new Error(
|
|
228
|
+
"Use createBrowserWebSocketClient() in browser environment for auto-reconnect support"
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
this.serverUrl = options.serverUrl;
|
|
232
|
+
}
|
|
233
|
+
get readyState() {
|
|
234
|
+
if (!this.ws) return "closed";
|
|
235
|
+
const state = this.ws.readyState;
|
|
236
|
+
if (state === 0) return "connecting";
|
|
237
|
+
if (state === 1) return "open";
|
|
238
|
+
if (state === 2) return "closing";
|
|
239
|
+
return "closed";
|
|
240
|
+
}
|
|
241
|
+
async connect() {
|
|
242
|
+
if (this.ws) {
|
|
243
|
+
throw new Error("Already connected or connecting");
|
|
244
|
+
}
|
|
245
|
+
const { WebSocket: NodeWebSocket } = await import('ws');
|
|
246
|
+
this.ws = new NodeWebSocket(this.serverUrl);
|
|
247
|
+
return new Promise((resolve, reject) => {
|
|
248
|
+
const onOpen = () => {
|
|
249
|
+
logger.info("WebSocket connected", { serverUrl: this.serverUrl });
|
|
250
|
+
for (const handler of this.openHandlers) {
|
|
251
|
+
handler();
|
|
252
|
+
}
|
|
253
|
+
resolve();
|
|
254
|
+
};
|
|
255
|
+
const onError = (err) => {
|
|
256
|
+
logger.error("WebSocket connection failed", {
|
|
257
|
+
serverUrl: this.serverUrl,
|
|
258
|
+
error: err?.message
|
|
259
|
+
});
|
|
260
|
+
reject(err || new Error("WebSocket connection failed"));
|
|
261
|
+
};
|
|
262
|
+
this.ws.once("open", onOpen);
|
|
263
|
+
this.ws.once("error", onError);
|
|
264
|
+
this.ws.on("message", (data) => {
|
|
265
|
+
const message = data.toString();
|
|
266
|
+
for (const handler of this.messageHandlers) {
|
|
267
|
+
handler(message);
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
this.ws.on("close", () => {
|
|
271
|
+
logger.warn("WebSocket closed");
|
|
272
|
+
for (const handler of this.closeHandlers) {
|
|
273
|
+
handler();
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
this.ws.on("error", (err) => {
|
|
277
|
+
logger.error("WebSocket error", { error: err.message });
|
|
278
|
+
for (const handler of this.errorHandlers) {
|
|
279
|
+
handler(err);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
send(message) {
|
|
285
|
+
if (!this.ws || this.ws.readyState !== 1) {
|
|
286
|
+
throw new Error("WebSocket is not open");
|
|
287
|
+
}
|
|
288
|
+
this.ws.send(message);
|
|
289
|
+
}
|
|
290
|
+
onMessage(handler) {
|
|
291
|
+
this.messageHandlers.add(handler);
|
|
292
|
+
return () => {
|
|
293
|
+
this.messageHandlers.delete(handler);
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
onOpen(handler) {
|
|
297
|
+
this.openHandlers.add(handler);
|
|
298
|
+
return () => {
|
|
299
|
+
this.openHandlers.delete(handler);
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
onClose(handler) {
|
|
303
|
+
this.closeHandlers.add(handler);
|
|
304
|
+
return () => {
|
|
305
|
+
this.closeHandlers.delete(handler);
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
onError(handler) {
|
|
309
|
+
this.errorHandlers.add(handler);
|
|
310
|
+
return () => {
|
|
311
|
+
this.errorHandlers.delete(handler);
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
close() {
|
|
315
|
+
if (this.ws) {
|
|
316
|
+
this.ws.close();
|
|
317
|
+
this.ws = null;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
dispose() {
|
|
321
|
+
this.close();
|
|
322
|
+
this.messageHandlers.clear();
|
|
323
|
+
this.openHandlers.clear();
|
|
324
|
+
this.closeHandlers.clear();
|
|
325
|
+
this.errorHandlers.clear();
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
BrowserWebSocketClient = class {
|
|
329
|
+
// Track if this is a reconnection
|
|
330
|
+
constructor(options) {
|
|
331
|
+
__publicField2(this, "ws", null);
|
|
332
|
+
__publicField2(this, "serverUrl");
|
|
333
|
+
__publicField2(this, "options");
|
|
334
|
+
__publicField2(this, "messageHandlers", /* @__PURE__ */ new Set());
|
|
335
|
+
__publicField2(this, "openHandlers", /* @__PURE__ */ new Set());
|
|
336
|
+
__publicField2(this, "closeHandlers", /* @__PURE__ */ new Set());
|
|
337
|
+
__publicField2(this, "errorHandlers", /* @__PURE__ */ new Set());
|
|
338
|
+
__publicField2(this, "hasConnectedBefore", false);
|
|
339
|
+
if (!isBrowser) {
|
|
340
|
+
throw new Error("BrowserWebSocketClient can only be used in browser environment");
|
|
341
|
+
}
|
|
342
|
+
this.serverUrl = options.serverUrl;
|
|
343
|
+
this.options = {
|
|
344
|
+
autoReconnect: true,
|
|
345
|
+
minReconnectionDelay: 1e3,
|
|
346
|
+
maxReconnectionDelay: 1e4,
|
|
347
|
+
maxRetries: Infinity,
|
|
348
|
+
connectionTimeout: 4e3,
|
|
349
|
+
debug: false,
|
|
350
|
+
...options
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
get readyState() {
|
|
354
|
+
if (!this.ws) return "closed";
|
|
355
|
+
const state = this.ws.readyState;
|
|
356
|
+
if (state === 0) return "connecting";
|
|
357
|
+
if (state === 1) return "open";
|
|
358
|
+
if (state === 2) return "closing";
|
|
359
|
+
return "closed";
|
|
360
|
+
}
|
|
361
|
+
async connect() {
|
|
362
|
+
if (this.ws) {
|
|
363
|
+
throw new Error("Already connected or connecting");
|
|
364
|
+
}
|
|
365
|
+
if (this.options.autoReconnect) {
|
|
366
|
+
const ReconnectingWebSocket = (await import('reconnecting-websocket')).default;
|
|
367
|
+
this.ws = new ReconnectingWebSocket(this.serverUrl, [], {
|
|
368
|
+
maxReconnectionDelay: this.options.maxReconnectionDelay,
|
|
369
|
+
minReconnectionDelay: this.options.minReconnectionDelay,
|
|
370
|
+
reconnectionDelayGrowFactor: 1.3,
|
|
371
|
+
connectionTimeout: this.options.connectionTimeout,
|
|
372
|
+
maxRetries: this.options.maxRetries,
|
|
373
|
+
debug: this.options.debug
|
|
374
|
+
});
|
|
375
|
+
} else {
|
|
376
|
+
this.ws = new WebSocket(this.serverUrl);
|
|
377
|
+
}
|
|
378
|
+
return new Promise((resolve, reject) => {
|
|
379
|
+
const onOpen = () => {
|
|
380
|
+
if (this.hasConnectedBefore) {
|
|
381
|
+
logger.info("WebSocket reconnected successfully", { serverUrl: this.serverUrl });
|
|
382
|
+
} else {
|
|
383
|
+
logger.info("WebSocket connected", { serverUrl: this.serverUrl });
|
|
384
|
+
this.hasConnectedBefore = true;
|
|
385
|
+
}
|
|
386
|
+
for (const handler of this.openHandlers) {
|
|
387
|
+
handler();
|
|
388
|
+
}
|
|
389
|
+
resolve();
|
|
390
|
+
};
|
|
391
|
+
const onError = (_event) => {
|
|
392
|
+
logger.error("WebSocket connection failed", { serverUrl: this.serverUrl });
|
|
393
|
+
const error = new Error("WebSocket connection failed");
|
|
394
|
+
for (const handler of this.errorHandlers) {
|
|
395
|
+
handler(error);
|
|
396
|
+
}
|
|
397
|
+
reject(error);
|
|
398
|
+
};
|
|
399
|
+
this.ws.addEventListener("open", onOpen, { once: true });
|
|
400
|
+
this.ws.addEventListener("error", onError, { once: true });
|
|
401
|
+
this.ws.addEventListener("message", ((event) => {
|
|
402
|
+
const message = event.data;
|
|
403
|
+
for (const handler of this.messageHandlers) {
|
|
404
|
+
handler(message);
|
|
405
|
+
}
|
|
406
|
+
}));
|
|
407
|
+
this.ws.addEventListener("close", (() => {
|
|
408
|
+
logger.info("WebSocket closed, attempting to reconnect...");
|
|
409
|
+
for (const handler of this.closeHandlers) {
|
|
410
|
+
handler();
|
|
411
|
+
}
|
|
412
|
+
}));
|
|
413
|
+
this.ws.addEventListener("error", ((_event) => {
|
|
414
|
+
logger.error("WebSocket error");
|
|
415
|
+
const error = new Error("WebSocket error");
|
|
416
|
+
for (const handler of this.errorHandlers) {
|
|
417
|
+
handler(error);
|
|
418
|
+
}
|
|
419
|
+
}));
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
send(message) {
|
|
423
|
+
if (!this.ws || this.ws.readyState !== 1) {
|
|
424
|
+
throw new Error("WebSocket is not open");
|
|
425
|
+
}
|
|
426
|
+
this.ws.send(message);
|
|
427
|
+
}
|
|
428
|
+
onMessage(handler) {
|
|
429
|
+
this.messageHandlers.add(handler);
|
|
430
|
+
return () => {
|
|
431
|
+
this.messageHandlers.delete(handler);
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
onOpen(handler) {
|
|
435
|
+
this.openHandlers.add(handler);
|
|
436
|
+
return () => {
|
|
437
|
+
this.openHandlers.delete(handler);
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
onClose(handler) {
|
|
441
|
+
this.closeHandlers.add(handler);
|
|
442
|
+
return () => {
|
|
443
|
+
this.closeHandlers.delete(handler);
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
onError(handler) {
|
|
447
|
+
this.errorHandlers.add(handler);
|
|
448
|
+
return () => {
|
|
449
|
+
this.errorHandlers.delete(handler);
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
close() {
|
|
453
|
+
if (this.ws) {
|
|
454
|
+
this.ws.close();
|
|
455
|
+
this.ws = null;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
dispose() {
|
|
459
|
+
this.close();
|
|
460
|
+
this.messageHandlers.clear();
|
|
461
|
+
this.openHandlers.clear();
|
|
462
|
+
this.closeHandlers.clear();
|
|
463
|
+
this.errorHandlers.clear();
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
// ../network/dist/index.js
|
|
470
|
+
var dist_exports2 = {};
|
|
471
|
+
__export(dist_exports2, {
|
|
472
|
+
WebSocketClient: () => WebSocketClient,
|
|
473
|
+
WebSocketServer: () => WebSocketServer,
|
|
474
|
+
createWebSocketClient: () => createWebSocketClient
|
|
475
|
+
});
|
|
476
|
+
var logger2, WebSocketConnection, WebSocketServer;
|
|
477
|
+
var init_dist2 = __esm({
|
|
478
|
+
"../network/dist/index.js"() {
|
|
479
|
+
init_chunk_63P5VUHB();
|
|
480
|
+
init_dist();
|
|
481
|
+
logger2 = createLogger("network/WebSocketServer");
|
|
482
|
+
WebSocketConnection = class {
|
|
483
|
+
constructor(ws, options) {
|
|
484
|
+
__publicField2(this, "id");
|
|
485
|
+
__publicField2(this, "ws");
|
|
486
|
+
__publicField2(this, "messageHandlers", /* @__PURE__ */ new Set());
|
|
487
|
+
__publicField2(this, "closeHandlers", /* @__PURE__ */ new Set());
|
|
488
|
+
__publicField2(this, "errorHandlers", /* @__PURE__ */ new Set());
|
|
489
|
+
__publicField2(this, "heartbeatInterval");
|
|
490
|
+
__publicField2(this, "isAlive", true);
|
|
491
|
+
this.ws = ws;
|
|
492
|
+
this.id = `conn_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
|
|
493
|
+
if (options.heartbeat !== false) {
|
|
494
|
+
const interval = options.heartbeatInterval || 3e4;
|
|
495
|
+
ws.on("pong", () => {
|
|
496
|
+
this.isAlive = true;
|
|
497
|
+
logger2.debug("Heartbeat pong received", { id: this.id });
|
|
498
|
+
});
|
|
499
|
+
this.heartbeatInterval = setInterval(() => {
|
|
500
|
+
if (!this.isAlive) {
|
|
501
|
+
logger2.warn("Client heartbeat timeout, terminating connection", { id: this.id });
|
|
502
|
+
clearInterval(this.heartbeatInterval);
|
|
503
|
+
ws.terminate();
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
this.isAlive = false;
|
|
507
|
+
ws.ping();
|
|
508
|
+
logger2.debug("Heartbeat ping sent", { id: this.id });
|
|
509
|
+
}, interval);
|
|
510
|
+
}
|
|
511
|
+
ws.on("message", (data) => {
|
|
512
|
+
const message = data.toString();
|
|
513
|
+
for (const handler of this.messageHandlers) {
|
|
514
|
+
handler(message);
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
ws.on("close", () => {
|
|
518
|
+
if (this.heartbeatInterval) {
|
|
519
|
+
clearInterval(this.heartbeatInterval);
|
|
520
|
+
}
|
|
521
|
+
for (const handler of this.closeHandlers) {
|
|
522
|
+
handler();
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
ws.on("error", (err) => {
|
|
526
|
+
if (this.heartbeatInterval) {
|
|
527
|
+
clearInterval(this.heartbeatInterval);
|
|
528
|
+
}
|
|
529
|
+
for (const handler of this.errorHandlers) {
|
|
530
|
+
handler(err);
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
send(message) {
|
|
535
|
+
if (this.ws.readyState === 1) {
|
|
536
|
+
this.ws.send(message);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
onMessage(handler) {
|
|
540
|
+
this.messageHandlers.add(handler);
|
|
541
|
+
return () => {
|
|
542
|
+
this.messageHandlers.delete(handler);
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
onClose(handler) {
|
|
546
|
+
this.closeHandlers.add(handler);
|
|
547
|
+
return () => {
|
|
548
|
+
this.closeHandlers.delete(handler);
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
onError(handler) {
|
|
552
|
+
this.errorHandlers.add(handler);
|
|
553
|
+
return () => {
|
|
554
|
+
this.errorHandlers.delete(handler);
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
close() {
|
|
558
|
+
if (this.heartbeatInterval) {
|
|
559
|
+
clearInterval(this.heartbeatInterval);
|
|
560
|
+
}
|
|
561
|
+
this.ws.close();
|
|
562
|
+
}
|
|
563
|
+
};
|
|
564
|
+
WebSocketServer = class {
|
|
565
|
+
constructor(options = {}) {
|
|
566
|
+
__publicField2(this, "wss", null);
|
|
567
|
+
__publicField2(this, "connections", /* @__PURE__ */ new Set());
|
|
568
|
+
__publicField2(this, "connectionHandlers", /* @__PURE__ */ new Set());
|
|
569
|
+
__publicField2(this, "options");
|
|
570
|
+
__publicField2(this, "attachedToServer", false);
|
|
571
|
+
this.options = options;
|
|
572
|
+
}
|
|
573
|
+
async listen(port, host = "0.0.0.0") {
|
|
574
|
+
if (this.wss) {
|
|
575
|
+
throw new Error("Server already listening");
|
|
576
|
+
}
|
|
577
|
+
if (this.attachedToServer) {
|
|
578
|
+
throw new Error(
|
|
579
|
+
"Cannot listen when attached to existing server. The server should call listen() instead."
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
const { WebSocketServer: WSS } = await import('ws');
|
|
583
|
+
this.wss = new WSS({ port, host });
|
|
584
|
+
this.wss.on("connection", (ws) => {
|
|
585
|
+
this.handleConnection(ws);
|
|
586
|
+
});
|
|
587
|
+
logger2.info("WebSocket server listening", { port, host });
|
|
588
|
+
}
|
|
589
|
+
attach(server, path = "/ws") {
|
|
590
|
+
if (this.wss) {
|
|
591
|
+
throw new Error("Server already initialized");
|
|
592
|
+
}
|
|
593
|
+
import('ws').then(({ WebSocketServer: WSS }) => {
|
|
594
|
+
this.wss = new WSS({ noServer: true });
|
|
595
|
+
server.on("upgrade", (request, socket, head) => {
|
|
596
|
+
const url = new URL(request.url || "", `http://${request.headers.host}`);
|
|
597
|
+
if (url.pathname === path) {
|
|
598
|
+
this.wss.handleUpgrade(request, socket, head, (ws) => {
|
|
599
|
+
this.wss.emit("connection", ws, request);
|
|
600
|
+
});
|
|
601
|
+
} else {
|
|
602
|
+
socket.destroy();
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
this.wss.on("connection", (ws) => {
|
|
606
|
+
this.handleConnection(ws);
|
|
607
|
+
});
|
|
608
|
+
this.attachedToServer = true;
|
|
609
|
+
logger2.info("WebSocket attached to existing HTTP server", { path });
|
|
610
|
+
});
|
|
611
|
+
}
|
|
612
|
+
handleConnection(ws) {
|
|
613
|
+
const connection = new WebSocketConnection(ws, this.options);
|
|
614
|
+
this.connections.add(connection);
|
|
615
|
+
logger2.info("Client connected", {
|
|
616
|
+
connectionId: connection.id,
|
|
617
|
+
totalConnections: this.connections.size
|
|
618
|
+
});
|
|
619
|
+
connection.onClose(() => {
|
|
620
|
+
this.connections.delete(connection);
|
|
621
|
+
logger2.info("Client disconnected", {
|
|
622
|
+
connectionId: connection.id,
|
|
623
|
+
totalConnections: this.connections.size
|
|
624
|
+
});
|
|
625
|
+
});
|
|
626
|
+
for (const handler of this.connectionHandlers) {
|
|
627
|
+
handler(connection);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
onConnection(handler) {
|
|
631
|
+
this.connectionHandlers.add(handler);
|
|
632
|
+
return () => {
|
|
633
|
+
this.connectionHandlers.delete(handler);
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
broadcast(message) {
|
|
637
|
+
for (const connection of this.connections) {
|
|
638
|
+
connection.send(message);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
async close() {
|
|
642
|
+
if (!this.wss) return;
|
|
643
|
+
for (const connection of this.connections) {
|
|
644
|
+
connection.close();
|
|
645
|
+
}
|
|
646
|
+
this.connections.clear();
|
|
647
|
+
if (!this.attachedToServer) {
|
|
648
|
+
await new Promise((resolve) => {
|
|
649
|
+
this.wss.close(() => resolve());
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
this.wss = null;
|
|
653
|
+
}
|
|
654
|
+
async dispose() {
|
|
655
|
+
await this.close();
|
|
656
|
+
this.connectionHandlers.clear();
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
|
|
197
662
|
// src/createLocalAgentX.ts
|
|
198
663
|
var createLocalAgentX_exports = {};
|
|
199
664
|
__export(createLocalAgentX_exports, {
|
|
200
665
|
createLocalAgentX: () => createLocalAgentX
|
|
201
666
|
});
|
|
202
|
-
function setupConnectionHandler(ws, connections, runtime) {
|
|
203
|
-
connections.add(ws);
|
|
204
|
-
logger.info("Client connected", { totalConnections: connections.size });
|
|
205
|
-
ws.on("message", (data) => {
|
|
206
|
-
try {
|
|
207
|
-
const event = JSON.parse(data.toString());
|
|
208
|
-
logger.info("Received from client", {
|
|
209
|
-
type: event.type,
|
|
210
|
-
requestId: event.data?.requestId
|
|
211
|
-
});
|
|
212
|
-
runtime.emit(event);
|
|
213
|
-
} catch {
|
|
214
|
-
}
|
|
215
|
-
});
|
|
216
|
-
ws.on("close", () => {
|
|
217
|
-
connections.delete(ws);
|
|
218
|
-
logger.info("Client disconnected", { totalConnections: connections.size });
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
function setupBroadcasting(connections, runtime) {
|
|
222
|
-
runtime.onAny((event) => {
|
|
223
|
-
if (event.broadcastable === false) {
|
|
224
|
-
return;
|
|
225
|
-
}
|
|
226
|
-
logger.info("Broadcasting to clients", {
|
|
227
|
-
type: event.type,
|
|
228
|
-
category: event.category,
|
|
229
|
-
requestId: event.data?.requestId
|
|
230
|
-
});
|
|
231
|
-
const message = JSON.stringify(event);
|
|
232
|
-
for (const ws of connections) {
|
|
233
|
-
if (ws.readyState === 1) {
|
|
234
|
-
ws.send(message);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
667
|
async function createLocalAgentX(config) {
|
|
240
668
|
if (config.logger) {
|
|
241
669
|
const { LoggerFactoryImpl: LoggerFactoryImpl2, setLoggerFactory: setLoggerFactory2 } = await Promise.resolve().then(() => (init_dist(), dist_exports));
|
|
@@ -263,28 +691,28 @@ async function createLocalAgentX(config) {
|
|
|
263
691
|
})
|
|
264
692
|
}
|
|
265
693
|
});
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
});
|
|
278
|
-
} else {
|
|
279
|
-
socket.destroy();
|
|
694
|
+
const wsServer = new WebSocketServer({
|
|
695
|
+
heartbeat: true,
|
|
696
|
+
heartbeatInterval: 3e4,
|
|
697
|
+
debug: false
|
|
698
|
+
});
|
|
699
|
+
wsServer.onConnection((connection) => {
|
|
700
|
+
connection.onMessage((message) => {
|
|
701
|
+
try {
|
|
702
|
+
const event = JSON.parse(message);
|
|
703
|
+
runtime.emit(event);
|
|
704
|
+
} catch {
|
|
280
705
|
}
|
|
281
706
|
});
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
707
|
+
});
|
|
708
|
+
runtime.onAny((event) => {
|
|
709
|
+
if (event.broadcastable === false) {
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
wsServer.broadcast(JSON.stringify(event));
|
|
713
|
+
});
|
|
714
|
+
if (config.server) {
|
|
715
|
+
wsServer.attach(config.server, "/ws");
|
|
288
716
|
}
|
|
289
717
|
return {
|
|
290
718
|
// Core API - delegate to runtime
|
|
@@ -294,56 +722,25 @@ async function createLocalAgentX(config) {
|
|
|
294
722
|
emitCommand: (type, data) => runtime.emitCommand(type, data),
|
|
295
723
|
// Server API
|
|
296
724
|
async listen(port, host) {
|
|
297
|
-
if (
|
|
725
|
+
if (config.server) {
|
|
298
726
|
throw new Error(
|
|
299
727
|
"Cannot listen when attached to existing server. The server should call listen() instead."
|
|
300
728
|
);
|
|
301
729
|
}
|
|
302
|
-
|
|
303
|
-
throw new Error("Server already listening");
|
|
304
|
-
}
|
|
305
|
-
const { WebSocketServer } = await import('ws');
|
|
306
|
-
peer = new WebSocketServer({ port, host: host ?? "0.0.0.0" });
|
|
307
|
-
peer.on("connection", (ws) => {
|
|
308
|
-
setupConnectionHandler(ws, connections, runtime);
|
|
309
|
-
});
|
|
310
|
-
setupBroadcasting(connections, runtime);
|
|
730
|
+
await wsServer.listen(port, host);
|
|
311
731
|
},
|
|
312
732
|
async close() {
|
|
313
|
-
|
|
314
|
-
for (const ws of connections) {
|
|
315
|
-
ws.close();
|
|
316
|
-
}
|
|
317
|
-
connections.clear();
|
|
318
|
-
if (!attachedToServer) {
|
|
319
|
-
await new Promise((resolve) => {
|
|
320
|
-
peer.close(() => resolve());
|
|
321
|
-
});
|
|
322
|
-
}
|
|
323
|
-
peer = null;
|
|
733
|
+
await wsServer.close();
|
|
324
734
|
},
|
|
325
735
|
async dispose() {
|
|
326
|
-
|
|
327
|
-
for (const ws of connections) {
|
|
328
|
-
ws.close();
|
|
329
|
-
}
|
|
330
|
-
connections.clear();
|
|
331
|
-
if (!attachedToServer) {
|
|
332
|
-
await new Promise((resolve) => {
|
|
333
|
-
peer.close(() => resolve());
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
peer = null;
|
|
337
|
-
}
|
|
736
|
+
await wsServer.dispose();
|
|
338
737
|
await runtime.dispose();
|
|
339
738
|
}
|
|
340
739
|
};
|
|
341
740
|
}
|
|
342
|
-
var logger;
|
|
343
741
|
var init_createLocalAgentX = __esm({
|
|
344
742
|
"src/createLocalAgentX.ts"() {
|
|
345
|
-
|
|
346
|
-
logger = createLogger("agentx/createAgentX");
|
|
743
|
+
init_dist2();
|
|
347
744
|
}
|
|
348
745
|
});
|
|
349
746
|
|
|
@@ -365,25 +762,22 @@ async function createAgentX(config) {
|
|
|
365
762
|
const { createLocalAgentX: createLocalAgentX2 } = await Promise.resolve().then(() => (init_createLocalAgentX(), createLocalAgentX_exports));
|
|
366
763
|
return createLocalAgentX2(config ?? {});
|
|
367
764
|
}
|
|
368
|
-
var isBrowser = typeof window !== "undefined" && typeof window.WebSocket !== "undefined";
|
|
369
765
|
async function createRemoteAgentX(serverUrl) {
|
|
370
|
-
const
|
|
371
|
-
const
|
|
766
|
+
const { createWebSocketClient: createWebSocketClient2 } = await Promise.resolve().then(() => (init_dist2(), dist_exports2));
|
|
767
|
+
const client = await createWebSocketClient2({
|
|
768
|
+
serverUrl,
|
|
769
|
+
autoReconnect: true,
|
|
770
|
+
minReconnectionDelay: 1e3,
|
|
771
|
+
maxReconnectionDelay: 1e4,
|
|
772
|
+
connectionTimeout: 4e3,
|
|
773
|
+
maxRetries: Infinity,
|
|
774
|
+
debug: false
|
|
775
|
+
});
|
|
372
776
|
const handlers = /* @__PURE__ */ new Map();
|
|
373
777
|
const pendingRequests = /* @__PURE__ */ new Map();
|
|
374
|
-
|
|
375
|
-
if (isBrowser) {
|
|
376
|
-
ws.onopen = () => resolve();
|
|
377
|
-
ws.onerror = () => reject(new Error("WebSocket connection failed"));
|
|
378
|
-
} else {
|
|
379
|
-
ws.on("open", () => resolve());
|
|
380
|
-
ws.on("error", (err) => reject(err));
|
|
381
|
-
}
|
|
382
|
-
});
|
|
383
|
-
const handleMessage = (data) => {
|
|
778
|
+
client.onMessage((message) => {
|
|
384
779
|
try {
|
|
385
|
-
const
|
|
386
|
-
const event = JSON.parse(text);
|
|
780
|
+
const event = JSON.parse(message);
|
|
387
781
|
remoteLogger.info("Received event", {
|
|
388
782
|
type: event.type,
|
|
389
783
|
category: event.category,
|
|
@@ -421,12 +815,13 @@ async function createRemoteAgentX(serverUrl) {
|
|
|
421
815
|
}
|
|
422
816
|
} catch {
|
|
423
817
|
}
|
|
424
|
-
};
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
|
|
818
|
+
});
|
|
819
|
+
client.onClose(() => {
|
|
820
|
+
remoteLogger.warn("WebSocket closed");
|
|
821
|
+
});
|
|
822
|
+
client.onError((error) => {
|
|
823
|
+
remoteLogger.error("WebSocket error", { error: error.message });
|
|
824
|
+
});
|
|
430
825
|
function subscribe(type, handler) {
|
|
431
826
|
if (!handlers.has(type)) {
|
|
432
827
|
handlers.set(type, /* @__PURE__ */ new Set());
|
|
@@ -457,7 +852,7 @@ async function createRemoteAgentX(serverUrl) {
|
|
|
457
852
|
category: "request",
|
|
458
853
|
intent: "request"
|
|
459
854
|
};
|
|
460
|
-
|
|
855
|
+
client.send(JSON.stringify(event));
|
|
461
856
|
});
|
|
462
857
|
},
|
|
463
858
|
on(type, handler) {
|
|
@@ -475,7 +870,7 @@ async function createRemoteAgentX(serverUrl) {
|
|
|
475
870
|
category: type.toString().endsWith("_response") ? "response" : "request",
|
|
476
871
|
intent: type.toString().endsWith("_response") ? "result" : "request"
|
|
477
872
|
};
|
|
478
|
-
|
|
873
|
+
client.send(JSON.stringify(event));
|
|
479
874
|
},
|
|
480
875
|
async listen() {
|
|
481
876
|
throw new Error("Cannot listen in remote mode");
|
|
@@ -489,11 +884,7 @@ async function createRemoteAgentX(serverUrl) {
|
|
|
489
884
|
}
|
|
490
885
|
pendingRequests.clear();
|
|
491
886
|
handlers.clear();
|
|
492
|
-
|
|
493
|
-
ws.close();
|
|
494
|
-
} else {
|
|
495
|
-
ws.close();
|
|
496
|
-
}
|
|
887
|
+
client.dispose();
|
|
497
888
|
}
|
|
498
889
|
};
|
|
499
890
|
}
|