agent

package
v0.2.16 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxSteps        = 15
	DefaultParseRetries    = 2
	DefaultToolRepeatLimit = 3
)
View Source
const (
	PlanStatusPending    = "pending"
	PlanStatusInProgress = "in_progress"
	PlanStatusCompleted  = "completed"
)
View Source
const (
	TypeToolCall    = "tool_call"
	TypePlan        = "plan"
	TypeFinal       = "final"
	TypeFinalAnswer = "final_answer"
)

Variables

View Source
var (
	ErrParseFailure    = errors.New("failed to parse agent response from LLM output")
	ErrInvalidToolCall = errors.New("tool_call JSON responses are not supported")
	ErrInvalidPlan     = errors.New("plan response missing payload")
	ErrInvalidFinal    = errors.New("final response missing payload")
)

Functions

func AdvancePlanOnSuccess

func AdvancePlanOnSuccess(p *Plan) (completedIndex int, completedStep string, startedIndex int, startedStep string, ok bool)

func BuildSystemPrompt

func BuildSystemPrompt(registry *tools.Registry, spec PromptSpec) string

func CompleteAllPlanSteps

func CompleteAllPlanSteps(p *Plan)

func ExtractFileWritePaths

func ExtractFileWritePaths(task string) []string

func NormalizePlanSteps

func NormalizePlanSteps(p *Plan)

Types

type AgentResponse

type AgentResponse struct {
	Type           string          `json:"type"`
	ToolCall       *ToolCall       `json:"tool_call,omitempty"`
	ToolCalls      []ToolCall      `json:"tool_calls,omitempty"`
	Plan           *Plan           `json:"plan,omitempty"`
	Final          *Final          `json:"final,omitempty"`
	FinalAnswer    *Final          `json:"final_answer,omitempty"`
	RawFinalAnswer json.RawMessage `json:"-"`
}

func ParseResponse

func ParseResponse(result llm.Result) (*AgentResponse, error)

func (*AgentResponse) FinalPayload

func (r *AgentResponse) FinalPayload() *Final

func (*AgentResponse) PlanPayload

func (r *AgentResponse) PlanPayload() *Plan

type Config

type Config struct {
	MaxSteps        int
	MaxTokenBudget  int
	ParseRetries    int
	ToolRepeatLimit int
}

type Context

type Context struct {
	Task           string
	Steps          []Step
	MaxSteps       int
	Plan           *Plan
	Metrics        *Metrics
	RawFinalAnswer json.RawMessage
}

func NewContext

func NewContext(task string, maxSteps int) *Context

func (*Context) AddUsage

func (c *Context) AddUsage(usage llm.Usage, dur time.Duration)

func (*Context) RecordStep

func (c *Context) RecordStep(step Step)

type Engine

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

func New

func New(client llm.Client, registry *tools.Registry, cfg Config, spec PromptSpec, opts ...Option) *Engine

func (*Engine) Resume

func (e *Engine) Resume(ctx context.Context, approvalRequestID string) (*Final, *Context, error)

func (*Engine) Run

func (e *Engine) Run(ctx context.Context, task string, opts RunOptions) (*Final, *Context, error)

type Final

type Final struct {
	Thought       string `json:"thought,omitempty"`
	Output        any    `json:"output,omitempty"`
	Plan          *Plan  `json:"plan,omitempty"`
	Reaction      string `json:"reaction,omitempty"`
	IsLightweight bool   `json:"is_lightweight,omitempty"`
}

type Hook

type Hook func(ctx context.Context, step int, agentCtx *Context, messages *[]llm.Message) error

type Limits added in v0.2.7

type Limits struct {
	MaxSteps        int
	ParseRetries    int
	MaxTokenBudget  int
	ToolRepeatLimit int
}

Limits groups loop-control knobs so upper layers can pass agent limits as a single value instead of repeating individual fields.

func (Limits) NormalizeForRuntime added in v0.2.7

func (l Limits) NormalizeForRuntime() Limits

NormalizeForRuntime applies channel/runtime defaults that historically treated <=0 values as unset for retries/repeat limits.

