Documentation
¶
Overview ¶
Package sdk defines the Adapter interface and registry for llm-router provider adapters.
To add a new provider type:
- Implement the Adapter interface in a new package.
- Call sdk.Register(yourAdapter{}) from an init() function.
- That's it — the router will use it automatically.
Package sdk defines error types for llm-router provider adapters.
Package sdk defines shared types for llm-router provider adapters.
Index ¶
- Variables
- func GetAllDefaultProviders() map[string][]ProviderInfo
- func Register(a Adapter)
- func Registered() []string
- type Adapter
- type AuthFlowContext
- type AuthFlowHandler
- type AuthFlowState
- type AuthStore
- type AuthType
- type ChatCompletionChoice
- type ChatCompletionRequest
- type ChatCompletionResponse
- type ChatCompletionUsage
- type ChatMessage
- type ChatMessageContentPart
- type ChatTool
- type ChatToolCall
- type ChatToolFunction
- type Credential
- type ErrorType
- type ModelId
- type ModelInfo
- type ProviderError
- type ProviderInfo
- type StreamChunk
- type StreamChunkChoice
Constants ¶
This section is empty.
Variables ¶
var ErrNoRefreshNeeded = fmt.Errorf("no refresh needed for this credential type")
ErrNoRefreshNeeded is returned by RefreshCredential for credential types that do not expire (e.g. static API keys).
Functions ¶
func GetAllDefaultProviders ¶
func GetAllDefaultProviders() map[string][]ProviderInfo
GetAllDefaultProviders collects default providers from all registered adapters.
Types ¶
type Adapter ¶
type Adapter interface {
// TypeKey returns the unique string identifier for this provider type.
// This value is stored in Provider.Type and used for adapter lookup.
// Examples: "openai", "anthropic", "ollama"
TypeKey() string
// AuthType declares what kind of credentials this provider uses.
AuthType() AuthType
// ValidateCredentials validates and normalises the raw credential data
// submitted through the dashboard and returns a ready-to-store Credential.
ValidateCredentials(data map[string]string) error
// Complete sends a non-streaming chat completion request.
Complete(ctx context.Context, cred *Credential, req *ChatCompletionRequest) (*ChatCompletionResponse, error)
// CompleteStream sends a streaming chat completion request, writing
// Server-Sent Events to w.
CompleteStream(ctx context.Context, cred *Credential, req *ChatCompletionRequest, w io.Writer) error
// NeedsRefresh returns true if the credential should be proactively
// refreshed before it expires (e.g. within the next 5 minutes).
NeedsRefresh(cred *Credential) bool
// RefreshCredential obtains new auth data using the existing credential
// (e.g. using a refresh_token for OAuth 2.0 flows).
// For static API keys, this should return ErrNoRefreshNeeded.
RefreshCredential(ctx context.Context, cred *Credential) (*Credential, error)
// GetModelInfos retrieves metadata for all available models from the provider.
// Called by the modelinfo service when cache is expired or missing.
//
// Parameters:
// - ctx: Context for timeout/cancellation
// - cred: Credential to use for API authentication
// - providerQualifier: Provider qualifier (e.g., "", "azure")
//
// Returns:
// - Array of ModelInfo with rate limits and capabilities
// - Error if fetching fails (network, auth, parsing, etc.)
//
// Implementation notes:
// - Should fetch from provider API when possible
// - Can return hardcoded values if API doesn't provide metadata
// - Should respect ctx timeout/cancellation
// - Return empty array (not error) if provider has no models
// - Framework will cache results with TTL
GetModelInfos(ctx context.Context, cred *Credential, providerQualifier string) ([]ModelInfo, error)
// GetAuthFlow returns an optional handler for automated provider-specific auth.
GetAuthFlow() AuthFlowHandler
// GetDefaultProviders returns a list of default providers this adapter supports.
// These providers will be automatically created during bootstrap.
// Return an empty slice if this adapter should not have default providers.
GetDefaultProviders() []ProviderInfo
}
Adapter is implemented by every provider type (e.g. OpenAI, Anthropic, Ollama). Each Adapter is stateless; per-provider state lives in Credential records.
type AuthFlowContext ¶
type AuthFlowContext struct {
ProviderID string
FlowID string
Store AuthStore // For adapter to persist its own state between steps
}
AuthFlowContext provides framework services to the adapter
type AuthFlowHandler ¶
type AuthFlowHandler interface {
// InitiateFlow starts the auth flow and returns the initial state.
InitiateFlow(ctx AuthFlowContext) (AuthFlowState, error)
// HandleStep processes user input and returns the next state.
// Called on every form submission, callback, or interaction.
HandleStep(ctx AuthFlowContext, input map[string][]string) (AuthFlowState, error)
}
AuthFlowHandler defines the custom auth orchestration flow.
type AuthFlowState ¶
type AuthFlowState struct {
RenderHTML string // Render this HTML in the wizard (can include error messages)
ExternalURL string // Open this URL in new tab (OAuth)
Credentials map[string]string // Flow complete, save these credentials
// Optional metadata:
WaitingForCallback bool // True if waiting for external OAuth callback
}
AuthFlowState represents the current state of the auth flow
type AuthStore ¶
type AuthStore interface {
Set(key, value string) error
Get(key string) (string, error)
Delete(key string) error
}
AuthStore provides isolated, ephemeral storage for auth flows.
type AuthType ¶
type AuthType string
AuthType describes the authentication mechanism a provider uses.
type ChatCompletionChoice ¶
type ChatCompletionChoice struct {
Index int `json:"index"`
Message ChatMessage `json:"message"`
FinishReason string `json:"finish_reason"`
}
type ChatCompletionRequest ¶
type ChatCompletionRequest struct {
Model ModelId `json:"model" example:"openai/gpt-4o"`
Messages []ChatMessage `json:"messages"`
Tools []ChatTool `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
Stream bool `json:"stream,omitempty" example:"false"`
MaxTokens int `json:"max_tokens,omitempty" example:"1000"`
Temperature float64 `json:"temperature,omitempty" example:"0.7"`
TopP float64 `json:"top_p,omitempty" example:"1.0"`
}
ChatCompletionRequest is the incoming /v1/chat/completions body.
type ChatCompletionResponse ¶
type ChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []ChatCompletionChoice `json:"choices"`
Usage ChatCompletionUsage `json:"usage"`
}
ChatCompletionResponse mirrors the OpenAI response schema.
type ChatCompletionUsage ¶
type ChatMessage ¶
type ChatMessage struct {
Role string `json:"role" example:"user" enums:"system,user,assistant,tool"`
Content string `json:"-" example:"Hello, how are you?"`
ContentParts []ChatMessageContentPart `json:"-"`
ToolCalls []ChatToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
ChatMessage is a single turn in a conversation.
func (ChatMessage) MarshalJSON ¶
func (m ChatMessage) MarshalJSON() ([]byte, error)
func (ChatMessage) TextContent ¶
func (m ChatMessage) TextContent() string
func (*ChatMessage) UnmarshalJSON ¶
func (m *ChatMessage) UnmarshalJSON(data []byte) error
type ChatMessageContentPart ¶
type ChatMessageContentPart struct {
Type string `json:"type,omitempty"`
Text string `json:"text,omitempty"`
ToolUseID string `json:"tool_use_id,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input json.RawMessage `json:"input,omitempty"`
Content []ChatMessageContentPart `json:"-"`
ContentText string `json:"-"`
}
func (ChatMessageContentPart) MarshalJSON ¶
func (p ChatMessageContentPart) MarshalJSON() ([]byte, error)
func (ChatMessageContentPart) TextContent ¶
func (p ChatMessageContentPart) TextContent() string
func (*ChatMessageContentPart) UnmarshalJSON ¶
func (p *ChatMessageContentPart) UnmarshalJSON(data []byte) error
type ChatTool ¶
type ChatTool struct {
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Parameters map[string]any `json:"parameters,omitempty"`
InputSchema map[string]any `json:"input_schema,omitempty"`
Function *ChatToolFunction `json:"function,omitempty"`
}
type ChatToolCall ¶
type ChatToolCall struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Function ChatToolFunction `json:"function"`
}
type ChatToolFunction ¶
type Credential ¶
type Credential struct {
ID string `json:"id"`
Data map[string]string `json:"data"` // e.g. {"api_key": "sk-…"} or {"access_token": "…", "refresh_token": "…"}
}
Credential holds provider-specific authentication data.
type ErrorType ¶
type ErrorType int
ErrorType classifies provider errors for retry logic.
const ( ErrorTypeUnknown ErrorType = iota ErrorTypeRateLimit // Temporary rate limit, rotate credential ErrorTypeQuotaExceeded // Credential quota exhausted, deprioritize (MUST have RetryAfter) ErrorTypeAuth // Auth failure, credential may be invalid ErrorTypeUpstream // Upstream error, don't retry ErrorTypeTimeout // Timeout, may retry )
type ModelId ¶
type ModelId string
ModelId uniquely identifies a model in the format: provider/model-name[:version] Examples: "openai/gpt-4o", "openai:azure/gpt-4o", "anthropic/claude-3-opus:20240229"
func (ModelId) Parse ¶
Parse splits a ModelId into providerID and model name. The providerID may be composite (e.g., "openai" or "openai:azure"). Examples:
"openai/gpt-4o" -> ("openai", "gpt-4o")
"openai:azure/gpt-4o" -> ("openai:azure", "gpt-4o")
type ModelInfo ¶
type ModelInfo struct {
Name string `json:"name"` // Model name without provider prefix
DisplayName string `json:"display_name"` // Human-readable name (optional)
RPM int64 `json:"rpm"` // Requests per minute (0 = unlimited/unknown)
TPM int64 `json:"tpm"` // Tokens per minute (0 = unlimited/unknown)
RPD int64 `json:"rpd"` // Requests per day (0 = unlimited/unknown)
ContextWindow int64 `json:"context_window,omitempty"` // Max context tokens
MaxTokens int64 `json:"max_tokens,omitempty"` // Max output tokens
}
ModelInfo contains metadata about a specific model.
type ProviderError ¶
type ProviderError struct {
StatusCode int
Message string
Type ErrorType
RetryAfter *time.Time // Required for ErrorTypeQuotaExceeded, optional for ErrorTypeRateLimit
}
ProviderError represents errors returned by provider adapters. Adapters should return this type to enable intelligent retry and credential rotation.
func (*ProviderError) Error ¶
func (e *ProviderError) Error() string
func (*ProviderError) IsRetryable ¶
func (e *ProviderError) IsRetryable() bool
IsRetryable returns true if this error should trigger credential rotation.
type ProviderInfo ¶
type ProviderInfo struct {
Name string // Display name (e.g., "OpenAI", "Azure OpenAI")
Qualifier string // Optional qualifier (e.g., "azure"), empty for default
BaseURL string // API base URL
IconURL string // Icon URL (remote CDN or data URI)
}
ProviderInfo defines a default provider configuration returned by adapters.
type StreamChunk ¶
type StreamChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []StreamChunkChoice `json:"choices"`
}
StreamChunk is a single SSE data payload for streaming responses.
type StreamChunkChoice ¶
type StreamChunkChoice struct {
Index int `json:"index"`
Delta ChatMessage `json:"delta"`
FinishReason *string `json:"finish_reason"`
}