Skip to content

Commit 11bb560

Browse files
committed
SimpleChatTC:MeInTools: WebWorkers in Me
Given that Me is now passed to the tools logic during setup, have the web worker handles in Me itself, instead of in tool related modules. Move setup of web worker related main thread callbacks, as well as posting messages directly to these main thread callbacks, into Me.
1 parent 861acbd commit 11bb560

File tree

5 files changed

+72
-58
lines changed

5 files changed

+72
-58
lines changed

tools/server/public_simplechat/simplechat.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,8 @@ class MultiChatUI {
10691069
this.handle_tool_run(this.curChatId);
10701070
})
10711071

1072-
// Handle messages from Tools web worker
1073-
tools.setup((cid, tcid, name, data)=>{
1072+
// Handle messages from tools web workers
1073+
this.me.workers_cb((cid, tcid, name, data)=>{
10741074
clearTimeout(this.timers.toolcallResponseTimeout)
10751075
this.timers.toolcallResponseTimeout = undefined
10761076
let chat = this.simpleChats[cid];
@@ -1386,6 +1386,10 @@ export class Me {
13861386
//"frequency_penalty": 1.2,
13871387
//"presence_penalty": 1.2,
13881388
};
1389+
this.workers = {
1390+
js: /** @type {Worker} */(/** @type {unknown} */(undefined)),
1391+
db: /** @type {Worker} */(/** @type {unknown} */(undefined)),
1392+
}
13891393
}
13901394

13911395
/**
@@ -1469,6 +1473,35 @@ export class Me {
14691473
})
14701474
}
14711475

1472-
}
1476+
/**
1477+
* Setup the callback that will be called when ever message
1478+
* is recieved from the Tools Web Workers.
1479+
* @param {(chatId: string, toolCallId: string, name: string, data: string) => void} cb
1480+
*/
1481+
workers_cb(cb) {
1482+
this.workers.js.onmessage = function (ev) {
1483+
cb(ev.data.cid, ev.data.tcid, ev.data.name, ev.data.data)
1484+
}
1485+
this.workers.db.onmessage = function (ev) {
1486+
cb(ev.data.cid, ev.data.tcid, ev.data.name, JSON.stringify(ev.data.data, (k,v)=>{
1487+
return (v === undefined) ? '__UNDEFINED__' : v;
1488+
}));
1489+
}
1490+
}
14731491

1492+
/**
1493+
* Send a message to specified tools web worker's monitor in main thread directly
1494+
* @param {Worker} worker
1495+
* @param {string} chatid
1496+
* @param {string} toolcallid
1497+
* @param {string} toolname
1498+
* @param {string} data
1499+
*/
1500+
workers_postmessage_for_main(worker, chatid, toolcallid, toolname, data) {
1501+
let mev = new MessageEvent('message', {data: {cid: chatid, tcid: toolcallid, name: toolname, data: data}});
1502+
if (worker.onmessage != null) {
1503+
worker.onmessage(mev)
1504+
}
1505+
}
14741506

1507+
}

tools/server/public_simplechat/tooldb.mjs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//@ts-check
22
// ALERT - Simple Stupid flow - Using from a discardable VM is better
33
// Helpers to handle tools/functions calling wrt data store
4-
// using a web worker.
4+
// using a db specific web worker.
55
// by Humans for All
66
//
77

8+
import * as mChatMagic from './simplechat.js'
89

9-
let gToolsDBWorker = /** @type{Worker} */(/** @type {unknown} */(null));
10+
11+
let gMe = /** @type{mChatMagic.Me} */(/** @type {unknown} */(null));
1012

1113

1214
let dsget_meta = {
@@ -93,7 +95,7 @@ let dslist_meta = {
9395
* @param {any} obj
9496
*/
9597
function dsops_run(chatid, toolcallid, toolname, obj) {
96-
gToolsDBWorker.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, args: obj})
98+
gMe.workers.db.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, args: obj})
9799
}
98100

99101

