adapters

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const ModelListCap = 10

ModelListCap is the maximum number of models shown per provider in the /models listing. When a provider has more, the first ModelListCap are shown followed by a "… and N more" notice.

Variables

This section is empty.

Functions

func BuildErrorResponse

func BuildErrorResponse(modelID, qualifiedID string, start time.Time, totalInput, totalOutput, totalReasoning int64, err error) models.ModelResponse

BuildErrorResponse constructs a ModelResponse for an API error encountered mid-loop. qualifiedID is the provider-prefixed model ID passed to CostUSD (e.g. "anthropic/claude-sonnet-4-6"); pass the bare modelID for OpenRouter/LiteLLM.

func BuildInterruptedResponse

func BuildInterruptedResponse(modelID, qualifiedID string, textParts []string,
	start time.Time, totalInput, totalOutput, totalReasoning int64,
	proposed []tools.FileWrite, toolCalls map[string]int, err error) models.ModelResponse

BuildInterruptedResponse constructs a ModelResponse for a run that was cancelled mid-flight. It preserves the partial text, proposed writes, and token counts accumulated before cancellation.

func BuildMaxStepsResponse

func BuildMaxStepsResponse(modelID, qualifiedID string, textParts []string, start time.Time,
	totalInput, totalOutput, totalReasoning int64,
	proposed []tools.FileWrite, toolCalls map[string]int) models.ModelResponse

BuildMaxStepsResponse constructs a ModelResponse when the agentic loop hit the max_steps limit.

func BuildSuccessResponse

func BuildSuccessResponse(modelID, qualifiedID string, textParts []string, start time.Time,
	totalInput, totalOutput, totalReasoning int64,
	proposed []tools.FileWrite, toolCalls map[string]int) models.ModelResponse

BuildSuccessResponse constructs a ModelResponse after a completed agentic loop. qualifiedID is the provider-prefixed model ID passed to CostUSD.

func DebugRequestsFromContext

func DebugRequestsFromContext(ctx context.Context) bool

DebugRequestsFromContext reports whether raw API request logging is enabled.

func DispatchTool

func DispatchTool(
	ctx context.Context,
	name string,
	args map[string]string,
	onEvent func(models.AgentEvent),
	proposed *[]tools.FileWrite,
	toolCalls *map[string]int,
) (result string, ok bool)

DispatchTool executes a tool call by name. It emits the appropriate AgentEvent, executes the tool, and returns the result string for the adapter to feed back to the model. Returns ("", false) for unrecognised tool names.

ctx is checked for MCP dispatchers (registered at startup via tools.WithMCPDispatchers) which take priority over built-in tool names.

toolCalls, if non-nil, is incremented for the tool name on successful dispatch.

func EmitRequest

func EmitRequest(ctx context.Context, onEvent func(models.AgentEvent), params any)

EmitRequest JSON-marshals params and emits it as an EventRequest event. Only emits when debug request logging is enabled in the context, avoiding serialization overhead in production.

func EmitSnapshot

func EmitSnapshot(onEvent func(models.AgentEvent), qualifiedID string,
	textParts []string, start time.Time, totalInput, totalOutput, totalReasoning int64,
	proposed []tools.FileWrite, toolCalls map[string]int)

EmitSnapshot sends a "snapshot" event with the current turn state for incremental checkpoint persistence. Called at each turn boundary inside the agentic loop.

func ListAdapters

func ListAdapters(cfg config.Config) ([]models.ModelAdapter, []string)

ListAdapters builds adapters for all resolved active models. Returns the adapter slice and a list of warning strings for skipped models.

func NewAdapter

func NewAdapter(modelID string, cfg config.Config) (models.ModelAdapter, error)

NewAdapter returns a ModelAdapter for the given model ID using cfg for API keys.

Routing rules (checked in order):

  1. "litellm/<model>" → LiteLLMAdapter (requires LITELLM_BASE_URL)
  2. "bedrock/<model>" → BedrockAdapter (requires AWS_REGION)
  3. "azure/<model>" → AzureOpenAIAdapter (requires AZURE_OPENAI_API_KEY + AZURE_OPENAI_ENDPOINT)
  4. "vertex/<model>" → VertexAIAdapter (requires VERTEX_AI_PROJECT + VERTEX_AI_LOCATION)
  5. "provider/model" → OpenRouterAdapter (requires OPENROUTER_API_KEY)
  6. "claude*" → AnthropicAdapter
  7. "gpt-*", "o1", "o3*" → OpenAIAdapter
  8. "gemini*" → GeminiAdapter

func NewAdapterForProvider

func NewAdapterForProvider(modelID, provider string, cfg config.Config) (models.ModelAdapter, error)

