backend-api-test-doc-agent-hub.md 14.5 KB

接口测试文档 — agent-hub


1. 模块概览

字段 值
模块名称 agent-hub
模块说明 中央调度器 — Agent 消息路由、会话管理、回复发送
路由前缀 /api/v1/chat
数据库表 sessions, session_messages
外部依赖 Model API(LLM 模型调用)、IM Service(企微消息发送)
核心组件 AgentHub(调度器)、SessionService(会话服务)、PostReplyHook(回复钩子)

2. 接口 & 组件列表

序号 类型 路径 / 类 说明
1 API POST /api/v1/chat/completions 对话补全接口(核心入口)
2 Service SessionService 会话 CRUD + 消息管理
3 Hook PostReplyHook 回复发送钩子(清理 thinking 块 + IM 发送)
4 Service AgentHub 消息路由 + 模型调用(process_message)

3. 接口 & 组件详细说明

3.1 对话补全接口

基本信息

字段 值
方法 POST
路径 /api/v1/chat/completions
说明 接收用户消息,经 AgentHub 路由到对应 Agent 处理,返回回复内容
Content-Type application/json

请求参数

参数名 位置 类型 必填 说明
session_id body string 是 会话ID,空字符串时自动生成
user_id body string 是 用户ID
room_id body string 否 群ID,默认 ""
channel body string 否 消息渠道,默认 wechat_group
query body string 是 用户问题
mentioned_agent_code body string 否 @提及的 Agent 编码,如 meeting
file_url body string 否 文件URL

请求示例

{
  "session_id": "sess_api_001",
  "user_id": "user_001",
  "room_id": "room_001",
  "query": "你好",
  "mentioned_agent_code": "meeting"
}

响应参数

字段路径 类型 说明
session_id string 会话ID
response_agent_code string 实际响应的 Agent 编码(main / 子 Agent 编码)
response_content string Agent 回复内容
routing_reason string 路由原因:default(默认main)/ mention(@提及)/ inheritance(继承上次)
processing_time_ms float 处理耗时(毫秒)

成功响应示例

{
  "session_id": "sess_api_001",
  "response_agent_code": "main",
  "response_content": "你好,我是智能助手,有什么可以帮助你的?",
  "routing_reason": "default",
  "processing_time_ms": 123.45
}

错误码

HTTP 状态码 触发条件 错误信息示例
400 业务校验失败(ValueError) —
422 请求参数校验失败(缺少必填字段) —
502 模型调用失败(AgentHubError) 模型API返回错误: 500

3.2 SessionService(会话服务)

基本信息

字段 值
类型 Service(非 HTTP 接口,被 AgentHub 内部调用)
依赖 SQLAlchemy Session
说明 管理会话的创建/查询和消息的增删查

方法列表

方法 说明
get_or_create_session(session_id, room_id, user_id, channel) 按 session_id 查询,不存在则创建;session_id 为空时自动生成 sess_ 前缀 ID
get_session(db_session_id) 按数据库主键 ID 查询会话
get_session_by_uuid(session_id) 按 session_id 字符串查询会话
add_message(db_session_id, role, content, agent_code) 添加一条消息(user/assistant)
get_session_messages(db_session_id) 获取会话消息列表,上限 50 条
update_current_agent(db_session_id, agent_code) 更新会话当前绑定的 Agent

会话字段(Session)

字段 类型 说明
id integer 数据库主键
session_id string 会话唯一标识
user_id string 用户ID
channel string 消息渠道,默认 wechat_group
current_agent_code string 当前绑定的 Agent 编码,默认 None
status string 会话状态:active / archived
is_deleted boolean 软删除标记
created_at datetime 创建时间
updated_at datetime 更新时间

3.3 PostReplyHook(回复发送钩子)

基本信息

字段 值
类型 Hook(异步执行,非 HTTP 接口)
依赖 IMSender(消息发送服务)、account_mapping(Agent → 企微账号映射)
说明 Agent 回复后触发,清理 thinking 块并通过 IM 发送消息

输入参数(context 字典)

参数名 类型 必填 说明
agent_name string 是 Agent 编码,用于查找对应企微账号
response_content string 是 Agent 回复原始内容
room_id string 是 目标群ID
mentioned_list string[] 否 @提及的用户列表

