Peterase commited on
Commit
4ce2e2e
·
1 Parent(s): 3be9e60

fix: implement missing _get_history_text in RagChatUseCase to fix 500 error during stream

Browse files
src/core/use_cases/rag_chat_use_case.py CHANGED
@@ -779,8 +779,22 @@ JSON:"""
779
  # Collapse multiple blank lines
780
  content = re.sub(r'\n{3,}', '\n\n', content)
781
  return content.strip()
782
- past_messages = self.chat_history_db.get_history(session_id, limit=6)
783
- return "".join([f"{msg.role}: {msg.content}\n" for msg in past_messages])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
784
 
785
  def _get_cache_keys(self, query: str) -> Dict[str, str]:
786
  """Generate cache keys — kept for backward compat, new code uses RedisAdapter methods directly."""
@@ -826,7 +840,7 @@ JSON:"""
826
  return cached_result
827
 
828
  logger.info("[RAG] Cache MISS — running full RAG pipeline")
829
- history_text = self._get_history_text(session_id)
830
 
831
  context_text, final_sources = await self._build_context(
832
  request.query,
 
779
  # Collapse multiple blank lines
780
  content = re.sub(r'\n{3,}', '\n\n', content)
781
  return content.strip()
782
+
783
+ def _get_history_text(self, session_id: str) -> str:
784
+ if not session_id:
785
+ return ""
786
+ try:
787
+ history = self.chat_history_db.get_history(session_id, limit=6)
788
+ if not history:
789
+ return ""
790
+ history_lines = []
791
+ for msg in history:
792
+ role = "User" if msg.role == "user" else "Assistant"
793
+ history_lines.append(f"{role}: {msg.content}")
794
+ return "\n".join(history_lines)
795
+ except Exception as e:
796
+ logger.error(f"[RAG] Failed to fetch history: {e}")
797
+ return ""
798
 
799
  def _get_cache_keys(self, query: str) -> Dict[str, str]:
800
  """Generate cache keys — kept for backward compat, new code uses RedisAdapter methods directly."""
 
840
  return cached_result
841
 
842
  logger.info("[RAG] Cache MISS — running full RAG pipeline")
843
+ history_text = "" if is_guest else self._get_history_text(session_id)
844
 
845
  context_text, final_sources = await self._build_context(
846
  request.query,