providers

package
v0.0.0-...-8155ea7 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: GPL-2.0, GPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSkipProvider = errors.New("plugin does not handle this config")

ErrSkipProvider is returned by a plugin when it does not handle the given config (e.g., openai_oauth when auth is API key). The resolver should try the next plugin.

Functions

func CreateCodexCliTokenSource

func CreateCodexCliTokenSource() func() (string, string, error)

CreateCodexCliTokenSource creates a token source that reads from ~/.codex/auth.json. This allows the existing CodexProvider to reuse Codex CLI credentials.

func ExtractProtocol

func ExtractProtocol(model string) (protocol, modelID string)

ExtractProtocol extracts the protocol prefix and model identifier from a model string. If no prefix is specified, it defaults to "openai". Examples:

  • "openai/gpt-4o" -> ("openai", "gpt-4o")
  • "anthropic/claude-sonnet-4.6" -> ("anthropic", "claude-sonnet-4.6")
  • "gpt-4o" -> ("openai", "gpt-4o") // default protocol

func FetchAntigravityProjectID

func FetchAntigravityProjectID(accessToken string) (string, error)

FetchAntigravityProjectID retrieves the Google Cloud project ID from the loadCodeAssist endpoint.

func IsImageDimensionError

func IsImageDimensionError(msg string) bool

IsImageDimensionError returns true if the message indicates an image dimension error.

func IsImageSizeError

func IsImageSizeError(msg string) bool

IsImageSizeError returns true if the message indicates an image file size error.

func ModelKey

func ModelKey(provider, model string) string

ModelKey returns a canonical "provider/model" key for deduplication.

func NormalizeProvider

func NormalizeProvider(provider string) string

NormalizeProvider normalizes provider identifiers to canonical form.

func ReadCodexCliCredentials

func ReadCodexCliCredentials() (accessToken, accountID string, expiresAt time.Time, err error)

ReadCodexCliCredentials reads OAuth tokens from the Codex CLI's auth.json file. Expiry is estimated as file modification time + 1 hour (same approach as moltbot).

func SetPluginProviderResolver

func SetPluginProviderResolver(resolver PluginProviderResolver)

SetPluginProviderResolver sets the resolver used by CreateProviderFromConfig. Must be called before CreateProvider. The gateway calls this with a function that checks plugin.GlobalRegistry().GetProviderPlugins().

Types

type AntigravityModelInfo

type AntigravityModelInfo struct {
	ID          string `json:"id"`
	DisplayName string `json:"display_name"`
	IsExhausted bool   `json:"is_exhausted"`
}

func FetchAntigravityModels

func FetchAntigravityModels(accessToken, projectID string) ([]AntigravityModelInfo, error)

FetchAntigravityModels fetches available models from the Cloud Code Assist API.

type AntigravityProvider

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

AntigravityProvider implements LLMProvider using Google's Cloud Code Assist (Antigravity) API. This provider authenticates via Google OAuth and provides access to models like Claude and Gemini through Google's infrastructure.

func NewAntigravityProvider

func NewAntigravityProvider() *AntigravityProvider

NewAntigravityProvider creates a new Antigravity provider using stored auth credentials.

func (*AntigravityProvider) Chat

func (p *AntigravityProvider) Chat(
	ctx context.Context,
	messages []Message,
	tools []ToolDefinition,
	model string,
	options map[string]any,
) (*LLMResponse, error)

Chat implements LLMProvider.Chat using the Cloud Code Assist v1internal API. The v1internal endpoint wraps the standard Gemini request in an envelope with project, model, request, requestType, userAgent, and requestId fields.

func (*AntigravityProvider) GetDefaultModel

func (p *AntigravityProvider) GetDefaultModel() string

GetDefaultModel returns the default model identifier.

type CacheControl

type CacheControl = protocoltypes.CacheControl

type ClaudeCliProvider

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

ClaudeCliProvider implements LLMProvider using the claude CLI as a subprocess.

func NewClaudeCliProvider

func NewClaudeCliProvider(workspace string) *ClaudeCliProvider

NewClaudeCliProvider creates a new Claude CLI provider.

func (*ClaudeCliProvider) Chat

func (p *ClaudeCliProvider) Chat(
	ctx context.Context, messages []Message, tools []ToolDefinition, model string, options map[string]any,
) (*LLMResponse, error)

Chat implements LLMProvider.Chat by executing the claude CLI.

func (*ClaudeCliProvider) GetDefaultModel

func (p *ClaudeCliProvider) GetDefaultModel() string

