providers

package
v0.0.0-...-071fe3b Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

Index

Constants

This section is empty.

Variables

This section is empty.

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

Types

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]interface{}) (*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]interface{}) (*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]interface{}) (*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]interface{}) (*LLMResponse, error)

func (*CodexProvider) GetDefaultModel

func (p *CodexProvider) GetDefaultModel() string

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

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]interface{}) (*LLMResponse, error)

Chat sends a chat request to GitHub Copilot

func (*GitHubCopilotProvider) GetDefaultModel

func (p *GitHubCopilotProvider) GetDefaultModel() string

type HTTPProvider

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

func NewHTTPProvider

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

func (*HTTPProvider) Chat

func (p *HTTPProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition, model string, options map[string]interface{}) (*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]interface{}) (*LLMResponse, error)
	GetDefaultModel() string
}

func CreateProvider

func CreateProvider(cfg *config.Config) (LLMProvider, 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 ToolCall

type ToolCall = protocoltypes.ToolCall

type ToolDefinition

type ToolDefinition = protocoltypes.ToolDefinition

type ToolFunctionDefinition

type ToolFunctionDefinition = protocoltypes.ToolFunctionDefinition

type UsageInfo

type UsageInfo = protocoltypes.UsageInfo

Directories

Path Synopsis
NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors
NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors
NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors
NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors
NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors
NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors
NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors
NemesisBot - AI agent License: MIT Copyright (c) 2026 NemesisBot contributors

Jump to

Keyboard shortcuts

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