ai

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ThinkingLevels = []string{"none", "off", "on", "minimal", "low", "medium", "high", "extra high", "max", "ultra"}

ThinkingLevels is the ordered list of thinking levels the user can cycle through with /thinking (no argument). "none" is the default: no thinking parameter is sent to the provider. "off" explicitly disables thinking. The rest enable thinking at increasing intensity.

Functions

func EncodeMessage

func EncodeMessage(msg Message) (string, []byte, error)

EncodeMessage serializes an ai.Message to its role discriminator and JSON payload. Used by the store extension for persistence and wire transfer.

func HTTPClient

func HTTPClient() *http.Client

HTTPClient returns the shared HTTP client. Extensions import this to make HTTP calls with the same timeout and proxy settings.

func NowISO

func NowISO() string

NowISO returns a UTC timestamp in ISO 8601 format.

func RetryHTTP

func RetryHTTP(req *http.Request) (*http.Response, error)

RetryHTTP runs req with exponential backoff on transient failures. Extensions import this for provider HTTP calls.

func SSEData

func SSEData(reader *bufio.Reader) (string, bool, error)

SSEData returns the payload of each `data:` line in an SSE stream, skipping comments, event/blank lines, and the trailing `[DONE]` sentinel. Extensions import this for parsing SSE responses.

func SetHTTPTimeout

func SetHTTPTimeout(seconds int)

SetHTTPTimeout updates the shared HTTP client's timeout. Called during config loading from gateway.Config.HTTPTimeout.

func ThinkingEnabled

func ThinkingEnabled(level string) bool

ThinkingEnabled returns true if the level enables thinking (anything except "none" and "off").

Types

type Assistant

type Assistant struct {
	Thinking  *string    `json:"thinking,omitempty"`
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
	Content   string     `json:"content"`
	Timestamp string     `json:"timestamp,omitempty"`
}

Assistant is the model's response in a turn.

func NewAssistant

func NewAssistant(content string) Assistant

NewAssistant creates an Assistant message with timestamp set. Thinking and tool calls are set by the caller.

type FakeProvider

type FakeProvider struct {

	// LastMessages records the messages passed to the most recent Stream
	// call, so tests can assert on the history the agent built up.
	LastMessages []Message
	// contains filtered or unexported fields
}

FakeProvider implements Provider and emits a scripted sequence of StreamEvents instead of calling a real API. It unlocks deterministic agent loop testing.

func NewFakeProvider

func NewFakeProvider(model string, script ...StreamEvent) *FakeProvider

NewFakeProvider creates a FakeProvider that replays script in order on each Stream call. A non-zero delay is applied before each event.

func NewFakeProviderScripts

func NewFakeProviderScripts(model string, scripts ...[]StreamEvent) *FakeProvider

NewFakeProviderScripts creates a FakeProvider that replays each script in order on successive Stream calls; the last script repeats. Use it for multi-turn loops where each turn needs different events.

func (*FakeProvider) Calls

func (p *FakeProvider) Calls() int

Calls returns the number of Stream calls made. Safe to read after the caller has drained a stream channel: the counter increments before the channel closes.

func (*FakeProvider) ListModels

func (p *FakeProvider) ListModels() ([]string, error)

ListModels returns a hardcoded list for testing.

func (*FakeProvider) ModelInfo added in v0.1.4

func (p *FakeProvider) ModelInfo() (ModelInfo, error)

ModelInfo returns a fixed context window for testing.

func (*FakeProvider) ModelName

func (p *FakeProvider) ModelName() string

ModelName returns the model name used by this provider.

func (*FakeProvider) SetModel added in v0.2.0

func (p *FakeProvider) SetModel(model string)

SetModel updates the model name for testing.

func (*FakeProvider) SetThinkingLevel

func (p *FakeProvider) SetThinkingLevel(level string)

SetThinkingLevel is a no-op for the fake provider.

func (*FakeProvider) Stream

func (p *FakeProvider) Stream(ctx context.Context, messages []Message, _ []ToolSchema) <-chan StreamEvent

Stream replays the scripted events on a channel, respecting context cancellation, and closes the channel when done. An empty script closes the channel immediately.

func (*FakeProvider) WithDelay

func (p *FakeProvider) WithDelay(delay time.Duration) *FakeProvider

WithDelay returns a copy of the provider that sleeps delay before emitting each event, so cancellation can be observed mid-stream.

type ImageContent

type ImageContent struct {
	MediaType string `json:"media_type"` // "image/png", "image/jpeg", etc.
	Base64    string `json:"base64"`     // base64-encoded image data (no data: prefix)
}

ImageContent carries a base64-encoded image with its MIME type. When present on a User message, providers serialize it as image content blocks alongside the text content.

type Message

