planner

package
v0.5.12 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package planner 提供 AI Agent 的规划能力

规划器负责将复杂任务分解为可执行的步骤序列。

支持的规划器类型:

  • SequentialPlanner: 顺序规划器,生成线性步骤序列
  • StepwisePlanner: 逐步规划器,边执行边规划
  • ActionPlanner: 动作规划器,选择单一最佳动作

使用示例:

planner := planner.NewSequentialPlanner(
    planner.WithSequentialPlannerLLM(llmProvider),
    planner.WithSequentialPlannerTools(tools...),
)
plan, err := planner.Plan(ctx, "完成用户注册流程")

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	// Type 动作类型
	Type ActionType `json:"type"`

	// Name 动作名称 (工具名/Agent名等)
	Name string `json:"name"`

	// Parameters 动作参数
	Parameters map[string]any `json:"parameters,omitempty"`

	// Description 动作描述
	Description string `json:"description,omitempty"`
}

Action 执行动作

type ActionDefinition

type ActionDefinition struct {
	Name        string
	Description string
	Parameters  map[string]any
}

ActionDefinition 动作定义

type ActionPlanner

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

ActionPlanner 动作规划器 选择单一最佳动作

func NewActionPlanner

func NewActionPlanner(actions []ActionDefinition, opts ...ActionPlannerOption) *ActionPlanner

NewActionPlanner 创建动作规划器

func (*ActionPlanner) Name

func (p *ActionPlanner) Name() string

Name 返回规划器名称

func (*ActionPlanner) Plan

func (p *ActionPlanner) Plan(ctx context.Context, goal string, opts ...PlanOption) (*Plan, error)

Plan 创建执行计划 (单一动作)

func (*ActionPlanner) Replan

func (p *ActionPlanner) Replan(ctx context.Context, plan *Plan, feedback string) (*Plan, error)

Replan 根据执行结果重新规划

type ActionPlannerOption

type ActionPlannerOption func(*ActionPlanner)

ActionPlannerOption 动作规划器选项

func WithActionPlannerLLM

func WithActionPlannerLLM(provider llm.Provider) ActionPlannerOption

WithActionPlannerLLM 设置 LLM Provider

func WithActionPlannerName

func WithActionPlannerName(name string) ActionPlannerOption

WithActionPlannerName 设置规划器名称

type ActionType

type ActionType string

ActionType 动作类型

const (
	// ActionTypeTool 工具调用
	ActionTypeTool ActionType = "tool"
	// ActionTypeAgent Agent 调用
	ActionTypeAgent ActionType = "agent"
	// ActionTypeLLM LLM 调用
	ActionTypeLLM ActionType = "llm"
	// ActionTypeFunction 函数调用
	ActionTypeFunction ActionType = "function"
	// ActionTypeSubPlan 子计划
	ActionTypeSubPlan ActionType = "subplan"
)

type Executor

type Executor interface {
	// Execute 执行计划
	Execute(ctx context.Context, plan *Plan) error

	// ExecuteStep 执行单个步骤
	ExecuteStep(ctx context.Context, step *Step) (*StepResult, error)

	// Pause 暂停执行
	Pause(ctx context.Context, plan *Plan) error

	// Resume 恢复执行
	Resume(ctx context.Context, plan *Plan) error

	// Cancel 取消执行
	Cancel(ctx context.Context, plan *Plan) error
}

Executor 计划执行器

type Plan

type Plan struct {
	// ID 计划唯一标识
	ID string `json:"id"`

	// Goal 目标描述
	Goal string `json:"goal"`

	// Steps 执行步骤
	Steps []*Step `json:"steps"`

	// State 计划状态
	State PlanState `json:"state"`

	// Metadata 元数据
	Metadata map[string]any `json:"metadata,omitempty"`

	// CreatedAt 创建时间
	CreatedAt time.Time `json:"created_at"`

	// UpdatedAt 更新时间
	UpdatedAt time.Time `json:"updated_at"`
}

Plan 执行计划

type PlanOption

type PlanOption func(*planConfig)

PlanOption 规划选项

func WithAvailableTools

func WithAvailableTools(tools ...string) PlanOption

WithAvailableTools 设置可用工具列表

func WithMaxSteps

func WithMaxSteps(n int) PlanOption

WithMaxSteps 设置最大步骤数

func WithPlanContext

func WithPlanContext(ctx map[string]any) PlanOption

WithPlanContext 设置规划上下文

func WithPlanTimeout

func WithPlanTimeout(d time.Duration) PlanOption

WithPlanTimeout 设置规划超时时间

type PlanState

type PlanState string

PlanState 计划状态

const (
	// PlanStatePending 待执行
	PlanStatePending PlanState = "pending"
	// PlanStateRunning 执行中
	PlanStateRunning PlanState = "running"
	// PlanStateCompleted 已完成
	PlanStateCompleted PlanState = "completed"
	// PlanStateFailed 失败
	PlanStateFailed PlanState = "failed"
	// PlanStateCanceled 已取消
	PlanStateCanceled PlanState = "canceled"
)

type Planner

