|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { type NextRequest, NextResponse } from 'next/server' |
| 3 | +import { checkHybridAuth } from '@/lib/auth/hybrid' |
| 4 | +import { SSE_HEADERS } from '@/lib/core/utils/sse' |
| 5 | +import { |
| 6 | + type ExecutionStreamStatus, |
| 7 | + getExecutionMeta, |
| 8 | + readExecutionEvents, |
| 9 | +} from '@/lib/execution/event-buffer' |
| 10 | +import { formatSSEEvent } from '@/lib/workflows/executor/execution-events' |
| 11 | +import { authorizeWorkflowByWorkspacePermission } from '@/lib/workflows/utils' |
| 12 | + |
| 13 | +const logger = createLogger('ExecutionStreamReconnectAPI') |
| 14 | + |
| 15 | +const POLL_INTERVAL_MS = 500 |
| 16 | + |
| 17 | +function isTerminalStatus(status: ExecutionStreamStatus): boolean { |
| 18 | + return status === 'complete' || status === 'error' || status === 'cancelled' |
| 19 | +} |
| 20 | + |
| 21 | +export const runtime = 'nodejs' |
| 22 | +export const dynamic = 'force-dynamic' |
| 23 | + |
| 24 | +export async function GET( |
| 25 | + req: NextRequest, |
| 26 | + { params }: { params: Promise<{ id: string; executionId: string }> } |
| 27 | +) { |
| 28 | + const { id: workflowId, executionId } = await params |
| 29 | + |
| 30 | + try { |
| 31 | + const auth = await checkHybridAuth(req, { requireWorkflowId: false }) |
| 32 | + if (!auth.success || !auth.userId) { |
| 33 | + return NextResponse.json({ error: auth.error || 'Unauthorized' }, { status: 401 }) |
| 34 | + } |
| 35 | + |
| 36 | + const workflowAuthorization = await authorizeWorkflowByWorkspacePermission({ |
| 37 | + workflowId, |
| 38 | + userId: auth.userId, |
| 39 | + action: 'read', |
| 40 | + }) |
| 41 | + if (!workflowAuthorization.allowed) { |
| 42 | + return NextResponse.json( |
| 43 | + { error: workflowAuthorization.message || 'Access denied' }, |
| 44 | + { status: workflowAuthorization.status } |
| 45 | + ) |
| 46 | + } |
| 47 | + |
| 48 | + const meta = await getExecutionMeta(executionId) |
| 49 | + if (!meta) { |
| 50 | + return NextResponse.json({ error: 'Execution buffer not found or expired' }, { status: 404 }) |
| 51 | + } |
| 52 | + |
| 53 | + if (meta.workflowId && meta.workflowId !== workflowId) { |
| 54 | + return NextResponse.json( |
| 55 | + { error: 'Execution does not belong to this workflow' }, |
| 56 | + { status: 403 } |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + const fromParam = req.nextUrl.searchParams.get('from') |
| 61 | + const fromEventId = fromParam ? Number.parseInt(fromParam, 10) : 0 |
| 62 | + |
| 63 | + logger.info('Reconnection stream requested', { |
| 64 | + workflowId, |
| 65 | + executionId, |
| 66 | + fromEventId, |
| 67 | + metaStatus: meta.status, |
| 68 | + }) |
| 69 | + |
| 70 | + const encoder = new TextEncoder() |
| 71 | + |
| 72 | + const stream = new ReadableStream<Uint8Array>({ |
| 73 | + async start(controller) { |
| 74 | + let lastEventId = fromEventId |
| 75 | + let closed = false |
| 76 | + |
| 77 | + const enqueue = (text: string) => { |
| 78 | + if (closed) return |
| 79 | + try { |
| 80 | + controller.enqueue(encoder.encode(text)) |
| 81 | + } catch { |
| 82 | + closed = true |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + // Replay buffered events |
| 88 | + const events = await readExecutionEvents(executionId, lastEventId) |
| 89 | + for (const entry of events) { |
| 90 | + if (closed) return |
| 91 | + enqueue(formatSSEEvent(entry.event as any)) |
| 92 | + lastEventId = entry.eventId |
| 93 | + } |
| 94 | + |
| 95 | + // Check if execution is already done |
| 96 | + const currentMeta = await getExecutionMeta(executionId) |
| 97 | + if (!currentMeta || isTerminalStatus(currentMeta.status)) { |
| 98 | + enqueue('data: [DONE]\n\n') |
| 99 | + if (!closed) controller.close() |
| 100 | + return |
| 101 | + } |
| 102 | + |
| 103 | + // Poll for new events until execution completes |
| 104 | + while (!closed) { |
| 105 | + await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS)) |
| 106 | + if (closed) return |
| 107 | + |
| 108 | + const newEvents = await readExecutionEvents(executionId, lastEventId) |
| 109 | + for (const entry of newEvents) { |
| 110 | + if (closed) return |
| 111 | + enqueue(formatSSEEvent(entry.event as any)) |
| 112 | + lastEventId = entry.eventId |
| 113 | + } |
| 114 | + |
| 115 | + const polledMeta = await getExecutionMeta(executionId) |
| 116 | + if (!polledMeta || isTerminalStatus(polledMeta.status)) { |
| 117 | + // One final read to catch any events flushed alongside the meta update |
| 118 | + const finalEvents = await readExecutionEvents(executionId, lastEventId) |
| 119 | + for (const entry of finalEvents) { |
| 120 | + if (closed) return |
| 121 | + enqueue(formatSSEEvent(entry.event as any)) |
| 122 | + lastEventId = entry.eventId |
| 123 | + } |
| 124 | + enqueue('data: [DONE]\n\n') |
| 125 | + if (!closed) controller.close() |
| 126 | + return |
| 127 | + } |
| 128 | + } |
| 129 | + } catch (error) { |
| 130 | + logger.error('Error in reconnection stream', { |
| 131 | + executionId, |
| 132 | + error: error instanceof Error ? error.message : String(error), |
| 133 | + }) |
| 134 | + if (!closed) { |
| 135 | + try { |
| 136 | + controller.close() |
| 137 | + } catch {} |
| 138 | + } |
| 139 | + } |
| 140 | + }, |
| 141 | + cancel() { |
| 142 | + logger.info('Client disconnected from reconnection stream', { executionId }) |
| 143 | + }, |
| 144 | + }) |
| 145 | + |
| 146 | + return new NextResponse(stream, { |
| 147 | + headers: { |
| 148 | + ...SSE_HEADERS, |
| 149 | + 'X-Execution-Id': executionId, |
| 150 | + }, |
| 151 | + }) |
| 152 | + } catch (error: any) { |
| 153 | + logger.error('Failed to start reconnection stream', { |
| 154 | + workflowId, |
| 155 | + executionId, |
| 156 | + error: error.message, |
| 157 | + }) |
| 158 | + return NextResponse.json( |
| 159 | + { error: error.message || 'Failed to start reconnection stream' }, |
| 160 | + { status: 500 } |
| 161 | + ) |
| 162 | + } |
| 163 | +} |
0 commit comments