SimpleChatTC:WebUrl FetchStrip through simple proxy

This commit is contained in:
hanishkvc 2025-10-17 02:08:38 +05:30
parent b46bbc542a
commit 82ab08ec1a
1 changed files with 63 additions and 4 deletions

View File

@ -72,6 +72,16 @@ function calc_run(toolcallid, toolname, obj) {
}
/**
* Send a message to Tools WebWorker's monitor in main thread directly
* @param {MessageEvent<any>} mev
*/
function message_toolsworker(mev) {
// @ts-ignore
gToolsWorker.onmessage(mev)
}
let weburlfetch_meta = {
"type": "function",
"function": {
@ -96,8 +106,8 @@ let weburlfetch_meta = {
* Expects a simple minded proxy server to be running locally
* * listening on port 3128
* * expecting http requests
* * with a query token named path which gives the actual url to fetch
* ALERT: Has access to the javascript web worker environment and can mess with it and beyond
* * with a query token named url which gives the actual url to fetch
* ALERT: Accesses a external web proxy/caching server be aware and careful
* @param {string} toolcallid
* @param {string} toolname
* @param {any} obj
@ -106,9 +116,53 @@ function weburlfetch_run(toolcallid, toolname, obj) {
if (gToolsWorker.onmessage != null) {
let newUrl = `http://127.0.0.1:3128/urlraw?url=${obj.url}`
fetch(newUrl).then(resp=>resp.text()).then(data => {
gToolsWorker.onmessage(new MessageEvent('message', {data: {id: toolcallid, name: toolname, data: data}}))
message_toolsworker(new MessageEvent('message', {data: {id: toolcallid, name: toolname, data: data}}))
}).catch((err)=>{
gToolsWorker.onmessage(new MessageEvent('message', {data: {id: toolcallid, name: toolname, data: `Error:${err}`}}))
message_toolsworker(new MessageEvent('message', {data: {id: toolcallid, name: toolname, data: `Error:${err}`}}))
})
}
}
let weburlfetchstrip_meta = {
"type": "function",
"function": {
"name": "web_url_fetch_strip",
"description": "Fetch the requested web url through a proxy server and strip away head, script, styles blocks before sending remaining body in few seconds",
"parameters": {
"type": "object",
"properties": {
"url":{
"type":"string",
"description":"the url of the page that will be fetched from the internet and inturn contents stripped to some extent"
}
},
"required": ["url"]
}
}
}
/**
* Implementation of the web url logic. Dumb initial go.
* Expects a simple minded proxy server to be running locally
* * listening on port 3128
* * expecting http requests
* * with a query token named url which gives the actual url to fetch
* * strips out head as well as any script and style blocks in body
* before returning remaining body contents.
* ALERT: Accesses a external web proxy/caching server be aware and careful
* @param {string} toolcallid
* @param {string} toolname
* @param {any} obj
*/
function weburlfetchstrip_run(toolcallid, toolname, obj) {
if (gToolsWorker.onmessage != null) {
let newUrl = `http://127.0.0.1:3128/urltext?url=${obj.url}`
fetch(newUrl).then(resp=>resp.text()).then(data => {
message_toolsworker(new MessageEvent('message', {data: {id: toolcallid, name: toolname, data: data}}))
}).catch((err)=>{
message_toolsworker(new MessageEvent('message', {data: {id: toolcallid, name: toolname, data: `Error:${err}`}}))
})
}
}
@ -132,6 +186,11 @@ export let tc_switch = {
"handler": weburlfetch_run,
"meta": weburlfetch_meta,
"result": ""
},
"web_url_fetch_strip": {
"handler": weburlfetchstrip_run,
"meta": weburlfetchstrip_meta,
"result": ""
}
}