func (Limits) ToConfig added in v0.2.7

func (l Limits) ToConfig() Config

type LogOptions

type LogOptions struct {
	IncludeThoughts      bool
	IncludeToolParams    bool
	IncludeSkillContents bool

	MaxThoughtChars      int
	MaxJSONBytes         int
	MaxStringValueChars  int
	MaxSkillContentChars int

	RedactKeys []string
}

func DefaultLogOptions

func DefaultLogOptions() LogOptions

type Metrics

type Metrics struct {
	LLMRounds    int
	TotalTokens  int
	TotalCost    float64
	StartTime    time.Time
	ElapsedMs    int64
	ToolCalls    int
	ParseRetries int
}

type Option

type Option func(*Engine)

func WithFallbackFinal

func WithFallbackFinal(fn func() *Final) Option

func WithGuard

func WithGuard(g *guard.Guard) Option

func WithHook

func WithHook(h Hook) Option

func WithLogOptions

func WithLogOptions(o LogOptions) Option

func WithLogger

func WithLogger(l *slog.Logger) Option

func WithOnToolSuccess

func WithOnToolSuccess(fn func(*Context, string)) Option

func WithParamsBuilder

func WithParamsBuilder(fn func(RunOptions) map[string]any) Option

func WithPlanStepUpdate

func WithPlanStepUpdate(fn func(*Context, PlanStepUpdate)) Option

func WithPromptBuilder

func WithPromptBuilder(fn func(*tools.Registry, string) string) Option

WithPromptBuilder replaces the default system prompt builder. This hook is intended for tests in this repository.

func WithSkillAuthProfiles

func WithSkillAuthProfiles(authProfiles []string, enforce bool) Option

type PendingOutput

type PendingOutput struct {
	Status            string `json:"status"`
	ApprovalRequestID string `json:"approval_request_id"`
	Message           string `json:"message"`
}

PendingOutput is returned as Final.Output when the run is paused awaiting an external approval. It is intentionally small and safe to serialize (no raw tool params or secrets).

type Plan

type Plan struct {
	Thought string    `json:"thought,omitempty"`
	Steps   PlanSteps `json:"steps,omitempty"`
}

type PlanStep

type PlanStep struct {
	Step   string `json:"step"`
	Status string `json:"status,omitempty"` // pending|in_progress|completed
}

type PlanStepUpdate

type PlanStepUpdate struct {
	CompletedIndex int
	CompletedStep  string
	StartedIndex   int
	StartedStep    string
	Reason         string
}

type PlanSteps

type PlanSteps []PlanStep

func (*PlanSteps) UnmarshalJSON

func (s *PlanSteps) UnmarshalJSON(data []byte) error

type PromptBlock

type PromptBlock struct {
	Content string
}

type PromptSkill added in v0.1.2

type PromptSkill struct {
	Name         string
	FilePath     string
	Description  string
	Requirements []string
}

type PromptSpec

type PromptSpec struct {
	Identity string
	Rules    []string
	Skills   []PromptSkill
	Blocks   []PromptBlock
}

func DefaultPromptSpec

func DefaultPromptSpec() PromptSpec

type RunOptions

type RunOptions struct {
	Model   string
	History []llm.Message
	Meta    map[string]any
	// SkipTaskMessage suppresses appending task as a trailing user message.
	// Useful when the current user input is already represented in structured History.
	SkipTaskMessage bool
}

type Step

type Step struct {
	StepNumber  int
	Thought     string
	Action      string
	ActionInput map[string]any
	Observation string
	Error       error
	Duration    time.Duration
}

type ToolCall

type ToolCall struct {
	ID               string         `json:"tool_call_id,omitempty"`
	Type             string         `json:"tool_call_type,omitempty"`
	Thought          string         `json:"thought"`
	Name             string         `json:"tool_name"`
	Params           map[string]any `json:"tool_params"`
	RawArguments     string         `json:"raw_arguments,omitempty"`
	ThoughtSignature string         `json:"thought_signature,omitempty"`
}

Jump to

Keyboard shortcuts

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