loop

package
v0.99.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMaxSteps is returned when the loop exceeds msg.Config.MaxSteps.
	ErrMaxSteps = msg.ErrMaxSteps
	// ErrAborted is returned when the loop is cancelled.
	ErrAborted = msg.ErrAborted
	// ErrToolNotFound is returned when a tool call names an unknown tool.
	ErrToolNotFound = msg.ErrToolNotFound
	// ErrEmptyContext is returned when Continue is called with no messages.
	ErrEmptyContext = msg.ErrEmptyContext
	// ErrInvalidContinue is returned when Continue cannot resume from the last message.
	ErrInvalidContinue = msg.ErrInvalidContinue
)

Functions

func DefaultConfig

func DefaultConfig() msg.Config

DefaultConfig returns conservative defaults for a new agent run.

func NewUserMessage

func NewUserMessage(text string) msg.UserMessage

NewUserMessage builds a text user message with the current timestamp.

func NewUserMessageWithParts

func NewUserMessageWithParts(parts ...msg.ContentPart) msg.UserMessage

NewUserMessageWithParts builds a multimodal user message.

func RunLoop

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

RunLoop starts a new agent loop from prompt messages.

func RunLoopContinue

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

RunLoopContinue resumes a loop from existing context without adding prompts.

Types

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(*msg.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(*msg.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() msg.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 msg.AgentMessage)

FollowUp enqueues a message injected after the inner loop completes.

func (*Agent) Prompt

func (a *Agent) Prompt(ctx context.Context, prompts ...msg.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() *msg.Context

State returns a snapshot of the current agent context.

func (*Agent) Steer

func (a *Agent) Steer(message msg.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 LoopDeps

type LoopDeps struct {
	Model llms.Model
	// ResolveModel optionally returns the langchaingo client for a turn's model name.
	// Dual-model routing changes ModelName across turns; without ResolveModel the loop
	// would keep calling deps.Model (bound to the chat provider endpoint).
	ResolveModel ModelResolver
	Registry     *tool.Registry
}

LoopDeps holds runtime dependencies for the agent loop.

type ModelResolver

type ModelResolver func(ctx context.Context, modelName string) (llms.Model, error)

ModelResolver returns the langchaingo client for a specific model name.

type Options

type Options struct {
	InitialState *msg.Context
	Config       msg.Config
	Model        llms.Model
	// ResolveModel optionally resolves per-turn clients for dual-model routing.
	ResolveModel ModelResolver
	Registry     *tool.Registry
}

Options configures a new Agent instance.

Jump to

Keyboard shortcuts

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