agent

package
v0.93.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package agent implements the Observe-Think-Act agent loop and stateful runtime.

Index

Constants

View Source
const (
	RoleUser                = msg.RoleUser
	RoleAssistant           = msg.RoleAssistant
	RoleToolResult          = msg.RoleToolResult
	RoleCustom              = msg.RoleCustom
	RoleBranchSummary       = msg.RoleBranchSummary
	RoleCompactionSummary   = msg.RoleCompactionSummary
	ToolExecutionParallel   = msg.ToolExecutionParallel
	ToolExecutionSequential = msg.ToolExecutionSequential
	QueueAll                = msg.QueueAll
	QueueOne                = msg.QueueOne
	StopReasonComplete      = msg.StopReasonComplete
	StopReasonError         = msg.StopReasonError
	StopReasonAborted       = msg.StopReasonAborted
)

Variables

View Source
var (
	ErrMaxSteps        = msg.ErrMaxSteps
	ErrAborted         = msg.ErrAborted
	ErrToolNotFound    = msg.ErrToolNotFound
	ErrEmptyContext    = msg.ErrEmptyContext
	ErrInvalidContinue = msg.ErrInvalidContinue
)

Functions

This section is empty.

Types

type AfterToolCallFn

type AfterToolCallFn = msg.AfterToolCallFn

type AfterToolContext

type AfterToolContext = msg.AfterToolContext

type AfterToolResult

type AfterToolResult = msg.AfterToolResult

type Agent

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

Agent is a stateful wrapper around the agent loop with queues and subscriptions.

func NewAgent

func NewAgent(opts Options) *Agent

NewAgent creates an agent with default transforms and optional dependencies.

func (*Agent) Abort

func (a *Agent) Abort()

Abort cancels the in-flight loop, if any.

func (*Agent) ApplyConfig

func (a *Agent) ApplyConfig(fn func(*Config))

ApplyConfig atomically mutates loop configuration before the next Prompt or Continue. Callers must preserve queue drains when replacing the whole struct.

func (*Agent) ApplyState

func (a *Agent) ApplyState(fn func(*Context))

ApplyState atomically mutates the agent's internal state using the provided function. This avoids the clone-modify-discard pattern when the caller needs to update state in place.

func (*Agent) Config

func (a *Agent) Config() Config

Config returns a snapshot of the current loop configuration.

func (*Agent) Continue

func (a *Agent) Continue(ctx context.Context) (*agentevent.Stream, error)

Continue resumes the loop from the current context.

func (*Agent) FollowUp

func (a *Agent) FollowUp(message AgentMessage)

FollowUp enqueues a message injected after the inner loop completes.

func (*Agent) Prompt

func (a *Agent) Prompt(ctx context.Context, prompts ...AgentMessage) (*agentevent.Stream, error)

Prompt starts a new loop turn with one or more user messages.

func (*Agent) SetActiveTools

func (a *Agent) SetActiveTools(names []string)

SetActiveTools restricts which registered tools are exposed to the model.

func (*Agent) SetModel

func (a *Agent) SetModel(model llms.Model)

SetModel swaps the underlying langchaingo model at runtime.

func (*Agent) SetTools

func (a *Agent) SetTools(registry *tool.Registry)

SetTools replaces the active tool registry contents.

func (*Agent) State

func (a *Agent) State() *Context

State returns a snapshot of the current agent context.

func (*Agent) Steer

func (a *Agent) Steer(message AgentMessage)

Steer enqueues a message injected between inner-loop turns.

func (*Agent) Subscribe

func (a *Agent) Subscribe(handler agentevent.Handler)

Subscribe registers a lifecycle event handler.

type AgentMessage

type AgentMessage = msg.AgentMessage

func RunLoop

func RunLoop(ctx context.Context, prompts []AgentMessage, agentCtx *Context, cfg Config, deps LoopDeps, stream *agentevent.Stream) ([]AgentMessage, error)

RunLoop starts a new agent loop from prompt messages.

func RunLoopContinue

func RunLoopContinue(ctx context.Context, agentCtx *Context, cfg Config, deps LoopDeps, stream *agentevent.Stream) ([]AgentMessage, error)

RunLoopContinue resumes a loop from existing context without adding prompts.

type AssistantMessage

type AssistantMessage = msg.AssistantMessage

type BeforeToolCallFn

type BeforeToolCallFn = msg.BeforeToolCallFn

type BeforeToolContext

type BeforeToolContext = msg.BeforeToolContext

type BeforeToolResult

