Class WebFunctionTools
- All Implemented Interfaces:
FunctionTools
Genai.
This tool set provides two host-side functions:
get_web_content– Fetches web page content over HTTP(S) via GET and optionally returns plain text or content selected via a CSS selector.call_rest_api– Executes a generic REST call using an arbitrary HTTP method with optional headers and request body.
Header variable placeholders
Header values may include placeholders in the form ${propertyName}. When a Configurator is provided via
setConfigurator(Configurator), those placeholders are resolved at runtime.
Authentication
HTTP Basic authentication is supported via the URL userInfo component (for example
https://user:password@host/path), which is converted into an Authorization: Basic ... header. You
can also specify an explicit Authorization header.
Outbound network policy (allow/deny lists) is intentionally left to the host application.
- Author:
- Viktor Tovstyi
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) StringapplySelectorIfPresent(String selector, String response) Applies a CSS selector to the response HTML if one was provided.voidapplyTools(Genai provider) Registers web content and REST API function tools with the providedGenai.callRestApi(Object[] params) Implementscall_rest_apiby executing an HTTP request against the provided endpoint.(package private) voidfillHeader(String headers, HttpURLConnection connection) Applies headers to the connection.(package private) HttpURLConnectiongetConnection(URI uri, String headers) Creates and configures anHttpURLConnection.getWebContent(Object[] params) Implementsget_web_contentby retrieving web content via an HTTP GET request.(package private) StringgetWebPage(HttpURLConnection connection, int timeout, String charsetName) Performs the HTTP GET request and returns the response content.voidsetConfigurator(Configurator configurator) Supplies configuration for resolving header placeholders.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.machanism.machai.ai.tools.FunctionTools
replace
-
Constructor Details
-
WebFunctionTools
public WebFunctionTools()
-
-
Method Details
-
applyTools
Registers web content and REST API function tools with the providedGenai.- Specified by:
applyToolsin interfaceFunctionTools- Parameters:
provider- the provider to register tools with
-
getWebContent
Implementsget_web_contentby retrieving web content via an HTTP GET request.Parameters are passed in
params:JsonNodecontaining the tool arguments- (optional) additional runtime-supplied arguments, ignored by this tool
Supported JSON properties:
url(required) – target URLheaders(optional) – newline-separatedNAME=VALUEpairstimeout(optional) – timeout in milliseconds (default 10000)charsetName(optional) – response decoding charset (defaultUTF-8)textOnly(optional) – iftrue, strips HTML to plain textselector(optional) – extracts content matching the CSS selector (text or HTML depending ontextOnly)
- Parameters:
params- tool arguments- Returns:
- response content or an error message
-
applySelectorIfPresent
Applies a CSS selector to the response HTML if one was provided.- Parameters:
selector- CSS selector (may be blank)response- full response content- Returns:
- selected HTML content (joined with newlines) or the original response if
selectoris blank
-
getConnection
Creates and configures anHttpURLConnection.If the URI contains
userInfo, it is removed from the request URI and used to set an HTTP BasicAuthorizationheader.- Parameters:
uri- URI to connect toheaders- optional headers (newline-separatedNAME=VALUE)- Returns:
- connection
- Throws:
IOException- if opening a connection fails
-
getWebPage
Performs the HTTP GET request and returns the response content.- Parameters:
connection- open connectiontimeout- timeout in millisecondscharsetName- charset used to decode the response- Returns:
- response content including an initial status line
- Throws:
IOException- if the request cannot be executed
-
callRestApi
Implementscall_rest_apiby executing an HTTP request against the provided endpoint.Supported JSON properties:
url(required) – endpoint URLmethod(optional) – HTTP method (defaultGET)headers(optional) – newline-separatedNAME=VALUEpairsbody(optional) – request body (used for POST/PUT/PATCH only)timeout(optional) – timeout in milliseconds (default 10000)charsetName(optional) – request/response charset (defaultUTF-8)
- Parameters:
params- tool arguments- Returns:
- response content including an initial HTTP status line, or an error message
-
fillHeader
Applies headers to the connection.Each header line must be in the form
NAME=VALUE. Header values may include ${...} placeholders.- Parameters:
headers- newline-separated header definitionsconnection- connection to configure
-
setConfigurator
Supplies configuration for resolving header placeholders.- Specified by:
setConfiguratorin interfaceFunctionTools- Parameters:
configurator- configurator to use (may benull)
-