// Remote machines store — tracks connection state for multi-machine support import { listRemoteMachines, addRemoteMachine, removeRemoteMachine, connectRemoteMachine, disconnectRemoteMachine, onRemoteMachineReady, onRemoteMachineDisconnected, onRemoteError, type RemoteMachineConfig, type RemoteMachineInfo, } from '../adapters/remote-bridge'; import { notify } from './notifications.svelte'; export interface Machine extends RemoteMachineInfo {} let machines = $state([]); export function getMachines(): Machine[] { return machines; } export function getMachine(id: string): Machine | undefined { return machines.find(m => m.id === id); } export async function loadMachines(): Promise { try { machines = await listRemoteMachines(); } catch (e) { console.warn('Failed to load remote machines:', e); } } export async function addMachine(config: RemoteMachineConfig): Promise { const id = await addRemoteMachine(config); machines.push({ id, label: config.label, url: config.url, status: 'disconnected', auto_connect: config.auto_connect, }); return id; } export async function removeMachine(id: string): Promise { await removeRemoteMachine(id); machines = machines.filter(m => m.id !== id); } export async function connectMachine(id: string): Promise { const machine = machines.find(m => m.id === id); if (machine) machine.status = 'connecting'; try { await connectRemoteMachine(id); if (machine) machine.status = 'connected'; } catch (e) { if (machine) machine.status = 'error'; throw e; } } export async function disconnectMachine(id: string): Promise { await disconnectRemoteMachine(id); const machine = machines.find(m => m.id === id); if (machine) machine.status = 'disconnected'; } // Initialize event listeners for machine status updates export async function initMachineListeners(): Promise { await onRemoteMachineReady((msg) => { const machine = machines.find(m => m.id === msg.machineId); if (machine) { machine.status = 'connected'; notify('success', `Connected to ${machine.label}`); } }); await onRemoteMachineDisconnected((msg) => { const machine = machines.find(m => m.id === msg.machineId); if (machine) { machine.status = 'disconnected'; notify('warning', `Disconnected from ${machine.label}`); } }); await onRemoteError((msg) => { const machine = machines.find(m => m.id === msg.machineId); if (machine) { machine.status = 'error'; notify('error', `Error from ${machine.label}: ${msg.error}`); } }); }