feat: Implement Server-Sent Events (SSE) for long-running workflows
Backend: - workflow_executor.py: Add progress_callback parameter, emit events for execution_started, node_complete, execution_complete, execution_failed - prompt_executor.py: Thread progress_callback through execute chain - routers/prompts.py: New /execute-stream endpoint with asyncio Queue for SSE Frontend: - utils/api.js: New executeUnifiedPromptStream() function with EventSource - pages/Analysis.jsx: Use SSE with live progress display (X/Y Nodes) Fixes: - No more gateway timeouts for complex workflows (10+ nodes) - Live progress feedback for users - Unlimited workflow complexity Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
790e6df8ef
commit
ba474b0a57
5 files changed
+304
-12
No files matched your search
@@ -42,7 +42,8 @@ async def execute_workflow(
|
||||
profile_id: str = None,
|
||||
variables: Dict[str, Any] = None,
|
||||
openrouter_call_func = None, # Callback für LLM-Calls: async (prompt, model) -> str
|
||||
enable_debug: bool = False
|
||||
enable_debug: bool = False,
|
||||
progress_callback = None # NEW: Optional callback für Progress-Updates: async (event_type, data) -> None
|
||||
) -> ExecutionResult:
|
||||
"""
|
||||
Führt einen Workflow aus (mit conditional branching und path consolidation).
|
||||
@@ -76,6 +77,13 @@ async def execute_workflow(
|
||||
|
||||
logger.info(f"Starting workflow execution: {execution_id}")
|
||||
|
||||
# NEW: Progress-Callback für Start
|
||||
if progress_callback:
|
||||
await progress_callback("execution_started", {
|
||||
"execution_id": execution_id,
|
||||
"started_at": started_at
|
||||
})
|
||||
|
||||
try:
|
||||
# 1. Lade Workflow-Definition
|
||||
if graph_data:
|
||||
@@ -161,6 +169,18 @@ async def execute_workflow(
|
||||
node_states.append(node_state)
|
||||
context["node_results"][node_id] = node_state
|
||||
|
||||
# NEW: Progress-Callback aufrufen (für SSE Streaming)
|
||||
if progress_callback:
|
||||
await progress_callback("node_complete", {
|
||||
"node_id": node_id,
|
||||
"node_type": node.type,
|
||||
"node_label": node.label,
|
||||
"status": node_state.status.value,
|
||||
"total_nodes": len(graph.nodes),
|
||||
"completed_nodes": len([ns for ns in node_states if ns.status in [NodeStatus.COMPLETED, NodeStatus.SKIPPED]]),
|
||||
"error": node_state.error if node_state.status == NodeStatus.FAILED else None
|
||||
})
|
||||
|
||||
# Füge Nachfolger zur Queue hinzu
|
||||
outgoing_edges = [e for e in graph.edges if e.from_node == node_id]
|
||||
for edge in outgoing_edges:
|
||||
@@ -185,6 +205,19 @@ async def execute_workflow(
|
||||
|
||||
logger.info(f"Workflow execution completed: {execution_id}")
|
||||
|
||||
# NEW: Progress-Callback für erfolgreiche Fertigstellung
|
||||
if progress_callback:
|
||||
await progress_callback("execution_complete", {
|
||||
"execution_id": execution_id,
|
||||
"status": "completed",
|
||||
"aggregated_result": aggregated,
|
||||
"total_nodes": len(node_states),
|
||||
"completed_nodes": len([ns for ns in node_states if ns.status == NodeStatus.COMPLETED]),
|
||||
"skipped_nodes": len([ns for ns in node_states if ns.status == NodeStatus.SKIPPED]),
|
||||
"failed_nodes": len([ns for ns in node_states if ns.status == NodeStatus.FAILED]),
|
||||
"completed_at": completed_at
|
||||
})
|
||||
|
||||
return ExecutionResult(
|
||||
execution_id=execution_id,
|
||||
workflow_id=workflow_id or "N/A", # Placeholder when graph_data is used directly
|
||||
@@ -198,6 +231,15 @@ async def execute_workflow(
|
||||
except Exception as e:
|
||||
logger.error(f"Workflow execution failed: {e}", exc_info=True)
|
||||
|
||||
# NEW: Progress-Callback für Fehler
|
||||
if progress_callback:
|
||||
await progress_callback("execution_failed", {
|
||||
"execution_id": execution_id,
|
||||
"status": "failed",
|
||||
"error": str(e),
|
||||
"completed_at": datetime.utcnow().isoformat()
|
||||
})
|
||||
|
||||
# Speichere Failed State
|
||||
completed_at = datetime.utcnow().isoformat()
|
||||
save_execution_state(
|
||||
|
||||
Reference in new issue
Block a user