const fs = require("fs"); const { resolveData, ensureDataDirs } = require("./paths"); const DEFAULT_CONFIG = { enabled: false, selected_model_id: "qwen3-1.7b-q4", context_size: 4096, threads: 0, concurrency: 1, max_queue_length: 8, request_timeout_ms: 120000, per_user_requests_per_minute: 6, admin_bypass_rate_limit: false, assistant_visibility: { admins: true, mods: false, users: false }, instructions: { identity: "You are Lumi Assistant, a concise assistant for this Lumi bot and community.", style: "Be brief, factual, and provide internal WebUI links when known.", allowed_topics: "Lumi, its WebUI, plugins, community systems, streams, and videos.", out_of_scope_response: "I am sorry, but that is outside my scope.", maximum_answer_length: 700, roleplay_intensity: 0, community_tone: "", admin_custom: "" }, logging: { log_prompts: false, log_responses: false, log_tool_calls: true, log_metrics: true, log_internal_audit: true } }; function readJson(name, fallback) { ensureDataDirs(); const file = resolveData("config", name); if (!fs.existsSync(file)) { writeJson(name, fallback); return structuredClone(fallback); } try { return { ...structuredClone(fallback), ...JSON.parse(fs.readFileSync(file, "utf8")) }; } catch { return structuredClone(fallback); } } function writeJson(name, value) { const file = resolveData("config", name); const tmp = `${file}.tmp`; fs.writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`); fs.renameSync(tmp, file); } function getConfig() { return readJson("ai_config.json", DEFAULT_CONFIG); } function saveConfig(value) { const merged = { ...DEFAULT_CONFIG, ...value }; merged.assistant_visibility = { ...DEFAULT_CONFIG.assistant_visibility, ...(value.assistant_visibility || {}) }; merged.instructions = { ...DEFAULT_CONFIG.instructions, ...(value.instructions || {}) }; merged.logging = { ...DEFAULT_CONFIG.logging, ...(value.logging || {}) }; writeJson("ai_config.json", merged); return merged; } function getRuntimeState() { return readJson("runtime_state.json", { desired_state: "stopped", last_known_state: "stopped", last_stop_reason: "never_started", last_manual_stop: true, last_crashed: false, last_exit_code: null, last_diagnostic_category: null, selected_model_id: null, updated_at: new Date().toISOString() }); } function saveRuntimeState(value) { writeJson("runtime_state.json", { ...value, updated_at: new Date().toISOString() }); } module.exports = { DEFAULT_CONFIG, getConfig, saveConfig, getRuntimeState, saveRuntimeState, readJson, writeJson };