type BeforeToolResult = msg.BeforeToolResult

type BranchSummaryMessage

type BranchSummaryMessage = msg.BranchSummaryMessage

type CompactionSummaryMessage

type CompactionSummaryMessage = msg.CompactionSummaryMessage

type Config

type Config = msg.Config

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns conservative defaults for a new agent run.

type ContentPart

type ContentPart = msg.ContentPart

type Context

type Context = msg.Context

type ConvertToLLMFn

type ConvertToLLMFn = msg.ConvertToLLMFn

type CustomMessage

type CustomMessage = msg.CustomMessage

type GetMessagesFn

type GetMessagesFn = msg.GetMessagesFn

type ImagePart

type ImagePart = msg.ImagePart

type LoopDeps

type LoopDeps struct {
	Model    llms.Model
	Registry *tool.Registry
}

LoopDeps holds runtime dependencies for the agent loop.

type MessageRole

type MessageRole = msg.MessageRole

type Options

type Options struct {
	InitialState *Context
	Config       Config
	Model        llms.Model
	Registry     *tool.Registry
}

Options configures a new Agent instance.

type PrepareNextTurnFn

type PrepareNextTurnFn = msg.PrepareNextTurnFn

type QueueMode

type QueueMode = msg.QueueMode

type ShouldStopAfterTurnFn

type ShouldStopAfterTurnFn = msg.ShouldStopAfterTurnFn

type StopReason

type StopReason = msg.StopReason

type TextPart

type TextPart = msg.TextPart

type ToolCallPart

type ToolCallPart = msg.ToolCallPart

type ToolExecutionMode

type ToolExecutionMode = msg.ToolExecutionMode

type ToolResultMessage

type ToolResultMessage = msg.ToolResultMessage

type TransformContextFn

type TransformContextFn = msg.TransformContextFn

type TurnContext

type TurnContext = msg.TurnContext

type TurnUpdate

type TurnUpdate = msg.TurnUpdate

type Usage

type Usage = msg.Usage

type UserMessage

type UserMessage = msg.UserMessage

func NewUserMessage

func NewUserMessage(text string) UserMessage

NewUserMessage builds a text user message with the current timestamp.

func NewUserMessageWithParts

func NewUserMessageWithParts(parts ...ContentPart) UserMessage

NewUserMessageWithParts builds a multimodal user message.

Directories

Path Synopsis
Package coding provides agent tools for terminal control, file I/O, web search, and code execution.
Package coding provides agent tools for terminal control, file I/O, web search, and code execution.
Package ctxmgr manages agent context budgets, compaction, and branch summarization.
Package ctxmgr manages agent context budgets, compaction, and branch summarization.
Package env provides filesystem and shell execution with Result-based error handling.
Package env provides filesystem and shell execution with Result-based error handling.
Package event provides lifecycle event streaming for agent runs.
Package event provides lifecycle event streaming for agent runs.
example
echo
Package echo provides a reference echo tool for agent examples.
Package echo provides a reference echo tool for agent examples.
Package harness orchestrates agent runs with hooks, tools, and session persistence.
Package harness orchestrates agent runs with hooks, tools, and session persistence.
Package hooks provides typed agent hook registration, emission, and loop Config bridging.
Package hooks provides typed agent hook registration, emission, and loop Config bridging.
Package llm adapts langchaingo models for the agent loop and single-shot module tasks.
Package llm adapts langchaingo models for the agent loop and single-shot module tasks.
Package model provides built-in LLM metadata and dual-model routing.
Package model provides built-in LLM metadata and dual-model routing.
Package msg defines shared agent domain types used across subpackages.
Package msg defines shared agent domain types used across subpackages.
Package permission evaluates tool invocation permissions with OpenCode-compatible rules.
Package permission evaluates tool invocation permissions with OpenCode-compatible rules.
Package result provides a discriminated Result type for expected failures in the agent stack.
Package result provides a discriminated Result type for expected failures in the agent stack.
Package session manages branchable conversation trees and JSONL serialization.
Package session manages branchable conversation trees and JSONL serialization.
Package subagent runs a specialized agent loop in isolation so a primary agent can delegate a self-contained task and receive only the final result back.
Package subagent runs a specialized agent loop in isolation so a primary agent can delegate a self-contained task and receive only the final result back.
Package tool provides tool registration and execution for agent runs.
Package tool provides tool registration and execution for agent runs.
Package transform converts agent messages for LLM providers.
Package transform converts agent messages for LLM providers.

Jump to

Keyboard shortcuts

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