provider

package
v5.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UsageSourceMissing   = "missing"
	UsageSourceExact     = "exact"
	UsageSourceEstimated = "estimated"
)

Variables

View Source
var ErrNoProvidersAvailable = errors.New("no configured model providers available")

Functions

func BuildConversationInput

func BuildConversationInput(parseHistory []ChatMessage, parseUserMessage string) string

BuildConversationInput builds the provider-facing conversation input.

func ParseNormalizeRole

func ParseNormalizeRole(parseRole string) string

ParseNormalizeRole normalizes one chat role for provider payloads.

Types

type AnthropicProvider

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

func ParseNewAnthropicProvider

func ParseNewAnthropicProvider(parseApiKey string, parseCatalog Catalog) *AnthropicProvider

ParseNewAnthropicProvider creates an Anthropic provider wired to the supplied catalog and API key.

func (*AnthropicProvider) ParseAvailable

func (parseP *AnthropicProvider) ParseAvailable() bool

ParseAvailable reports whether the provider is available.

func (*AnthropicProvider) ParseCapabilities

func (parseP *AnthropicProvider) ParseCapabilities(parseModel string) ModelCapabilities

ParseCapabilities returns the capability snapshot.

func (*AnthropicProvider) ParseCurrentRateLimits

func (parseP *AnthropicProvider) ParseCurrentRateLimits() RateLimitSnapshot

ParseCurrentRateLimits returns the current rate-limit snapshot.

func (*AnthropicProvider) ParseDefaultModel

func (parseP *AnthropicProvider) ParseDefaultModel() string

ParseDefaultModel returns the default model for the provider.

func (*AnthropicProvider) ParseExtractUserMemories

func (parseP *AnthropicProvider) ParseExtractUserMemories(parseCtx context.Context, parseReq MemoryExtractionRequest) ([]UserMemoryCandidate, error)

ParseExtractUserMemories extracts user-memory candidates from the current conversation.

func (*AnthropicProvider) ParseGenerateTitle

func (parseP *AnthropicProvider) ParseGenerateTitle(parseCtx context.Context, parseReq TitleRequest) (string, error)

ParseGenerateTitle generates one conversation title.

func (*AnthropicProvider) ParseHealth

func (parseP *AnthropicProvider) ParseHealth() ProviderHealth

ParseHealth returns the health snapshot.

func (*AnthropicProvider) ParseID

func (parseP *AnthropicProvider) ParseID() string

ParseID returns the provider identifier.

func (*AnthropicProvider) ParseInfo

func (parseP *AnthropicProvider) ParseInfo() ProviderInfo

ParseInfo returns the provider info snapshot.

func (*AnthropicProvider) ParseModelMetadata

func (parseP *AnthropicProvider) ParseModelMetadata(parseModel string) (ModelMetadata, bool)

ParseModelMetadata returns the provider model metadata.

func (*AnthropicProvider) ParseModelOptions

func (parseP *AnthropicProvider) ParseModelOptions() []ModelOption

ParseModelOptions returns the provider model options.

func (*AnthropicProvider) ParseStreamChat

func (parseP *AnthropicProvider) ParseStreamChat(parseCtx context.Context, parseReq ChatRequest, parseEmit func(ChatEvent) error) (ChatResult, error)

ParseStreamChat streams one chat completion.

func (*AnthropicProvider) ParseSupportsModel

func (parseP *AnthropicProvider) ParseSupportsModel(parseModel string) bool

ParseSupportsModel reports whether the provider supports the requested model.

func (*AnthropicProvider) ParseSynthesizeSpeech

func (parseP *AnthropicProvider) ParseSynthesizeSpeech(parseCtx context.Context, parseReq SpeechRequest, parseEmit func(SpeechChunk) error) (SpeechResult, error)

ParseSynthesizeSpeech streams one speech-synthesis response.

type Capability

type Capability string
const (
	CapabilityThinking Capability = "thinking"
	CapabilitySpeech   Capability = "speech"
)

type Catalog

type Catalog struct {
	Models       []ModelMetadata
	Options      []ModelOption
	DefaultModel string
	TitleModel   string
}

