SimpleSallap:SimpleMCP:TCWeb:Update TCUrlRaw + Helper
Now generic handle_urlreq and handle_urlraw updated to work with the new ToolCall flow
This commit is contained in:
parent
cbb632eec0
commit
b17cd18bc5
|
|
@ -3,32 +3,18 @@
|
||||||
|
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import urlvalidator as uv
|
import urlvalidator as uv
|
||||||
from dataclasses import dataclass
|
|
||||||
import html.parser
|
import html.parser
|
||||||
import debug
|
import debug
|
||||||
import filemagic as mFile
|
import filemagic as mFile
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
from typing import TYPE_CHECKING, Any, cast
|
import http.client
|
||||||
|
from typing import Any, cast
|
||||||
if TYPE_CHECKING:
|
import toolcall as mTC
|
||||||
from simpleproxy import ProxyHandler
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
def handle_urlreq(url: str, inHeaders: http.client.HTTPMessage, tag: str):
|
||||||
class UrlReqResp:
|
|
||||||
"""
|
|
||||||
Used to return result wrt urlreq helper below.
|
|
||||||
"""
|
|
||||||
callOk: bool
|
|
||||||
httpStatus: int
|
|
||||||
httpStatusMsg: str = ""
|
|
||||||
contentType: str = ""
|
|
||||||
contentData: str = ""
|
|
||||||
|
|
||||||
|
|
||||||
def handle_urlreq(ph: 'ProxyHandler', pr: urllib.parse.ParseResult, tag: str):
|
|
||||||
"""
|
"""
|
||||||
Common part of the url request handling used by both urlraw and urltext.
|
Common part of the url request handling used by both urlraw and urltext.
|
||||||
|
|
||||||
|
|
@ -42,17 +28,14 @@ def handle_urlreq(ph: 'ProxyHandler', pr: urllib.parse.ParseResult, tag: str):
|
||||||
Fetch the requested url.
|
Fetch the requested url.
|
||||||
"""
|
"""
|
||||||
tag=f"UrlReq:{tag}"
|
tag=f"UrlReq:{tag}"
|
||||||
queryParams = urllib.parse.parse_qs(pr.query)
|
|
||||||
url = queryParams['url']
|
|
||||||
print(f"DBUG:{tag}:Url:{url}")
|
print(f"DBUG:{tag}:Url:{url}")
|
||||||
url = url[0]
|
|
||||||
gotVU = uv.validate_url(url, tag)
|
gotVU = uv.validate_url(url, tag)
|
||||||
if not gotVU.callOk:
|
if not gotVU.callOk:
|
||||||
return UrlReqResp(gotVU.callOk, gotVU.statusCode, gotVU.statusMsg)
|
return mTC.TCOutResponse(gotVU.callOk, gotVU.statusCode, gotVU.statusMsg)
|
||||||
try:
|
try:
|
||||||
hUA = ph.headers.get('User-Agent', None)
|
hUA = inHeaders.get('User-Agent', None)
|
||||||
hAL = ph.headers.get('Accept-Language', None)
|
hAL = inHeaders.get('Accept-Language', None)
|
||||||
hA = ph.headers.get('Accept', None)
|
hA = inHeaders.get('Accept', None)
|
||||||
headers = {
|
headers = {
|
||||||
'User-Agent': hUA,
|
'User-Agent': hUA,
|
||||||
'Accept': hA,
|
'Accept': hA,
|
||||||
|
|
@ -60,27 +43,36 @@ def handle_urlreq(ph: 'ProxyHandler', pr: urllib.parse.ParseResult, tag: str):
|
||||||
}
|
}
|
||||||
# Get requested url
|
# Get requested url
|
||||||
gotFile = mFile.get_file(url, tag, "text/html", headers)
|
gotFile = mFile.get_file(url, tag, "text/html", headers)
|
||||||
return UrlReqResp(gotFile.callOk, gotFile.statusCode, gotFile.statusMsg, gotFile.contentType, gotFile.contentData.decode('utf-8'))
|
return mTC.TCOutResponse(gotFile.callOk, gotFile.statusCode, gotFile.statusMsg, gotFile.contentType, gotFile.contentData)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
return UrlReqResp(False, 502, f"WARN:{tag}:Failed:{exc}")
|
return mTC.TCOutResponse(False, 502, f"WARN:{tag}:Failed:{exc}")
|
||||||
|
|
||||||
|
|
||||||
def handle_urlraw(ph: 'ProxyHandler', pr: urllib.parse.ParseResult):
|
class TCUrlRaw(mTC.ToolCall):
|
||||||
try:
|
|
||||||
# Get requested url
|
def tcf_meta(self) -> mTC.TCFunction:
|
||||||
got = handle_urlreq(ph, pr, "HandleUrlRaw")
|
return mTC.TCFunction(
|
||||||
if not got.callOk:
|
self.name,
|
||||||
ph.send_error(got.httpStatus, got.httpStatusMsg)
|
"Fetch contents of the requested url (local file path / web based) through a proxy server and return the got content as is, in few seconds. Mainly useful for getting textual non binary contents",
|
||||||
return
|
mTC.TCInParameters(
|
||||||
# Send back to client
|
"object",
|
||||||
ph.send_response(got.httpStatus)
|
{
|
||||||
ph.send_header('Content-Type', got.contentType)
|
"url": mTC.TCInProperty(
|
||||||
# Add CORS for browser fetch, just in case
|
"string",
|
||||||
ph.send_header('Access-Control-Allow-Origin', '*')
|
"url of the local file / web content to fetch"
|
||||||
ph.end_headers()
|
)
|
||||||
ph.wfile.write(got.contentData.encode('utf-8'))
|
},
|
||||||
except Exception as exc:
|
[ "url" ]
|
||||||
ph.send_error(502, f"WARN:UrlRawFailed:{exc}")
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
def tc_handle(self, args: mTC.TCInArgs, inHeaders: http.client.HTTPMessage) -> mTC.TCOutResponse:
|
||||||
|
try:
|
||||||
|
# Get requested url
|
||||||
|
got = handle_urlreq(args['url'], inHeaders, "HandleTCUrlRaw")
|
||||||
|
return got
|
||||||
|
except Exception as exc:
|
||||||
|
return mTC.TCOutResponse(False, 502, f"WARN:UrlRawFailed:{exc}")
|
||||||
|
|
||||||
|
|
||||||
class TextHtmlParser(html.parser.HTMLParser):
|
class TextHtmlParser(html.parser.HTMLParser):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue