llm

package
v0.0.0-...-391baa6 Latest Latest
Warning

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

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

Documentation

Overview

Package llm defines the pluggable LLM provider interface and message types.

Index

Constants

View Source
const (
	RoleUser      = "user"
	RoleAssistant = "assistant"

	BlockText       = "text"
	BlockToolUse    = "tool_use"
	BlockToolResult = "tool_result"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int
	Body       string
}

APIError represents an HTTP error from an LLM API.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) Retryable

func (e *APIError) Retryable() bool

Retryable returns true for transient HTTP status codes (429, 500, 502, 503, 529).

type Anthropic

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

Anthropic implements Provider using the Anthropic Messages API.

func NewAnthropic

func NewAnthropic(cfg config.ProviderConfig) (*Anthropic, error)

NewAnthropic creates an Anthropic provider from the given config.

func (*Anthropic) Complete

func (a *Anthropic) Complete(ctx context.Context, req *Request) (*Response, error)

type ContentBlock

type ContentBlock struct {
	Type    string          `json:"type"`
	Text    string          `json:"text,omitempty"`
	ID      string          `json:"id,omitempty"`
	Name    string          `json:"name,omitempty"`
	Input   json.RawMessage `json:"input,omitempty"`
	Content string          `json:"content,omitempty"`
	IsError bool            `json:"is_error,omitempty"`
}

ContentBlock is a union type discriminated by Type:

  • "text": Text is set
  • "tool_use": ID (call ID), Name, and Input are set
  • "tool_result": ID (matching tool_use call ID), Content, and IsError are set

type LoggingProvider

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

LoggingProvider wraps a Provider and writes every request/response exchange as JSONL to a file for prompt diagnostics.

func NewLoggingProvider

func NewLoggingProvider(inner Provider, dir string) (*LoggingProvider, error)

NewLoggingProvider creates a provider that logs all exchanges to a JSONL file in the given directory. The file is named with a timestamp.

func (*LoggingProvider) Complete

func (l *LoggingProvider) Complete(ctx context.Context, req *Request) (*Response, error)

func (*LoggingProvider) Path

func (l *LoggingProvider) Path() string

Path returns the log file path.

type Message

type Message struct {
	Role    string         `json:"role"`
	Content []ContentBlock `json:"content"`
}

Message is a single message in a conversation.

func NewTextMessage

func NewTextMessage(role, text string) Message

NewTextMessage creates a simple text message.

func NewToolResultMessage

func NewToolResultMessage(toolUseID, content string, isError bool) Message

NewToolResultMessage creates a tool_result message.

type OpenAI

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

OpenAI implements Provider using the OpenAI Chat Completions API.

func NewOpenAI

func NewOpenAI(cfg config.ProviderConfig) (*OpenAI, error)

NewOpenAI creates an OpenAI provider from the given config.

func (*OpenAI) Complete

func (o *OpenAI) Complete(ctx context.Context, req *Request) (*Response, error)

type Provider

type Provider interface {
	Complete(ctx context.Context, req *Request) (*Response, error)
}

Provider is the interface for LLM backends that support tool use.

func NewProvider

func NewProvider(cfg config.ProviderConfig) (Provider, error)

NewProvider creates a Provider from the given config. The returned provider is wrapped with retry logic for transient errors.

type Request

type Request struct {
	System    string
	Messages  []Message
	Tools     []ToolDef
	MaxTokens int
}

Request is a completion request sent to the provider.

type Response

type Response struct {
	Content    []ContentBlock
	StopReason StopReason
	Usage      Usage
}

Response is the model's response to a completion request.

func (*Response) TextContent

func (r *Response) TextContent() string

TextContent returns the concatenated text from all text blocks.

func (*Response) ToolUseBlocks

func (r *Response) ToolUseBlocks() []ContentBlock

ToolUseBlocks returns only the tool_use content blocks from a response.

type RetryProvider

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

RetryProvider wraps a Provider with exponential backoff retry for transient errors.

func NewRetryProvider

func NewRetryProvider(inner Provider, maxRetries int, baseDelay time.Duration) *RetryProvider

NewRetryProvider wraps inner with retry logic. maxRetries is the number of retries after the first attempt (so total attempts = maxRetries + 1).

func (*RetryProvider) Complete

func (r *RetryProvider) Complete(ctx context.Context, req *Request) (*Response, error)

type StopReason

type StopReason string

StopReason indicates why the model stopped generating.

const (
	StopEndTurn   StopReason = "end_turn"
	StopToolUse   StopReason = "tool_use"
	StopMaxTokens StopReason = "max_tokens"
)

type ToolDef

type ToolDef struct {
	Name        string          `json:"name"`
	Description string          `json:"description"`
	InputSchema json.RawMessage `json:"input_schema"`
}

ToolDef defines a tool the LLM can call.

type Usage

type Usage struct {
	InputTokens  int
	OutputTokens int
}

Usage tracks token consumption.

Jump to

Keyboard shortcuts

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