mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-02 21:19:12 +00:00
Fix rendering online with note references post streaming chat response
Previously only the notes references would get rendered post response streaming when when both online and notes references were used to respond to the user's message
This commit is contained in:
@@ -357,15 +357,16 @@
|
|||||||
|
|
||||||
let numReferences = 0;
|
let numReferences = 0;
|
||||||
|
|
||||||
if (Array.isArray(references)) {
|
if (references.hasOwnProperty("notes")) {
|
||||||
numReferences = references.length;
|
numReferences += references["notes"].length;
|
||||||
|
|
||||||
references.forEach((reference, index) => {
|
references["notes"].forEach((reference, index) => {
|
||||||
let polishedReference = generateReference(reference, index);
|
let polishedReference = generateReference(reference, index);
|
||||||
referenceSection.appendChild(polishedReference);
|
referenceSection.appendChild(polishedReference);
|
||||||
});
|
});
|
||||||
} else {
|
}
|
||||||
numReferences += processOnlineReferences(referenceSection, references);
|
if (references.hasOwnProperty("online")){
|
||||||
|
numReferences += processOnlineReferences(referenceSection, references["online"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let referenceExpandButton = document.createElement('button');
|
let referenceExpandButton = document.createElement('button');
|
||||||
@@ -511,7 +512,7 @@
|
|||||||
// Handle streamed response of type text/event-stream or text/plain
|
// Handle streamed response of type text/event-stream or text/plain
|
||||||
const reader = response.body.getReader();
|
const reader = response.body.getReader();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
let references = null;
|
let references = {};
|
||||||
|
|
||||||
readStream();
|
readStream();
|
||||||
|
|
||||||
@@ -519,8 +520,8 @@
|
|||||||
reader.read().then(({ done, value }) => {
|
reader.read().then(({ done, value }) => {
|
||||||
if (done) {
|
if (done) {
|
||||||
// Append any references after all the data has been streamed
|
// Append any references after all the data has been streamed
|
||||||
if (references != null) {
|
if (references != {}) {
|
||||||
newResponseText.appendChild(references);
|
newResponseText.appendChild(createReferenceSection(references));
|
||||||
}
|
}
|
||||||
document.getElementById("chat-body").scrollTop = document.getElementById("chat-body").scrollHeight;
|
document.getElementById("chat-body").scrollTop = document.getElementById("chat-body").scrollHeight;
|
||||||
document.getElementById("chat-input").removeAttribute("disabled");
|
document.getElementById("chat-input").removeAttribute("disabled");
|
||||||
@@ -538,7 +539,11 @@
|
|||||||
|
|
||||||
const rawReference = chunk.split("### compiled references:")[1];
|
const rawReference = chunk.split("### compiled references:")[1];
|
||||||
const rawReferenceAsJson = JSON.parse(rawReference);
|
const rawReferenceAsJson = JSON.parse(rawReference);
|
||||||
references = createReferenceSection(rawReferenceAsJson);
|
if (rawReferenceAsJson instanceof Array) {
|
||||||
|
references["notes"] = rawReferenceAsJson;
|
||||||
|
} else if (typeof rawReferenceAsJson === "object" && rawReferenceAsJson !== null) {
|
||||||
|
references["online"] = rawReferenceAsJson;
|
||||||
|
}
|
||||||
readStream();
|
readStream();
|
||||||
} else {
|
} else {
|
||||||
// Display response from Khoj
|
// Display response from Khoj
|
||||||
|
|||||||
@@ -368,15 +368,16 @@ To get started, just start typing below. You can also type / to see a list of co
|
|||||||
|
|
||||||
let numReferences = 0;
|
let numReferences = 0;
|
||||||
|
|
||||||
if (Array.isArray(references)) {
|
if (references.hasOwnProperty("notes")) {
|
||||||
numReferences = references.length;
|
numReferences += references["notes"].length;
|
||||||
|
|
||||||
references.forEach((reference, index) => {
|
references["notes"].forEach((reference, index) => {
|
||||||
let polishedReference = generateReference(reference, index);
|
let polishedReference = generateReference(reference, index);
|
||||||
referenceSection.appendChild(polishedReference);
|
referenceSection.appendChild(polishedReference);
|
||||||
});
|
});
|
||||||
} else {
|
}
|
||||||
numReferences += processOnlineReferences(referenceSection, references);
|
if (references.hasOwnProperty("online")) {
|
||||||
|
numReferences += processOnlineReferences(referenceSection, references["online"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let referenceExpandButton = document.createElement('button');
|
let referenceExpandButton = document.createElement('button');
|
||||||
@@ -518,7 +519,7 @@ To get started, just start typing below. You can also type / to see a list of co
|
|||||||
// Handle streamed response of type text/event-stream or text/plain
|
// Handle streamed response of type text/event-stream or text/plain
|
||||||
const reader = response.body.getReader();
|
const reader = response.body.getReader();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
let references = null;
|
let references = {};
|
||||||
|
|
||||||
readStream();
|
readStream();
|
||||||
|
|
||||||
@@ -526,8 +527,8 @@ To get started, just start typing below. You can also type / to see a list of co
|
|||||||
reader.read().then(({ done, value }) => {
|
reader.read().then(({ done, value }) => {
|
||||||
if (done) {
|
if (done) {
|
||||||
// Append any references after all the data has been streamed
|
// Append any references after all the data has been streamed
|
||||||
if (references != null) {
|
if (references != {}) {
|
||||||
newResponseText.appendChild(references);
|
newResponseText.appendChild(createReferenceSection(references));
|
||||||
}
|
}
|
||||||
document.getElementById("chat-body").scrollTop = document.getElementById("chat-body").scrollHeight;
|
document.getElementById("chat-body").scrollTop = document.getElementById("chat-body").scrollHeight;
|
||||||
document.getElementById("chat-input").removeAttribute("disabled");
|
document.getElementById("chat-input").removeAttribute("disabled");
|
||||||
@@ -545,7 +546,11 @@ To get started, just start typing below. You can also type / to see a list of co
|
|||||||
|
|
||||||
const rawReference = chunk.split("### compiled references:")[1];
|
const rawReference = chunk.split("### compiled references:")[1];
|
||||||
const rawReferenceAsJson = JSON.parse(rawReference);
|
const rawReferenceAsJson = JSON.parse(rawReference);
|
||||||
references = createReferenceSection(rawReferenceAsJson);
|
if (rawReferenceAsJson instanceof Array) {
|
||||||
|
references["notes"] = rawReferenceAsJson;
|
||||||
|
} else if (typeof rawReferenceAsJson === "object" && rawReferenceAsJson !== null) {
|
||||||
|
references["online"] = rawReferenceAsJson;
|
||||||
|
}
|
||||||
readStream();
|
readStream();
|
||||||
} else {
|
} else {
|
||||||
// Display response from Khoj
|
// Display response from Khoj
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class ThreadedGenerator:
|
|||||||
def close(self):
|
def close(self):
|
||||||
if self.compiled_references and len(self.compiled_references) > 0:
|
if self.compiled_references and len(self.compiled_references) > 0:
|
||||||
self.queue.put(f"### compiled references:{json.dumps(self.compiled_references)}")
|
self.queue.put(f"### compiled references:{json.dumps(self.compiled_references)}")
|
||||||
elif self.online_results and len(self.online_results) > 0:
|
if self.online_results and len(self.online_results) > 0:
|
||||||
self.queue.put(f"### compiled references:{json.dumps(self.online_results)}")
|
self.queue.put(f"### compiled references:{json.dumps(self.online_results)}")
|
||||||
self.queue.put(StopIteration)
|
self.queue.put(StopIteration)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user