spb/zyquo-cloud-web Public MIT
Zyquo Cloud Web — every cloud model, one beautiful chat, entirely in your browser.
TypeScript 81.9%
CSS 8.9%
JavaScript 7.5%
Shell 1.1%
HTML 0.6%
1/*2 * backup.ts3 * Zyquo Cloud Web4 *5 * Author: Simon-Pierre Boucher6 * Mail: contact@spboucher.ai7 *8 * Export / import of all local data — the only "backup" mechanism since there9 * is no cloud. Exports may contain API keys ONLY when the user explicitly10 * opts in (the UI warns about this). Clear-data controls live here too.11 */1213import type { Conversation, Settings } from '../types'14import { clearConversations, loadConversations, saveConversation } from './conversations'15import { clearAllKeys, readKeyMap, replaceKeyMap, type KeyMap } from './keys'16import { clearSettings, loadSettings, saveSettings } from './settings'1718const EXPORT_VERSION = 11920export interface ExportedData {21 app: 'zyquo-cloud-web'22 version: number23 exportedAt: string24 settings: Settings25 conversations: Conversation[]26 /** Present only when the user opted in to exporting keys. */27 keys?: KeyMap28}2930export async function exportAll(includeKeys: boolean): Promise<ExportedData> {31 const data: ExportedData = {32 app: 'zyquo-cloud-web',33 version: EXPORT_VERSION,34 exportedAt: new Date().toISOString(),35 settings: loadSettings(),36 conversations: await loadConversations(),37 }38 if (includeKeys) data.keys = readKeyMap()39 return data40}4142export function isExportedData(value: unknown): value is ExportedData {43 return (44 typeof value === 'object' &&45 value !== null &&46 (value as ExportedData).app === 'zyquo-cloud-web' &&47 typeof (value as ExportedData).version === 'number' &&48 Array.isArray((value as ExportedData).conversations)49 )50}5152/** Restores an export. Existing conversations with the same id are overwritten. */53export async function importAll(data: ExportedData): Promise<{ conversations: number; keys: number }> {54 if (data.settings) saveSettings({ ...loadSettings(), ...data.settings })55 for (const conversation of data.conversations) {56 await saveConversation(conversation)57 }58 let keyCount = 059 if (data.keys) {60 replaceKeyMap({ ...readKeyMap(), ...data.keys })61 keyCount = Object.keys(data.keys).length62 }63 return { conversations: data.conversations.length, keys: keyCount }64}6566export async function clearEverything(): Promise<void> {67 await clearConversations()68 clearAllKeys()69 clearSettings()70}71