Skip to content

Commit 05a2448

Browse files
committed
SimpleChatTC: Get ready for decoupled tool call response
tools manager/module * setup the web worker that will help execute the tool call related codes in a js environment that is isolated from the browsers main js environment * pass the web worker to the tool call providers, for them to use * dont wait for the result from the tool call, as it will be got later asynchronously through a message * allow users of the tools manager to register a call back, which will be called when ever a message is got from the web worker containing response wrt previously requested tool call execution. simplechat * decouple toolcall response handling and toolcall requesting logic * setup a timeout to take back control if tool call takes up too much time. Inturn help alert the ai model, that the tool call took up too much time and so was aborted, by placing a approriate tagged tool response into user query area. * register a call back that will be called when response is got asynchronously wrt anye requested tool calls. In turn take care of updating the user query area with response got wrt the tool call, along with tool response tag around it.
1 parent 75f6e57 commit 05a2448

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

tools/server/public_simplechat/simplechat.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,13 +539,15 @@ class SimpleChat {
539539
}
540540

541541
/**
542-
* Call the requested tool/function and get its response
542+
* Call the requested tool/function.
543+
* Returns undefined, if the call was placed successfully
544+
* Else some appropriate error message will be returned.
543545
* @param {string} toolname
544546
* @param {string} toolargs
545547
*/
546548
async handle_toolcall(toolname, toolargs) {
547549
if (toolname === "") {
548-
return undefined
550+
return "Tool/Function call name not specified"
549551
}
550552
try {
551553
return await tools.tool_call(toolname, toolargs)
@@ -675,6 +677,11 @@ class MultiChatUI {
675677
this.handle_tool_run(this.curChatId);
676678
})
677679

680+
tools.setup((name, data)=>{
681+
this.elInUser.value = `<tool_response>${data}</tool_response>`
682+
this.ui_reset_userinput(false)
683+
})
684+
678685
this.elInUser.addEventListener("keyup", (ev)=> {
679686
// allow user to insert enter into their message using shift+enter.
680687
// while just pressing enter key will lead to submitting.
@@ -771,17 +778,24 @@ class MultiChatUI {
771778

772779
/**
773780
* Handle running of specified tool call if any, for the specified chat session.
781+
* Also sets up a timeout, so that user gets control back to interact with the ai model.
774782
* @param {string} chatId
775783
*/
776784
async handle_tool_run(chatId) {
777785
let chat = this.simpleChats[chatId];
778786
this.elInUser.value = "toolcall in progress...";
779787
this.elInUser.disabled = true;
780-
let toolResult = await chat.handle_toolcall(this.elInToolName.value, this.elInToolArgs.value)
788+
let toolname = this.elInToolName.value.trim()
789+
let toolResult = await chat.handle_toolcall(toolname, this.elInToolArgs.value)
781790
if (toolResult !== undefined) {
782791
this.elInUser.value = `<tool_response>${toolResult}</tool_response>`
792+
this.ui_reset_userinput(false)
793+
} else {
794+
setTimeout(() => {
795+
this.elInUser.value = `<tool_response>Tool/Function call ${toolname} taking too much time, aborting...</tool_response>`
796+
this.ui_reset_userinput(false)
797+
}, 10000)
783798
}
784-
this.ui_reset_userinput(toolResult === undefined);
785799
}
786800

787801
/**
@@ -1073,7 +1087,7 @@ function startme() {
10731087
document["gMe"] = gMe;
10741088
document["du"] = du;
10751089
document["tools"] = tools;
1076-
tools.setup()
1090+
tools.init()
10771091
for (let cid of gMe.defaultChatIds) {
10781092
gMe.multiChat.new_chat_session(cid);
10791093
}

tools/server/public_simplechat/tools.mjs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
import * as tjs from './tooljs.mjs'
99

1010

11+
let gToolsWorker = new Worker('./toolsworker.mjs');
1112
/**
1213
* @type {Object<string,Object<string,any>>}
1314
*/
1415
export let tc_switch = {}
1516

16-
export function setup() {
17+
export function init() {
18+
tjs.init(gToolsWorker)
1719
for (const key in tjs.tc_switch) {
1820
tc_switch[key] = tjs.tc_switch[key]
1921
}
@@ -27,9 +29,22 @@ export function meta() {
2729
return tools
2830
}
2931

32+
/**
33+
* Setup the callback that will be called when ever message
34+
* is recieved from the Tools Web Worker.
35+
* @param {(name: string, data: string) => void} cb
36+
*/
37+
export function setup(cb) {
38+
gToolsWorker.onmessage = function (ev) {
39+
cb(ev.data.name, ev.data.data)
40+
}
41+
}
42+
3043

3144
/**
32-
* Try call the specified tool/function call and return its response
45+
* Try call the specified tool/function call.
46+
* Returns undefined, if the call was placed successfully
47+
* Else some appropriate error message will be returned.
3348
* @param {string} toolname
3449
* @param {string} toolargs
3550
*/
@@ -38,7 +53,7 @@ export async function tool_call(toolname, toolargs) {
3853
if (fn == toolname) {
3954
try {
4055
tc_switch[fn]["handler"](fn, JSON.parse(toolargs))
41-
return tc_switch[fn]["result"]
56+
return undefined
4257
} catch (/** @type {any} */error) {
4358
return `Tool/Function call raised an exception:${error.name}:${error.message}`
4459
}

0 commit comments

Comments
 (0)