Documentation
¶
Overview ¶
Package model provides the provider abstraction (§40) and the centralized structured-output pipeline (§39). Providers are config-driven; the fake provider is the deterministic CI path, while the HTTP adapters are exercised by the tagged real-provider smoke test.
Index ¶
- Variables
- func CostUSD(model string, inputTokens, outputTokens int64) float64
- func DefaultModelFor(provider string) string
- type AnthropicProvider
- type Capabilities
- type FakeProvider
- func (f *FakeProvider) Calls() []Request
- func (f *FakeProvider) Capabilities() Capabilities
- func (f *FakeProvider) Complete(_ context.Context, req Request) (Response, error)
- func (f *FakeProvider) EnqueueResponse(schemaName, rawJSON string)
- func (f *FakeProvider) Name() string
- func (f *FakeProvider) SetResponse(schemaName, rawJSON string)
- type GeminiProvider
- type Image
- type Message
- type ModelLister
- type OllamaProvider
- type OpenAIProvider
- type Pricing
- type Provider
- type Registry
- func (r *Registry) Add(name string, p Provider)
- func (r *Registry) Complete(ctx context.Context, spec string, req Request) (Response, error)
- func (r *Registry) Default() string
- func (r *Registry) Get(name string) (Provider, bool)
- func (r *Registry) Len() int
- func (r *Registry) ListModels(ctx context.Context, name string) ([]string, error)
- func (r *Registry) Names() []string
- func (r *Registry) Resolve(spec string) (Provider, string, error)
- func (r *Registry) SetDefault(name string)
- type Request
- type Response
- type StructuredResult
- type Usage
Constants ¶
This section is empty.
Variables ¶
var DefaultModels = map[string][]string{
"openai": {"gpt-4o", "gpt-4o-mini", "gpt-4.1", "gpt-4.1-mini", "o3-mini"},
"anthropic": {"claude-sonnet-4-5", "claude-3-7-sonnet-latest", "claude-3-5-haiku-latest", "claude-opus-4-1"},
"gemini": {"gemini-2.5-flash", "gemini-2.5-pro", "gemini-2.0-flash", "gemini-1.5-pro"},
"deepseek": {"deepseek-chat", "deepseek-reasoner"},
"openrouter": {"openai/gpt-4o", "anthropic/claude-3.7-sonnet", "google/gemini-2.5-flash", "meta-llama/llama-4-maverick"},
"ollama": {"llama3.1", "llama3.2", "qwen2.5", "mistral", "deepseek-r1"},
}
DefaultModels is a curated catalog of common models per provider. It seeds the UI/CLI pickers and, since audit 4, backs config.UnknownModelProblems.
Still advisory in the sense that matters: any model name is accepted, and a name absent from this list produces a *warning*, never a rejection, because a valid model can postdate the catalog. But it is no longer only cosmetic -- deleting an entry here makes a correct id start warning, so keep it current.
var ErrTruncated = errors.New("model: output truncated at the token budget")
ErrTruncated reports that the provider stopped because it hit the output budget rather than because the model finished.
This is the safety net under the stage-budget cap. Capping a request to the provider's ceiling turns a guaranteed 400 into a request that succeeds -- but a structured reply cut mid-object is NOT reliably caught downstream: repairJSON closes unterminated strings and containers, and the review schema has no minItems, so a findings list truncated from 15 entries to 1 validates clean. The result would be a silently shorter adversarial review.
So truncation is detected where it is actually visible -- the provider's own finish reason -- and raised loudly. The engine's retry loop resends the same request, so a truncation surfaces as a real failure with a message naming the cause instead of a quietly diminished artifact.
Functions ¶
func CostUSD ¶
CostUSD returns the cost of input/out tokens (counts) for a model name, using the canonical pricing table and longest-prefix normalization.
func DefaultModelFor ¶
DefaultModelFor returns a sensible default model name for a provider, or "model" if the provider is unknown.
Types ¶
type AnthropicProvider ¶
type AnthropicProvider struct {
// contains filtered or unexported fields
}
AnthropicProvider calls the Anthropic messages API natively (§41).
func NewAnthropic ¶
func NewAnthropic(apiKey, baseURL string) *AnthropicProvider
NewAnthropic builds an Anthropic provider.
func (*AnthropicProvider) Capabilities ¶
func (p *AnthropicProvider) Capabilities() Capabilities
func (*AnthropicProvider) ListModels ¶
func (p *AnthropicProvider) ListModels(ctx context.Context) ([]string, error)
ListModels lists models for Anthropic via GET /models.
func (*AnthropicProvider) Name ¶
func (p *AnthropicProvider) Name() string
type Capabilities ¶
type Capabilities struct {
StructuredOutput bool
Tools bool
Streaming bool
ReasoningControls bool
ContextLimit int
MaxOutput int
}
Capabilities describes what a provider/model supports (§40).
type FakeProvider ¶
type FakeProvider struct {
// contains filtered or unexported fields
}
FakeProvider is a deterministic provider for hermetic CI and tests. When no scripted response is registered it generates input-aware (but clearly synthetic) output from the user's idea, so the no-provider demo responds to what the user actually typed instead of returning hardcoded content.
func NewFakeProvider ¶
func NewFakeProvider() *FakeProvider
NewFakeProvider returns a fake provider with full capabilities.
func (*FakeProvider) Calls ¶
func (f *FakeProvider) Calls() []Request
Calls returns the requests seen so far (for assertions).
func (*FakeProvider) Capabilities ¶
func (f *FakeProvider) Capabilities() Capabilities
Capabilities implements Provider.
func (*FakeProvider) EnqueueResponse ¶
func (f *FakeProvider) EnqueueResponse(schemaName, rawJSON string)
EnqueueResponse appends a response to the FIFO queue for a schema name; the queue is consumed before the default response.
func (*FakeProvider) SetResponse ¶
func (f *FakeProvider) SetResponse(schemaName, rawJSON string)
SetResponse registers the raw JSON to return for a schema name.
type GeminiProvider ¶
type GeminiProvider struct {
// contains filtered or unexported fields
}
GeminiProvider calls the Google generative-language API natively (§41).
func NewGemini ¶
func NewGemini(apiKey, baseURL string) *GeminiProvider
NewGemini builds a Google Gemini provider. baseURL defaults to the v1beta generative-language endpoint.
func (*GeminiProvider) Capabilities ¶
func (p *GeminiProvider) Capabilities() Capabilities
func (*GeminiProvider) ListModels ¶
func (p *GeminiProvider) ListModels(ctx context.Context) ([]string, error)
ListModels lists models for Google Gemini via GET /models.
func (*GeminiProvider) Name ¶
func (p *GeminiProvider) Name() string
type Message ¶
type Message struct {
Role string `json:"role"` // system | user | assistant
Content string `json:"content"`
}
Message is a single conversation turn.
type ModelLister ¶
ModelLister is implemented by providers that can list their available models. The catalog (DefaultModels) is only a fallback when a provider cannot (or does not need to) enumerate models live.
type OllamaProvider ¶
type OllamaProvider struct {
*OpenAIProvider
// contains filtered or unexported fields
}
OllamaProvider is an OpenAI-compatible local provider that additionally enumerates installed models via GET /api/tags (at the server root, not /v1).
func NewOllama ¶
func NewOllama(baseURL string) *OllamaProvider
NewOllama builds an Ollama provider for the given OpenAI-compatible base URL (e.g. http://localhost:11434/v1).
func (*OllamaProvider) ListModels ¶
func (p *OllamaProvider) ListModels(ctx context.Context) ([]string, error)
ListModels lists installed Ollama models via GET /api/tags.
type OpenAIProvider ¶
type OpenAIProvider struct {
// contains filtered or unexported fields
}
OpenAIProvider calls an OpenAI-compatible chat-completions API (§41). It is used for OpenAI, OpenRouter, Ollama, and any custom OpenAI-compatible endpoint (Groq, Together, LM Studio, …).
func NewOpenAI ¶
func NewOpenAI(apiKey, baseURL string) *OpenAIProvider
NewOpenAI builds an OpenAI provider.
func NewOpenAICompat ¶
func NewOpenAICompat(name, apiKey, baseURL string) *OpenAIProvider
NewOpenAICompat builds an OpenAI-compatible provider with a custom name.
func (*OpenAIProvider) Capabilities ¶
func (p *OpenAIProvider) Capabilities() Capabilities
func (*OpenAIProvider) ListModels ¶
func (p *OpenAIProvider) ListModels(ctx context.Context) ([]string, error)
ListModels lists models for an OpenAI-compatible provider (OpenAI, OpenRouter, Groq, Together, …) via GET /models.
func (*OpenAIProvider) Name ¶
func (p *OpenAIProvider) Name() string
type Provider ¶
type Provider interface {
Name() string
Capabilities() Capabilities
// Complete runs one request and returns a response. It must set
// ActualModel to the model actually used (no silent fallback, §36).
Complete(ctx context.Context, req Request) (Response, error)
}
Provider is a model provider adapter.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds multiple named providers and routes requests by model spec ("provider/model"). It supports OpenAI, Anthropic, Gemini, OpenRouter, Ollama, and arbitrary OpenAI-compatible endpoints (§40).
func NewRegistry ¶
NewRegistry builds an empty registry with a default provider name.
func (*Registry) ListModels ¶
ListModels returns live models for a provider, falling back to the catalog.
func (*Registry) Resolve ¶
Resolve maps a model spec to a provider and model name. A spec of "provider/model" selects that provider; a bare model name uses the default provider (§40). Splitting is on the first slash.
func (*Registry) SetDefault ¶
SetDefault sets the provider used when a model spec has no "/" prefix.
type Request ¶
type Request struct {
Model string
System string
Messages []Message
Images []Image
ReasoningEffort string
MaxOutput int
JSONSchema []byte // if set, structured output is requested
JSONSchemaName string
}
Request is a normalized model request. Images, when present, are attached to the (single) user message as vision content by providers that support it.
type Response ¶
Response is a normalized model response. RawJSON is the provider's raw (post-redaction) output; Text is the plain-text form when not structured.
type StructuredResult ¶
type StructuredResult struct {
Raw []byte // raw model output (post-redaction)
Repaired []byte // repaired content (post-redaction)
Valid map[string]any // validated object
WasRepaired bool
}
StructuredResult is the outcome of the structured-output pipeline (§39).
func DecodeStructured ¶
func DecodeStructured(raw []byte, schema []byte) (*StructuredResult, error)
DecodeStructured validates raw JSON against schema, applying bounded repair on failure (single pass). It returns the validated object and whether repair was used. Raw and repaired content are returned for digest storage.