sunheycho commited on
Commit
54e2305
Β·
1 Parent(s): 2e645b0

Fix: Update streaming response handling for blank screen issue

Browse files

- Fix frontend JavaScript to handle actual backend SSE data format
- Add debug logging to SSE stream generation
- Frontend now properly processes message, status, final_result, and error data
- Add console logging for debugging SSE data reception

Files changed (1) hide show
  1. api.py +20 -3
api.py CHANGED
@@ -1099,6 +1099,7 @@ def stream_product_comparison(session_id):
1099
 
1100
  def generate():
1101
  """Generate SSE events for streaming"""
 
1102
  coordinator = get_product_comparison_coordinator()
1103
  last_message_index = 0
1104
  retry_count = 0
@@ -1107,17 +1108,22 @@ def stream_product_comparison(session_id):
1107
  while retry_count < max_retries:
1108
  # Get current status
1109
  status = coordinator.get_session_status(session_id)
 
 
1110
  if status is None:
1111
  # Session not found
 
1112
  yield f"data: {json.dumps({'error': 'Session not found'})}\n\n"
1113
  break
1114
 
1115
  # Get all messages
1116
  messages = coordinator.get_session_messages(session_id)
 
1117
 
1118
  # Send any new messages
1119
  if messages and len(messages) > last_message_index:
1120
  new_messages = messages[last_message_index:]
 
1121
  for msg in new_messages:
1122
  yield f"data: {json.dumps({'message': msg})}\n\n"
1123
  last_message_index = len(messages)
@@ -1128,6 +1134,7 @@ def stream_product_comparison(session_id):
1128
  # If completed or error, send final result and end stream
1129
  if status in ['completed', 'error']:
1130
  result = coordinator.get_session_result(session_id)
 
1131
  yield f"data: {json.dumps({'final_result': result})}\n\n"
1132
  break
1133
 
@@ -1137,6 +1144,7 @@ def stream_product_comparison(session_id):
1137
 
1138
  # End the stream if we've reached max retries
1139
  if retry_count >= max_retries:
 
1140
  yield f"data: {json.dumps({'error': 'Timeout waiting for results'})}\n\n"
1141
 
1142
  return Response(
@@ -1658,11 +1666,20 @@ def product_comparison_lite_page():
1658
  const resultDiv = document.getElementById('result');
1659
  eventSource.onmessage = function(event) {
1660
  const data = JSON.parse(event.data);
1661
- if (data.type === 'log') {
 
 
1662
  logDiv.textContent += data.message + '\\n';
1663
  logDiv.scrollTop = logDiv.scrollHeight;
1664
- } else if (data.type === 'result') {
1665
- resultDiv.textContent = JSON.stringify(data.data, null, 2);
 
 
 
 
 
 
 
1666
  eventSource.close();
1667
  }
1668
  };
 
1099
 
1100
  def generate():
1101
  """Generate SSE events for streaming"""
1102
+ print(f"[DEBUG] 🌊 Starting SSE stream for session: {session_id}")
1103
  coordinator = get_product_comparison_coordinator()
1104
  last_message_index = 0
1105
  retry_count = 0
 
1108
  while retry_count < max_retries:
1109
  # Get current status
1110
  status = coordinator.get_session_status(session_id)
1111
+ print(f"[DEBUG] πŸ“Š Session {session_id} status: {status}")
1112
+
1113
  if status is None:
1114
  # Session not found
1115
+ print(f"[DEBUG] ❌ Session {session_id} not found")
1116
  yield f"data: {json.dumps({'error': 'Session not found'})}\n\n"
1117
  break
1118
 
1119
  # Get all messages
1120
  messages = coordinator.get_session_messages(session_id)
1121
+ print(f"[DEBUG] πŸ“ Session {session_id} has {len(messages) if messages else 0} messages")
1122
 
1123
  # Send any new messages
1124
  if messages and len(messages) > last_message_index:
1125
  new_messages = messages[last_message_index:]
1126
+ print(f"[DEBUG] πŸ“€ Sending {len(new_messages)} new messages")
1127
  for msg in new_messages:
1128
  yield f"data: {json.dumps({'message': msg})}\n\n"
1129
  last_message_index = len(messages)
 
1134
  # If completed or error, send final result and end stream
1135
  if status in ['completed', 'error']:
1136
  result = coordinator.get_session_result(session_id)
1137
+ print(f"[DEBUG] 🏁 Session {session_id} finished with status: {status}")
1138
  yield f"data: {json.dumps({'final_result': result})}\n\n"
1139
  break
1140
 
 
1144
 
1145
  # End the stream if we've reached max retries
1146
  if retry_count >= max_retries:
1147
+ print(f"[DEBUG] ⏰ Session {session_id} timed out after {max_retries} retries")
1148
  yield f"data: {json.dumps({'error': 'Timeout waiting for results'})}\n\n"
1149
 
1150
  return Response(
 
1666
  const resultDiv = document.getElementById('result');
1667
  eventSource.onmessage = function(event) {
1668
  const data = JSON.parse(event.data);
1669
+ console.log('Received SSE data:', data);
1670
+
1671
+ if (data.message) {
1672
  logDiv.textContent += data.message + '\\n';
1673
  logDiv.scrollTop = logDiv.scrollHeight;
1674
+ } else if (data.status) {
1675
+ logDiv.textContent += 'Status: ' + data.status + '\\n';
1676
+ logDiv.scrollTop = logDiv.scrollHeight;
1677
+ } else if (data.final_result) {
1678
+ resultDiv.textContent = JSON.stringify(data.final_result, null, 2);
1679
+ eventSource.close();
1680
+ } else if (data.error) {
1681
+ logDiv.textContent += 'Error: ' + data.error + '\\n';
1682
+ logDiv.scrollTop = logDiv.scrollHeight;
1683
  eventSource.close();
1684
  }
1685
  };