type Message interface {
	// contains filtered or unexported methods
}

Message is the sealed interface for all message types. Consumers use type switches or type assertions to access concrete types.

func DecodeMessage

func DecodeMessage(role string, payload []byte) (Message, error)

DecodeMessage reconstructs a Message from a role discriminator and JSON payload. Inverse of EncodeMessage.

type ModelChange

type ModelChange struct {
	Model     string `json:"model"`
	Timestamp string `json:"timestamp,omitempty"`
}

ModelChange is a session entry recording a model switch. It is persisted to the store and replayed on resume to restore the model.

func NewModelChange

func NewModelChange(model string) ModelChange

NewModelChange creates a ModelChange with timestamp set.

type ModelInfo added in v0.1.4

type ModelInfo struct {
	ContextWindow int // max context in tokens, 0 if unknown
}

ModelInfo holds metadata about the current model, queried from the provider when available. Zero values mean the provider does not expose that field; callers fall back to config defaults.

type Provider

type Provider interface {
	Stream(ctx context.Context, messages []Message, tools []ToolSchema) <-chan StreamEvent
	ModelName() string
	SetThinkingLevel(level string)
	SetModel(model string)
	ListModels() ([]string, error)
	ModelInfo() (ModelInfo, error)
}

Provider is the interface for LLM provider implementations. Stream returns a channel of stream events. Errors are encoded as StreamEnd(FinishReason="error", Error=...), not returned as Go errors. The channel is closed when the stream ends.

type ResponseChunk

type ResponseChunk struct {
	Type    string `json:"type"`
	Content string `json:"content"`
}

ResponseChunk is a chunk of the model's response content.

type StreamEnd

type StreamEnd struct {
	Type            string `json:"type"`
	FinishReason    string `json:"finish_reason"`
	PromptEvalCount *int   `json:"prompt_eval_count,omitempty"`
	EvalCount       *int   `json:"eval_count,omitempty"`
	Error           string `json:"error,omitempty"`
}

StreamEnd is emitted at the end of a stream. Errors are encoded here (FinishReason="error", Error set), not returned as Go errors.

type StreamEvent

type StreamEvent interface {
	// contains filtered or unexported methods
}

StreamEvent is the sealed interface for all stream event types. Consumers dispatch on the concrete type via a type switch.

type System

type System struct {
	Content   string `json:"content"`
	Timestamp string `json:"timestamp,omitempty"`
}

System is the system prompt message.

func NewSystem

func NewSystem(content string) System

NewSystem creates a System message with timestamp set.

type ThinkingChunk

type ThinkingChunk struct {
	Type    string `json:"type"`
	Content string `json:"content"`
}

ThinkingChunk is a chunk of the model's thinking content.

type ThinkingLevelChange

type ThinkingLevelChange struct {
	Level     string `json:"level"`
	Timestamp string `json:"timestamp,omitempty"`
}

ThinkingLevelChange is a session entry recording a thinking level change. It is persisted to the store and replayed on resume.

func NewThinkingLevelChange

func NewThinkingLevelChange(level string) ThinkingLevelChange

NewThinkingLevelChange creates a ThinkingLevelChange with timestamp set.

type ToolCall

type ToolCall struct {
	ID        string         `json:"id"`
	Name      string         `json:"name"`
	Arguments map[string]any `json:"arguments"`
}

ToolCall is a tool invocation request from the model.

type ToolCallEvent

type ToolCallEvent struct {
	Type     string   `json:"type"`
	ToolCall ToolCall `json:"tool_call"`
}

ToolCallEvent carries a tool call parsed from the stream.

type ToolResult

type ToolResult struct {
	Content    string `json:"content"`
	ToolCallID string `json:"tool_call_id"`
	IsError    bool   `json:"is_error,omitempty"`
	Timestamp  string `json:"timestamp,omitempty"`
}

ToolResult is the result of a tool execution, appended to the message history so the model can see the result.

func NewToolResult

func NewToolResult(content, toolCallID string, isError bool) ToolResult

NewToolResult creates a ToolResult with timestamp set.

type ToolSchema

type ToolSchema struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Parameters  map[string]any `json:"parameters"`
}

ToolSchema describes a tool the model may call. It is passed to the provider and serialized into the API request.

type User

type User struct {
	Content   string         `json:"content"`
	Images    []ImageContent `json:"images,omitempty"`
	Timestamp string         `json:"timestamp,omitempty"`
}

User is a user chat message. Images is optional; when empty the message is text-only and providers send content as a plain string.

func NewUser

func NewUser(content string) User

NewUser creates a User message with timestamp set.

func NewUserWithImages

func NewUserWithImages(content string, images []ImageContent) User

NewUserWithImages creates a User message with image content.

Jump to

Keyboard shortcuts

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