Help us shape the future of Go! Take the 2026 Go Developer Survey

engine

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package engine is the loop itself. Start at Run.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Deps

type Deps struct {
	Model            model.ToolCallingChatModel
	Caller           ModelCaller
	Tools            ToolDispatcher
	Stop             StopPolicy
	Finish           *Finisher
	StopInstructions string
	Inbox            Inbox
	Compactor        HistoryCompactor
	State            *state.Run
	Emitter          *emitter.Emitter
	Audit            *audit.Recorder
	Transcript       *transcript.Logger
	Logger           *slog.Logger
	TotalTimeout     time.Duration
	Reviewer         Reviewer
	MaxReopens       int // finishes re-opened after a failed finish review
}

Deps is everything an Engine works with. wire.go fills it. State, emitter, audit and transcript are concrete: each has one implementation.

type Engine

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

func New

func New(d Deps) *Engine

func (*Engine) ConfigChanged

func (e *Engine) ConfigChanged()

ConfigChanged reports a settings change; the inbox has already applied it.

func (*Engine) Injected

func (e *Engine) Injected(message string)

Injected adds the caller's message to the conversation.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context)

Run is the loop. loop.Loop.Start runs it as its own goroutine (1). Each model call may add two short-lived goroutines inside modelcall (2: the stream reader, 3: the iteration-timeout watcher); both end when that call returns. Run always ends through Finisher.Close, which closes the event channel.

func (*Engine) Seed

func (e *Engine) Seed(messages []*schema.Message) []*schema.Message

Seed returns the conversation the run starts from: messages plus the stop instructions, placed after the caller's own system messages. Providers such as Anthropic accept system messages only before the first user message. A conversation that already has them, such as a resumed run, is returned unchanged.

type Finisher

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

Finisher ends a run. Every way a run can end goes through it, and the emitter delivers only the first done event, so a run has one outcome. It also implements dispatch.Ender.

func NewFinisher

func NewFinisher(st *state.Run, em *emitter.Emitter, logger *slog.Logger) *Finisher

func (*Finisher) Close

func (f *Finisher) Close()

Close runs when Engine.Run returns, whatever path ended it: it makes sure a done event exists, settles the status and closes the event channel.

func (*Finisher) Done

func (f *Finisher) Done(reason event.DoneReason, finalContent string)

Done ends the run for reason.

func (*Finisher) EndAwaitingHuman

func (f *Finisher) EndAwaitingHuman(awaiting event.AwaitingInputData)

EndAwaitingHuman ends the run so a person can answer later.

func (*Finisher) EndStopped

func (f *Finisher) EndStopped()

EndStopped ends the run because it was told to stop.

func (*Finisher) Fail

func (f *Finisher) Fail(msg string)

Fail ends the run on a fatal error.

func (*Finisher) Wait

func (f *Finisher) Wait()

Wait blocks until Close has run.

type HistoryCompactor

type HistoryCompactor interface {
	Compact(msgs []*schema.Message) []*schema.Message
}

HistoryCompactor shapes the history sent to the model; the stored history is not changed. → the caller's loop.HistoryCompactor, or defaults.FullHistory

type Inbox

type Inbox interface {
	Drain(n control.Notifier) bool
	StopRequested() bool
}

Inbox holds controls sent to the run. → internal/control

type ModelCaller

type ModelCaller interface {
	Call(ctx context.Context, m model.ToolCallingChatModel, msgs []*schema.Message, opts ...model.Option) (*schema.Message, error)
}

ModelCaller makes one logical model call. → internal/modelcall

type Reviewer

type Reviewer interface {
	// AfterIteration judges one iteration's messages against the task.
	AfterIteration(ctx context.Context, task, iteration []*schema.Message) (reflection.Verdict, error)
	// AtFinish judges the final answer against the task.
	AtFinish(ctx context.Context, task []*schema.Message, answer string) (reflection.Verdict, error)
}

Reviewer is the run's reflection policy, chosen once in wire.go. Each method returns the zero Verdict when it does not review. → internal/reflection

type StopPolicy

type StopPolicy interface {
	Label() string
	// Instructions returns the instructions for the stop condition.
	Instructions() string
	// FinishCall returns the call that ends the run, when the reply has one.
	FinishCall(reply *schema.Message) (schema.ToolCall, bool)
	// AfterText says whether a text-only reply ends the run, and what
	// message, if any, to send the model when it does not.
	AfterText() (ends bool, nudge string)
}

StopPolicy is the run's stop condition, chosen once in wire.go. → internal/stopping

type ToolDispatcher

type ToolDispatcher interface {
	Dispatch(ctx context.Context, tc schema.ToolCall) bool
}

ToolDispatcher runs one tool call; false means the run ended. → internal/dispatch

Jump to

Keyboard shortcuts

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