SimpleChatTC: AssistantResponse class initial go
Make latestResponse into a new class based type instance wrt ai assistant response, which is what it represents. Move clearing, appending fields' values and getting assistant's response info (irrespective of a content or toolcall response) into this new class and inturn use the same.
This commit is contained in:
parent
5a26831ad2
commit
3f3aa8d043
|
|
@ -37,6 +37,39 @@ class ApiEP {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class AssistantResponse {
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.response = { content: "", toolname: "", toolargs: "" };
|
||||||
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
this.response = { content: "", toolname: "", toolargs: "" };
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helps collate the latest response from the server/ai-model, as it is becoming available.
|
||||||
|
* This is mainly useful for the stream mode.
|
||||||
|
* @param {{key: string, value: string}} resp
|
||||||
|
*/
|
||||||
|
append_response(resp) {
|
||||||
|
if (resp.value == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
console.debug(resp.key, resp.value)
|
||||||
|
this.response[resp.key] += resp.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
content_equiv() {
|
||||||
|
if (this.response.content !== "") {
|
||||||
|
return this.response.content;
|
||||||
|
} else {
|
||||||
|
return `ToolCall:${this.response.toolname}:${this.response.toolargs}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
let gUsageMsg = `
|
let gUsageMsg = `
|
||||||
<p class="role-system">Usage</p>
|
<p class="role-system">Usage</p>
|
||||||
|
|
@ -72,7 +105,7 @@ class SimpleChat {
|
||||||
*/
|
*/
|
||||||
this.xchat = [];
|
this.xchat = [];
|
||||||
this.iLastSys = -1;
|
this.iLastSys = -1;
|
||||||
this.latestResponse = { content: "", toolname: "", toolargs: "" };
|
this.latestResponse = new AssistantResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
clear() {
|
||||||
|
|
@ -80,10 +113,6 @@ 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}`
|
||||||
}
|
}
|
||||||
|
|
@ -149,19 +178,6 @@ class SimpleChat {
|
||||||
return rchat;
|
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 {{key: string, value: string}} resp
|
|
||||||
*/
|
|
||||||
append_response(resp) {
|
|
||||||
if (resp.value == null) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
console.debug(resp.key, resp.value)
|
|
||||||
this.latestResponse[resp.key] += resp.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an entry into xchat
|
* Add an entry into xchat
|
||||||
* @param {string} role
|
* @param {string} role
|
||||||
|
|
@ -416,7 +432,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.clear_latestresponse()
|
this.latestResponse.clear()
|
||||||
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();
|
||||||
|
|
@ -441,20 +457,16 @@ class SimpleChat {
|
||||||
}
|
}
|
||||||
let curJson = JSON.parse(curLine);
|
let curJson = JSON.parse(curLine);
|
||||||
console.debug("DBUG:SC:PART:Json:", curJson);
|
console.debug("DBUG:SC:PART:Json:", curJson);
|
||||||
this.append_response(this.response_extract_stream(curJson, apiEP));
|
this.latestResponse.append_response(this.response_extract_stream(curJson, apiEP));
|
||||||
}
|
|
||||||
if (this.latestResponse.content !== "") {
|
|
||||||
elP.innerText = this.latestResponse.content;
|
|
||||||
} else {
|
|
||||||
elP.innerText = `ToolCall:${this.latestResponse.toolname}:${this.latestResponse.toolargs}`;
|
|
||||||
}
|
}
|
||||||
|
elP.innerText = this.latestResponse.content_equiv()
|
||||||
elP.scrollIntoView(false);
|
elP.scrollIntoView(false);
|
||||||
if (done) {
|
if (done) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.debug("DBUG:SC:PART:Full:", this.latestResponse.content);
|
console.debug("DBUG:SC:PART:Full:", this.latestResponse.content_equiv());
|
||||||
return this.latestResponse.content;
|
return this.latestResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -483,11 +495,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.clear_latestresponse()
|
this.latestResponse.clear()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
theResp.assistant = this.latestResponse.content;
|
theResp.assistant = this.latestResponse.content;
|
||||||
this.add(Roles.Assistant, theResp.assistant);
|
this.add(Roles.Assistant, theResp.assistant);
|
||||||
this.clear_latestresponse()
|
this.latestResponse.clear()
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue