docs

package
v0.50.68 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLibraryNotFound = errors.New("library not found")

ErrLibraryNotFound is returned when a library cannot be resolved.

View Source
var ErrQuotaExceeded = errors.New("quota exceeded")

ErrQuotaExceeded is returned when the upstream documentation source reports quota exhaustion.

View Source
var ErrRateLimited = errors.New("rate limited")

ErrRateLimited is returned when the upstream documentation source rejects requests due to rate limits.

Functions

func CalculateTokenBudget

func CalculateTokenBudget(libCount int) int

CalculateTokenBudget returns the per-library token budget based on the number of libraries. Adaptive schedule: 1→5000, 2→3000, 3→2500, 4-5→2000. Hard cap: total ≤ 10000.

func DetectFromGoMod

func DetectFromGoMod(path string) ([]string, error)

DetectFromGoMod parses a go.mod file and returns each direct dependency module path as-is (e.g., "github.com/spf13/cobra"). Indirect dependencies (lines with "// indirect") are skipped.

func DetectFromPackageJSON

func DetectFromPackageJSON(path string) ([]string, error)

DetectFromPackageJSON parses a package.json file and returns all dependency and devDependency package names.

func DetectFromPyProjectToml

func DetectFromPyProjectToml(path string) ([]string, error)

DetectFromPyProjectToml parses a pyproject.toml file and returns dependency names, stripping version specifiers and extras (e.g., "fastapi[all]>=0.100.0" → "fastapi").

func DetectFromText

func DetectFromText(lang string, text string) []string

DetectFromText scans free-form text (e.g., SPEC or plan.md) and returns known library names for the given language, excluding standard library modules.

func FilterStdLib

func FilterStdLib(lang string, libs []string) []string

FilterStdLib removes standard library module names from the input slice for the given language ("go", "node", "python").

func FormatPromptInjection

func FormatPromptInjection(results []*DocResult) (string, error)

FormatPromptInjection formats documentation results as a prompt injection section. Returns an error if results is nil or empty.

Types

type Cache

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

Cache stores documentation entries on disk as JSON files with TTL support.

func NewCache

func NewCache(dir string, ttl time.Duration) *Cache

NewCache creates a Cache that persists entries to dir with the given TTL.

func (*Cache) Clear

func (c *Cache) Clear() error

Clear removes all .json cache files in the cache directory.

func (*Cache) Get

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

Get reads the cached entry for key. Returns nil if not found or expired.

func (*Cache) List

func (c *Cache) List() ([]ListEntry, error)

List returns metadata for all cached entries including their expiry time.

func (*Cache) Set

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

Set writes entry to disk under the given key, setting CachedAt to now.

type CacheEntry

type CacheEntry struct {
	LibraryID string    // e.g., "/spf13/cobra"
	Topic     string    // e.g., "commands"
	Version   string    // resolved library version, when available
	SourceRef string    // library ID or official URL used as evidence
	Content   string    // documentation content
	Tokens    int       // approximate token count
	CachedAt  time.Time // when the entry was cached (for TTL)
}

CacheEntry represents a cached documentation entry on disk.

type Context7Client

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

Context7Client fetches documentation from the Context7 API.

func NewContext7Client

func NewContext7Client(baseURL string) *Context7Client

NewContext7Client creates a new Context7Client with the given base URL. If baseURL is empty, the default Context7 API URL is used.

func (*Context7Client) Fetch

func (c *Context7Client) Fetch(library, topic string) (*DocResult, error)

Fetch implements the DocFetcher interface. It resolves the library name then fetches documentation for the given topic, returning a DocResult.

func (*Context7Client) GetDocs

func (c *Context7Client) GetDocs(libraryID, topic string) (*DocContent, error)

GetDocs fetches documentation for a library ID and topic from the Context7 API. Returns an error for any non-200 response.

func (*Context7Client) ResolveLibrary

func (c *Context7Client) ResolveLibrary(name string) (*LibraryInfo, error)

ResolveLibrary resolves a library name to a LibraryInfo using the Context7 API. Returns ErrLibraryNotFound if the library cannot be found (HTTP 404).

type DocContent

type DocContent struct {
	Content string // documentation content
	Tokens  int    // token count
}

DocContent is the result of fetching docs for a library.

