Pulastya B commited on
Commit
ecb7263
Β·
1 Parent(s): 942a6fa

Simplify SSE: UUID-only dependency, remove isTyping gate. Event replay architecture

Browse files
FRRONTEEEND/components/ChatInterface.tsx CHANGED
@@ -61,23 +61,19 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
61
  }
62
  }, [activeSession.messages, isTyping]);
63
 
64
- // Connect to SSE when workflow starts, disconnect when it completes
65
  useEffect(() => {
66
- // Only connect SSE if we have a backend UUID (contains hyphens), not the initial UI session '1'
67
- const hasBackendUUID = activeSessionId && activeSessionId.includes('-');
68
-
69
- if (!isTyping || !hasBackendUUID) {
70
- // Close SSE connection when workflow completes or no backend UUID yet
71
- if (eventSourceRef.current) {
72
- console.log('πŸ”Œ Closing SSE connection');
73
- eventSourceRef.current.close();
74
- eventSourceRef.current = null;
75
- }
76
- setCurrentStep('');
77
  return;
78
  }
79
 
80
- // Connect to SSE stream with actual backend UUID
 
 
 
 
 
81
  const API_URL = window.location.origin;
82
  console.log(`πŸ”Œ Connecting SSE to session: ${activeSessionId}`);
83
  const eventSource = new EventSource(`${API_URL}/api/progress/stream/${activeSessionId}`);
@@ -122,7 +118,7 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
122
 
123
  eventSourceRef.current = eventSource;
124
 
125
- // Cleanup on unmount or when isTyping changes to false
126
  return () => {
127
  if (eventSourceRef.current) {
128
  console.log('🧹 Cleaning up SSE connection');
@@ -130,7 +126,7 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
130
  eventSourceRef.current = null;
131
  }
132
  };
133
- }, [isTyping, activeSessionId]);
134
 
135
  const handleSend = async () => {
136
  if ((!input.trim() && !uploadedFile) || isTyping) return;
@@ -146,7 +142,7 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
146
  const newMessages = [...activeSession.messages, userMessage];
147
  updateSession(activeSessionId, newMessages);
148
  setInput('');
149
- setIsTyping(true);
150
 
151
  try {
152
 
@@ -213,7 +209,7 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
213
 
214
  const data = await response.json();
215
 
216
- // CRITICAL: Store the actual session UUID returned from backend for SSE routing
217
  if (data.session_id && data.session_id !== activeSessionId) {
218
  console.log(`πŸ”‘ Session UUID from backend: ${data.session_id}`);
219
 
@@ -232,6 +228,9 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
232
  setActiveSessionId(data.session_id);
233
  }
234
 
 
 
 
235
  let assistantContent = '';
236
  let reports: Array<{name: string, path: string}> = [];
237
  let plots: Array<{title: string, url: string, type?: 'image' | 'html'}> = [];
 
61
  }
62
  }, [activeSession.messages, isTyping]);
63
 
64
+ // Connect to SSE when we receive a valid backend UUID
65
  useEffect(() => {
66
+ // Only connect if we have a backend UUID (contains hyphens)
67
+ if (!activeSessionId || !activeSessionId.includes('-')) {
 
 
 
 
 
 
 
 
 
68
  return;
69
  }
70
 
71
+ // Don't reconnect if already connected to this session
72
+ if (eventSourceRef.current) {
73
+ return;
74
+ }
75
+
76
+ // Connect to SSE stream - will receive history + any new events
77
  const API_URL = window.location.origin;
78
  console.log(`πŸ”Œ Connecting SSE to session: ${activeSessionId}`);
79
  const eventSource = new EventSource(`${API_URL}/api/progress/stream/${activeSessionId}`);
 
118
 
119
  eventSourceRef.current = eventSource;
120
 
121
+ // Cleanup on unmount or session change
122
  return () => {
123
  if (eventSourceRef.current) {
124
  console.log('🧹 Cleaning up SSE connection');
 
126
  eventSourceRef.current = null;
127
  }
128
  };
129
+ }, [activeSessionId]);
130
 
131
  const handleSend = async () => {
132
  if ((!input.trim() && !uploadedFile) || isTyping) return;
 
142
  const newMessages = [...activeSession.messages, userMessage];
143
  updateSession(activeSessionId, newMessages);
144
  setInput('');
145
+ // DON'T set isTyping yet - wait until we have UUID from backend
146
 
147
  try {
148
 
 
209
 
210
  const data = await response.json();
211
 
212
+ // CRITICAL: Get UUID from backend FIRST, then start SSE
213
  if (data.session_id && data.session_id !== activeSessionId) {
214
  console.log(`πŸ”‘ Session UUID from backend: ${data.session_id}`);
215
 
 
228
  setActiveSessionId(data.session_id);
229
  }
230
 
231
+ // NOW start SSE - after we have the UUID
232
+ setIsTyping(true);
233
+
234
  let assistantContent = '';
235
  let reports: Array<{name: string, path: string}> = [];
236
  let plots: Array<{title: string, url: string, type?: 'image' | 'html'}> = [];