SimpleChatTC:ChatMessageEx: Better tool result extractor

This commit is contained in:
hanishkvc 2025-10-15 05:31:08 +05:30
parent 61b70bfa5d
commit 7dbbc46390
1 changed files with 24 additions and 1 deletions

View File

@ -91,7 +91,7 @@ class ChatMessageEx {
* Extract the elements of the all in one tool call result string
* @param {string} allInOne
*/
static extractToolCallResultAllInOne(allInOne) {
static extractToolCallResultAllInOneSimpleMinded(allInOne) {
const regex = /<tool_response>\s*<id>(.*?)<\/id>\s*<name>(.*?)<\/name>\s*<content>([\s\S]*?)<\/content>\s*<\/tool_response>/si;
const caught = allInOne.match(regex)
let data = { tool_call_id: "Error", name: "Error", content: "Error" }
@ -105,6 +105,29 @@ class ChatMessageEx {
return data
}
/**
* Extract the elements of the all in one tool call result string
* This should potentially account for content tag having xml content within to an extent.
* @param {string} allInOne
*/
static extractToolCallResultAllInOne(allInOne) {
const dParser = new DOMParser();
const got = dParser.parseFromString(allInOne, 'text/xml');
const parseErrors = got.querySelector('parseerror')
if (parseErrors) {
console.debug("WARN:ChatMessageEx:ExtractToolCallResultAllInOne:", parseErrors.textContent.trim())
}
const id = got.querySelector('id')?.textContent.trim();
const name = got.querySelector('name')?.textContent.trim();
const content = got.querySelector('content')?.textContent.trim();
let data = {
tool_call_id: id? id : "Error",
name: name? name : "Error",
content: content? content : "Error"
}
return data
}
/**
* Set extra members into the ns object
* @param {string | number} key