Show temp status message in Khoj Obsidian chat while Khoj is thinking

- Scroll to bottom after adding temporary status message and
references too
This commit is contained in:
Debanjum Singh Solanky 2023-07-05 18:01:49 -07:00
parent 8271abe729
commit 0ba838b53a

View file

@ -156,6 +156,10 @@ export class KhojChatModal extends Modal {
// Get chat response from Khoj backend
let encodedQuery = encodeURIComponent(query);
let chatUrl = `${this.setting.khojUrl}/api/chat?q=${encodedQuery}&client=obsidian`;
let responseElement = this.createKhojResponseDiv();
// Temporary status message to indicate that Khoj is thinking
this.renderIncrementalMessage(responseElement, "🤔");
let response = await fetch(chatUrl, {
method: "GET",
@ -164,22 +168,25 @@ export class KhojChatModal extends Modal {
"Content-Type": "text/event-stream"
},
})
let responseElemeent = this.createKhojResponseDiv();
try {
if (response.body == null) {
throw new Error("Response body is null");
}
// Clear thinking status message
if (responseElement.innerHTML === "🤔") {
responseElement.innerHTML = "";
}
for await (const chunk of response.body) {
const responseText = chunk.toString();
if (responseText.startsWith("### compiled references:")) {
return;
}
this.renderIncrementalMessage(responseElemeent, responseText);
this.renderIncrementalMessage(responseElement, responseText);
}
} catch (err) {
this.renderIncrementalMessage(responseElemeent, "Sorry, unable to get response from Khoj backend ❤️‍🩹. Contact developer for help at team@khoj.dev or <a href='https://discord.gg/BDgyabRM6e'>in Discord</a>")
this.renderIncrementalMessage(responseElement, "Sorry, unable to get response from Khoj backend ❤️‍🩹. Contact developer for help at team@khoj.dev or <a href='https://discord.gg/BDgyabRM6e'>in Discord</a>")
}
}
}