runtime

package
v0.40.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package runtime executes agent conversations in already-resolved sessions.

The only entry point for callers is Runtime.Chat. Callers must obtain a validated session.Info from session.Registry before calling Chat. Runtime never creates or repairs session metadata.

Index

Constants

This section is empty.

Variables

View Source
var ErrChatTimeout = agenterr.ErrChatTimeout

ErrChatTimeout is emitted when a chat turn exceeds its deadline.

View Source
var ErrSessionBusy = agenterr.ErrSessionBusy

ErrSessionBusy is returned when a session already has an active chat turn.

Functions

func MessageText

func MessageText(message MessageContent) string

MessageText extracts and joins all text from a MessageContent.

Types

type BeforeRunFunc

type BeforeRunFunc func(ctx context.Context, info session.Info, model, msgText, system string, history []ai.Message) (systemOut string, err error)

BeforeRunFunc is called before each chat turn to inject/override the system prompt.

type CompactionConfig

type CompactionConfig struct {
	MaxTokens int
	KeepTail  int
}

CompactionConfig controls automatic compaction thresholds.

func (CompactionConfig) WithDefaults

func (c CompactionConfig) WithDefaults() CompactionConfig

WithDefaults returns the config with zero values replaced by defaults.

type Config

type Config struct {
	Factory        NewRunnerFunc
	Memory         memory.Provider
	IdleTimeout    time.Duration
	Compaction     CompactionConfig
	DefaultModel   string
	FastModel      string
	HooksFn        func() []hooks.HookPlugin
	BeforeRun      BeforeRunFunc
	SnapshotPrompt SnapshotPromptFunc
}

Config holds all dependencies for a Runtime instance.

type Event

type Event struct {
	Text      string
	Reasoning string
	Image     *ImageEvent
	File      *FileEvent
	ToolUse   *ToolUseEvent
	Step      *StepEvent
	Store     ai.Message // non-nil → append to session history
	Err       error
}

Event is the consumer-facing stream event.

type FileEvent

type FileEvent struct {
	Path string
	Name string
}

FileEvent carries a local file path.

type ImageEvent

type ImageEvent struct {
	Data     string
	MimeType string
}

ImageEvent carries a base64-encoded image.

type MessageContent

type MessageContent = any

MessageContent is a user message: string (text) or []ai.ContentBlock (multimodal).

type NewRunnerFunc

type NewRunnerFunc func(ctx context.Context, params RunnerParams) (Runner, error)

NewRunnerFunc creates a new Runner with the given params.

type Option

type Option func(*chatOptions)

Option configures a single Chat call.

func WithExcludedTools

func WithExcludedTools(names ...string) Option

WithExcludedTools hides the named tools for this Chat call.

func WithModel

func WithModel(model string) Option

WithModel overrides the model for this Chat call.

func WithSystemOverride

func WithSystemOverride(system string) Option

WithSystemOverride overrides the system prompt for this Chat call.

type Runner

type Runner interface {
	Chat(ctx context.Context, history []ai.Message, message MessageContent) <-chan Event
	Alive() bool
	Busy() bool
	LastActivity() time.Time
	SystemPrompt() string
	Close() error
}

Runner executes prompts against an AI backend.

type RunnerParams

type RunnerParams struct {
	Model          string
	Memory         any // memory.Provider — typed as any to avoid circular imports
	UserID         string
	SessionID      string
	AgentID        string
	ProjectID      string
	HooksFn        func() []hooks.HookPlugin
	ExtraTools     []tools.Tool
	DelegateRunner delegatetool.SessionRunner
}

RunnerParams holds dependencies for creating a new Runner.

type Runtime

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

Runtime executes agent conversations in already-resolved sessions. It owns the runner cache, runner factory, and event streaming. It does NOT own session creation, kind validation, or list/archive APIs.

func New

func New(cfg Config) (*Runtime, error)

New creates a Runtime from the given config.

func (*Runtime) Chat

func (rt *Runtime) Chat(ctx context.Context, info session.Info, msg MessageContent, opts ...Option) <-chan Event

Chat executes a user message inside the given session and streams events back. info must have been obtained from session.Registry — this method does not create or repair session metadata.

Only one active turn per session is allowed. A second concurrent Chat on the same session returns ErrSessionBusy immediately.

func (*Runtime) Close

func (rt *Runtime) Close() error

Close shuts down all runners and releases resources.

func (*Runtime) CloseSession

func (rt *Runtime) CloseSession(_ context.Context, sessionID string) error

CloseSession closes the runner for a single session without affecting others.

func (*Runtime) Factory

func (rt *Runtime) Factory() NewRunnerFunc

Factory returns the current runner factory. Used by the task system to create standalone runners with custom tools.

func (*Runtime) Memory

func (rt *Runtime) Memory() memory.Provider

Memory returns the memory provider backing this runtime.

func (*Runtime) ResetRunners

func (rt *Runtime) ResetRunners() error

ResetRunners closes all live runners while keeping session metadata in storage.

func (*Runtime) ResetRunnersForUser

func (rt *Runtime) ResetRunnersForUser(userID string) error

ResetRunnersForUser closes live runners for a specific user.

func (*Runtime) SetDefaultModel

func (rt *Runtime) SetDefaultModel(model string)

SetDefaultModel updates the default model for new runners.

func (*Runtime) SetDelegateRunner

func (rt *Runtime) SetDelegateRunner(r delegatetool.SessionRunner)

SetDelegateRunner wires the delegate session runner into new runners. Call after construction so runners can spawn persistent child sessions.

func (*Runtime) SetFactory

func (rt *Runtime) SetFactory(f NewRunnerFunc)

SetFactory replaces the runner factory. Existing runners are not affected until their session is next reused.

func (*Runtime) SetHooks

func (rt *Runtime) SetHooks(fn func() []hooks.HookPlugin)

SetHooks updates the hook getter used when creating new runners.

func (*Runtime) StartReaper

func (rt *Runtime) StartReaper(ctx context.Context)

StartReaper begins the idle-runner eviction loop. Call in a goroutine.

type SnapshotPromptFunc

type SnapshotPromptFunc func(ctx context.Context, info session.Info, snap memory.SessionSnapshot) string

SnapshotPromptFunc builds a system prompt from the session's snapshot version.

type StepEvent

type StepEvent struct {
	Kind string // "start" or "finish"
}

StepEvent marks the boundary of an agentic step.

type ToolUseEvent

type ToolUseEvent struct {
	ID        string
	Tool      string
	Status    string
	Input     string
	Arguments map[string]any
	Detail    string
	Content   string
}

ToolUseEvent describes a tool invocation in progress or completed.

Jump to

Keyboard shortcuts

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