web_fetch

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const MaxContentLen = 50000

MaxContentLen caps body read at 2x this value via io.LimitReader.

Variables

This section is empty.

Functions

func New

func New(cfg Config) (tools.Tool, error)

New creates a new web_fetch tool with the given configuration

func NormalizeString

func NormalizeString(s string) string

NormalizeString trims whitespace and normalizes Unicode

Types

type Cache

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

Cache implements a simple LRU cache with TTL

func NewCache

func NewCache(ttl time.Duration, maxLen int) *Cache

NewCache creates a new cache with the given TTL and max length

func (*Cache) Clear

func (c *Cache) Clear()

Clear removes all entries from the cache

func (*Cache) Get

func (c *Cache) Get(key string) (*CacheEntry, bool)

Get retrieves an entry from the cache

func (*Cache) Set

func (c *Cache) Set(key string, entry *CacheEntry)

Set adds an entry to the cache

type CacheEntry

type CacheEntry struct {
	Bytes       int
	Code        int
	CodeText    string
	ContentType string
	Content     string
	ExpiresAt   time.Time
}

CacheEntry stores fetched content with metadata

type Config

type Config struct {
	Enabled       bool     `toml:"enabled"`
	AllowDomains  []string `toml:"allow_domains"`
	BlockDomains  []string `toml:"block_domains"`
	MaxContentLen int      `toml:"max_content_len"` // max markdown output length
}

Config holds web_fetch configuration

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default configuration

func LoadConfigFromEnv

func LoadConfigFromEnv() Config

LoadConfigFromEnv loads configuration from environment variables

func LoadConfigFromToml

func LoadConfigFromToml(path string) (Config, error)

LoadConfigFromToml loads configuration from a TOML file

func (Config) MarshalJSON

func (c Config) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler for Config

func (*Config) UnmarshalJSON

func (c *Config) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler for Config

type DomainPolicy

type DomainPolicy struct {
	Allow []string
	Block []string
}

DomainPolicy controls which domains are allowed/blocked

func DefaultDomainPolicy

func DefaultDomainPolicy() *DomainPolicy

DefaultDomainPolicy returns a default policy that allows all domains

func (*DomainPolicy) IsAllowed

func (p *DomainPolicy) IsAllowed(domain string) bool

IsAllowed checks if a domain is allowed based on the policy

type FetchResult

type FetchResult struct {
	URL         string `json:"url"`
	Code        int    `json:"code"`
	CodeText    string `json:"code_text"`
	ContentType string `json:"content_type"`
	Bytes       int    `json:"bytes"`
	Markdown    string `json:"markdown"`
	DurationMs  int64  `json:"duration_ms"`
}

FetchResult contains the result of a web fetch operation

func FetchURL

func FetchURL(rawURL string, policy *DomainPolicy) (*FetchResult, error)

FetchURL fetches content from a URL and converts it to markdown.

type Tool

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

Tool implements the tools.Tool interface for web fetching

func (*Tool) Run

func (t *Tool) Run(ctx context.Context, input map[string]any) (tools.Result, error)

Run executes the web fetch operation

func (*Tool) SaveConfigToToml

func (t *Tool) SaveConfigToToml(path string) error

SaveConfigToToml saves configuration to a TOML file

func (*Tool) Spec

func (t *Tool) Spec() llm.ToolSpec

Spec returns the tool specification

Jump to

Keyboard shortcuts

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