provider

package
v1.0.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProviderOpenAI    = config.ProviderOpenAI
	ProviderAnthropic = config.ProviderAnthropic
)

Variables

View Source
var (
	ErrCallTimeout      = errors.New("provider call timeout")
	ErrStreamStalled    = errors.New("stream stalled")
	ErrStreamIncomplete = errors.New("stream ended before terminal marker")
)

Functions

func CacheHitRatio

func CacheHitRatio(usage *aop.TokenUsage) float64

CacheHitRatio returns the proportion of prompt tokens served from cache.

func InferFromBaseURL

func InferFromBaseURL(baseURL string) string

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 IsImageUnsupportedError(err error) bool

func IsSupportedProvider

func IsSupportedProvider(name string) bool

func MessageReasoning

func MessageReasoning(msg *aop.Message) string

MessageReasoning joins the reasoning parts of an aop message.

func MessageText

func MessageText(msg *aop.Message) string

MessageText joins the text parts of an aop message.

func MessageToolCalls

func MessageToolCalls(msg *aop.Message) []*aop.ToolCall

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 NormalizeProvider(name string) string

func StripImageParts

func StripImageParts(msgs []*aop.Message) []*aop.Message

StripImageParts rewrites media parts into a placeholder note for models without image support.

func TextMessage

func TextMessage(role, content string) *aop.Message

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.

func WithFrameObserver

func WithFrameObserver(ctx context.Context, observer func(RawFrame)) context.Context

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) Error

func (e *APIError) Error() string

func (*APIError) IsRetryable

func (e *APIError) IsRetryable() bool

type AnthropicProvider

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

func NewAnthropicProvider

func NewAnthropicProvider(cfg *ProviderConfig) (*AnthropicProvider, error)

func (*AnthropicProvider) ChatCompletion

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)
	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 Choice

type Choice struct {
	Message      *aop.Message
	FinishReason string
}

type OpenAIProvider

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

func NewOpenAIProvider

func NewOpenAIProvider(cfg *ProviderConfig) (*OpenAIProvider, error)

func (*OpenAIProvider) ChatCompletion

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 RawFrame

type RawFrame struct {
	Provider  string
	Protocol  string
	EventType string
	Direction string
	Transport string
	Payload   []byte
	MediaType string
}

type StreamingProvider

type StreamingProvider interface {
	Provider
	ChatCompletionStream(ctx context.Context, req *ChatCompletionRequest) (<-chan ChatCompletionStreamEvent, error)
}

type WebSearchProvider

type WebSearchProvider interface {
	WebSearch(ctx context.Context, query string, maxResults int) (*WebSearchResponse, error)
}

type WebSearchResponse

type WebSearchResponse struct {
	Results []WebSearchResult `json:"results,omitempty"`
	Summary string            `json:"summary,omitempty"`
}

type WebSearchResult

type WebSearchResult struct {
	Title string `json:"title"`
	URL   string `json:"url"`
}

Jump to

Keyboard shortcuts

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