"""Host client configuration loader. Loads host_config.yaml (multi-host mode) or keyring.yaml (standalone mode). Fields: - NODE_ID, DISPLAY_NAME - ROUTER_URL, ROUTER_SECRET - OPENAI_BASE_URL, OPENAI_API_KEY, OPENAI_MODEL - WORKING_DIR, METASO_API_KEY - SERVES_USERS / ALLOWED_OPEN_IDS, CAPABILITIES """ from __future__ import annotations from pathlib import Path from typing import Optional import yaml def _load_yaml(path: Path) -> dict: if not path.exists(): raise FileNotFoundError(f"Config file not found: {path}") with open(path, "r", encoding="utf-8") as f: return yaml.safe_load(f) or {} class HostConfig: """Configuration for a host client node.""" def __init__( self, data: dict, *, serves_users_key: str = "SERVES_USERS", default_node_id: str = "unknown-node", default_display_name: str = "", ): self.node_id: str = data.get("NODE_ID", default_node_id) self.display_name: str = data.get("DISPLAY_NAME", default_display_name or self.node_id) self.router_url: str = data.get("ROUTER_URL", "ws://127.0.0.1:8000/ws/node") self.router_secret: str = data.get("ROUTER_SECRET", "") self.openai_base_url: str = data.get( "OPENAI_BASE_URL", "https://open.bigmodel.cn/api/paas/v4/" ) self.openai_api_key: str = data.get("OPENAI_API_KEY", "") self.openai_model: str = data.get("OPENAI_MODEL", "glm-4.7") self.working_dir: str = data.get("WORKING_DIR", str(Path.home() / "projects")) self.metaso_api_key: Optional[str] = data.get("METASO_API_KEY") serves_users = data.get(serves_users_key, []) self.serves_users: list[str] = serves_users if isinstance(serves_users, list) else [] capabilities = data.get("CAPABILITIES", ["claude_code", "shell", "file_ops", "web", "scheduler"]) self.capabilities: list[str] = capabilities if isinstance(capabilities, list) else [] if not self.openai_api_key: raise ValueError("OPENAI_API_KEY is required in config but was not set") @classmethod def from_file(cls, config_path: Optional[Path] = None) -> "HostConfig": """Load from host_config.yaml (multi-host mode).""" path = config_path or Path(__file__).parent.parent / "host_config.yaml" return cls(_load_yaml(path), serves_users_key="SERVES_USERS", default_node_id="unknown-node") @classmethod def from_keyring(cls, keyring_path: Optional[Path] = None) -> "HostConfig": """Load from keyring.yaml (standalone mode).""" path = keyring_path or Path(__file__).parent.parent / "keyring.yaml" return cls( _load_yaml(path), serves_users_key="ALLOWED_OPEN_IDS", default_node_id="local-node", default_display_name="Local Machine", ) _host_config: Optional[HostConfig] = None def get_host_config() -> HostConfig: """Get the global host config instance (multi-host mode).""" global _host_config if _host_config is None: _host_config = HostConfig.from_file() return _host_config