func (Catalog) ParseModelMetadata

func (parseC Catalog) ParseModelMetadata(parseModel string) (ModelMetadata, bool)

ParseModelMetadata returns the catalog metadata for one model.

func (Catalog) ParseModelOptions

func (parseC Catalog) ParseModelOptions() []ModelOption

ParseModelOptions returns the catalog model picker options.

func (Catalog) ParseSupportsModel

func (parseC Catalog) ParseSupportsModel(parseModel string) bool

ParseSupportsModel reports whether the catalog supports the requested model.

type CerebrasProvider

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

func ParseNewCerebrasProvider

func ParseNewCerebrasProvider(parseApiKey string, parseCatalog Catalog) *CerebrasProvider

ParseNewCerebrasProvider creates a Cerebras provider wired to the supplied catalog and API key.

func (*CerebrasProvider) ParseAvailable

func (parseP *CerebrasProvider) ParseAvailable() bool

ParseAvailable reports whether the provider is available.

func (*CerebrasProvider) ParseCapabilities

func (parseP *CerebrasProvider) ParseCapabilities(parseModel string) ModelCapabilities

ParseCapabilities returns the capability snapshot.

func (*CerebrasProvider) ParseCurrentRateLimits

func (parseP *CerebrasProvider) ParseCurrentRateLimits() RateLimitSnapshot

ParseCurrentRateLimits returns the current rate-limit snapshot.

func (*CerebrasProvider) ParseDefaultModel

func (parseP *CerebrasProvider) ParseDefaultModel() string

ParseDefaultModel returns the default model for the provider.

func (*CerebrasProvider) ParseExtractUserMemories

func (parseP *CerebrasProvider) ParseExtractUserMemories(parseCtx context.Context, parseReq MemoryExtractionRequest) ([]UserMemoryCandidate, error)

ParseExtractUserMemories extracts user-memory candidates from the current conversation.

func (*CerebrasProvider) ParseGenerateTitle

func (parseP *CerebrasProvider) ParseGenerateTitle(parseCtx context.Context, parseReq TitleRequest) (string, error)

ParseGenerateTitle generates one conversation title.

func (*CerebrasProvider) ParseHealth

func (parseP *CerebrasProvider) ParseHealth() ProviderHealth

ParseHealth returns the health snapshot.

func (*CerebrasProvider) ParseID

func (parseP *CerebrasProvider) ParseID() string

ParseID returns the provider identifier.

func (*CerebrasProvider) ParseInfo

func (parseP *CerebrasProvider) ParseInfo() ProviderInfo

ParseInfo returns the provider info snapshot.

func (*CerebrasProvider) ParseModelMetadata

func (parseP *CerebrasProvider) ParseModelMetadata(parseModel string) (ModelMetadata, bool)

ParseModelMetadata returns the provider model metadata.

func (*CerebrasProvider) ParseModelOptions

func (parseP *CerebrasProvider) ParseModelOptions() []ModelOption

ParseModelOptions returns the provider model options.

func (*CerebrasProvider) ParseStreamChat

func (parseP *CerebrasProvider) ParseStreamChat(parseCtx context.Context, parseReq ChatRequest, parseEmit func(ChatEvent) error) (ChatResult, error)

ParseStreamChat streams one chat completion.

func (*CerebrasProvider) ParseSupportsModel

func (parseP *CerebrasProvider) ParseSupportsModel(parseModel string) bool

ParseSupportsModel reports whether the provider supports the requested model.

func (*CerebrasProvider) ParseSynthesizeSpeech

func (parseP *CerebrasProvider) ParseSynthesizeSpeech(parseCtx context.Context, parseReq SpeechRequest, parseEmit func(SpeechChunk) error) (SpeechResult, error)

ParseSynthesizeSpeech streams one speech-synthesis response.

type ChatEvent

type ChatEvent struct {
	TextDelta    string
	ThoughtDelta string
	ThoughtDone  bool
}

type ChatMessage

type ChatMessage struct {
	Role    string
	Content string
}

type ChatProvider

