Skip to content

Commit

Permalink
fix: undo changes processStream()
Browse files Browse the repository at this point in the history
  • Loading branch information
PandorasActorMS committed Oct 16, 2023
1 parent c37ce22 commit a40882f
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,21 +407,41 @@ function handleKeydownUserPost(event){
}

const decodedData = new TextDecoder().decode(value);
console.log(decodedData);
//console.log(decodedData);
let chunks = decodedData.split("data: ");
chunks.forEach((chunk, index) => {
if(chunk.indexOf('finish_reason":"stop"') > 0) return false;
if(chunk.indexOf('DONE') > 0) return false;
if(chunk.indexOf('role') > 0) return false;
if(chunk.length == 0) return false;
if(chunk != "") console.log(JSON.parse(chunk)["choices"][0]["delta"])
console.log(JSON.parse(chunk)["choices"][0]["delta"]);
document.querySelector(".message:last-child").querySelector(".message-text").innerHTML += escapeHTML(JSON.parse(chunk)["choices"][0]["delta"].content);
if(chunk.length === 0) return false;
// First check if chunk is valid json.
// Otherwise we do not see the correct error message.
try {
const json = JSON.parse(chunk);
if ("choices" in json) {
// console.log(json["choices"]);
// normal response
document.querySelector(".message:last-child").querySelector(".message-text").innerHTML +=
json["choices"][0]["delta"].content;
} else {
if ("error" in json) {
if ("message" in json.error) {
// console.log(json.error.message);
document.querySelector(".message:last-child").querySelector(".message-text").innerHTML =
'<em>' + json.error.message + '</em>';
} else {
console.log(json.error);
}
} else {
console.log(json);
}
}
} catch(error) {
console.log(chunk);
console.error(error.message);
}
})

// Check if the content has code block
document.querySelector(".message:last-child").querySelector(".message-text").innerHTML = document.querySelector(".message:last-child").querySelector(".message-text").innerHTML.replace(/```([\s\S]+?)```/g, '<pre><code>$1</code></pre>').replace(/\*\*.*?\*\*/g, '');;
hljs.highlightAll();
scrollToLast();
}
}
Expand Down

0 comments on commit a40882f

Please sign in to comment.