provider

package
v1.1.32 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DiscoverModels added in v1.0.12

func DiscoverModels(ctx context.Context, resolved *config.ResolvedEndpoint) ([]string, error)

DiscoverModels fetches the latest model list for a resolved endpoint when the remote API exposes it.

func IsImageBlockFallbackCandidate added in v1.1.17

func IsImageBlockFallbackCandidate(err error) bool

IsImageBlockFallbackCandidate reports whether an image-bearing request should be retried without image blocks. We intentionally gate on HTTP 400 only and only for requests that already attempted to send image content.

Types

type AnthropicProvider

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

AnthropicProvider implements Provider using the Anthropic SDK.

func NewAnthropicProvider

func NewAnthropicProvider(apiKey string, model string, maxTokens int) *AnthropicProvider

NewAnthropicProvider creates a new Anthropic provider.

func NewAnthropicProviderWithBaseURL

func NewAnthropicProviderWithBaseURL(apiKey string, model string, maxTokens int, baseURL string) *AnthropicProvider

NewAnthropicProviderWithBaseURL creates a new Anthropic provider with a custom base URL.

func (*AnthropicProvider) Chat

func (p *AnthropicProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition) (*ChatResponse, error)

func (*AnthropicProvider) ChatStream

func (p *AnthropicProvider) ChatStream(ctx context.Context, messages []Message, tools []ToolDefinition) (<-chan StreamEvent, error)

func (*AnthropicProvider) CountTokens

func (p *AnthropicProvider) CountTokens(ctx context.Context, messages []Message) (int, error)

func (*AnthropicProvider) Name

func (p *AnthropicProvider) Name() string

type ChatResponse

type ChatResponse struct {
	Message Message
	Usage   TokenUsage
}

ChatResponse is the complete response from a non-streaming Chat call.

type ContentBlock

type ContentBlock struct {
	Type      string          `json:"type"` // "text", "image", "tool_use", "tool_result"
	Text      string          `json:"text,omitempty"`
	ImageMIME string          `json:"image_mime,omitempty"` // MIME type for image blocks
	ImageData string          `json:"image_data,omitempty"` // base64-encoded image data
	ToolName  string          `json:"tool_name,omitempty"`
	ToolID    string          `json:"tool_id,omitempty"`
	Input     json.RawMessage `json:"input,omitempty"`
	Output    string          `json:"output,omitempty"`
	IsError   bool            `json:"is_error,omitempty"`
}

ContentBlock is a union type: text, image, tool call, or tool result.

func ImageBlock

func ImageBlock(mime, base64Data string) ContentBlock

ImageBlock creates an image content block with base64-encoded data.

func TextBlock

func TextBlock(text string) ContentBlock

TextBlock creates a text content block.

func ToolResultBlock

func ToolResultBlock(id, output string, isError bool) ContentBlock

ToolResultBlock creates a tool result content block.

func ToolResultNamedBlock added in v1.1.14

func ToolResultNamedBlock(id, name, output string, isError bool) ContentBlock

ToolResultNamedBlock creates a tool result content block with the originating tool name.

func ToolUseBlock

func ToolUseBlock(id, name string, input json.RawMessage) ContentBlock

ToolUseBlock creates a tool call content block.

type CopilotProvider added in v1.1.16

type CopilotProvider struct {
	*OpenAIProvider
}

func NewCopilotProvider added in v1.1.16

func NewCopilotProvider(apiKey, model string, maxTokens int, baseURL string) *CopilotProvider

func (*CopilotProvider) Name added in v1.1.16

func (p *CopilotProvider) Name() string

type GeminiProvider

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

GeminiProvider implements Provider using the Google Generative AI API.

func NewGeminiProvider

func NewGeminiProvider(apiKey string, model string, maxTokens int) (*GeminiProvider, error)

NewGeminiProvider creates a new Gemini provider.

func NewGeminiProviderWithBaseURL added in v1.1.14

func NewGeminiProviderWithBaseURL(apiKey string, model string, maxTokens int, baseURL string) (*GeminiProvider, error)

NewGeminiProviderWithBaseURL creates a new Gemini provider with a custom base URL.

func (*GeminiProvider) Chat

func (p *GeminiProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition) (*ChatResponse, error)

func (*GeminiProvider) ChatStream