type ChatProvider interface {
	ParseID() string
	ParseAvailable() bool
	ParseInfo() ProviderInfo
	ParseDefaultModel() string
	ParseSupportsModel(model string) bool
	ParseModelOptions() []ModelOption
	ParseModelMetadata(model string) (ModelMetadata, bool)
	ParseCapabilities(model string) ModelCapabilities
	ParseHealth() ProviderHealth
	ParseCurrentRateLimits() RateLimitSnapshot
	ParseStreamChat(ctx context.Context, req ChatRequest, emit func(ChatEvent) error) (ChatResult, error)
	ParseGenerateTitle(ctx context.Context, req TitleRequest) (string, error)
	ParseExtractUserMemories(ctx context.Context, req MemoryExtractionRequest) ([]UserMemoryCandidate, error)
	ParseSynthesizeSpeech(ctx context.Context, req SpeechRequest, emit func(SpeechChunk) error) (SpeechResult, error)
}

type ChatRequest

type ChatRequest struct {
	Model           string
	SystemPrompt    string
	History         []ChatMessage
	UserMessage     string
	ThinkingEnabled bool
	ThinkingEffort  string
}

type ChatResult

type ChatResult struct {
	Model             string
	PromptTokens      int64
	CompletionTokens  int64
	UsageSource       string
	ProviderRequestID string
}

type CostEstimate

type CostEstimate struct {
	InputCostUSD  float64
	OutputCostUSD float64
	TotalCostUSD  float64
}

CostEstimate is the reusable provider-agnostic result of one pricing calculation.

func ParseEstimateCost

func ParseEstimateCost(parsePromptTokens, parseCompletionTokens int64, parsePricing ModelPricing) CostEstimate

ParseEstimateCost calculates approximate request cost from token counts and model pricing metadata.

type ErrorKind

type ErrorKind string
const (
	ErrorKindAuthConfig ErrorKind = "auth_config"
	ErrorKindRateLimit  ErrorKind = "rate_limit"
	ErrorKindInvalid    ErrorKind = "invalid_request"
	ErrorKindUpstream   ErrorKind = "upstream_failure"
	ErrorKindTimeout    ErrorKind = "timeout"
	ErrorKindStream     ErrorKind = "stream_failure"
	ErrorKindUnknown    ErrorKind = "unknown"
)

type MemoryExtractionRequest

type MemoryExtractionRequest struct {
	Model       string
	UserMessage string
}

type ModelCapabilities

type ModelCapabilities struct {
	ProviderID       string
	ProviderLabel    string
	SupportsThinking bool
	SupportsSpeech   bool
}

func (ModelCapabilities) ParseSupports

func (parseC ModelCapabilities) ParseSupports(parseCapability Capability) bool

ParseSupports reports whether the capability set includes the requested feature.

type ModelMetadata

type ModelMetadata struct {
	ID                        string
	DisplayName               string
	Description               string
	ProviderID                string
	ProviderLabel             string
	ProviderFamily            string
	Capabilities              ModelCapabilities
	StreamingSupported        bool
	ReasoningSupported        bool
	ToolUseSupported          bool
	StructuredOutputSupport   string
	MaxContextTokens          int64
	MaxOutputTokens           int64
	ThroughputTokensPerSecond float64
	OnboardingReady           bool
	CompatibilityNotes        []string
	Pricing                   ModelPricing
}

ModelMetadata is the richer catalog entry for one provider-backed model.

ModelOption remains the compact picker shape used by the current client. ModelMetadata adds pricing, limits, compatibility notes, and onboarding status so providers can plug into future dashboard/config surfaces cleanly.

type ModelOption

type ModelOption struct {
	ID           string
	Label        string
	Note         string
	Capabilities ModelCapabilities
	Pricing      ModelPricing
}

func ParseModelOptionFromMetadata

func ParseModelOptionFromMetadata(parseMetadata ModelMetadata, parseNote string) ModelOption

ParseModelOptionFromMetadata collapses one rich catalog entry into the compact UI picker shape.

type ModelPricing

type ModelPricing struct {
	InputPerMillionUSD  float64
	OutputPerMillionUSD float64
	Currency            string
}

ModelPricing stores token pricing as USD per 1M tokens.

type NormalizedError

