|
| 1 | +// SPDX-License-Identifier: AGPL-3.0 |
| 2 | +import { mkdir, readFile, open, rename } from 'node:fs/promises'; |
| 3 | +import { dirname, join } from 'node:path'; |
| 4 | +import { homedir } from 'node:os'; |
| 5 | + |
| 6 | +export const normalizeModelName = name => typeof name === 'string' |
| 7 | + ? name.trim().split('/').pop().replace(/\.gguf$/i, '').replace(/-\d{5}-of-\d{5}$/, '') : ''; |
| 8 | + |
| 9 | +/** Optional local chooser bridge. Never sends the intent key to the chooser. |
| 10 | + * Claims are durable BEFORE dispatch. An uncertain dispatch is reconciled, |
| 11 | + * never replayed: exactly-once side effects cannot be promised by HTTP. |
| 12 | + */ |
| 13 | +export class ModelSwitcher { |
| 14 | + #client; #url; #file; #brain; #fetch; #now; #timeout; #busy = false; |
| 15 | + #records; #timer; #fields; #warn; #lastWarning = -Infinity; |
| 16 | + constructor(client, { |
| 17 | + chooserUrl = process.env.INTENT_CHOOSER_URL, |
| 18 | + brainOf = process.env.INTENT_DEVICE_BRAIN_OF || '', |
| 19 | + stateFile = process.env.INTENT_SWITCH_STATE_FILE || join(homedir(), '.local', 'state', 'user-intent-kit', |
| 20 | + `switch-${encodeURIComponent(client.userId)}-${encodeURIComponent(client.deviceId)}.json`), |
| 21 | + fetchImpl = fetch, now = Date.now, timeoutMs = 15 * 60 * 1000, warn = console.warn, |
| 22 | + } = {}) { |
| 23 | + this.#client = client; this.#brain = brainOf; this.#file = stateFile; |
| 24 | + this.#fetch = fetchImpl; this.#now = now; this.#timeout = timeoutMs; |
| 25 | + this.#warn = warn; |
| 26 | + if (chooserUrl) { |
| 27 | + const u = new URL(chooserUrl); |
| 28 | + if (!['http:', 'https:'].includes(u.protocol) || u.username || u.password || u.search || u.hash) |
| 29 | + throw new Error('Invalid INTENT_CHOOSER_URL'); |
| 30 | + this.#url = u.href.replace(/\/+$/, ''); |
| 31 | + } |
| 32 | + this.#fields = this.#down(); |
| 33 | + } |
| 34 | + get enabled() { return !!this.#url; } |
| 35 | + #down() { return { model_catalog: [], model: null, model_state: 'down', brain_of: this.#brain }; } |
| 36 | + fields() { return this.enabled ? structuredClone(this.#fields) : {}; } |
| 37 | + #warning(reason) { |
| 38 | + const now = this.#now(); |
| 39 | + if (now >= this.#lastWarning && now - this.#lastWarning < 300000) return; |
| 40 | + this.#lastWarning = now; |
| 41 | + // Fixed diagnostics only: never log URLs, keys, response bodies or model paths. |
| 42 | + this.#warn(`uik model switcher: ${reason}`); |
| 43 | + } |
| 44 | + async #registry() { |
| 45 | + try { return await this.#readRegistry(); } |
| 46 | + catch (error) { this.#warning('local chooser unavailable or invalid; no new switch dispatched'); throw error; } |
| 47 | + } |
| 48 | + async #readRegistry() { |
| 49 | + const response = await this.#fetch(`${this.#url}/api/models`, { signal: AbortSignal.timeout(10000) }); |
| 50 | + if (!response.ok) throw new Error('Chooser unavailable'); |
| 51 | + const r = await response.json(); |
| 52 | + if (!Array.isArray(r.models) || !['ready', 'loading', 'down'].includes(r.state)) |
| 53 | + throw new Error('Invalid chooser registry'); |
| 54 | + this.#fields = { |
| 55 | + model_catalog: r.models.filter(m => m && typeof m.name === 'string' && m.name.trim()).map(m => ({ |
| 56 | + name: m.name, size_gb: Number.isFinite(m.size_gb) ? m.size_gb : null, |
| 57 | + params_label: typeof m.params_label === 'string' ? m.params_label : null, fits: m.fits === true, |
| 58 | + })), |
| 59 | + model: typeof r.running === 'string' ? r.running : null, |
| 60 | + model_state: r.state, brain_of: this.#brain, |
| 61 | + }; |
| 62 | + return r; |
| 63 | + } |
| 64 | + async refresh() { |
| 65 | + if (!this.enabled) return {}; |
| 66 | + try { await this.#registry(); } catch { this.#fields = this.#down(); } |
| 67 | + return this.fields(); |
| 68 | + } |
| 69 | + async #load() { |
| 70 | + if (this.#records) return; |
| 71 | + try { |
| 72 | + const data = JSON.parse(await readFile(this.#file, 'utf8')); |
| 73 | + if (data.version !== 1 || !data.records || typeof data.records !== 'object' || Array.isArray(data.records)) |
| 74 | + throw new Error('Invalid switch state'); |
| 75 | + const records = new Map(Object.entries(data.records)); |
| 76 | + for (const record of records.values()) { |
| 77 | + if (!record || typeof record.model !== 'string' || !Number.isFinite(record.started) || |
| 78 | + !['claimed', 'dispatched', 'done', 'failed', 'expired'].includes(record.status)) |
| 79 | + throw new Error('Invalid switch state record'); |
| 80 | + } |
| 81 | + this.#records = records; |
| 82 | + } catch (error) { |
| 83 | + if (error.code !== 'ENOENT') { |
| 84 | + this.#warning('state file unreadable or corrupt; switching paused (preserve the file for inspection)'); |
| 85 | + throw error; // corrupt/unreadable state fails closed |
| 86 | + } |
| 87 | + this.#records = new Map(); |
| 88 | + } |
| 89 | + } |
| 90 | + async #save(id, record) { |
| 91 | + // Do not update the in-memory claim until persistence succeeds. |
| 92 | + const next = new Map(this.#records); next.set(id, record); |
| 93 | + await mkdir(dirname(this.#file), { recursive: true, mode: 0o700 }); |
| 94 | + const tmp = `${this.#file}.${process.pid}.tmp`; |
| 95 | + const file = await open(tmp, 'w', 0o600); |
| 96 | + try { |
| 97 | + await file.writeFile(JSON.stringify({ version: 1, records: Object.fromEntries(next) })); |
| 98 | + await file.sync(); |
| 99 | + } finally { await file.close(); } |
| 100 | + await rename(tmp, this.#file); |
| 101 | + const directory = await open(dirname(this.#file), 'r'); |
| 102 | + try { await directory.sync(); } finally { await directory.close(); } |
| 103 | + this.#records = next; |
| 104 | + } |
| 105 | + async #finish(id, record, status, detail) { |
| 106 | + const terminal = { ...record, status, detail }; |
| 107 | + await this.#save(id, terminal); // retain report even if PATCH is unavailable |
| 108 | + await this.#client.reportModelSwitch({ id, status, ...(detail === undefined ? {} : { detail }) }); |
| 109 | + } |
| 110 | + async poll() { |
| 111 | + if (!this.enabled || this.#busy) return; |
| 112 | + this.#busy = true; |
| 113 | + try { |
| 114 | + await this.#load(); |
| 115 | + const { request: req } = await this.#client.getModelSwitch(); |
| 116 | + if (!req || typeof req.id !== 'string' || !req.id || typeof req.model !== 'string') return; |
| 117 | + let record = this.#records.get(req.id); |
| 118 | + if (['done', 'failed'].includes(req.status) || (req.status === 'expired' && record?.status === 'expired')) return; |
| 119 | + if (record && ['done', 'failed', 'expired'].includes(record.status)) { |
| 120 | + await this.#client.reportModelSwitch({ id: req.id, status: record.status, detail: record.detail }); |
| 121 | + return; |
| 122 | + } |
| 123 | + if (!record) { |
| 124 | + if (!['pending', 'expired'].includes(req.status)) return; // never adopt an unknown running command |
| 125 | + const filed = Date.parse(req.requested_at); |
| 126 | + record = { model: req.model, started: this.#now(), status: 'claimed' }; |
| 127 | + if (req.status === 'expired' || !Number.isFinite(filed) || this.#now() - filed > 600000 || filed > this.#now() + 60000) |
| 128 | + return await this.#finish(req.id, record, 'expired', 'Request expired'); |
| 129 | + const registry = await this.#registry(); // current LOCAL catalog, not the server's list |
| 130 | + const matches = registry.models.filter(m => normalizeModelName(m?.name) === normalizeModelName(req.model)); |
| 131 | + const pick = matches.length === 1 ? matches[0] : null; |
| 132 | + if (!pick || pick.fits !== true) |
| 133 | + return await this.#finish(req.id, record, 'failed', 'Model is not in the local fitting catalog'); |
| 134 | + if (registry.state === 'ready' && normalizeModelName(registry.running) === normalizeModelName(pick.name)) |
| 135 | + return await this.#finish(req.id, record, 'done', 'already running'); |
| 136 | + await this.#save(req.id, record); |
| 137 | + // If this PATCH fails/has an uncertain result, no local POST is made. |
| 138 | + await this.#client.reportModelSwitch({ id: req.id, status: 'running' }); |
| 139 | + await this.#save(req.id, { ...record, status: 'dispatched' }); |
| 140 | + let response; |
| 141 | + try { |
| 142 | + response = await this.#fetch(`${this.#url}/api/model`, { |
| 143 | + method: 'POST', headers: { 'Content-Type': 'application/json' }, |
| 144 | + body: JSON.stringify({ name: pick.name }), signal: AbortSignal.timeout(70000), |
| 145 | + }); |
| 146 | + } catch { |
| 147 | + this.#warning('chooser dispatch response uncertain; reconciling without replay'); |
| 148 | + return; // reconcile later; NEVER retry an uncertain POST |
| 149 | + } |
| 150 | + const body = await response.text(); |
| 151 | + if (!response.ok) return await this.#finish(req.id, record, 'failed', body); |
| 152 | + let result; |
| 153 | + try { result = JSON.parse(body); } catch { return; } |
| 154 | + if (result.ok !== true) return await this.#finish(req.id, record, 'failed', body); |
| 155 | + return; // readiness checked on the independent next 15s poll |
| 156 | + } |
| 157 | + if (req.status === 'pending') |
| 158 | + return await this.#finish(req.id, record, 'failed', 'Interrupted before dispatch; not replayed'); |
| 159 | + if (req.status !== 'running') return; |
| 160 | + let registry; |
| 161 | + try { registry = await this.#registry(); } catch { /* transient outage until deadline */ } |
| 162 | + if (registry?.state === 'ready' && normalizeModelName(registry.running) === normalizeModelName(record.model)) |
| 163 | + return await this.#finish(req.id, record, 'done'); |
| 164 | + if (this.#now() - record.started >= this.#timeout) |
| 165 | + await this.#finish(req.id, record, 'failed', 'Timed out waiting for the requested model to be ready; not replayed'); |
| 166 | + } catch (error) { |
| 167 | + this.#warning('poll/report or state persistence failed; switching fails closed without dispatch replay'); |
| 168 | + throw error; |
| 169 | + } finally { this.#busy = false; } |
| 170 | + } |
| 171 | + start() { |
| 172 | + this.stop(); |
| 173 | + if (!this.enabled) return; |
| 174 | + this.poll().catch(() => {}); |
| 175 | + this.#timer = setInterval(() => this.poll().catch(() => {}), 15000); |
| 176 | + this.#timer.unref?.(); |
| 177 | + } |
| 178 | + stop() { if (this.#timer) clearInterval(this.#timer); this.#timer = null; } |
| 179 | +} |
0 commit comments