SimpleChatTC: Skeleton to handle diff fields when streaming

Changed latestResponse type to an object instead of a string.
Inturn it contains entries for content, toolname and toolargs.

Added a custom clear logic due to the same and used it to replace
the previously simple assigning of empty string to latestResponse.

For now in all places where latestReponse is used, I have replaced
with latestReponse.content.

Next need to handle identifying the field being streamed and inturn
append to it. Also need to add logic to call tool, when tool_call
triggered by genai.
This commit is contained in:
hanishkvc 2025-10-11 01:17:19 +05:30
parent 32f5278e8c
commit bfe7ef69fa
1 changed files with 13 additions and 9 deletions

View File

@ -72,7 +72,7 @@ class SimpleChat {
*/
this.xchat = [];
this.iLastSys = -1;
this.latestResponse = "";
this.latestResponse = { content: "", toolname: "", toolargs: "" };
}
clear() {
@ -80,6 +80,10 @@ class SimpleChat {
this.iLastSys = -1;
}
clear_latestresponse() {
this.latestResponse = { content: "", toolname: "", toolargs: "" };
}
ods_key() {
return `SimpleChat-${this.chatId}`
}
@ -151,7 +155,7 @@ class SimpleChat {
* @param {string} content
*/
append_response(content) {
this.latestResponse += content;
this.latestResponse.content += content;
}
/**
@ -392,7 +396,7 @@ class SimpleChat {
}
let tdUtf8 = new TextDecoder("utf-8");
let rr = resp.body.getReader();
this.latestResponse = "";
this.clear_latestresponse()
let xLines = new du.NewLines();
while(true) {
let { value: cur, done: done } = await rr.read();
@ -419,14 +423,14 @@ class SimpleChat {
console.debug("DBUG:SC:PART:Json:", curJson);
this.append_response(this.response_extract_stream(curJson, apiEP));
}
elP.innerText = this.latestResponse;
elP.innerText = this.latestResponse.content;
elP.scrollIntoView(false);
if (done) {
break;
}
}
console.debug("DBUG:SC:PART:Full:", this.latestResponse);
return this.latestResponse;
console.debug("DBUG:SC:PART:Full:", this.latestResponse.content);
return this.latestResponse.content;
}
/**
@ -455,11 +459,11 @@ class SimpleChat {
if (gMe.bStream) {
try {
theResp.assistant = await this.handle_response_multipart(resp, apiEP, elDiv);
this.latestResponse = "";
this.clear_latestresponse()
} catch (error) {
theResp.assistant = this.latestResponse;
theResp.assistant = this.latestResponse.content;
this.add(Roles.Assistant, theResp.assistant);
this.latestResponse = "";
this.clear_latestresponse()
throw error;
}
} else {