| """ |
| Persistent config — survives across requests within the same container session. |
| Falls back to env vars, then to saved JSON file. |
| """ |
| import os, json |
| from pathlib import Path |
|
|
| CONFIG_FILE = Path(os.environ.get("HOME", "/home/user")) / ".praison_config.json" |
|
|
| _config: dict = {} |
|
|
| def _load(): |
| global _config |
| |
| _config = { |
| "longcat_api_key": os.environ.get("LONGCAT_API_KEY", ""), |
| "longcat_model": os.environ.get("LONGCAT_MODEL", "LongCat-Flash-Lite"), |
| "telegram_token": os.environ.get("TELEGRAM_BOT_TOKEN", ""), |
| } |
| |
| if CONFIG_FILE.exists(): |
| try: |
| saved = json.loads(CONFIG_FILE.read_text()) |
| for k, v in saved.items(): |
| if v: |
| _config[k] = v |
| except Exception: |
| pass |
|
|
| def _save(): |
| try: |
| CONFIG_FILE.write_text(json.dumps(_config, indent=2)) |
| except Exception: |
| pass |
|
|
| |
| _load() |
|
|
| def get(key: str, default: str = "") -> str: |
| return _config.get(key, default) or default |
|
|
| def set(key: str, value: str): |
| _config[key] = value |
| _save() |
|
|
| def get_longcat_key() -> str: |
| return get("longcat_api_key") |
|
|
| def get_model() -> str: |
| return get("longcat_model", "LongCat-Flash-Lite") |
|
|
| def get_telegram_token() -> str: |
| return get("telegram_token") |
|
|
| def set_longcat_key(key: str): |
| set("longcat_api_key", key) |
| os.environ["LONGCAT_API_KEY"] = key |
|
|
| def set_telegram_token(token: str): |
| set("telegram_token", token) |
| os.environ["TELEGRAM_BOT_TOKEN"] = token |
|
|
| def set_model(model: str): |
| set("longcat_model", model) |
|
|
| def all_config() -> dict: |
| return {k: ("***" if "key" in k or "token" in k else v) |
| for k, v in _config.items()} |