model

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// MiddlewareStateKey exposes the context key so other packages can attach middleware state.
	MiddlewareStateKey = middlewareStateKey
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AnthropicConfig

type AnthropicConfig struct {
	APIKey      string
	BaseURL     string
	Model       string
	MaxTokens   int
	MaxRetries  int
	System      string
	Temperature *float64
	HTTPClient  *http.Client
}

AnthropicConfig wires a plain anthropic-sdk-go client into the Model interface.

type AnthropicProvider

type AnthropicProvider struct {
	APIKey      string
	BaseURL     string
	ModelName   string
	MaxTokens   int
	MaxRetries  int
	System      string
	Temperature *float64
	CacheTTL    time.Duration
	// contains filtered or unexported fields
}

AnthropicProvider caches anthropic clients with optional TTL.

func (*AnthropicProvider) Model

func (p *AnthropicProvider) Model(ctx context.Context) (Model, error)

Model implements Provider with caching using double-checked locking.

type ContentBlock

type ContentBlock struct {
	Type      ContentBlockType `json:"type"`
	Text      string           `json:"text,omitempty"`
	MediaType string           `json:"media_type,omitempty"` // e.g. "image/jpeg", "application/pdf"
	Data      string           `json:"data,omitempty"`       // base64-encoded content
	URL       string           `json:"url,omitempty"`        // URL source (alternative to Data)
}

ContentBlock represents a single piece of content within a message. For text blocks only Text is populated. For image/document blocks, MediaType and either Data (base64) or URL are set.

type ContentBlockType

type ContentBlockType string

ContentBlockType discriminates the kind of content in a ContentBlock.

const (
	ContentBlockText     ContentBlockType = "text"
	ContentBlockImage    ContentBlockType = "image"
	ContentBlockDocument ContentBlockType = "document"
)

type Message

type Message struct {
	Role             string
	Content          string
	ContentBlocks    []ContentBlock // Multimodal content; takes precedence over Content when non-empty
	ToolCalls        []ToolCall
	ReasoningContent string // For thinking models (e.g. DeepSeek, Kimi k2.5)
}

Message represents a single conversational turn. Tool calls emitted by the assistant are kept on ToolCalls. When ContentBlocks is non-empty it takes precedence over Content for multimodal messages (images, documents). Existing text-only callers continue to use Content unchanged.

func (Message) TextContent

func (m Message) TextContent() string

TextContent returns the text portion of the message. When ContentBlocks is populated it concatenates all text blocks; otherwise it falls back to Content.

type MiddlewareState

type MiddlewareState interface {
	SetModelInput(any)
	SetModelOutput(any)
	SetValue(string, any)
}

MiddlewareState is the minimal contract required for model providers to surface request/response data to middleware consumers without depending on the middleware package (which would cause an import cycle).

type Model

type Model interface {
	Complete(ctx context.Context, req Request) (*Response, error)
	CompleteStream(ctx context.Context, req Request, cb StreamHandler) error
}

Model is the provider-agnostic interface used by the agent runtime.

func MustProvider

func MustProvider(p Provider) Model

MustProvider materialises a model immediately and panics on failure.

func NewAnthropic

func NewAnthropic(cfg AnthropicConfig) (Model, error)

NewAnthropic constructs a production-ready Anthropic-backed Model.

func NewOpenAI

func NewOpenAI(cfg OpenAIConfig) (Model, error)

NewOpenAI constructs a production-ready OpenAI-backed Model.

func NewOpenAIResponses

func NewOpenAIResponses(cfg OpenAIConfig) (Model, error)

NewOpenAIResponses constructs an OpenAI model using the Responses API.

type OpenAIConfig

type OpenAIConfig struct {
	APIKey       string
	BaseURL      string // Optional: for Azure or proxies
	Model        string // e.g., "gpt-4o", "gpt-4-turbo"
	MaxTokens    int
	MaxRetries   int
	System       string
	Temperature  *float64
	HTTPClient   *http.Client
	UseResponses bool // true = /responses API, false = /chat/completions
}

OpenAIConfig configures the OpenAI-backed Model.

type OpenAIProvider

type OpenAIProvider struct {
	APIKey      string
	BaseURL     string // Optional: for Azure or proxies
	ModelName   string
	MaxTokens   int
	MaxRetries  int
	System      string
	Temperature *float64
	CacheTTL    time.Duration
	// contains filtered or unexported fields
}

OpenAIProvider caches OpenAI clients with optional TTL.

func (*OpenAIProvider) Model

func (p *OpenAIProvider) Model(ctx context.Context) (Model, error)

Model implements Provider with caching using double-checked locking.

type Provider

type Provider interface {
	Model(ctx context.Context) (Model, error)
}

Provider gives runtime access to lazily-instantiated models.

type ProviderFunc

type ProviderFunc func(context.Context) (Model, error)

ProviderFunc is an adapter to allow use of ordinary functions as providers.

func (ProviderFunc) Model

func (fn ProviderFunc) Model(ctx context.Context) (Model, error)

Model implements Provider.

type Request

type Request struct {
	Messages          []Message
	Tools             []ToolDefinition
	System            string
	Model             string
	SessionID         string
	MaxTokens         int
	Temperature       *float64
	EnablePromptCache bool // Enable prompt caching for system and recent messages
}

Request drives a single model completion.

type Response

type Response struct {
	Message    Message
	Usage      Usage
	StopReason string
}

Response wraps the final assistant message and accounting.

type StreamHandler

type StreamHandler func(StreamResult) error

StreamHandler consumes streaming updates in order.

type StreamOnlyModel

type StreamOnlyModel struct {
	Inner Model
}

StreamOnlyModel wraps a Model so that Complete() internally uses CompleteStream() to collect the response. This works around API proxies that return empty tool_use.input in non-streaming mode but work correctly in streaming mode.

func NewStreamOnlyModel

func NewStreamOnlyModel(inner Model) *StreamOnlyModel

NewStreamOnlyModel returns a wrapper that forces all completions through the streaming path.

func (*StreamOnlyModel) Complete

func (s *StreamOnlyModel) Complete(ctx context.Context, req Request) (*Response, error)

Complete calls CompleteStream internally and assembles the final Response.

func (*StreamOnlyModel) CompleteStream

func (s *StreamOnlyModel) CompleteStream(ctx context.Context, req Request, cb StreamHandler) error

CompleteStream delegates directly to the inner model.

type StreamOnlyProvider

type StreamOnlyProvider struct {
	Inner Provider
}

StreamOnlyProvider wraps a Provider so that the Model it returns always routes Complete() through CompleteStream(). Use this when the upstream API proxy only returns correct tool_use.input in streaming mode.

func (*StreamOnlyProvider) Model

func (p *StreamOnlyProvider) Model(ctx context.Context) (Model, error)

Model implements Provider.

type StreamResult

type StreamResult struct {
	Delta    string
	ToolCall *ToolCall
	Final    bool
	Response *Response
}

StreamResult delivers incremental updates during streaming calls.

type ToolCall

type ToolCall struct {
	ID        string
	Name      string
	Arguments map[string]any
	Result    string // Result stores the execution result for this specific tool call
}

ToolCall captures a function-style invocation generated by the model.

type ToolDefinition

type ToolDefinition struct {
	Name        string
	Description string
	Parameters  map[string]any
}

ToolDefinition describes a callable function exposed to the model.

type Usage

type Usage struct {
	InputTokens         int
	OutputTokens        int
	TotalTokens         int
	CacheReadTokens     int
	CacheCreationTokens int
}

Usage reports token accounting for a completion.

Jump to

Keyboard shortcuts

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