type NormalizedError struct {
	Kind       ErrorKind
	ProviderID string
	Model      string
	Message    string
	Retryable  bool
	StatusCode int
	Err        error
}

NormalizedError is the provider-agnostic error envelope for higher layers.

func (*NormalizedError) Error

func (parseE *NormalizedError) Error() string

Error returns the provider error string.

func (*NormalizedError) ParseError

func (parseE *NormalizedError) ParseError() string

ParseError returns the normalized error value.

func (*NormalizedError) ParseUnwrap

func (parseE *NormalizedError) ParseUnwrap() error

ParseUnwrap returns the wrapped error.

func (*NormalizedError) Unwrap

func (parseE *NormalizedError) Unwrap() error

Unwrap returns the wrapped error.

type OpenAIProvider

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

func ParseNewOpenAIProvider

func ParseNewOpenAIProvider(parseApiKey string, parseCatalog Catalog) *OpenAIProvider

ParseNewOpenAIProvider creates an OpenAI provider wired to the supplied catalog and API key.

func (*OpenAIProvider) ParseAvailable

func (parseP *OpenAIProvider) ParseAvailable() bool

ParseAvailable reports whether the provider is available.

func (*OpenAIProvider) ParseCapabilities

func (parseP *OpenAIProvider) ParseCapabilities(parseModel string) ModelCapabilities

ParseCapabilities returns the capability snapshot.

func (*OpenAIProvider) ParseCurrentRateLimits

func (parseP *OpenAIProvider) ParseCurrentRateLimits() RateLimitSnapshot

ParseCurrentRateLimits returns the current rate-limit snapshot.

func (*OpenAIProvider) ParseDefaultModel

func (parseP *OpenAIProvider) ParseDefaultModel() string

ParseDefaultModel returns the default model for the provider.

func (*OpenAIProvider) ParseExtractUserMemories

func (parseP *OpenAIProvider) ParseExtractUserMemories(parseCtx context.Context, parseReq MemoryExtractionRequest) ([]UserMemoryCandidate, error)

ParseExtractUserMemories extracts user-memory candidates from the current conversation.

func (*OpenAIProvider) ParseGenerateTitle

func (parseP *OpenAIProvider) ParseGenerateTitle(parseCtx context.Context, parseReq TitleRequest) (string, error)

ParseGenerateTitle generates one conversation title.

func (*OpenAIProvider) ParseHealth

func (parseP *OpenAIProvider) ParseHealth() ProviderHealth

ParseHealth returns the health snapshot.

func (*OpenAIProvider) ParseID

func (parseP *OpenAIProvider) ParseID() string

ParseID returns the provider identifier.

func (*OpenAIProvider) ParseInfo

func (parseP *OpenAIProvider) ParseInfo() ProviderInfo

ParseInfo returns the provider info snapshot.

func (*OpenAIProvider) ParseModelMetadata

func (parseP *OpenAIProvider) ParseModelMetadata(parseModel string) (ModelMetadata, bool)

ParseModelMetadata returns the provider model metadata.

func (*OpenAIProvider) ParseModelOptions

func (parseP *OpenAIProvider) ParseModelOptions() []ModelOption

ParseModelOptions returns the provider model options.

func (*OpenAIProvider) ParseStreamChat

func (parseP *OpenAIProvider) ParseStreamChat(parseCtx context.Context, parseReq ChatRequest, parseEmit func(ChatEvent) error) (ChatResult, error)

ParseStreamChat streams one chat completion.

func (*OpenAIProvider) ParseSupportsModel

func (parseP *OpenAIProvider) ParseSupportsModel(parseModel string) bool

ParseSupportsModel reports whether the provider supports the requested model.

func (*OpenAIProvider) ParseSynthesizeSpeech

func (parseP *OpenAIProvider) ParseSynthesizeSpeech(parseCtx context.Context, parseReq SpeechRequest, parseEmit func(SpeechChunk) error) (SpeechResult, error)

ParseSynthesizeSpeech streams one speech-synthesis response.

type ProviderHealth