type Planner interface {
	// Name 返回规划器名称
	Name() string

	// Plan 创建执行计划
	Plan(ctx context.Context, goal string, opts ...PlanOption) (*Plan, error)

	// Replan 根据执行结果重新规划
	Replan(ctx context.Context, plan *Plan, feedback string) (*Plan, error)
}

Planner 规划器接口

type SequentialPlanner

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

SequentialPlanner 顺序规划器 生成线性执行的步骤序列

func NewSequentialPlanner

func NewSequentialPlanner(opts ...SequentialPlannerOption) *SequentialPlanner

NewSequentialPlanner 创建顺序规划器

func (*SequentialPlanner) Name

func (p *SequentialPlanner) Name() string

Name 返回规划器名称

func (*SequentialPlanner) Plan

func (p *SequentialPlanner) Plan(ctx context.Context, goal string, opts ...PlanOption) (*Plan, error)

Plan 创建执行计划

func (*SequentialPlanner) Replan

func (p *SequentialPlanner) Replan(ctx context.Context, plan *Plan, feedback string) (*Plan, error)

Replan 根据执行结果重新规划

type SequentialPlannerOption

type SequentialPlannerOption func(*SequentialPlanner)

SequentialPlannerOption 顺序规划器选项

func WithSequentialPlannerLLM

func WithSequentialPlannerLLM(provider llm.Provider) SequentialPlannerOption

WithSequentialPlannerLLM 设置 LLM Provider

func WithSequentialPlannerName

func WithSequentialPlannerName(name string) SequentialPlannerOption

WithSequentialPlannerName 设置规划器名称

func WithSequentialPlannerTools

func WithSequentialPlannerTools(tools ...ToolInfo) SequentialPlannerOption

WithSequentialPlannerTools 设置可用工具

type Step

type Step struct {
	// ID 步骤唯一标识
	ID string `json:"id"`

	// Index 步骤序号
	Index int `json:"index"`

	// Description 步骤描述
	Description string `json:"description"`

	// Action 执行动作
	Action *Action `json:"action"`

	// State 步骤状态
	State StepState `json:"state"`

	// Dependencies 依赖的步骤 ID
	Dependencies []string `json:"dependencies,omitempty"`

	// Result 执行结果
	Result *StepResult `json:"result,omitempty"`

	// Metadata 元数据
	Metadata map[string]any `json:"metadata,omitempty"`
}

Step 执行步骤

type StepResult

type StepResult struct {
	// Success 是否成功
	Success bool `json:"success"`

	// Output 输出内容
	Output any `json:"output,omitempty"`

	// Error 错误信息
	Error string `json:"error,omitempty"`

	// Duration 执行时长 (毫秒)
	Duration int64 `json:"duration_ms"`

	// Tokens 消耗的 Token 数
	Tokens int `json:"tokens,omitempty"`
}

StepResult 步骤执行结果

type StepState

type StepState string

StepState 步骤状态

const (
	// StepStatePending 待执行
	StepStatePending StepState = "pending"
	// StepStateRunning 执行中
	StepStateRunning StepState = "running"
	// StepStateCompleted 已完成
	StepStateCompleted StepState = "completed"
	// StepStateFailed 失败
	StepStateFailed StepState = "failed"
	// StepStateSkipped 已跳过
	StepStateSkipped StepState = "skipped"
)

type StepwisePlanner

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

StepwisePlanner 逐步规划器 边执行边规划,每次只规划下一步

func NewStepwisePlanner

func NewStepwisePlanner(opts ...StepwisePlannerOption) *StepwisePlanner

NewStepwisePlanner 创建逐步规划器

func (*StepwisePlanner) Name

func (p *StepwisePlanner) Name() string

Name 返回规划器名称

func (*StepwisePlanner) Plan

func (p *StepwisePlanner) Plan(ctx context.Context, goal string, opts ...PlanOption) (*Plan, error)

Plan 创建执行计划 (初始只规划第一步)

func (*StepwisePlanner) PlanNextStep

func (p *StepwisePlanner) PlanNextStep(ctx context.Context, plan *Plan, lastResult *StepResult) (*Step, error)

PlanNextStep 规划下一步

func (*StepwisePlanner) Replan

func (p *StepwisePlanner) Replan(ctx context.Context, plan *Plan, feedback string) (*Plan, error)

Replan 根据执行结果重新规划

type StepwisePlannerOption

type StepwisePlannerOption func(*StepwisePlanner)

StepwisePlannerOption 逐步规划器选项

func WithStepwisePlannerLLM

func WithStepwisePlannerLLM(provider llm.Provider) StepwisePlannerOption

WithStepwisePlannerLLM 设置 LLM Provider

func WithStepwisePlannerMaxSteps

func WithStepwisePlannerMaxSteps(n int) StepwisePlannerOption

WithStepwisePlannerMaxSteps 设置最大步骤数

func WithStepwisePlannerName

func WithStepwisePlannerName(name string) StepwisePlannerOption

WithStepwisePlannerName 设置规划器名称

func WithStepwisePlannerTools

func WithStepwisePlannerTools(tools ...ToolInfo) StepwisePlannerOption

WithStepwisePlannerTools 设置可用工具

type ToolInfo

type ToolInfo struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Parameters  map[string]any `json:"parameters,omitempty"`
}

ToolInfo 工具信息

Jump to

Keyboard shortcuts

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