Spaces:
Paused

assistant / backend /user_config.py
localailb's picture
Deploy Full LocalAiLab Application (FastAPI + Svelte + Agents)
bc9f3e6 verified
Raw History Blame Contribute Delete
2.48 kB
"""
user_config.py — Tiny persisted-settings store (survives app restarts).
Currently stores the GGUF model folder path (so the user only has to
type/scan it once) and the UI language (so the app restarts in the user's
last-chosen Khmer or English UI).
"""
import json
from pathlib import Path
from backend.paths import user_data_dir
USER_DATA_DIR = user_data_dir()
USER_CONFIG_PATH = USER_DATA_DIR / "user_config.json"
def load_user_config() -> dict:
try:
if USER_CONFIG_PATH.exists():
return json.loads(USER_CONFIG_PATH.read_text(encoding="utf-8"))
except Exception as e:
print(f"[Config] Could not read '{USER_CONFIG_PATH}': {e}")
return {}
def save_user_config(updates: dict) -> None:
try:
USER_DATA_DIR.mkdir(parents=True, exist_ok=True)
data = load_user_config()
data.update(updates)
USER_CONFIG_PATH.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
USER_CONFIG.update(updates)
except Exception as e:
print(f"[Config] Could not write '{USER_CONFIG_PATH}': {e}")
# Loaded once at import time; individual values are re-read via
# load_user_config() when freshness matters (there's only ever one process
# writing this file, so a module-level snapshot is fine for startup use).
USER_CONFIG = load_user_config()
DEFAULT_LANG = "kh"
def get_saved_lang() -> str:
"""The persisted UI language ('kh' or 'en'); defaults to Khmer when the
user has never changed it or the stored value is invalid."""
lang = USER_CONFIG.get("lang", DEFAULT_LANG)
return "kh" if lang != "en" else "en"
def set_saved_lang(lang: str) -> None:
"""Persist the UI language choice ('kh' or 'en') for the next launch."""
save_user_config({"lang": "kh" if lang != "en" else "en"})
def is_first_run() -> bool:
"""True until the user has seen (and dismissed) the first-run onboarding
— the Settings-tab landing + popup shown on the very first launch.
Persisted under the 'first_run_done' key, so it shows exactly once and
never again on later launches."""
return not USER_CONFIG.get("first_run_done", False)
def mark_first_run_done() -> None:
"""Persist that the first-run onboarding has been seen so it never
shows again (idempotent — the UI calls this when the popup is
dismissed)."""
save_user_config({"first_run_done": True})