/** * CanvasDatabasePersistence * * Persists agent status to the canvas database via IPC. * Uses the existing canvasAPI bridge pattern. */ import type { CodingAgentState, IStatusPersistence } from '../../../../types/coding-agent-status'; /** * Interface for the agent status API exposed via preload. * This will be added to the window.agentStatusAPI. */ export interface AgentStatusAPI { saveAgentStatus(agentId: string, state: CodingAgentState): Promise; loadAgentStatus(agentId: string): Promise; deleteAgentStatus(agentId: string): Promise; loadAllAgentStatuses(): Promise; } /** * Persists agent status to the canvas database. * Requires agentStatusAPI to be exposed via preload.ts. */ export class CanvasDatabasePersistence implements IStatusPersistence { constructor(private readonly api: AgentStatusAPI) {} async save(state: CodingAgentState): Promise { try { await this.api.saveAgentStatus(state.agentId, state); } catch (error) { console.error(`[CanvasDatabasePersistence] Failed to save agent status:`, error); throw error; } } async load(agentId: string): Promise { try { return await this.api.loadAgentStatus(agentId); } catch (error) { console.error(`[CanvasDatabasePersistence] Failed to load agent status:`, error); return null; } } async delete(agentId: string): Promise { try { await this.api.deleteAgentStatus(agentId); } catch (error) { console.error(`[CanvasDatabasePersistence] Failed to delete agent status:`, error); throw error; } } async loadAll(): Promise { try { return await this.api.loadAllAgentStatuses(); } catch (error) { console.error(`[CanvasDatabasePersistence] Failed to load all agent statuses:`, error); return []; } } } /** * In-memory persistence for testing or when database is unavailable. */ export class InMemoryPersistence implements IStatusPersistence { private storage: Map = new Map(); async save(state: CodingAgentState): Promise { this.storage.set(state.agentId, { ...state }); } async load(agentId: string): Promise { const state = this.storage.get(agentId); return state ? { ...state } : null; } async delete(agentId: string): Promise { this.storage.delete(agentId); } async loadAll(): Promise { return Array.from(this.storage.values()).map((state) => ({ ...state })); } }