provider

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ModelPolicySourceDefault        = "default"
	ModelPolicySourceFamilyOverride = "family"
	ModelPolicySourceProviderID     = "provider_id"
	ModelPolicySourceBaseURL        = "base_url"
)

Variables

View Source
var (
	ErrProviderNotFound  = errors.New(string(ErrCodeProviderNotFound))
	ErrDuplicateProvider = errors.New(string(ErrCodeDuplicateProvider))
)

Functions

func FetchModelContextWindow added in v1.0.1

func FetchModelContextWindow(ctx context.Context, providerType, baseURL, apiKey, modelID string) int

FetchModelContextWindow queries the provider API for a model's context window. Returns 0 if the provider doesn't support querying this info.

func ListModels

func ListModels(ctx context.Context, reg Registry) ([]ModelInfo, []Warning, error)

func LookupModelContextWindow added in v1.0.1

func LookupModelContextWindow(ctx context.Context, providerType, baseURL, apiKey, modelID string) int

LookupModelContextWindow checks the known table first, then tries the provider API.

func NewClient

func NewClient(cfg config.ProviderConfig) (llm.Client, error)

func NewClientFromRuntime

func NewClientFromRuntime(cfg config.ProviderRuntimeConfig, health HealthChecker) (llm.Client, error)

func NewRoutedClient

func NewRoutedClient(router Router) llm.Client

func NewRoutedClientWithPolicy

func NewRoutedClientWithPolicy(router Router, health HealthChecker, allowFallback bool) llm.Client

func NewRouterClient

func NewRouterClient(cfg config.ProviderRuntimeConfig, health HealthChecker) (llm.Client, error)

func WithRouteContext

func WithRouteContext(ctx context.Context, rc RouteContext) context.Context

Types

type Anthropic

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

func NewAnthropic

func NewAnthropic(cfg Config) *Anthropic

func (*Anthropic) CreateMessage

func (c *Anthropic) CreateMessage(ctx context.Context, req llm.ChatRequest) (llm.Message, error)

func (*Anthropic) StreamMessage

func (c *Anthropic) StreamMessage(ctx context.Context, req llm.ChatRequest, onDelta func(string)) (llm.Message, error)

type AssistantToolCallContentMode

type AssistantToolCallContentMode string
const (
	AssistantToolCallContentPreserve    AssistantToolCallContentMode = "preserve"
	AssistantToolCallContentEmptyString AssistantToolCallContentMode = "empty_string"
	AssistantToolCallContentNull        AssistantToolCallContentMode = "null"
	AssistantToolCallContentOmit        AssistantToolCallContentMode = "omit"
)

type Availability

type Availability struct {
	Ready  bool
	Reason string
	Detail string
}

func CheckAvailability

func CheckAvailability(ctx context.Context, cfg config.ProviderConfig) Availability

type Client

type Client interface {
	ProviderID() ProviderID
	ListModels(ctx context.Context) ([]ModelInfo, error)
	Stream(ctx context.Context, req Request) (<-chan Event, error)
}

func NewDomainClient

func NewDomainClient(cfg config.ProviderConfig) (Client, error)

func NewDomainClientWithID

func NewDomainClientWithID(providerID ProviderID, cfg config.ProviderConfig) (Client, error)

func WrapClient

func WrapClient(providerID ProviderID, defaultModel ModelID, client llm.Client) Client

type Config

type Config struct {
	ProviderID       ProviderID
	ProviderFamily   string
	Type             string
	BaseURL          string
	APIPath          string
	APIKey           string
	AuthHeader       string
	AuthScheme       string
	ExtraHeaders     map[string]string
	Model            string
	AnthropicVersion string
}

type Error

type Error struct {
	Code      ErrorCode
	Provider  ProviderID
	Message   string
	Retryable bool
	Err       error
	Detail    string
}

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type ErrorCode

