fetch

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fetch implements the Web Reader's core retrieval module.

Fetch is the single deep module behind the tool: it takes a URL and returns model-friendly content. The HTTP client is injected so tests never touch the network. See CONTEXT.md for the Web Reader / Extraction vocabulary and docs/adr/ for the decisions this module honours.

Index

Constants

View Source
const CeilingMB = 8

CeilingMB is the hard ceiling on Params.MaxMB. 8 MB covers the ~4 MB a 1M-token research context needs with headroom; beyond that the agent should narrow the fetch instead (ADR-0007).

View Source
const DefaultCacheTTL = time.Hour

DefaultCacheTTL is how long a cached result stays fresh. One hour per the spec — short enough that content churn on most pages is respected, long enough to skip redundant re-fetches within a working session.

View Source
const DefaultMaxMB = 2

DefaultMaxMB is the response size cap applied when Params.MaxMB is zero, in megabytes. 2 MB fits what a 200k-token context can usefully consume; larger needs go through max_mb, not the default (ADR-0007).

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout is applied when Params.Timeout is zero. Per the spec it is 30 seconds — generous enough for a slow public page, short enough that a hung site never blocks the server.

Variables

This section is empty.

Functions

func IsInvalidMaxMBError added in v0.3.0

func IsInvalidMaxMBError(err error) bool

IsInvalidMaxMBError reports whether err is an InvalidMaxMBError.

func IsInvalidReturnFormatError added in v0.3.0

func IsInvalidReturnFormatError(err error) bool

IsInvalidReturnFormatError reports whether err is an InvalidReturnFormatError.

func IsInvalidURLError

func IsInvalidURLError(err error) bool

IsInvalidURLError reports whether err is an InvalidURLError. It is the typed check callers use rather than a brittle string comparison.

func IsSizeLimitExceededError added in v0.3.0

func IsSizeLimitExceededError(err error) bool

IsSizeLimitExceededError reports whether err is a SizeLimitExceededError.

func IsTimeoutError

func IsTimeoutError(err error) bool

IsTimeoutError reports whether err is a TimeoutError or wraps a context deadline-exceeded. Either form is treated as a timeout for the agent.

func IsUnsupportedContentError

func IsUnsupportedContentError(err error) bool

IsUnsupportedContentError reports whether err is an UnsupportedContentError.

func ValidateMaxMB added in v0.3.0

func ValidateMaxMB(maxMB int, zeroMeansDefault bool) error

ValidateMaxMB is the single authority on which max_mb values are allowed, called by both fetch.Fetch and the MCP adapter so the rule and its message cannot drift between the layers (ADR-0007). zeroMeansDefault selects the surface's rule: the Go API accepts 0 as "use the default cap", while the MCP tool surface rejects an explicit 0 as a caller mistake and keeps only its own pointer-based absent-vs-explicit distinction.

Types

type FetchError

type FetchError struct {
	Err error
}

FetchError wraps a transport-level failure (DNS, connection refused, TLS, etc.) as a structured tool-error so callers can tell it apart from HTTP status errors, content-type errors, and timeouts.

func (*FetchError) Error

func (e *FetchError) Error() string

func (*FetchError) Unwrap

func (e *FetchError) Unwrap() error

type HTTPClient

type HTTPClient interface {
	Do(ctx context.Context, req *http.Request) (*http.Response, error)
}

HTTPClient is the seam at which the network is injected. It executes a fully-formed request, so the caller controls headers and method; the client only provides transport. Tests substitute a fake so no test ever reaches the network.

type HTTPError

type HTTPError struct {
	StatusCode int
	Reason     string
}

HTTPError is returned when the server responds with a non-2xx status code. It carries the status code and a short reason so the agent knows what went wrong without seeing the raw response.

func (*HTTPError) Error

func (e *HTTPError) Error() string

type InvalidMaxMBError added in v0.3.0

type InvalidMaxMBError struct {
	MaxMB   int
	Ceiling int
	// ZeroMeansDefault records which surface rejected the value. The Go API
	// treats 0 as "use the default cap" and never rejects it; the MCP tool
	// surface rejects an explicit 0 because an absent field, not 0, means
	// the default there. Error() names each surface's real accepted set so
	// the message stays truthful at both boundaries.
	ZeroMeansDefault bool
}

InvalidMaxMBError is returned when the max_mb parameter falls outside its allowed range: a positive integer no greater than the hard ceiling. The message names the accepted set of the surface that rejected the value so the agent learns the boundary instead of guessing (ADR-0007).

func (*InvalidMaxMBError) Error added in v0.3.0

func (e *InvalidMaxMBError) Error() string

type InvalidReturnFormatError added in v0.3.0

type InvalidReturnFormatError struct {
	Value string
}