NewAdapterForProvider creates an adapter using an explicit provider name (e.g. "Anthropic", "OpenAI", "Gemini") rather than inferring from the model ID prefix. Falls back to NewAdapter for unrecognised providers (OpenRouter, LiteLLM, custom).

func WithDebugRequests

func WithDebugRequests(ctx context.Context) context.Context

WithDebugRequests returns a context that enables raw API request logging.

Types

type AnthropicAdapter

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

AnthropicAdapter implements ModelAdapter for Anthropic Claude models.

func NewAnthropicAdapter

func NewAnthropicAdapter(modelID, apiKey string) *AnthropicAdapter

NewAnthropicAdapter creates an AnthropicAdapter.

func (*AnthropicAdapter) Capabilities

func (*AnthropicAdapter) ID

func (a *AnthropicAdapter) ID() string

func (*AnthropicAdapter) RunAgent

func (a *AnthropicAdapter) RunAgent(
	ctx context.Context,
	history []models.ConversationTurn,
	prompt string,
	onEvent func(models.AgentEvent),
) (models.ModelResponse, error)

type AzureOpenAIAdapter

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

AzureOpenAIAdapter implements ModelAdapter for Azure OpenAI Service.

Model IDs are configured with an "azure/" prefix (e.g. "azure/gpt-4o"). The prefix is stripped before the API call; the bare portion is used as the Azure deployment name. The full prefixed ID is preserved for display and logging.

Set AZURE_OPENAI_API_KEY and AZURE_OPENAI_ENDPOINT to enable this adapter. AZURE_OPENAI_API_VERSION defaults to "2024-10-21".

func NewAzureOpenAIAdapter

func NewAzureOpenAIAdapter(modelID, apiKey, endpoint, apiVersion string) *AzureOpenAIAdapter

NewAzureOpenAIAdapter creates an AzureOpenAIAdapter.

func (*AzureOpenAIAdapter) Capabilities

func (*AzureOpenAIAdapter) ID

func (a *AzureOpenAIAdapter) ID() string

func (*AzureOpenAIAdapter) RunAgent

func (a *AzureOpenAIAdapter) RunAgent(
	ctx context.Context,
	history []models.ConversationTurn,
	prompt string,
	onEvent func(models.AgentEvent),
) (models.ModelResponse, error)

type BedrockAdapter

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

BedrockAdapter implements ModelAdapter for Amazon Bedrock using the Converse API.

Model IDs are configured with a "bedrock/" prefix (e.g. "bedrock/anthropic.claude-sonnet-4-20250514-v1:0"). The prefix is stripped before the API call; the full prefixed ID is preserved for display and logging.

Authentication uses the AWS SDK default credential chain:

  • AWS_ACCESS_KEY_ID + AWS_SECRET_ACCESS_KEY environment variables
  • AWS_PROFILE for named profiles in ~/.aws/credentials
  • EC2/ECS/Lambda instance roles

Set AWS_REGION (or AWS_DEFAULT_REGION) to specify the Bedrock endpoint region.

func NewBedrockAdapter

func NewBedrockAdapter(modelID, region string) *BedrockAdapter

NewBedrockAdapter creates a BedrockAdapter.

func (*BedrockAdapter) Capabilities

Capabilities infers defaults from the sub-provider in the Bedrock model ID (e.g. "anthropic.claude-*" → anthropic defaults).

func (*BedrockAdapter) ID

func (a *BedrockAdapter) ID() string

func (*BedrockAdapter) RunAgent

func (a *BedrockAdapter) RunAgent(
	ctx context.Context,
	history []models.ConversationTurn,
	prompt string,
	onEvent func(models.AgentEvent),
) (models.ModelResponse, error)

type GeminiAdapter

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

GeminiAdapter implements ModelAdapter for Gemini models.

func NewGeminiAdapter

func NewGeminiAdapter(modelID, apiKey string) *GeminiAdapter

NewGeminiAdapter creates a GeminiAdapter.

func (*GeminiAdapter) Capabilities

func (a *GeminiAdapter) Capabilities(ctx context.Context) models.ModelCapabilities

Capabilities queries the Gemini models API for context/output token limits, falling back to hardcoded defaults on error.

func (*GeminiAdapter) ID

func (a *GeminiAdapter) ID() string

func (*GeminiAdapter) RunAgent

func (a *GeminiAdapter) RunAgent(
	ctx context.Context,
	history []models.ConversationTurn,
	prompt string,
	onEvent func(models.AgentEvent),
) (models.ModelResponse, error)

type LiteLLMAdapter

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

LiteLLMAdapter implements ModelAdapter for a LiteLLM proxy using its OpenAI-compatible API.

