llm

package
v0.0.0-...-75ec8e3 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// OpenAI model identifiers
	ModelIDGPT5          = "gpt-5"
	ModelIDChatGPT5      = "chatgpt-5"
	ModelIDO3Mini        = "o3-mini"
	ModelIDO3            = "o3"
	ModelIDGPT4o         = "gpt-4o"
	ModelIDChatGPT4o     = "chatgpt-4o"
	ModelIDGPT4          = "gpt-4"
	ModelIDChatGPT4      = "chatgpt-4"
	ModelIDGPT35         = "gpt-3.5"
	ModelIDGPT35Alt      = "gpt-35"
	ModelIDMini          = "mini"
	ModelIDRealtime      = "realtime"
	ModelIDTurbo         = "turbo"
	ModelIDVision        = "vision"
	ModelID0125          = "0125"
	ModelID1106          = "1106"
	ModelID0301          = "0301"
	ModelID0314          = "0314"
	ModelIDTurbo20240409 = "turbo-2024-04-09"
	ModelIDPreview       = "preview"
	ModelIDAudioPreview  = "audio-preview"

	// Anthropic model identifiers
	ModelIDClaude45       = "claude-4-5"
	ModelIDClaude45Alt    = "claude-4.5"
	ModelIDClaude45Sonnet = "claude-sonnet-4.5"
	ModelIDClaude45Opus   = "claude-opus-4.5"
	ModelIDClaude46       = "claude-4-6"
	ModelIDClaude46Alt    = "claude-4.6"
	ModelIDClaude46Sonnet = "claude-sonnet-4.6"
	ModelIDClaude46Opus   = "claude-opus-4.6"
	ModelIDClaude41       = "claude-4-1"
	ModelIDClaude41Alt    = "claude-4.1"
	ModelIDClaude4        = "claude-4"
	ModelIDClaude35       = "claude-3-5"
	ModelIDClaude35Alt    = "claude-3.5"
	ModelIDClaude3        = "claude-3"
	ModelIDClaude2        = "claude-2"
	ModelIDSonnet         = "sonnet"
	ModelIDOpus           = "opus"
	ModelIDHaiku          = "haiku"

	// Google model identifiers
	ModelIDGemini2     = "gemini-2"
	ModelIDGemini2Alt  = "gemini2"
	ModelIDGemini15    = "gemini-1.5"
	ModelIDGemini15Alt = "gemini-15"
	ModelIDGemini1     = "gemini-1"
	ModelIDGemini1Alt  = "gemini1"
	ModelIDFlash       = "flash"

	// Meta Llama model identifiers
	ModelIDLlama33    = "llama-3.3"
	ModelIDLlama33Alt = "llama3.3"
	ModelIDLlama32    = "llama-3.2"
	ModelIDLlama32Alt = "llama3.2"
	ModelIDLlama31    = "llama-3.1"
	ModelIDLlama31Alt = "llama3.1"
	ModelIDLlama3     = "llama-3"
	ModelIDLlama3Alt  = "llama3"
	ModelIDLlama2     = "llama-2"
	ModelIDLlama2Alt  = "llama2"
	ModelID70B        = "70b"

	// Mistral model identifiers
	ModelIDCodestral     = "codestral"
	ModelIDPixtral       = "pixtral"
	ModelIDMixtral       = "mixtral"
	ModelIDMistralLarge  = "mistral-large"
	ModelIDMistralMedium = "mistral-medium"
	ModelIDMistralSmall  = "mistral-small"
	ModelIDDevstral      = "devstral"
	ModelIDOpenMistral   = "open-mistral"
	ModelIDMedium        = "medium"
	ModelIDSmall         = "small"

	// Other model identifiers
	ModelIDQwen     = "qwen"
	ModelIDGemma    = "gemma"
	ModelIDPhi      = "phi"
	ModelIDDeepSeek = "deepseek"
	ModelIDCommand  = "command"
	ModelIDZaiGLM   = "zai-glm"
	ModelIDGLM      = "glm-"
	ModelIDKimi     = "kimi"
	ModelIDK2       = "k2"
	ModelIDMoonshot = "moonshot"
	ModelIDMiniMax  = "minimax"

	// Size indicators
	ModelID128K   = "128k"
	ModelID100K   = "100k"
	ModelID64K    = "64k"
	ModelID32K    = "32k"
	ModelID16K    = "16k"
	ModelID8K     = "8k"
	ModelID4K     = "4k"
	ModelID200K   = "200k"
	ModelID131072 = "131072"
	ModelID32768  = "32768"
	ModelID16384  = "16384"
	ModelID8192   = "8192"
)

Model identifier constants for pattern matching

View Source
const AgentsFileName = "AGENTS.md"
View Source
const AgentsLocalFileName = "AGENTS.local.md"

Variables

This section is empty.

Functions

func CleanLLMJSONResponse

func CleanLLMJSONResponse(response string) string

CleanLLMJSONResponse removes common formatting from LLM JSON responses. It handles: - Markdown code blocks (```json or ```) - XML-style tags (<tag>content</tag>) - Leading/trailing whitespace

func DetectContextWindow

func DetectContextWindow(modelID string, family ModelFamily) int

DetectContextWindow detects context window size for a model

func DetectMaxOutputTokens

func DetectMaxOutputTokens(modelID string, family ModelFamily, contextWindow int) int

DetectMaxOutputTokens detects maximum output tokens for a model

func EstimateTokenCount

func EstimateTokenCount(content string) int

EstimateTokenCount returns a rough token estimate for the provided content.

func EstimateTokenCountForMessage

func EstimateTokenCountForMessage(msg *Message) int

EstimateTokenCountForMessage returns the token estimate for a single message's content.

func ExtractJSON

func ExtractJSON[T any](response string, target T) error

