AgentEvalTool/backend/agenteval/config/settings.py
sinohqb 12481cd1b8 v0.3-s1: 规则层异步化 + 工具函数去重 + HTTP 通道
## 核心变更

### 规则层全面异步化(DEBT-1)
- EvalRule.evaluate() 签名改为 async def,全量同步改造(无兼容层)
- LlmScoreRule._call_llm: requests.post → httpx.AsyncClient,彻底消除事件循环阻塞
- engine._save_rule_results: rule.evaluate() → await rule.evaluate()

### 工具函数去重(DEBT-2)
- 新建 agenteval/utils/llm.py,统一三个函数:
  - extract_reply_text (原 5 处重复)
  - extract_content_from_llm_response (原 2 处重复)
  - parse_json_from_llm_text (统一 LLM 输出 JSON 解析)
- engine.py / llm_score.py / runs.py / report.py 全部切换到 utils.llm

### HTTP 通用通道(S1-3)
- 新建 channels/http.py (HttpChannel)
  - 配置化 send_url / reply_url 模板 ({message}, {msg_id} 占位)
  - dot-path 提取 msg_id 和 reply_text
  - 可选 reply_ready_path 就绪标志
  - 长连接 AsyncClient 复用
- ChannelFactory 注册 ChannelType.HTTP → HttpChannel

### 测试
- 新增 tests/unit/test_http_channel_and_rules.py (19 个测试)
- _get_path / health_check / send / poll_reply / 超时 / 就绪标志 / async 规则评估
- 测试总数:24 → 43,全部通过

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-17 10:52:32 +08:00

93 lines
3.5 KiB
Python

"""Centralized settings loaded from environment variables and .env files.
Priority (highest first):
1. Explicit environment variables
2. .env file at project root (loaded via python-dotenv on module import)
3. Defaults defined below
All secrets and deployment-specific values must be configured here rather than
hardcoded in source files.
"""
from functools import lru_cache
from pathlib import Path
from typing import Optional
from pydantic import Field, model_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
ROOT_DIR = Path(__file__).resolve().parent.parent.parent.parent
ENV_FILE = ROOT_DIR / ".env"
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=str(ENV_FILE),
env_file_encoding="utf-8",
env_prefix="AGENTEVAL_",
extra="ignore",
)
# ── API security ──────────────────────────────────────────────
api_key: Optional[str] = Field(
default=None,
description="If set, all /api/* endpoints require the X-API-Key header to match.",
)
allowed_origins: list[str] = Field(
default_factory=lambda: ["*"],
description="CORS allow_origins list. Use explicit URLs in production.",
)
# ── OpenClaw proxy ─────────────────────────────────────────────
openclaw_upstream: str = "http://openclaw-eval:18789"
openclaw_ws_upstream: str = "ws://openclaw-eval:18789"
openclaw_proxy_user: str = "agenteval"
openclaw_auth_token: str = "change-me-in-production"
openclaw_ws_origin: Optional[str] = Field(
default=None,
description=(
"Explicit Origin header sent to OpenClaw during WS handshake. "
"If unset, auto-derived from allowed_origins so the OpenClaw "
"gateway's allowedOrigins check passes."
),
)
# ── Frontend ───────────────────────────────────────────────────
frontend_dist_path: Optional[str] = None
# ── File Management ────────────────────────────────────────────
max_upload_size_mb: int = Field(
default=50,
description="Maximum single file upload size in megabytes.",
)
allowed_extensions: str = Field(
default="txt,md,json,yaml,yml,csv,xml,xlsx,xls,png,jpg,jpeg,gif,svg,zip,py,js,ts",
description="Comma-separated list of allowed file extensions for upload.",
)
@model_validator(mode="after")
def _derive_openclaw_ws_origin(self) -> "Settings":
"""Auto-derive openclaw_ws_origin from allowed_origins.
Picks the first non-wildcard, non-localhost origin from allowed_origins
(the typical "public URL" of this deployment). Falls back to
http://localhost:8000 if nothing suitable is found.
"""
if self.openclaw_ws_origin:
return self
for origin in self.allowed_origins:
if not origin or origin == "*" or "localhost" in origin or "127.0.0.1" in origin:
continue
self.openclaw_ws_origin = origin
return self
self.openclaw_ws_origin = "http://localhost:8000"
return self
@lru_cache(maxsize=1)
def get_settings() -> Settings:
"""Return the cached Settings instance."""
return Settings()