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:
parent
ef85ed41d4
commit
f7284a8b89
|
|
@ -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}`
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}`
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue