Do not create new chat session when an old chat session is deleted

- Fix
  `get_conversation_by_user' shouldn't return new conversation if
  conversation with requested id not found.

  It should only return new conversation if no specific conversation
  is requested and no conversations found for user at all

- Repro
  - Delete a new chat, this calls loadChat via window.onload which
    calls server /chat/history API endpoint with conversationId set to
    that of just deleted conversation sporadically

    The call to GET chat/history API with conversationId set occurs
    when window.onload triggers before the conversationId is deleted
    by the delete button after the DELETE /chat/history API call (via race)

  - In such a scenario, get_conversation_by_user called by
    chat/history API with conversationId of deleted conversation
    returns a new conversation

- Miscellaneous
  - Chat history load should be logged as call to that chat_history api,
    not the "chat" api
  - Show status updates of clearing conversation history in chat input
  - Simplify web, desktop client code by removing unnecessary new variables
This commit is contained in:
Debanjum Singh Solanky
2024-03-09 19:59:30 +05:30
parent b7fad04870
commit fd81446ba3
4 changed files with 24 additions and 22 deletions

View File

@@ -626,10 +626,9 @@
let chatBody = document.getElementById("chat-body");
chatBody.innerHTML = "";
let conversationId = chatBody.dataset.conversationId;
let chatHistoryUrl = `/api/chat/history?client=desktop`;
if (conversationId) {
chatHistoryUrl += `&conversation_id=${conversationId}`;
if (chatBody.dataset.conversationId) {
chatHistoryUrl += `&conversation_id=${chatBody.dataset.conversationId}`;
}
fetch(`${hostURL}${chatHistoryUrl}`, { headers })
@@ -650,6 +649,8 @@
// Disable chat input field and update placeholder text
document.getElementById("chat-input").setAttribute("disabled", "disabled");
document.getElementById("chat-input").setAttribute("placeholder", "Configure Khoj to enable chat");
} else if (data.status != "ok") {
throw new Error(data.message);
} else {
// Set welcome message on load
renderMessage("Hey 👋🏾, what's up?", "khoj");
@@ -657,12 +658,9 @@
return data.response;
})
.then(response => {
conversationId = response.conversation_id;
const conversationTitle = response.slug || `New conversation 🌱`;
let chatBody = document.getElementById("chat-body");
chatBody.dataset.conversationId = conversationId;
chatBody.dataset.conversationTitle = conversationTitle;
chatBody.dataset.conversationId = response.conversation_id;
chatBody.dataset.conversationTitle = response.slug || `New conversation 🌱`;
const fullChatLog = response.chat || [];
@@ -791,7 +789,7 @@
chatBody.dataset.conversationId = "";
chatBody.dataset.conversationTitle = "";
loadChat();
flashStatusInChatInput("🗑 Cleared conversation history");
flashStatusInChatInput("🗑 Cleared previous conversation history");
})
.catch(err => {
flashStatusInChatInput("⛔️ Failed to clear conversation history");