agent

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMaxTurns bounds the internal model→tools→model loop within a
	// single Send(). Each model invocation counts as one — a response that
	// fires 10 parallel tool calls is still one tick. Big audits and refactors
	// routinely take 30-80 rounds; 200 leaves comfortable headroom while
	// still catching runaway tool-call cycles before they burn many tokens.
	DefaultMaxTurns    = 200
	DefaultToolTimeout = 10 * time.Minute

	// DefaultContextWindow is the input-token capacity assumed when the
	// Config.ContextWindow override is zero. 400K is a deliberate middle
	// ground: it covers GPT-5 at face value, and on 1M-default models
	// (modern Claude family, GPT-5.5) it caps usable context at 350K — a
	// trade against the "lost in the middle" / long-context retrieval
	// degradation that shows up well before 1M is reached. Users who want
	// the full 1M should set Config.ContextWindow = 1_000_000 explicitly.
	// Smaller-window models (200K Claude, 128K GPT-4o) need a smaller
	// override so proactive compaction fires before the API rejects.
	DefaultContextWindow = 400_000

	// DefaultReserveTokens is the headroom kept free for the next request's
	// new content (model response + appended tool results). At the default
	// 400K window this puts the proactive trigger at 368K (~8% headroom) —
	// enough to absorb a typical turn (a handful of tool calls with the
	// truncation hook's soft caps in play, plus the model response). A
	// pathologically heavy turn can still overflow; the reactive recovery
	// path in recovery.go catches that at the cost of one wasted round-trip.
	DefaultReserveTokens = 32_000
)

Variables

View Source
var ErrMaxTurnsExceeded = errors.New("agent: internal turn-loop safety bound exceeded — likely a runaway tool-call cycle")

Functions

This section is empty.

Types

type Agent

type Agent struct {
	*Config

	Messages []Message
	Usage    Usage
	// contains filtered or unexported fields
}

func (*Agent) Send

func (a *Agent) Send(ctx context.Context, input []Content) iter.Seq2[Message, error]

Send routes input one of two ways:

  • if a turn loop is already running for this agent, the input is queued and Send returns nil. The in-flight loop will drain the queue at its next safe boundary (between iterations, never inside a tool_call / tool_result pair).
  • otherwise, the input opens a new turn and Send returns an iterator over the stream. The loop's exit clears the running flag and discards any queue leftovers, so a cancel that aborts the loop also drops queued work.

type Config

type Config struct {
	Model        func() string
	Effort       func() string
	Tools        func() []tool.Tool
	Instructions func() string

	Hooks hook.Hooks

	// MaxTurns is a safety bound on the internal model→tool→model loop
	// within a single Send(), to catch runaway tool-call cycles. It is NOT
	// a user-visible conversational turn count — a single user message can
	// legitimately drive dozens of tool calls. Zero applies the default;
	// negative disables.
	MaxTurns    int
	ToolTimeout time.Duration

	// ContextWindow is the model's input-token capacity. The loop summarizes
	// older messages proactively when the last turn's input would otherwise
	// approach this limit. Zero applies DefaultContextWindow; negative
	// disables proactive compaction (the reactive on-error path still runs).
	ContextWindow int

	// ReserveTokens is the headroom kept free for the upcoming model
	// response and appended tool results. Compaction fires when the prior
	// turn's input exceeds (ContextWindow - ReserveTokens). Zero applies
	// DefaultReserveTokens.
	ReserveTokens int
	// contains filtered or unexported fields
}

func DefaultConfig

func DefaultConfig() (*Config, error)

func (*Config) Derive added in v0.6.2

func (c *Config) Derive() *Config

func (*Config) Models added in v0.6.9

func (c *Config) Models(ctx context.Context) ([]ModelInfo, error)

type Content

type Content struct {
	Text    string `json:"text,omitempty"`
	Refusal string `json:"refusal,omitempty"`

	File *File `json:"file,omitempty"`

	Reasoning *Reasoning `json:"reasoning,omitempty"`

	ToolCall   *ToolCall   `json:"tool_call,omitempty"`
	ToolResult *ToolResult `json:"tool_result,omitempty"`
}

type File

type File struct {
	Name string `json:"name,omitempty"`
	Data string `json:"data,omitempty"` // base64 data URL, e.g. "data:image/png;base64,..."
}

type Message

type Message struct {
	Role MessageRole `json:"role"`

	Content []Content `json:"content"`
	Hidden  bool      `json:"hidden,omitempty"`
}

type MessageRole

type MessageRole string
const (
	RoleUser      MessageRole = "user"
	RoleAssistant MessageRole = "assistant"
	RoleSystem    MessageRole = "system"
)

type ModelInfo added in v0.6.2

type ModelInfo struct {
	ID string
}

type Reasoning added in v0.6.2

type Reasoning struct {
	ID string `json:"id,omitempty"`

	Summary string `json:"summary,omitempty"`
}

type State added in v0.6.2

type State struct {
	Usage    Usage     `json:"usage"`
	Messages []Message `json:"messages,omitempty"`
}

func (*State) Load added in v0.6.2

func (s *State) Load(path string) error

func (*State) Save added in v0.6.2

func (s *State) Save(path string) error

type ToolCall

type ToolCall struct {
	ID string `json:"id"`

	Name string `json:"name"`
	Args string `json:"args,omitempty"`
}

type ToolResult

type ToolResult struct {
	ID string `json:"id,omitempty"`

	Name string `json:"name"`
	Args string `json:"args,omitempty"`

	Content string `json:"content,omitempty"`
}

type Usage

type Usage struct {
	InputTokens  int64 `json:"input_tokens"`
	CachedTokens int64 `json:"cached_tokens"`
	OutputTokens int64 `json:"output_tokens"`
}

Directories

Path Synopsis
ask
fs
lsp
mcp

Jump to

Keyboard shortcuts

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