// Per-user application directories, Tenet-style: everything lives under one // app-data root keyed by the bundle identifier. ZIRAN_DATA_DIR overrides the // root for tests and throwaway profiles. import { join } from '@std/path'; const IDENTIFIER = 'com.berjon.ziran'; export function appDataDir(): string { const override = Deno.env.get('ZIRAN_DATA_DIR'); if (override) return override; const home = Deno.env.get('HOME') ?? '.'; switch (Deno.build.os) { case 'darwin': return join(home, 'Library', 'Application Support', IDENTIFIER); case 'windows': return join(Deno.env.get('APPDATA') ?? join(home, 'AppData', 'Roaming'), IDENTIFIER); default: return join(Deno.env.get('XDG_DATA_HOME') ?? join(home, '.local', 'share'), IDENTIFIER); } } export const documentsDir = () => join(appDataDir(), 'documents'); export const modelsDir = () => join(appDataDir(), 'models'); export const importsDir = () => join(appDataDir(), 'imports'); export const recentsFile = () => join(appDataDir(), 'recents.json'); export const sessionFile = () => join(appDataDir(), 'session.json'); export const apiKeyFile = () => join(appDataDir(), 'api-key'); /** The running instance's API port, so a second invocation (a ziran:// click, a double-clicked .tile) can hand its argument over instead of booting a whole second app. Staleness is fine: the hand-off simply fails to connect and the new process boots normally. */ export const appPortFile = () => join(appDataDir(), 'port'); export const syncDir = () => join(appDataDir(), 'sync'); export const groupsDir = () => join(syncDir(), 'groups'); export const identityFile = () => join(syncDir(), 'identity.json'); /** Durable MLS identity (KeyPackage + private keys), one file per DID — the dev identity and the AT identity each keep their own. */ export const mlsIdentityFile = (did: string) => join(syncDir(), 'identities', `${encodeURIComponent(did)}.json`); export const syncRegistryFile = () => join(syncDir(), 'docs.json'); /** Registry v2 — the Spaces provider's spaces + docs (docs/SYNC-SPACES.md). */ export const spacesRegistryFile = () => join(syncDir(), 'spaces.json'); export async function ensureDirs(): Promise { for (const dir of [documentsDir(), modelsDir(), importsDir(), groupsDir()]) { await Deno.mkdir(dir, { recursive: true }); } }