type ProviderHealth struct {
	ProviderID   string
	Status       ProviderHealthStatus
	LastSuccess  time.Time
	LastFailure  time.Time
	LastError    string
	LastLatency  time.Duration
	RequestCount int64
	TokenCount   int64
	RateLimits   RateLimitSnapshot
}

ProviderHealth is the normalized runtime/operational snapshot for one provider.

type ProviderHealthStatus

type ProviderHealthStatus string
const (
	ProviderHealthUnknown     ProviderHealthStatus = "unknown"
	ProviderHealthHealthy     ProviderHealthStatus = "healthy"
	ProviderHealthDegraded    ProviderHealthStatus = "degraded"
	ProviderHealthUnavailable ProviderHealthStatus = "unavailable"
)

type ProviderInfo

type ProviderInfo struct {
	ID                 string
	Label              string
	BaseURL            string
	AuthConfigured     bool
	Available          bool
	StreamingSupported bool
	ReasoningSupported bool
	ToolUseSupported   bool
	Notes              []string
}

ProviderInfo describes one configured provider in a provider-agnostic way.

This is the static/operator-facing view used by config, onboarding, and dashboard surfaces. Runtime health and rate-limit state are kept separate.

type RateLimitBucket

type RateLimitBucket struct {
	Limit      int64
	Remaining  int64
	ResetAfter time.Duration
}

RateLimitBucket describes one quota window such as requests/day or tokens/minute.

type RateLimitSnapshot

type RateLimitSnapshot struct {
	RequestsPerMinute RateLimitBucket
	RequestsPerHour   RateLimitBucket
	RequestsPerDay    RateLimitBucket
	TokensPerMinute   RateLimitBucket
	TokensPerHour     RateLimitBucket
	TokensPerDay      RateLimitBucket
	LastUpdated       time.Time
	Source            string
}

RateLimitSnapshot is the normalized provider quota state that dashboards and preflight request checks can consume.

func (RateLimitSnapshot) ParseEmpty

func (parseSnapshot RateLimitSnapshot) ParseEmpty() bool

ParseEmpty returns the exported helper result.

type Registry

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

func ParseNewRegistry

func ParseNewRegistry(parseProviders ...ChatProvider) *Registry

ParseNewRegistry creates one provider registry from the supplied providers.

func (*Registry) ParseCapabilities

func (parseR *Registry) ParseCapabilities(parseModel string) (ModelCapabilities, string, error)

ParseCapabilities returns the capability snapshot.

func (*Registry) ParseDefaultModel

func (parseR *Registry) ParseDefaultModel() string

ParseDefaultModel returns the default model for the provider.

func (*Registry) ParseHealthSnapshots

func (parseR *Registry) ParseHealthSnapshots() []ProviderHealth

ParseHealthSnapshots returns the health snapshots.

func (*Registry) ParseModelMetadata

func (parseR *Registry) ParseModelMetadata(parseModel string) (ModelMetadata, string, error)

ParseModelMetadata returns the provider model metadata.

func (*Registry) ParseModelOptions

func (parseR *Registry) ParseModelOptions() []ModelOption

ParseModelOptions returns the provider model options.

func (*Registry) ParsePricing

func (parseR *Registry) ParsePricing(parseModel string) (ModelPricing, string, error)

ParsePricing returns the pricing snapshot.

func (*Registry) ParseProviderInfos

func (parseR *Registry) ParseProviderInfos() []ProviderInfo

ParseProviderInfos returns the provider info list.

func (*Registry) ParseRequireCapability

func (parseR *Registry) ParseRequireCapability(parseModel string, parseCapability Capability) (ChatProvider, string, ModelCapabilities, error)

ParseRequireCapability returns an error unless the requested capability is present.

func (*Registry) ParseResolve

func (parseR *Registry) ParseResolve(parseModel string) (ChatProvider, string, error)

ParseResolve resolves one provider from the registry by ID.

type SpeechChunk

type SpeechChunk struct {
	AudioChunk []byte
	Done       bool
	MimeType   string
	Model      string
	Voice      string
	Script     string
}

type SpeechRequest

type SpeechRequest struct {
	Model string
	Text  string
}

type SpeechResult

