Fix creating new chat sessions from the Desktop app

Code wasn't passing the authorization header in the POST request to
create new chat session
This commit is contained in:
Debanjum Singh Solanky
2024-02-21 19:35:06 +05:30
parent 8a219b6e9c
commit 05c1903784

View File

@@ -344,22 +344,20 @@
let chat_body = document.getElementById("chat-body"); let chat_body = document.getElementById("chat-body");
let conversationID = chat_body.dataset.conversationId; let conversationID = chat_body.dataset.conversationId;
let hostURL = await window.hostURLAPI.getURL(); let hostURL = await window.hostURLAPI.getURL();
const khojToken = await window.tokenAPI.getToken();
const headers = { 'Authorization': `Bearer ${khojToken}` };
if (!conversationID) { if (!conversationID) {
let response = await fetch(`${hostURL}/api/chat/sessions`, { method: "POST" }); let response = await fetch(`${hostURL}/api/chat/sessions`, { method: "POST", headers });
let data = await response.json(); let data = await response.json();
conversationID = data.conversation_id; conversationID = data.conversation_id;
chat_body.dataset.conversationId = conversationID; chat_body.dataset.conversationId = conversationID;
await refreshChatSessionsPanel(); await refreshChatSessionsPanel();
} }
// Generate backend API URL to execute query // Generate backend API URL to execute query
let url = `${hostURL}/api/chat?q=${encodeURIComponent(query)}&n=${resultsCount}&client=web&stream=true&conversation_id=${conversationID}&region=${region}&city=${city}&country=${countryName}`; let chatApi = `${hostURL}/api/chat?q=${encodeURIComponent(query)}&n=${resultsCount}&client=web&stream=true&conversation_id=${conversationID}&region=${region}&city=${city}&country=${countryName}`;
const khojToken = await window.tokenAPI.getToken();
const headers = { 'Authorization': `Bearer ${khojToken}` };
let new_response = document.createElement("div"); let new_response = document.createElement("div");
new_response.classList.add("chat-message", "khoj"); new_response.classList.add("chat-message", "khoj");
@@ -382,8 +380,8 @@
let chatInput = document.getElementById("chat-input"); let chatInput = document.getElementById("chat-input");
chatInput.classList.remove("option-enabled"); chatInput.classList.remove("option-enabled");
// Call specified Khoj API // Call Khoj chat API
let response = await fetch(url, { headers }); let response = await fetch(chatApi, { headers });
let rawResponse = ""; let rawResponse = "";
const contentType = response.headers.get("content-type"); const contentType = response.headers.get("content-type");