From 6c9d569a22c2e2d62b9057141cc0ffc4c145ee28 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Thu, 29 May 2025 11:02:24 -0700 Subject: [PATCH] Fix to get user questions in chat history from user not khoj message Since partial state reload after interrupt drops Khoj messages. The assumption that there will always be a Khoj message after a user message is broken. That is, there can now be multiple user messages preceding a Khoj user message now. This change allow for user queries to still be extracted for chat history even if no khoj message follow. --- src/khoj/processor/conversation/utils.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/khoj/processor/conversation/utils.py b/src/khoj/processor/conversation/utils.py index 039b745f..3a29952d 100644 --- a/src/khoj/processor/conversation/utils.py +++ b/src/khoj/processor/conversation/utils.py @@ -193,11 +193,8 @@ def construct_chat_history(conversation_history: dict, n: int = 4, agent_name="A chat_history = "" for chat in conversation_history.get("chat", [])[-n:]: if chat["by"] == "khoj" and chat["intent"].get("type") in ["remember", "reminder", "summarize"]: - chat_history += f"User: {chat['intent']['query']}\n" - if chat["intent"].get("inferred-queries"): chat_history += f'{agent_name}: {{"queries": {chat["intent"].get("inferred-queries")}}}\n' - chat_history += f"{agent_name}: {chat['message']}\n\n" elif chat["by"] == "khoj" and chat.get("images"): chat_history += f"User: {chat['intent']['query']}\n" @@ -206,6 +203,7 @@ def construct_chat_history(conversation_history: dict, n: int = 4, agent_name="A chat_history += f"User: {chat['intent']['query']}\n" chat_history += f"{agent_name}: {chat['intent']['inferred-queries'][0]}\n" elif chat["by"] == "you": + chat_history += f"User: {chat['message']}\n" raw_query_files = chat.get("queryFiles") if raw_query_files: query_files: Dict[str, str] = {} @@ -229,9 +227,12 @@ def construct_question_history( Constructs a chat history string formatted for query extraction purposes. """ history_parts = "" + original_query = None for chat in conversation_log.get("chat", [])[-lookback:]: + if chat["by"] == "you": + original_query = chat.get("message") + history_parts += f"{query_prefix}: {original_query}\n" if chat["by"] == "khoj": - original_query = chat.get("intent", {}).get("query") if original_query is None: continue @@ -254,6 +255,9 @@ def construct_question_history( else: history_parts += f"{agent_name}: {message}\n\n" + # Reset original_query for the next turn + original_query = None + return history_parts