providers

package
v0.0.0-beta Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package providers defines the common interface for LLM provider implementations.

Index

Constants

View Source
const DefaultRetryAfter = 5 * time.Second

DefaultRetryAfter is applied when a provider returns 429 but doesn't expose the Retry-After header (Google, OpenRouter, Ollama).

Variables

View Source
var (
	ErrRateLimit        = errors.New("rate limited")
	ErrAuth             = errors.New("authentication failed")
	ErrServerError      = errors.New("server error")
	ErrNetwork          = errors.New("network error")
	ErrCreditExhausted  = errors.New("credits exhausted")
	ErrModelUnavailable = errors.New("model unavailable")
	ErrContentFilter    = errors.New("content filtered")

	// ErrEstimateNotSupported is returned when a provider does not support native token estimation.
	ErrEstimateNotSupported = errors.New("provider does not support native token estimation")
)

Provider error sentinels — returned by handleError() in each provider so the assistant loop can show actionable user-facing messages.

View Source
var ErrContextExhausted = errors.New("context length exceeded")

ErrContextExhausted indicates the conversation exceeded the model's context window. Providers wrap their provider-specific context-length errors with this sentinel so the assistant loop can detect and trigger emergency compaction.

Functions

func As

func As[T any](p Provider) (T, bool)

As unwraps wrapper layers (RetryProvider, etc.) to find the first provider that satisfies interface T. Follows the same pattern as errors.As.

func BuildParametersMap

func BuildParametersMap(p tool.Parameters) map[string]any

BuildParametersMap converts tool parameters to the full JSON Schema map format used by OpenAI and OpenRouter.

func BuildPropertyEntry

func BuildPropertyEntry(prop tool.Property) map[string]any

BuildPropertyEntry converts a single Property to a JSON Schema map, recursing into nested types.

func BuildPropertyMap

func BuildPropertyMap(props map[string]tool.Property) map[string]any

BuildPropertyMap converts tool properties to the generic map format used by OpenAI-compatible provider SDKs.

func ClassifyHTTPError

func ClassifyHTTPError(statusCode int, providerName, msg string, retryAfter time.Duration) error

ClassifyHTTPError maps an HTTP status code to a provider-agnostic sentinel. Providers call this AFTER their message-based pre-checks (context exhaustion, content filter, credit/quota patterns) which vary per SDK. When retryAfter is zero and status is 429, DefaultRetryAfter is applied.

func Float32sToFloat64s

func Float32sToFloat64s(vals []float32) []float64

Float32sToFloat64s converts a slice of float32 values to float64.

func IsNetworkError

func IsNetworkError(err error) bool

IsNetworkError reports whether err is a network-level failure (connection refused, DNS resolution, timeout) as opposed to an API-level error from the provider SDK. Network errors bypass SDK error types, so this must be checked BEFORE SDK assertions.

func ParseRetryAfter

func ParseRetryAfter(resp *http.Response) time.Duration

ParseRetryAfter extracts a Retry-After duration from an HTTP response. Returns zero if the response is nil or the header is absent/unparseable.

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to the given value.

func WrapNetworkError

func WrapNetworkError(err error) error

WrapNetworkError wraps a network error with the ErrNetwork sentinel.

Types

type Embedder

type Embedder interface {
	Embed(context.Context, embedding.Request) (embedding.Response, usage.Usage, error)
}

Embedder is an optional interface for providers that support embedding.

type ModelLoader

type ModelLoader interface {
	LoadModel(ctx context.Context, name string) error
	UnloadModel(ctx context.Context, name string) error
}

ModelLoader is an optional interface for providers that support explicit model load/unload.

type Provider

Provider defines the core interface for LLM provider implementations.

type RateLimitError

type RateLimitError struct {
	RetryAfter time.Duration
	Err        error
}

RateLimitError carries retry metadata alongside the ErrRateLimit sentinel. RetryAfter is parsed from the Retry-After header where available (Anthropic, OpenAI). Providers without header access (Google, Ollama, OpenRouter) get DefaultRetryAfter.

func (*RateLimitError) Error

func (e *RateLimitError) Error() string

func (*RateLimitError) Is

func (e *RateLimitError) Is(target error) bool

func (*RateLimitError) Unwrap

func (e *RateLimitError) Unwrap() error

type Reranker

type Reranker interface {
	Rerank(context.Context, rerank.Request) (rerank.Response, error)
}

Reranker is an optional interface for providers that support reranking.

type RetryProvider

type RetryProvider struct {
	Provider // embedded for pass-through of Models, Model, Estimate, etc.
	Bo       backoff.BackOff
}

RetryProvider wraps a Provider with exponential-backoff retry logic for Chat(). All other Provider methods pass through to the embedded provider unchanged.

Retry is stream-safe: once the stream callback has been invoked (partial content emitted to the terminal), the error is marked permanent and no retry is attempted.

Fields are exported because the factory creating RetryProvider lives in internal/providers/ (different package).

func (*RetryProvider) Chat

Chat calls the underlying provider's Chat with retry logic for transient errors.

func (*RetryProvider) Unwrap

func (r *RetryProvider) Unwrap() Provider

Unwrap returns the inner provider, enabling providers.As[T]() to find opt-in interfaces through the retry wrapper.

type Synthesizer

type Synthesizer interface {
	Synthesize(context.Context, synthesize.Request) (synthesize.Response, error)
}

Synthesizer is an optional interface for providers that support speech synthesis.

type Transcriber

type Transcriber interface {
	Transcribe(context.Context, transcribe.Request) (transcribe.Response, error)
}

Transcriber is an optional interface for providers that support audio transcription.

Directories

Path Synopsis
Package adapter bridges Aura's LLM types with Fantasy's unified provider abstraction.
Package adapter bridges Aura's LLM types with Fantasy's unified provider abstraction.
Package anthropic implements the Anthropic Messages API provider.
Package anthropic implements the Anthropic Messages API provider.
Package capabilities defines model capability flags.
Package capabilities defines model capability flags.
Package codex implements the OpenAI Plus (ChatGPT Codex) provider.
Package codex implements the OpenAI Plus (ChatGPT Codex) provider.
Package copilot implements the GitHub Copilot dual-protocol provider.
Package copilot implements the GitHub Copilot dual-protocol provider.
Package google implements the Google Gemini API provider.
Package google implements the Google Gemini API provider.
Package llamacpp implements the llama.cpp server provider.
Package llamacpp implements the llama.cpp server provider.
Package noop provides a no-operation LLM provider that returns fixed responses without making any network calls.
Package noop provides a no-operation LLM provider that returns fixed responses without making any network calls.
Package ollama implements the Ollama provider.
Package ollama implements the Ollama provider.
Package openai implements a generic OpenAI-compatible provider for any /v1/chat/completions endpoint.
Package openai implements a generic OpenAI-compatible provider for any /v1/chat/completions endpoint.
Package openrouter implements the OpenRouter provider.
Package openrouter implements the OpenRouter provider.
Package registry provides model capability enrichment using Catwalk metadata.
Package registry provides model capability enrichment using Catwalk metadata.
Package transport provides HTTP transport with Bearer token authentication.
Package transport provides HTTP transport with Bearer token authentication.

Jump to

Keyboard shortcuts

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