处理逻辑

  1. 校验 agent_name 和 response_content 非空,否则跳过(返回 None)
  2. 根据 agent_name 查找 account_mapping,未找到则跳过
  3. 调用 _strip_thinking_blocks() 清理内容中的思考块/工具块
  4. 清理后内容为空则跳过
  5. 调用 im_service.send_message() 发送消息

清理的正则模式

模式 说明
...\n...\n thinking 标签块
<thinking>...</thinking> thinking 标签块
[TOOL_USE]...[/TOOL_USE] 工具调用块
tool... 代码块格式的工具调用

4. 测试场景

4.1 对话补全接口 (POST /api/v1/chat/completions)

序号 场景名称 分类 前置条件 请求参数 预期结果
1 默认路由到 main 正常 无 @提及,session 无继承 query="你好", 无 mentioned_agent_code response_agent_code="main", routing_reason="default"
2 @提及路由到子 Agent 正常 mentioned_agent_code="meeting" query="整理会议纪要", mentioned_agent_code="meeting" response_agent_code="meeting", routing_reason="mention"
3 继承路由:首次 @mention,二次自动继承 正常 同一 session_id 请求1: mentioned_agent_code="meeting";请求2: 无 mention 请求2: response_agent_code="meeting", routing_reason="inheritance"
4 同 session 消息累积 正常 同一 session_id 发送 3 次请求 3 次 query DB 中该 session 共 6 条消息(3 user + 3 assistant)
5 模型调用失败返回 502 异常 Mock _call_model 抛出 AgentHubError query="触发错误" HTTP 502
6 缺少必填字段返回 422 异常 仅传 session_id json={"session_id": "xxx"} HTTP 422
7 channel 默认值 边界 不传 channel query="测试", 无 channel DB 中 session.channel == "wechat_group"

场景1详细验证点:

  • response_agent_code == "main"
  • routing_reason == "default"
  • response_content 非空
  • processing_time_ms > 0

场景2详细验证点:

  • response_agent_code == "meeting"
  • routing_reason == "mention"

场景3详细验证点:

  • 请求1:response_agent_code == "meeting", routing_reason == "mention"
  • 请求2:response_agent_code == "meeting", routing_reason == "inheritance"

场景4详细验证点:

  • 同一 session_id sess_api_004 发送 3 次请求
  • DB 查询 Session + SessionMessage,共 6 条消息
  • 3 条 role="user" + 3 条 role="assistant"

4.2 SessionService 单元测试

序号 场景名称 分类 前置条件 操作 预期结果
1 创建新会话 正常 DB 无该 session_id get_or_create_session("new_sess_001", "room_001", "user_001") session_id="new_sess_001", channel="wechat_group", status="active", is_deleted=False
2 获取已存在会话 正常 DB 已有该 session_id 第二次调用 get_or_create_session("dup_sess", ...) 返回同一 session(id 相同)
3 session_id 为空自动生成 边界 session_id="" get_or_create_session("", "room_001", "user_001") session_id 以 "sess_" 开头,长度 > 5
4 添加并检索消息 正常 会话已创建 add_message(user) + add_message(assistant) 消息列表长度=2,role/content 正确
5 消息裁剪到上限 50 条 边界 插入 55 条消息 get_session_messages() 返回 50 条消息
6 按 session_id 查询会话 正常 DB 中存在记录 get_session_by_uuid("uuid_test_001") 返回对应 session;不存在的返回 None
7 更新当前 Agent 正常 会话刚创建,current_agent_code=None update_current_agent(session.id, "meeting") 更新后 current_agent_code=="meeting"

场景1详细验证点:

  • session.session_id == "new_sess_001"
  • session.room_id == "room_001"
  • session.user_id == "user_001"
  • session.channel == "wechat_group"
  • session.status == "active"
  • session.is_deleted == False

场景5补充说明:

  • SQLite 的 func.now() 精度为秒级,created_at 相同时排序不确定
  • 实际 MySQL 环境中按真实时间戳排序,行为正确

4.3 PostReplyHook 单元测试

序号 场景名称 分类 前置条件 输入 context 预期结果
1 清理 thinking 块后发送 正常 agent_name 在映射中 agent_name="main", response_content="正常回复<thinking>内部思考</thinking>继续回复" 发送内容不含 <thinking>,包含"正常回复"和"继续回复"
2 缺少 agent_name 跳过 边界 — 无 agent_name,有 response_content 返回 None,不调用 send_message
3 缺少 response_content 跳过 边界 — 有 agent_name,无 response_content 返回 None,不调用 send_message
4 Agent 未在映射中跳过 边界 account_mapping 仅含 "main" agent_name="unknown_agent" 返回 None,不调用 send_message
5 清理后内容为空跳过 边界 — response_content="<thinking>只有思考</thinking>" 返回 None,不调用 send_message

