SimpleChatTC: Move tool calling to tools, try trap async failures

Move tool calling logic into tools module.

Try trap async promise failures by awaiting results of tool calling
and putting full thing in an outer try catch. Have forgotten the
nitty gritties of JS flow, this might help, need to check.
This commit is contained in:
hanishkvc 2025-10-12 16:08:11 +05:30
parent ef85ed41d4
commit f7284a8b89
2 changed files with 23 additions and 10 deletions

View File

@ -536,17 +536,11 @@ class SimpleChat {
if (toolname === "") {
return undefined
}
for (const fn in tools.tc_switch) {
if (fn == toolname) {
try {
tools.tc_switch[fn]["handler"](JSON.parse(ar.response.toolargs))
return tools.tc_switch[fn]["result"]
} catch (/** @type {any} */error) {
return `Tool/Function call raised an exception:${error.name}:${error.message}`
}
}
try {
return await tools.tool_call(toolname, ar.response.toolargs)
} catch (/** @type {any} */error) {
return `Tool/Function call raised an exception:${error.name}:${error.message}`
}
return `Unknown Tool/Function Call:${toolname}`
}
}

View File

@ -27,3 +27,22 @@ export function meta() {
return tools
}
/**
* Try call the specified tool/function call and return its response
* @param {string} toolname
* @param {string} toolargs
*/
export async function tool_call(toolname, toolargs) {
for (const fn in tc_switch) {
if (fn == toolname) {
try {
tc_switch[fn]["handler"](JSON.parse(toolargs))
return tc_switch[fn]["result"]
} catch (/** @type {any} */error) {
return `Tool/Function call raised an exception:${error.name}:${error.message}`
}
}
}
return `Unknown Tool/Function Call:${toolname}`
}