import type { PersistedSession, PersistenceService } from "../app/types"; import { PERSISTENCE_VERSION } from "../app/types"; const STORAGE_KEY = "private-voice-agent/session"; interface StorageLike { getItem(key: string): string | null; setItem(key: string, value: string): void; removeItem(key: string): void; } export class LocalPersistenceService implements PersistenceService { #storage: StorageLike | null; constructor(storage: StorageLike | null = typeof localStorage !== "undefined" ? localStorage : null) { this.#storage = storage; } load(): PersistedSession | null { if (!this.#storage) { return null; } const raw = this.#storage.getItem(STORAGE_KEY); if (!raw) { return null; } try { const parsed = JSON.parse(raw) as PersistedSession; if (parsed.version !== PERSISTENCE_VERSION) { this.clear(); return null; } return parsed; } catch { this.clear(); return null; } } save(value: PersistedSession): void { if (!this.#storage) { return; } this.#storage.setItem(STORAGE_KEY, JSON.stringify(value)); } clear(): void { this.#storage?.removeItem(STORAGE_KEY); } }