diff --git a/tools/server/public_simplechat/readme.md b/tools/server/public_simplechat/readme.md index a856b21b82..3a1b5e104c 100644 --- a/tools/server/public_simplechat/readme.md +++ b/tools/server/public_simplechat/readme.md @@ -350,8 +350,11 @@ server logic, this helps bypass the CORS restrictions applied if trying to direc browser js runtime environment. Depending on the path specified wrt the proxy server, if urltext (and not urlraw), it additionally tries to convert html content into equivalent text to some extent in a simple minded manner by dropping head block as well as all scripts/styles/footers/headers/nav. -May add support for white list of allowed sites to access or so. The simple proxy can be found at -* tools/server/public_simplechat/local.tools/simpleproxy.py +May add support for white list of allowed sites to access or so. +* the logic does a simple dumb check to see if there is something running at specified proxyUrl + before enabling fetch web related tool calls. +* The bundled simple proxy can be found at + * tools/server/public_simplechat/local.tools/simpleproxy.py #### Extending with new tools diff --git a/tools/server/public_simplechat/tooljs.mjs b/tools/server/public_simplechat/tooljs.mjs index ffaab5de2e..f013cac70a 100644 --- a/tools/server/public_simplechat/tooljs.mjs +++ b/tools/server/public_simplechat/tooljs.mjs @@ -131,6 +131,23 @@ function fetchweburlraw_run(toolcallid, toolname, obj) { } +/** + * Setup fetch_web_url_raw for tool calling + * NOTE: Currently it just checks there is something at given proxyUrl + * @param {Object>} tcs + */ +async function fetchweburlraw_setup(tcs) { + // @ts-ignore + let got = await fetch(document["gMe"].proxyUrl).then(resp=>{ + tcs["fetch_web_url_raw"] = { + "handler": fetchweburlraw_run, + "meta": fetchweburlraw_meta, + "result": "" + }; + }).catch(err=>console.log(`WARN:ToolJS:FetchWebUrlRaw:ProxyServer missing?:${err}\nDont forget to run the bundled local.tools/simpleproxy.py`)) +} + + let fetchweburltext_meta = { "type": "function", "function": { @@ -182,6 +199,23 @@ function fetchweburltext_run(toolcallid, toolname, obj) { } +/** + * Setup fetch_web_url_text for tool calling + * NOTE: Currently it just checks there is something at given proxyUrl + * @param {Object>} tcs + */ +async function fetchweburltext_setup(tcs) { + // @ts-ignore + let got = await fetch(document["gMe"].proxyUrl).then(resp=>{ + tcs["fetch_web_url_text"] = { + "handler": fetchweburltext_run, + "meta": fetchweburltext_meta, + "result": "" + }; + }).catch(err=>console.log(`WARN:ToolJS:FetchWebUrlText:ProxyServer missing?:${err}\nDont forget to run the bundled local.tools/simpleproxy.py`)) +} + + /** * @type {Object>} */ @@ -196,23 +230,16 @@ export let tc_switch = { "meta": calc_meta, "result": "" }, - "fetch_web_url_raw": { - "handler": fetchweburlraw_run, - "meta": fetchweburlraw_meta, - "result": "" - }, - "fetch_web_url_text": { - "handler": fetchweburltext_run, - "meta": fetchweburltext_meta, - "result": "" - } } /** * 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 * @param {Worker} toolsWorker */ -export function init(toolsWorker) { +export async function init(toolsWorker) { gToolsWorker = toolsWorker + await fetchweburlraw_setup(tc_switch) + await fetchweburltext_setup(tc_switch) } diff --git a/tools/server/public_simplechat/tools.mjs b/tools/server/public_simplechat/tools.mjs index 8c89e96525..14249b517a 100644 --- a/tools/server/public_simplechat/tools.mjs +++ b/tools/server/public_simplechat/tools.mjs @@ -15,10 +15,11 @@ let gToolsWorker = new Worker('./toolsworker.mjs', { type: 'module' }); export let tc_switch = {} export function init() { - tjs.init(gToolsWorker) - for (const key in tjs.tc_switch) { - tc_switch[key] = tjs.tc_switch[key] - } + tjs.init(gToolsWorker).then(()=>{ + for (const key in tjs.tc_switch) { + tc_switch[key] = tjs.tc_switch[key] + } + }) } export function meta() {