From b5f90331e717572946abd2ec10e2d9fa224af679 Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Mon, 15 Jun 2026 22:56:17 +0200 Subject: [PATCH] Enhance chat turn processing to correctly handle memory-only responses --- backend/chat_memory.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/chat_memory.py b/backend/chat_memory.py index 0fcfda0..e6f4ddf 100644 --- a/backend/chat_memory.py +++ b/backend/chat_memory.py @@ -124,6 +124,19 @@ def _attachment_summary(raw_value: Any) -> str: return ", ".join(labels[:8]) +def _is_memory_only_response(row: models.ChatMessage) -> bool: + try: + sources = json.loads(getattr(row, "sources_json", None) or "[]") + except Exception: + return False + if not isinstance(sources, list) or not sources: + return False + for source in sources: + if not isinstance(source, dict) or source.get("type") != "chat_memory": + return False + return True + + def _completed_turns(session: models.ChatSession, rows: Sequence[models.ChatMessage]) -> List[Dict[str, Any]]: turns: List[Dict[str, Any]] = [] pending_user: Optional[models.ChatMessage] = None @@ -134,6 +147,9 @@ def _completed_turns(session: models.ChatSession, rows: Sequence[models.ChatMess continue if row.role != "assistant" or pending_user is None: continue + if _is_memory_only_response(row): + pending_user = None + continue user_content = _clean_content(pending_user.content) assistant_content = _clean_content(row.content)