diff --git a/tools/server/public_simplechat/main.js b/tools/server/public_simplechat/main.js index 0da0ea508a..c26dab47c8 100644 --- a/tools/server/public_simplechat/main.js +++ b/tools/server/public_simplechat/main.js @@ -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); } diff --git a/tools/server/public_simplechat/readme.md b/tools/server/public_simplechat/readme.md index 65f77897ec..784c5c6ef6 100644 --- a/tools/server/public_simplechat/readme.md +++ b/tools/server/public_simplechat/readme.md @@ -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 diff --git a/tools/server/public_simplechat/tools.mjs b/tools/server/public_simplechat/tools.mjs index da22de2ed0..b3aa3d843f 100644 --- a/tools/server/public_simplechat/tools.mjs +++ b/tools/server/public_simplechat/tools.mjs @@ -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); } }