ExtractJSON extracts a JSON object from a response using flexible strategies. It tries: 1. Direct parse of cleaned response 2. Extract content between { and } braces Returns the parsed object or an error.

func ExtractJSONArray

func ExtractJSONArray[T any](response string) ([]T, error)

ExtractJSONArray attempts to extract and parse a JSON array from a response. It tries multiple strategies: 1. Direct parse of cleaned response 2. Extract content between [ and ] brackets Returns the parsed array or an error.

func FormatModelDisplayName

func FormatModelDisplayName(modelID string, family ModelFamily) string

FormatModelDisplayName formats a model ID into a human-readable display name

func GetModelDescription

func GetModelDescription(modelID string, family ModelFamily) string

GetModelDescription generates a description for a model

func GetProviderAndFamily

func GetProviderAndFamily(modelID string) (provider, modelFamily string)

GetProviderAndFamily is a helper function to extract provider and family from model ID

func InitPrompt

func InitPrompt() string

InitPrompt returns the prompt for the /init command that asks the LLM to analyze the codebase

func IsMistralModel

func IsMistralModel(modelID string) bool

IsMistralModel checks if the given model ID belongs to the Mistral family

func IsQwen3Model

func IsQwen3Model(modelID string) bool

IsQwen3Model checks if the given model ID belongs to the Qwen 3 family

func MatchExactOrPrefix

func MatchExactOrPrefix(modelID string, exactMatches map[string]int, prefixPatterns []PrefixPattern) (int, bool)

MatchExactOrPrefix tries exact match first, then prefix patterns (longest first)

func NormalizeToolCallIDs

func NormalizeToolCallIDs(toolCalls []map[string]interface{}) []map[string]interface{}

NormalizeToolCallIDs ensures every tool call has a stable identifier and consistent format across providers. Some providers (e.g. Ollama) omit the "type" field or return "arguments" as a map instead of a JSON string. This function normalizes all tool calls to the canonical format:

{"id": "...", "type": "function", "function": {"name": "...", "arguments": "<json string>"}}

func ParseExplicitSize

func ParseExplicitSize(modelID string) (int, bool)

ParseExplicitSize extracts explicit size indicators from model ID (128k, 32768, etc.)

func ParseLLMJSONResponse

func ParseLLMJSONResponse(response string, target interface{}) error

ParseLLMJSONResponse parses a JSON response from an LLM, cleaning it first. Returns an error if parsing fails.

func StripThinkTags

func StripThinkTags(content string) string

StripThinkTags removes <think>...</think> blocks from content. Reasoning models like DeepSeek and Qwen 3 wrap their internal reasoning in these tags.

func SupportsToolCalling

func SupportsToolCalling(modelID string, family ModelFamily) bool

SupportsToolCalling detects if a model supports tool/function calling

func TruncateForError

func TruncateForError(value string, limit int) string

TruncateForError truncates a string for error messages.

Types

type AnthropicClient

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

AnthropicClient implements the Client interface using the official Anthropic SDK.

func (*AnthropicClient) Complete

func (c *AnthropicClient) Complete(ctx context.Context, prompt string) (string, error)

func (*AnthropicClient) CompleteWithRequest

func (c *AnthropicClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*AnthropicClient) GetLastResponseID

func (c *AnthropicClient) GetLastResponseID() string

func (*AnthropicClient) GetModelName

func (c *AnthropicClient) GetModelName() string

func (*AnthropicClient) SetPreviousResponseID

func (c *AnthropicClient) SetPreviousResponseID(responseID string)

func (*AnthropicClient) Stream

