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.
This commit is contained in:
parent
4d71ded5df
commit
b2e7f5fd44
|
|
@ -1,28 +1,34 @@
|
|||
// @ts-check
|
||||
// A simple completions and chat/completions test related web front end logic
|
||||
// A simple implementation of GenAi/LLM chat web client ui / front end logic.
|
||||
// It handshake with ai server's completions and chat/completions endpoints
|
||||
// and helps with basic usage and testing.
|
||||
// by Humans for All
|
||||
|
||||
|
||||
import * as mChatMagic from './simplechat.js'
|
||||
import * as tools from "./tools.mjs"
|
||||
import * as du from "./datautils.mjs";
|
||||
|
||||
|
||||
|
||||
/** @type {mChatMagic.Me} */
|
||||
let gMe;
|
||||
|
||||
function startme() {
|
||||
console.log("INFO:SimpleChat:StartMe:Starting...");
|
||||
gMe = new mChatMagic.Me();
|
||||
gMe.debug_disable();
|
||||
|
||||
function devel_expose() {
|
||||
// @ts-ignore
|
||||
document["gMe"] = gMe;
|
||||
// @ts-ignore
|
||||
document["du"] = du;
|
||||
// @ts-ignore
|
||||
document["tools"] = tools;
|
||||
tools.init(gMe).then((toolNames)=>gMe.tools.toolNames=toolNames).then(()=>gMe.multiChat.chat_show(gMe.multiChat.curChatId))
|
||||
}
|
||||
|
||||
|
||||
function startme() {
|
||||
console.log("INFO:SimpleChat:StartMe:Starting...");
|
||||
gMe = new mChatMagic.Me();
|
||||
gMe.debug_disable();
|
||||
devel_expose()
|
||||
gMe.toolsMgr.init(gMe).then(()=>{
|
||||
gMe.multiChat.chat_show(gMe.multiChat.curChatId);
|
||||
})
|
||||
for (let cid of gMe.defaultChatIds) {
|
||||
gMe.multiChat.new_chat_session(cid);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -632,9 +632,17 @@ sliding window based drop off or even before they kick in, this can help in many
|
|||
|
||||
* sys_date_time tool call has been added.
|
||||
|
||||
* SimpleChat - Move the main chat related classes into its own js module file, independent of the
|
||||
main runtime entry point. This allows these classes to be referenced from other modules like tools
|
||||
related modules with full access to their details for developers and static check tools.
|
||||
* Refactor code and flow a bit wrt the client web ui
|
||||
* Move the main chat related classes into its own js module file, independent of the main
|
||||
runtime entry point (rather move out the runtime entry point into its own file). This allows
|
||||
these classes to be referenced from other modules like tools related modules with full access
|
||||
to these classes's details for developers and static check tools.
|
||||
* building on same make the Tools management code into a ToolsManager class which is inturn
|
||||
instantiated and the handle stored in top level Me class. This class also maintains and
|
||||
manages the web workers as well as routing of the tool calling among others.
|
||||
* add a common helper for posting results directly to the main thread side web worker callback
|
||||
handlers. Inturn run the calling through a setTimeout0, so that delayed/missing response
|
||||
situation rescuer timeout logic etal flow doesnt get messed for now.
|
||||
|
||||
|
||||
#### ToDo
|
||||
|
|
|
|||
|
|
@ -43,25 +43,24 @@ export class ToolsManager {
|
|||
/**
|
||||
* @type {string[]}
|
||||
*/
|
||||
let toolNames = []
|
||||
me.tools.toolNames = []
|
||||
await tjs.init(me).then(()=>{
|
||||
for (const key in tjs.tc_switch) {
|
||||
this.tc_switch[key] = tjs.tc_switch[key]
|
||||
toolNames.push(key)
|
||||
me.tools.toolNames.push(key)
|
||||
}
|
||||
})
|
||||
await tdb.init(me).then(()=>{
|
||||
for (const key in tdb.tc_switch) {
|
||||
this.tc_switch[key] = tdb.tc_switch[key]
|
||||
toolNames.push(key)
|
||||
me.tools.toolNames.push(key)
|
||||
}
|
||||
})
|
||||
let tNs = await tweb.init(me)
|
||||
for (const key in tNs) {
|
||||
this.tc_switch[key] = tNs[key]
|
||||
toolNames.push(key)
|
||||
me.tools.toolNames.push(key)
|
||||
}
|
||||
return toolNames
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -115,7 +114,18 @@ export class ToolsManager {
|
|||
}
|
||||
|
||||
/**
|
||||
* Send a message to specified tools web worker's monitor in main thread directly
|
||||
* Send message to specified Tools-WebWorker's monitor/onmessage handler of main thread
|
||||
* by calling it directly.
|
||||
*
|
||||
* The specified web worker's main thread monitor/callback logic is triggerd in a delayed
|
||||
* manner by cycling the call through the events loop by using a setTimeout 0, so that the
|
||||
* callback gets executed only after the caller's code following the call to this helper
|
||||
* is done.
|
||||
*
|
||||
* NOTE: This is needed to ensure that any tool call handler that returns the tool call
|
||||
* result immidiately without using any asynhronous mechanism, doesnt get-messed-by /
|
||||
* mess-with the delayed response identifier and rescuer timeout logic.
|
||||
*
|
||||
* @param {Worker} worker
|
||||
* @param {string} chatid
|
||||
* @param {string} toolcallid
|
||||
|
|
@ -124,9 +134,11 @@ export class ToolsManager {
|
|||
*/
|
||||
workers_postmessage_for_main(worker, chatid, toolcallid, toolname, data) {
|
||||
let mev = new MessageEvent('message', {data: {cid: chatid, tcid: toolcallid, name: toolname, data: data}});
|
||||
if (worker.onmessage != null) {
|
||||
worker.onmessage(mev)
|
||||
}
|
||||
setTimeout(function() {
|
||||
if (worker.onmessage != null) {
|
||||
worker.onmessage(mev)
|
||||
}
|
||||
}, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue