Skip to content

Commit a74abce

Browse files
committed
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.
1 parent d86bcc5 commit a74abce

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

tools/server/public_simplechat/simplechat.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ class SimpleChat {
721721
return "Tool/Function call name not specified"
722722
}
723723
try {
724-
return await tools.tool_call(toolcallid, toolname, toolargs)
724+
return await tools.tool_call(this.chatId, toolcallid, toolname, toolargs)
725725
} catch (/** @type {any} */error) {
726726
return `Tool/Function call raised an exception:${error.name}:${error.message}`
727727
}
@@ -847,10 +847,11 @@ class MultiChatUI {
847847
*/
848848
chat_show(chatId, bClear=true, bShowInfoAll=false) {
849849
if (chatId != this.curChatId) {
850-
return
850+
return false
851851
}
852852
let chat = this.simpleChats[this.curChatId];
853853
chat.show(this.elDivChat, this.elInUser, bClear, bShowInfoAll)
854+
return true
854855
}
855856

856857
/**
@@ -897,21 +898,19 @@ class MultiChatUI {
897898
})
898899

899900
// Handle messages from Tools web worker
900-
tools.setup((id, name, data)=>{
901+
tools.setup((cid, tcid, name, data)=>{
901902
clearTimeout(this.timers.toolcallResponseTimeout)
902903
this.timers.toolcallResponseTimeout = undefined
903-
// TODO: Check for chat id in future so as to
904-
// identify the right chat session to add the tc response to
905-
// as well as to decide whether to show this chat currently or not and same with auto submit
906-
let chat = this.simpleChats[this.curChatId]; // rather we should pick chat based on tool response's chatId
907-
chat.add(new ChatMessageEx(Roles.ToolTemp, ChatMessageEx.createToolCallResultAllInOne(id, name, data)))
908-
this.chat_show(chat.chatId) // one needs to use tool response's chatId
909-
this.ui_reset_userinput(false)
910-
if (gMe.tools.auto > 0) {
911-
this.timers.toolcallResponseSubmitClick = setTimeout(()=>{
912-
this.elBtnUser.click()
913-
}, gMe.tools.auto*this.TimePeriods.ToolCallAutoTimeUnit)
904+
let chat = this.simpleChats[cid];
905+
chat.add(new ChatMessageEx(Roles.ToolTemp, ChatMessageEx.createToolCallResultAllInOne(tcid, name, data)))
906+
if (this.chat_show(cid)) {
907+
if (gMe.tools.auto > 0) {
908+
this.timers.toolcallResponseSubmitClick = setTimeout(()=>{
909+
this.elBtnUser.click()
910+
}, gMe.tools.auto*this.TimePeriods.ToolCallAutoTimeUnit)
911+
}
914912
}
913+
this.ui_reset_userinput(false)
915914
})
916915

917916
this.elInUser.addEventListener("keyup", (ev)=> {

tools/server/public_simplechat/tools.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ export function meta() {
4949
/**
5050
* Setup the callback that will be called when ever message
5151
* is recieved from the Tools Web Worker.
52-
* @param {(id: string, name: string, data: string) => void} cb
52+
* @param {(chatId: string, toolCallId: string, name: string, data: string) => void} cb
5353
*/
5454
export function setup(cb) {
5555
gToolsWorker.onmessage = function (ev) {
56-
cb(ev.data.id, ev.data.name, ev.data.data)
56+
cb(ev.data.cid, ev.data.tcid, ev.data.name, ev.data.data)
5757
}
5858
}
5959

@@ -62,15 +62,16 @@ export function setup(cb) {
6262
* Try call the specified tool/function call.
6363
* Returns undefined, if the call was placed successfully
6464
* Else some appropriate error message will be returned.
65+
* @param {string} chatid
6566
* @param {string} toolcallid
6667
* @param {string} toolname
6768
* @param {string} toolargs
6869
*/
69-
export async function tool_call(toolcallid, toolname, toolargs) {
70+
export async function tool_call(chatid, toolcallid, toolname, toolargs) {
7071
for (const fn in tc_switch) {
7172
if (fn == toolname) {
7273
try {
73-
tc_switch[fn]["handler"](toolcallid, fn, JSON.parse(toolargs))
74+
tc_switch[fn]["handler"](chatid, toolcallid, fn, JSON.parse(toolargs))
7475
return undefined
7576
} catch (/** @type {any} */error) {
7677
return `Tool/Function call raised an exception:${error.name}:${error.message}`

0 commit comments

Comments
 (0)