type DocFetcher

type DocFetcher interface {
	Fetch(library, topic string) (*DocResult, error)
}

DocFetcher is the interface for documentation sources (Context7, Scraper).

type DocResult

type DocResult struct {
	LibraryName string // e.g., "cobra"
	Package     string // e.g., "github.com/spf13/cobra"
	Source      string // "context7", "scraper", or "cache"
	Version     string // resolved library version, when the upstream source reports it
	SourceRef   string // library ID or official URL used as evidence
	CheckedAt   time.Time
	Content     string // documentation text content
	Tokens      int    // approximate token count
}

DocResult represents a fetched documentation result.

type Fetcher

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

Fetcher orchestrates documentation retrieval with a fallback chain: Context7 → Scraper → Cache.

func NewFetcher

func NewFetcher(c7 DocFetcher, scraper DocFetcher, cache FetcherCache) *Fetcher

NewFetcher creates a Fetcher with the given Context7 client, scraper, and cache.

func (*Fetcher) Fetch

func (f *Fetcher) Fetch(library, topic string) (*DocResult, error)

Fetch retrieves documentation for a library and topic using the fallback chain. Order: Context7 → Scraper → Cache. The first successful source is cached and returned.

func (*Fetcher) FetchMultiple

func (f *Fetcher) FetchMultiple(libraries []string, topic string) ([]*DocResult, error)

FetchMultiple retrieves documentation for multiple libraries with adaptive token budgeting. Each result is trimmed to the per-library budget (budget * 4 chars ≈ tokens).

type FetcherCache

type FetcherCache interface {
	Get(key string) (*DocResult, error)
	Set(key string, result *DocResult) error
}

FetcherCache is the interface for the cache used by Fetcher.

type LibraryInfo

type LibraryInfo struct {
	ID      string // Context7 library ID
	Name    string // library name
	Version string // version string
}

LibraryInfo is the result of resolving a library name.

type ListEntry

type ListEntry struct {
	Key       string    // cache key e.g. "cobra:commands"
	ExpiresAt time.Time // when the entry expires
}

ListEntry represents a cache list item.

type Scraper

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

Scraper fetches documentation from public package registries.

func NewScraper

func NewScraper(opts ...ScraperOption) *Scraper

NewScraper creates a Scraper with default settings, applying any provided options.

func (*Scraper) Fetch

func (s *Scraper) Fetch(library, _ string) (*DocResult, error)

Fetch implements DocFetcher. It auto-detects the language/registry from the library name. Libraries with "/" or starting with "github.com" are treated as Go packages. Libraries with no "/" and no "." are treated as npm packages. Otherwise, PyPI is tried.

func (*Scraper) FetchGoDocs

func (s *Scraper) FetchGoDocs(pkg string) (*DocResult, error)

FetchGoDocs fetches Go package documentation from pkg.go.dev. It extracts text from the <section id="pkg-overview"> element.

func (*Scraper) FetchNpmDocs

func (s *Scraper) FetchNpmDocs(pkg string) (*DocResult, error)

FetchNpmDocs fetches npm package documentation from the registry JSON API. It uses the readme field, falling back to description if readme is empty.

func (*Scraper) FetchPyPIDocs

func (s *Scraper) FetchPyPIDocs(pkg string) (*DocResult, error)

FetchPyPIDocs fetches Python package documentation from the PyPI JSON API. It uses the info.description field, falling back to info.summary.

type ScraperOption

type ScraperOption func(*Scraper)

ScraperOption configures a Scraper instance.

func WithGoDocsBaseURL

func WithGoDocsBaseURL(url string) ScraperOption

WithGoDocsBaseURL overrides the pkg.go.dev base URL (useful for tests).

func WithGoProxyBaseURL added in v0.46.0

func WithGoProxyBaseURL(url string) ScraperOption

WithGoProxyBaseURL overrides the Go module proxy base URL (useful for tests).

func WithNpmRegistryURL

func WithNpmRegistryURL(url string) ScraperOption

WithNpmRegistryURL overrides the npm registry base URL (useful for tests).

func WithPyPIBaseURL

func WithPyPIBaseURL(url string) ScraperOption

WithPyPIBaseURL overrides the PyPI base URL (useful for tests).

Jump to

Keyboard shortcuts

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