agent

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AggregateUsage

func AggregateUsage(a, b provider.Usage) provider.Usage

Types

type Agent

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

func New

func New(p provider.Provider) *Agent

func (*Agent) Continue

func (a *Agent) Continue(ctx context.Context, messages []provider.Message, prompt string) (*LoopResult, error)

Continue resumes a conversation with existing messages and a new user prompt. The messages should include the system prompt if one was used.

func (*Agent) ContinueStream

func (a *Agent) ContinueStream(ctx context.Context, messages []provider.Message, prompt string, events chan<- StreamEvent) (*LoopResult, error)

ContinueStream resumes a conversation with streaming.

func (*Agent) Messages

func (a *Agent) Messages(systemPrompt string, result *LoopResult) []provider.Message

Messages returns the full message history from a LoopResult, including the system prompt if provided.

func (*Agent) Run

func (a *Agent) Run(ctx context.Context, prompt string) (*LoopResult, error)

func (*Agent) RunStream

func (a *Agent) RunStream(ctx context.Context, prompt string, events chan<- StreamEvent) (*LoopResult, error)

RunStream runs the agent with streaming, sending events to the provided channel. The channel will be closed when the agent completes.

func (*Agent) WithMaxToolSteps

func (a *Agent) WithMaxToolSteps(n int) *Agent

func (*Agent) WithOnIterationEnd

func (a *Agent) WithOnIterationEnd(cb IterationCallback) *Agent

func (*Agent) WithOnIterationStart

func (a *Agent) WithOnIterationStart(cb IterationCallback) *Agent

func (*Agent) WithOnToolEnd

func (a *Agent) WithOnToolEnd(cb ToolEndCallback) *Agent

func (*Agent) WithOnToolStart

func (a *Agent) WithOnToolStart(cb ToolStartCallback) *Agent

func (*Agent) WithSettings

func (a *Agent) WithSettings(s Settings) *Agent

func (*Agent) WithStopConditions

func (a *Agent) WithStopConditions(conditions ...StopCondition) *Agent

func (*Agent) WithSystemPrompt

func (a *Agent) WithSystemPrompt(prompt string) *Agent

func (*Agent) WithTools

func (a *Agent) WithTools(tools ...*tool.Tool) *Agent

func (*Agent) WithVerification

func (a *Agent) WithVerification(v VerifyCompletion) *Agent

type CompletionReason

type CompletionReason string
const (
	CompletionReasonVerified      CompletionReason = "verified"
	CompletionReasonMaxIterations CompletionReason = "max_iterations"
	CompletionReasonStopCondition CompletionReason = "stop_condition"
	CompletionReasonAborted       CompletionReason = "aborted"
	CompletionReasonError         CompletionReason = "error"
)

type IterationCallback

type IterationCallback func(iteration int, result *IterationResult)

type IterationResult

type IterationResult struct {
	Iteration   int
	Response    *provider.ChatResponse
	ToolCalls   []provider.ToolCall
	ToolResults []ToolResult
	Usage       provider.Usage
	// Messages contains all messages generated during this iteration
	// (assistant messages with tool_calls + tool result messages)
	Messages []provider.Message
}

type LoopResult

type LoopResult struct {
	FinalText        string
	Iterations       int
	CompletionReason CompletionReason
	Reason           string
	AllResults       []*IterationResult
	TotalUsage       provider.Usage
}

type Settings

type Settings struct {
	MaxToolSteps     int
	StopConditions   []StopCondition
	VerifyCompletion VerifyCompletion
	OnIterationStart IterationCallback
	OnIterationEnd   IterationCallback
	OnToolStart      ToolStartCallback
	OnToolEnd        ToolEndCallback
}

func DefaultSettings

func DefaultSettings() Settings

type StopCondition

type StopCondition func(ctx StopConditionContext) bool

func IterationCountIs

func IterationCountIs(n int) StopCondition

func PromptTokensExceed

func PromptTokensExceed(maxTokens int) StopCondition

PromptTokensExceed returns a stop condition that triggers when prompt tokens exceed the limit. This is useful for preventing context window overflow.

func TokenUsagePercent

func TokenUsagePercent(maxTokens int, percent float64) StopCondition

TokenUsagePercent returns a stop condition that triggers when token usage exceeds a percentage of the limit.

func TotalTokensExceed

func TotalTokensExceed(maxTokens int) StopCondition

TotalTokensExceed returns a stop condition that triggers when total tokens exceed the limit.

type StopConditionContext

type StopConditionContext struct {
	Iteration  int
	TotalUsage provider.Usage
	LastResult *IterationResult
	AllResults []*IterationResult
}

type StreamEvent

type StreamEvent struct {
	Type       string `json:"type"` // "token", "tool_start", "tool_end", "error", "done"
	Content    string `json:"content,omitempty"`
	ToolName   string `json:"tool_name,omitempty"`
	ToolArgs   string `json:"tool_args,omitempty"`
	ToolResult string `json:"tool_result,omitempty"`
	Error      string `json:"error,omitempty"`
	SessionID  string `json:"session_id,omitempty"`
}

StreamEvent represents an event during streaming

type TokenCallback

type TokenCallback func(token string)

type ToolEndCallback

type ToolEndCallback func(toolName string, args string, result *ToolResult)

type ToolResult

type ToolResult struct {
	ToolCallID string
	ToolName   string
	Result     string
	Error      error
}

type ToolStartCallback

type ToolStartCallback func(toolName string, args string)

type VerificationContext

type VerificationContext struct {
	LastResult     *IterationResult
	Iteration      int
	AllResults     []*IterationResult
	OriginalPrompt string
	TotalUsage     provider.Usage
}

type VerificationResult

type VerificationResult struct {
	Complete bool
	Reason   string
}

type VerifyCompletion

type VerifyCompletion func(ctx VerificationContext) VerificationResult

func DefaultVerification

func DefaultVerification() VerifyCompletion

func NoToolCallsVerification

func NoToolCallsVerification() VerifyCompletion

func TextContainsVerification

func TextContainsVerification(substring string) VerifyCompletion

Jump to

Keyboard shortcuts

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