SimpleChat:Try read json early, if available

For later

the server flow doesnt seem to be sending back data early, atleast
for the request (inc options) that is currently sent.

if able to read json data early on in future, as and when ai model
is generating data, then this helper needs to indirectly update
the chat div with the recieved data, without waiting for the
overall data to be available.
This commit is contained in:
HanishKVC 2024-05-23 14:32:01 +05:30
parent e2164d66e6
commit 40fbbeb2f6

View file

@ -342,6 +342,29 @@ class MultiChatUI {
} }
} }
/**
* Try read json response early, if available.
* @param {Response} resp
*/
async read_json_early(resp) {
if (!resp.body) {
throw Error("ERRR:SimpleChat:MCUI:ReadJsonEarly:No body...");
}
let tdUtf8 = new TextDecoder("utf-8");
let rr = resp.body.getReader();
let gotBody = "";
while(true) {
let { value: cur, done: done} = await rr.read();
let curBody = tdUtf8.decode(cur);
console.debug("DBUG:SC:PART:", curBody);
gotBody += curBody;
if (done) {
break;
}
}
return JSON.parse(gotBody);
}
/** /**
* Handle user query submit request, wrt specified chat session. * Handle user query submit request, wrt specified chat session.
* @param {string} chatId * @param {string} chatId
@ -388,6 +411,7 @@ class MultiChatUI {
}); });
let respBody = await resp.json(); let respBody = await resp.json();
//let respBody = await this.read_json_early(resp);
console.debug(`DBUG:SimpleChat:MCUI:${chatId}:HandleUserSubmit:RespBody:${JSON.stringify(respBody)}`); console.debug(`DBUG:SimpleChat:MCUI:${chatId}:HandleUserSubmit:RespBody:${JSON.stringify(respBody)}`);
let assistantMsg; let assistantMsg;
if (apiEP == ApiEP.Chat) { if (apiEP == ApiEP.Chat) {