AgentEvalTool/backend/agenteval/channels/factory.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

26 lines
942 B
Python

"""Factory for creating message channels from target configuration."""
from agenteval.channels.base import EvalChannel
from agenteval.channels.http import HttpChannel
from agenteval.channels.tutu import TutuApiChannel
from agenteval.models import ChannelType, EvalTarget
class ChannelFactory:
"""Create the appropriate channel adapter for a target."""
_mapping: dict[ChannelType, type[EvalChannel]] = {
ChannelType.TUTU_API: TutuApiChannel,
ChannelType.HTTP: HttpChannel,
}
@classmethod
def create(cls, target: EvalTarget) -> EvalChannel:
if target.channel_type not in cls._mapping:
raise ValueError(f"unsupported channel type: {target.channel_type}")
return cls._mapping[target.channel_type](target.channel_config)
@classmethod
def register(cls, channel_type: ChannelType, channel_cls: type[EvalChannel]) -> None:
cls._mapping[channel_type] = channel_cls