SimpleChatTC:Cleanup:MeInTools: update tools, toolweb

Now gMe can be used in toolweb with proper knowledge of available
members and can also be cross checked by tools
This commit is contained in:
hanishkvc 2025-11-05 17:32:34 +05:30
parent d56a4a06b0
commit 85c2779579
4 changed files with 33 additions and 28 deletions

View File

@ -22,7 +22,7 @@ function startme() {
document["du"] = du; document["du"] = du;
// @ts-ignore // @ts-ignore
document["tools"] = tools; document["tools"] = tools;
tools.init().then((toolNames)=>gMe.tools.toolNames=toolNames).then(()=>gMe.multiChat.chat_show(gMe.multiChat.curChatId)) tools.init(gMe).then((toolNames)=>gMe.tools.toolNames=toolNames).then(()=>gMe.multiChat.chat_show(gMe.multiChat.curChatId))
for (let cid of gMe.defaultChatIds) { for (let cid of gMe.defaultChatIds) {
gMe.multiChat.new_chat_session(cid); gMe.multiChat.new_chat_session(cid);
} }

View File

@ -632,6 +632,10 @@ sliding window based drop off or even before they kick in, this can help in many
* sys_date_time tool call has been added. * sys_date_time tool call has been added.
* SimpleChat - Move the main chat related classes into its own js module file, independent of the
main runtime entry point. This allows these classes to be referenced from other modules like tools
related modules with full access to their details for developers and static check tools.
#### ToDo #### ToDo

View File

@ -8,6 +8,7 @@
import * as tjs from './tooljs.mjs' import * as tjs from './tooljs.mjs'
import * as tweb from './toolweb.mjs' import * as tweb from './toolweb.mjs'
import * as tdb from './tooldb.mjs' import * as tdb from './tooldb.mjs'
import * as mChatMagic from './simplechat.js'
let gToolsWorker = new Worker('./toolsworker.mjs', { type: 'module' }); let gToolsWorker = new Worker('./toolsworker.mjs', { type: 'module' });
@ -19,7 +20,10 @@ let gToolsDBWorker = new Worker('./toolsdbworker.mjs', { type: 'module' });
export let tc_switch = {} export let tc_switch = {}
export async function init() { /**
* @param {mChatMagic.Me} me
*/
export async function init(me) {
/** /**
* @type {string[]} * @type {string[]}
*/ */
@ -36,7 +40,7 @@ export async function init() {
toolNames.push(key) toolNames.push(key)
} }
}) })
let tNs = await tweb.init(gToolsWorker) let tNs = await tweb.init(gToolsWorker, me)
for (const key in tNs) { for (const key in tNs) {
tc_switch[key] = tNs[key] tc_switch[key] = tNs[key]
toolNames.push(key) toolNames.push(key)

View File

@ -5,8 +5,14 @@
// by Humans for All // by Humans for All
// //
import * as mChatMagic from './simplechat.js'
let gToolsWorker = /** @type{Worker} */(/** @type {unknown} */(null)); let gToolsWorker = /** @type{Worker} */(/** @type {unknown} */(null));
/**
* @type {mChatMagic.Me}
*/
let gMe = /** @type{mChatMagic.Me} */(/** @type {unknown} */(null));
/** /**
@ -19,22 +25,13 @@ function message_toolsworker(mev) {
} }
/**
* Retrieve the global Me instance
*/
function get_gme() {
return (/** @type {Object<string, Object<string, any>>} */(/** @type {unknown} */(document)))['gMe']
}
/** /**
* For now hash the shared secret with the year. * For now hash the shared secret with the year.
*/ */
function bearer_transform() { async function bearer_transform() {
let data = `${new Date().getUTCFullYear()}${get_gme().tools.proxyAuthInsecure}` let data = `${new Date().getUTCFullYear()}${gMe.tools.proxyAuthInsecure}`
return crypto.subtle.digest('sha-256', new TextEncoder().encode(data)).then(ab=>{ const ab = await crypto.subtle.digest('sha-256', new TextEncoder().encode(data));
return Array.from(new Uint8Array(ab)).map(b=>b.toString(16).padStart(2,'0')).join('') return Array.from(new Uint8Array(ab)).map(b => b.toString(16).padStart(2, '0')).join('');
})
} }
/** /**
@ -56,7 +53,7 @@ function bearer_transform() {
async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchParams, path, objHeaders={}) { async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchParams, path, objHeaders={}) {
if (gToolsWorker.onmessage != null) { if (gToolsWorker.onmessage != null) {
let params = new URLSearchParams(objSearchParams) let params = new URLSearchParams(objSearchParams)
let newUrl = `${get_gme().tools.proxyUrl}/${path}?${params}` let newUrl = `${gMe.tools.proxyUrl}/${path}?${params}`
let headers = new Headers(objHeaders) let headers = new Headers(objHeaders)
let btoken = await bearer_transform() let btoken = await bearer_transform()
headers.append('Authorization', `Bearer ${btoken}`) headers.append('Authorization', `Bearer ${btoken}`)
@ -84,7 +81,7 @@ async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchPa
* @param {Object<string, Object<string, any>>} tcs * @param {Object<string, Object<string, any>>} tcs
*/ */
async function proxyserver_tc_setup(tag, tcPath, tcName, tcsData, tcs) { async function proxyserver_tc_setup(tag, tcPath, tcName, tcsData, tcs) {
await fetch(`${get_gme().tools.proxyUrl}/aum?url=${tcPath}.jambudweepe.akashaganga.multiverse.987654321123456789`).then(resp=>{ await fetch(`${gMe.tools.proxyUrl}/aum?url=${tcPath}.jambudweepe.akashaganga.multiverse.987654321123456789`).then(resp=>{
if (resp.statusText != 'bharatavarshe') { if (resp.statusText != 'bharatavarshe') {
console.log(`WARN:ToolWeb:${tag}:Dont forget to run the bundled local.tools/simpleproxy.py to enable me`) console.log(`WARN:ToolWeb:${tag}:Dont forget to run the bundled local.tools/simpleproxy.py to enable me`)
return return
@ -253,15 +250,13 @@ let searchwebtext_meta = {
* @param {any} obj * @param {any} obj
*/ */
function searchwebtext_run(chatid, toolcallid, toolname, obj) { function searchwebtext_run(chatid, toolcallid, toolname, obj) {
if (gToolsWorker.onmessage != null) { /** @type {string} */
/** @type {string} */ let searchUrl = gMe.tools.searchUrl;
let searchUrl = get_gme().tools.searchUrl; searchUrl = searchUrl.replace("SEARCHWORDS", encodeURIComponent(obj.words));
searchUrl = searchUrl.replace("SEARCHWORDS", encodeURIComponent(obj.words)); delete(obj.words)
delete(obj.words) obj['url'] = searchUrl
obj['url'] = searchUrl let headers = { 'urltext-tag-drops': JSON.stringify(gMe.tools.searchDrops) }
let headers = { 'urltext-tag-drops': JSON.stringify(get_gme().tools.searchDrops) } return proxyserver_get_anyargs(chatid, toolcallid, toolname, obj, 'urltext', headers);
return proxyserver_get_anyargs(chatid, toolcallid, toolname, obj, 'urltext', headers);
}
} }
@ -349,13 +344,15 @@ async function fetchpdftext_setup(tcs) {
* Used to get hold of the web worker to use for running tool/function call related code * Used to get hold of the web worker to use for running tool/function call related code
* Also to setup tool calls, which need to cross check things at runtime * Also to setup tool calls, which need to cross check things at runtime
* @param {Worker} toolsWorker * @param {Worker} toolsWorker
* @param {mChatMagic.Me} me
*/ */
export async function init(toolsWorker) { export async function init(toolsWorker, me) {
/** /**
* @type {Object<string, Object<string, any>>} tcs * @type {Object<string, Object<string, any>>} tcs
*/ */
let tc_switch = {} let tc_switch = {}
gToolsWorker = toolsWorker gToolsWorker = toolsWorker
gMe = me
await fetchweburlraw_setup(tc_switch) await fetchweburlraw_setup(tc_switch)
await fetchweburltext_setup(tc_switch) await fetchweburltext_setup(tc_switch)
await searchwebtext_setup(tc_switch) await searchwebtext_setup(tc_switch)