@agentxjs/ui 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/index.js
CHANGED
|
@@ -172,7 +172,7 @@ __publicField$1(LoggerFactoryImpl$1, "config", {
|
|
|
172
172
|
function createLogger$1(name2) {
|
|
173
173
|
return LoggerFactoryImpl$1.getLogger(name2);
|
|
174
174
|
}
|
|
175
|
-
const logger$
|
|
175
|
+
const logger$3 = createLogger$1("ui/useAgent");
|
|
176
176
|
function useAgent(agentx, imageId, options = {}) {
|
|
177
177
|
const { initialMessages = [], onSend, onError, onStatusChange } = options;
|
|
178
178
|
const [messages, setMessages] = useState(initialMessages);
|
|
@@ -203,10 +203,10 @@ function useAgent(agentx, imageId, options = {}) {
|
|
|
203
203
|
metadata: m.role === "error" && m.errorCode ? { errorCode: m.errorCode } : void 0
|
|
204
204
|
}));
|
|
205
205
|
setMessages(mappedMessages);
|
|
206
|
-
logger$
|
|
206
|
+
logger$3.debug("Loaded messages from storage", { imageId, count: mappedMessages.length });
|
|
207
207
|
}
|
|
208
208
|
}).catch((err) => {
|
|
209
|
-
logger$
|
|
209
|
+
logger$3.error("Failed to load messages", { imageId, error: err });
|
|
210
210
|
});
|
|
211
211
|
}
|
|
212
212
|
}, [imageId, agentx]);
|
|
@@ -347,11 +347,11 @@ function useAgent(agentx, imageId, options = {}) {
|
|
|
347
347
|
onStatusChange == null ? void 0 : onStatusChange("error");
|
|
348
348
|
})
|
|
349
349
|
);
|
|
350
|
-
logger$
|
|
350
|
+
logger$3.debug("Subscribed to agent events", { imageId });
|
|
351
351
|
return () => {
|
|
352
352
|
mountedRef.current = false;
|
|
353
353
|
unsubscribes.forEach((unsub) => unsub());
|
|
354
|
-
logger$
|
|
354
|
+
logger$3.debug("Unsubscribed from agent events", { imageId });
|
|
355
355
|
};
|
|
356
356
|
}, [agentx, imageId, onError, onStatusChange]);
|
|
357
357
|
const send = useCallback(
|
|
@@ -376,10 +376,10 @@ function useAgent(agentx, imageId, options = {}) {
|
|
|
376
376
|
if (response.data.agentId) {
|
|
377
377
|
agentIdRef.current = response.data.agentId;
|
|
378
378
|
setAgentIdState(response.data.agentId);
|
|
379
|
-
logger$
|
|
379
|
+
logger$3.debug("Agent activated", { imageId, agentId: response.data.agentId });
|
|
380
380
|
}
|
|
381
381
|
} catch (error) {
|
|
382
|
-
logger$
|
|
382
|
+
logger$3.error("Failed to send message", { imageId, error });
|
|
383
383
|
setStatus("error");
|
|
384
384
|
onStatusChange == null ? void 0 : onStatusChange("error");
|
|
385
385
|
}
|
|
@@ -389,7 +389,7 @@ function useAgent(agentx, imageId, options = {}) {
|
|
|
389
389
|
const interrupt = useCallback(() => {
|
|
390
390
|
if (!agentx || !imageId) return;
|
|
391
391
|
agentx.request("agent_interrupt_request", { imageId }).catch((error) => {
|
|
392
|
-
logger$
|
|
392
|
+
logger$3.error("Failed to interrupt agent", { imageId, error });
|
|
393
393
|
});
|
|
394
394
|
}, [agentx, imageId]);
|
|
395
395
|
const clearMessages = useCallback(() => {
|
|
@@ -412,18 +412,23 @@ function useAgent(agentx, imageId, options = {}) {
|
|
|
412
412
|
agentId: agentIdState
|
|
413
413
|
};
|
|
414
414
|
}
|
|
415
|
+
var __defProp2 = Object.defineProperty;
|
|
415
416
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
416
417
|
var __esm = (fn, res) => function __init() {
|
|
417
418
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
418
419
|
};
|
|
420
|
+
var __export = (target, all2) => {
|
|
421
|
+
for (var name2 in all2)
|
|
422
|
+
__defProp2(target, name2, { get: all2[name2], enumerable: true });
|
|
423
|
+
};
|
|
419
424
|
function createLogger(name2) {
|
|
420
425
|
return LoggerFactoryImpl2.getLogger(name2);
|
|
421
426
|
}
|
|
422
|
-
var
|
|
427
|
+
var __defProp22, __defNormalProp2, __publicField2, _ConsoleLogger2, ConsoleLogger, externalFactory, LoggerFactoryImpl2;
|
|
423
428
|
var init_dist = __esm({
|
|
424
429
|
"../common/dist/index.js"() {
|
|
425
|
-
|
|
426
|
-
__defNormalProp2 = (obj, key, value) => key in obj ?
|
|
430
|
+
__defProp22 = Object.defineProperty;
|
|
431
|
+
__defNormalProp2 = (obj, key, value) => key in obj ? __defProp22(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
427
432
|
__publicField2 = (obj, key, value) => __defNormalProp2(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
428
433
|
_ConsoleLogger2 = class _ConsoleLogger22 {
|
|
429
434
|
constructor(name2, options = {}) {
|
|
@@ -589,31 +594,489 @@ var init_dist = __esm({
|
|
|
589
594
|
});
|
|
590
595
|
}
|
|
591
596
|
});
|
|
597
|
+
async function createWebSocketClient(options) {
|
|
598
|
+
if (isBrowser) {
|
|
599
|
+
const client = new BrowserWebSocketClient(options);
|
|
600
|
+
await client.connect();
|
|
601
|
+
return client;
|
|
602
|
+
} else {
|
|
603
|
+
const client = new WebSocketClient(options);
|
|
604
|
+
await client.connect();
|
|
605
|
+
return client;
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
var __defProp3, __defNormalProp22, __publicField22, logger$2, isBrowser, WebSocketClient, BrowserWebSocketClient;
|
|
609
|
+
var init_chunk_63P5VUHB = __esm({
|
|
610
|
+
"../network/dist/chunk-63P5VUHB.js"() {
|
|
611
|
+
init_dist();
|
|
612
|
+
__defProp3 = Object.defineProperty;
|
|
613
|
+
__defNormalProp22 = (obj, key, value) => key in obj ? __defProp3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
614
|
+
__publicField22 = (obj, key, value) => __defNormalProp22(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
615
|
+
logger$2 = createLogger("network/WebSocketClient");
|
|
616
|
+
isBrowser = typeof globalThis !== "undefined" && typeof globalThis.window !== "undefined" && typeof globalThis.window.WebSocket !== "undefined";
|
|
617
|
+
WebSocketClient = class {
|
|
618
|
+
constructor(options) {
|
|
619
|
+
__publicField22(this, "ws", null);
|
|
620
|
+
__publicField22(this, "serverUrl");
|
|
621
|
+
__publicField22(this, "messageHandlers", /* @__PURE__ */ new Set());
|
|
622
|
+
__publicField22(this, "openHandlers", /* @__PURE__ */ new Set());
|
|
623
|
+
__publicField22(this, "closeHandlers", /* @__PURE__ */ new Set());
|
|
624
|
+
__publicField22(this, "errorHandlers", /* @__PURE__ */ new Set());
|
|
625
|
+
if (isBrowser) {
|
|
626
|
+
throw new Error(
|
|
627
|
+
"Use createBrowserWebSocketClient() in browser environment for auto-reconnect support"
|
|
628
|
+
);
|
|
629
|
+
}
|
|
630
|
+
this.serverUrl = options.serverUrl;
|
|
631
|
+
}
|
|
632
|
+
get readyState() {
|
|
633
|
+
if (!this.ws) return "closed";
|
|
634
|
+
const state = this.ws.readyState;
|
|
635
|
+
if (state === 0) return "connecting";
|
|
636
|
+
if (state === 1) return "open";
|
|
637
|
+
if (state === 2) return "closing";
|
|
638
|
+
return "closed";
|
|
639
|
+
}
|
|
640
|
+
async connect() {
|
|
641
|
+
if (this.ws) {
|
|
642
|
+
throw new Error("Already connected or connecting");
|
|
643
|
+
}
|
|
644
|
+
const { WebSocket: NodeWebSocket } = await import("./browser-Bab_T5G7.js").then((n) => n.b);
|
|
645
|
+
this.ws = new NodeWebSocket(this.serverUrl);
|
|
646
|
+
return new Promise((resolve, reject) => {
|
|
647
|
+
const onOpen = () => {
|
|
648
|
+
logger$2.info("WebSocket connected", { serverUrl: this.serverUrl });
|
|
649
|
+
for (const handler of this.openHandlers) {
|
|
650
|
+
handler();
|
|
651
|
+
}
|
|
652
|
+
resolve();
|
|
653
|
+
};
|
|
654
|
+
const onError = (err) => {
|
|
655
|
+
logger$2.error("WebSocket connection failed", {
|
|
656
|
+
serverUrl: this.serverUrl,
|
|
657
|
+
error: err == null ? void 0 : err.message
|
|
658
|
+
});
|
|
659
|
+
reject(err || new Error("WebSocket connection failed"));
|
|
660
|
+
};
|
|
661
|
+
this.ws.once("open", onOpen);
|
|
662
|
+
this.ws.once("error", onError);
|
|
663
|
+
this.ws.on("message", (data2) => {
|
|
664
|
+
const message = data2.toString();
|
|
665
|
+
for (const handler of this.messageHandlers) {
|
|
666
|
+
handler(message);
|
|
667
|
+
}
|
|
668
|
+
});
|
|
669
|
+
this.ws.on("close", () => {
|
|
670
|
+
logger$2.warn("WebSocket closed");
|
|
671
|
+
for (const handler of this.closeHandlers) {
|
|
672
|
+
handler();
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
this.ws.on("error", (err) => {
|
|
676
|
+
logger$2.error("WebSocket error", { error: err.message });
|
|
677
|
+
for (const handler of this.errorHandlers) {
|
|
678
|
+
handler(err);
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
send(message) {
|
|
684
|
+
if (!this.ws || this.ws.readyState !== 1) {
|
|
685
|
+
throw new Error("WebSocket is not open");
|
|
686
|
+
}
|
|
687
|
+
this.ws.send(message);
|
|
688
|
+
}
|
|
689
|
+
onMessage(handler) {
|
|
690
|
+
this.messageHandlers.add(handler);
|
|
691
|
+
return () => {
|
|
692
|
+
this.messageHandlers.delete(handler);
|
|
693
|
+
};
|
|
694
|
+
}
|
|
695
|
+
onOpen(handler) {
|
|
696
|
+
this.openHandlers.add(handler);
|
|
697
|
+
return () => {
|
|
698
|
+
this.openHandlers.delete(handler);
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
onClose(handler) {
|
|
702
|
+
this.closeHandlers.add(handler);
|
|
703
|
+
return () => {
|
|
704
|
+
this.closeHandlers.delete(handler);
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
onError(handler) {
|
|
708
|
+
this.errorHandlers.add(handler);
|
|
709
|
+
return () => {
|
|
710
|
+
this.errorHandlers.delete(handler);
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
close() {
|
|
714
|
+
if (this.ws) {
|
|
715
|
+
this.ws.close();
|
|
716
|
+
this.ws = null;
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
dispose() {
|
|
720
|
+
this.close();
|
|
721
|
+
this.messageHandlers.clear();
|
|
722
|
+
this.openHandlers.clear();
|
|
723
|
+
this.closeHandlers.clear();
|
|
724
|
+
this.errorHandlers.clear();
|
|
725
|
+
}
|
|
726
|
+
};
|
|
727
|
+
BrowserWebSocketClient = class {
|
|
728
|
+
// Track if this is a reconnection
|
|
729
|
+
constructor(options) {
|
|
730
|
+
__publicField22(this, "ws", null);
|
|
731
|
+
__publicField22(this, "serverUrl");
|
|
732
|
+
__publicField22(this, "options");
|
|
733
|
+
__publicField22(this, "messageHandlers", /* @__PURE__ */ new Set());
|
|
734
|
+
__publicField22(this, "openHandlers", /* @__PURE__ */ new Set());
|
|
735
|
+
__publicField22(this, "closeHandlers", /* @__PURE__ */ new Set());
|
|
736
|
+
__publicField22(this, "errorHandlers", /* @__PURE__ */ new Set());
|
|
737
|
+
__publicField22(this, "hasConnectedBefore", false);
|
|
738
|
+
if (!isBrowser) {
|
|
739
|
+
throw new Error("BrowserWebSocketClient can only be used in browser environment");
|
|
740
|
+
}
|
|
741
|
+
this.serverUrl = options.serverUrl;
|
|
742
|
+
this.options = {
|
|
743
|
+
autoReconnect: true,
|
|
744
|
+
minReconnectionDelay: 1e3,
|
|
745
|
+
maxReconnectionDelay: 1e4,
|
|
746
|
+
maxRetries: Infinity,
|
|
747
|
+
connectionTimeout: 4e3,
|
|
748
|
+
debug: false,
|
|
749
|
+
...options
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
get readyState() {
|
|
753
|
+
if (!this.ws) return "closed";
|
|
754
|
+
const state = this.ws.readyState;
|
|
755
|
+
if (state === 0) return "connecting";
|
|
756
|
+
if (state === 1) return "open";
|
|
757
|
+
if (state === 2) return "closing";
|
|
758
|
+
return "closed";
|
|
759
|
+
}
|
|
760
|
+
async connect() {
|
|
761
|
+
if (this.ws) {
|
|
762
|
+
throw new Error("Already connected or connecting");
|
|
763
|
+
}
|
|
764
|
+
if (this.options.autoReconnect) {
|
|
765
|
+
const ReconnectingWebSocket = (await import("./reconnecting-websocket-mjs-CzX0YT0F.js")).default;
|
|
766
|
+
this.ws = new ReconnectingWebSocket(this.serverUrl, [], {
|
|
767
|
+
maxReconnectionDelay: this.options.maxReconnectionDelay,
|
|
768
|
+
minReconnectionDelay: this.options.minReconnectionDelay,
|
|
769
|
+
reconnectionDelayGrowFactor: 1.3,
|
|
770
|
+
connectionTimeout: this.options.connectionTimeout,
|
|
771
|
+
maxRetries: this.options.maxRetries,
|
|
772
|
+
debug: this.options.debug
|
|
773
|
+
});
|
|
774
|
+
} else {
|
|
775
|
+
this.ws = new WebSocket(this.serverUrl);
|
|
776
|
+
}
|
|
777
|
+
return new Promise((resolve, reject) => {
|
|
778
|
+
const onOpen = () => {
|
|
779
|
+
if (this.hasConnectedBefore) {
|
|
780
|
+
logger$2.info("WebSocket reconnected successfully", { serverUrl: this.serverUrl });
|
|
781
|
+
} else {
|
|
782
|
+
logger$2.info("WebSocket connected", { serverUrl: this.serverUrl });
|
|
783
|
+
this.hasConnectedBefore = true;
|
|
784
|
+
}
|
|
785
|
+
for (const handler of this.openHandlers) {
|
|
786
|
+
handler();
|
|
787
|
+
}
|
|
788
|
+
resolve();
|
|
789
|
+
};
|
|
790
|
+
const onError = (_event) => {
|
|
791
|
+
logger$2.error("WebSocket connection failed", { serverUrl: this.serverUrl });
|
|
792
|
+
const error = new Error("WebSocket connection failed");
|
|
793
|
+
for (const handler of this.errorHandlers) {
|
|
794
|
+
handler(error);
|
|
795
|
+
}
|
|
796
|
+
reject(error);
|
|
797
|
+
};
|
|
798
|
+
this.ws.addEventListener("open", onOpen, { once: true });
|
|
799
|
+
this.ws.addEventListener("error", onError, { once: true });
|
|
800
|
+
this.ws.addEventListener("message", ((event) => {
|
|
801
|
+
const message = event.data;
|
|
802
|
+
for (const handler of this.messageHandlers) {
|
|
803
|
+
handler(message);
|
|
804
|
+
}
|
|
805
|
+
}));
|
|
806
|
+
this.ws.addEventListener("close", (() => {
|
|
807
|
+
logger$2.info("WebSocket closed, attempting to reconnect...");
|
|
808
|
+
for (const handler of this.closeHandlers) {
|
|
809
|
+
handler();
|
|
810
|
+
}
|
|
811
|
+
}));
|
|
812
|
+
this.ws.addEventListener("error", ((_event) => {
|
|
813
|
+
logger$2.error("WebSocket error");
|
|
814
|
+
const error = new Error("WebSocket error");
|
|
815
|
+
for (const handler of this.errorHandlers) {
|
|
816
|
+
handler(error);
|
|
817
|
+
}
|
|
818
|
+
}));
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
send(message) {
|
|
822
|
+
if (!this.ws || this.ws.readyState !== 1) {
|
|
823
|
+
throw new Error("WebSocket is not open");
|
|
824
|
+
}
|
|
825
|
+
this.ws.send(message);
|
|
826
|
+
}
|
|
827
|
+
onMessage(handler) {
|
|
828
|
+
this.messageHandlers.add(handler);
|
|
829
|
+
return () => {
|
|
830
|
+
this.messageHandlers.delete(handler);
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
onOpen(handler) {
|
|
834
|
+
this.openHandlers.add(handler);
|
|
835
|
+
return () => {
|
|
836
|
+
this.openHandlers.delete(handler);
|
|
837
|
+
};
|
|
838
|
+
}
|
|
839
|
+
onClose(handler) {
|
|
840
|
+
this.closeHandlers.add(handler);
|
|
841
|
+
return () => {
|
|
842
|
+
this.closeHandlers.delete(handler);
|
|
843
|
+
};
|
|
844
|
+
}
|
|
845
|
+
onError(handler) {
|
|
846
|
+
this.errorHandlers.add(handler);
|
|
847
|
+
return () => {
|
|
848
|
+
this.errorHandlers.delete(handler);
|
|
849
|
+
};
|
|
850
|
+
}
|
|
851
|
+
close() {
|
|
852
|
+
if (this.ws) {
|
|
853
|
+
this.ws.close();
|
|
854
|
+
this.ws = null;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
dispose() {
|
|
858
|
+
this.close();
|
|
859
|
+
this.messageHandlers.clear();
|
|
860
|
+
this.openHandlers.clear();
|
|
861
|
+
this.closeHandlers.clear();
|
|
862
|
+
this.errorHandlers.clear();
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
}
|
|
866
|
+
});
|
|
867
|
+
var dist_exports = {};
|
|
868
|
+
__export(dist_exports, {
|
|
869
|
+
WebSocketClient: () => WebSocketClient,
|
|
870
|
+
WebSocketServer: () => WebSocketServer,
|
|
871
|
+
createWebSocketClient: () => createWebSocketClient
|
|
872
|
+
});
|
|
873
|
+
var logger2, WebSocketConnection, WebSocketServer;
|
|
874
|
+
var init_dist2 = __esm({
|
|
875
|
+
"../network/dist/index.js"() {
|
|
876
|
+
init_chunk_63P5VUHB();
|
|
877
|
+
init_dist();
|
|
878
|
+
logger2 = createLogger("network/WebSocketServer");
|
|
879
|
+
WebSocketConnection = class {
|
|
880
|
+
constructor(ws, options) {
|
|
881
|
+
__publicField22(this, "id");
|
|
882
|
+
__publicField22(this, "ws");
|
|
883
|
+
__publicField22(this, "messageHandlers", /* @__PURE__ */ new Set());
|
|
884
|
+
__publicField22(this, "closeHandlers", /* @__PURE__ */ new Set());
|
|
885
|
+
__publicField22(this, "errorHandlers", /* @__PURE__ */ new Set());
|
|
886
|
+
__publicField22(this, "heartbeatInterval");
|
|
887
|
+
__publicField22(this, "isAlive", true);
|
|
888
|
+
this.ws = ws;
|
|
889
|
+
this.id = `conn_${Date.now()}_${Math.random().toString(36).substring(2, 8)}`;
|
|
890
|
+
if (options.heartbeat !== false) {
|
|
891
|
+
const interval = options.heartbeatInterval || 3e4;
|
|
892
|
+
ws.on("pong", () => {
|
|
893
|
+
this.isAlive = true;
|
|
894
|
+
logger2.debug("Heartbeat pong received", { id: this.id });
|
|
895
|
+
});
|
|
896
|
+
this.heartbeatInterval = setInterval(() => {
|
|
897
|
+
if (!this.isAlive) {
|
|
898
|
+
logger2.warn("Client heartbeat timeout, terminating connection", { id: this.id });
|
|
899
|
+
clearInterval(this.heartbeatInterval);
|
|
900
|
+
ws.terminate();
|
|
901
|
+
return;
|
|
902
|
+
}
|
|
903
|
+
this.isAlive = false;
|
|
904
|
+
ws.ping();
|
|
905
|
+
logger2.debug("Heartbeat ping sent", { id: this.id });
|
|
906
|
+
}, interval);
|
|
907
|
+
}
|
|
908
|
+
ws.on("message", (data2) => {
|
|
909
|
+
const message = data2.toString();
|
|
910
|
+
for (const handler of this.messageHandlers) {
|
|
911
|
+
handler(message);
|
|
912
|
+
}
|
|
913
|
+
});
|
|
914
|
+
ws.on("close", () => {
|
|
915
|
+
if (this.heartbeatInterval) {
|
|
916
|
+
clearInterval(this.heartbeatInterval);
|
|
917
|
+
}
|
|
918
|
+
for (const handler of this.closeHandlers) {
|
|
919
|
+
handler();
|
|
920
|
+
}
|
|
921
|
+
});
|
|
922
|
+
ws.on("error", (err) => {
|
|
923
|
+
if (this.heartbeatInterval) {
|
|
924
|
+
clearInterval(this.heartbeatInterval);
|
|
925
|
+
}
|
|
926
|
+
for (const handler of this.errorHandlers) {
|
|
927
|
+
handler(err);
|
|
928
|
+
}
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
send(message) {
|
|
932
|
+
if (this.ws.readyState === 1) {
|
|
933
|
+
this.ws.send(message);
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
onMessage(handler) {
|
|
937
|
+
this.messageHandlers.add(handler);
|
|
938
|
+
return () => {
|
|
939
|
+
this.messageHandlers.delete(handler);
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
onClose(handler) {
|
|
943
|
+
this.closeHandlers.add(handler);
|
|
944
|
+
return () => {
|
|
945
|
+
this.closeHandlers.delete(handler);
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
onError(handler) {
|
|
949
|
+
this.errorHandlers.add(handler);
|
|
950
|
+
return () => {
|
|
951
|
+
this.errorHandlers.delete(handler);
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
close() {
|
|
955
|
+
if (this.heartbeatInterval) {
|
|
956
|
+
clearInterval(this.heartbeatInterval);
|
|
957
|
+
}
|
|
958
|
+
this.ws.close();
|
|
959
|
+
}
|
|
960
|
+
};
|
|
961
|
+
WebSocketServer = class {
|
|
962
|
+
constructor(options = {}) {
|
|
963
|
+
__publicField22(this, "wss", null);
|
|
964
|
+
__publicField22(this, "connections", /* @__PURE__ */ new Set());
|
|
965
|
+
__publicField22(this, "connectionHandlers", /* @__PURE__ */ new Set());
|
|
966
|
+
__publicField22(this, "options");
|
|
967
|
+
__publicField22(this, "attachedToServer", false);
|
|
968
|
+
this.options = options;
|
|
969
|
+
}
|
|
970
|
+
async listen(port, host = "0.0.0.0") {
|
|
971
|
+
if (this.wss) {
|
|
972
|
+
throw new Error("Server already listening");
|
|
973
|
+
}
|
|
974
|
+
if (this.attachedToServer) {
|
|
975
|
+
throw new Error(
|
|
976
|
+
"Cannot listen when attached to existing server. The server should call listen() instead."
|
|
977
|
+
);
|
|
978
|
+
}
|
|
979
|
+
const { WebSocketServer: WSS } = await import("./browser-Bab_T5G7.js").then((n) => n.b);
|
|
980
|
+
this.wss = new WSS({ port, host });
|
|
981
|
+
this.wss.on("connection", (ws) => {
|
|
982
|
+
this.handleConnection(ws);
|
|
983
|
+
});
|
|
984
|
+
logger2.info("WebSocket server listening", { port, host });
|
|
985
|
+
}
|
|
986
|
+
attach(server, path2 = "/ws") {
|
|
987
|
+
if (this.wss) {
|
|
988
|
+
throw new Error("Server already initialized");
|
|
989
|
+
}
|
|
990
|
+
import("./browser-Bab_T5G7.js").then((n) => n.b).then(({ WebSocketServer: WSS }) => {
|
|
991
|
+
this.wss = new WSS({ noServer: true });
|
|
992
|
+
server.on("upgrade", (request, socket, head) => {
|
|
993
|
+
const url = new URL(request.url || "", `http://${request.headers.host}`);
|
|
994
|
+
if (url.pathname === path2) {
|
|
995
|
+
this.wss.handleUpgrade(request, socket, head, (ws) => {
|
|
996
|
+
this.wss.emit("connection", ws, request);
|
|
997
|
+
});
|
|
998
|
+
} else {
|
|
999
|
+
socket.destroy();
|
|
1000
|
+
}
|
|
1001
|
+
});
|
|
1002
|
+
this.wss.on("connection", (ws) => {
|
|
1003
|
+
this.handleConnection(ws);
|
|
1004
|
+
});
|
|
1005
|
+
this.attachedToServer = true;
|
|
1006
|
+
logger2.info("WebSocket attached to existing HTTP server", { path: path2 });
|
|
1007
|
+
});
|
|
1008
|
+
}
|
|
1009
|
+
handleConnection(ws) {
|
|
1010
|
+
const connection = new WebSocketConnection(ws, this.options);
|
|
1011
|
+
this.connections.add(connection);
|
|
1012
|
+
logger2.info("Client connected", {
|
|
1013
|
+
connectionId: connection.id,
|
|
1014
|
+
totalConnections: this.connections.size
|
|
1015
|
+
});
|
|
1016
|
+
connection.onClose(() => {
|
|
1017
|
+
this.connections.delete(connection);
|
|
1018
|
+
logger2.info("Client disconnected", {
|
|
1019
|
+
connectionId: connection.id,
|
|
1020
|
+
totalConnections: this.connections.size
|
|
1021
|
+
});
|
|
1022
|
+
});
|
|
1023
|
+
for (const handler of this.connectionHandlers) {
|
|
1024
|
+
handler(connection);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
onConnection(handler) {
|
|
1028
|
+
this.connectionHandlers.add(handler);
|
|
1029
|
+
return () => {
|
|
1030
|
+
this.connectionHandlers.delete(handler);
|
|
1031
|
+
};
|
|
1032
|
+
}
|
|
1033
|
+
broadcast(message) {
|
|
1034
|
+
for (const connection of this.connections) {
|
|
1035
|
+
connection.send(message);
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
async close() {
|
|
1039
|
+
if (!this.wss) return;
|
|
1040
|
+
for (const connection of this.connections) {
|
|
1041
|
+
connection.close();
|
|
1042
|
+
}
|
|
1043
|
+
this.connections.clear();
|
|
1044
|
+
if (!this.attachedToServer) {
|
|
1045
|
+
await new Promise((resolve) => {
|
|
1046
|
+
this.wss.close(() => resolve());
|
|
1047
|
+
});
|
|
1048
|
+
}
|
|
1049
|
+
this.wss = null;
|
|
1050
|
+
}
|
|
1051
|
+
async dispose() {
|
|
1052
|
+
await this.close();
|
|
1053
|
+
this.connectionHandlers.clear();
|
|
1054
|
+
}
|
|
1055
|
+
};
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
592
1058
|
function isRemoteConfig(config) {
|
|
593
1059
|
return "serverUrl" in config && typeof config.serverUrl === "string";
|
|
594
1060
|
}
|
|
595
1061
|
init_dist();
|
|
596
1062
|
var remoteLogger = createLogger("agentx/RemoteClient");
|
|
597
|
-
var isBrowser = typeof window !== "undefined" && typeof window.WebSocket !== "undefined";
|
|
598
1063
|
async function createRemoteAgentX(serverUrl) {
|
|
599
|
-
const
|
|
600
|
-
const
|
|
1064
|
+
const { createWebSocketClient: createWebSocketClient2 } = await Promise.resolve().then(() => (init_dist2(), dist_exports));
|
|
1065
|
+
const client = await createWebSocketClient2({
|
|
1066
|
+
serverUrl,
|
|
1067
|
+
autoReconnect: true,
|
|
1068
|
+
minReconnectionDelay: 1e3,
|
|
1069
|
+
maxReconnectionDelay: 1e4,
|
|
1070
|
+
connectionTimeout: 4e3,
|
|
1071
|
+
maxRetries: Infinity,
|
|
1072
|
+
debug: false
|
|
1073
|
+
});
|
|
601
1074
|
const handlers2 = /* @__PURE__ */ new Map();
|
|
602
1075
|
const pendingRequests = /* @__PURE__ */ new Map();
|
|
603
|
-
|
|
604
|
-
if (isBrowser) {
|
|
605
|
-
ws.onopen = () => resolve();
|
|
606
|
-
ws.onerror = () => reject(new Error("WebSocket connection failed"));
|
|
607
|
-
} else {
|
|
608
|
-
ws.on("open", () => resolve());
|
|
609
|
-
ws.on("error", (err) => reject(err));
|
|
610
|
-
}
|
|
611
|
-
});
|
|
612
|
-
const handleMessage = (data2) => {
|
|
1076
|
+
client.onMessage((message) => {
|
|
613
1077
|
var _a, _b, _c;
|
|
614
1078
|
try {
|
|
615
|
-
const
|
|
616
|
-
const event = JSON.parse(text2);
|
|
1079
|
+
const event = JSON.parse(message);
|
|
617
1080
|
remoteLogger.info("Received event", {
|
|
618
1081
|
type: event.type,
|
|
619
1082
|
category: event.category,
|
|
@@ -651,12 +1114,13 @@ async function createRemoteAgentX(serverUrl) {
|
|
|
651
1114
|
}
|
|
652
1115
|
} catch {
|
|
653
1116
|
}
|
|
654
|
-
};
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
|
|
1117
|
+
});
|
|
1118
|
+
client.onClose(() => {
|
|
1119
|
+
remoteLogger.warn("WebSocket closed");
|
|
1120
|
+
});
|
|
1121
|
+
client.onError((error) => {
|
|
1122
|
+
remoteLogger.error("WebSocket error", { error: error.message });
|
|
1123
|
+
});
|
|
660
1124
|
function subscribe(type, handler) {
|
|
661
1125
|
if (!handlers2.has(type)) {
|
|
662
1126
|
handlers2.set(type, /* @__PURE__ */ new Set());
|
|
@@ -688,7 +1152,7 @@ async function createRemoteAgentX(serverUrl) {
|
|
|
688
1152
|
category: "request",
|
|
689
1153
|
intent: "request"
|
|
690
1154
|
};
|
|
691
|
-
|
|
1155
|
+
client.send(JSON.stringify(event));
|
|
692
1156
|
});
|
|
693
1157
|
},
|
|
694
1158
|
on(type, handler) {
|
|
@@ -706,7 +1170,7 @@ async function createRemoteAgentX(serverUrl) {
|
|
|
706
1170
|
category: type.toString().endsWith("_response") ? "response" : "request",
|
|
707
1171
|
intent: type.toString().endsWith("_response") ? "result" : "request"
|
|
708
1172
|
};
|
|
709
|
-
|
|
1173
|
+
client.send(JSON.stringify(event));
|
|
710
1174
|
},
|
|
711
1175
|
async listen() {
|
|
712
1176
|
throw new Error("Cannot listen in remote mode");
|
|
@@ -720,11 +1184,7 @@ async function createRemoteAgentX(serverUrl) {
|
|
|
720
1184
|
}
|
|
721
1185
|
pendingRequests.clear();
|
|
722
1186
|
handlers2.clear();
|
|
723
|
-
|
|
724
|
-
ws.close();
|
|
725
|
-
} else {
|
|
726
|
-
ws.close();
|
|
727
|
-
}
|
|
1187
|
+
client.dispose();
|
|
728
1188
|
}
|
|
729
1189
|
};
|
|
730
1190
|
}
|