from __future__ import annotations import os from dataclasses import dataclass from pathlib import Path def _load_env_file(path: Path) -> None: if not path.exists(): return for raw_line in path.read_text(encoding="utf-8").splitlines(): line = raw_line.strip() if not line or line.startswith("#"): continue if line.startswith("export "): line = line[len("export ") :].lstrip() if "=" not in line: continue key, value = line.split("=", 1) key = key.strip() value = value.strip() if not key or key in os.environ: continue if len(value) >= 2 and value[0] == value[-1] and value[0] in {'"', "'"}: value = value[1:-1] os.environ[key] = value @dataclass(frozen=True) class AppSettings: config_path: Path cache_dir: Path refresh_interval_seconds: int host: str port: int @classmethod def from_env(cls) -> "AppSettings": project_root = Path(__file__).resolve().parents[1] _load_env_file(project_root / ".env") return cls( config_path=Path( os.environ.get("OPENCLAW_CONFIG_PATH", "/home/lucky/.openclaw/openclaw.json") ), cache_dir=Path(os.environ.get("MODEL_SELECTOR_CACHE_DIR", "data/model-cache")), refresh_interval_seconds=int(os.environ.get("MODEL_SELECTOR_REFRESH_INTERVAL", "1800")), host=os.environ.get("MODEL_SELECTOR_HOST", "0.0.0.0"), port=int(os.environ.get("MODEL_SELECTOR_PORT", "3300")), )