AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(builder): Automatically (re)connect websocket when sending message (#7739)
Reinier van der Leer55d32f03243a
diff --git a/rnd/autogpt_builder/src/lib/autogpt-server-api/client.ts b/rnd/autogpt_builder/src/lib/autogpt-server-api/client.tsindex dd2ee46267..157a4a3e3b 100644--- a/rnd/autogpt_builder/src/lib/autogpt-server-api/client.ts+++ b/rnd/autogpt_builder/src/lib/autogpt-server-api/client.ts@@ -12,8 +12,9 @@ import {export default class AutoGPTServerAPI {private baseUrl: string;private wsUrl: string;- private socket: WebSocket | null = null;- private messageHandlers: { [key: string]: (data: any) => void } = {};+ private webSocket: WebSocket | null = null;+ private wsConnecting: Promise<void> | null = null;+ private wsMessageHandlers: { [key: string]: (data: any) => void } = {};private supabaseClient = createClient();constructor(@@ -174,34 +175,34 @@ export default class AutoGPTServerAPI {}async connectWebSocket(): Promise<void> {- return new Promise(async (resolve, reject) => {+ this.wsConnecting ??= new Promise(async (resolve, reject) => {try {const token =(await this.supabaseClient?.auth.getSession())?.data.session?.access_token || "";const wsUrlWithToken = `${this.wsUrl}?token=${token}`;- this.socket = new WebSocket(wsUrlWithToken);+ this.webSocket = new WebSocket(wsUrlWithToken);- this.socket.onopen = () => {+ this.webSocket.onopen = () => {console.log("WebSocket connection established");resolve();};- this.socket.onclose = (event) => {+ this.webSocket.onclose = (event) => {console.log("WebSocket connection closed", event);- this.socket = null;+ this.webSocket = null;};- this.socket.onerror = (error) => {+ this.webSocket.onerror = (error) => {console.error("WebSocket error:", error);reject(error);};- this.socket.onmessage = (event) => {+ this.webSocket.onmessage = (event) => {const message = JSON.parse(event.data);- if (this.messageHandlers[message.method]) {- this.messageHandlers[message.method](message.data);+ if (this.wsMessageHandlers[message.method]) {+ this.wsMessageHandlers[message.method](message.data);}};} catch (error) {@@ -209,11 +210,12 @@ export default class AutoGPTServerAPI {reject(error);}});+ return this.wsConnecting;}disconnectWebSocket() {- if (this.socket && this.socket.readyState === WebSocket.OPEN) {- this.socket.close();+ if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) {+ this.webSocket.close();}}@@ -221,10 +223,12 @@ export default class AutoGPTServerAPI {method: M,data: WebsocketMessageTypeMap[M],) {- if (this.socket && this.socket.readyState === WebSocket.OPEN) {- this.socket.send(JSON.stringify({ method, data }));+ if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) {+ this.webSocket.send(JSON.stringify({ method, data }));} else {- console.error("WebSocket is not connected");+ this.connectWebSocket().then(() =>+ this.sendWebSocketMessage(method, data),+ );}}@@ -232,7 +236,7 @@ export default class AutoGPTServerAPI {method: M,handler: (data: WebsocketMessageTypeMap[M]) => void,) {- this.messageHandlers[method] = handler;+ this.wsMessageHandlers[method] = handler;}subscribeToExecution(graphId: string) {