Skip to content

Commit 7888a88

Browse files
committed
SimpleChatTC:WebFetch: Enable only if something at proxyUrl
NOTE: not a robust check, just tries to establish a http connection for now and doesnt really check if it is the specific proxy srvr of interest or not.
1 parent 8be6d8e commit 7888a88

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

tools/server/public_simplechat/readme.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,11 @@ server logic, this helps bypass the CORS restrictions applied if trying to direc
350350
browser js runtime environment. Depending on the path specified wrt the proxy server, if urltext
351351
(and not urlraw), it additionally tries to convert html content into equivalent text to some extent
352352
in a simple minded manner by dropping head block as well as all scripts/styles/footers/headers/nav.
353-
May add support for white list of allowed sites to access or so. The simple proxy can be found at
354-
* tools/server/public_simplechat/local.tools/simpleproxy.py
353+
May add support for white list of allowed sites to access or so.
354+
* the logic does a simple dumb check to see if there is something running at specified proxyUrl
355+
before enabling fetch web related tool calls.
356+
* The bundled simple proxy can be found at
357+
* tools/server/public_simplechat/local.tools/simpleproxy.py
355358

356359

357360
#### Extending with new tools

tools/server/public_simplechat/tooljs.mjs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ function fetchweburlraw_run(toolcallid, toolname, obj) {
131131
}
132132

133133

134+
/**
135+
* Setup fetch_web_url_raw for tool calling
136+
* NOTE: Currently it just checks there is something at given proxyUrl
137+
* @param {Object<string, Object<string, any>>} tcs
138+
*/
139+
async function fetchweburlraw_setup(tcs) {
140+
// @ts-ignore
141+
let got = await fetch(document["gMe"].proxyUrl).then(resp=>{
142+
tcs["fetch_web_url_raw"] = {
143+
"handler": fetchweburlraw_run,
144+
"meta": fetchweburlraw_meta,
145+
"result": ""
146+
};
147+
}).catch(err=>console.log(`WARN:ToolJS:FetchWebUrlRaw:ProxyServer missing?:${err}\nDont forget to run the bundled local.tools/simpleproxy.py`))
148+
}
149+
150+
134151
let fetchweburltext_meta = {
135152
"type": "function",
136153
"function": {
@@ -182,6 +199,23 @@ function fetchweburltext_run(toolcallid, toolname, obj) {
182199
}
183200

184201

202+
/**
203+
* Setup fetch_web_url_text for tool calling
204+
* NOTE: Currently it just checks there is something at given proxyUrl
205+
* @param {Object<string, Object<string, any>>} tcs
206+
*/
207+
async function fetchweburltext_setup(tcs) {
208+
// @ts-ignore
209+
let got = await fetch(document["gMe"].proxyUrl).then(resp=>{
210+
tcs["fetch_web_url_text"] = {
211+
"handler": fetchweburltext_run,
212+
"meta": fetchweburltext_meta,
213+
"result": ""
214+
};
215+
}).catch(err=>console.log(`WARN:ToolJS:FetchWebUrlText:ProxyServer missing?:${err}\nDont forget to run the bundled local.tools/simpleproxy.py`))
216+
}
217+
218+
185219
/**
186220
* @type {Object<string, Object<string, any>>}
187221
*/
@@ -196,23 +230,16 @@ export let tc_switch = {
196230
"meta": calc_meta,
197231
"result": ""
198232
},
199-
"fetch_web_url_raw": {
200-
"handler": fetchweburlraw_run,
201-
"meta": fetchweburlraw_meta,
202-
"result": ""
203-
},
204-
"fetch_web_url_text": {
205-
"handler": fetchweburltext_run,
206-
"meta": fetchweburltext_meta,
207-
"result": ""
208-
}
209233
}
210234

211235

212236
/**
213237
* Used to get hold of the web worker to use for running tool/function call related code
238+
* Also to setup tool calls, which need to cross check things at runtime
214239
* @param {Worker} toolsWorker
215240
*/
216-
export function init(toolsWorker) {
241+
export async function init(toolsWorker) {
217242
gToolsWorker = toolsWorker
243+
await fetchweburlraw_setup(tc_switch)
244+
await fetchweburltext_setup(tc_switch)
218245
}

tools/server/public_simplechat/tools.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ let gToolsWorker = new Worker('./toolsworker.mjs', { type: 'module' });
1515
export let tc_switch = {}
1616

1717
export function init() {
18-
tjs.init(gToolsWorker)
19-
for (const key in tjs.tc_switch) {
20-
tc_switch[key] = tjs.tc_switch[key]
21-
}
18+
tjs.init(gToolsWorker).then(()=>{
19+
for (const key in tjs.tc_switch) {
20+
tc_switch[key] = tjs.tc_switch[key]
21+
}
22+
})
2223
}
2324

2425
export function meta() {

0 commit comments

Comments
 (0)