type ErrorCode string
const (
	ErrCodeUnauthorized      ErrorCode = "unauthorized"
	ErrCodeRateLimited       ErrorCode = "rate_limited"
	ErrCodeTimeout           ErrorCode = "timeout"
	ErrCodeUnavailable       ErrorCode = "unavailable"
	ErrCodeBadRequest        ErrorCode = "bad_request"
	ErrCodeProviderNotFound  ErrorCode = "provider_not_found"
	ErrCodeDuplicateProvider ErrorCode = "duplicate_provider"
)

type Event

type Event struct {
	ID         string
	TraceID    string
	ProviderID ProviderID
	ModelID    ModelID
	Type       EventType
	Delta      string
	ToolCall   *llm.ToolCall
	Usage      *Usage
	Result     *llm.Message
	Error      *Error
}

type EventType

type EventType string
const (
	EventStart    EventType = "start"
	EventDelta    EventType = "delta"
	EventToolCall EventType = "tool_call"
	EventUsage    EventType = "usage"
	EventResult   EventType = "result"
	EventError    EventType = "error"
)

type ExternalHealthTicker

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

func NewExternalHealthTicker

func NewExternalHealthTicker(health HealthChecker, ids func(context.Context) ([]ProviderID, error)) *ExternalHealthTicker

func (*ExternalHealthTicker) Tick

type Gemini

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

func NewGemini

func NewGemini(cfg Config) *Gemini

func (*Gemini) CreateMessage

func (c *Gemini) CreateMessage(ctx context.Context, req llm.ChatRequest) (llm.Message, error)

func (*Gemini) StreamMessage

func (c *Gemini) StreamMessage(ctx context.Context, req llm.ChatRequest, onDelta func(string)) (llm.Message, error)

type HealthChecker

type HealthChecker interface {
	Check(ctx context.Context, id ProviderID) error
	Status(ctx context.Context, id ProviderID) HealthSnapshot
	RecordSuccess(ctx context.Context, id ProviderID)
	RecordFailure(ctx context.Context, id ProviderID, err error)
}

func NewHealthChecker

func NewHealthChecker(cfg HealthConfig, checker func(context.Context, ProviderID) error) HealthChecker

type HealthConfig

type HealthConfig struct {
	FailThreshold           int
	RecoverProbeSec         int
	RecoverSuccessThreshold int
	WindowSize              int
}

type HealthSnapshot

type HealthSnapshot struct {
	ProviderID       ProviderID
	Status           HealthStatus
	FailureCount     int
	SuccessCount     int
	WindowSize       int
	NextProbeAt      time.Time
	LastCheckAt      time.Time
	LastFailureAt    time.Time
	LastSuccessAt    time.Time
	LastErrorMessage string
}

type HealthStatus

type HealthStatus string
const (
	HealthStatusHealthy     HealthStatus = "healthy"
	HealthStatusDegraded    HealthStatus = "degraded"
	HealthStatusUnavailable HealthStatus = "unavailable"
	HealthStatusHalfOpen    HealthStatus = "half_open"
)

type ModelID

type ModelID string

type ModelInfo

type ModelInfo struct {
	ProviderID   ProviderID
	ModelID      ModelID
	DisplayAlias string
	Metadata     map[string]string
}

func (ModelInfo) ModelMetadata added in v0.1.8

func (m ModelInfo) ModelMetadata() ModelMetadata

type ModelMetadata added in v0.1.8

type ModelMetadata struct {
	ProviderID      ProviderID
	ModelID         ModelID
	Family          string
	ContextWindow   int
	MaxOutputTokens int
	SupportsTools   bool
	UsageSource     string
	Metadata        map[string]string
}

type ModelRegistry added in v0.1.8

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

func NewModelRegistry added in v0.1.8

func NewModelRegistry(runtimeCfg config.ProviderRuntimeConfig, discovered []ModelInfo) ModelRegistry

func (ModelRegistry) ContextWindow added in v0.1.8

func (r ModelRegistry) ContextWindow(providerID ProviderID, modelID ModelID) int

func (ModelRegistry) Lookup added in v0.1.8

