fix: fix chat webui response parsing (#2515)

fix: fix chat webui

Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
This commit is contained in:
Sertaç Özercan 2024-06-07 18:20:31 +03:00 committed by GitHub
parent d38e9090df
commit 0d62594099
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 83 additions and 39 deletions

View file

@ -176,29 +176,73 @@ function readInputImage() {
return; return;
} }
while (true) { // Function to add content to the chat and handle DOM updates efficiently
const { value, done } = await reader.read(); const addToChat = (token) => {
if (done) break; const chatStore = Alpine.store("chat");
let dataDone = false; chatStore.add("assistant", token);
const arr = value.split("\n"); // Efficiently scroll into view without triggering multiple reflows
arr.forEach((data) => { const messages = document.getElementById('messages');
if (data.length === 0) return; messages.scrollTop = messages.scrollHeight;
if (data.startsWith(":")) return; };
if (data === "data: [DONE]") {
dataDone = true; let buffer = "";
return; let contentBuffer = [];
try {
while (true) {
const { value, done } = await reader.read();
if (done) break;
buffer += value;
let lines = buffer.split("\n");
buffer = lines.pop(); // Retain any incomplete line in the buffer
lines.forEach((line) => {
if (line.length === 0 || line.startsWith(":")) return;
if (line === "data: [DONE]") {
return;
}
if (line.startsWith("data: ")) {
try {
const jsonData = JSON.parse(line.substring(6));
const token = jsonData.choices[0].delta.content;
if (token) {
contentBuffer.push(token);
}
} catch (error) {
console.error("Failed to parse line:", line, error);
}
}
});
// Efficiently update the chat in batch
if (contentBuffer.length > 0) {
addToChat(contentBuffer.join(""));
contentBuffer = [];
} }
const token = JSON.parse(data.substring(6)).choices[0].delta.content; }
if (!token) {
return; // Final content flush if any data remains
} if (contentBuffer.length > 0) {
hljs.highlightAll(); addToChat(contentBuffer.join(""));
Alpine.store("chat").add("assistant", token); }
document.getElementById('messages').scrollIntoView(false)
}); // Highlight all code blocks once at the end
hljs.highlightAll(); hljs.highlightAll();
if (dataDone) break; } catch (error) {
console.error("An error occurred while reading the stream:", error);
Alpine.store("chat").add(
"assistant",
`<span class='error'>Error: Failed to process stream</span>`,
);
} finally {
// Perform any cleanup if necessary
reader.releaseLock();
} }
// Remove class "loader" from the element with "loader" id // Remove class "loader" from the element with "loader" id
//document.getElementById("loader").classList.remove("loader"); //document.getElementById("loader").classList.remove("loader");
document.getElementById("loader").style.display = "none"; document.getElementById("loader").style.display = "none";