🐛 修复工具调用参数解析错误,添加异常处理并记录警告信息

This commit is contained in:
FuQuan233 2026-06-01 14:35:47 +08:00
parent 66f1f71a02
commit 975cc4705d

View file

@ -479,7 +479,20 @@ async def process_messages(context_id: int, is_group: bool = True):
for tool_call in message.tool_calls: for tool_call in message.tool_calls:
tool_name = tool_call.function.name tool_name = tool_call.function.name
try:
tool_args = json.loads(tool_call.function.arguments) tool_args = json.loads(tool_call.function.arguments)
except (json.JSONDecodeError, TypeError, ValueError) as e:
error_message = (
f"工具调用参数格式错误,无法解析 {tool_name} 的 arguments: {e!s}. "
f"原始参数: {tool_call.function.arguments}"
)
logger.warning(error_message)
new_messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": error_message,
})
continue
# 发送工具调用提示 # 发送工具调用提示
await handler.send(Message(f"正在使用{mcp_client.get_friendly_name(tool_name)}")) await handler.send(Message(f"正在使用{mcp_client.get_friendly_name(tool_name)}"))