type SpeechResult struct {
	MimeType string
	Model    string
	Voice    string
	Script   string
}

type StubProvider

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

func ParseNewStubProvider

func ParseNewStubProvider(parseProviderID string, parseCatalog Catalog) *StubProvider

ParseNewStubProvider creates one stub provider for local testing.

func (*StubProvider) ParseAvailable

func (parseP *StubProvider) ParseAvailable() bool

ParseAvailable reports whether the provider is available.

func (*StubProvider) ParseCapabilities

func (parseP *StubProvider) ParseCapabilities(parseModel string) ModelCapabilities

ParseCapabilities returns the capability snapshot.

func (*StubProvider) ParseCurrentRateLimits

func (parseP *StubProvider) ParseCurrentRateLimits() RateLimitSnapshot

ParseCurrentRateLimits returns the current rate-limit snapshot.

func (*StubProvider) ParseDefaultModel

func (parseP *StubProvider) ParseDefaultModel() string

ParseDefaultModel returns the default model for the provider.

func (*StubProvider) ParseExtractUserMemories

func (parseP *StubProvider) ParseExtractUserMemories(_ context.Context, _ MemoryExtractionRequest) ([]UserMemoryCandidate, error)

ParseExtractUserMemories extracts user-memory candidates from the current conversation.

func (*StubProvider) ParseGenerateTitle

func (parseP *StubProvider) ParseGenerateTitle(_ context.Context, parseReq TitleRequest) (string, error)

ParseGenerateTitle generates one conversation title.

func (*StubProvider) ParseHealth

func (parseP *StubProvider) ParseHealth() ProviderHealth

ParseHealth returns the health snapshot.

func (*StubProvider) ParseID

func (parseP *StubProvider) ParseID() string

ParseID returns the provider identifier.

func (*StubProvider) ParseInfo

func (parseP *StubProvider) ParseInfo() ProviderInfo

ParseInfo returns the provider info snapshot.

func (*StubProvider) ParseModelMetadata

func (parseP *StubProvider) ParseModelMetadata(parseModel string) (ModelMetadata, bool)

ParseModelMetadata returns the provider model metadata.

func (*StubProvider) ParseModelOptions

func (parseP *StubProvider) ParseModelOptions() []ModelOption

ParseModelOptions returns the provider model options.

func (*StubProvider) ParseStreamChat

func (parseP *StubProvider) ParseStreamChat(parseCtx context.Context, parseReq ChatRequest, parseEmit func(ChatEvent) error) (ChatResult, error)

ParseStreamChat streams one chat completion. The reply is emitted word-by-word with a small pacing delay (see parseStubChunkDelay) so local development exercises the same incremental-render path as a real provider.

func (*StubProvider) ParseSupportsModel

func (parseP *StubProvider) ParseSupportsModel(parseModel string) bool

ParseSupportsModel reports whether the provider supports the requested model.

func (*StubProvider) ParseSynthesizeSpeech

func (parseP *StubProvider) ParseSynthesizeSpeech(_ context.Context, parseReq SpeechRequest, parseEmit func(SpeechChunk) error) (SpeechResult, error)

ParseSynthesizeSpeech streams one speech-synthesis response.

type TitleRequest

type TitleRequest struct {
	Model        string
	SystemPrompt string
	Prompt       string
}

type UnsupportedCapabilityError

type UnsupportedCapabilityError struct {
	Capability Capability
	Model      string
	ProviderID string
}

func (*UnsupportedCapabilityError) Error

func (parseE *UnsupportedCapabilityError) Error() string

Error returns the provider error string.

func (*UnsupportedCapabilityError) ParseError

func (parseE *UnsupportedCapabilityError) ParseError() string

ParseError returns the normalized error value.

type UserMemoryCandidate

type UserMemoryCandidate struct {
	Key             string  `json:"key"`
	Category        string  `json:"category"`
	Summary         string  `json:"summary"`
	Detail          string  `json:"detail"`
	UsefulnessScore int     `json:"usefulness_score"`
	ConfidenceScore float64 `json:"confidence_score"`
	RubricReason    string  `json:"rubric_reason"`
}

Jump to

Keyboard shortcuts

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