GetDefaultModel returns the default model identifier.

type ClaudeProvider

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

func NewClaudeProvider

func NewClaudeProvider(token string) *ClaudeProvider

func NewClaudeProviderWithBaseURL

func NewClaudeProviderWithBaseURL(token, apiBase string) *ClaudeProvider

func NewClaudeProviderWithTokenSource

func NewClaudeProviderWithTokenSource(token string, tokenSource func() (string, error)) *ClaudeProvider

func NewClaudeProviderWithTokenSourceAndBaseURL

func NewClaudeProviderWithTokenSourceAndBaseURL(
	token string, tokenSource func() (string, error), apiBase string,
) *ClaudeProvider

func (*ClaudeProvider) Chat

func (p *ClaudeProvider) Chat(
	ctx context.Context, messages []Message, tools []ToolDefinition, model string, options map[string]any,
) (*LLMResponse, error)

func (*ClaudeProvider) GetDefaultModel

func (p *ClaudeProvider) GetDefaultModel() string

type CodexCliAuth

type CodexCliAuth struct {
	Tokens struct {
		AccessToken  string `json:"access_token"`
		RefreshToken string `json:"refresh_token"`
		AccountID    string `json:"account_id"`
	} `json:"tokens"`
}

CodexCliAuth represents the ~/.codex/auth.json file structure.

type CodexCliProvider

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

CodexCliProvider implements LLMProvider by wrapping the codex CLI as a subprocess.

func NewCodexCliProvider

func NewCodexCliProvider(workspace string) *CodexCliProvider

NewCodexCliProvider creates a new Codex CLI provider.

func (*CodexCliProvider) Chat

func (p *CodexCliProvider) Chat(
	ctx context.Context, messages []Message, tools []ToolDefinition, model string, options map[string]any,
) (*LLMResponse, error)

Chat implements LLMProvider.Chat by executing the codex CLI in non-interactive mode.

func (*CodexCliProvider) GetDefaultModel

func (p *CodexCliProvider) GetDefaultModel() string

GetDefaultModel returns the default model identifier.

type CodexProvider

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

func NewCodexProvider

func NewCodexProvider(token, accountID string) *CodexProvider

func NewCodexProviderWithTokenSource

func NewCodexProviderWithTokenSource(
	token, accountID string, tokenSource func() (string, string, error),
) *CodexProvider

func (*CodexProvider) Chat

func (p *CodexProvider) Chat(
	ctx context.Context, messages []Message, tools []ToolDefinition, model string, options map[string]any,
) (*LLMResponse, error)

func (*CodexProvider) GetDefaultModel

func (p *CodexProvider) GetDefaultModel() string

type ContentBlock

type ContentBlock = protocoltypes.ContentBlock

type CooldownTracker

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

CooldownTracker manages per-provider cooldown state for the fallback chain. Thread-safe via sync.RWMutex. In-memory only (resets on restart).

func NewCooldownTracker

func NewCooldownTracker() *CooldownTracker

NewCooldownTracker creates a tracker with default 24h failure window.

func (*CooldownTracker) CooldownRemaining

func (ct *CooldownTracker) CooldownRemaining(provider string) time.Duration

CooldownRemaining returns how long until the provider becomes available. Returns 0 if already available.

func (*CooldownTracker) ErrorCount

func (ct *CooldownTracker) ErrorCount(provider string) int

ErrorCount returns the current error count for a provider.

func (*CooldownTracker) FailureCount

func (ct *CooldownTracker) FailureCount(provider string, reason FailoverReason) int

FailureCount returns the failure count for a specific reason.

func (*CooldownTracker) IsAvailable

func (ct *CooldownTracker) IsAvailable(provider string) bool

IsAvailable returns true if the provider is not in cooldown or disabled.

func (*CooldownTracker) MarkFailure

func (ct *CooldownTracker) MarkFailure(provider string, reason FailoverReason)

MarkFailure records a failure for a provider and sets appropriate cooldown. Resets error counts if last failure was more than failureWindow ago.

func (*CooldownTracker) MarkSuccess

func (ct *CooldownTracker) MarkSuccess(provider string)

MarkSuccess resets all counters and cooldowns for a provider.

type ExtraContent

type ExtraContent = protocoltypes.ExtraContent

type FailoverError

type FailoverError struct {
	Reason   FailoverReason
	Provider string
	Model    string
	Status   int
	Wrapped  error
}

FailoverError wraps an LLM provider error with classification metadata.

func ClassifyError

func ClassifyError(err error, provider, model string) *FailoverError

