Skip to content

Commit 504d9ea

Browse files
committed
SimpleChatTC:Rather bring in Tools Class
So that all tools related management logic sits in tools module itself, but is accessible from Me by having a instance of Tools. The Workers moved into Tools class. The tc_switch moved into Tools class. The setup_workers, init, meta and tool_call moved into Tools class.
1 parent 11bb560 commit 504d9ea

File tree

1 file changed

+69
-61
lines changed

1 file changed

+69
-61
lines changed

tools/server/public_simplechat/tools.mjs

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,80 +11,88 @@ import * as tdb from './tooldb.mjs'
1111
import * as mChatMagic from './simplechat.js'
1212

1313

14-
/**
15-
* Maintain currently available tool/function calls
16-
* @type {Object<string,Object<string,any>>}
17-
*/
18-
export let tc_switch = {}
1914

15+
class Tools {
2016

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-
}
17+
constructor() {
18+
/**
19+
* Maintain currently available tool/function calls
20+
* @type {Object<string,Object<string,any>>}
21+
*/
22+
this.tc_switch = {}
23+
24+
this.workers = {
25+
js: /** @type {Worker} */(/** @type {unknown} */(undefined)),
26+
db: /** @type {Worker} */(/** @type {unknown} */(undefined)),
27+
}
28+
29+
}
2830

31+
setup_workers() {
32+
this.workers.js = new Worker('./toolsworker.mjs', { type: 'module' });
33+
this.workers.db = new Worker('./toolsdbworker.mjs', { type: 'module' });
34+
}
2935

30-
/**
31-
* @param {mChatMagic.Me} me
32-
*/
33-
export async function init(me) {
34-
setup_workers(me);
3536
/**
36-
* @type {string[]}
37+
* @param {mChatMagic.Me} me
3738
*/
38-
let toolNames = []
39-
await tjs.init(me).then(()=>{
40-
for (const key in tjs.tc_switch) {
41-
tc_switch[key] = tjs.tc_switch[key]
42-
toolNames.push(key)
43-
}
44-
})
45-
await tdb.init(me).then(()=>{
46-
for (const key in tdb.tc_switch) {
47-
tc_switch[key] = tdb.tc_switch[key]
39+
async init(me) {
40+
this.setup_workers();
41+
/**
42+
* @type {string[]}
43+
*/
44+
let toolNames = []
45+
await tjs.init(me).then(()=>{
46+
for (const key in tjs.tc_switch) {
47+
this.tc_switch[key] = tjs.tc_switch[key]
48+
toolNames.push(key)
49+
}
50+
})
51+
await tdb.init(me).then(()=>{
52+
for (const key in tdb.tc_switch) {
53+
this.tc_switch[key] = tdb.tc_switch[key]
54+
toolNames.push(key)
55+
}
56+
})
57+
let tNs = await tweb.init(me)
58+
for (const key in tNs) {
59+
this.tc_switch[key] = tNs[key]
4860
toolNames.push(key)
4961
}
50-
})
51-
let tNs = await tweb.init(me)
52-
for (const key in tNs) {
53-
tc_switch[key] = tNs[key]
54-
toolNames.push(key)
62+
return toolNames
5563
}
56-
return toolNames
57-
}
58-
5964

60-
export function meta() {
61-
let tools = []
62-
for (const key in tc_switch) {
63-
tools.push(tc_switch[key]["meta"])
65+
meta() {
66+
let tools = []
67+
for (const key in this.tc_switch) {
68+
tools.push(this.tc_switch[key]["meta"])
69+
}
70+
return tools
6471
}
65-
return tools
66-
}
67-
6872

69-
/**
70-
* Try call the specified tool/function call.
71-
* Returns undefined, if the call was placed successfully
72-
* Else some appropriate error message will be returned.
73-
* @param {string} chatid
74-
* @param {string} toolcallid
75-
* @param {string} toolname
76-
* @param {string} toolargs
77-
*/
78-
export async function tool_call(chatid, toolcallid, toolname, toolargs) {
79-
for (const fn in tc_switch) {
80-
if (fn == toolname) {
81-
try {
82-
tc_switch[fn]["handler"](chatid, toolcallid, fn, JSON.parse(toolargs))
83-
return undefined
84-
} catch (/** @type {any} */error) {
85-
return `Tool/Function call raised an exception:${error.name}:${error.message}`
73+
/**
74+
* Try call the specified tool/function call.
75+
* Returns undefined, if the call was placed successfully
76+
* Else some appropriate error message will be returned.
77+
* @param {string} chatid
78+
* @param {string} toolcallid
79+
* @param {string} toolname
80+
* @param {string} toolargs
81+
*/
82+
async tool_call(chatid, toolcallid, toolname, toolargs) {
83+
for (const fn in this.tc_switch) {
84+
if (fn == toolname) {
85+
try {
86+
this.tc_switch[fn]["handler"](chatid, toolcallid, fn, JSON.parse(toolargs))
87+
return undefined
88+
} catch (/** @type {any} */error) {
89+
return `Tool/Function call raised an exception:${error.name}:${error.message}`
90+
}
8691
}
8792
}
93+
return `Unknown Tool/Function Call:${toolname}`
8894
}
89-
return `Unknown Tool/Function Call:${toolname}`
95+
96+
97+
9098
}

0 commit comments

Comments
 (0)