Skip to content

Commit 3e7f1ae

Browse files
committed
SimpleChatTC:ToolsManager: Instantiate in Me and Use
Rename Tools to ToolsManager to convey its semantic better. Move setup of workers onmessage callback as well as directly passing result to these callbacks into ToolsManager. Now that Workers have been moved into ToolsManager, and ToolsManager has been instantiated as a member of Me, use the same in place of prev workers of Me.
1 parent 504d9ea commit 3e7f1ae

File tree

5 files changed

+47
-47
lines changed

5 files changed

+47
-47
lines changed

tools/server/public_simplechat/simplechat.js

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as du from "./datautils.mjs";
77
import * as ui from "./ui.mjs"
8-
import * as tools from "./tools.mjs"
8+
import * as mTools from "./tools.mjs"
99

1010

1111
class Roles {
@@ -522,7 +522,7 @@ class SimpleChat {
522522
obj["stream"] = true;
523523
}
524524
if (this.me.tools.enabled) {
525-
obj["tools"] = tools.meta();
525+
obj["tools"] = this.me.toolsMgr.meta();
526526
}
527527
return JSON.stringify(obj);
528528
}
@@ -751,7 +751,7 @@ class SimpleChat {
751751
return "Tool/Function call name not specified"
752752
}
753753
try {
754-
return await tools.tool_call(this.chatId, toolcallid, toolname, toolargs)
754+
return await this.me.toolsMgr.tool_call(this.chatId, toolcallid, toolname, toolargs)
755755
} catch (/** @type {any} */error) {
756756
return `Tool/Function call raised an exception:${error.name}:${error.message}`
757757
}
@@ -1070,7 +1070,7 @@ class MultiChatUI {
10701070
})
10711071

10721072
// Handle messages from tools web workers
1073-
this.me.workers_cb((cid, tcid, name, data)=>{
1073+
this.me.toolsMgr.workers_cb((cid, tcid, name, data)=>{
10741074
clearTimeout(this.timers.toolcallResponseTimeout)
10751075
this.timers.toolcallResponseTimeout = undefined
10761076
let chat = this.simpleChats[cid];
@@ -1386,10 +1386,7 @@ 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-
}
1389+
this.toolsMgr = new mTools.ToolsManager()
13931390
}
13941391

13951392
/**
@@ -1473,35 +1470,4 @@ export class Me {
14731470
})
14741471
}
14751472

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-
}
1491-
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-
}
1506-
15071473
}

tools/server/public_simplechat/tooldb.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ let dslist_meta = {
9595
* @param {any} obj
9696
*/
9797
function dsops_run(chatid, toolcallid, toolname, obj) {
98-
gMe.workers.db.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, args: obj})
98+
gMe.toolsMgr.workers.db.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, args: obj})
9999
}
100100

101101

tools/server/public_simplechat/tooljs.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ function sysdatetime_run(chatid, toolcallid, toolname, obj) {
7777
break;
7878
}
7979
}
80-
gMe.workers_postmessage_for_main(gMe.workers.js, chatid, toolcallid, toolname, sDT);
80+
gMe.toolsMgr.workers_postmessage_for_main(gMe.toolsMgr.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-
gMe.workers.js.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: obj["code"]})
112+
gMe.toolsMgr.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-
gMe.workers.js.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: `console.log(${obj["arithexpr"]})`})
144+
gMe.toolsMgr.workers.js.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: `console.log(${obj["arithexpr"]})`})
145145
}
146146

147147

tools/server/public_simplechat/tools.mjs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as mChatMagic from './simplechat.js'
1212

1313

1414

15-
class Tools {
15+
export class ToolsManager {
1616

1717
constructor() {
1818
/**
@@ -34,6 +34,8 @@ class Tools {
3434
}
3535

3636
/**
37+
* Initialise the ToolsManager,
38+
* including all the different tools groups.
3739
* @param {mChatMagic.Me} me
3840
*/
3941
async init(me) {
@@ -62,6 +64,9 @@ class Tools {
6264
return toolNames
6365
}
6466

67+
/**
68+
* Prepare the tools meta data that can be passed to the ai server.
69+
*/
6570
meta() {
6671
let tools = []
6772
for (const key in this.tc_switch) {
@@ -93,6 +98,35 @@ class Tools {
9398
return `Unknown Tool/Function Call:${toolname}`
9499
}
95100

101+
/**
102+
* Setup the callback that will be called when ever message
103+
* is recieved from the Tools Web Workers.
104+
* @param {(chatId: string, toolCallId: string, name: string, data: string) => void} cb
105+
*/
106+
workers_cb(cb) {
107+
this.workers.js.onmessage = function (ev) {
108+
cb(ev.data.cid, ev.data.tcid, ev.data.name, ev.data.data)
109+
}
110+
this.workers.db.onmessage = function (ev) {
111+
cb(ev.data.cid, ev.data.tcid, ev.data.name, JSON.stringify(ev.data.data, (k,v)=>{
112+
return (v === undefined) ? '__UNDEFINED__' : v;
113+
}));
114+
}
115+
}
96116

117+
/**
118+
* Send a message to specified tools web worker's monitor in main thread directly
119+
* @param {Worker} worker
120+
* @param {string} chatid
121+
* @param {string} toolcallid
122+
* @param {string} toolname
123+
* @param {string} data
124+
*/
125+
workers_postmessage_for_main(worker, chatid, toolcallid, toolname, data) {
126+
let mev = new MessageEvent('message', {data: {cid: chatid, tcid: toolcallid, name: toolname, data: data}});
127+
if (worker.onmessage != null) {
128+
worker.onmessage(mev)
129+
}
130+
}
97131

98132
}

tools/server/public_simplechat/toolweb.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async function bearer_transform() {
4141
* @param {any} objHeaders
4242
*/
4343
async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchParams, path, objHeaders={}) {
44-
if (gMe.workers.js.onmessage != null) {
44+
if (gMe.toolsMgr.workers.js.onmessage != null) {
4545
let params = new URLSearchParams(objSearchParams)
4646
let newUrl = `${gMe.tools.proxyUrl}/${path}?${params}`
4747
let headers = new Headers(objHeaders)
@@ -53,9 +53,9 @@ async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchPa
5353
}
5454
return resp.text()
5555
}).then(data => {
56-
gMe.workers_postmessage_for_main(gMe.workers.js, chatid, toolcallid, toolname, data);
56+
gMe.toolsMgr.workers_postmessage_for_main(gMe.toolsMgr.workers.js, chatid, toolcallid, toolname, data);
5757
}).catch((err)=>{
58-
gMe.workers_postmessage_for_main(gMe.workers.js, chatid, toolcallid, toolname, `Error:${err}`);
58+
gMe.toolsMgr.workers_postmessage_for_main(gMe.toolsMgr.workers.js, chatid, toolcallid, toolname, `Error:${err}`);
5959
})
6060
}
6161
}

0 commit comments

Comments
 (0)