Documentation
¶
Overview ¶
Package provider defines the model-backend abstraction and a registry mapping a provider "kind" to a factory. Concrete implementations live in subpackages (e.g. provider/openai) and self-register via init(). The core resolves providers by kind from config and never hardcodes a specific model.
Index ¶
- func Kinds() []string
- func Register(kind string, f Factory)
- type AuthError
- type Chunk
- type ChunkType
- type Config
- type Factory
- type FallbackProvider
- type Message
- type Pricing
- type Provider
- type ProviderEntry
- type ProviderRegistry
- type Request
- type Role
- type Tier
- type ToolCall
- type ToolSchema
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AuthError ¶
type AuthError struct {
Provider string // the provider instance name, e.g. "deepseek"
KeyEnv string // the api_key_env the key is read from, when known
Status int // the HTTP status (401 or 403)
}
AuthError reports that a provider rejected the API key (HTTP 401/403). Its message is already user-facing and actionable — it names the provider and, when known, the environment variable the key comes from — so the CLI can surface it verbatim instead of dumping a raw status body. Providers should return this (rather than a generic status error) for auth failures.
type Chunk ¶
type Chunk struct {
Type ChunkType
Text string // ChunkText, ChunkReasoning
ToolCall *ToolCall // ChunkToolCallStart (ID+Name only), ChunkToolCall (complete)
Usage *Usage // ChunkUsage
Err error // ChunkError
}
Chunk is a single streamed event. Read the field matching Type.
type ChunkType ¶
type ChunkType int
ChunkType identifies the kind of a streamed increment.
const ( ChunkText ChunkType = iota // text delta ChunkReasoning // thinking-mode reasoning delta (before the visible answer) ChunkToolCallStart // a tool call has begun (ToolCall: ID+Name; args still streaming) ChunkToolCall // one complete tool call ChunkUsage // token usage for the completion ChunkDone // completion finished normally ChunkError // an error occurred )
type Config ¶
type Config struct {
Name string // instance name, e.g. "deepseek"
BaseURL string // OpenAI-compatible endpoint
Model string // model id
APIKey string // resolved from api_key_env
APIKeyEnv string // the env var name the key comes from, for actionable auth errors
Extra map[string]any // kind-specific options
}
Config is a resolved provider instance configuration.
type FallbackProvider ¶
FallbackProvider wraps a primary and secondary provider. When the primary fails (Stream returns error), it transparently retries with the secondary. This enables offline fallback: primary = cloud API, secondary = local Ollama.
func (*FallbackProvider) Name ¶
func (f *FallbackProvider) Name() string
type Message ¶
type Message struct {
Role Role `json:"role"`
Content string `json:"content,omitempty"`
ReasoningContent string `json:"reasoning_content,omitempty"` // assistant: thinking-mode chain-of-thought, round-tripped on multi-turn
ToolCalls []ToolCall `json:"tool_calls,omitempty"` // set by assistant
ToolCallID string `json:"tool_call_id,omitempty"` // links a tool result to its call
Name string `json:"name,omitempty"` // tool message: tool name
}
Message is a single conversation message.
type Pricing ¶
type Pricing struct {
CacheHit float64 `toml:"cache_hit"` // per 1M cached prompt tokens
Input float64 `toml:"input"` // per 1M uncached prompt tokens
Output float64 `toml:"output"` // per 1M completion tokens
Currency string `toml:"currency"`
}
Pricing is a provider's per-1M-token rates, used to estimate spend. Currency is just a display symbol (default "¥"). toml tags let config decode it.
type Provider ¶
type Provider interface {
// Name returns the provider instance name, e.g. "deepseek" / "mimo".
Name() string
// Stream starts a streaming completion, pushing increments on the channel.
// Canceling ctx must abort the underlying request; a closed channel marks
// the end of the completion.
Stream(ctx context.Context, req Request) (<-chan Chunk, error)
}
Provider is a chat-capable model backend.
type ProviderEntry ¶
ProviderEntry pairs a Provider with its tier.
type ProviderRegistry ¶
type ProviderRegistry struct {
// contains filtered or unexported fields
}
ProviderRegistry holds multiple providers with tier metadata, supporting automatic selection and fallback.
func NewProviderRegistry ¶
func NewProviderRegistry(entries []ProviderEntry) *ProviderRegistry
NewProviderRegistry creates a registry from a slice of providers + tiers.
func (*ProviderRegistry) All ¶
func (r *ProviderRegistry) All() []ProviderEntry
All returns all entries.
type Request ¶
type Request struct {
Messages []Message
Tools []ToolSchema
Temperature float64
MaxTokens int
}
Request is a single completion request.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Arguments string `json:"arguments"`
}
ToolCall is a tool invocation requested by the model. Arguments is raw JSON.
type ToolSchema ¶
type ToolSchema struct {
Name string `json:"name"`
Description string `json:"description"`
Parameters json.RawMessage `json:"parameters"`
}
ToolSchema is a tool definition exposed to the model. Parameters is JSON Schema.
type Usage ¶
type Usage struct {
PromptTokens int
CompletionTokens int
TotalTokens int
CacheHitTokens int // prompt tokens served from cache
CacheMissTokens int // prompt tokens not cached
ReasoningTokens int // subset of CompletionTokens spent on chain-of-thought
FinishReason string // "stop", "tool_calls", "length", "content_filter", "repetition_truncation", …
}
Usage reports token accounting for a completion. Cache hit/miss come from either DeepSeek's top-level prompt_cache_{hit,miss}_tokens or the OpenAI/MiMo standard prompt_tokens_details.cached_tokens — the openai provider normalises both shapes into these fields. ReasoningTokens is the thinking-mode subset of CompletionTokens reported by thinking-capable models. FinishReason carries the model's last reported choices[0].finish_reason so the agent can surface abnormal terminations ("length", "content_filter", "repetition_truncation").
Directories
¶
| Path | Synopsis |
|---|---|
|
Package anthropic implements the Anthropic Claude Messages API provider.
|
Package anthropic implements the Anthropic Claude Messages API provider. |
|
Package ollama registers a local-model provider that speaks the Ollama OpenAI-compatible API (http://localhost:11434/v1).
|
Package ollama registers a local-model provider that speaks the Ollama OpenAI-compatible API (http://localhost:11434/v1). |
|
Package openai implements the OpenAI-compatible /chat/completions provider.
|
Package openai implements the OpenAI-compatible /chat/completions provider. |