agent

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatResult

func FormatResult(r *AgentResult) string

FormatResult 格式化执行结果为可读文本

Types

type AgentLoop

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

AgentLoop ReAct 循环引擎 执行模式:思考 → 行动 → 观察 → 循环,直到 Agent 给出最终回答

func NewAgentLoop

func NewAgentLoop(model chatmodel.BaseModel, registry *tools.ToolRegistry, opts ...Option) *AgentLoop

NewAgentLoop 创建 ReAct 循环引擎

func (*AgentLoop) Run

func (al *AgentLoop) Run(ctx context.Context, userMessage string) (*AgentResult, error)

Run 执行 ReAct 循环

func (*AgentLoop) RunStream

func (al *AgentLoop) RunStream(ctx context.Context, userMessage string, onChunk func(msg *schema.Message, isToolCall bool) bool) (*AgentResult, error)

RunStream 流式执行 ReAct 循环

type AgentResult

type AgentResult struct {
	Answer string
	Steps  []*Step
	Rounds int
	Usage  schema.Usage
}

AgentResult ReAct 循环执行结果

type ExecStep

type ExecStep struct {
	PlanStep    PlanStep
	AgentResult *AgentResult
	Status      StepStatus
	Error       string
	Duration    time.Duration
}

ExecStep 执行步骤记录

type Option

type Option func(*AgentLoop)

Option AgentLoop 配置选项

func WithMaxMessages

func WithMaxMessages(n int) Option

WithMaxMessages 设置消息窗口大小(防止长任务内存膨胀,0=不限制) 保留系统消息 + 最近 N 条消息

func WithMaxRounds

func WithMaxRounds(n int) Option

WithMaxRounds 设置最大循环次数(0 = 不限制)

func WithStepCallback

func WithStepCallback(fn func(step *Step)) Option

WithStepCallback 设置每步回调

func WithSystemPrompt

func WithSystemPrompt(prompt string) Option

WithSystemPrompt 设置系统提示词

type PlanExecutor

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

PlanExecutor Plan-and-Execute 策略引擎 Phase 1: LLM 生成执行计划(自然语言步骤) Phase 2: 验证计划(依赖检查) Phase 3: 逐步执行,每步用 ReAct 循环 Phase 4: 验证结果,必要时重规划

func NewPlanExecutor

func NewPlanExecutor(model chatmodel.BaseModel, registry *tools.ToolRegistry, opts ...PlanOption) *PlanExecutor

NewPlanExecutor 创建 Plan-and-Execute 引擎

func (*PlanExecutor) Run

func (pe *PlanExecutor) Run(ctx context.Context, goal string) (*PlanResult, error)

Run 执行 Plan-and-Execute 流程

type PlanOption

type PlanOption func(*PlanExecutor)

PlanOption PlanExecutor 配置选项

func WithPlanCallback

func WithPlanCallback(fn func(steps []PlanStep)) PlanOption

func WithPlanMaxPlanRounds

func WithPlanMaxPlanRounds(n int) PlanOption

func WithPlanMaxReplans

func WithPlanMaxReplans(n int) PlanOption

func WithPlanMaxRoundsPerStep

func WithPlanMaxRoundsPerStep(n int) PlanOption

func WithPlanMaxSteps

func WithPlanMaxSteps(n int) PlanOption

func WithPlanPersistPath

func WithPlanPersistPath(path string) PlanOption

func WithPlanStepCallback

func WithPlanStepCallback(fn func(step *ExecStep)) PlanOption

func WithPlanSystemPrompt

func WithPlanSystemPrompt(prompt string) PlanOption

type PlanResult

type PlanResult struct {
	Answer    string
	Steps     []*ExecStep
	PlanSteps []PlanStep
	Rounds    int // 总 ReAct 轮次
	Replans   int // 重规划次数
	Usage     schema.Usage
}

PlanResult 规划执行结果

type PlanStep

type PlanStep struct {
	ID          string   `json:"id"`
	Description string   `json:"description"`
	DependsOn   []string `json:"depends_on,omitempty"`
}

PlanStep 计划中的单个步骤

type Step

type Step struct {
	Round       int
	Thinking    string              // Agent 的思考内容
	ToolCalls   []schema.ToolCall   // 本轮工具调用
	ToolResults []schema.ToolResult // 工具执行结果
	Response    string              // 最终回答(最后一轮)
	Duration    time.Duration
	IsFinal     bool // 是否是最终回答
}

Step ReAct 单步记录

type StepStatus

type StepStatus string

StepStatus 步骤状态

const (
	StepPending StepStatus = "pending"
	StepRunning StepStatus = "running"
	StepSuccess StepStatus = "success"
	StepFailed  StepStatus = "failed"
	StepSkipped StepStatus = "skipped"
)

Jump to

Keyboard shortcuts

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