fetch

package
v0.2.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: 17 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 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 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 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 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.

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 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
}

Params holds the inputs to a single fetch. Timeout bounds the request (zero means use DefaultTimeout). ReturnFormat selects "markdown" (default, when empty) or "text". NoCache requests a fresh fetch that bypasses the cache.

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 markdown body — extracted by Readability, falling back to full-document markdown so the reader never returns empty. For raw-text documents Content is the decoded body verbatim (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.

Validated results are served from the in-memory cache keyed by (url, return_format); params.NoCache forces a fresh fetch. The request is bounded by params.Timeout (default 30s). A non-2xx response returns an HTTPError. 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 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