|
| 1 | +import { setTimeout } from "node:timers/promises"; |
| 2 | +import type { HubInboundMessage } from "./types.js"; |
| 3 | + |
| 4 | +export type WsHubOptions = { |
| 5 | + url: string; |
| 6 | + agentId: string; |
| 7 | + secret: string; |
| 8 | + abortSignal?: AbortSignal; |
| 9 | + onMessages: (messages: HubInboundMessage[]) => void | Promise<void>; |
| 10 | + onError?: (error: Error) => void; |
| 11 | + onConnected?: () => void; |
| 12 | +}; |
| 13 | + |
| 14 | +function httpToWs(url: string): string { |
| 15 | + return url.replace(/^http/, "ws"); |
| 16 | +} |
| 17 | + |
| 18 | +function computeReconnectDelay(attempt: number): number { |
| 19 | + const initialMs = 1_000; |
| 20 | + const maxMs = 60_000; |
| 21 | + const base = Math.min(initialMs * 2 ** attempt, maxMs); |
| 22 | + const jitter = base * 0.3 * (Math.random() * 2 - 1); |
| 23 | + return Math.max(initialMs, Math.round(base + jitter)); |
| 24 | +} |
| 25 | + |
| 26 | +export async function connectHubWebSocket(opts: WsHubOptions): Promise<void> { |
| 27 | + const { url, agentId, secret, abortSignal, onMessages, onError, onConnected } = opts; |
| 28 | + let attempt = 0; |
| 29 | + |
| 30 | + while (!abortSignal?.aborted) { |
| 31 | + try { |
| 32 | + const wsUrl = `${httpToWs(url)}/agents/${encodeURIComponent(agentId)}/ws`; |
| 33 | + const ws = new WebSocket(wsUrl); |
| 34 | + |
| 35 | + await new Promise<void>((resolve, reject) => { |
| 36 | + const cleanup = () => { |
| 37 | + ws.removeEventListener("open", onOpen); |
| 38 | + ws.removeEventListener("error", onErr); |
| 39 | + ws.removeEventListener("close", onClose); |
| 40 | + ws.removeEventListener("message", onMsg); |
| 41 | + }; |
| 42 | + |
| 43 | + const onOpen = () => { |
| 44 | + ws.send(JSON.stringify({ secret })); |
| 45 | + }; |
| 46 | + |
| 47 | + const onErr = () => { |
| 48 | + cleanup(); |
| 49 | + reject(new Error("WebSocket error")); |
| 50 | + }; |
| 51 | + |
| 52 | + const onClose = () => { |
| 53 | + cleanup(); |
| 54 | + resolve(); |
| 55 | + }; |
| 56 | + |
| 57 | + const onMsg = async (ev: MessageEvent) => { |
| 58 | + try { |
| 59 | + const data = JSON.parse(String(ev.data)); |
| 60 | + |
| 61 | + if (data.type === "auth" && data.ok) { |
| 62 | + attempt = 0; |
| 63 | + onConnected?.(); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (!data.ok && data.error) { |
| 68 | + cleanup(); |
| 69 | + reject(new Error(`Hub auth failed: ${data.error}`)); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if (data.type === "message" && data.data) { |
| 74 | + const msg: HubInboundMessage = { |
| 75 | + messageId: data.data.messageId || `hub-${crypto.randomUUID()}`, |
| 76 | + from: data.data.from, |
| 77 | + text: data.data.text, |
| 78 | + timestamp: |
| 79 | + typeof data.data.timestamp === "number" |
| 80 | + ? data.data.timestamp |
| 81 | + : typeof data.data.timestamp === "string" |
| 82 | + ? new Date(data.data.timestamp).getTime() || Date.now() |
| 83 | + : Date.now(), |
| 84 | + }; |
| 85 | + await onMessages([msg]); |
| 86 | + } |
| 87 | + } catch (err) { |
| 88 | + onError?.(err instanceof Error ? err : new Error(String(err))); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + ws.addEventListener("open", onOpen); |
| 93 | + ws.addEventListener("error", onErr); |
| 94 | + ws.addEventListener("close", onClose); |
| 95 | + ws.addEventListener("message", onMsg); |
| 96 | + |
| 97 | + const pingInterval = setInterval(() => { |
| 98 | + if (ws.readyState === WebSocket.OPEN) { |
| 99 | + ws.send(JSON.stringify({ type: "ping" })); |
| 100 | + } |
| 101 | + }, 25_000); |
| 102 | + |
| 103 | + abortSignal?.addEventListener( |
| 104 | + "abort", |
| 105 | + () => { |
| 106 | + clearInterval(pingInterval); |
| 107 | + cleanup(); |
| 108 | + ws.close(); |
| 109 | + resolve(); |
| 110 | + }, |
| 111 | + { once: true }, |
| 112 | + ); |
| 113 | + |
| 114 | + ws.addEventListener("close", () => clearInterval(pingInterval), { once: true }); |
| 115 | + }); |
| 116 | + } catch (err) { |
| 117 | + if (abortSignal?.aborted) break; |
| 118 | + const error = err instanceof Error ? err : new Error(String(err)); |
| 119 | + onError?.(error); |
| 120 | + } |
| 121 | + |
| 122 | + if (abortSignal?.aborted) break; |
| 123 | + const delay = computeReconnectDelay(attempt); |
| 124 | + attempt++; |
| 125 | + try { |
| 126 | + await setTimeout(delay, undefined, { signal: abortSignal }); |
| 127 | + } catch { |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments