From a19a8af4ffe2b86cefddbd9d46d75cd848475f0f Mon Sep 17 00:00:00 2001 From: "DESKTOP-RTLN3BA\\$punk" Date: Thu, 10 Jul 2025 15:06:25 -0700 Subject: [PATCH] fix: bad chat history logic --- surfsense_backend/app/routes/chats_routes.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/surfsense_backend/app/routes/chats_routes.py b/surfsense_backend/app/routes/chats_routes.py index ff80b7a..9db77f4 100644 --- a/surfsense_backend/app/routes/chats_routes.py +++ b/surfsense_backend/app/routes/chats_routes.py @@ -54,10 +54,14 @@ async def handle_chat_data( if message['role'] == "user": langchain_chat_history.append(HumanMessage(content=message['content'])) elif message['role'] == "assistant": - # Last annotation type will always be "ANSWER" here - answer_annotation = message['annotations'][-1] - answer_text = "" - if answer_annotation['type'] == "ANSWER": + # Find the last "ANSWER" annotation specifically + answer_annotation = None + for annotation in reversed(message['annotations']): + if annotation['type'] == "ANSWER": + answer_annotation = annotation + break + + if answer_annotation: answer_text = answer_annotation['content'] # If content is a list, join it into a single string if isinstance(answer_text, list):