From a40882f829638aff95c9802a02f3e07f464a27e2 Mon Sep 17 00:00:00 2001 From: Mehmet Sagir Date: Mon, 16 Oct 2023 14:41:16 +0000 Subject: [PATCH] fix: undo changes processStream() --- interface.php | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/interface.php b/interface.php index cb32e1d..6bee17e 100644 --- a/interface.php +++ b/interface.php @@ -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 = + '' + json.error.message + ''; + } 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, '
$1
').replace(/\*\*.*?\*\*/g, '');; - hljs.highlightAll(); scrollToLast(); } }