|
| 1 | +// Host-agnostic path/resource resolution for the gateway runtime. |
| 2 | +// |
| 3 | +// The runtime (llm, imagegen, tts, embeddings, active-models, model-server, |
| 4 | +// rag/extractors, mflux) must run BOTH embedded in Electron AND standalone |
| 5 | +// (the future @offgrid/gateway: a Node CLI / Docker image). This is the single |
| 6 | +// seam that hides where data + bundled binaries live, so none of those modules |
| 7 | +// import `electron` directly. |
| 8 | +// |
| 9 | +// Resolution order: explicit configure() → env vars → Electron `app` → cwd. |
| 10 | +import path from 'path'; |
| 11 | + |
| 12 | +interface RuntimeConfig { |
| 13 | + dataDir?: string; // writable per-user dir (models, caches, generated output) |
| 14 | + binRoots?: string[]; // dirs to search for bundled binaries (llama-server, ffmpeg, …) |
| 15 | + resourceDirs?: string[]; // dirs to search for bundled resources (tts-worker.mjs, …) |
| 16 | +} |
| 17 | + |
| 18 | +let cfg: RuntimeConfig = {}; |
| 19 | + |
| 20 | +/** Host calls this once at startup. Electron host passes app paths; a standalone |
| 21 | + * host passes its own. Any field left out falls back to env/electron/cwd. */ |
| 22 | +export function configureRuntime(c: RuntimeConfig): void { |
| 23 | + cfg = { ...cfg, ...c }; |
| 24 | +} |
| 25 | + |
| 26 | +// Lazily probe Electron without a hard dependency (so the module loads in plain Node). |
| 27 | +function electron(): { dataDir: string; binRoots: string[]; resourceDirs: string[] } | null { |
| 28 | + try { |
| 29 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 30 | + const { app } = require('electron'); |
| 31 | + if (!app?.getPath) return null; |
| 32 | + const packaged = app.isPackaged; |
| 33 | + return { |
| 34 | + dataDir: app.getPath('userData'), |
| 35 | + binRoots: packaged |
| 36 | + ? [path.join(process.resourcesPath, 'bin')] |
| 37 | + : [path.join(app.getAppPath(), 'resources', 'bin'), path.join(process.cwd(), 'resources', 'bin')], |
| 38 | + resourceDirs: packaged |
| 39 | + ? [process.resourcesPath] |
| 40 | + : [path.join(app.getAppPath(), 'resources'), path.join(process.cwd(), 'resources')], |
| 41 | + }; |
| 42 | + } catch { |
| 43 | + return null; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** Writable per-user data dir (models, caches, generated images, settings). */ |
| 48 | +export function dataDir(): string { |
| 49 | + if (cfg.dataDir) return cfg.dataDir; |
| 50 | + if (process.env.OFFGRID_DATA_DIR) return process.env.OFFGRID_DATA_DIR; |
| 51 | + const e = electron(); |
| 52 | + if (e) return e.dataDir; |
| 53 | + return path.join(process.cwd(), '.offgrid'); |
| 54 | +} |
| 55 | + |
| 56 | +/** The models directory under the data dir. */ |
| 57 | +export function modelsDir(): string { |
| 58 | + return path.join(dataDir(), 'models'); |
| 59 | +} |
| 60 | + |
| 61 | +/** Dirs to search for bundled binaries (binary lives under one of these). */ |
| 62 | +export function binRoots(): string[] { |
| 63 | + if (cfg.binRoots?.length) return cfg.binRoots; |
| 64 | + if (process.env.OFFGRID_BIN_DIR) return [process.env.OFFGRID_BIN_DIR]; |
| 65 | + const e = electron(); |
| 66 | + if (e) return e.binRoots; |
| 67 | + return [path.join(process.cwd(), 'resources', 'bin')]; |
| 68 | +} |
| 69 | + |
| 70 | +/** App/package root (cwd for spawned helpers that resolve their own deps). */ |
| 71 | +export function appRoot(): string { |
| 72 | + if (process.env.OFFGRID_APP_ROOT) return process.env.OFFGRID_APP_ROOT; |
| 73 | + try { |
| 74 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 75 | + const { app } = require('electron'); |
| 76 | + if (app?.getAppPath) return app.getAppPath(); |
| 77 | + } catch { |
| 78 | + /* standalone */ |
| 79 | + } |
| 80 | + return process.cwd(); |
| 81 | +} |
| 82 | + |
| 83 | +/** Resolve a bundled resource file by name across the resource dirs, or null. */ |
| 84 | +export function resourceFile(name: string): string | null { |
| 85 | + for (const d of resourceDirs()) { |
| 86 | + const p = path.join(d, name); |
| 87 | + try { |
| 88 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 89 | + if (require('fs').existsSync(p)) return p; |
| 90 | + } catch { |
| 91 | + /* ignore */ |
| 92 | + } |
| 93 | + } |
| 94 | + return null; |
| 95 | +} |
| 96 | + |
| 97 | +/** Whether running inside a packaged app (affects quarantine handling on macOS). */ |
| 98 | +export function isPackaged(): boolean { |
| 99 | + if (process.env.OFFGRID_PACKAGED) return process.env.OFFGRID_PACKAGED === '1'; |
| 100 | + try { |
| 101 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 102 | + return !!require('electron')?.app?.isPackaged; |
| 103 | + } catch { |
| 104 | + return false; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** Register a shutdown callback. In Electron, hooks 'before-quit'; standalone |
| 109 | + * hosts handle process teardown themselves (no-op here). */ |
| 110 | +export function onHostQuit(fn: () => void): void { |
| 111 | + try { |
| 112 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 113 | + require('electron')?.app?.on?.('before-quit', fn); |
| 114 | + } catch { |
| 115 | + /* standalone: host owns shutdown */ |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/** Dirs to search for bundled resources (e.g. tts-worker.mjs). */ |
| 120 | +export function resourceDirs(): string[] { |
| 121 | + if (cfg.resourceDirs?.length) return cfg.resourceDirs; |
| 122 | + if (process.env.OFFGRID_RESOURCE_DIR) return [process.env.OFFGRID_RESOURCE_DIR]; |
| 123 | + const e = electron(); |
| 124 | + if (e) return e.resourceDirs; |
| 125 | + return [path.join(process.cwd(), 'resources')]; |
| 126 | +} |
0 commit comments