ClassifyError classifies an error into a FailoverError with reason. Returns nil if the error is not classifiable (unknown errors should not trigger fallback).

func (*FailoverError) Error

func (e *FailoverError) Error() string

func (*FailoverError) IsRetriable

func (e *FailoverError) IsRetriable() bool

IsRetriable returns true if this error should trigger fallback to next candidate. Non-retriable: Format errors (bad request structure, image dimension/size).

func (*FailoverError) Unwrap

func (e *FailoverError) Unwrap() error

type FailoverReason

type FailoverReason string

FailoverReason classifies why an LLM request failed for fallback decisions.

const (
	FailoverAuth       FailoverReason = "auth"
	FailoverRateLimit  FailoverReason = "rate_limit"
	FailoverBilling    FailoverReason = "billing"
	FailoverTimeout    FailoverReason = "timeout"
	FailoverFormat     FailoverReason = "format"
	FailoverOverloaded FailoverReason = "overloaded"
	FailoverUnknown    FailoverReason = "unknown"
)

type FallbackAttempt

type FallbackAttempt struct {
	Provider string
	Model    string
	Error    error
	Reason   FailoverReason
	Duration time.Duration
	Skipped  bool // true if skipped due to cooldown
}

FallbackAttempt records one attempt in the fallback chain.

type FallbackCandidate

type FallbackCandidate struct {
	Provider string
	Model    string
}

FallbackCandidate represents one model/provider to try.

func ResolveCandidates

func ResolveCandidates(cfg ModelConfig, defaultProvider string) []FallbackCandidate

ResolveCandidates parses model config into a deduplicated candidate list.

func ResolveCandidatesWithLookup

func ResolveCandidatesWithLookup(
	cfg ModelConfig,
	defaultProvider string,
	lookup func(raw string) (resolved string, ok bool),
) []FallbackCandidate

type FallbackChain

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

FallbackChain orchestrates model fallback across multiple candidates.

func NewFallbackChain

func NewFallbackChain(cooldown *CooldownTracker) *FallbackChain

NewFallbackChain creates a new fallback chain with the given cooldown tracker.

func (*FallbackChain) Execute

func (fc *FallbackChain) Execute(
	ctx context.Context,
	candidates []FallbackCandidate,
	run func(ctx context.Context, provider, model string) (*LLMResponse, error),
) (*FallbackResult, error)

Execute runs the fallback chain for text/chat requests. It tries each candidate in order, respecting cooldowns and error classification.

Behavior:

  • Candidates in cooldown are skipped (logged as skipped attempt).
  • context.Canceled aborts immediately (user abort, no fallback).
  • Non-retriable errors (format) abort immediately.
  • Retriable errors trigger fallback to next candidate.
  • Success marks provider as good (resets cooldown).
  • If all fail, returns aggregate error with all attempts.

func (*FallbackChain) ExecuteImage

func (fc *FallbackChain) ExecuteImage(
	ctx context.Context,
	candidates []FallbackCandidate,
	run func(ctx context.Context, provider, model string) (*LLMResponse, error),
) (*FallbackResult, error)

ExecuteImage runs the fallback chain for image/vision requests. Simpler than Execute: no cooldown checks (image endpoints have different rate limits). Image dimension/size errors abort immediately (non-retriable).

type FallbackExhaustedError

type FallbackExhaustedError struct {
	Attempts []FallbackAttempt
}

FallbackExhaustedError indicates all fallback candidates were tried and failed.

func (*FallbackExhaustedError) Error

func (e *FallbackExhaustedError) Error() string

type FallbackResult

type FallbackResult struct {
	Response *LLMResponse
	Provider string
	Model    string
	Attempts []FallbackAttempt
}

FallbackResult contains the successful response and metadata about all attempts.

type FunctionCall

type FunctionCall = protocoltypes.FunctionCall

type GitHubCopilotProvider

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

func NewGitHubCopilotProvider

func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*GitHubCopilotProvider, error)

func (*GitHubCopilotProvider) Chat

func (p *GitHubCopilotProvider) Chat(
	ctx context.Context,
	messages []Message,
	tools []ToolDefinition,
	model string,
	options map[string]any,
) (*LLMResponse, error)

func (*GitHubCopilotProvider) Close

func (p *GitHubCopilotProvider) Close()

func (*GitHubCopilotProvider) GetDefaultModel

func (p *GitHubCopilotProvider) GetDefaultModel() string

type GoogleExtra

type GoogleExtra = protocoltypes.GoogleExtra

