AgentEvalTool/backend/agenteval/config/settings.py
sinohqb a77cd83e6a v0.2.0-dev: 文件管理 + 页面布局统一 + 6 个 bug 修复
## 新增功能
- 文件管理模块:分类树 + 文件上传/下载/删除
- 文件上传支持拖拽(Dragger)+ 手动上传(customRequest 模式)

## 页面布局统一(参照评测执行页)
- 仪表盘/评测对象/评测场景/评测报告 全部改为全高 flex 布局
- 统一内联页头样式(h2 + 竖线分隔 + 描述)
- 表格撑满高度、overflow 处理
- 每页添加刷新按钮

## Bug 修复
- 分类树操作按钮 hover 不可见(CSS 规则缺失)
- 文件上传失败(multipart boundary 缺失)
- LLM API 响应 content blocks 数组格式支持(_extract_content_from_api_response)
- response_time_max_ms 被静默忽略(隐式规则传空 params)
- 空 messages 导致 IndexError 崩溃
- poll_reply 异常中止整个 run(缺 try/catch)
- engine finally 未关闭 session
- 3 个页面 UTC 时间戳解析偏差 8 小时

## 后端
- EvalEngine: poll_reply 异常保护、空 dialog 保护、session 关闭
- LLM API 响应解析支持 content-block-array 格式
- 隐式 response_time 规则正确传递 max_ms 参数

## 前端
- api.ts: 移除手动 Content-Type(让浏览器自动添加 boundary)
- Files.tsx: customRequest 替代 beforeUpload、布局优化
- index.css: 分类树 hover 规则
- Targets/Scenarios/Home/Reports: 全高布局改造
- 3 个页面时间戳改用 formatDateTime()(修复 UTC 偏差)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-16 15:25:22 +08:00

94 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()