Skip to content

Commit

Permalink
HTML is now escaped on the process stream function
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesvan authored Oct 4, 2023
1 parent 4804851 commit 8a6b8a0
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,44 +383,34 @@ function handleKeydownUserPost(event){
}

const decodedData = new TextDecoder().decode(value);
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;
// 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);
}
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);
})

// 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();
}
}

function escapeHTML(str) {
return str.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}
}


function addMessage(message){
const messagesElement = document.querySelector(".messages");
Expand Down

0 comments on commit 8a6b8a0

Please sign in to comment.