InvalidReturnFormatError is returned when the return_format parameter is not one of the accepted values ("markdown", "text"; empty means the markdown default). Like InvalidMaxMBError, a typo'd parameter is a caller mistake the reader surfaces with a typed error naming the accepted values rather than silently defaulting (spec #16).

func (*InvalidReturnFormatError) Error added in v0.3.0

func (e *InvalidReturnFormatError) Error() string

type InvalidURLError

type InvalidURLError struct {
	URL string
}

InvalidURLError is the structured error returned for a malformed URL. It is the "invalid url" tool-error the agent receives — distinct from transport or HTTP errors so callers can tell failure modes apart.

func (*InvalidURLError) Error

func (e *InvalidURLError) Error() string

type PanicError

type PanicError struct {
	Recovered any
}

PanicError is returned when a panic is recovered during the fetch/extract path. Surfacing it as an error keeps the server process alive (ADR-0005).

func (*PanicError) Error

func (e *PanicError) Error() string

type Params

type Params struct {
	URL          string
	Timeout      time.Duration
	ReturnFormat string
	NoCache      bool
	MaxMB        int
}

Params holds the inputs to a single fetch. Timeout bounds the request (zero means use DefaultTimeout). ReturnFormat selects "markdown" (default, when empty) or "text" (both case-insensitive; any other value is rejected with InvalidReturnFormatError). NoCache requests a fresh fetch that bypasses the cache. MaxMB sets the response size cap in MB (zero means DefaultMaxMB); negative values and values above CeilingMB are rejected with InvalidMaxMBError.

type Result

type Result struct {
	Title   string
	Content string
}

Result holds the output of a fetch. Title is the extracted article title (possibly empty for non-article pages; for raw-text documents it is the URL path basename). Content is the model-friendly body (markdown by default, or plain text when Params.ReturnFormat is "text"), extracted by Readability and falling back to the whole document so the reader never returns empty. For raw-text documents Content is the decoded body verbatim, identical for both formats (Passthrough, ADR-0006).

func Fetch

func Fetch(ctx context.Context, params Params, client HTTPClient) (result *Result, err error)

Fetch retrieves the content at params.URL. It validates the URL first: a malformed, non-absolute, or non-http(s) URL returns an InvalidURLError without issuing any request. An out-of-range params.MaxMB likewise returns an InvalidMaxMBError before any lookup, as does an unknown non-empty params.ReturnFormat (InvalidReturnFormatError).

Validated results are served from the in-memory cache keyed by (url, return_format, max_mb); params.NoCache forces a fresh fetch. The request is bounded by params.Timeout (default 30s). A non-2xx response returns an HTTPError. A body over the size cap (params.MaxMB MB, default 2, ceiling 8) returns a SizeLimitExceededError: the body is rejected, never truncated (ADR-0007). An HTML response is extracted as today; a raw-text response (ADR-0006) is returned verbatim; any other content type returns an UnsupportedContentError. A transport failure returns a FetchError. Any panic during the fetch or extract path is recovered and returned as a PanicError, so the process never crashes (ADR-0005).

type SizeLimitExceededError added in v0.3.0

type SizeLimitExceededError struct {
	// SizeBytes is the document's size in bytes when SizeKnown is true (a
	// Content-Length declared it). Otherwise it is the lower bound the
	// bounded read reached before stopping (always LimitBytes + 1).
	SizeBytes int64
	// LimitBytes is the active cap in bytes (MaxMB * 1 MiB).
	LimitBytes int64
	// SizeKnown reports whether the exact size was known up front.
	SizeKnown bool
}

SizeLimitExceededError is returned when a response body is larger than the active size cap. It states the document's size and the limit so the agent can decide whether to raise max_mb or narrow the fetch (ADR-0007). The body is never truncated: a truncated document would let the agent believe it has the whole file when it does not.

func (*SizeLimitExceededError) Error added in v0.3.0

func (e *SizeLimitExceededError) Error() string

type TimeoutError

type TimeoutError struct {
	URL string
}

TimeoutError is returned when the fetch exceeds its deadline. Distinct from FetchError so the agent can tell "slow/unreachable" from "rejected".

func (*TimeoutError) Error

func (e *TimeoutError) Error() string

type UnsupportedContentError

type UnsupportedContentError struct {
	ContentType string
}

UnsupportedContentError is returned when the response is a binary or other non-text type (e.g. a PDF, image, audio/video, or generic binary) or has no Content-Type at all. HTML goes through Extraction and raw text documents go through Passthrough (ADR-0006); the remaining types are reported clearly rather than producing garbage.

func (*UnsupportedContentError) Error

func (e *UnsupportedContentError) Error() string

Jump to

Keyboard shortcuts

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