func (r ModelRegistry) Lookup(providerID ProviderID, modelID ModelID) (ModelInfo, bool)

func (ModelRegistry) Models added in v0.1.8

func (r ModelRegistry) Models() []ModelInfo

type OpenAICompatible

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

func NewOpenAICompatible

func NewOpenAICompatible(cfg Config) *OpenAICompatible

func (*OpenAICompatible) CreateMessage

func (c *OpenAICompatible) CreateMessage(ctx context.Context, req llm.ChatRequest) (llm.Message, error)

func (*OpenAICompatible) StreamMessage

func (c *OpenAICompatible) StreamMessage(ctx context.Context, req llm.ChatRequest, onDelta func(string)) (llm.Message, error)

type ProviderID

type ProviderID string
const (
	ProviderOpenAI    ProviderID = "openai"
	ProviderAnthropic ProviderID = "anthropic"
	ProviderGemini    ProviderID = "gemini"
	ProviderMock      ProviderID = "mock"
)

type ReasoningReplayMode

type ReasoningReplayMode string
const (
	ReasoningReplayNone        ReasoningReplayMode = "none"
	ReasoningReplayHiddenField ReasoningReplayMode = "hidden_field"
)

type Registry

type Registry interface {
	Register(ctx context.Context, client Client) error
	Get(ctx context.Context, id ProviderID) (Client, bool)
	List(ctx context.Context) ([]ProviderID, error)
}

func NewRegistry

func NewRegistry(cfg config.ProviderRuntimeConfig) (Registry, error)

func NewRegistryFromProviderConfig

func NewRegistryFromProviderConfig(cfg config.ProviderConfig) (Registry, error)

type Request

type Request struct {
	llm.ChatRequest
	TraceID string
	Tags    map[string]string
}

type ResolvedModelPolicy

type ResolvedModelPolicy struct {
	ProviderID                   ProviderID
	ProviderType                 string
	ModelID                      ModelID
	Family                       string
	AssistantToolCallContentMode AssistantToolCallContentMode
	ReasoningField               string
	ReasoningReplayMode          ReasoningReplayMode
	Source                       string
}

func ResolveModelPolicy

func ResolveModelPolicy(providerID ProviderID, providerType string, modelID ModelID, family string, baseURL string) ResolvedModelPolicy

func (ResolvedModelPolicy) ReplayReasoning

func (p ResolvedModelPolicy) ReplayReasoning() bool

type RouteContext

type RouteContext struct {
	Scenario      string
	Region        string
	PreferLatency bool
	PreferLowCost bool
	AllowFallback bool
	Tags          map[string]string
}

func RouteContextFromContext

func RouteContextFromContext(ctx context.Context) RouteContext

type RouteResult

type RouteResult struct {
	Primary   RouteTarget
	Fallbacks []RouteTarget
}

type RouteTarget

type RouteTarget struct {
	ProviderID ProviderID
	ModelID    ModelID
	Client     Client
}

type RoutedClient

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

func (*RoutedClient) CreateMessage

func (c *RoutedClient) CreateMessage(ctx context.Context, req llm.ChatRequest) (llm.Message, error)

func (*RoutedClient) StreamMessage

func (c *RoutedClient) StreamMessage(ctx context.Context, req llm.ChatRequest, onDelta func(string)) (llm.Message, error)

type Router

type Router interface {
	Route(ctx context.Context, requestedModel ModelID, rc RouteContext) (RouteResult, error)
}

func NewRouter

func NewRouter(reg Registry, health HealthChecker, cfg RouterConfig) Router

type RouterConfig

type RouterConfig struct {
	DefaultProvider ProviderID
	DefaultModel    ModelID
}

type Usage

type Usage struct {
	InputTokens  int64
	OutputTokens int64
	TotalTokens  int64
	Cost         float64
	Currency     string
	IsEstimated  bool
}

type Warning

type Warning struct {
	ProviderID ProviderID
	Reason     string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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