Files

142 lines
3.8 KiB
Python

"""Конфигурация Xenith Studio: движки, сервер, MCP, UI.
Единый JSON в ~/.config/xenith-studio/config.json.
Секреты (API-ключи, токен сервера) хранятся там же с правами 0600.
"""
import copy
import json
import os
import threading
_DIR = os.path.join(os.path.expanduser("~"), ".config", "xenith-studio")
_PATH = os.path.join(_DIR, "config.json")
_LOCK = threading.Lock()
DEFAULT_CONFIG = {
"version": 1,
"active_engine": "xenith",
"xenith_backend": "auto",
"engines": {
"xenith": {
"type": "xenith",
"name": "Xenith",
"enabled": True,
},
"ollama-local": {
"type": "ollama",
"name": "Ollama",
"base_url": "http://127.0.0.1:11434",
"enabled": True,
},
},
"models": {
"dirs": [],
"download_dir": "~/.local/share/xenith-studio/models",
"presets": {},
"defaults": {
"ctx": 8192,
"gpu_layers": 99,
"threads": 0,
"flash_attn": False,
},
"hf_token": "",
"hf_proxy": "auto",
},
"server": {
"host": "127.0.0.1",
"port": 1234,
"token": "",
"enabled": False,
},
"mcp": {
"mode": "smart", # smart | full | restricted
"roots": [],
"servers": {},
},
"app_update": {
"provider": "gitea",
"base_url": "https://git.bipfr.ru",
"repo": "BIPfR/Xenith-Studio",
"github_repo": "BIPfR/Xenith-Studio",
"include_prerelease": False,
},
}
def _merge(base, override):
out = copy.deepcopy(base)
if not isinstance(override, dict):
return out
for k, v in override.items():
if isinstance(v, dict) and isinstance(out.get(k), dict):
out[k] = _merge(out[k], v)
else:
out[k] = copy.deepcopy(v)
return out
def load() -> dict:
_migrate_prefs()
try:
with open(_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
except Exception:
data = {}
cfg = _merge(DEFAULT_CONFIG, data if isinstance(data, dict) else {})
try:
update = cfg.get("app_update")
if isinstance(update, dict):
update.setdefault("provider", "gitea")
update.setdefault("base_url", "https://git.bipfr.ru")
if not update.get("repo"):
update["repo"] = update.get("github_repo") or "BIPfR/Xenith-Studio"
except Exception:
pass
try:
eng = (cfg.get("engines") or {}).get("xenith")
if isinstance(eng, dict) and eng.get("name") in ("Xenith (локальный)", "Xenith (local)"):
eng["name"] = "Xenith"
except Exception:
pass
return cfg
def save(cfg: dict) -> bool:
try:
os.makedirs(_DIR, exist_ok=True)
tmp = _PATH + ".tmp"
with _LOCK:
with open(tmp, "w", encoding="utf-8") as f:
json.dump(cfg, f, ensure_ascii=False, indent=2)
try:
os.chmod(tmp, 0o600)
except Exception:
pass
os.replace(tmp, _PATH)
return True
except Exception:
return False
def _migrate_prefs():
"""Перенести старый ui.json в новую структуру один раз."""
if os.path.exists(_PATH):
return
old = os.path.join(_DIR, "ui.json")
if not os.path.exists(old):
return
try:
with open(old, "r", encoding="utf-8") as f:
ui = json.load(f)
except Exception:
return
cfg = copy.deepcopy(DEFAULT_CONFIG)
if isinstance(ui, dict):
cfg["ui"] = {
"theme": ui.get("theme", "dark"),
"ui_scale": ui.get("ui_scale", 1.0),
}
save(cfg)