func (p *GeminiProvider) ChatStream(ctx context.Context, messages []Message, tools []ToolDefinition) (<-chan StreamEvent, error)

func (*GeminiProvider) CountTokens

func (p *GeminiProvider) CountTokens(ctx context.Context, messages []Message) (int, error)

func (*GeminiProvider) Name

func (p *GeminiProvider) Name() string

type Message

type Message struct {
	Role    string         `json:"role"` // "user", "assistant", "system"
	Content []ContentBlock `json:"content"`
}

Message represents a single message in the conversation.

type OpenAIProvider

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

OpenAIProvider implements Provider using the OpenAI-compatible API.

func NewOpenAIProvider

func NewOpenAIProvider(apiKey string, model string, maxTokens int) *OpenAIProvider

NewOpenAIProvider creates a new OpenAI provider.

func NewOpenAIProviderWithBaseURL

func NewOpenAIProviderWithBaseURL(apiKey string, model string, maxTokens int, baseURL string) *OpenAIProvider

NewOpenAIProviderWithBaseURL creates a new OpenAI provider with a custom base URL.

func NewOpenAIProviderWithConfig added in v1.1.16

func NewOpenAIProviderWithConfig(config openai.ClientConfig, model string, maxTokens int, name string) *OpenAIProvider

func (*OpenAIProvider) Chat

func (p *OpenAIProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition) (*ChatResponse, error)

func (*OpenAIProvider) ChatStream

func (p *OpenAIProvider) ChatStream(ctx context.Context, messages []Message, tools []ToolDefinition) (<-chan StreamEvent, error)

func (*OpenAIProvider) CountTokens

func (p *OpenAIProvider) CountTokens(ctx context.Context, messages []Message) (int, error)

func (*OpenAIProvider) Name

func (p *OpenAIProvider) Name() string

type Provider

type Provider interface {
	// Name returns the provider identifier (e.g., "anthropic", "openai", "gemini").
	Name() string

	// Chat sends a non-streaming request and returns the complete response.
	// Used for token counting, summarization, and cost estimation.
	Chat(ctx context.Context, messages []Message, tools []ToolDefinition) (*ChatResponse, error)

	// ChatStream sends a streaming request and returns a channel of events.
	// The channel is closed when the stream ends.
	ChatStream(ctx context.Context, messages []Message, tools []ToolDefinition) (<-chan StreamEvent, error)

	// CountTokens returns the token count for the given messages.
	// Returns an error if the provider does not support counting.
	CountTokens(ctx context.Context, messages []Message) (int, error)
}

Provider is the interface every LLM backend must implement.

func NewProvider

func NewProvider(resolved *config.ResolvedEndpoint) (Provider, error)

NewProvider creates a protocol adapter from a resolved endpoint.

type StreamEvent

type StreamEvent struct {
	Type    StreamEventType
	Text    string        // for TextChunk
	Tool    ToolCallDelta // for ToolCallChunk / ToolCallDone
	Result  string        // for ToolResult
	IsError bool          // for ToolResult
	Usage   *TokenUsage   // for Done (nil if not final)
	Error   error         // for Error
}

StreamEvent is sent over a channel during streaming responses.

type StreamEventType

type StreamEventType int
const (
	StreamEventText StreamEventType = iota
	StreamEventToolCallChunk
	StreamEventToolCallDone
	StreamEventToolResult
	StreamEventDone
	StreamEventError
)

type TokenUsage

type TokenUsage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
	CacheRead    int `json:"cache_read_tokens"`
	CacheWrite   int `json:"cache_write_tokens"`
}

TokenUsage records token consumption for a single API call.

type ToolCallDelta

type ToolCallDelta struct {
	ID        string          // tool call ID (stable across chunks)
	Index     int             // position in the tool call list
	Name      string          // tool name (may be empty in early chunks)
	Arguments json.RawMessage // accumulated arguments so far
}

ToolCallDelta represents a (possibly partial) tool call from a streaming response.

type ToolDefinition

type ToolDefinition struct {
	Name        string          `json:"name"`
	Description string          `json:"description"`
	Parameters  json.RawMessage `json:"parameters"` // JSON Schema
}

ToolDefinition describes a tool to the LLM provider.

Jump to

Keyboard shortcuts

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