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.
This commit is contained in:
parent
2534af8215
commit
4d71ded5df
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
import * as du from "./datautils.mjs";
|
||||
import * as ui from "./ui.mjs"
|
||||
import * as tools from "./tools.mjs"
|
||||
import * as mTools from "./tools.mjs"
|
||||
|
||||
|
||||
class Roles {
|
||||
|
|
@ -522,7 +522,7 @@ class SimpleChat {
|
|||
obj["stream"] = true;
|
||||
}
|
||||
if (this.me.tools.enabled) {
|
||||
obj["tools"] = tools.meta();
|
||||
obj["tools"] = this.me.toolsMgr.meta();
|
||||
}
|
||||
return JSON.stringify(obj);
|
||||
}
|
||||
|
|
@ -751,7 +751,7 @@ class SimpleChat {
|
|||
return "Tool/Function call name not specified"
|
||||
}
|
||||
try {
|
||||
return await tools.tool_call(this.chatId, toolcallid, toolname, toolargs)
|
||||
return await this.me.toolsMgr.tool_call(this.chatId, toolcallid, toolname, toolargs)
|
||||
} catch (/** @type {any} */error) {
|
||||
return `Tool/Function call raised an exception:${error.name}:${error.message}`
|
||||
}
|
||||
|
|
@ -1070,7 +1070,7 @@ class MultiChatUI {
|
|||
})
|
||||
|
||||
// Handle messages from tools web workers
|
||||
this.me.workers_cb((cid, tcid, name, data)=>{
|
||||
this.me.toolsMgr.workers_cb((cid, tcid, name, data)=>{
|
||||
clearTimeout(this.timers.toolcallResponseTimeout)
|
||||
this.timers.toolcallResponseTimeout = undefined
|
||||
let chat = this.simpleChats[cid];
|
||||
|
|
@ -1386,10 +1386,7 @@ export class Me {
|
|||
//"frequency_penalty": 1.2,
|
||||
//"presence_penalty": 1.2,
|
||||
};
|
||||
this.workers = {
|
||||
js: /** @type {Worker} */(/** @type {unknown} */(undefined)),
|
||||
db: /** @type {Worker} */(/** @type {unknown} */(undefined)),
|
||||
}
|
||||
this.toolsMgr = new mTools.ToolsManager()
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -1473,35 +1470,4 @@ export class Me {
|
|||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the callback that will be called when ever message
|
||||
* is recieved from the Tools Web Workers.
|
||||
* @param {(chatId: string, toolCallId: string, name: string, data: string) => void} cb
|
||||
*/
|
||||
workers_cb(cb) {
|
||||
this.workers.js.onmessage = function (ev) {
|
||||
cb(ev.data.cid, ev.data.tcid, ev.data.name, ev.data.data)
|
||||
}
|
||||
this.workers.db.onmessage = function (ev) {
|
||||
cb(ev.data.cid, ev.data.tcid, ev.data.name, JSON.stringify(ev.data.data, (k,v)=>{
|
||||
return (v === undefined) ? '__UNDEFINED__' : v;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to specified tools web worker's monitor in main thread directly
|
||||
* @param {Worker} worker
|
||||
* @param {string} chatid
|
||||
* @param {string} toolcallid
|
||||
* @param {string} toolname
|
||||
* @param {string} data
|
||||
*/
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ let dslist_meta = {
|
|||
* @param {any} obj
|
||||
*/
|
||||
function dsops_run(chatid, toolcallid, toolname, obj) {
|
||||
gMe.workers.db.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, args: obj})
|
||||
gMe.toolsMgr.workers.db.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, args: obj})
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ function sysdatetime_run(chatid, toolcallid, toolname, obj) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
gMe.workers_postmessage_for_main(gMe.workers.js, chatid, toolcallid, toolname, sDT);
|
||||
gMe.toolsMgr.workers_postmessage_for_main(gMe.toolsMgr.workers.js, chatid, toolcallid, toolname, sDT);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -109,7 +109,7 @@ let js_meta = {
|
|||
* @param {any} obj
|
||||
*/
|
||||
function js_run(chatid, toolcallid, toolname, obj) {
|
||||
gMe.workers.js.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: obj["code"]})
|
||||
gMe.toolsMgr.workers.js.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: obj["code"]})
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -141,7 +141,7 @@ let calc_meta = {
|
|||
* @param {any} obj
|
||||
*/
|
||||
function calc_run(chatid, toolcallid, toolname, obj) {
|
||||
gMe.workers.js.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: `console.log(${obj["arithexpr"]})`})
|
||||
gMe.toolsMgr.workers.js.postMessage({ cid: chatid, tcid: toolcallid, name: toolname, code: `console.log(${obj["arithexpr"]})`})
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import * as mChatMagic from './simplechat.js'
|
|||
|
||||
|
||||
|
||||
class Tools {
|
||||
export class ToolsManager {
|
||||
|
||||
constructor() {
|
||||
/**
|
||||
|
|
@ -34,6 +34,8 @@ class Tools {
|
|||
}
|
||||
|
||||
/**
|
||||
* Initialise the ToolsManager,
|
||||
* including all the different tools groups.
|
||||
* @param {mChatMagic.Me} me
|
||||
*/
|
||||
async init(me) {
|
||||
|
|
@ -62,6 +64,9 @@ class Tools {
|
|||
return toolNames
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the tools meta data that can be passed to the ai server.
|
||||
*/
|
||||
meta() {
|
||||
let tools = []
|
||||
for (const key in this.tc_switch) {
|
||||
|
|
@ -93,6 +98,35 @@ class Tools {
|
|||
return `Unknown Tool/Function Call:${toolname}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup the callback that will be called when ever message
|
||||
* is recieved from the Tools Web Workers.
|
||||
* @param {(chatId: string, toolCallId: string, name: string, data: string) => void} cb
|
||||
*/
|
||||
workers_cb(cb) {
|
||||
this.workers.js.onmessage = function (ev) {
|
||||
cb(ev.data.cid, ev.data.tcid, ev.data.name, ev.data.data)
|
||||
}
|
||||
this.workers.db.onmessage = function (ev) {
|
||||
cb(ev.data.cid, ev.data.tcid, ev.data.name, JSON.stringify(ev.data.data, (k,v)=>{
|
||||
return (v === undefined) ? '__UNDEFINED__' : v;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to specified tools web worker's monitor in main thread directly
|
||||
* @param {Worker} worker
|
||||
* @param {string} chatid
|
||||
* @param {string} toolcallid
|
||||
* @param {string} toolname
|
||||
* @param {string} data
|
||||
*/
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ async function bearer_transform() {
|
|||
* @param {any} objHeaders
|
||||
*/
|
||||
async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchParams, path, objHeaders={}) {
|
||||
if (gMe.workers.js.onmessage != null) {
|
||||
if (gMe.toolsMgr.workers.js.onmessage != null) {
|
||||
let params = new URLSearchParams(objSearchParams)
|
||||
let newUrl = `${gMe.tools.proxyUrl}/${path}?${params}`
|
||||
let headers = new Headers(objHeaders)
|
||||
|
|
@ -53,9 +53,9 @@ async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchPa
|
|||
}
|
||||
return resp.text()
|
||||
}).then(data => {
|
||||
gMe.workers_postmessage_for_main(gMe.workers.js, chatid, toolcallid, toolname, data);
|
||||
gMe.toolsMgr.workers_postmessage_for_main(gMe.toolsMgr.workers.js, chatid, toolcallid, toolname, data);
|
||||
}).catch((err)=>{
|
||||
gMe.workers_postmessage_for_main(gMe.workers.js, chatid, toolcallid, toolname, `Error:${err}`);
|
||||
gMe.toolsMgr.workers_postmessage_for_main(gMe.toolsMgr.workers.js, chatid, toolcallid, toolname, `Error:${err}`);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue