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
@@ -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