type HTTPProvider

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

func NewHTTPProvider

func NewHTTPProvider(apiKey, apiBase, proxy string) *HTTPProvider

func NewHTTPProviderWithMaxTokensField

func NewHTTPProviderWithMaxTokensField(apiKey, apiBase, proxy, maxTokensField string) *HTTPProvider

func NewHTTPProviderWithMaxTokensFieldAndRequestTimeout

func NewHTTPProviderWithMaxTokensFieldAndRequestTimeout(
	apiKey, apiBase, proxy, maxTokensField string,
	requestTimeoutSeconds int,
) *HTTPProvider

func (*HTTPProvider) Chat

func (p *HTTPProvider) Chat(
	ctx context.Context,
	messages []Message,
	tools []ToolDefinition,
	model string,
	options map[string]any,
) (*LLMResponse, error)

func (*HTTPProvider) GetDefaultModel

func (p *HTTPProvider) GetDefaultModel() string

type LLMProvider

type LLMProvider interface {
	Chat(
		ctx context.Context,
		messages []Message,
		tools []ToolDefinition,
		model string,
		options map[string]any,
	) (*LLMResponse, error)
	GetDefaultModel() string
}

func CreateClaudeProviderFromAuthStore

func CreateClaudeProviderFromAuthStore() (LLMProvider, error)

CreateClaudeProviderFromAuthStore creates a Claude provider from OAuth credentials. Exported for use by provider plugins.

func CreateCodexProviderFromAuthStore

func CreateCodexProviderFromAuthStore() (LLMProvider, error)

CreateCodexProviderFromAuthStore creates a Codex provider from OAuth credentials. Exported for use by provider plugins.

func CreateProvider

func CreateProvider(cfg *config.Config) (LLMProvider, string, error)

CreateProvider creates a provider based on the configuration. It uses the model_list configuration (new format) to create providers. The old providers config is automatically converted to model_list during config loading. Returns the provider, the model ID to use, and any error.

func CreateProviderFromConfig

func CreateProviderFromConfig(cfg *config.ModelConfig) (LLMProvider, string, error)

CreateProviderFromConfig creates a provider based on the ModelConfig. It uses the protocol prefix in the Model field to determine which provider to create. If SetPluginProviderResolver was called, it tries plugins first. Supported protocols: openai, litellm, anthropic, anthropic-messages, antigravity, claude-cli, codex-cli, github-copilot Returns the provider, the model ID (without protocol prefix), and any error.

type LLMResponse

type LLMResponse = protocoltypes.LLMResponse

type Message

type Message = protocoltypes.Message

type ModelConfig

type ModelConfig struct {
	Primary   string
	Fallbacks []string
}

ModelConfig holds primary model and fallback list.

type ModelRef

type ModelRef struct {
	Provider string
	Model    string
}

ModelRef represents a parsed model reference with provider and model name.

func ParseModelRef

func ParseModelRef(raw string, defaultProvider string) *ModelRef

ParseModelRef parses "anthropic/claude-opus" into {Provider: "anthropic", Model: "claude-opus"}. If no slash present, uses defaultProvider. Returns nil for empty input.

type PluginProviderResolver

type PluginProviderResolver func(modelCfg *config.ModelConfig) (LLMProvider, string, error)

PluginProviderResolver is a function that attempts to create a provider from a plugin. It is set by the gateway to avoid circular imports (plugin -> providers). Returns (provider, modelID, nil) on success, or (nil, "", err) when no plugin handles the config. When err is ErrSkipProvider, the resolver should try the next plugin.

type StatefulProvider

type StatefulProvider interface {
	LLMProvider
	Close()
}

type ThinkingCapable

type ThinkingCapable interface {
	SupportsThinking() bool
}

ThinkingCapable is an optional interface for providers that support extended thinking (e.g. Anthropic). Used by the agent loop to warn when thinking_level is configured but the active provider cannot use it.

type ToolCall

type ToolCall = protocoltypes.ToolCall

func NormalizeToolCall

func NormalizeToolCall(tc ToolCall) ToolCall

NormalizeToolCall normalizes a ToolCall to ensure all fields are properly populated. It handles cases where Name/Arguments might be in different locations (top-level vs Function) and ensures both are populated consistently.

type ToolDefinition

type ToolDefinition = protocoltypes.ToolDefinition

type ToolFunctionDefinition

type ToolFunctionDefinition = protocoltypes.ToolFunctionDefinition

type UsageInfo

type UsageInfo = protocoltypes.UsageInfo

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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