web-fetch fetches a page of the public web for a model and cleans it to compact text. It is the web_fetch tool of agc-agent, shared as a package so that other programs fetch the same way: WebFetch.fetch makes the request, WebFetch.check_url is the URL check it makes first, and WebFetch.compact_html is the cleaning of HTML. It runs on the http-client package.

Install #

Needs Agentica 0.40.0 or later. Add it in a project with an agentica.toml:

agentica add web-fetch https://github.com/agentica-lang/packages --tag web-fetch-0.2.0 --subdir web-fetch

That writes this line of agentica.toml, and the entry of agentica.lock:

[dependencies]
web-fetch = { git = "https://github.com/agentica-lang/packages", tag = "web-fetch-0.2.0", subdir = "web-fetch" }

Example #

import WebFetch from "pkg/web-fetch/web_fetch"

def main() -> Void
  case WebFetch.get("https://example.com/", WebFetch.Settings(timeout_ms: 8_000, follow_redirects: true))
    ok(page) -> puts("#{page.status} #{page.processor_used}: #{page.body}")
    error(e) -> puts(WebFetch.code_of(e))
  end
end

Public addresses only #

fetch reaches public addresses only. The host is resolved and every address that is not public (loopback, private, link-local, reserved, and the IPv4 forms inside IPv6) is dropped; the connection goes to an address that was checked, so a second DNS answer cannot lead elsewhere, and a redirect is dialed the same way, hop by hop. That is the public_only filter of http-client. Before any lookup, check_url refuses what the system would read as this machine: localhost and *.localhost, a host that ends in a number but is not a plain dotted quad (127.1, 2130706433, 0x7f.0.0.1), user info, and a host or port that no URL has.

The address rule cannot be turned off. Settings.public_address, resolver and compactor are seams for tests: a test makes its peer on 127.0.0.1 count as public (and every other address stays refused, at every hop), picks what a name resolves to, or replaces the compaction. Never set them from a config or from the input of a model.

Untrusted pages #

The text of a page is the page's, whatever it says. Give it to a model as data, never as instructions.

API #

WebFetch.fetch(settings, args) -> Result[WebFetch.Page] #

Makes the request of args (its URL stripped) within the limits of settings, and gives the page shaped for a model: HTML as compact text, plain text with its whitespace normalized, JSON as it is. A body over max_bytes is cut and the page says so. An error has a kind that code_of maps to an error code; failure makes the text of it.

WebFetch.get(url, settings = WebFetch.Settings()) -> Result[WebFetch.Page] #

A GET of url: fetch with a FetchArgs of the URL alone.

WebFetch.failure(args, error) -> WebFetch.Failure #

What a failed call of args gives: the error code, and a reason that names the method and the URL.

WebFetch.code_of(error) -> String #

The error code of a failure. The codes are the constants WebFetch.PRIVATE_ADDRESS (web_fetch_private_address), INVALID_URL (web_fetch_invalid_url), TIMEOUT (web_fetch_timeout), INVALID_REQUEST (web_fetch_invalid_request) and FAILED (web_fetch_failed). A URL that names this machine fails before any lookup, so this example makes no connection:

import WebFetch from "pkg/web-fetch/web_fetch"

def main() -> Void
  request = WebFetch.FetchArgs(url: "http://localhost:8080/admin", method: some("post"))
  case WebFetch.fetch(WebFetch.Settings(), request)
    ok(_) -> puts("fetched")
    error(e) ->
      puts(WebFetch.code_of(e) == WebFetch.PRIVATE_ADDRESS)
      puts(WebFetch.failure(request, e).error)
  end
end
Output
true
POST http://localhost:8080/admin failed: web_fetch: localhost is a local or private address

WebFetch.method_of(args) -> String #

The method of args in upper case, GET when none.

WebFetch.check_url(url) -> Result[String] #

The URL stripped, when it is an http or https URL that fetch would try; else an error of kind web_fetch.url (a bad URL) or web_fetch.private_address (a host that is, or may be read as, this machine).

import WebFetch from "pkg/web-fetch/web_fetch"

def main() -> Void
  for url in [" https://example.com/a ", "http://127.1/", "http://user@example.com/"]
    case WebFetch.check_url(url)
      ok(checked) -> puts("ok #{checked}")
      error(e) -> puts(e.kind)
    end
  end
end
Output
ok https://example.com/a
web_fetch.private_address
web_fetch.url

WebFetch.canonical_url(url) -> Result[String] #

The URL as a rule matches it: the scheme and the host in lower case, no trailing dot on the host, no default port, and / when there is no path.

import WebFetch from "pkg/web-fetch/web_fetch"

def main() -> Void
  puts(WebFetch.canonical_url("HTTPS://Docs.Example.com.:443").unwrap!)
end
Output
https://docs.example.com/

WebFetch.compact_html(html) -> String #

A page of HTML as compact, indented text: it keeps what a model acts on (text, links with their targets, forms, inputs, headings, lists, tables) and drops the presentation (classes, styles, scripts, SVG, comments, hidden nodes). A line is a tag and its text in quotes. It never fails, and gives an empty string for a page with no text.

import WebFetch from "pkg/web-fetch/web_fetch"

def main() -> Void
  html = "<h1>News</h1><script>steal()</script><p class=\"x\">Agents <b>ship</b> fast.</p><a href=\"/more\">More</a>"
  puts(WebFetch.compact_html(html))
end
Output
h1 "News"
p "Agents ship fast."
a "More" /more

WebFetch.Settings #

The limits of a call: timeout_ms (10 s; the whole request gets three times that), follow_redirects (false) and max_redirects (5), max_bytes (16384, the most bytes of a body kept), max_parse_bytes (2000000, how much of an HTML page is read for the compaction), compact_chars and compact_timeout_ms (the compaction runs in a task of its own; past either limit the page is plain text), and max_chars (the longest text given back, max_bytes when none). public_address, resolver and compactor are the test seams above.

WebFetch.FetchArgs #

A request: url, and optional method, headers, body (text) and json (all none by default) (the text of a JSON value, not with body). A header that the client sets itself (Host, Content-Length, Transfer-Encoding, Connection), a name that is not an HTTP token, or a value with a control character is refused before any connection.

WebFetch.Page #

What a good call gives: status, headers (the first MAX_HEADERS, 10), body (the text), content_type, truncated (the body was over max_bytes), truncated_chars (the text was over max_chars) and processor_used (html_compact, plain or none).

WebFetch.Failure #

What a failed call gives: ok (false), error_code, error (the reason) and url.