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.
This commit is contained in:
Debanjum
2025-05-29 11:02:24 -07:00
parent b6aa77a6f5
commit 6c9d569a22

View File

@@ -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