feat: rewrite错误堆栈,回滚run_workflow

This commit is contained in:
ai-business-hql
2025-07-29 11:22:02 +08:00
parent 096713bcef
commit 858488978b
2 changed files with 91 additions and 19 deletions
+86 -16
View File
@@ -38,6 +38,35 @@ load_env_config()
set_default_openai_api("chat_completions")
set_tracing_disabled(True)
# @function_tool
# def run_workflow(session_id: str) -> str:
# """验证当前session的工作流并返回结果"""
# try:
# workflow_data = get_workflow_data(session_id)
# if not workflow_data:
# return json.dumps({"error": "No workflow data found for this session"})
# print(f"Run workflow for session {session_id}")
# # 使用 ComfyGateway 调用 server.py 的 post_prompt 逻辑
# from ..utils.comfy_gateway import ComfyGateway
# gateway = ComfyGateway()
# # 准备请求数据格式(与server.py post_prompt接口一致)
# request_data = {
# "prompt": workflow_data,
# "client_id": f"debug_agent_{session_id}"
# }
# # 调用ComfyGateway.run_prompt,它会直接复用server.py的post_prompt逻辑
# result = gateway.run_prompt(request_data)
# return json.dumps(result)
# except Exception as e:
# return json.dumps({"error": f"Failed to run workflow: {str(e)}"})
@function_tool
def run_workflow(session_id: str) -> str:
"""验证当前session的工作流并返回结果"""
@@ -46,25 +75,66 @@ def run_workflow(session_id: str) -> str:
if not workflow_data:
return json.dumps({"error": "No workflow data found for this session"})
print(f"Run workflow for session {session_id}")
print(f"Validating workflow for session {session_id}")
# 使用 ComfyGateway 调用 server.py 的 post_prompt 逻辑
from ..utils.comfy_gateway import ComfyGateway
# 直接调用 ComfyUI 的内部验证函数,避免 HTTP 请求
try:
# 调用工作流验证
valid = execution.validate_prompt(workflow_data)
if valid[0]: # 验证成功
# valid = (True, None, outputs_to_execute, node_errors)
prompt_id = str(uuid.uuid4())
result = {
"success": True,
"prompt_id": prompt_id,
"outputs_to_execute": valid[2],
"node_errors": valid[3],
"message": "Workflow validation successful"
}
print(f"Workflow validation successful for session {session_id}")
return json.dumps(result)
else:
# valid = (False, error, outputs_to_execute, node_errors)
error_info = valid[1] # 错误信息
node_errors = valid[3] # 节点错误
result = {
"success": False,
"error": error_info,
"node_errors": node_errors,
"message": "Workflow validation failed"
}
print(f"Workflow validation failed for session {session_id}: {error_info}")
return json.dumps(result)
except Exception as e:
print(f"Exception during workflow validation: {str(e)}")
import traceback
traceback.print_exc()
return json.dumps({
"success": False,
"error": {
"type": "validation_exception",
"message": f"Exception during validation: {str(e)}",
"details": str(e)
},
"node_errors": {}
})
gateway = ComfyGateway()
# 准备请求数据格式(与server.py post_prompt接口一致)
request_data = {
"prompt": workflow_data,
"client_id": f"debug_agent_{session_id}"
}
# 调用ComfyGateway.run_prompt,它会直接复用server.py的post_prompt逻辑
result = gateway.run_prompt(request_data)
return json.dumps(result)
except Exception as e:
return json.dumps({"error": f"Failed to run workflow: {str(e)}"})
print(f"Error in run_workflow: {str(e)}")
import traceback
traceback.print_exc()
return json.dumps({
"success": False,
"error": {
"type": "function_error",
"message": f"Failed to validate workflow: {str(e)}",
"details": str(e)
},
"node_errors": {}
})
@function_tool
def analyze_error_type(error_data: str) -> str:
+5 -3
View File
@@ -246,7 +246,7 @@ You must adhere to the following constraints to complete the task:
except (json.JSONDecodeError, TypeError) as e:
# If not JSON or parsing fails, treat as regular text
print(f"-- Failed to parse tool output as JSON: {e}")
print(f"-- Full traceback: {traceback.format_exc()}")
print(f"-- Traceback: {traceback.format_exc()}")
tool_results[tool_name] = {
"answer": tool_output_data_str,
"data": None,
@@ -280,12 +280,13 @@ You must adhere to the following constraints to complete the task:
except (json.JSONDecodeError, TypeError) as e:
# Not JSON or no ext field, continue normally
print(f"-- Message not JSON or no ext field: {e}")
print(f"-- Full traceback: {traceback.format_exc()}")
print(f"-- Traceback: {traceback.format_exc()}")
pass
else:
pass # Ignore other event types
except Exception as stream_error:
print(f"Stream error (retrying once): {stream_error}")
print(f"Traceback: {traceback.format_exc()}")
# Simple one-time retry for streaming errors
try:
async for event in result.stream_events():
@@ -294,6 +295,7 @@ You must adhere to the following constraints to complete the task:
continue # Just consume events on retry, don't process
except Exception as retry_error:
print(f"Retry failed: {retry_error}")
print(f"Traceback: {traceback.format_exc()}")
# Continue to normal processing, error will be handled by outer try-catch
print("\n=== MCP Agent Run complete ===")
@@ -438,7 +440,7 @@ You must adhere to the following constraints to complete the task:
except Exception as e:
print(f"Error in comfyui_agent_invoke: {str(e)}")
print(f"Full traceback: {traceback.format_exc()}")
print(f"Traceback: {traceback.format_exc()}")
error_message = f"I apologize, but an error occurred while processing your request: {str(e)}"
# Yield error as tuple with finished=True
error_ext = {