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:
@@ -55,7 +55,6 @@ class MallClient:
|
||||
"""
|
||||
if self._client is None:
|
||||
default_headers = {
|
||||
"Authorization": f"Bearer {self.api_token}",
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json, text/plain, */*",
|
||||
"Device-Type": "pc",
|
||||
@@ -70,6 +69,10 @@ class MallClient:
|
||||
"DNT": "1",
|
||||
}
|
||||
|
||||
# 只有在有 token 时才添加 Authorization header
|
||||
if self.api_token:
|
||||
default_headers["Authorization"] = f"Bearer {self.api_token}"
|
||||
|
||||
# 合并额外的 headers(用于 Authorization2 等)
|
||||
if extra_headers:
|
||||
default_headers.update(extra_headers)
|
||||
@@ -131,6 +134,14 @@ class MallClient:
|
||||
json=json,
|
||||
headers=request_headers
|
||||
)
|
||||
|
||||
# Debug logging for product search
|
||||
if "/spu" in endpoint:
|
||||
print(f"[DEBUG MallClient] Request: {method} {endpoint}")
|
||||
print(f"[DEBUG MallClient] Params: {params}")
|
||||
print(f"[DEBUG MallClient] Response URL: {response.url}")
|
||||
print(f"[DEBUG MallClient] Response Status: {response.status_code}")
|
||||
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
@@ -197,14 +208,14 @@ class MallClient:
|
||||
async def get_order_list(
|
||||
self,
|
||||
page: int = 1,
|
||||
limit: int = 10,
|
||||
limit: int = 5,
|
||||
customer_id: int = 0,
|
||||
order_types: Optional[list[int]] = None,
|
||||
shipping_status: int = 10000,
|
||||
date_added: Optional[str] = None,
|
||||
date_end: Optional[str] = None,
|
||||
no: Optional[str] = None,
|
||||
status: Optional[int] = None,
|
||||
status: int = 10000,
|
||||
is_drop_shopping: int = 0
|
||||
) -> dict[str, Any]:
|
||||
"""Query order list with filters
|
||||
@@ -213,14 +224,14 @@ class MallClient:
|
||||
|
||||
Args:
|
||||
page: 页码 (default: 1)
|
||||
limit: 每页数量 (default: 10)
|
||||
limit: 每页数量 (default: 5)
|
||||
customer_id: 客户ID (default: 0)
|
||||
order_types: 订单类型数组,如 [1, 2] (default: None)
|
||||
shipping_status: 物流状态 (default: 10000)
|
||||
shipping_status: 物流状态 (default: 10000, 10000表示全部状态)
|
||||
date_added: 开始日期,格式 YYYY-MM-DD (default: None)
|
||||
date_end: 结束日期,格式 YYYY-MM-DD (default: None)
|
||||
no: 订单号 (default: None)
|
||||
status: 订单状态 (default: None)
|
||||
status: 订单状态 (default: 10000, 10000表示全部状态)
|
||||
is_drop_shopping: 是否代发货 (default: 0)
|
||||
|
||||
Returns:
|
||||
@@ -239,6 +250,7 @@ class MallClient:
|
||||
"limit": limit,
|
||||
"customerId": customer_id,
|
||||
"shippingStatus": shipping_status,
|
||||
"status": status,
|
||||
"isDropShopping": is_drop_shopping
|
||||
}
|
||||
|
||||
@@ -253,8 +265,6 @@ class MallClient:
|
||||
params["dateEnd"] = date_end
|
||||
if no:
|
||||
params["no"] = no
|
||||
if status is not None:
|
||||
params["status"] = status
|
||||
|
||||
result = await self.get(
|
||||
"/mall/api/order/list",
|
||||
@@ -265,6 +275,47 @@ class MallClient:
|
||||
except Exception as e:
|
||||
raise Exception(f"查询订单列表失败 (Query order list failed): {str(e)}")
|
||||
|
||||
# ============ Product APIs ============
|
||||
|
||||
async def search_spu_products(
|
||||
self,
|
||||
keyword: str,
|
||||
page_size: int = 5,
|
||||
page: int = 1
|
||||
) -> dict[str, Any]:
|
||||
"""Search SPU products by keyword
|
||||
|
||||
根据关键词搜索商品 SPU
|
||||
|
||||
Args:
|
||||
keyword: 搜索关键词(商品名称、编号等)
|
||||
page_size: 每页数量 (default: 5, max 100)
|
||||
page: 页码 (default: 1)
|
||||
|
||||
Returns:
|
||||
商品列表,包含 SPU 信息、商品图片、价格等
|
||||
Product list including SPU info, images, prices, etc.
|
||||
|
||||
Example:
|
||||
>>> client = MallClient()
|
||||
>>> result = await client.search_spu_products("61607", page_size=60, page=1)
|
||||
>>> print(f"找到 {len(result.get('list', []))} 个商品")
|
||||
"""
|
||||
try:
|
||||
params = {
|
||||
"pageSize": min(page_size, 100), # 限制最大 100
|
||||
"page": page,
|
||||
"keyword": keyword
|
||||
}
|
||||
|
||||
result = await self.get(
|
||||
"/mall/api/spu",
|
||||
params=params
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
raise Exception(f"搜索商品失败 (Search SPU products failed): {str(e)}")
|
||||
|
||||
|
||||
# Global Mall client instance
|
||||
mall_client: Optional[MallClient] = None
|
||||
|
||||
Reference in New Issue
Block a user