SimpleChatTC:ChatSessionID through the tool call cycle

Pass chatId to tool call, and use chatId in got tool call resp,
to decide as to to which chat session the async tool call resp
belongs and inturn if auto submit timer should be started if auto
is enabled.
This commit is contained in:
hanishkvc 2025-10-28 02:37:34 +05:30
parent 13d312fe0d
commit 734beb08f5
2 changed files with 18 additions and 18 deletions

View File

@ -721,7 +721,7 @@ class SimpleChat {
return "Tool/Function call name not specified"
}
try {
return await tools.tool_call(toolcallid, toolname, toolargs)
return await tools.tool_call(this.chatId, toolcallid, toolname, toolargs)
} catch (/** @type {any} */error) {
return `Tool/Function call raised an exception:${error.name}:${error.message}`
}
@ -847,10 +847,11 @@ class MultiChatUI {
*/
chat_show(chatId, bClear=true, bShowInfoAll=false) {
if (chatId != this.curChatId) {
return
return false
}
let chat = this.simpleChats[this.curChatId];
chat.show(this.elDivChat, this.elInUser, bClear, bShowInfoAll)
return true
}
/**
@ -897,21 +898,19 @@ class MultiChatUI {
})
// Handle messages from Tools web worker
tools.setup((id, name, data)=>{
tools.setup((cid, tcid, name, data)=>{
clearTimeout(this.timers.toolcallResponseTimeout)
this.timers.toolcallResponseTimeout = undefined
// TODO: Check for chat id in future so as to
// identify the right chat session to add the tc response to
// as well as to decide whether to show this chat currently or not and same with auto submit
let chat = this.simpleChats[this.curChatId]; // rather we should pick chat based on tool response's chatId
chat.add(new ChatMessageEx(Roles.ToolTemp, ChatMessageEx.createToolCallResultAllInOne(id, name, data)))
this.chat_show(chat.chatId) // one needs to use tool response's chatId
this.ui_reset_userinput(false)
if (gMe.tools.auto > 0) {
this.timers.toolcallResponseSubmitClick = setTimeout(()=>{
this.elBtnUser.click()
}, gMe.tools.auto*this.TimePeriods.ToolCallAutoTimeUnit)
let chat = this.simpleChats[cid];
chat.add(new ChatMessageEx(Roles.ToolTemp, ChatMessageEx.createToolCallResultAllInOne(tcid, name, data)))
if (this.chat_show(cid)) {
if (gMe.tools.auto > 0) {
this.timers.toolcallResponseSubmitClick = setTimeout(()=>{
this.elBtnUser.click()
}, gMe.tools.auto*this.TimePeriods.ToolCallAutoTimeUnit)
}
}
this.ui_reset_userinput(false)
})
this.elInUser.addEventListener("keyup", (ev)=> {

View File

@ -49,11 +49,11 @@ export function meta() {
/**
* Setup the callback that will be called when ever message
* is recieved from the Tools Web Worker.
* @param {(id: string, name: string, data: string) => void} cb
* @param {(chatId: string, toolCallId: string, name: string, data: string) => void} cb
*/
export function setup(cb) {
gToolsWorker.onmessage = function (ev) {
cb(ev.data.id, ev.data.name, ev.data.data)
cb(ev.data.cid, ev.data.tcid, ev.data.name, ev.data.data)
}
}
@ -62,15 +62,16 @@ export function setup(cb) {
* Try call the specified tool/function call.
* Returns undefined, if the call was placed successfully
* Else some appropriate error message will be returned.
* @param {string} chatid
* @param {string} toolcallid
* @param {string} toolname
* @param {string} toolargs
*/
export async function tool_call(toolcallid, toolname, toolargs) {
export async function tool_call(chatid, toolcallid, toolname, toolargs) {
for (const fn in tc_switch) {
if (fn == toolname) {
try {
tc_switch[fn]["handler"](toolcallid, fn, JSON.parse(toolargs))
tc_switch[fn]["handler"](chatid, toolcallid, fn, JSON.parse(toolargs))
return undefined
} catch (/** @type {any} */error) {
return `Tool/Function call raised an exception:${error.name}:${error.message}`