|
| 1 | +import { onMounted, onUnmounted, ref } from 'vue' |
| 2 | + |
| 3 | +import { useAgentSession } from './useAgentSession' |
| 4 | + |
| 5 | +const DAEMON_WS = 'ws://127.0.0.1:7437/spa' |
| 6 | +const PROTOCOL_VERSION = 1 |
| 7 | +const SESSION_ID = crypto.randomUUID() |
| 8 | + |
| 9 | +type SpaToDaemon = |
| 10 | + | { v: number; type: 'hello'; sessionId: string; title?: string } |
| 11 | + | { |
| 12 | + v: number |
| 13 | + type: 'evalResult' |
| 14 | + sessionId: string |
| 15 | + opId: string |
| 16 | + stdout: string |
| 17 | + stderr?: string |
| 18 | + exitCode: number |
| 19 | + } |
| 20 | + | { v: number; type: 'pair-request'; sessionId: string; code: string } |
| 21 | + | { v: number; type: 'pong'; sessionId: string } |
| 22 | + |
| 23 | +type DaemonToSpa = |
| 24 | + | { v: number; type: 'send'; text: string } |
| 25 | + | { v: number; type: 'eval'; opId: string; script: string } |
| 26 | + | { v: number; type: 'abort' } |
| 27 | + | { v: number; type: 'paired'; code: string } |
| 28 | + | { v: number; type: 'ping' } |
| 29 | + |
| 30 | +// Singleton state — shared across all callers of useLocalBridge() |
| 31 | +const connected = ref(false) |
| 32 | +const activePairCode = ref<string | null>(null) |
| 33 | + |
| 34 | +let ws: WebSocket | null = null |
| 35 | +let refCount = 0 |
| 36 | +let sendFn: ((text: string) => void) | null = null |
| 37 | +let evalFn: |
| 38 | + | (( |
| 39 | + opId: string, |
| 40 | + script: string |
| 41 | + ) => Promise<{ stdout: string; stderr?: string; exitCode: number }>) |
| 42 | + | null = null |
| 43 | +let stopFn: (() => void) | null = null |
| 44 | + |
| 45 | +function sendMsg(msg: SpaToDaemon) { |
| 46 | + if (ws?.readyState === WebSocket.OPEN) ws.send(JSON.stringify(msg)) |
| 47 | +} |
| 48 | + |
| 49 | +function connect( |
| 50 | + onSend: typeof sendFn, |
| 51 | + onEval: typeof evalFn, |
| 52 | + onStop: typeof stopFn |
| 53 | +) { |
| 54 | + sendFn = onSend |
| 55 | + evalFn = onEval |
| 56 | + stopFn = onStop |
| 57 | + if (ws && ws.readyState !== WebSocket.CLOSED) return |
| 58 | + |
| 59 | + ws = new WebSocket(DAEMON_WS) |
| 60 | + |
| 61 | + ws.addEventListener('open', () => { |
| 62 | + connected.value = true |
| 63 | + sendMsg({ |
| 64 | + v: PROTOCOL_VERSION, |
| 65 | + type: 'hello', |
| 66 | + sessionId: SESSION_ID, |
| 67 | + title: 'ComfyUI' |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + ws.addEventListener('message', async (ev) => { |
| 72 | + let msg: DaemonToSpa |
| 73 | + try { |
| 74 | + msg = JSON.parse(ev.data as string) as DaemonToSpa |
| 75 | + } catch { |
| 76 | + return |
| 77 | + } |
| 78 | + if (msg.v !== PROTOCOL_VERSION) return |
| 79 | + |
| 80 | + switch (msg.type) { |
| 81 | + case 'ping': |
| 82 | + sendMsg({ v: PROTOCOL_VERSION, type: 'pong', sessionId: SESSION_ID }) |
| 83 | + break |
| 84 | + case 'send': |
| 85 | + sendFn?.(msg.text) |
| 86 | + break |
| 87 | + case 'eval': { |
| 88 | + const result = (await evalFn?.(msg.opId, msg.script)) ?? { |
| 89 | + stdout: '', |
| 90 | + exitCode: 0 |
| 91 | + } |
| 92 | + sendMsg({ |
| 93 | + v: PROTOCOL_VERSION, |
| 94 | + type: 'evalResult', |
| 95 | + sessionId: SESSION_ID, |
| 96 | + opId: msg.opId, |
| 97 | + stdout: result.stdout, |
| 98 | + stderr: result.stderr, |
| 99 | + exitCode: result.exitCode |
| 100 | + }) |
| 101 | + break |
| 102 | + } |
| 103 | + case 'abort': |
| 104 | + stopFn?.() |
| 105 | + break |
| 106 | + case 'paired': |
| 107 | + if (activePairCode.value === msg.code) activePairCode.value = null |
| 108 | + break |
| 109 | + } |
| 110 | + }) |
| 111 | + |
| 112 | + ws.addEventListener('close', () => { |
| 113 | + connected.value = false |
| 114 | + ws = null |
| 115 | + // Reconnect after 3s if still mounted |
| 116 | + if (refCount > 0) |
| 117 | + setTimeout(() => { |
| 118 | + if (refCount > 0) connect(sendFn, evalFn, stopFn) |
| 119 | + }, 3000) |
| 120 | + }) |
| 121 | + |
| 122 | + ws.addEventListener('error', () => { |
| 123 | + connected.value = false |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +function disconnect() { |
| 128 | + refCount-- |
| 129 | + if (refCount <= 0) { |
| 130 | + ws?.close() |
| 131 | + ws = null |
| 132 | + refCount = 0 |
| 133 | + connected.value = false |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +/** Mount in the root component (AgentRoot) to manage the WS lifecycle. */ |
| 138 | +export function useLocalBridge() { |
| 139 | + const { send, stop, execShell } = useAgentSession() |
| 140 | + |
| 141 | + onMounted(() => { |
| 142 | + refCount++ |
| 143 | + connect( |
| 144 | + (text) => void send(text, []), |
| 145 | + (_opId, script) => execShell(script), |
| 146 | + () => stop() |
| 147 | + ) |
| 148 | + }) |
| 149 | + |
| 150 | + onUnmounted(disconnect) |
| 151 | +} |
| 152 | + |
| 153 | +function requestPair(): void { |
| 154 | + const code = Math.random().toString(36).slice(2, 8).toUpperCase() |
| 155 | + activePairCode.value = code |
| 156 | + sendMsg({ |
| 157 | + v: PROTOCOL_VERSION, |
| 158 | + type: 'pair-request', |
| 159 | + sessionId: SESSION_ID, |
| 160 | + code |
| 161 | + }) |
| 162 | +} |
| 163 | + |
| 164 | +/** Read bridge state from any component — no lifecycle side-effects. */ |
| 165 | +export function useBridgeStatus() { |
| 166 | + return { connected, activePairCode, requestPair } |
| 167 | +} |
0 commit comments