Spaces:
Running
Running
Pulastya B
commited on
Commit
Β·
942a6fa
1
Parent(s):
c14102f
Fix message display: properly handle session migration from UI session to backend UUID
Browse files
FRRONTEEEND/components/ChatInterface.tsx
CHANGED
|
@@ -42,7 +42,7 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
|
|
| 42 |
updatedAt: new Date(),
|
| 43 |
}
|
| 44 |
]);
|
| 45 |
-
const [activeSessionId, setActiveSessionId] = useState<string>(''); //
|
| 46 |
const [input, setInput] = useState('');
|
| 47 |
const [isTyping, setIsTyping] = useState(false);
|
| 48 |
const [currentStep, setCurrentStep] = useState<string>('');
|
|
@@ -63,8 +63,11 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
|
|
| 63 |
|
| 64 |
// Connect to SSE when workflow starts, disconnect when it completes
|
| 65 |
useEffect(() => {
|
| 66 |
-
if (
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
if (eventSourceRef.current) {
|
| 69 |
console.log('π Closing SSE connection');
|
| 70 |
eventSourceRef.current.close();
|
|
@@ -74,7 +77,7 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
|
|
| 74 |
return;
|
| 75 |
}
|
| 76 |
|
| 77 |
-
// Connect to SSE stream with actual
|
| 78 |
const API_URL = window.location.origin;
|
| 79 |
console.log(`π Connecting SSE to session: ${activeSessionId}`);
|
| 80 |
const eventSource = new EventSource(`${API_URL}/api/progress/stream/${activeSessionId}`);
|
|
@@ -211,9 +214,21 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
|
|
| 211 |
const data = await response.json();
|
| 212 |
|
| 213 |
// CRITICAL: Store the actual session UUID returned from backend for SSE routing
|
| 214 |
-
if (data.session_id) {
|
| 215 |
console.log(`π Session UUID from backend: ${data.session_id}`);
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
setActiveSessionId(data.session_id);
|
| 218 |
}
|
| 219 |
|
|
|
|
| 42 |
updatedAt: new Date(),
|
| 43 |
}
|
| 44 |
]);
|
| 45 |
+
const [activeSessionId, setActiveSessionId] = useState<string>('1'); // Start with default session, update to UUID after first API call
|
| 46 |
const [input, setInput] = useState('');
|
| 47 |
const [isTyping, setIsTyping] = useState(false);
|
| 48 |
const [currentStep, setCurrentStep] = useState<string>('');
|
|
|
|
| 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();
|
|
|
|
| 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}`);
|
|
|
|
| 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 |
+
|
| 220 |
+
// Migrate existing session to use backend UUID
|
| 221 |
+
const currentSession = sessions.find(s => s.id === activeSessionId);
|
| 222 |
+
if (currentSession) {
|
| 223 |
+
// Create new session with backend UUID and copy existing messages
|
| 224 |
+
const migratedSession: ChatSession = {
|
| 225 |
+
...currentSession,
|
| 226 |
+
id: data.session_id,
|
| 227 |
+
};
|
| 228 |
+
setSessions(prev => [...prev.filter(s => s.id !== activeSessionId), migratedSession]);
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
// Switch to the backend UUID session
|
| 232 |
setActiveSessionId(data.session_id);
|
| 233 |
}
|
| 234 |
|