agentloop

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package agentloop provides a reusable stateless agent loop.

Run() executes one agent loop iteration: prompt → LLM → tool calls → response. The caller provides conversation history, a system prompt, tools, and an optional sandbox env. No persistence — the caller receives StepMessages and handles storage.

Plane: shared

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildSystemPrompt

func BuildSystemPrompt(data PromptData) (string, error)

BuildSystemPrompt renders the default system prompt with runtime context. The result is the base prompt — consumers append their own instructions after this.

Types

type Config

type Config struct {
	Provider      fantasy.Provider
	Model         string
	SystemPrompt  string
	Tools         []fantasy.AgentTool
	MaxSteps      int      // 0 means use default (20)
	MaxTokens     int      // 0 means use default (4096)
	SandboxEnv    []string // passed to sandbox ExecConfig
	AllowedPaths  []string // absolute dirs the read/glob/grep tools may access
	TreeThreshold int      // chars — content above this returns tree by default; 0 = use default (5000)
}

Config holds everything needed to run one agent loop iteration.

type PromptData

type PromptData struct {
	WorkingDir   string
	Platform     string
	Date         string
	AllowedPaths []string
	Tools        []ToolInfo
}

PromptData holds the runtime context used to render the default system prompt.

type RunResult

type RunResult struct {
	Response string        // final text response (accumulated assistant text)
	Steps    []StepMessage // all messages generated (for persistence by caller)
	Result   *fantasy.AgentResult
}

RunResult contains the agent's output after a loop completes.

func Run

func Run(
	ctx context.Context,
	cfg Config,
	history []fantasy.Message,
	prompt string,
	onDelta func(text string),
) (*RunResult, error)

Run executes one agent loop: prompt → LLM → tool calls → response. Stateless — no DB, no conversation persistence. The caller handles that. onDelta is called with each text delta as it streams; pass nil to disable streaming.

type StepMessage

type StepMessage struct {
	Role       StepRole       // StepRoleAssistant or StepRoleTool
	Content    string         // text content
	ToolCalls  []ToolCallInfo // for assistant messages with tool use
	ToolCallID string         // for tool result messages
	Timestamp  time.Time
}

StepMessage represents one message generated during the agent loop. Richer than fantasy.Message — includes tool call metadata for persistence.

type StepRole

type StepRole string

StepRole represents the role of a message step in the agent loop.

const (
	StepRoleAssistant StepRole = "assistant"
	StepRoleTool      StepRole = "tool"
)

type ToolCallInfo

type ToolCallInfo struct {
	ID    string
	Name  string
	Input json.RawMessage // JSON-encoded tool input
}

ToolCallInfo carries tool call metadata for persistence.

type ToolInfo

type ToolInfo struct {
	Name        string
	Description string
}

ToolInfo holds a tool's name and full description for system prompt injection.

Directories

Path Synopsis
Package sandbox provides bubblewrap-based sandboxed command execution.
Package sandbox provides bubblewrap-based sandboxed command execution.
Package tools provides reusable agent tool implementations.
Package tools provides reusable agent tool implementations.

Jump to

Keyboard shortcuts

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