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