|
| 1 | +import { loadChatHistory } from "./controllers/chat"; |
| 2 | +import { loadNodes } from "./controllers/nodes"; |
| 3 | +import type { GatewayEventFrame, GatewayHelloOk } from "./gateway"; |
| 4 | +import { GatewayBrowserClient } from "./gateway"; |
| 5 | +import type { EventLogEntry } from "./app-events"; |
| 6 | +import type { PresenceEntry, HealthSnapshot, StatusSummary } from "./types"; |
| 7 | +import type { Tab } from "./navigation"; |
| 8 | +import { handleAgentEvent, resetToolStream, type AgentEventPayload } from "./app-tool-stream"; |
| 9 | +import { flushChatQueueForEvent } from "./app-chat"; |
| 10 | +import { loadCron, refreshActiveTab, setLastActiveSessionKey } from "./app-settings"; |
| 11 | +import { handleChatEvent, type ChatEventPayload } from "./controllers/chat"; |
| 12 | +import type { ClawdbotApp } from "./app"; |
| 13 | + |
| 14 | +type GatewayHost = { |
| 15 | + settings: { gatewayUrl: string; token: string }; |
| 16 | + password: string; |
| 17 | + client: GatewayBrowserClient | null; |
| 18 | + connected: boolean; |
| 19 | + hello: GatewayHelloOk | null; |
| 20 | + lastError: string | null; |
| 21 | + eventLogBuffer: EventLogEntry[]; |
| 22 | + eventLog: EventLogEntry[]; |
| 23 | + tab: Tab; |
| 24 | + presenceEntries: PresenceEntry[]; |
| 25 | + presenceError: string | null; |
| 26 | + presenceStatus: StatusSummary | null; |
| 27 | + debugHealth: HealthSnapshot | null; |
| 28 | + sessionKey: string; |
| 29 | + chatRunId: string | null; |
| 30 | +}; |
| 31 | + |
| 32 | +export function connectGateway(host: GatewayHost) { |
| 33 | + host.lastError = null; |
| 34 | + host.hello = null; |
| 35 | + host.connected = false; |
| 36 | + |
| 37 | + host.client?.stop(); |
| 38 | + host.client = new GatewayBrowserClient({ |
| 39 | + url: host.settings.gatewayUrl, |
| 40 | + token: host.settings.token.trim() ? host.settings.token : undefined, |
| 41 | + password: host.password.trim() ? host.password : undefined, |
| 42 | + clientName: "clawdbot-control-ui", |
| 43 | + mode: "webchat", |
| 44 | + onHello: (hello) => { |
| 45 | + host.connected = true; |
| 46 | + host.hello = hello; |
| 47 | + applySnapshot(host, hello); |
| 48 | + void loadNodes(host as unknown as ClawdbotApp, { quiet: true }); |
| 49 | + void refreshActiveTab(host as unknown as Parameters<typeof refreshActiveTab>[0]); |
| 50 | + }, |
| 51 | + onClose: ({ code, reason }) => { |
| 52 | + host.connected = false; |
| 53 | + host.lastError = `disconnected (${code}): ${reason || "no reason"}`; |
| 54 | + }, |
| 55 | + onEvent: (evt) => handleGatewayEvent(host, evt), |
| 56 | + onGap: ({ expected, received }) => { |
| 57 | + host.lastError = `event gap detected (expected seq ${expected}, got ${received}); refresh recommended`; |
| 58 | + }, |
| 59 | + }); |
| 60 | + host.client.start(); |
| 61 | +} |
| 62 | + |
| 63 | +export function handleGatewayEvent(host: GatewayHost, evt: GatewayEventFrame) { |
| 64 | + host.eventLogBuffer = [ |
| 65 | + { ts: Date.now(), event: evt.event, payload: evt.payload }, |
| 66 | + ...host.eventLogBuffer, |
| 67 | + ].slice(0, 250); |
| 68 | + if (host.tab === "debug") { |
| 69 | + host.eventLog = host.eventLogBuffer; |
| 70 | + } |
| 71 | + |
| 72 | + if (evt.event === "agent") { |
| 73 | + handleAgentEvent( |
| 74 | + host as unknown as Parameters<typeof handleAgentEvent>[0], |
| 75 | + evt.payload as AgentEventPayload | undefined, |
| 76 | + ); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + if (evt.event === "chat") { |
| 81 | + const payload = evt.payload as ChatEventPayload | undefined; |
| 82 | + if (payload?.sessionKey) { |
| 83 | + setLastActiveSessionKey( |
| 84 | + host as unknown as Parameters<typeof setLastActiveSessionKey>[0], |
| 85 | + payload.sessionKey, |
| 86 | + ); |
| 87 | + } |
| 88 | + const state = handleChatEvent(host as unknown as ClawdbotApp, payload); |
| 89 | + if (state === "final" || state === "error" || state === "aborted") { |
| 90 | + resetToolStream(host as unknown as Parameters<typeof resetToolStream>[0]); |
| 91 | + void flushChatQueueForEvent( |
| 92 | + host as unknown as Parameters<typeof flushChatQueueForEvent>[0], |
| 93 | + ); |
| 94 | + } |
| 95 | + if (state === "final") void loadChatHistory(host as unknown as ClawdbotApp); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (evt.event === "presence") { |
| 100 | + const payload = evt.payload as { presence?: PresenceEntry[] } | undefined; |
| 101 | + if (payload?.presence && Array.isArray(payload.presence)) { |
| 102 | + host.presenceEntries = payload.presence; |
| 103 | + host.presenceError = null; |
| 104 | + host.presenceStatus = null; |
| 105 | + } |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (evt.event === "cron" && host.tab === "cron") { |
| 110 | + void loadCron(host as unknown as Parameters<typeof loadCron>[0]); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +export function applySnapshot(host: GatewayHost, hello: GatewayHelloOk) { |
| 115 | + const snapshot = hello.snapshot as |
| 116 | + | { presence?: PresenceEntry[]; health?: HealthSnapshot } |
| 117 | + | undefined; |
| 118 | + if (snapshot?.presence && Array.isArray(snapshot.presence)) { |
| 119 | + host.presenceEntries = snapshot.presence; |
| 120 | + } |
| 121 | + if (snapshot?.health) { |
| 122 | + host.debugHealth = snapshot.health; |
| 123 | + } |
| 124 | +} |
0 commit comments