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.
This commit is contained in:
parent
0e7fe8bcf2
commit
2534af8215
|
|
@ -11,80 +11,88 @@ import * as tdb from './tooldb.mjs'
|
|||
import * as mChatMagic from './simplechat.js'
|
||||
|
||||
|
||||
/**
|
||||
* Maintain currently available tool/function calls
|
||||
* @type {Object<string,Object<string,any>>}
|
||||
*/
|
||||
export let tc_switch = {}
|
||||
|
||||
class Tools {
|
||||
|
||||
/**
|
||||
* @param {mChatMagic.Me} me
|
||||
*/
|
||||
function setup_workers(me) {
|
||||
me.workers.js = new Worker('./toolsworker.mjs', { type: 'module' });
|
||||
me.workers.db = new Worker('./toolsdbworker.mjs', { type: 'module' });
|
||||
}
|
||||
constructor() {
|
||||
/**
|
||||
* Maintain currently available tool/function calls
|
||||
* @type {Object<string,Object<string,any>>}
|
||||
*/
|
||||
this.tc_switch = {}
|
||||
|
||||
this.workers = {
|
||||
js: /** @type {Worker} */(/** @type {unknown} */(undefined)),
|
||||
db: /** @type {Worker} */(/** @type {unknown} */(undefined)),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
setup_workers() {
|
||||
this.workers.js = new Worker('./toolsworker.mjs', { type: 'module' });
|
||||
this.workers.db = new Worker('./toolsdbworker.mjs', { type: 'module' });
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {mChatMagic.Me} me
|
||||
*/
|
||||
export async function init(me) {
|
||||
setup_workers(me);
|
||||
/**
|
||||
* @type {string[]}
|
||||
* @param {mChatMagic.Me} me
|
||||
*/
|
||||
let toolNames = []
|
||||
await tjs.init(me).then(()=>{
|
||||
for (const key in tjs.tc_switch) {
|
||||
tc_switch[key] = tjs.tc_switch[key]
|
||||
async init(me) {
|
||||
this.setup_workers();
|
||||
/**
|
||||
* @type {string[]}
|
||||
*/
|
||||
let toolNames = []
|
||||
await tjs.init(me).then(()=>{
|
||||
for (const key in tjs.tc_switch) {
|
||||
this.tc_switch[key] = tjs.tc_switch[key]
|
||||
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)
|
||||
}
|
||||
})
|
||||
let tNs = await tweb.init(me)
|
||||
for (const key in tNs) {
|
||||
this.tc_switch[key] = tNs[key]
|
||||
toolNames.push(key)
|
||||
}
|
||||
})
|
||||
await tdb.init(me).then(()=>{
|
||||
for (const key in tdb.tc_switch) {
|
||||
tc_switch[key] = tdb.tc_switch[key]
|
||||
toolNames.push(key)
|
||||
return toolNames
|
||||
}
|
||||
|
||||
meta() {
|
||||
let tools = []
|
||||
for (const key in this.tc_switch) {
|
||||
tools.push(this.tc_switch[key]["meta"])
|
||||
}
|
||||
})
|
||||
let tNs = await tweb.init(me)
|
||||
for (const key in tNs) {
|
||||
tc_switch[key] = tNs[key]
|
||||
toolNames.push(key)
|
||||
return tools
|
||||
}
|
||||
return toolNames
|
||||
}
|
||||
|
||||
|
||||
export function meta() {
|
||||
let tools = []
|
||||
for (const key in tc_switch) {
|
||||
tools.push(tc_switch[key]["meta"])
|
||||
}
|
||||
return tools
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Try call the specified tool/function call.
|
||||
* Returns undefined, if the call was placed successfully
|
||||
* Else some appropriate error message will be returned.
|
||||
* @param {string} chatid
|
||||
* @param {string} toolcallid
|
||||
* @param {string} toolname
|
||||
* @param {string} toolargs
|
||||
*/
|
||||
export async function tool_call(chatid, toolcallid, toolname, toolargs) {
|
||||
for (const fn in tc_switch) {
|
||||
if (fn == toolname) {
|
||||
try {
|
||||
tc_switch[fn]["handler"](chatid, toolcallid, fn, JSON.parse(toolargs))
|
||||
return undefined
|
||||
} catch (/** @type {any} */error) {
|
||||
return `Tool/Function call raised an exception:${error.name}:${error.message}`
|
||||
/**
|
||||
* Try call the specified tool/function call.
|
||||
* Returns undefined, if the call was placed successfully
|
||||
* Else some appropriate error message will be returned.
|
||||
* @param {string} chatid
|
||||
* @param {string} toolcallid
|
||||
* @param {string} toolname
|
||||
* @param {string} toolargs
|
||||
*/
|
||||
async tool_call(chatid, toolcallid, toolname, toolargs) {
|
||||
for (const fn in this.tc_switch) {
|
||||
if (fn == toolname) {
|
||||
try {
|
||||
this.tc_switch[fn]["handler"](chatid, toolcallid, fn, JSON.parse(toolargs))
|
||||
return undefined
|
||||
} catch (/** @type {any} */error) {
|
||||
return `Tool/Function call raised an exception:${error.name}:${error.message}`
|
||||
}
|
||||
}
|
||||
}
|
||||
return `Unknown Tool/Function Call:${toolname}`
|
||||
}
|
||||
return `Unknown Tool/Function Call:${toolname}`
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue