Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
refactor(frontend): replace HTTP API with pure WebSocket RPC
Naiyuan Qing150c87a4966c
diff --git a/apps/web/app/layout.tsx b/apps/web/app/layout.tsxindex 716a2e724f..8d468eb491 100644--- a/apps/web/app/layout.tsx+++ b/apps/web/app/layout.tsx@@ -12,7 +12,6 @@ import { Toaster } from "@multica/ui/components/ui/sonner";import { HubSidebar } from "@multica/ui/components/hub-sidebar";setConfig({- consoleUrl: process.env.NEXT_PUBLIC_CONSOLE_URL ?? "http://localhost:4000",gatewayUrl: process.env.NEXT_PUBLIC_GATEWAY_URL ?? "http://localhost:3000",});diff --git a/packages/store/src/gateway.ts b/packages/store/src/gateway.tsindex da5dd0d9eb..ae7baff0b7 100644--- a/packages/store/src/gateway.ts+++ b/packages/store/src/gateway.ts@@ -5,26 +5,31 @@ import { useMessagesStore } from "./messages"interface GatewayState {connectionState: ConnectionState+ hubId: string | nulllastError: SendErrorResponse | null}interface GatewayActions {- connect: (deviceId: string) => void+ connect: (deviceId: string, hubId: string) => voiddisconnect: () => voidsend: (to: string, action: string, payload: unknown) => void+ request: <T = unknown>(method: string, params?: unknown) => Promise<T>}export type GatewayStore = GatewayState & GatewayActionslet client: GatewayClient | null = null-export const useGatewayStore = create<GatewayStore>()((set) => ({+export const useGatewayStore = create<GatewayStore>()((set, get) => ({connectionState: "disconnected",+ hubId: null,lastError: null,- connect: (deviceId) => {+ connect: (deviceId, hubId) => {if (client) return+ set({ hubId })+client = new GatewayClient({url: getGatewayUrl(),deviceId,@@ -47,11 +52,19 @@ export const useGatewayStore = create<GatewayStore>()((set) => ({client.disconnect()client = null}- set({ connectionState: "disconnected" })+ set({ connectionState: "disconnected", hubId: null })},send: (to, action, payload) => {if (!client?.isRegistered) returnclient.send(to, action, payload)},++ request: <T = unknown>(method: string, params?: unknown): Promise<T> => {+ const { hubId } = get()+ if (!client?.isRegistered || !hubId) {+ return Promise.reject(new Error("Not connected"))+ }+ return client.request<T>(hubId, method, params)+ },}))diff --git a/packages/store/src/hub-init.ts b/packages/store/src/hub-init.tsindex bf06c56af4..6d7ed7029f 100644--- a/packages/store/src/hub-init.ts+++ b/packages/store/src/hub-init.ts@@ -2,29 +2,21 @@import { useEffect } from "react"import { useHubStore } from "./hub"-import { useDeviceId } from "./device-id"import { useGatewayStore } from "./gateway"export function useHubInit() {+ const gwState = useGatewayStore((s) => s.connectionState)const fetchHub = useHubStore((s) => s.fetchHub)- const status = useHubStore((s) => s.status)const fetchAgents = useHubStore((s) => s.fetchAgents)- const deviceId = useDeviceId()- useEffect(() => { fetchHub() }, [fetchHub])+ // Once WS is registered, fetch hub info and agents via RPCuseEffect(() => {- if (status === "connected") fetchAgents()- }, [status, fetchAgents])- useEffect(() => {- const id = setInterval(fetchHub, 30_000)- return () => clearInterval(id)- }, [fetchHub])-- // Connect gateway when hub is ready and deviceId is available- useEffect(() => {- if (status === "connected" && deviceId) {- useGatewayStore.getState().connect(deviceId)- return () => { useGatewayStore.getState().disconnect() }+ if (gwState === "registered") {+ fetchHub()+ fetchAgents()+ }+ if (gwState === "disconnected") {+ useHubStore.setState({ status: "idle", hub: null, agents: [], activeAgentId: null })}- }, [status, deviceId])+ }, [gwState, fetchHub, fetchAgents])}diff --git a/packages/store/src/hub.ts b/packages/store/src/hub.tsindex ed5cc41fab..b5101eec9c 100644--- a/packages/store/src/hub.ts+++ b/packages/store/src/hub.ts@@ -1,13 +1,14 @@import { create } from "zustand"-import { consoleApi } from "@multica/fetch"import { toast } from "sonner"+import type {+ GetHubInfoResult,+ ListAgentsResult,+ CreateAgentResult,+ DeleteAgentResult,+} from "@multica/sdk"+import { useGatewayStore } from "./gateway"-export interface HubInfo {- hubId: string- url: string- connectionState: string- agentCount: number-}+export type HubInfo = GetHubInfoResultexport interface Agent {id: string@@ -44,11 +45,9 @@ export const useHubStore = create<HubStore>()((set, get) => ({fetchHub: async () => {set({ status: "loading" })try {- const data = await consoleApi.get<HubInfo>("/api/hub")- set({- hub: data,- status: data.connectionState === "registered" ? "connected" : "error",- })+ const { request } = useGatewayStore.getState()+ const data = await request<GetHubInfoResult>("getHubInfo")+ set({ hub: data, status: "connected" })} catch {set({ status: "error", hub: null })}@@ -56,8 +55,9 @@ export const useHubStore = create<HubStore>()((set, get) => ({fetchAgents: async () => {try {- const data = await consoleApi.get<Agent[]>("/api/agents")- set({ agents: data })+ const { request } = useGatewayStore.getState()+ const data = await request<ListAgentsResult>("listAgents")+ set({ agents: data.agents })} catch (e) {toast.error("Failed to fetch agents")console.error(e)@@ -66,7 +66,8 @@ export const useHubStore = create<HubStore>()((set, get) => ({createAgent: async (options?) => {try {- const data = await consoleApi.post<{ id: string }>("/api/agents", options)+ const { request } = useGatewayStore.getState()+ const data = await request<CreateAgentResult>("createAgent", options)await get().fetchAgents()if (data.id) set({ activeAgentId: data.id })} catch (e) {@@ -78,7 +79,8 @@ export const useHubStore = create<HubStore>()((set, get) => ({deleteAgent: async (id) => {if (get().activeAgentId === id) set({ activeAgentId: null })try {- await consoleApi.delete("/api/agents/" + id)+ const { request } = useGatewayStore.getState()+ await request<DeleteAgentResult>("deleteAgent", { id })await get().fetchAgents()} catch (e) {toast.error("Failed to delete agent")diff --git a/packages/ui/src/components/chat.tsx b/packages/ui/src/components/chat.tsxindex a510ca3020..f7fa8910e1 100644--- a/packages/ui/src/components/chat.tsx+++ b/packages/ui/src/components/chat.tsx@@ -29,11 +29,11 @@ export function Chat() {const filtered = useMemo(() => messages.filter(m => m.agentId === activeAgentId), [messages, activeAgentId])const handleSend = useCallback((text: string) => {- const hub = useHubStore.getState().hub+ const { hubId } = useGatewayStore.getState()const agentId = useHubStore.getState().activeAgentId- if (!hub?.hubId || !agentId) return+ if (!hubId || !agentId) returnuseMessagesStore.getState().addUserMessage(text, agentId)- useGatewayStore.getState().send(hub.hubId, "message", { agentId, content: text })+ useGatewayStore.getState().send(hubId, "message", { agentId, content: text })}, [])const canSend = gwState === "registered" && !!activeAgentIddiff --git a/packages/ui/src/components/hub-sidebar.tsx b/packages/ui/src/components/hub-sidebar.tsxindex fdd51596b8..8897ebc77f 100644--- a/packages/ui/src/components/hub-sidebar.tsx+++ b/packages/ui/src/components/hub-sidebar.tsx@@ -1,5 +1,6 @@"use client"+import { useState } from "react"import {SidebarGroup,SidebarGroupLabel,@@ -9,9 +10,10 @@ import {SidebarMenuItem,} from "@multica/ui/components/ui/sidebar"import { Button } from "@multica/ui/components/ui/button"+import { Input } from "@multica/ui/components/ui/input"import { HugeiconsIcon } from "@hugeicons/react"import { PlusSignIcon, Delete02Icon } from "@hugeicons/core-free-icons"-import { useHubStore } from "@multica/store"+import { useHubStore, useDeviceId, useGatewayStore } from "@multica/store"import { useHubInit } from "@multica/store"import { Skeleton } from "@multica/ui/components/ui/skeleton"@@ -36,38 +38,75 @@ export function HubSidebar() {const hub = useHubStore((s) => s.hub)const agents = useHubStore((s) => s.agents)const activeAgentId = useHubStore((s) => s.activeAgentId)- const fetchHub = useHubStore((s) => s.fetchHub)const createAgent = useHubStore((s) => s.createAgent)const deleteAgent = useHubStore((s) => s.deleteAgent)const setActiveAgentId = useHubStore((s) => s.setActiveAgentId)+ const gwState = useGatewayStore((s) => s.connectionState)+ const connect = useGatewayStore((s) => s.connect)+ const disconnect = useGatewayStore((s) => s.disconnect)+ const deviceId = useDeviceId()++ const [hubIdInput, setHubIdInput] = useState("")+ const isDisconnected = gwState === "disconnected"+ const isConnecting = gwState === "connecting" || gwState === "connected"++ const handleConnect = () => {+ const id = hubIdInput.trim()+ if (!id || !deviceId) return+ connect(deviceId, id)+ }+return (<><SidebarGroup><SidebarGroupLabel>Hub</SidebarGroupLabel><SidebarGroupContent>- <div className="flex items-center gap-2 px-2 py-1 text-sm">- <span className={`size-2 rounded-full shrink-0 ${STATUS_DOT[status]}`} />- <span className="text-muted-foreground/70 text-xs">{STATUS_LABEL[status]}</span>- </div>- {status === "connected" && hub ? (- <div className="px-2 text-xs text-muted-foreground/50 font-mono truncate">- {hub.hubId}- </div>- ) : (status === "idle" || status === "loading") ? (- <Skeleton className="mx-2 h-3.5 w-32" />- ) : null}- {status === "error" && (- <div className="px-2 pt-1">- <Button variant="outline" size="sm" onClick={fetchHub} className="w-full text-xs">- Retry+ {isDisconnected ? (+ <div className="px-2 space-y-2 py-1">+ <Input+ value={hubIdInput}+ onChange={(e) => setHubIdInput(e.target.value)}+ placeholder="Enter Hub ID..."+ className="h-7 text-xs font-mono"+ onKeyDown={(e) => e.key === "Enter" && handleConnect()}+ />+ <Button+ variant="outline"+ size="sm"+ onClick={handleConnect}+ disabled={!hubIdInput.trim() || !deviceId}+ className="w-full text-xs"+ >+ Connect</Button></div>+ ) : (+ <>+ <div className="flex items-center gap-2 px-2 py-1 text-sm">+ <span className={`size-2 rounded-full shrink-0 ${STATUS_DOT[status] ?? STATUS_DOT.idle}`} />+ <span className="text-muted-foreground/70 text-xs">+ {isConnecting ? "Connecting..." : STATUS_LABEL[status]}+ </span>+ </div>+ {status === "connected" && hub ? (+ <div className="px-2 text-xs text-muted-foreground/50 font-mono truncate">+ {hub.hubId}+ </div>+ ) : (status === "idle" || status === "loading") ? (+ <Skeleton className="mx-2 h-3.5 w-32" />+ ) : null}+ <div className="px-2 pt-1">+ <Button variant="outline" size="sm" onClick={disconnect} className="w-full text-xs">+ Disconnect+ </Button>+ </div>+ </>)}</SidebarGroupContent></SidebarGroup>- {(status === "idle" || status === "loading") && (+ {isConnecting && (<SidebarGroup><SidebarGroupLabel>Agents</SidebarGroupLabel><SidebarGroupContent>