provider

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCallTimeout   = errors.New("provider call timeout")
	ErrStreamStalled = errors.New("stream stalled")
)

Functions

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 NormalizeProvider

func NormalizeProvider(name string) string

func ParseDataURI

func ParseDataURI(dataURI string) (mediaType, base64Data string)

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 added in v0.3.0

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           `json:"model"`
	Messages       []ChatMessage    `json:"messages"`
	Tools          []ToolDefinition `json:"tools,omitempty"`
	MaxTokens      int              `json:"max_tokens,omitempty"`
	Temperature    *float64         `json:"temperature,omitempty"`
	Stream         bool             `json:"stream,omitempty"`
	ResponseFormat *ResponseFormat  `json:"response_format,omitempty"`
	CacheRetention CacheRetention   `json:"-"`
	SessionID      string           `json:"-"`
}

type ChatCompletionResponse

type ChatCompletionResponse struct {
	ID      string    `json:"id"`
	Choices []Choice  `json:"choices"`
	Usage   *Usage    `json:"usage,omitempty"`
	Error   *APIError `json:"error,omitempty"`
}

type ChatCompletionStreamEvent

type ChatCompletionStreamEvent struct {
	Delta        ChatMessageDelta
	FinishReason string
	Usage        *Usage
	Done         bool
	Err          error
}

type ChatMessage

type ChatMessage struct {
	Role             string        `json:"role"`
	Content          *string       `json:"content,omitempty"`
	ContentParts     []ContentPart `json:"-"`
	ReasoningContent *string       `json:"reasoning_content,omitempty"`
	ToolCalls        []ToolCall    `json:"tool_calls,omitempty"`
	ToolCallID       string        `json:"tool_call_id,omitempty"`
}

func NewMultimodalMessage

func NewMultimodalMessage(role string, parts []ContentPart) ChatMessage

func NewTextMessage

func NewTextMessage(role, content string) ChatMessage

func NewToolResultMessage

func NewToolResultMessage(toolCallID, content string) ChatMessage

func StripImageParts

func StripImageParts(msgs []ChatMessage) []ChatMessage

func (ChatMessage) MarshalJSON

func (m ChatMessage) MarshalJSON() ([]byte, error)

type ChatMessageDelta

type ChatMessageDelta struct {
	Role             string          `json:"role,omitempty"`
	Content          *string         `json:"content,omitempty"`
	ReasoningContent *string         `json:"reasoning_content,omitempty"`
	ToolCalls        []ToolCallDelta `json:"tool_calls,omitempty"`
}

type Choice

type Choice struct {
	Message      ChatMessage `json:"message"`
	FinishReason string      `json:"finish_reason"`
}

type ContentPart

type ContentPart struct {
	Type     string    `json:"type"`
	Text     string    `json:"text,omitempty"`
	ImageURL *ImageURL `json:"image_url,omitempty"`
}

func ImagePart

func ImagePart(mimeType, base64Data, detail string) ContentPart

func TextPart

func TextPart(text string) ContentPart

type FunctionCall

type FunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

type FunctionCallDelta

type FunctionCallDelta struct {
	Name      string `json:"name,omitempty"`
	Arguments string `json:"arguments,omitempty"`
}

type FunctionDefinition

type FunctionDefinition struct {
	Name        string                 `json:"name"`
	Description string                 `json:"description"`
	Parameters  map[string]interface{} `json:"parameters"`
}

type ImageURL

type ImageURL struct {
	URL    string `json:"url"`
	Detail string `json:"detail,omitempty"`
}

type JSONSchemaSpec

type JSONSchemaSpec struct {
	Name   string      `json:"name"`
	Schema interface{} `json:"schema"`
	Strict bool        `json:"strict,omitempty"`
}

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 added in v0.3.0

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"`
}

func Resolve

func Resolve(cfg *ProviderConfig) (*ProviderConfig, error)

type ResponseFormat

type ResponseFormat struct {
	Type       string          `json:"type"`
	JSONSchema *JSONSchemaSpec `json:"json_schema,omitempty"`
}

type StreamingProvider

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

type ToolCall

type ToolCall struct {
	ID       string       `json:"id"`
	Type     string       `json:"type"`
	Function FunctionCall `json:"function"`
}

type ToolCallDelta

type ToolCallDelta struct {
	Index    int               `json:"index,omitempty"`
	ID       string            `json:"id,omitempty"`
	Type     string            `json:"type,omitempty"`
	Function FunctionCallDelta `json:"function,omitempty"`
}

type ToolDefinition

type ToolDefinition struct {
	Type     string             `json:"type"`
	Function FunctionDefinition `json:"function"`
}

type Usage

type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
	CacheReadTokens  int `json:"cache_read_tokens,omitempty"`
	CacheWriteTokens int `json:"cache_write_tokens,omitempty"`
}

func (*Usage) CacheHitRatio

func (u *Usage) CacheHitRatio() float64

CacheHitRatio returns the proportion of prompt tokens served from cache, based on the API response. Returns 0 when no cache data is available.

func (*Usage) UnmarshalJSON

func (u *Usage) UnmarshalJSON(data []byte) 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