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
@@ -338,6 +338,8 @@ export default function Analysis() {
|
||||
/** Kategorie-Schlüssel aus `buildPipelineGroups` (Navigation); Detail = alle Pipelines dieser Kategorie */
|
||||
const [activeCategoryKey, setActiveCategoryKey] = useState(null)
|
||||
const [historyScopePick, setHistoryScopePick] = useState(null)
|
||||
// NEW: Progress tracking for SSE workflows
|
||||
const [progress, setProgress] = useState(null) // { total_nodes, completed_nodes, current_node_label }
|
||||
|
||||
const loadAll = async () => {
|
||||
const [p, i] = await Promise.all([
|
||||
@@ -377,10 +379,21 @@ export default function Analysis() {
|
||||
}, [newResult?.scope, prompts])
|
||||
|
||||
const runPrompt = async (slug) => {
|
||||
setLoading(slug); setError(null); setNewResult(null)
|
||||
setLoading(slug); setError(null); setNewResult(null); setProgress(null)
|
||||
try {
|
||||
// Use new unified executor with save=true
|
||||
const result = await api.executeUnifiedPrompt(slug, null, null, false, true)
|
||||
// Use SSE-based executor for long-running workflows
|
||||
const result = await api.executeUnifiedPromptStream(slug, null, null, false, true, (event) => {
|
||||
// Progress callback: update UI in real-time
|
||||
if (event.type === 'execution_started') {
|
||||
setProgress({ total_nodes: 0, completed_nodes: 0, current_node_label: 'Starte...' })
|
||||
} else if (event.type === 'node_complete') {
|
||||
setProgress({
|
||||
total_nodes: event.total_nodes || 0,
|
||||
completed_nodes: event.completed_nodes || 0,
|
||||
current_node_label: event.node_label || `Node ${event.node_id}`
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
// Transform result to match old format for InsightCard
|
||||
let content = ''
|
||||
@@ -434,7 +447,10 @@ export default function Analysis() {
|
||||
setTab('run')
|
||||
} catch(e) {
|
||||
setError('Fehler: ' + e.message)
|
||||
} finally { setLoading(null) }
|
||||
} finally {
|
||||
setLoading(null)
|
||||
setProgress(null) // Clear progress
|
||||
}
|
||||
}
|
||||
|
||||
const deleteInsight = async (id) => {
|
||||
@@ -618,7 +634,9 @@ export default function Analysis() {
|
||||
disabled={!!loading||!canUseAI||(aiUsage && !aiUsage.allowed)}
|
||||
>
|
||||
{loading===p.slug
|
||||
? <><div className="spinner" style={{width:13,height:13}}/> Läuft…</>
|
||||
? (progress
|
||||
? <><div className="spinner" style={{width:13,height:13}}/> {progress.completed_nodes}/{progress.total_nodes} Nodes</>
|
||||
: <><div className="spinner" style={{width:13,height:13}}/> Läuft…</>)
|
||||
: (aiUsage && !aiUsage.allowed) ? '🔒 Limit'
|
||||
: <><Brain size={13}/> Starten</>}
|
||||
</button>
|
||||
|
||||
@@ -402,6 +402,72 @@ export const api = {
|
||||
return req('/prompts/execute?' + params, json(body))
|
||||
},
|
||||
|
||||
// NEW: SSE-based execution for long-running workflows
|
||||
executeUnifiedPromptStream: (slug, modules=null, timeframes=null, debug=false, save=false, onProgress=null) => {
|
||||
const params = new URLSearchParams({ prompt_slug: slug })
|
||||
if (debug) params.append('debug', 'true')
|
||||
if (save) params.append('save', 'true')
|
||||
|
||||
// TODO: Security improvement - use session cookie instead of token in URL
|
||||
// For now, send token as query param since EventSource doesn't support custom headers
|
||||
const token = localStorage.getItem('token')
|
||||
if (token) params.append('token', token)
|
||||
|
||||
if (modules) {
|
||||
Object.entries(modules).forEach(([k, v]) => params.append(`modules[${k}]`, v))
|
||||
}
|
||||
if (timeframes) {
|
||||
Object.entries(timeframes).forEach(([k, v]) => params.append(`timeframes[${k}]`, v))
|
||||
}
|
||||
|
||||
// Return a Promise that resolves with final result
|
||||
return new Promise((resolve, reject) => {
|
||||
const url = `${BASE_URL}/prompts/execute-stream?${params}`
|
||||
|
||||
const eventSource = new EventSource(url)
|
||||
|
||||
let finalResult = null
|
||||
|
||||
eventSource.onmessage = (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data)
|
||||
|
||||
// Call progress callback if provided
|
||||
if (onProgress) {
|
||||
onProgress(data)
|
||||
}
|
||||
|
||||
// Check for final result
|
||||
if (data.type === 'execution_complete') {
|
||||
// Transform SSE result to match regular execute format
|
||||
finalResult = {
|
||||
type: 'workflow',
|
||||
execution_id: data.execution_id,
|
||||
status: data.status,
|
||||
aggregated_result: data.aggregated_result,
|
||||
debug: {
|
||||
node_states: [] // TODO: collect from progress events if needed
|
||||
}
|
||||
}
|
||||
eventSource.close()
|
||||
resolve(finalResult)
|
||||
} else if (data.type === 'execution_failed') {
|
||||
eventSource.close()
|
||||
reject(new Error(data.error || 'Workflow execution failed'))
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error parsing SSE event:', e)
|
||||
}
|
||||
}
|
||||
|
||||
eventSource.onerror = (error) => {
|
||||
console.error('SSE connection error:', error)
|
||||
eventSource.close()
|
||||
reject(new Error('Connection to server lost'))
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// Workflow Execution (Part 2: Frontend Execute Integration)
|
||||
executeWorkflow: (slug, variables=null, debug=true, save=false) => {
|
||||
const params = new URLSearchParams({ prompt_slug: slug })
|
||||
|
||||
Reference in new issue
Block a user