SimpleChatTC: Implement a simple toolcall handling flow

Checks for toolname to be defined or not in the GenAi's response

If toolname is set, then check if a corresponding tool/func exists,
and if so call the same by passing it the GenAi provided toolargs
as a object.

Inturn the text generated by the tool/func is captured and put
into the user input entry text box, with tool_response tag around
it.
This commit is contained in:
hanishkvc 2025-10-12 01:20:53 +05:30
parent fa63a86c71
commit 301910c3a1
2 changed files with 25 additions and 1 deletions

View File

@ -190,6 +190,7 @@ class SimpleChat {
/**
* Add an entry into xchat
* Also update iLastSys system prompt index tracker
* @param {string} role
* @param {string|undefined|null} content
*/
@ -398,6 +399,7 @@ class SimpleChat {
/**
* Allow setting of system prompt, at any time.
* Updates the system prompt, if one was never set or if the newly passed is different from the last set system prompt.
* @param {string} sysPrompt
* @param {string} msgTag
*/
@ -523,6 +525,24 @@ class SimpleChat {
return theResp;
}
/**
* Call the requested tool/function and get its response
* @param {AssistantResponse} ar
*/
async handle_toolcall(ar) {
let toolname = ar.response.toolname.trim();
if (toolname === "") {
return undefined
}
for (const fn in tools.tc_switch) {
if (fn == toolname) {
tools.tc_switch[fn]["handler"](JSON.parse(ar.response.toolargs))
return tools.tc_switch[fn]["result"]
}
}
return `Unknown Tool/Function Call:${toolname}`
}
}
@ -694,6 +714,10 @@ class MultiChatUI {
} else {
console.debug(`DBUG:SimpleChat:MCUI:HandleUserSubmit:ChatId has changed:[${chatId}] [${this.curChatId}]`);
}
let toolResult = await chat.handle_toolcall(theResp)
if (toolResult !== undefined) {
this.elInUser.value = `<tool_response>${toolResult}</tool_response>`
}
this.ui_reset_userinput();
}

View File

@ -11,7 +11,7 @@ import * as tjs from './tooljs.mjs'
/**
* @type {Object<string,Object<string,any>>}
*/
let tc_switch = {}
export let tc_switch = {}
export function setup() {
for (const key in tjs.tc_switch) {