Skip to content

Commit 75a63e3

Browse files
committed
SimpleChatTC:ChatMessageEx: Better tool result extractor
1 parent 61c2314 commit 75a63e3

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

tools/server/public_simplechat/simplechat.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class ChatMessageEx {
9191
* Extract the elements of the all in one tool call result string
9292
* @param {string} allInOne
9393
*/
94-
static extractToolCallResultAllInOne(allInOne) {
94+
static extractToolCallResultAllInOneSimpleMinded(allInOne) {
9595
const regex = /<tool_response>\s*<id>(.*?)<\/id>\s*<name>(.*?)<\/name>\s*<content>([\s\S]*?)<\/content>\s*<\/tool_response>/si;
9696
const caught = allInOne.match(regex)
9797
let data = { tool_call_id: "Error", name: "Error", content: "Error" }
@@ -105,6 +105,29 @@ class ChatMessageEx {
105105
return data
106106
}
107107

108+
/**
109+
* Extract the elements of the all in one tool call result string
110+
* This should potentially account for content tag having xml content within to an extent.
111+
* @param {string} allInOne
112+
*/
113+
static extractToolCallResultAllInOne(allInOne) {
114+
const dParser = new DOMParser();
115+
const got = dParser.parseFromString(allInOne, 'text/xml');
116+
const parseErrors = got.querySelector('parseerror')
117+
if (parseErrors) {
118+
console.debug("WARN:ChatMessageEx:ExtractToolCallResultAllInOne:", parseErrors.textContent.trim())
119+
}
120+
const id = got.querySelector('id')?.textContent.trim();
121+
const name = got.querySelector('name')?.textContent.trim();
122+
const content = got.querySelector('content')?.textContent.trim();
123+
let data = {
124+
tool_call_id: id? id : "Error",
125+
name: name? name : "Error",
126+
content: content? content : "Error"
127+
}
128+
return data
129+
}
130+
108131
/**
109132
* Set extra members into the ns object
110133
* @param {string | number} key

0 commit comments

Comments
 (0)