Skip to content

Commit 4efa232

Browse files
committed
SimpleChatTC:ChatMessageEx: Build tool role result fully
Expand the xml format id, name and content in content field of tool result into apropriate fields in the tool result message sent to the genai/llm engine on the server.
1 parent f379e65 commit 4efa232

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

tools/server/public_simplechat/simplechat.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,33 @@ class ChatMessageEx {
8787
return `<tool_response> <id>${toolCallId}</id> <name>${toolName}</name> <content>${toolResult}</content> </tool_response>`;
8888
}
8989

90+
/**
91+
* Extract the elements of the all in one tool call result string
92+
* @param {string} allInOne
93+
*/
94+
static extractToolCallResultAllInOne(allInOne) {
95+
const regex = /<tool_response>\s*<id>(.*?)<\/id>\s*<name>(.*?)<\/name>\s*<content>([\s\S]*?)<\/content>\s*<\/tool_response>/si;
96+
const caught = allInOne.match(regex)
97+
let data = { tool_call_id: "Error", name: "Error", content: "Error" }
98+
if (caught) {
99+
data = {
100+
tool_call_id: caught[1].trim(),
101+
name: caught[2].trim(),
102+
content: caught[3].trim()
103+
}
104+
}
105+
return data
106+
}
107+
108+
/**
109+
* Set extra members into the ns object
110+
* @param {string | number} key
111+
* @param {any} value
112+
*/
113+
ns_set_extra(key, value) {
114+
// @ts-ignore
115+
this.ns[key] = value
116+
}
90117

91118
/**
92119
* Update based on the drip by drip data got from network in streaming mode.
@@ -305,6 +332,12 @@ class SimpleChat {
305332
// @ts-ignore
306333
delete(tmsg.ns.tool_calls)
307334
}
335+
if (tmsg.ns.role == Roles.Tool) {
336+
let res = ChatMessageEx.extractToolCallResultAllInOne(tmsg.ns.content)
337+
tmsg.ns.content = res.content
338+
tmsg.ns_set_extra("tool_call_id", res.tool_call_id)
339+
tmsg.ns_set_extra("name", res.name)
340+
}
308341
chat.push(tmsg.ns);
309342
}
310343
return chat

0 commit comments

Comments
 (0)