import { ConfigError, loadConfig } from './config.ts'; import { openDb } from './db.ts'; import { Devices } from './devices.ts'; import { loadEnvFile } from './env.ts'; import { fetchModel, verifyModel } from './stt/modelStore.mjs'; /** * Operator CLI. It talks to the SQLite file directly, so there is no admin * surface on the network: whoever can run this on the bridge host is the owner. */ function usage(): never { console.error( [ 'Usage: npm run bridge:cli -- ', '', ' pair Print a single-use pairing code for one new device', ' devices List paired devices', ' revoke Revoke a device credential immediately', ' fetch-model Download the pinned speech model and verify every file', ' verify-model Check the speech model on disk against its pinned hashes', ].join('\n'), ); process.exit(2); } async function main(): Promise { loadEnvFile(); const config = loadConfig(); const [command, arg] = process.argv.slice(2); // Model commands run at deploy time, possibly as a different user from the service, and // must not create or touch the database. if (command === 'fetch-model' || command === 'verify-model') { const manifest = config.stt.manifest; if (!manifest) { console.log('STT_PROVIDER is not "builtin": there is no speech model to manage.'); return; } console.log(`${manifest.model} at ${manifest.revision} (${manifest.dtype}) in ${config.stt.modelDir}`); if (command === 'fetch-model') { const result = await fetchModel(config.stt.modelDir, manifest, { onFile: (path, bytes) => console.log(` fetched ${path} (${bytes} bytes)`), }); console.log(`Verified ${result.verified} file(s); downloaded ${result.downloaded}.`); return; } const check = await verifyModel(config.stt.modelDir, manifest); if (!check.ok) { console.error(`Verification FAILED: ${check.reason} ${check.path}`); process.exit(1); } console.log(`Verified ${manifest.files.length} file(s).`); return; } const db = openDb(config.dbPath); const devices = new Devices(db, config); switch (command) { case 'pair': { const { code, expiresAt } = devices.createPairingCode(); const minutes = Math.round((expiresAt - Date.now()) / 60_000); console.log(`Pairing code: ${code}`); console.log(`Valid for ${minutes} minute(s), one device, one use. Any earlier unused code is now void.`); break; } case 'devices': { const rows = devices.list(); if (!rows.length) console.log('No devices paired.'); for (const d of rows) { const status = d.revoked_at ? 'revoked' : d.pairing_expires_at <= Date.now() ? 'pairing expired' : d.token_expires_at <= Date.now() ? 'token expired' : 'active'; const seen = d.last_seen_at ? new Date(d.last_seen_at).toISOString() : 'never'; console.log(`${d.id} ${status.padEnd(15)} last seen ${seen} ${d.name}`); } break; } case 'revoke': { if (!arg) usage(); console.log(devices.revoke(arg) ? `Revoked ${arg}.` : `No active device ${arg}.`); break; } default: usage(); } db.close(); } main().catch((err: unknown) => { console.error(err instanceof ConfigError ? `Configuration error: ${err.message}` : err instanceof Error ? err.message : err); process.exit(1); });