@@ -128,8 +130,8 @@ export let tc_switch = {
128130
/**
129131
* Used to get hold of the web worker to use for running tool/function call related code
130132
* Also to setup tool calls, which need to cross check things at runtime
131-
* @param {Worker} toolsWorker
133+
* @param {mChatMagic.Me} me
132134
*/
133-
export async function init(toolsWorker) {
134-
gToolsDBWorker = toolsWorker
135+
export async function init(me) {
136+
gMe = me
135137
}

tools/server/public_simplechat/tooljs.mjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
// Helpers to handle tools/functions calling wrt
44
// * javascript interpreter
55
// * simple arithmatic calculator
6-
// using a web worker.
6+
// using the js specific web worker.
77
// by Humans for All
88
//
99

10+
import * as mChatMagic from './simplechat.js'
1011

11-
let gToolsWorker = /** @type{Worker} */(/** @type {unknown} */(null));
12+
13+
let gMe = /** @type{mChatMagic.Me} */(/** @type {unknown} */(null));
1214

1315

1416
let sysdatetime_meta = {
@@ -75,9 +77,7 @@ function sysdatetime_run(chatid, toolcallid, toolname, obj) {
7577
break;
7678
}
7779
}
78-
if (gToolsWorker.onmessage != null) {
79-
gToolsWorker.onmessage(new MessageEvent('message', {data: {cid: chatid, tcid: toolcallid, name: toolname, data: sDT}}))
80-
}
80+
gMe.workers_postmessage_for_main(gMe.workers.js, chatid, toolcallid, toolname, sDT);
8181
}
8282

8383

@@ -109,7 +109,7 @@ let js_meta = {
109109
* @param {any} obj
110110
*/
111111
function js_run(chatid, toolcallid, toolname, obj) {
112-
gToolsWorker.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: obj["code"]})
112+
gMe.workers.js.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: obj["code"]})
113113
}
114114

115115

@@ -141,7 +141,7 @@ let calc_meta = {
141141
* @param {any} obj
142142
*/
143143
function calc_run(chatid, toolcallid, toolname, obj) {
144-
gToolsWorker.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: `console.log(${obj["arithexpr"]})`})
144+
gMe.workers.js.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: `console.log(${obj["arithexpr"]})`})
145145
}
146146

147147