Model IDs are configured with a "litellm/" prefix to distinguish them from native adapters (e.g. "litellm/claude-sonnet-4-6"). The prefix is stripped before the API call; the full prefixed ID is preserved for display and logging.

Set LITELLM_BASE_URL to the proxy base URL including the /v1 path (e.g. "http://localhost:4000/v1"). LITELLM_API_KEY is optional.

func NewLiteLLMAdapter

func NewLiteLLMAdapter(modelID, apiKey, baseURL string) *LiteLLMAdapter

NewLiteLLMAdapter creates a LiteLLMAdapter.

func (*LiteLLMAdapter) Capabilities

func (*LiteLLMAdapter) ID

func (a *LiteLLMAdapter) ID() string

func (*LiteLLMAdapter) RunAgent

func (a *LiteLLMAdapter) RunAgent(
	ctx context.Context,
	history []models.ConversationTurn,
	prompt string,
	onEvent func(models.AgentEvent),
) (models.ModelResponse, error)

type OpenAIAdapter

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

OpenAIAdapter implements ModelAdapter for OpenAI models.

func NewOpenAIAdapter

func NewOpenAIAdapter(modelID, apiKey string) *OpenAIAdapter

NewOpenAIAdapter creates an OpenAIAdapter.

func (*OpenAIAdapter) Capabilities

func (*OpenAIAdapter) ID

func (a *OpenAIAdapter) ID() string

func (*OpenAIAdapter) RunAgent

func (a *OpenAIAdapter) RunAgent(
	ctx context.Context,
	history []models.ConversationTurn,
	prompt string,
	onEvent func(models.AgentEvent),
) (models.ModelResponse, error)

type OpenRouterAdapter

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

OpenRouterAdapter implements ModelAdapter using OpenRouter's OpenAI-compatible API. Model IDs are in OpenRouter's "provider/model" format (e.g. "anthropic/claude-sonnet-4-6").

func NewOpenRouterAdapter

func NewOpenRouterAdapter(modelID, apiKey string) *OpenRouterAdapter

NewOpenRouterAdapter creates an OpenRouterAdapter.

func (*OpenRouterAdapter) Capabilities

Capabilities infers capabilities from the sub-provider in the model ID (e.g. "anthropic/claude-sonnet-4-6" → anthropic defaults) and uses the context window from pricing data when available.

func (*OpenRouterAdapter) ID

func (a *OpenRouterAdapter) ID() string

func (*OpenRouterAdapter) RunAgent

func (a *OpenRouterAdapter) RunAgent(
	ctx context.Context,
	history []models.ConversationTurn,
	prompt string,
	onEvent func(models.AgentEvent),
) (models.ModelResponse, error)

type ProviderModels

type ProviderModels struct {
	Provider   string
	Models     []string
	TotalCount int
	Err        error
}

ProviderModels holds the result of querying one provider for its model catalogue. TotalCount is the number of models returned by the API before any chat filter is applied. For providers that return all models unfiltered (Anthropic, OpenRouter, LiteLLM), TotalCount == len(Models). For filtered providers (OpenAI, Gemini), TotalCount > len(Models) when non-chat models were dropped.

func ListAvailableModels

func ListAvailableModels(ctx context.Context, cfg config.Config) []ProviderModels

ListAvailableModels concurrently queries each configured provider for its available model IDs. Results are returned in a consistent provider order (Anthropic, OpenAI, Gemini, OpenRouter, LiteLLM); only providers whose keys are set are included.

type VertexAIAdapter

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

VertexAIAdapter implements ModelAdapter for Google Vertex AI (Gemini models).

Model IDs are configured with a "vertex/" prefix (e.g. "vertex/gemini-2.0-flash"). The prefix is stripped before the API call; the full prefixed ID is preserved for display and logging.

Authentication uses Application Default Credentials (ADC):

  • gcloud auth application-default login (local development)
  • GOOGLE_APPLICATION_CREDENTIALS (service account key file)
  • GCE/GKE/Cloud Run metadata server

Set VERTEX_AI_PROJECT and VERTEX_AI_LOCATION to enable this adapter.

func NewVertexAIAdapter

func NewVertexAIAdapter(modelID, project, location string) *VertexAIAdapter

NewVertexAIAdapter creates a VertexAIAdapter.

func (*VertexAIAdapter) Capabilities

Capabilities queries the Vertex AI models API for context/output token limits, falling back to hardcoded defaults on error.

func (*VertexAIAdapter) ID

func (a *VertexAIAdapter) ID() string

func (*VertexAIAdapter) RunAgent

func (a *VertexAIAdapter) RunAgent(
	ctx context.Context,
	history []models.ConversationTurn,
	prompt string,
	onEvent func(models.AgentEvent),
) (models.ModelResponse, error)

Jump to

Keyboard shortcuts

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