docs(feishu): 添加富文本组件文档并更新配置说明

feat(config): 重构主机配置加载逻辑以支持多主机和独立模式

docs(readme): 更新安装和运行说明,添加多主机模式配置指南
This commit is contained in:
Yuyao Huang (Sam)
2026-03-29 02:56:47 +08:00
parent 52a9d085f7
commit 6cf2143987
3 changed files with 646 additions and 107 deletions
+88 -97
View File
@@ -1,97 +1,88 @@
"""Host client configuration loader.
Loads host_config.yaml which contains:
- NODE_ID, DISPLAY_NAME
- ROUTER_URL, ROUTER_SECRET
- LLM config (OPENAI_*)
- WORKING_DIR, METASO_API_KEY
- SERVES_USERS list
"""
from __future__ import annotations
import os
from pathlib import Path
from typing import List, Optional
import yaml
class HostConfig:
"""Configuration for a host client node."""
def __init__(self, config_path: Optional[Path] = None):
config_path = config_path or Path(__file__).parent.parent / "host_config.yaml"
self._load(config_path)
def _load(self, config_path: Path) -> None:
if not config_path.exists():
raise FileNotFoundError(f"Config file not found: {config_path}")
with open(config_path, "r", encoding="utf-8") as f:
data = yaml.safe_load(f) or {}
self.node_id: str = data.get("NODE_ID", "unknown-node")
self.display_name: str = data.get("DISPLAY_NAME", 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", [])
self.serves_users: list[str] = serves_users if isinstance(serves_users, list) else []
self.capabilities: list[str] = data.get(
"CAPABILITIES",
["claude_code", "shell", "file_ops", "web", "scheduler"],
)
@classmethod
def from_keyring(cls, keyring_path: Optional[Path] = None) -> "HostConfig":
"""Create config from keyring.yaml (for standalone mode)."""
keyring_path = keyring_path or Path(__file__).parent.parent / "keyring.yaml"
if not keyring_path.exists():
raise FileNotFoundError(f"keyring.yaml not found: {keyring_path}")
with open(keyring_path, "r", encoding="utf-8") as f:
data = yaml.safe_load(f) or {}
config = cls.__new__(cls)
config.node_id = data.get("NODE_ID", "local-node")
config.display_name = data.get("DISPLAY_NAME", "Local Machine")
config.router_url = data.get("ROUTER_URL", "ws://127.0.0.1:8000/ws/node")
config.router_secret = data.get("ROUTER_SECRET", "")
config.openai_base_url = data.get(
"OPENAI_BASE_URL", "https://open.bigmodel.cn/api/paas/v4/"
)
config.openai_api_key = data.get("OPENAI_API_KEY", "")
config.openai_model = data.get("OPENAI_MODEL", "glm-4.7")
config.working_dir = data.get("WORKING_DIR", str(Path.home() / "projects"))
config.metaso_api_key = data.get("METASO_API_KEY")
serves_users = data.get("ALLOWED_OPEN_IDS", [])
config.serves_users = serves_users if isinstance(serves_users, list) else []
config.capabilities = ["claude_code", "shell", "file_ops", "web", "scheduler"]
return config
host_config: Optional[HostConfig] = None
def get_host_config() -> HostConfig:
"""Get the global host config instance."""
global host_config
if host_config is None:
host_config = HostConfig()
return host_config
"""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