Skip to content

Commit fbb3a3d

Browse files
committed
SimpleChatTC:ToolsManager: Cleanup inc delayed direct posting
Me.tools.toolNames is now directly updated by init of ToolsManager The two then in the old tools.init was also unneeded then also as both could have been merged into a single then, even then. However with the new flow, the 1st then is no longer required. Also now the direct calling of onmessage handler on the main thread side wrt immidiate result from tool call is delayed for a cycling through the events loop, by using a setTimeout. No longer expose the tools module throught documents, given that the tools module mainly contains ToolsManager, whose only instance is available through the global gMe. Move the devel related exposing throught document object into a function of its own.
1 parent 6ed5f17 commit fbb3a3d

File tree

3 files changed

+48
-22
lines changed

3 files changed

+48
-22
lines changed

tools/server/public_simplechat/main.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
// @ts-check
2-
// A simple completions and chat/completions test related web front end logic
2+
// A simple implementation of GenAi/LLM chat web client ui / front end logic.
3+
// It handshake with ai server's completions and chat/completions endpoints
4+
// and helps with basic usage and testing.
35
// by Humans for All
46

57

68
import * as mChatMagic from './simplechat.js'
7-
import * as tools from "./tools.mjs"
89
import * as du from "./datautils.mjs";
910

1011

11-
1212
/** @type {mChatMagic.Me} */
1313
let gMe;
1414

15-
function startme() {
16-
console.log("INFO:SimpleChat:StartMe:Starting...");
17-
gMe = new mChatMagic.Me();
18-
gMe.debug_disable();
15+
16+
function devel_expose() {
1917
// @ts-ignore
2018
document["gMe"] = gMe;
2119
// @ts-ignore
2220
document["du"] = du;
23-
// @ts-ignore
24-
document["tools"] = tools;
25-
tools.init(gMe).then((toolNames)=>gMe.tools.toolNames=toolNames).then(()=>gMe.multiChat.chat_show(gMe.multiChat.curChatId))
21+
}
22+
23+
24+
function startme() {
25+
console.log("INFO:SimpleChat:StartMe:Starting...");
26+
gMe = new mChatMagic.Me();
27+
gMe.debug_disable();
28+
devel_expose()
29+
gMe.toolsMgr.init(gMe).then(()=>{
30+
gMe.multiChat.chat_show(gMe.multiChat.curChatId);
31+
})
2632
for (let cid of gMe.defaultChatIds) {
2733
gMe.multiChat.new_chat_session(cid);
2834
}

tools/server/public_simplechat/readme.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,9 +632,17 @@ sliding window based drop off or even before they kick in, this can help in many
632632

633633
* sys_date_time tool call has been added.
634634

635-
* SimpleChat - Move the main chat related classes into its own js module file, independent of the
636-
main runtime entry point. This allows these classes to be referenced from other modules like tools
637-
related modules with full access to their details for developers and static check tools.
635+
* Refactor code and flow a bit wrt the client web ui
636+
* Move the main chat related classes into its own js module file, independent of the main
637+
runtime entry point (rather move out the runtime entry point into its own file). This allows
638+
these classes to be referenced from other modules like tools related modules with full access
639+
to these classes's details for developers and static check tools.
640+
* building on same make the Tools management code into a ToolsManager class which is inturn
641+
instantiated and the handle stored in top level Me class. This class also maintains and
642+
manages the web workers as well as routing of the tool calling among others.
643+
* add a common helper for posting results directly to the main thread side web worker callback
644+
handlers. Inturn run the calling through a setTimeout0, so that delayed/missing response
645+
situation rescuer timeout logic etal flow doesnt get messed for now.
638646

639647

640648
#### ToDo

tools/server/public_simplechat/tools.mjs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,24 @@ export class ToolsManager {
4343
/**
4444
* @type {string[]}
4545
*/
46-
let toolNames = []
46+
me.tools.toolNames = []
4747
await tjs.init(me).then(()=>{
4848
for (const key in tjs.tc_switch) {
4949
this.tc_switch[key] = tjs.tc_switch[key]
50-
toolNames.push(key)
50+
me.tools.toolNames.push(key)
5151
}
5252
})
5353
await tdb.init(me).then(()=>{
5454
for (const key in tdb.tc_switch) {
5555
this.tc_switch[key] = tdb.tc_switch[key]
56-
toolNames.push(key)
56+
me.tools.toolNames.push(key)
5757
}
5858
})
5959
let tNs = await tweb.init(me)
6060
for (const key in tNs) {
6161
this.tc_switch[key] = tNs[key]
62-
toolNames.push(key)
62+
me.tools.toolNames.push(key)
6363
}
64-
return toolNames
6564
}
6665

6766
/**
@@ -115,7 +114,18 @@ export class ToolsManager {
115114
}
116115

117116
/**
118-
* Send a message to specified tools web worker's monitor in main thread directly
117+
* Send message to specified Tools-WebWorker's monitor/onmessage handler of main thread
118+
* by calling it directly.
119+
*
120+
* The specified web worker's main thread monitor/callback logic is triggerd in a delayed
121+
* manner by cycling the call through the events loop by using a setTimeout 0, so that the
122+
* callback gets executed only after the caller's code following the call to this helper
123+
* is done.
124+
*
125+
* NOTE: This is needed to ensure that any tool call handler that returns the tool call
126+
* result immidiately without using any asynhronous mechanism, doesnt get-messed-by /
127+
* mess-with the delayed response identifier and rescuer timeout logic.
128+
*
119129
* @param {Worker} worker
120130
* @param {string} chatid
121131
* @param {string} toolcallid
@@ -124,9 +134,11 @@ export class ToolsManager {
124134
*/
125135
workers_postmessage_for_main(worker, chatid, toolcallid, toolname, data) {
126136
let mev = new MessageEvent('message', {data: {cid: chatid, tcid: toolcallid, name: toolname, data: data}});
127-
if (worker.onmessage != null) {
128-
worker.onmessage(mev)
129-
}
137+
setTimeout(function() {
138+
if (worker.onmessage != null) {
139+
worker.onmessage(mev)
140+
}
141+
}, 0);
130142
}
131143

132144
}

0 commit comments

Comments
 (0)