webfetch

package
v0.0.0-...-1c1409b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package webfetch reads a URL as markdown for a caller that pays per token: it asks the site for a form meant for agents, follows the llms.txt conventions, and falls back to the readable part of the HTML.

The package follows the /llms.txt proposal for content negotiation, for the `.md` variants of a URL, and for the `llms.txt` file itself. A Rule in the Registry gives a site its own selector, its own furniture list, or a mirror, for the pages the generic pass reads badly.

Index

Constants

View Source
const (
	// MethodNegotiated marks content the site served in a form meant for
	// agents.
	MethodNegotiated = "negotiated"
	// MethodAlternate marks a `rel="alternate" type="text/markdown"` link,
	// from the HTML or from a `Link:` header.
	MethodAlternate = "alternate"
	// MethodVariant marks the `.md` variant of the URL.
	MethodVariant = "md-variant"
	// MethodLlmsTxt marks an `llms.txt` file or one of its entries.
	MethodLlmsTxt = "llms.txt"
	// MethodRulePrefix opens the name of the site rule that served the page.
	MethodRulePrefix = "rule:"
	// MethodGeneric marks HTML with its furniture stripped.
	MethodGeneric = "generic"
)

Method names the branch Read took.

View Source
const (
	// UserAgent identifies the command to a site, and names the product token
	// a robots.txt file matches.
	UserAgent = "agentic-web-fetch/1.0 (+https://github.com/kafo-dev/agentic-tools)"
	// DefaultTimeout bounds one request.
	DefaultTimeout = 32 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Fetcher

type Fetcher struct {
	// contains filtered or unexported fields
}

Fetcher gets pages with colly, under the rules the site publishes in its robots.txt file. A Fetcher holds one collector, so the rules it has read serve every request and a site answers for its robots.txt once.

func New

func New(opts Options) *Fetcher

New returns a fetcher.

func (*Fetcher) Get

func (f *Fetcher) Get(rawURL, accept string) (*Page, error)

Get fetches rawURL, asking for the media types in accept. A response with an error status still arrives as a page; a failed transfer, and a URL the site's robots.txt file disallows, return an error.

type Options

type Options struct {
	// Timeout bounds one request; zero and below keep [DefaultTimeout].
	Timeout time.Duration
	// UserAgent replaces [UserAgent] when it carries text.
	UserAgent string
}

Options configures a Fetcher. The zero value keeps every default.

type Page

type Page struct {
	// URL is the address the response came from, after redirects.
	URL *url.URL
	// Status is the HTTP status code.
	Status int
	// Type is the media type of the body, without its parameters.
	Type string
	// Header holds the response headers.
	Header http.Header
	// Body holds the response body.
	Body []byte
}

Page is one HTTP response.

func (*Page) IsHTML

func (p *Page) IsHTML() bool

IsHTML reports whether the body holds HTML: by its media type, or by the markers a server that mislabels it leaves in the head.

func (*Page) Text

func (p *Page) Text() string

Text returns the body as text.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds the rules, indexed by host.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns a registry that holds no rules.

func (*Registry) Add

func (r *Registry) Add(rules ...Rule)

Add registers rules. A later rule for the same host replaces an earlier one.

func (*Registry) Lookup

func (r *Registry) Lookup(host string) (Rule, bool)

Lookup returns the rule that serves host, preferring an exact host over a wildcard entry.

type Result

type Result struct {
	// URL is the address the content came from, after redirects and
	// negotiation.
	URL string
	// Method names the branch that produced the content: one of
	// [MethodNegotiated], [MethodAlternate], [MethodVariant], [MethodLlmsTxt]
	// and [MethodGeneric], or [MethodRulePrefix] followed by a rule's name.
	Method string
	// Content holds the markdown.
	Content string
}

Result holds one page's content and the branch that produced it.

func Read

func Read(fetcher *Fetcher, rules *Registry, rawURL string) (Result, error)

Read fetches rawURL and returns its content as markdown. The branches run in the order the llms.txt proposal ranks them: what the site served, what it advertises, the `.md` variant, the llms.txt file, a site rule, and finally the HTML already in hand.

type Rule

type Rule struct {
	// Name labels the rule in the reader's output, which reports it as
	// `rule:<name>`.
	Name string
	// Hosts lists the hosts the rule serves. An entry starting with `*.`
	// serves that domain and every subdomain of it.
	Hosts []string
	// Mirror rewrites a page URL into one that serves the same content in a
	// form an agent reads without cleaning: a raw file, an API, a mirror that
	// allows scraping. It reports false when the URL has no mirror.
	Mirror func(u *url.URL) (string, bool)
	// Select holds the CSS selector of the readable part. An empty selector
	// leaves the choice to the generic pass.
	Select string
	// Drop lists CSS selectors of page furniture to remove before conversion,
	// in addition to the elements the generic pass always drops.
	Drop []string
}

Rule tells the reader how to read one site's pages.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL