Skip to content

Commit 861acbd

Browse files
committed
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
1 parent 2ce7ba6 commit 861acbd

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

tools/server/public_simplechat/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function startme() {
2222
document["du"] = du;
2323
// @ts-ignore
2424
document["tools"] = tools;
25-
tools.init().then((toolNames)=>gMe.tools.toolNames=toolNames).then(()=>gMe.multiChat.chat_show(gMe.multiChat.curChatId))
25+
tools.init(gMe).then((toolNames)=>gMe.tools.toolNames=toolNames).then(()=>gMe.multiChat.chat_show(gMe.multiChat.curChatId))
2626
for (let cid of gMe.defaultChatIds) {
2727
gMe.multiChat.new_chat_session(cid);
2828
}

tools/server/public_simplechat/readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ sliding window based drop off or even before they kick in, this can help in many
632632

633633
* sys_date_time tool call has been added.
634634

635+
* SimpleChat - Move the main chat related classes into its own js module file, independent of the
636+
main runtime entry point. This allows these classes to be referenced from other modules like tools
637+
related modules with full access to their details for developers and static check tools.
638+
635639

636640
#### ToDo
637641

tools/server/public_simplechat/tools.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import * as tjs from './tooljs.mjs'
99
import * as tweb from './toolweb.mjs'
1010
import * as tdb from './tooldb.mjs'
11+
import * as mChatMagic from './simplechat.js'
1112

1213

1314
let gToolsWorker = new Worker('./toolsworker.mjs', { type: 'module' });
@@ -19,7 +20,10 @@ let gToolsDBWorker = new Worker('./toolsdbworker.mjs', { type: 'module' });
1920
export let tc_switch = {}
2021

2122

22-
export async function init() {
23+
/**
24+
* @param {mChatMagic.Me} me
25+
*/
26+
export async function init(me) {
2327
/**
2428
* @type {string[]}
2529
*/
@@ -36,7 +40,7 @@ export async function init() {
3640
toolNames.push(key)
3741
}
3842
})
39-
let tNs = await tweb.init(gToolsWorker)
43+
let tNs = await tweb.init(gToolsWorker, me)
4044
for (const key in tNs) {
4145
tc_switch[key] = tNs[key]
4246
toolNames.push(key)

tools/server/public_simplechat/toolweb.mjs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
// by Humans for All
66
//
77

8+
import * as mChatMagic from './simplechat.js'
9+
810

911
let gToolsWorker = /** @type{Worker} */(/** @type {unknown} */(null));
12+
/**
13+
* @type {mChatMagic.Me}
14+
*/
15+
let gMe = /** @type{mChatMagic.Me} */(/** @type {unknown} */(null));
1016

1117

1218
/**
@@ -19,22 +25,13 @@ function message_toolsworker(mev) {
1925
}
2026

2127

22-
/**
23-
* Retrieve the global Me instance
24-
*/
25-
function get_gme() {
26-
return (/** @type {Object<string, Object<string, any>>} */(/** @type {unknown} */(document)))['gMe']
27-
}
28-
29-
3028
/**
3129
* For now hash the shared secret with the year.
3230
*/
33-
function bearer_transform() {
34-
let data = `${new Date().getUTCFullYear()}${get_gme().tools.proxyAuthInsecure}`
35-
return crypto.subtle.digest('sha-256', new TextEncoder().encode(data)).then(ab=>{
36-
return Array.from(new Uint8Array(ab)).map(b=>b.toString(16).padStart(2,'0')).join('')
37-
})
31+
async function bearer_transform() {
32+
let data = `${new Date().getUTCFullYear()}${gMe.tools.proxyAuthInsecure}`
33+
const ab = await crypto.subtle.digest('sha-256', new TextEncoder().encode(data));
34+
return Array.from(new Uint8Array(ab)).map(b => b.toString(16).padStart(2, '0')).join('');
3835
}
3936

4037
/**
@@ -56,7 +53,7 @@ function bearer_transform() {
5653
async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchParams, path, objHeaders={}) {
5754
if (gToolsWorker.onmessage != null) {
5855
let params = new URLSearchParams(objSearchParams)
59-
let newUrl = `${get_gme().tools.proxyUrl}/${path}?${params}`
56+
let newUrl = `${gMe.tools.proxyUrl}/${path}?${params}`
6057
let headers = new Headers(objHeaders)
6158
let btoken = await bearer_transform()
6259
headers.append('Authorization', `Bearer ${btoken}`)
@@ -84,7 +81,7 @@ async function proxyserver_get_anyargs(chatid, toolcallid, toolname, objSearchPa
8481
* @param {Object<string, Object<string, any>>} tcs
8582
*/
8683
async function proxyserver_tc_setup(tag, tcPath, tcName, tcsData, tcs) {
87-
await fetch(`${get_gme().tools.proxyUrl}/aum?url=${tcPath}.jambudweepe.akashaganga.multiverse.987654321123456789`).then(resp=>{
84+
await fetch(`${gMe.tools.proxyUrl}/aum?url=${tcPath}.jambudweepe.akashaganga.multiverse.987654321123456789`).then(resp=>{
8885
if (resp.statusText != 'bharatavarshe') {
8986
console.log(`WARN:ToolWeb:${tag}:Dont forget to run the bundled local.tools/simpleproxy.py to enable me`)
9087
return
@@ -253,15 +250,13 @@ let searchwebtext_meta = {
253250
* @param {any} obj
254251
*/
255252
function searchwebtext_run(chatid, toolcallid, toolname, obj) {
256-
if (gToolsWorker.onmessage != null) {
257-
/** @type {string} */
258-
let searchUrl = get_gme().tools.searchUrl;
259-
searchUrl = searchUrl.replace("SEARCHWORDS", encodeURIComponent(obj.words));
260-
delete(obj.words)
261-
obj['url'] = searchUrl
262-
let headers = { 'urltext-tag-drops': JSON.stringify(get_gme().tools.searchDrops) }
263-
return proxyserver_get_anyargs(chatid, toolcallid, toolname, obj, 'urltext', headers);
264-
}
253+
/** @type {string} */
254+
let searchUrl = gMe.tools.searchUrl;
255+
searchUrl = searchUrl.replace("SEARCHWORDS", encodeURIComponent(obj.words));
256+
delete(obj.words)
257+
obj['url'] = searchUrl
258+
let headers = { 'urltext-tag-drops': JSON.stringify(gMe.tools.searchDrops) }
259+
return proxyserver_get_anyargs(chatid, toolcallid, toolname, obj, 'urltext', headers);
265260
}
266261

267262

@@ -349,13 +344,15 @@ async function fetchpdftext_setup(tcs) {
349344
* Used to get hold of the web worker to use for running tool/function call related code
350345
* Also to setup tool calls, which need to cross check things at runtime
351346
* @param {Worker} toolsWorker
347+
* @param {mChatMagic.Me} me
352348
*/
353-
export async function init(toolsWorker) {
349+
export async function init(toolsWorker, me) {
354350
/**
355351
* @type {Object<string, Object<string, any>>} tcs
356352
*/
357353
let tc_switch = {}
358354
gToolsWorker = toolsWorker
355+
gMe = me
359356
await fetchweburlraw_setup(tc_switch)
360357
await fetchweburltext_setup(tc_switch)
361358
await searchwebtext_setup(tc_switch)

0 commit comments

Comments
 (0)