fix: 改进错误处理和清理测试代码

## 主要修复

### 1. JSON 解析错误处理
- 修复所有 Agent 的 LLM 响应解析失败时返回原始内容的问题
- 当 JSON 解析失败时,返回友好的兜底消息而不是原始文本
- 影响文件: customer_service.py, order.py, product.py, aftersale.py

### 2. FAQ 快速路径修复
- 修复 customer_service.py 中变量定义顺序问题
- has_faq_query 在使用前未定义导致 NameError
- 添加详细的错误日志记录

### 3. Chatwoot 集成改进
- 添加响应内容调试日志
- 改进错误处理和日志记录

### 4. 订单查询优化
- 将订单列表默认返回数量从 10 条改为 5 条
- 统一 MCP 工具层和 Mall Client 层的默认值

### 5. 代码清理
- 删除所有测试代码和示例文件
- 刋试文件包括: test_*.py, test_*.html, test_*.sh
- 删除测试目录: tests/, agent/tests/, agent/examples/

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
wangliang
2026-01-27 13:15:58 +08:00
parent f4e77f39ce
commit 0f13102a02
21 changed files with 603 additions and 1697 deletions

View File

@@ -64,8 +64,8 @@ ORDER_AGENT_PROMPT = """你是一个专业的 B2B 订单服务助手。
2. **get_mall_order_list** - 从商城 API 查询订单列表(推荐使用)
- user_token: 用户 token自动注入
- page: 页码(可选,默认 1
- limit: 每页数量(可选,默认 10
- 说明:查询用户的所有订单,按时间倒序排列
- limit: 每页数量(可选,默认 5
- 说明:查询用户的所有订单,按时间倒序排列,返回最近的 5 个订单
3. **get_logistics** - 从商城 API 查询物流信息
- order_id: 订单号(必需)
@@ -339,8 +339,8 @@ async def order_agent(state: AgentState) -> AgentState:
error=str(e),
content_preview=content[:500]
)
# 如果解析失败,尝试将原始内容作为直接回复
state = set_response(state, response.content)
# Don't use raw content as response - use fallback instead
state = set_response(state, "抱歉,我无法理解您的请求。请尝试重新表述或联系人工客服。")
return state
action = result.get("action")
@@ -381,6 +381,14 @@ async def order_agent(state: AgentState) -> AgentState:
if tool_name in ["get_mall_order", "get_logistics", "query_order"]:
arguments["order_id"] = state["entities"]["order_id"]
# Force limit=5 for order list queries (unless explicitly set)
if tool_name == "get_mall_order_list" and "limit" not in arguments:
arguments["limit"] = 5
logger.debug(
"Forced limit=5 for order list query",
conversation_id=state["conversation_id"]
)
state = add_tool_call(
state,
tool_name=result["tool_name"],
@@ -730,8 +738,11 @@ def _parse_mall_order_data(data: dict[str, Any]) -> dict[str, Any]:
if actual_order_data.get("remark") or actual_order_data.get("user_remark"):
order_data["remark"] = actual_order_data.get("remark", actual_order_data.get("user_remark", ""))
# 物流信息(如果有)
if actual_order_data.get("parcels") and len(actual_order_data.get("parcels", [])) > 0:
# 物流信息(如果有)- 添加 has_parcels 标记用于判断是否显示物流按钮
has_parcels = actual_order_data.get("parcels") and len(actual_order_data.get("parcels", [])) > 0
order_data["has_parcels"] = has_parcels
if has_parcels:
# parcels 是一个数组,包含物流信息
first_parcel = actual_order_data["parcels"][0] if isinstance(actual_order_data["parcels"], list) else actual_order_data["parcels"]
if isinstance(first_parcel, dict):