Fix progress tracking synchronization issues

- Pre-generate sessionId on client side before API call to avoid race conditions
- Add small delays in progress tracker to ensure EventSource connection is established
- Improve logging and error handling in progress streaming
- Add connection confirmation messages in EventSource stream
- Fix TypeScript interface to include sessionId in AnalysisProgress

This should resolve the lag between actual analysis progress and progress bar display.
This commit is contained in:
mindesbunister
2025-07-17 12:10:47 +02:00
parent 7e8f033bb2
commit 0399103f8a
4 changed files with 45 additions and 13 deletions

View File

@@ -12,21 +12,30 @@ export async function GET(
const stream = new ReadableStream({
start(controller) {
console.log(`🔍 Starting EventSource stream for session: ${sessionId}`)
// Send initial progress if session exists
const initialProgress = progressTracker.getProgress(sessionId)
if (initialProgress) {
console.log(`🔍 Sending initial progress for ${sessionId}:`, initialProgress.currentStep)
const data = `data: ${JSON.stringify(initialProgress)}\n\n`
controller.enqueue(encoder.encode(data))
} else {
// Send a connection established message even if no progress yet
const connectMsg = `data: ${JSON.stringify({ type: 'connected', sessionId })}\n\n`
controller.enqueue(encoder.encode(connectMsg))
}
// Listen for progress updates
const progressHandler = (progress: any) => {
console.log(`🔍 Streaming progress update for ${sessionId}:`, progress.currentStep)
const data = `data: ${JSON.stringify(progress)}\n\n`
controller.enqueue(encoder.encode(data))
}
// Listen for completion
const completeHandler = () => {
console.log(`🔍 Streaming completion for ${sessionId}`)
const data = `data: ${JSON.stringify({ type: 'complete' })}\n\n`
controller.enqueue(encoder.encode(data))
controller.close()