loop

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package loop provides middleware that re-invokes an agent until one of its evaluators decides no more work is needed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg Config) agent.Middleware

New creates loop middleware.

Types

type CompletionMarkerConfig

type CompletionMarkerConfig struct {
	// Marker is the completion marker that stops the loop when present in the
	// latest response text.
	Marker string

	// FeedbackMessageTemplate is used when the marker is absent. When nil, the
	// default template is used. The
	// completionMarkerPlaceholder token is replaced when the evaluator is
	// created, and lastResponsePlaceholder is replaced on each evaluation.
	FeedbackMessageTemplate *string
}

CompletionMarkerConfig configures a completion-marker evaluator.

type CompletionMarkerEvaluator

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

CompletionMarkerEvaluator stops the loop once a marker appears in the latest response text, otherwise it asks the agent to continue.

func NewCompletionMarkerEvaluator

func NewCompletionMarkerEvaluator(config CompletionMarkerConfig) *CompletionMarkerEvaluator

NewCompletionMarkerEvaluator creates an evaluator that waits for the configured marker in the latest response text.

func (*CompletionMarkerEvaluator) Evaluate

Evaluate implements Evaluator.

type Config

type Config struct {
	// Evaluators decide after each iteration whether the wrapped agent should
	// be re-invoked. Evaluators are checked in order; the first evaluator that
	// asks to continue wins, and the loop stops only when all evaluators stop.
	Evaluators []Evaluator

	// MaxIterations is the absolute safety cap for a single run. When nil,
	// DefaultMaxIterations is used.
	MaxIterations *int

	// OnBehalfOfAuthorName stamps loop-synthesized feedback messages. When empty,
	// feedback messages are unattributed.
	OnBehalfOfAuthorName string

	// ExcludeOnBehalfOfMessages prevents loop-synthesized feedback messages
	// from being yielded to the caller. They are still sent to the agent.
	ExcludeOnBehalfOfMessages bool

	// FreshContextPerIteration restarts each reinvocation from the original
	// input messages plus an aggregated feedback log, and resets the session
	// to a pristine snapshot taken before the first iteration.
	FreshContextPerIteration bool

	// NonStreamingReturnsLastResponseOnly causes non-streaming runs (those
	// without agent.Stream(true)) to surface only the final iteration response.
	NonStreamingReturnsLastResponseOnly bool

	// SessionCreatedCallback is invoked when the loop creates a fresh session
	// for reinvocation.
	SessionCreatedCallback func(context.Context, *agent.Session) error
}

Config configures loop middleware.

type Context

type Context struct {
	// InitialMessages are the messages passed to the first iteration.
	InitialMessages []*message.Message

	// LastResponse is the response produced by the most recent iteration.
	LastResponse *agent.Response

	// Options are the options supplied to the agent run.
	Options []agent.Option

	// Iteration is the number of completed agent runs so far.
	Iteration int

	// Feedback contains one entry for each reinvocation requested by an
	// evaluator. Entries are empty when the reinvocation supplied no feedback
	// string or used explicit messages.
	Feedback []string

	// AdditionalProperties is a mutable bag for evaluator-specific per-run
	// state.
	AdditionalProperties map[string]any
}

Context contains per-run state passed to evaluators.

type Evaluation

type Evaluation struct {
	// ShouldReinvoke indicates whether the agent should run another iteration.
	ShouldReinvoke bool

	// Feedback is sent as a user message on the next iteration when Messages is
	// empty.
	Feedback string

	// Messages, when non-empty, are sent verbatim on the next iteration instead
	// of the loop building a feedback message.
	Messages []*message.Message
}

Evaluation is the result of an evaluator decision.

func Continue

func Continue(feedback string) Evaluation

Continue returns an evaluation that re-invokes the agent with optional feedback.

func ContinueWithMessages

func ContinueWithMessages(messages []*message.Message) Evaluation

ContinueWithMessages returns an evaluation that re-invokes the agent with explicit messages.

func Stop

func Stop() Evaluation

Stop returns an evaluation that stops the loop.

type Evaluator

type Evaluator interface {
	Evaluate(ctx context.Context, loop *Context) (Evaluation, error)
}

Evaluator decides whether a loop should continue after an agent iteration.

type EvaluatorFunc

type EvaluatorFunc func(ctx context.Context, loop *Context) (Evaluation, error)

EvaluatorFunc adapts a function to the Evaluator interface.

func (EvaluatorFunc) Evaluate

func (f EvaluatorFunc) Evaluate(ctx context.Context, loop *Context) (Evaluation, error)

Evaluate implements Evaluator.

Jump to

Keyboard shortcuts

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