From b75b3db7bfd361f5a330ce264fdb697d854f9315 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Thu, 30 May 2024 03:51:28 +0530 Subject: [PATCH] SimpleChat:WIP:Collate internally, Stream mode Trap exceptions This can help ensure that data fetched till that point, can be made use of, rather than losing it. On some platforms, the time taken wrt generating a long response, may lead to the network connection being broken when it enters some user-no-interaction related power saving mode. --- .../server/public_simplechat/simplechat.js | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/examples/server/public_simplechat/simplechat.js b/examples/server/public_simplechat/simplechat.js index f8d21826f..24a26dbe6 100644 --- a/examples/server/public_simplechat/simplechat.js +++ b/examples/server/public_simplechat/simplechat.js @@ -71,6 +71,7 @@ class SimpleChat { */ this.xchat = []; this.iLastSys = -1; + this.latestResponse = ""; } clear() { @@ -122,6 +123,15 @@ class SimpleChat { return rchat; } + /** + * Collate the latest response from the server/ai-model, as it is becoming available. + * This is mainly useful for the stream mode. + * @param {string} content + */ + append_response(content) { + this.latestResponse += content; + } + /** * Add an entry into xchat * @param {string} role @@ -355,7 +365,7 @@ class SimpleChat { } let tdUtf8 = new TextDecoder("utf-8"); let rr = resp.body.getReader(); - let gotBody = ""; + this.latestResponse = ""; let xLines = new du.NewLines(); while(true) { let { value: cur, done: done } = await rr.read(); @@ -377,16 +387,16 @@ class SimpleChat { } let curJson = JSON.parse(curLine); console.debug("DBUG:SC:PART:Json:", curJson); - gotBody += this.response_extract_stream(curJson, apiEP); + this.append_response(this.response_extract_stream(curJson, apiEP)); } - elP.innerText = gotBody; + elP.innerText = this.latestResponse; elP.scrollIntoView(false); if (done) { break; } } - console.debug("DBUG:SC:PART:Full:", gotBody); - return gotBody; + console.debug("DBUG:SC:PART:Full:", this.latestResponse); + return this.latestResponse; } /** @@ -414,7 +424,13 @@ class SimpleChat { } let origMsg; if (gMe.bStream) { - origMsg = await this.handle_response_multipart(resp, apiEP, elDiv); + try { + origMsg = await this.handle_response_multipart(resp, apiEP, elDiv); + this.latestResponse = ""; + } catch (error) { + origMsg = this.latestResponse; + throw error; + } } else { origMsg = await this.handle_response_oneshot(resp, apiEP); }