providers

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateCodexCliTokenSource added in v0.1.2

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

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

func FetchAntigravityProjectID(accessToken string) (string, error)

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

func IsImageDimensionError added in v0.2.0

func IsImageDimensionError(msg string) bool

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

func IsImageSizeError added in v0.2.0

func IsImageSizeError(msg string) bool

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

func ModelKey added in v0.2.0

func ModelKey(provider, model string) string

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

func NormalizeProvider added in v0.2.0

func NormalizeProvider(provider string) string

NormalizeProvider normalizes provider identifiers to canonical form.

func ReadCodexCliCredentials added in v0.1.2

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).

Types

type AntigravityModelInfo added in v0.2.0

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

func FetchAntigravityModels added in v0.2.0

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

FetchAntigravityModels fetches available models from the Cloud Code Assist API.

type AntigravityProvider added in v0.2.0

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

func NewAntigravityProvider() *AntigravityProvider

NewAntigravityProvider creates a new Antigravity provider using stored auth credentials.

func (*AntigravityProvider) Chat added in v0.2.0

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

func (p *AntigravityProvider) GetDefaultModel() string

GetDefaultModel returns the default model identifier.

type CacheControl added in v0.2.0

type CacheControl = protocoltypes.CacheControl

type ClaudeCliProvider added in v0.1.1

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

ClaudeCliProvider implements LLMProvider using the claude CLI as a subprocess.

func NewClaudeCliProvider added in v0.1.1

func NewClaudeCliProvider(workspace string) *ClaudeCliProvider

NewClaudeCliProvider creates a new Claude CLI provider.

func (*ClaudeCliProvider) Chat added in v0.1.1

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

func (p *ClaudeCliProvider) GetDefaultModel() string

GetDefaultModel returns the default model identifier.

type ClaudeProvider added in v0.1.1

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

func NewClaudeProvider added in v0.1.1

func NewClaudeProvider(token string) *ClaudeProvider

func NewClaudeProviderWithBaseURL added in v0.2.0

func NewClaudeProviderWithBaseURL(token, apiBase string) *ClaudeProvider

func NewClaudeProviderWithTokenSource added in v0.1.1

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

func NewClaudeProviderWithTokenSourceAndBaseURL added in v0.2.0

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

func (*ClaudeProvider) Chat added in v0.1.1

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

func (*ClaudeProvider) GetDefaultModel added in v0.1.1

func (p *ClaudeProvider) GetDefaultModel() string

type CodexCliAuth added in v0.1.2

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

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

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

func NewCodexCliProvider added in v0.1.2

func NewCodexCliProvider(workspace string) *CodexCliProvider

NewCodexCliProvider creates a new Codex CLI provider.

func (*CodexCliProvider) Chat added in v0.1.2

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

func (p *CodexCliProvider) GetDefaultModel() string

GetDefaultModel returns the default model identifier.

type CodexProvider added in v0.1.1

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

func NewCodexProvider added in v0.1.1

func NewCodexProvider(token, accountID string) *CodexProvider

func NewCodexProviderWithTokenSource added in v0.1.1

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

func (*CodexProvider) Chat added in v0.1.1

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

func (*CodexProvider) GetDefaultModel added in v0.1.1

func (p *CodexProvider) GetDefaultModel() string

type ContentBlock added in v0.2.0

type ContentBlock = protocoltypes.ContentBlock

type CooldownTracker added in v0.2.0

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

func NewCooldownTracker() *CooldownTracker

NewCooldownTracker creates a tracker with default 24h failure window.

func (*CooldownTracker) CooldownRemaining added in v0.2.0

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

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

ErrorCount returns the current error count for a provider.

func (*CooldownTracker) FailureCount added in v0.2.0

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

FailureCount returns the failure count for a specific reason.

func (*CooldownTracker) IsAvailable added in v0.2.0

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

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

func (*CooldownTracker) MarkFailure added in v0.2.0

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

func (ct *CooldownTracker) MarkSuccess(provider string)

MarkSuccess resets all counters and cooldowns for a provider.

type ExtraContent added in v0.2.0

type ExtraContent = protocoltypes.ExtraContent

type FailoverError added in v0.2.0

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

FailoverError wraps an LLM provider error with classification metadata.

func ClassifyError added in v0.2.0

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

func (e *FailoverError) Error() string

func (*FailoverError) IsRetriable added in v0.2.0

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

func (e *FailoverError) Unwrap() error

type FailoverReason added in v0.2.0

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

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

type FallbackCandidate struct {
	Provider string
	Model    string
}

FallbackCandidate represents one model/provider to try.

func ResolveCandidates added in v0.2.0

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

ResolveCandidates parses model config into a deduplicated candidate list.

func ResolveCandidatesWithLookup added in v0.2.0

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

type FallbackChain added in v0.2.0

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

FallbackChain orchestrates model fallback across multiple candidates.

func NewFallbackChain added in v0.2.0

func NewFallbackChain(cooldown *CooldownTracker) *FallbackChain

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

func (*FallbackChain) Execute added in v0.2.0

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

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

type FallbackExhaustedError struct {
	Attempts []FallbackAttempt
}

FallbackExhaustedError indicates all fallback candidates were tried and failed.

func (*FallbackExhaustedError) Error added in v0.2.0

func (e *FallbackExhaustedError) Error() string

type FallbackResult added in v0.2.0

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

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

func NewGitHubCopilotProvider added in v0.1.2

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

func (*GitHubCopilotProvider) Chat added in v0.1.2

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

func (*GitHubCopilotProvider) Close added in v0.2.0

func (p *GitHubCopilotProvider) Close()

func (*GitHubCopilotProvider) GetDefaultModel added in v0.1.2

func (p *GitHubCopilotProvider) GetDefaultModel() string

type GoogleExtra added in v0.2.0

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

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

func NewHTTPProviderWithMaxTokensFieldAndRequestTimeout added in v0.2.0

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

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. Supported protocols: openai, litellm, anthropic, 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 added in v0.2.0

type ModelConfig struct {
	Primary   string
	Fallbacks []string
}

ModelConfig holds primary model and fallback list.

type ModelRef added in v0.2.0

type ModelRef struct {
	Provider string
	Model    string
}

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

func ParseModelRef added in v0.2.0

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

type StatefulProvider interface {
	LLMProvider
	Close()
}

type ThinkingCapable added in v0.2.1

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

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