/* * backup.ts * Zyquo Cloud Web * * Author: Simon-Pierre Boucher * Mail: contact@spboucher.ai * * Export / import of all local data — the only "backup" mechanism since there * is no cloud. Exports may contain API keys ONLY when the user explicitly * opts in (the UI warns about this). Clear-data controls live here too. */ import type { Conversation, Settings } from '../types' import { clearConversations, loadConversations, saveConversation } from './conversations' import { clearAllKeys, readKeyMap, replaceKeyMap, type KeyMap } from './keys' import { clearSettings, loadSettings, saveSettings } from './settings' const EXPORT_VERSION = 1 export interface ExportedData { app: 'zyquo-cloud-web' version: number exportedAt: string settings: Settings conversations: Conversation[] /** Present only when the user opted in to exporting keys. */ keys?: KeyMap } export async function exportAll(includeKeys: boolean): Promise { const data: ExportedData = { app: 'zyquo-cloud-web', version: EXPORT_VERSION, exportedAt: new Date().toISOString(), settings: loadSettings(), conversations: await loadConversations(), } if (includeKeys) data.keys = readKeyMap() return data } export function isExportedData(value: unknown): value is ExportedData { return ( typeof value === 'object' && value !== null && (value as ExportedData).app === 'zyquo-cloud-web' && typeof (value as ExportedData).version === 'number' && Array.isArray((value as ExportedData).conversations) ) } /** Restores an export. Existing conversations with the same id are overwritten. */ export async function importAll(data: ExportedData): Promise<{ conversations: number; keys: number }> { if (data.settings) saveSettings({ ...loadSettings(), ...data.settings }) for (const conversation of data.conversations) { await saveConversation(conversation) } let keyCount = 0 if (data.keys) { replaceKeyMap({ ...readKeyMap(), ...data.keys }) keyCount = Object.keys(data.keys).length } return { conversations: data.conversations.length, keys: keyCount } } export async function clearEverything(): Promise { await clearConversations() clearAllKeys() clearSettings() }