backend-api-test-doc-routing.md
6.21 KB
接口测试文档 — routing
1. 模块概览
| 字段 | 值 |
|---|---|
| 模块名称 | routing |
| 模块说明 | 路由与意图识别 — 消息路由(mention/inheritance/default) + 意图识别 + Agent交接 + 文件上传 |
| 路由前缀 | /api/v1 |
| 数据库表 | ai_agents |
| 外部依赖 | LLMClient(意图识别)、SessionStore(会话状态)、FileParser、OSSClient |
2. 接口列表
| 序号 | 方法 | 路径 | 说明 |
|---|---|---|---|
| 1 | POST | /api/v1/chat/completions |
路由决策接口 |
| 2 | POST | /api/v1/files/upload |
文件上传与文本提取 |
3. 接口详细说明
3.1 路由决策接口
基本信息
| 字段 | 值 |
|---|---|
| 方法 | POST |
| 路径 | /api/v1/chat/completions |
| 说明 | 根据mention/继承/默认策略路由消息,执行意图识别和Agent交接 |
| Content-Type | application/json |
请求参数
| 参数名 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| session_id | body | string | 是 | 会话ID,最长64 |
| user_id | body | string | 是 | 用户ID,最长64 |
| channel | body | string | 是 | 消息渠道,最长64 |
| query | body | string | 是 | 用户问题,1~5000字符 |
| mentioned_agent_code | body | string | 否 | @提及的Agent编码 |
| file_url | body | string | 否 | 文件URL |
响应参数
| 字段路径 | 类型 | 说明 |
|---|---|---|
| session_id | string | 会话ID |
| routing | object | 路由信息 |
| routing.agent_id | integer | Agent ID |
| routing.agent_code | string | Agent编码 |
| routing.agent_name | string | Agent名称 |
| routing.routing_reason | string | 路由原因:mention/inheritance/default |
| intent | object | 意图信息(mention时为null) |
| intent.intent | string | 意图名 |
| intent.confidence | float | 置信度 |
| handoff | object | 交接信息(同Agent或首次对话时为null) |
| handoff.from_agent_id | integer | 源Agent ID |
| handoff.to_agent_id | integer | 目标Agent ID |
| handoff.to_agent_name | string | 目标Agent名称 |
| handoff.handoff_message | string | 交接消息 |
| response_content | string | 回复内容 |
| created_at | string | 创建时间 |
错误码
| HTTP 状态码 | 触发条件 |
|---|---|
| 400 | mentioned_agent不存在 / 管家Agent未配置 |
3.2 文件上传与文本提取
基本信息
| 字段 | 值 |
|---|---|
| 方法 | POST |
| 路径 | /api/v1/files/upload |
| Content-Type | multipart/form-data |
请求参数
| 参数名 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| session_id | form | string | 是 | 关联会话ID |
| file | form | file | 是 | 上传文件 |
响应参数
| 字段路径 | 类型 | 说明 |
|---|---|---|
| file_url | string | 文件URL |
| file_name | string | 文件名 |
| file_size | integer | 文件大小 |
| extracted_text | string | 提取的文本(图片为null) |
| session_id | string | 会话ID |
错误码
| HTTP 状态码 | 触发条件 |
|---|---|
| 400 | 不支持的文件格式 / 文件过大 |
4. 测试场景
4.1 路由决策
| 序号 | 场景名称 | 分类 | 预期结果 |
|---|---|---|---|
| 1 | @mention路由到指定Agent | 正常 | agent_code="metric-agent", reason="mention" |
| 2 | 继承路由到当前Agent | 正常 | agent_code="metric-agent", reason="inheritance" |
| 3 | 默认路由到管家Agent | 正常 | agent_code="steward", reason="default" |
| 4 | @mention覆盖继承 | 正常 | reason="mention", agent_code="detail-agent" |
4.2 意图识别
| 序号 | 场景名称 | 分类 | 预期结果 |
|---|---|---|---|
| 1 | 查询类意图 | 正常 | intent="query-metric", confidence>0 |
| 2 | 未知意图归为general-qa | 正常 | intent="general-qa" |
| 3 | @mention跳过意图识别 | 正常 | intent=None |
4.3 Agent交接
| 序号 | 场景名称 | 分类 | 预期结果 |
|---|---|---|---|
| 1 | Agent切换生成交接 | 正常 | handoff非null, 含from/to/handoff_message |
| 2 | 同Agent无交接 | 正常 | handoff=None |
| 3 | 首次对话无交接 | 正常 | handoff=None |
4.4 文件上传
| 序号 | 场景名称 | 分类 | 预期结果 |
|---|---|---|---|
| 1 | PDF上传提取文本 | 正常 | extracted_text非空 |
| 2 | 不支持的格式 | 异常 | ValueError "不支持的文件格式" |
| 3 | 文件过大 | 异常 | ValueError "文件大小超过限制" |
| 4 | 图片上传无文本提取 | 正常 | extracted_text=None |
4.5 集成与异常
| 序号 | 场景名称 | 分类 | 预期结果 |
|---|---|---|---|
| 1 | 默认→mention完整流程 | 集成 | 先default后mention, 生成handoff |
| 2 | mentioned Agent不存在 | 异常 | ValueError "Agent不存在" |
| 3 | 管家Agent未配置 | 异常 | ValueError "管家Agent" |
5. 对应测试代码
测试文件:backend/tests/test_routing_api.py
| 测试类 | 测试方法 | 对应场景 |
|---|---|---|
| TestRoutingDecision | test_mention_routes_to_mentioned_agent | 4.1-1 |
| TestRoutingDecision | test_inheritance_routes_to_current_agent | 4.1-2 |
| TestRoutingDecision | test_default_routes_to_steward | 4.1-3 |
| TestRoutingDecision | test_mention_overrides_inheritance | 4.1-4 |
| TestIntentRecognition | test_intent_recognized_for_query | 4.2-1 |
| TestIntentRecognition | test_intent_general_for_unknown_query | 4.2-2 |
| TestIntentRecognition | test_mention_skips_intent_recognition | 4.2-3 |
| TestHandoff | test_handoff_generated_on_agent_switch | 4.3-1 |
| TestHandoff | test_no_handoff_when_same_agent | 4.3-2 |
| TestHandoff | test_no_handoff_on_default_first_message | 4.3-3 |
| TestFileUpload | test_upload_and_extract_text | 4.4-1 |
| TestFileUpload | test_unsupported_format_returns_error | 4.4-2 |
| TestFileUpload | test_file_too_large_returns_error | 4.4-3 |
| TestFileUpload | test_upload_without_extraction | 4.4-4 |
| TestRoutingServiceIntegration | test_full_flow_default_then_mention | 4.5-1 |
| TestRoutingServiceIntegration | test_mentioned_agent_not_found | 4.5-2 |
| TestRoutingServiceIntegration | test_steward_agent_not_found | 4.5-3 |