AgentEvalTool/backend/agenteval/config/settings.py
sinohqb 349200e51f v0.3-s3: Webhook + OpenClaw HTTP Skill + Markdown/对比报告
## Webhook 通知(S3-1)
- settings.py: 增加 AGENTEVAL_WEBHOOK_URL / AGENTEVAL_WEBHOOK_SECRET
- utils/webhook.py: send_run_webhook(),非阻断,任何异常仅 warning log
- runs.py: run 完成后自动触发 webhook(payload 含 run_id/status/summary/report_url)
- .env.example: 新增 webhook 配置示例

## OpenClaw Skill HTTP 改造(S3-2)
- plugins/openclaw/agenteval_skill.py: 完全重写
  - 改用 HTTP API(POST /api/runs + GET /api/runs/{id} 轮询 + GET /api/reports/{id})
  - 移除 subprocess + CLI 依赖
  - 轮询等待至 completed/failed,支持配置 poll_interval / timeout
  - 返回结构化中文摘要(summary_text),直接可用于 OpenClaw 对话展示

## Markdown 报告导出(S3-3)
- report.py: render_markdown_report() — 完整的 Markdown 表格 + 对话展示
- save_report: 支持 fmt="markdown",输出 .md 文件
- reports.py: GET /api/reports/{run_id}/markdown,Content-Disposition 附件下载
- api.ts: reportsApi.markdownUrl()
- Reports.tsx: 「导出 MD」按钮

## 对比报告(S3-4)
- report.py: generate_compare_report(run_id_1, run_id_2)
  - run_a / run_b 汇总 + delta(pass_rate / passed_cases / passed_rules)
  - case-level diff,标记 changed 用例
- reports.py: GET /api/reports/compare?run1=&run2=
- api.ts: reportsApi.compare()
- Reports.tsx: 完整对比视图
  - Segmented 切换「单次报告」/「对比报告」
  - 双 Select(报告 A vs B)+ 对比按钮
  - 汇总 delta card(pass_rate 变化 + 变化用例数徽章)
  - 用例对比表(通过/失败/改善↑/退步↓)+ 展开规则明细

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-17 11:44:54 +08:00

103 lines
4.0 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.",
)
# ── Webhook ────────────────────────────────────────────────────
webhook_url: Optional[str] = Field(
default=None,
description="If set, POST run completion summaries to this URL.",
)
webhook_secret: Optional[str] = Field(
default=None,
description="If set, included as X-Webhook-Secret header for verification.",
)
@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()