fix: stabilize tool error rendering

This commit is contained in:
JOJO 2025-11-26 18:45:47 +08:00
parent b39d7cc795
commit 79a90f4fad
2 changed files with 65 additions and 2 deletions

View File

@ -1378,6 +1378,13 @@ const appOptions = {
toolAction.tool.status = 'completed'; toolAction.tool.status = 'completed';
toolAction.tool.result = result; toolAction.tool.result = result;
if (result && typeof result === 'object') {
if (result.error) {
toolAction.tool.message = result.error;
} else if (result.message && !toolAction.tool.message) {
toolAction.tool.message = result.message;
}
}
if (message.name === 'append_to_file' && result && result.message) { if (message.name === 'append_to_file' && result && result.message) {
toolAction.tool.message = result.message; toolAction.tool.message = result.message;
} }

View File

@ -3872,7 +3872,35 @@ async def handle_task_with_sender(terminal: WebTerminal, message, sender, client
success, arguments, error_msg = web_terminal.api_client._safe_tool_arguments_parse(arguments_str, function_name) success, arguments, error_msg = web_terminal.api_client._safe_tool_arguments_parse(arguments_str, function_name)
if not success: if not success:
debug_log(f"安全解析失败: {error_msg}") debug_log(f"安全解析失败: {error_msg}")
sender('error', {'message': f'工具参数解析失败: {error_msg}'}) error_text = f'工具参数解析失败: {error_msg}'
error_payload = {
"success": False,
"error": error_text,
"error_type": "parameter_format_error",
"tool_name": function_name,
"tool_call_id": tool_call_id,
"message": error_text
}
sender('error', {'message': error_text})
sender('update_action', {
'preparing_id': tool_call_id,
'status': 'completed',
'result': error_payload,
'message': error_text
})
error_content = json.dumps(error_payload, ensure_ascii=False)
web_terminal.context_manager.add_conversation(
"tool",
error_content,
tool_call_id=tool_call_id,
name=function_name
)
messages.append({
"role": "tool",
"tool_call_id": tool_call_id,
"name": function_name,
"content": error_content
})
continue continue
debug_log(f"使用安全解析成功,参数键: {list(arguments.keys())}") debug_log(f"使用安全解析成功,参数键: {list(arguments.keys())}")
else: else:
@ -3912,7 +3940,35 @@ async def handle_task_with_sender(terminal: WebTerminal, message, sender, client
debug_log(f"JSON修复也失败: {repair_error}") debug_log(f"JSON修复也失败: {repair_error}")
debug_log(f"修复尝试: {repair_attempts}") debug_log(f"修复尝试: {repair_attempts}")
debug_log(f"修复后内容前100字符: {repaired_str[:100]}") debug_log(f"修复后内容前100字符: {repaired_str[:100]}")
sender('error', {'message': f'工具参数解析失败: {e}'}) error_text = f'工具参数解析失败: {e}'
error_payload = {
"success": False,
"error": error_text,
"error_type": "parameter_format_error",
"tool_name": function_name,
"tool_call_id": tool_call_id,
"message": error_text
}
sender('error', {'message': error_text})
sender('update_action', {
'preparing_id': tool_call_id,
'status': 'completed',
'result': error_payload,
'message': error_text
})
error_content = json.dumps(error_payload, ensure_ascii=False)
web_terminal.context_manager.add_conversation(
"tool",
error_content,
tool_call_id=tool_call_id,
name=function_name
)
messages.append({
"role": "tool",
"tool_call_id": tool_call_id,
"name": function_name,
"content": error_content
})
continue continue
debug_log(f"执行工具: {function_name} (ID: {tool_call_id})") debug_log(f"执行工具: {function_name} (ID: {tool_call_id})")