Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

feat(web): WebSocket hooks from spec-plans

Genereae83b504850
diff --git a/src/web/src/hooks/use-mobile.ts b/src/web/src/hooks/use-mobile.ts
new file mode 100644
index 000000000..2b0fe1dfe
--- /dev/null
+++ b/src/web/src/hooks/use-mobile.ts
@@ -0,0 +1,19 @@
+import * as React from "react"
+
+const MOBILE_BREAKPOINT = 768
+
+export function useIsMobile() {
+ const [isMobile, setIsMobile] = React.useState<boolean | undefined>(undefined)
+
+ React.useEffect(() => {
+ const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
+ const onChange = () => {
+ setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
+ }
+ mql.addEventListener("change", onChange)
+ setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
+ return () => mql.removeEventListener("change", onChange)
+ }, [])
+
+ return !!isMobile
+}
diff --git a/src/web/src/lib/use-user-ws.ts b/src/web/src/lib/use-user-ws.ts
new file mode 100644
index 000000000..246076710
--- /dev/null
+++ b/src/web/src/lib/use-user-ws.ts
@@ -0,0 +1,68 @@
+"use client"
+import { useEffect, useRef, useCallback } from "react"
+import type { WsMessage } from "@alook/shared"
+
+const isDev = process.env.NODE_ENV === "development"
+const WS_DO_PORT = 8789
+
+export function useUserWs(onMessage: (msg: WsMessage) => void) {
+ const wsRef = useRef<WebSocket | null>(null)
+ const reconnectDelay = useRef(1000)
+
+ const connect = useCallback(async () => {
+ let url: string
+ let authToken: string | null = null
+
+ if (isDev) {
+ // Dev: connect directly to ws-do worker, authenticate via auth message
+ try {
+ const res = await fetch("/api/ws/token")
+ if (!res.ok) return
+ const { userId, token } = await res.json() as { userId: string; token: string }
+ url = `ws://localhost:${WS_DO_PORT}/?userId=${userId}`
+ authToken = token
+ } catch {
+ return
+ }
+ } else {
+ // Production: go through Next.js API route (service binding handles WS upgrade)
+ url = `${location.origin.replace("http", "ws")}/api/ws/user`
+ }
+
+ let ws: WebSocket
+ try {
+ ws = new WebSocket(url)
+ } catch {
+ return
+ }
+ wsRef.current = ws
+
+ ws.onopen = () => {
+ reconnectDelay.current = 1000
+ if (authToken) {
+ ws.send(JSON.stringify({ type: "auth", token: authToken }))
+ }
+ }
+
+ ws.onmessage = (e) => {
+ try {
+ const msg = JSON.parse(e.data)
+ if (msg.type === "auth.ok") return
+ onMessage(msg as WsMessage)
+ } catch {}
+ }
+
+ ws.onerror = () => {}
+
+ ws.onclose = () => {
+ const delay = Math.min(reconnectDelay.current, 30_000)
+ reconnectDelay.current = Math.min(delay * 2, 30_000)
+ setTimeout(connect, delay + Math.random() * 500)
+ }
+ }, [onMessage])
+
+ useEffect(() => {
+ connect()
+ return () => { wsRef.current?.close() }
+ }, [connect])
+}
diff --git a/src/web/src/lib/use-ws.ts b/src/web/src/lib/use-ws.ts
new file mode 100644
index 000000000..8329a549c
--- /dev/null
+++ b/src/web/src/lib/use-ws.ts
@@ -0,0 +1,68 @@
+"use client"
+import { useEffect, useRef, useCallback } from "react"
+import type { WsMessage } from "@alook/shared"
+
+const isDev = process.env.NODE_ENV === "development"
+const WS_DO_PORT = 8789
+
+export function useAgentWs(agentId: string, onMessage: (msg: WsMessage) => void) {
+ const wsRef = useRef<WebSocket | null>(null)
+ const reconnectDelay = useRef(1000)
+
+ const connect = useCallback(async () => {
+ let url: string
+ let authToken: string | null = null
+
+ if (isDev) {
+ // Dev: connect directly to ws-do worker, authenticate via auth message
+ try {
+ const res = await fetch("/api/ws/token")
+ if (!res.ok) return
+ const { token } = await res.json() as { token: string }
+ url = `ws://localhost:${WS_DO_PORT}/?agentId=${agentId}`
+ authToken = token
+ } catch {
+ return
+ }
+ } else {
+ // Production: go through Next.js API route (service binding handles WS upgrade)
+ url = `${location.origin.replace("http", "ws")}/api/ws?agentId=${agentId}`
+ }
+
+ let ws: WebSocket
+ try {
+ ws = new WebSocket(url)
+ } catch {
+ return
+ }
+ wsRef.current = ws
+
+ ws.onopen = () => {
+ reconnectDelay.current = 1000
+ if (authToken) {
+ ws.send(JSON.stringify({ type: "auth", token: authToken }))
+ }
+ }
+
+ ws.onmessage = (e) => {
+ try {
+ const msg = JSON.parse(e.data)
+ if (msg.type === "auth.ok") return
+ onMessage(msg as WsMessage)
+ } catch {}
+ }
+
+ ws.onerror = () => {}
+
+ ws.onclose = () => {
+ const delay = Math.min(reconnectDelay.current, 30_000)
+ reconnectDelay.current = Math.min(delay * 2, 30_000)
+ setTimeout(connect, delay + Math.random() * 500)
+ }
+ }, [agentId, onMessage])
+
+ useEffect(() => {
+ connect()
+ return () => { wsRef.current?.close() }
+ }, [connect])
+}