场景1详细验证点:

  • send_message 被调用一次
  • 传入的 content 中不包含 <thinking> 和 </thinking>
  • 传入的 content 包含"正常回复"和"继续回复"
  • 传入的 content 不包含"内部思考"

4.4 AgentHub 路由逻辑测试

序号 场景名称 分类 前置条件 操作 预期结果
1 process_message 返回完整路由元数据 正常 Mock _call_model 返回固定内容 process_message(session_id, room_id, user_id, user_query) 返回包含 session_id, response_agent_code, routing_reason, response_content, processing_time_ms
2 同 session 多次调用消息累积 正常 同一 session_id 2 次 process_message DB 中该 session 共 4 条消息(2 user + 2 assistant)

场景1详细验证点:

  • 返回字典包含 session_id 键
  • response_agent_code == "main"
  • routing_reason == "default"
  • response_content == "回复内容"
  • processing_time_ms 为 float 类型且 >= 0

5. 测试数据说明

5.1 AgentHub Mock 数据

Mock 对象 方法 返回值 用途
_call_model — "你好,我是智能助手,有什么可以帮助你的?" 等固定字符串 替代真实 LLM 调用
_call_model(异常) — AgentHubError("模型API返回错误: 500") 测试模型调用失败
im_service.send_message — {"code": 200} 模拟企微消息发送成功

5.2 数据库测试数据

Session 表

session_id user_id room_id channel 用途
sess_api_001 ~ 005 user_001 room_001 wechat_group API 集成测试
new_sess_001 user_001 room_001 wechat_group 会话创建测试
dup_sess user_001/002 room_001/002 wechat_group 会话去重测试
sess_api_003 user_001 room_001 wechat_group 继承路由测试(同 session 两次请求)

SessionMessage 表

session_id 消息数 用途
sess_api_004 6(3 user + 3 assistant) 消息累积测试
trim_sess 55 → 裁剪为 50 消息裁剪测试

5.3 PostReplyHook 测试数据

account_mapping agent_name response_content 预期行为
{"main": "bot_main_001"} main 正常回复<thinking>内部思考</thinking>继续回复 清理后发送
{"main": "bot_main_001"} — 内容 跳过
{"main": "bot_main_001"} main — 跳过
{"main": "bot_main_001"} unknown_agent 回复 跳过(未映射)
{"main": "bot_main_001"} main <thinking>只有思考</thinking> 跳过(清理后为空)

6. 对应测试代码

测试文件:backend/tests/test_agent_hub.py

测试类 测试方法 对应场景
TestChatCompletionsAPI test_default_routing_to_main 4.1-场景1
TestChatCompletionsAPI test_mention_routing_to_sub_agent 4.1-场景2
TestChatCompletionsAPI test_inheritance_routing 4.1-场景3
TestChatCompletionsAPI test_messages_accumulate_in_session 4.1-场景4
TestChatCompletionsAPI test_model_error_returns_502 4.1-场景5
TestChatCompletionsAPI test_missing_required_fields 4.1-场景6
TestChatCompletionsAPI test_channel_default_value 4.1-场景7
TestSessionService test_create_new_session 4.2-场景1
TestSessionService test_get_existing_session 4.2-场景2
TestSessionService test_auto_generate_session_id_when_empty 4.2-场景3
TestSessionService test_add_and_retrieve_messages 4.2-场景4
TestSessionService test_trim_messages_to_max_50 4.2-场景5
TestSessionService test_get_session_by_uuid 4.2-场景6
TestSessionService test_update_current_agent 4.2-场景7
TestPostReplyHook test_strips_thinking_blocks_before_send 4.3-场景1
TestPostReplyHook test_skips_when_agent_name_missing 4.3-场景2
TestPostReplyHook test_skips_when_response_content_missing 4.3-场景3
TestPostReplyHook test_skips_for_unmapped_agent 4.3-场景4
TestPostReplyHook test_skips_when_content_empty_after_strip 4.3-场景5
TestAgentHubRouting test_process_message_returns_routing_metadata 4.4-场景1
TestAgentHubRouting test_preserves_session_across_calls 4.4-场景2