From 69344a6aa625552ad04b867b7eff48f04f3f6359 Mon Sep 17 00:00:00 2001 From: sabaimran <65192171+sabaimran@users.noreply.github.com> Date: Sun, 11 Feb 2024 02:35:28 -0800 Subject: [PATCH] Add support for multiple chat sessions in the desktop application (#639) * Add chat sessions to the desktop application * Increase width of the main chat body to 90vw * Update the version of electron * Render the default message if chat history fails to load * Merge conversation migrations and fix slug setting * Update the welcome message, use the hostURL, and update background color for chat actions * Only update the window's web contents if the page is config --- src/interface/desktop/chat.html | 574 ++++++++++++++++++++++++++--- src/interface/desktop/main.js | 3 +- src/interface/desktop/package.json | 2 +- src/interface/desktop/yarn.lock | 8 +- 4 files changed, 531 insertions(+), 56 deletions(-) diff --git a/src/interface/desktop/chat.html b/src/interface/desktop/chat.html index f24990e8..5b9d1375 100644 --- a/src/interface/desktop/chat.html +++ b/src/interface/desktop/chat.html @@ -140,6 +140,9 @@ // Scroll to bottom of chat-body element chatBody.scrollTop = chatBody.scrollHeight; + + let chatBodyWrapper = document.getElementById("chat-body-wrapper"); + chatBodyWrapperHeight = chatBodyWrapper.clientHeight; } function processOnlineReferences(referenceSection, onlineContext) { @@ -319,14 +322,25 @@ autoResize(); document.getElementById("chat-input").setAttribute("disabled", "disabled"); + let chat_body = document.getElementById("chat-body"); + + let conversationID = chat_body.dataset.conversationId; + let hostURL = await window.hostURLAPI.getURL(); + if (!conversationID) { + let response = await fetch(`${hostURL}/api/chat/sessions`, { method: "POST" }); + let data = await response.json(); + conversationID = data.conversation_id; + chat_body.dataset.conversationId = conversationID; + } + + // Generate backend API URL to execute query - let url = `${hostURL}/api/chat?q=${encodeURIComponent(query)}&n=${resultsCount}&client=web&stream=true`; + let url = `${hostURL}/api/chat?q=${encodeURIComponent(query)}&n=${resultsCount}&client=web&stream=true&conversation_id=${conversationID}`; const khojToken = await window.tokenAPI.getToken(); const headers = { 'Authorization': `Bearer ${khojToken}` }; - let chat_body = document.getElementById("chat-body"); let new_response = document.createElement("div"); new_response.classList.add("chat-message", "khoj"); new_response.attributes["data-meta"] = "🏮 Khoj at " + formatDate(new Date()); @@ -559,7 +573,14 @@ const khojToken = await window.tokenAPI.getToken(); const headers = { 'Authorization': `Bearer ${khojToken}` }; - fetch(`${hostURL}/api/chat/history?client=desktop`, { headers }) + let chatBody = document.getElementById("chat-body"); + let conversationId = chatBody.dataset.conversationId; + let chatHistoryUrl = `/api/chat/history?client=desktop`; + if (conversationId) { + chatHistoryUrl += `&conversation_id=${conversationId}`; + } + + fetch(`${hostURL}${chatHistoryUrl}`, { headers }) .then(response => response.json()) .then(data => { if (data.detail) { @@ -584,17 +605,186 @@ 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; + + const fullChatLog = response.chat || []; - const fullChatLog = response.chat; - // Render conversation history, if any fullChatLog.forEach(chat_log => { - renderMessageWithReference(chat_log.message, chat_log.by, chat_log.context, new Date(chat_log.created), chat_log.onlineContext, chat_log.intent?.type, chat_log.intent?.["inferred-queries"]); - }); + if (chat_log.message != null) { + renderMessageWithReference( + chat_log.message, + chat_log.by, + chat_log.context, + new Date(chat_log.created), + chat_log.onlineContext, + chat_log.intent?.type, + chat_log.intent?.["inferred-queries"]); + } + }) + let chatBodyWrapper = document.getElementById("chat-body-wrapper"); + chatBodyWrapperHeight = chatBodyWrapper.clientHeight; + + chatBody.style.height = chatBodyWrapperHeight; }) .catch(err => { + // If the server returns a 500 error with detail, render a setup hint. + first_run_message = `Hi 👋🏾, to get started: +