Fix streaming chat response in Obsidian client

- Convert renderIncrementalMessage to an async method as
  MarkdownRenderer is an async method

- Simplify code, remove unneeded JSON check
This commit is contained in:
Debanjum Singh Solanky
2023-12-20 14:50:06 +05:30
parent e04fe921eb
commit 447c1b90e7
2 changed files with 11 additions and 17 deletions

View File

@@ -217,11 +217,11 @@ export class KhojChatModal extends Modal {
return chat_message_el return chat_message_el
} }
renderIncrementalMessage(htmlElement: HTMLDivElement, additionalMessage: string) { async renderIncrementalMessage(htmlElement: HTMLDivElement, additionalMessage: string) {
this.result += additionalMessage; this.result += additionalMessage;
htmlElement.innerHTML = ""; htmlElement.innerHTML = "";
// @ts-ignore // @ts-ignore
MarkdownRenderer.renderMarkdown(this.result, htmlElement, null, null); await MarkdownRenderer.renderMarkdown(this.result, htmlElement, null, null);
// Scroll to bottom of modal, till the send message input box // Scroll to bottom of modal, till the send message input box
this.modalEl.scrollTop = this.modalEl.scrollHeight; this.modalEl.scrollTop = this.modalEl.scrollHeight;
} }
@@ -277,7 +277,7 @@ export class KhojChatModal extends Modal {
// Temporary status message to indicate that Khoj is thinking // Temporary status message to indicate that Khoj is thinking
this.result = ""; this.result = "";
this.renderIncrementalMessage(responseElement, "🤔"); await this.renderIncrementalMessage(responseElement, "🤔");
let response = await fetch(chatUrl, { let response = await fetch(chatUrl, {
method: "GET", method: "GET",
@@ -312,17 +312,17 @@ export class KhojChatModal extends Modal {
// If the chunk is not a JSON object, just display it as is // If the chunk is not a JSON object, just display it as is
responseText = response.body.read().toString() responseText = response.body.read().toString()
} finally { } finally {
this.renderIncrementalMessage(responseElement, responseText); await this.renderIncrementalMessage(responseElement, responseText);
} }
} }
for await (const chunk of response.body) { for await (const chunk of response.body) {
let responseText = chunk.toString(); let responseText = chunk.toString();
if (responseText.includes("### compiled references:")) { if (responseText.includes("### compiled references:")) {
const additionalResponse = responseText.split("### compiled references:")[0]; const [additionalResponse, rawReference] = responseText.split("### compiled references:", 2);
this.renderIncrementalMessage(responseElement, additionalResponse); await this.renderIncrementalMessage(responseElement, additionalResponse);
console.log(`Raw: ${responseText}\nResponse: ${additionalResponse}\nReferences: ${rawReference}`);
const rawReference = responseText.split("### compiled references:")[1];
const rawReferenceAsJson = JSON.parse(rawReference); const rawReferenceAsJson = JSON.parse(rawReference);
let references = responseElement.createDiv(); let references = responseElement.createDiv();
references.classList.add("references"); references.classList.add("references");
@@ -360,13 +360,7 @@ export class KhojChatModal extends Modal {
referenceExpandButton.innerHTML = expandButtonText; referenceExpandButton.innerHTML = expandButtonText;
references.appendChild(referenceSection); references.appendChild(referenceSection);
} else { } else {
if (responseText.startsWith("{") && responseText.endsWith("}")) { await this.renderIncrementalMessage(responseElement, responseText);
} else {
// If the chunk is not a JSON object, just display it as is
continue;
}
this.renderIncrementalMessage(responseElement, responseText);
} }
} }
} catch (err) { } catch (err) {