@@ -170,8 +170,8 @@ export let tc_switch = {
170170
/**
171171
* Used to get hold of the web worker to use for running tool/function call related code
172172
* Also to setup tool calls, which need to cross check things at runtime
173-
* @param {Worker} toolsWorker
173+
* @param {mChatMagic.Me} me
174174
*/
175-
export async function init(toolsWorker) {
176-
gToolsWorker = toolsWorker
175+
export async function init(me) {
176+
gMe = me
177177
}

tools/server/public_simplechat/tools.mjs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,44 @@ import * as tdb from './tooldb.mjs'
1111
import * as mChatMagic from './simplechat.js'
1212

1313

14-
let gToolsWorker = new Worker('./toolsworker.mjs', { type: 'module' });
15-
let gToolsDBWorker = new Worker('./toolsdbworker.mjs', { type: 'module' });
1614
/**
1715
* Maintain currently available tool/function calls
1816
* @type {Object<string,Object<string,any>>}
1917
*/
2018
export let tc_switch = {}
2119

2220

21+
/**
22+
* @param {mChatMagic.Me} me
23+
*/
24+
function setup_workers(me) {
25+
me.workers.js = new Worker('./toolsworker.mjs', { type: 'module' });
26+
me.workers.db = new Worker('./toolsdbworker.mjs', { type: 'module' });
27+
}
28+
29+
2330
/**
2431
* @param {mChatMagic.Me} me
2532
*/
2633
export async function init(me) {
34+
setup_workers(me);
2735
/**
2836
* @type {string[]}
2937
*/
3038
let toolNames = []
31-
await tjs.init(gToolsWorker).then(()=>{
39+
await tjs.init(me).then(()=>{
3240
for (const key in tjs.tc_switch) {
3341
tc_switch[key] = tjs.tc_switch[key]
3442
toolNames.push(key)
3543
}
3644
})
37-
await tdb.init(gToolsDBWorker).then(()=>{
45+
await tdb.init(me).then(()=>{
3846
for (const key in tdb.tc_switch) {
3947
tc_switch[key] = tdb.tc_switch[key]
4048
toolNames.push(key)
4149
}
4250
})
43-
let tNs = await tweb.init(gToolsWorker, me)
51+
let tNs = await tweb.init(me)
4452
for (const key in tNs) {
4553
tc_switch[key] = tNs[key]
4654
toolNames.push(key)
@@ -58,23 +66,6 @@ export function meta() {
5866
}
5967

6068

61-
/**
62-
* Setup the callback that will be called when ever message
63-
* is recieved from the Tools Web Worker.
64-
* @param {(chatId: string, toolCallId: string, name: string, data: string) => void} cb
65-
*/
66-
export function setup(cb) {
67-
gToolsWorker.onmessage = function (ev) {
68-
cb(ev.data.cid, ev.data.tcid, ev.data.name, ev.data.data)
69-
}
70-
gToolsDBWorker.onmessage = function (ev) {
71-
cb(ev.data.cid, ev.data.tcid, ev.data.name, JSON.stringify(ev.data.data, (k,v)=>{
72-
return (v === undefined) ? '__UNDEFINED__' : v;
73-
}));
74-
}
75-
}
76-
77-
7869
/**
7970
* Try call the specified tool/function call.
8071
* Returns undefined, if the call was placed successfully

tools/server/public_simplechat/toolweb.mjs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,19 @@
22
// ALERT - Simple Stupid flow - Using from a discardable VM is better
33
// Helpers to handle tools/functions calling related to web access, pdf, etal
44
// which work in sync with the bundled simpleproxy.py server logic.
5+
// Uses the js specific web worker path.
56
// by Humans for All
67
//
78

89
import * as mChatMagic from './simplechat.js'
910

1011

11-
let gToolsWorker = /** @type{Worker} */(/** @type {unknown} */(null));
1212
/**
1313
* @type {mChatMagic.Me}
1414
*/
1515
let gMe = /** @type{mChatMagic.Me} */(/** @type {unknown} */(null));
1616

1717

18-
/**
19-
* Send a message to Tools WebWorker's monitor in main thread directly
20-
* @param {MessageEvent<any>} mev
21-
*/
22-
function message_toolsworker(mev) {
23-
// @ts-ignore
24-
gToolsWorker.onmessage(mev)
25-
}
26-
27-
2818
/**
2919
* For now hash the shared secret with the year.
3020
*/
@@ -51,7 +41,7 @@ async function bearer_transform() {
5141
* @param {any} objHeaders
5242
*/
5343
async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchParams, path, objHeaders={}) {
54-
if (gToolsWorker.onmessage != null) {
44+
if (gMe.workers.js.onmessage != null) {
5545
let params = new URLSearchParams(objSearchParams)
5646
let newUrl = `${gMe.tools.proxyUrl}/${path}?${params}`
5747
let headers = new Headers(objHeaders)
@@ -63,9 +53,9 @@ async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchPa
6353
}
6454
return resp.text()
6555
}).then(data => {
66-
message_toolsworker(new MessageEvent('message', {data: {cid: chatid, tcid: toolcallid, name: toolname, data: data}}))
56+
gMe.workers_postmessage_for_main(gMe.workers.js, chatid, toolcallid, toolname, data);
6757
}).catch((err)=>{
68-
message_toolsworker(new MessageEvent('message', {data: {cid: chatid, tcid: toolcallid, name: toolname, data: `Error:${err}`}}))
58+
gMe.workers_postmessage_for_main(gMe.workers.js, chatid, toolcallid, toolname, `Error:${err}`);
6959
})
7060
}
7161
}
@@ -343,15 +333,13 @@ async function fetchpdftext_setup(tcs) {
343333
/**
344334
* Used to get hold of the web worker to use for running tool/function call related code
345335
* Also to setup tool calls, which need to cross check things at runtime
346-
* @param {Worker} toolsWorker
347336
* @param {mChatMagic.Me} me
348337
*/
349-
export async function init(toolsWorker, me) {
338+
export async function init(me) {
350339
/**
351340
* @type {Object<string, Object<string, any>>} tcs
352341
*/
353342
let tc_switch = {}
354-
gToolsWorker = toolsWorker
355343
gMe = me
356344
await fetchweburlraw_setup(tc_switch)
357345
await fetchweburltext_setup(tc_switch)

0 commit comments

Comments
 (0)