SimpleChatTC: Extract streamed field - assume only 1f at any time

Update response_extract_stream to check for which field is being
currently streamed ie is it normal content or tool call func name
or tool call func args and then return the field name and extracted
value.

Previously it was always assumed that only normal content will be
returned.

Currently it is assumed that the server will only stream one of the
3 supported fields at any time and not more than one of them at the
same time.

TODO: Have to also add logic to extract the reasoning field later,
ie wrt gen ai models which give out their thinking.

Have updated append_response to expect both the key and the value
wrt the latestResponse object, which it will be manipualted.

Previously it was always assumed that content is what will be got
and inturn appended.
This commit is contained in:
hanishkvc 2025-10-11 01:46:23 +05:30
parent bfe7ef69fa
commit 63430dc9f7
1 changed files with 21 additions and 6 deletions

View File

@ -152,10 +152,10 @@ class SimpleChat {
/** /**
* Collate the latest response from the server/ai-model, as it is becoming available. * Collate the latest response from the server/ai-model, as it is becoming available.
* This is mainly useful for the stream mode. * This is mainly useful for the stream mode.
* @param {string} content * @param {{key: string, value: string}} resp
*/ */
append_response(content) { append_response(resp) {
this.latestResponse.content += content; this.latestResponse[resp.key] += resp.value;
} }
/** /**
@ -311,10 +311,25 @@ class SimpleChat {
* @param {string} apiEP * @param {string} apiEP
*/ */
response_extract_stream(respBody, apiEP) { response_extract_stream(respBody, apiEP) {
let key = "content"
let assistant = ""; let assistant = "";
if (apiEP == ApiEP.Type.Chat) { if (apiEP == ApiEP.Type.Chat) {
if (respBody["choices"][0]["finish_reason"] !== "stop") { if (respBody["choices"][0]["finish_reason"] !== null) {
assistant = respBody["choices"][0]["delta"]["content"]; if (respBody["choices"][0]["delta"]["content"] !== undefined) {
assistant = respBody["choices"][0]["delta"]["content"];
} else {
if (respBody["choices"][0]["delta"]["tool_calls"] !== undefined) {
if (respBody["choices"][0]["delta"]["tool_calls"][0]["function"]["name"] !== undefined) {
key = "toolname";
assistant = respBody["choices"][0]["delta"]["tool_calls"][0]["function"]["name"];
} else {
if (respBody["choices"][0]["delta"]["tool_calls"][0]["function"]["arguments"] !== undefined) {
key = "toolargs";
assistant = respBody["choices"][0]["delta"]["tool_calls"][0]["function"]["arguments"];
}
}
}
}
} }
} else { } else {
try { try {
@ -323,7 +338,7 @@ class SimpleChat {
assistant = respBody["content"]; assistant = respBody["content"];
} }
} }
return assistant; return { key: key, value: assistant };
} }
/** /**