// Shared local-dev state written by tools/lchat-dev-up.mjs and read back by it // (to reuse the same admin token / db / port across restarts) and by // tools/lchat-dev-bot.mjs (to auto-discover the running server's base URL + // admin token, so you don't have to copy them by hand). // // The file lives at the repo root as `.lchat-dev.json` and is gitignored — it // holds the instance's admin token, so treat it like the secret it is (it's // written 0600 and never committed). The SQLite db it points at lives next to // it as `.lchat-dev.db` (+ `-wal`/`-shm`) and is covered by the repo's `*.db` // ignore rules. import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); export const CONFIG_PATH = path.join(REPO_ROOT, ".lchat-dev.json"); export const DEFAULT_DB_PATH = path.join(REPO_ROOT, ".lchat-dev.db"); // Where tools/lchat-dev-bot.mjs keeps the dev bot's identity (its keypair + // device token) so it survives restarts like the server does. It's tied to the // db above — a fresh instance invalidates it — so lchat-dev-up clears it on any // fresh start, and lchat-dev-bot --clean drops it to re-provision. export const BOT_SESSION_PATH = path.join(REPO_ROOT, ".lchat-dev-bot.json"); // Where the dev hooks sidecar (lchat hooks run, spawned by lchat-dev-up) // persists its service-identity session. Tied to the db exactly like the bot // session: a fresh instance invalidates it, so lchat-dev-up clears it on any // fresh start and re-mints the service invite. export const HOOKS_SESSION_PATH = path.join(REPO_ROOT, ".lchat-dev-hooks.json"); // The three files SQLite writes for one WAL-mode database, so callers can clean // them all in one sweep. export function dbFiles(dbPath) { return [dbPath, `${dbPath}-wal`, `${dbPath}-shm`]; } // Returns the parsed config, or null if it's missing/unreadable. export function readConfig() { try { return JSON.parse(fs.readFileSync(CONFIG_PATH, "utf8")); } catch { return null; } } export function writeConfig(cfg) { fs.writeFileSync(CONFIG_PATH, JSON.stringify(cfg, null, 2) + "\n", { mode: 0o600 }); } export function clearConfig() { fs.rmSync(CONFIG_PATH, { force: true }); } export function clearBotSession() { fs.rmSync(BOT_SESSION_PATH, { force: true }); } export function clearHooksSession() { fs.rmSync(HOOKS_SESSION_PATH, { force: true }); }