Documentation
¶
Index ¶
- Constants
- Variables
- func CacheHitRatio(usage *aop.TokenUsage) float64
- func InferFromBaseURL(baseURL string) string
- func IsImageUnsupportedError(err error) bool
- func IsSupportedProvider(name string) bool
- func MessageReasoning(msg *aop.Message) string
- func MessageText(msg *aop.Message) string
- func MessageToolCalls(msg *aop.Message) []*aop.ToolCall
- func MessageToolResult(msg *aop.Message) *aop.ToolResult
- func NormalizeProvider(name string) string
- func StripImageParts(msgs []*aop.Message) []*aop.Message
- func TextMessage(role, content string) *aop.Message
- func TokenUsage(promptTokens, completionTokens, totalTokens, cacheRead, cacheWrite int) *aop.TokenUsage
- func ToolResultMessage(callID string, result *aop.ToolResult) *aop.Message
- func UsageTotalTokens(usage *aop.TokenUsage) int
- func WithFrameObserver(ctx context.Context, observer func(RawFrame)) context.Context
- type APIError
- type AnthropicProvider
- func (p *AnthropicProvider) ChatCompletion(ctx context.Context, req *ChatCompletionRequest) (*ChatCompletionResponse, error)
- func (p *AnthropicProvider) ChatCompletionStream(ctx context.Context, req *ChatCompletionRequest) (<-chan ChatCompletionStreamEvent, error)
- func (p *AnthropicProvider) DisableImages()
- func (p *AnthropicProvider) ListModels(ctx context.Context) ([]string, error)
- func (p *AnthropicProvider) Name() string
- func (p *AnthropicProvider) WebSearch(ctx context.Context, query string, maxResults int) (*WebSearchResponse, error)
- type CacheRetention
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatCompletionStreamEvent
- type Choice
- type OpenAIProvider
- func (p *OpenAIProvider) ChatCompletion(ctx context.Context, req *ChatCompletionRequest) (*ChatCompletionResponse, error)
- func (p *OpenAIProvider) ChatCompletionStream(ctx context.Context, req *ChatCompletionRequest) (<-chan ChatCompletionStreamEvent, error)
- func (p *OpenAIProvider) DisableImages()
- func (p *OpenAIProvider) ListModels(ctx context.Context) ([]string, error)
- func (p *OpenAIProvider) Name() string
- func (p *OpenAIProvider) WebSearch(ctx context.Context, query string, maxResults int) (*WebSearchResponse, error)
- type Provider
- type ProviderConfig
- type RawFrame
- type StreamingProvider
- type WebSearchProvider
- type WebSearchResponse
- type WebSearchResult
Constants ¶
const ( ProviderOpenAI = config.ProviderOpenAI ProviderAnthropic = config.ProviderAnthropic )
Variables ¶
Functions ¶
func CacheHitRatio ¶
func CacheHitRatio(usage *aop.TokenUsage) float64
CacheHitRatio returns the proportion of prompt tokens served from cache.
func InferFromBaseURL ¶
InferFromBaseURL guesses the wire protocol from the base URL when --provider is not set. The official Anthropic endpoint is unambiguous, so it is detected directly. Everything else — including custom third-party gateways — speaks the OpenAI protocol in the common case, so "openai" stays the default. The protocol genuinely cannot be sniffed for an arbitrary gateway (a gateway may serve, e.g., glm models over the Anthropic protocol), so a wrong guess is caught later as an actionable 404 from the provider (see hint404), not a silent failure.
func IsImageUnsupportedError ¶
func IsSupportedProvider ¶
func MessageReasoning ¶
MessageReasoning joins the reasoning parts of an aop message.
func MessageText ¶
MessageText joins the text parts of an aop message.
func MessageToolCalls ¶
MessageToolCalls extracts the tool calls carried by an assistant message.
func MessageToolResult ¶
func MessageToolResult(msg *aop.Message) *aop.ToolResult
MessageToolResult returns the tool result carried by a tool-role message.
func NormalizeProvider ¶
func StripImageParts ¶
StripImageParts rewrites media parts into a placeholder note for models without image support.
func TextMessage ¶
func TokenUsage ¶
func TokenUsage(promptTokens, completionTokens, totalTokens, cacheRead, cacheWrite int) *aop.TokenUsage
TokenUsage builds the canonical usage proto from vendor-reported counters.
func ToolResultMessage ¶
func ToolResultMessage(callID string, result *aop.ToolResult) *aop.Message
func UsageTotalTokens ¶
func UsageTotalTokens(usage *aop.TokenUsage) int
UsageTotalTokens prefers the vendor-reported total and falls back to the sum of input and output tokens.
Types ¶
type APIError ¶
type APIError struct {
Message string `json:"message"`
Type string `json:"type"`
Code string `json:"code"`
StatusCode int `json:"-"`
Header http.Header `json:"-"`
}
func (*APIError) IsRetryable ¶
type AnthropicProvider ¶
type AnthropicProvider struct {
// contains filtered or unexported fields
}
func NewAnthropicProvider ¶
func NewAnthropicProvider(cfg *ProviderConfig) (*AnthropicProvider, error)
func (*AnthropicProvider) ChatCompletion ¶
func (p *AnthropicProvider) ChatCompletion(ctx context.Context, req *ChatCompletionRequest) (*ChatCompletionResponse, error)
func (*AnthropicProvider) ChatCompletionStream ¶
func (p *AnthropicProvider) ChatCompletionStream(ctx context.Context, req *ChatCompletionRequest) (<-chan ChatCompletionStreamEvent, error)
func (*AnthropicProvider) DisableImages ¶
func (p *AnthropicProvider) DisableImages()
func (*AnthropicProvider) ListModels ¶
func (p *AnthropicProvider) ListModels(ctx context.Context) ([]string, error)
ListModels enumerates the model IDs advertised by GET {base}/models. The Anthropic Messages API and the OpenAI-compatible gateways that front it both answer this route with a {"data":[{"id":...}]} list, so the settings UI can offer a model picklist under provider=anthropic instead of erroring with "provider does not support listing models". Implementing this satisfies the probe's modelLister interface for the Anthropic provider.
func (*AnthropicProvider) Name ¶
func (p *AnthropicProvider) Name() string
func (*AnthropicProvider) WebSearch ¶
func (p *AnthropicProvider) WebSearch(ctx context.Context, query string, maxResults int) (*WebSearchResponse, error)
type CacheRetention ¶
type CacheRetention string
CacheRetention controls prompt caching behavior across providers.
const ( CacheNone CacheRetention = "" // no caching (zero value, backward compatible) CacheShort CacheRetention = "short" // Anthropic ephemeral / OpenAI automatic CacheLong CacheRetention = "long" // Anthropic ephemeral+TTL / OpenAI 24h retention )
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Model string
Messages []*aop.Message
Tools []*aop.ToolDefinition
MaxTokens int
Temperature *float64
Stream bool
CacheRetention CacheRetention
SessionID string
}
type ChatCompletionResponse ¶
type ChatCompletionResponse struct {
ID string
Choices []Choice
Usage *aop.TokenUsage
Error *APIError
}
type ChatCompletionStreamEvent ¶
type ChatCompletionStreamEvent struct {
Role string
MessageDelta *aop.MessageDelta
ToolDeltas []*aop.ToolCallDelta
FinishReason string
Usage *aop.TokenUsage
Done bool
Err error
}
ChatCompletionStreamEvent is one parsed SSE chunk. A chunk may carry a text or reasoning delta and/or tool-call deltas; Role is set on the first chunk of a message.
type OpenAIProvider ¶
type OpenAIProvider struct {
// contains filtered or unexported fields
}
func NewOpenAIProvider ¶
func NewOpenAIProvider(cfg *ProviderConfig) (*OpenAIProvider, error)
func (*OpenAIProvider) ChatCompletion ¶
func (p *OpenAIProvider) ChatCompletion(ctx context.Context, req *ChatCompletionRequest) (*ChatCompletionResponse, error)
func (*OpenAIProvider) ChatCompletionStream ¶
func (p *OpenAIProvider) ChatCompletionStream(ctx context.Context, req *ChatCompletionRequest) (<-chan ChatCompletionStreamEvent, error)
func (*OpenAIProvider) DisableImages ¶
func (p *OpenAIProvider) DisableImages()
func (*OpenAIProvider) ListModels ¶
func (p *OpenAIProvider) ListModels(ctx context.Context) ([]string, error)
ListModels enumerates the model IDs the endpoint advertises via the OpenAI-compatible GET /models route. Most third-party gateways implement it, so the settings UI can offer a picklist instead of a free-text field.
func (*OpenAIProvider) Name ¶
func (p *OpenAIProvider) Name() string
func (*OpenAIProvider) WebSearch ¶
func (p *OpenAIProvider) WebSearch(ctx context.Context, query string, maxResults int) (*WebSearchResponse, error)
type Provider ¶
type Provider interface {
Name() string
ChatCompletion(ctx context.Context, req *ChatCompletionRequest) (*ChatCompletionResponse, error)
}
func NewProvider ¶
func NewProvider(cfg *ProviderConfig) (Provider, error)
func NewProviderFromResolved ¶
func NewProviderFromResolved(cfg *ProviderConfig) (Provider, error)
type ProviderConfig ¶
type ProviderConfig struct {
Provider string `yaml:"provider" config:"provider"`
BaseURL string `yaml:"base_url" config:"base_url"`
APIKey string `yaml:"api_key" config:"api_key"`
Model string `yaml:"model" config:"model"`
Proxy string `yaml:"proxy" config:"proxy"`
Timeout int `yaml:"timeout" config:"timeout"`
Images *bool `yaml:"images,omitempty" config:"images"`
MaxTokens int `yaml:"max_tokens,omitempty" config:"max_tokens"`
ContextWindow int `yaml:"context_window,omitempty" config:"context_window"`
}
func Resolve ¶
func Resolve(cfg *ProviderConfig) (*ProviderConfig, error)
type StreamingProvider ¶
type StreamingProvider interface {
Provider
ChatCompletionStream(ctx context.Context, req *ChatCompletionRequest) (<-chan ChatCompletionStreamEvent, error)
}
type WebSearchProvider ¶
type WebSearchResponse ¶
type WebSearchResponse struct {
Results []WebSearchResult `json:"results,omitempty"`
Summary string `json:"summary,omitempty"`
}