func (c *AnthropicClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type AnthropicConverterImpl

type AnthropicConverterImpl struct{}

AnthropicConverterImpl implements NativeConverter for Anthropic/Claude models

func (*AnthropicConverterImpl) ConvertFromNative

func (c *AnthropicConverterImpl) ConvertFromNative(native []interface{}) ([]*Message, error)

ConvertFromNative converts Anthropic BetaMessageParam back to unified Message format

func (*AnthropicConverterImpl) ConvertToNative

func (c *AnthropicConverterImpl) ConvertToNative(messages []*Message, systemPrompt string, enableCaching bool, cacheTTL string) ([]interface{}, error)

ConvertToNative converts unified messages to Anthropic BetaMessageParam format

func (*AnthropicConverterImpl) GetModelFamily

func (c *AnthropicConverterImpl) GetModelFamily(modelID string) string

func (*AnthropicConverterImpl) GetProviderName

func (c *AnthropicConverterImpl) GetProviderName() string

func (*AnthropicConverterImpl) SupportsNativeStorage

func (c *AnthropicConverterImpl) SupportsNativeStorage() bool

type AnthropicProvider

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

AnthropicProvider implements the Provider interface for Anthropic

func NewAnthropicProvider

func NewAnthropicProvider(apiKey string) *AnthropicProvider

NewAnthropicProvider creates a new Anthropic provider

func (*AnthropicProvider) CreateClient

func (p *AnthropicProvider) CreateClient(modelID string) (Client, error)

func (*AnthropicProvider) GetName

func (p *AnthropicProvider) GetName() string

func (*AnthropicProvider) ListModels

func (p *AnthropicProvider) ListModels(ctx context.Context) ([]*ModelInfo, error)

func (*AnthropicProvider) ValidateAPIKey

func (p *AnthropicProvider) ValidateAPIKey(ctx context.Context) error

type CerebrasClient

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

CerebrasClient implements the Client interface using the native Cerebras Chat Completions API.

func (*CerebrasClient) Complete

func (c *CerebrasClient) Complete(ctx context.Context, prompt string) (string, error)

func (*CerebrasClient) CompleteWithRequest

func (c *CerebrasClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*CerebrasClient) GetLastResponseID

func (c *CerebrasClient) GetLastResponseID() string

func (*CerebrasClient) GetModelName

func (c *CerebrasClient) GetModelName() string

func (*CerebrasClient) SetPreviousResponseID

func (c *CerebrasClient) SetPreviousResponseID(responseID string)

func (*CerebrasClient) Stream

func (c *CerebrasClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type CerebrasConverterImpl

type CerebrasConverterImpl struct{}

CerebrasConverterImpl implements NativeConverter for Cerebras models

func (*CerebrasConverterImpl) ConvertFromNative

func (c *CerebrasConverterImpl) ConvertFromNative(native []interface{}) ([]*Message, error)

func (*CerebrasConverterImpl) ConvertToNative

func (c *CerebrasConverterImpl) ConvertToNative(messages []*Message, systemPrompt string, enableCaching bool, cacheTTL string) ([]interface{}, error)

func (*CerebrasConverterImpl) GetModelFamily

func (c *CerebrasConverterImpl) GetModelFamily(modelID string) string

func (*CerebrasConverterImpl) GetProviderName

func (c *CerebrasConverterImpl) GetProviderName() string

func (*CerebrasConverterImpl) SupportsNativeStorage

func (c *CerebrasConverterImpl) SupportsNativeStorage() bool

type CerebrasProvider

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

CerebrasProvider implements the Provider interface for Cerebras Cloud AI.

func NewCerebrasProvider

func NewCerebrasProvider(apiKey string) *CerebrasProvider

NewCerebrasProvider creates a new Cerebras provider instance.

func (*CerebrasProvider) CreateClient

func (p *CerebrasProvider) CreateClient(modelID string) (Client, error)

CreateClient creates a new client for the specified Cerebras model.

func (*CerebrasProvider) GetName

func (p *CerebrasProvider) GetName() string

GetName returns the provider name.

func (*CerebrasProvider) ListModels

func (p *CerebrasProvider) ListModels(ctx context.Context) ([]*ModelInfo, error)

ListModels fetches available models from the Cerebras API using OpenRouter format.

func (*CerebrasProvider) ValidateAPIKey

func (p *CerebrasProvider) ValidateAPIKey(ctx context.Context) error

ValidateAPIKey validates the Cerebras API key by attempting to list models.

type Client

type Client interface {
	// Complete sends a completion request and returns the response
	CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)
	// Complete is a simplified version for single prompt
	Complete(ctx context.Context, prompt string) (string, error)
	// Stream sends a streaming completion request
	Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error
	// GetModelName returns the model name
	GetModelName() string
	// GetLastResponseID returns the last response ID for prompt caching (OpenRouter-specific, returns empty string for other providers)
	GetLastResponseID() string
	// SetPreviousResponseID sets the previous response ID for the next request (OpenRouter-specific, no-op for other providers)
	SetPreviousResponseID(string)
}

Client is the interface for LLM clients

func NewAnthropicClient

func NewAnthropicClient(apiKey, modelName string) (Client, error)

NewAnthropicClient creates an Anthropic client backed by the official SDK.

func NewCachingAwareClient

func NewCachingAwareClient(base Client, provider string) Client

NewCachingAwareClient returns a Client that adjusts caching behavior based on provider type. The provider parameter should be the provider name (e.g., "openai", "anthropic", "openai-compatible").

func NewCerebrasClient

func NewCerebrasClient(apiKey, modelID string) (Client, error)

NewCerebrasClient constructs a Cerebras client for the specified model.

func NewGoogleAIClient

func NewGoogleAIClient(apiKey, modelName string) (Client, error)

NewGoogleAIClient creates a Google GenAI client for the provided model.

func NewGroqClient

func NewGroqClient(apiKey, modelID string) (Client, error)

NewGroqClient creates a Groq client that automatically chooses between the Responses API and standard chat completions API based on the model.

func NewMistralClient

func NewMistralClient(apiKey, modelName string) (Client, error)

NewMistralClient creates a new client for the Mistral chat completion API.

func NewOllamaClient

func NewOllamaClient(baseURL, model string) (Client, error)

NewOllamaClient creates a new Ollama client for the provided model.

func NewOpenAIClient

func NewOpenAIClient(apiKey, modelName string) (Client, error)

NewOpenAIClient constructs a client that talks directly to the OpenAI API.

func NewOpenAICompatibleGroqClient

func NewOpenAICompatibleGroqClient(apiKey, modelID string) (Client, error)

NewOpenAICompatibleGroqClient creates a Groq client backed by the native OpenAI-compatible implementation. This is a fallback for backward compatibility.

func NewOpenAIWebSocketClient

func NewOpenAIWebSocketClient(apiKey, modelName string) (Client, error)

func NewOpenRouterClient

func NewOpenRouterClient(apiKey, modelID string) (Client, error)

NewOpenRouterClient creates a new OpenRouter client.

func NewRateLimitedClient

func NewRateLimitedClient(base Client, interval time.Duration, tokensPerMinute int) Client

NewRateLimitedClient returns a Client that throttles calls using QPS and token budgets.

func NewZaiClient

func NewZaiClient(apiKey, baseURL, modelID string) (Client, error)

NewZaiClient constructs a Z.AI client for the specified model.

type CompletionRequest

type CompletionRequest struct {
	Messages           []*Message               `json:"messages"`
	Tools              []map[string]interface{} `json:"tools,omitempty"`
	Temperature        float64                  `json:"temperature"`
	MaxTokens          int                      `json:"max_tokens,omitempty"`
	TopP               float64                  `json:"top_p,omitempty"` // Nucleus sampling parameter (0.0-1.0)
	SystemPrompt       string                   `json:"system_prompt,omitempty"`
	EnableCaching      bool                     `json:"enable_caching,omitempty"`       // Enable prompt caching (Anthropic, OpenAI, OpenRouter)
	CacheTTL           string                   `json:"cache_ttl,omitempty"`            // Cache TTL: "5m" or "1h" (Anthropic only, others use provider defaults)
	ClearThinking      *bool                    `json:"clear_thinking,omitempty"`       // Cerebras: preserve reasoning traces (false recommended for agentic workflows)
	PreviousResponseID string                   `json:"previous_response_id,omitempty"` // For OpenRouter: reference previous response for better prompt caching
	ReasoningEffort    string                   `json:"reasoning_effort,omitempty"`     // Reasoning effort level: "xhigh", "high", "medium", "low", "minimal", "none"
}

CompletionRequest represents a completion request

type CompletionResponse

type CompletionResponse struct {
	Content    string                   `json:"content"`
	Reasoning  string                   `json:"reasoning,omitempty"` // Reasoning/thinking content (e.g., from extended thinking models)
	ToolCalls  []map[string]interface{} `json:"tool_calls,omitempty"`
	StopReason string                   `json:"stop_reason"`
	Usage      map[string]interface{}   `json:"usage,omitempty"` // Provider-specific usage data (tokens, cost, etc.)
}

CompletionResponse represents a completion response

type GoogleConverterImpl

type GoogleConverterImpl struct{}

GoogleConverterImpl implements NativeConverter for Google/Gemini models

func (*GoogleConverterImpl) ConvertFromNative

func (c *GoogleConverterImpl) ConvertFromNative(native []interface{}) ([]*Message, error)

ConvertFromNative converts Google GenAI messages back to unified format

func (*GoogleConverterImpl) ConvertToNative

func (c *GoogleConverterImpl) ConvertToNative(messages []*Message, systemPrompt string, enableCaching bool, cacheTTL string) ([]interface{}, error)

ConvertToNative converts unified messages to Google GenAI format Note: Google uses a different caching approach (cachedContent API) that requires separate content creation. This converter prepares messages in native format for potential future caching integration.

func (*GoogleConverterImpl) GetModelFamily

func (c *GoogleConverterImpl) GetModelFamily(modelID string) string

func (*GoogleConverterImpl) GetProviderName

func (c *GoogleConverterImpl) GetProviderName() string

func (*GoogleConverterImpl) SupportsNativeStorage

func (c *GoogleConverterImpl) SupportsNativeStorage() bool

type GoogleGenAIClient

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

GoogleGenAIClient implements the Client interface using the official Google GenAI SDK.

func (*GoogleGenAIClient) Complete

func (c *GoogleGenAIClient) Complete(ctx context.Context, prompt string) (string, error)

func (*GoogleGenAIClient) CompleteWithRequest

func (c *GoogleGenAIClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*GoogleGenAIClient) GetLastResponseID

func (c *GoogleGenAIClient) GetLastResponseID() string

func (*GoogleGenAIClient) GetModelName

func (c *GoogleGenAIClient) GetModelName() string

func (*GoogleGenAIClient) SetPreviousResponseID

func (c *GoogleGenAIClient) SetPreviousResponseID(responseID string)

func (*GoogleGenAIClient) Stream

func (c *GoogleGenAIClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type GoogleProvider

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

GoogleProvider implements the Provider interface using the official Google GenAI SDK.

func NewGoogleProvider

func NewGoogleProvider(apiKey string) *GoogleProvider

NewGoogleProvider creates a new Google provider

func (*GoogleProvider) CreateClient

func (p *GoogleProvider) CreateClient(modelID string) (Client, error)

func (*GoogleProvider) GetName

func (p *GoogleProvider) GetName() string

func (*GoogleProvider) ListModels

func (p *GoogleProvider) ListModels(ctx context.Context) ([]*ModelInfo, error)

func (*GoogleProvider) ValidateAPIKey

func (p *GoogleProvider) ValidateAPIKey(ctx context.Context) error

type GroqClient

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

GroqClient implements the Client interface for Groq's API, supporting both the standard OpenAI-compatible chat completions API and the new Responses API for specific models (e.g., models with "openai/" prefix).

The client automatically detects which API to use based on the model name: - Responses API: models with "openai/" prefix (e.g., "openai/gpt-oss-120b") - Chat Completions API: all other models

func (*GroqClient) Complete

func (c *GroqClient) Complete(ctx context.Context, prompt string) (string, error)

func (*GroqClient) CompleteWithRequest

func (c *GroqClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*GroqClient) GetLastResponseID

func (c *GroqClient) GetLastResponseID() string

func (*GroqClient) GetModelName

func (c *GroqClient) GetModelName() string

func (*GroqClient) SetPreviousResponseID

func (c *GroqClient) SetPreviousResponseID(responseID string)

func (*GroqClient) Stream

func (c *GroqClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type GroqProvider

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

GroqProvider implements the Provider interface for Groq's OpenAI-compatible API.

func NewGroqProvider

func NewGroqProvider(apiKey string) *GroqProvider

NewGroqProvider creates a new Groq provider instance.

func (*GroqProvider) CreateClient

func (p *GroqProvider) CreateClient(modelID string) (Client, error)

CreateClient creates a new client for the specified Groq model.

func (*GroqProvider) GetName

func (p *GroqProvider) GetName() string

func (*GroqProvider) ListModels

func (p *GroqProvider) ListModels(ctx context.Context) ([]*ModelInfo, error)

ListModels retrieves the available Groq models via the REST API.

func (*GroqProvider) ValidateAPIKey

func (p *GroqProvider) ValidateAPIKey(ctx context.Context) error

ValidateAPIKey validates the provided Groq API key by attempting to list models.

type JSONParseError

type JSONParseError struct {
	Response string
	Message  string
}

JSONParseError represents an error that occurred while parsing LLM JSON response.

func (*JSONParseError) Error

func (e *JSONParseError) Error() string

type KimiClient

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

KimiClient implements the Client interface for Kimi (Moonshot AI) Kimi's API is OpenAI-compatible, so we wrap the OpenAICompatibleClient

func NewKimiClient

func NewKimiClient(apiKey, model string) (*KimiClient, error)

NewKimiClient creates a new Kimi client

func (*KimiClient) Complete

func (c *KimiClient) Complete(ctx context.Context, prompt string) (string, error)

func (*KimiClient) CompleteWithRequest

func (c *KimiClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*KimiClient) GetLastResponseID

func (c *KimiClient) GetLastResponseID() string

func (*KimiClient) GetModelName

func (c *KimiClient) GetModelName() string

func (*KimiClient) SetPreviousResponseID

func (c *KimiClient) SetPreviousResponseID(responseID string)

func (*KimiClient) Stream

func (c *KimiClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type KimiProvider

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

KimiProvider implements the Provider interface for Kimi (Moonshot AI)

func NewKimiProvider

func NewKimiProvider(apiKey string) *KimiProvider

NewKimiProvider creates a new Kimi provider

func (*KimiProvider) CreateClient

func (p *KimiProvider) CreateClient(modelID string) (Client, error)

func (*KimiProvider) GetName

func (p *KimiProvider) GetName() string

func (*KimiProvider) ListModels

func (p *KimiProvider) ListModels(ctx context.Context) ([]*ModelInfo, error)

func (*KimiProvider) ValidateAPIKey

func (p *KimiProvider) ValidateAPIKey(ctx context.Context) error

type Message

type Message struct {
	Role         string                   `json:"role"`
	Content      string                   `json:"content"`
	Reasoning    string                   `json:"reasoning,omitempty"` // Reasoning/thinking content (e.g., from extended thinking models)
	ToolCalls    []map[string]interface{} `json:"tool_calls,omitempty"`
	ToolID       string                   `json:"tool_id,omitempty"`
	ToolName     string                   `json:"tool_name,omitempty"`     // Name of the tool for tool responses
	CacheControl bool                     `json:"cache_control,omitempty"` // Marks the message as the end of a cached prefix

	// Native format storage (for prompt caching)
	NativeFormat      interface{} `json:"native_format,omitempty"`       // Provider-specific message format
	NativeProvider    string      `json:"native_provider,omitempty"`     // e.g., "anthropic", "openai"
	NativeModelFamily string      `json:"native_model_family,omitempty"` // e.g., "claude-3", "gpt-4"
	NativeTimestamp   time.Time   `json:"native_timestamp,omitempty"`    // When native format was created
}

Message represents a chat message

func SanitizeMessages

func SanitizeMessages(messages []*Message) ([]*Message, bool)

SanitizeMessages validates and repairs a message array for OpenAI-compatible APIs. It fixes structural issues that can arise after context compaction races, such as:

  • Orphan tool responses (no matching assistant tool call)
  • Assistant messages with tool_calls but missing subsequent tool responses
  • Messages starting with a non-user/non-system role
  • Consecutive messages with the same role (merged where appropriate)

The function returns a repaired copy and whether any repairs were made.

type MistralClient

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

MistralClient implements the Client interface using the native Mistral API.

func (*MistralClient) Complete

func (c *MistralClient) Complete(ctx context.Context, prompt string) (string, error)

func (*MistralClient) CompleteWithRequest

func (c *MistralClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*MistralClient) GetLastResponseID

func (c *MistralClient) GetLastResponseID() string

func (*MistralClient) GetModelName

func (c *MistralClient) GetModelName() string

func (*MistralClient) SetPreviousResponseID

func (c *MistralClient) SetPreviousResponseID(responseID string)

func (*MistralClient) Stream

func (c *MistralClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type MistralConverterImpl

type MistralConverterImpl struct{}

MistralConverterImpl implements NativeConverter for Mistral models

func (*MistralConverterImpl) ConvertFromNative

func (c *MistralConverterImpl) ConvertFromNative(native []interface{}) ([]*Message, error)

func (*MistralConverterImpl) ConvertToNative

func (c *MistralConverterImpl) ConvertToNative(messages []*Message, systemPrompt string, enableCaching bool, cacheTTL string) ([]interface{}, error)

func (*MistralConverterImpl) GetModelFamily

func (c *MistralConverterImpl) GetModelFamily(modelID string) string

func (*MistralConverterImpl) GetProviderName

func (c *MistralConverterImpl) GetProviderName() string

func (*MistralConverterImpl) SupportsNativeStorage

func (c *MistralConverterImpl) SupportsNativeStorage() bool

type MistralProvider

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

MistralProvider implements the Provider interface for Mistral AI

func NewMistralProvider

func NewMistralProvider(apiKey string) *MistralProvider

NewMistralProvider creates a new Mistral provider

func (*MistralProvider) CreateClient

func (p *MistralProvider) CreateClient(modelID string) (Client, error)

func (*MistralProvider) GetName

func (p *MistralProvider) GetName() string

func (*MistralProvider) ListModels

func (p *MistralProvider) ListModels(ctx context.Context) ([]*ModelInfo, error)

func (*MistralProvider) ValidateAPIKey

func (p *MistralProvider) ValidateAPIKey(ctx context.Context) error

type Model

type Model struct {
	Provider string `json:"provider"` // openai, anthropic, etc.
	Name     string `json:"name"`
	ID       string `json:"id"`
}

Model represents an LLM model

type ModelFamily

type ModelFamily int

ModelFamily represents a family of models from a specific provider

const (
	FamilyUnknown ModelFamily = iota
	// OpenAI families
	FamilyGPT5
	FamilyO3
	FamilyGPT4o
	FamilyGPT4
	FamilyGPT35
	// Anthropic families
	FamilyClaude45
	FamilyClaude41
	FamilyClaude4
	FamilyClaude35
	FamilyClaude3
	FamilyClaude2
	// Google families
	FamilyGemini2
	FamilyGemini15
	FamilyGemini1
	// Meta families
	FamilyLlama33
	FamilyLlama32
	FamilyLlama31
	FamilyLlama3
	FamilyLlama2
	// Mistral families
	FamilyMistralLarge
	FamilyMistralMedium
	FamilyMistralSmall
	FamilyCodestral
	FamilyPixtral
	FamilyMixtral
	FamilyDevstral
	// Other families
	FamilyQwen
	FamilyGemma
	FamilyPhi
	FamilyDeepSeek
	FamilyCommand
	FamilyZaiGLM
	FamilyKimi
	FamilyMiniMax
)

func DetectModelFamily

func DetectModelFamily(modelID string) ModelFamily

DetectModelFamily detects the model family from a model ID

type ModelInfo

type ModelInfo struct {
	ID                      string   `json:"id"`
	Name                    string   `json:"name"`
	Provider                string   `json:"provider"`
	Description             string   `json:"description,omitempty"`
	ContextWindow           int      `json:"context_window,omitempty"`    // Input context window size
	MaxOutputTokens         int      `json:"max_output_tokens,omitempty"` // Maximum output tokens
	SupportsToolCalling     bool     `json:"supports_tool_calling"`
	SupportsStreaming       bool     `json:"supports_streaming"`
	CreatedAt               string   `json:"created_at,omitempty"`
	OwnedBy                 string   `json:"owned_by,omitempty"`
	Capabilities            []string `json:"capabilities,omitempty"`
	InputModalities         []string `json:"input_modalities,omitempty"`
	OutputModalities        []string `json:"output_modalities,omitempty"`
	Pricing                 *Pricing `json:"pricing,omitempty"`
	SupportedSamplingParams []string `json:"supported_sampling_parameters,omitempty"`
	SupportedFeatures       []string `json:"supported_features,omitempty"`
}

ModelInfo represents detailed information about an LLM model

func GetCerebrasModelInfo

func GetCerebrasModelInfo(modelID string) *ModelInfo

GetCerebrasModelInfo returns detailed information about a specific Cerebras model. This is a helper function for testing and debugging.

type NativeConverter

type NativeConverter interface {
	// GetProviderName returns the provider identifier (e.g., "anthropic", "openai")
	GetProviderName() string

	// GetModelFamily returns the model family for a given model ID (e.g., "claude-3", "gpt-4")
	GetModelFamily(modelID string) string

	// ConvertToNative converts unified messages to provider-specific format
	// Returns slice of provider-specific message objects
	ConvertToNative(messages []*Message, systemPrompt string, enableCaching bool, cacheTTL string) ([]interface{}, error)

	// ConvertFromNative converts provider-specific messages back to unified format
	ConvertFromNative(native []interface{}) ([]*Message, error)

	// SupportsNativeStorage indicates if this provider supports native message storage
	SupportsNativeStorage() bool
}

NativeConverter handles bidirectional message conversion for a provider

func GetConverter

func GetConverter(modelID string) NativeConverter

GetConverter returns the appropriate converter for a model ID

type NativeMessage

type NativeMessage struct {
	Native      interface{}
	Provider    string
	ModelFamily string
	Timestamp   time.Time
}

NativeMessage wraps a provider-specific message with metadata

type OllamaClient

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

OllamaClient implements the Client interface for the Ollama REST API.

func (*OllamaClient) Complete

func (c *OllamaClient) Complete(ctx context.Context, prompt string) (string, error)

func (*OllamaClient) CompleteWithRequest

func (c *OllamaClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*OllamaClient) GetLastResponseID

func (c *OllamaClient) GetLastResponseID() string

func (*OllamaClient) GetModelName

func (c *OllamaClient) GetModelName() string

func (*OllamaClient) SetPreviousResponseID

func (c *OllamaClient) SetPreviousResponseID(responseID string)

func (*OllamaClient) Stream

func (c *OllamaClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type OllamaConverterImpl

type OllamaConverterImpl struct{}

OllamaConverterImpl implements NativeConverter for Ollama models

func (*OllamaConverterImpl) ConvertFromNative

func (c *OllamaConverterImpl) ConvertFromNative(native []interface{}) ([]*Message, error)

func (*OllamaConverterImpl) ConvertToNative

func (c *OllamaConverterImpl) ConvertToNative(messages []*Message, systemPrompt string, enableCaching bool, cacheTTL string) ([]interface{}, error)

func (*OllamaConverterImpl) GetModelFamily

func (c *OllamaConverterImpl) GetModelFamily(modelID string) string

func (*OllamaConverterImpl) GetProviderName

func (c *OllamaConverterImpl) GetProviderName() string

func (*OllamaConverterImpl) SupportsNativeStorage

func (c *OllamaConverterImpl) SupportsNativeStorage() bool

type OllamaProvider

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

OllamaProvider implements the Provider interface for local or remote Ollama instances.

func NewOllamaProvider

func NewOllamaProvider(apiKey string) *OllamaProvider

NewOllamaProvider creates a new Ollama provider. The apiKey parameter is reused as a base URL for compatibility with the provider manager.

func (*OllamaProvider) CreateClient

func (p *OllamaProvider) CreateClient(modelID string) (Client, error)

func (*OllamaProvider) GetName

func (p *OllamaProvider) GetName() string

func (*OllamaProvider) ListModels

func (p *OllamaProvider) ListModels(ctx context.Context) ([]*ModelInfo, error)

func (*OllamaProvider) ValidateAPIKey

func (p *OllamaProvider) ValidateAPIKey(ctx context.Context) error

type OpenAIClient

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

OpenAIClient implements the Client interface using OpenAI's native APIs.

func (*OpenAIClient) Complete

func (c *OpenAIClient) Complete(ctx context.Context, prompt string) (string, error)

func (*OpenAIClient) CompleteWithRequest

func (c *OpenAIClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*OpenAIClient) GetLastResponseID

func (c *OpenAIClient) GetLastResponseID() string

func (*OpenAIClient) GetModelName

func (c *OpenAIClient) GetModelName() string

func (*OpenAIClient) SetPreviousResponseID

func (c *OpenAIClient) SetPreviousResponseID(responseID string)

func (*OpenAIClient) Stream

func (c *OpenAIClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type OpenAICompatibleClient

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

OpenAICompatibleClient implements the Client interface for generic OpenAI-compatible APIs. It uses the same JSON payloads as OpenAI's chat completions endpoint and supports optional API keys plus custom base URLs.

Features: - Chat completions (single-shot + structured requests) - Streaming responses via SSE-style data chunks - Tool call serialization / deserialization - Optional system prompts - Customizable temperature and max_tokens if supported - Uses provider-specific message normalization (Mistral) reused from helper functions

This client intentionally mirrors OpenAIClient's behavior but delegates HTTP calls to arbitrary OpenAI-compatible servers (LocalAI, LM Studio, Groq base, etc.).

func NewOpenAICompatibleClient

func NewOpenAICompatibleClient(apiKey, baseURL, modelName string) (*OpenAICompatibleClient, error)

NewOpenAICompatibleClient constructs a client for an OpenAI-compatible API. baseURL must point to the API root (e.g. http://localhost:11434/v1). If apiKey is empty, requests are sent without Authorization headers (useful for unsecured local servers).

func (*OpenAICompatibleClient) Complete

func (c *OpenAICompatibleClient) Complete(ctx context.Context, prompt string) (string, error)

func (*OpenAICompatibleClient) CompleteWithRequest

func (c *OpenAICompatibleClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*OpenAICompatibleClient) GetLastResponseID

func (c *OpenAICompatibleClient) GetLastResponseID() string

func (*OpenAICompatibleClient) GetModelName

func (c *OpenAICompatibleClient) GetModelName() string

func (*OpenAICompatibleClient) SetPreviousResponseID

func (c *OpenAICompatibleClient) SetPreviousResponseID(responseID string)

func (*OpenAICompatibleClient) Stream

func (c *OpenAICompatibleClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type OpenAICompatibleProvider

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

OpenAICompatibleProvider implements the Provider interface for OpenAI-compatible APIs This includes local LLMs (LM Studio, LocalAI, Ollama with OpenAI compat layer, etc.) and custom deployments that follow the OpenAI API specification

func NewOpenAICompatibleProvider

func NewOpenAICompatibleProvider(apiKey string, baseURL string) *OpenAICompatibleProvider

NewOpenAICompatibleProvider creates a new OpenAI-compatible provider baseURL should be the API endpoint (e.g., "http://localhost:1234/v1" for LM Studio) If apiKey is empty, requests will be made without authentication

func (*OpenAICompatibleProvider) CreateClient

func (p *OpenAICompatibleProvider) CreateClient(modelID string) (Client, error)

func (*OpenAICompatibleProvider) GetName

func (p *OpenAICompatibleProvider) GetName() string

func (*OpenAICompatibleProvider) ListModels

func (p *OpenAICompatibleProvider) ListModels(ctx context.Context) ([]*ModelInfo, error)

func (*OpenAICompatibleProvider) ValidateAPIKey

func (p *OpenAICompatibleProvider) ValidateAPIKey(ctx context.Context) error

type OpenAIConverterImpl

type OpenAIConverterImpl struct{}

OpenAIConverterImpl implements NativeConverter for OpenAI models

func (*OpenAIConverterImpl) ConvertFromNative

func (c *OpenAIConverterImpl) ConvertFromNative(native []interface{}) ([]*Message, error)

ConvertFromNative converts OpenAI messages back to unified format

func (*OpenAIConverterImpl) ConvertToNative

func (c *OpenAIConverterImpl) ConvertToNative(messages []*Message, systemPrompt string, enableCaching bool, cacheTTL string) ([]interface{}, error)

ConvertToNative converts unified messages to OpenAI format with enhanced caching

func (*OpenAIConverterImpl) GetModelFamily

func (c *OpenAIConverterImpl) GetModelFamily(modelID string) string

func (*OpenAIConverterImpl) GetProviderName

func (c *OpenAIConverterImpl) GetProviderName() string

func (*OpenAIConverterImpl) SupportsNativeStorage

func (c *OpenAIConverterImpl) SupportsNativeStorage() bool

type OpenAIProvider

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

OpenAIProvider implements the Provider interface for OpenAI

func NewOpenAIProvider

func NewOpenAIProvider(apiKey string) *OpenAIProvider

NewOpenAIProvider creates a new OpenAI provider

func (*OpenAIProvider) CreateClient

func (p *OpenAIProvider) CreateClient(modelID string) (Client, error)

func (*OpenAIProvider) GetName

func (p *OpenAIProvider) GetName() string

func (*OpenAIProvider) ListModels

func (p *OpenAIProvider) ListModels(ctx context.Context) ([]*ModelInfo, error)

func (*OpenAIProvider) ValidateAPIKey

func (p *OpenAIProvider) ValidateAPIKey(ctx context.Context) error

type OpenAIWebSocketClient

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

OpenAIWebSocketClient implements Client using OpenAI's realtime websocket endpoint.

func NewOpenAIWebSocketClientWithBaseURL

func NewOpenAIWebSocketClientWithBaseURL(apiKey, modelName, baseURL string) (*OpenAIWebSocketClient, error)

func (*OpenAIWebSocketClient) Complete

func (c *OpenAIWebSocketClient) Complete(ctx context.Context, prompt string) (string, error)

func (*OpenAIWebSocketClient) CompleteWithRequest

func (c *OpenAIWebSocketClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*OpenAIWebSocketClient) GetLastResponseID

func (c *OpenAIWebSocketClient) GetLastResponseID() string

func (*OpenAIWebSocketClient) GetModelName

func (c *OpenAIWebSocketClient) GetModelName() string

func (*OpenAIWebSocketClient) SetPreviousResponseID

func (c *OpenAIWebSocketClient) SetPreviousResponseID(responseID string)

func (*OpenAIWebSocketClient) Stream

func (c *OpenAIWebSocketClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type OpenRouterClient

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

OpenRouterClient implements the Client interface using the native OpenRouter API.

func (*OpenRouterClient) Complete

func (c *OpenRouterClient) Complete(ctx context.Context, prompt string) (string, error)

func (*OpenRouterClient) CompleteWithRequest

func (c *OpenRouterClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*OpenRouterClient) GetLastResponseID

func (c *OpenRouterClient) GetLastResponseID() string

GetLastResponseID returns the last response ID for prompt caching

func (*OpenRouterClient) GetModelName

func (c *OpenRouterClient) GetModelName() string

func (*OpenRouterClient) SetPreviousResponseID

func (c *OpenRouterClient) SetPreviousResponseID(responseID string)

SetPreviousResponseID sets the previous response ID for the next request

func (*OpenRouterClient) Stream

func (c *OpenRouterClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

type OpenRouterConverterImpl

type OpenRouterConverterImpl struct{}

OpenRouterConverterImpl implements NativeConverter for OpenRouter models

func (*OpenRouterConverterImpl) ConvertFromNative

func (c *OpenRouterConverterImpl) ConvertFromNative(native []interface{}) ([]*Message, error)

func (*OpenRouterConverterImpl) ConvertToNative

func (c *OpenRouterConverterImpl) ConvertToNative(messages []*Message, systemPrompt string, enableCaching bool, cacheTTL string) ([]interface{}, error)

func (*OpenRouterConverterImpl) GetModelFamily

func (c *OpenRouterConverterImpl) GetModelFamily(modelID string) string

func (*OpenRouterConverterImpl) GetProviderName

func (c *OpenRouterConverterImpl) GetProviderName() string

func (*OpenRouterConverterImpl) SupportsNativeStorage

func (c *OpenRouterConverterImpl) SupportsNativeStorage() bool

type OpenRouterProvider

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

OpenRouterProvider implements the Provider interface for OpenRouter

func NewOpenRouterProvider

func NewOpenRouterProvider(apiKey string) *OpenRouterProvider

NewOpenRouterProvider creates a new OpenRouter provider instance

func (*OpenRouterProvider) CreateClient

func (p *OpenRouterProvider) CreateClient(modelID string) (Client, error)

func (*OpenRouterProvider) GetName

func (p *OpenRouterProvider) GetName() string

func (*OpenRouterProvider) ListModels

func (p *OpenRouterProvider) ListModels(ctx context.Context) ([]*ModelInfo, error)

func (*OpenRouterProvider) ValidateAPIKey

func (p *OpenRouterProvider) ValidateAPIKey(ctx context.Context) error

type PrefixPattern

type PrefixPattern struct {
	Prefix string
	Value  int
}

PrefixPattern represents a pattern with a prefix and associated value

type Pricing

type Pricing struct {
	Prompt     string `json:"prompt,omitempty"`     // Cost per prompt token
	Completion string `json:"completion,omitempty"` // Cost per completion token
}

Pricing represents pricing information per token

type PromptBuilder

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

PromptBuilder builds system prompts for the LLM

func NewPromptBuilder

func NewPromptBuilder(filesystem fs.FileSystem, workingDir string, cfg *config.Config) *PromptBuilder

func (*PromptBuilder) BuildSystemPrompt

func (pb *PromptBuilder) BuildSystemPrompt(ctx context.Context, modelName string, cliMode bool, availableTools []map[string]interface{}) (string, error)

BuildSystemPrompt builds the system prompt including AGENTS.md and model-specific guidance

type Provider

type Provider interface {
	// GetName returns the provider name (e.g., "openai", "anthropic")
	GetName() string

	// ListModels lists all available models from the provider
	// Returns models that support the current API version
	ListModels(ctx context.Context) ([]*ModelInfo, error)

	// CreateClient creates a new LLM client for the specified model
	CreateClient(modelID string) (Client, error)

	// ValidateAPIKey tests if the API key is valid
	ValidateAPIKey(ctx context.Context) error
}

Provider is the interface for LLM providers

type ZaiClient

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

ZaiClient implements the Client interface using the native Z.AI Chat Completions API. It supports the thinking parameter for chain-of-thought reasoning and streaming tool calls.

func (*ZaiClient) Complete

func (c *ZaiClient) Complete(ctx context.Context, prompt string) (string, error)

func (*ZaiClient) CompleteWithRequest

func (c *ZaiClient) CompleteWithRequest(ctx context.Context, req *CompletionRequest) (*CompletionResponse, error)

func (*ZaiClient) GetLastResponseID

func (c *ZaiClient) GetLastResponseID() string

func (*ZaiClient) GetModelName

func (c *ZaiClient) GetModelName() string

func (*ZaiClient) SetPreviousResponseID

func (c *ZaiClient) SetPreviousResponseID(responseID string)

func (*ZaiClient) Stream

func (c *ZaiClient) Stream(ctx context.Context, req *CompletionRequest, callback func(chunk string) error) error

Jump to

Keyboard shortcuts

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