executor

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AgentExecutor

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

AgentExecutor Agent 执行器

提供高级的执行逻辑,包括: - 记忆管理 - 对话历史 - 工具管理 - 错误处理和重试 - 早停机制

func NewAgentExecutor

func NewAgentExecutor(config ExecutorConfig) *AgentExecutor

NewAgentExecutor 创建 Agent 执行器

func (*AgentExecutor) Batch

func (e *AgentExecutor) Batch(
	ctx context.Context,
	inputs []*agentcore.AgentInput,
) ([]*agentcore.AgentOutput, error)

Batch 批量执行

func (*AgentExecutor) Execute

Execute 执行 Agent 输入

func (*AgentExecutor) ExecuteWithCallbacks

func (e *AgentExecutor) ExecuteWithCallbacks(
	ctx context.Context,
	input *agentcore.AgentInput,
	callbacks ...agentcore.Callback,
) (*agentcore.AgentOutput, error)

ExecuteWithCallbacks 使用回调执行

func (*AgentExecutor) GetMemory

func (e *AgentExecutor) GetMemory() Memory

GetMemory 获取记忆系统

func (*AgentExecutor) GetTools

func (e *AgentExecutor) GetTools() []interfaces.Tool

GetTools 获取可用工具

func (*AgentExecutor) Run

func (e *AgentExecutor) Run(ctx context.Context, input string) (string, error)

Run 执行 Agent

func (*AgentExecutor) RunGenerator added in v0.2.0

RunGenerator 使用 Generator 模式执行(实验性功能)

RunGenerator 提供零分配的流式执行,相比 Stream 具有以下优势:

  • 零内存分配(无 channel、goroutine 开销)
  • 支持早期终止(用户可以在任意步骤 break)
  • 更低延迟(无 channel 发送/接收开销)

AgentExecutor 的 RunGenerator 会:

  1. 应用执行超时
  2. 加载记忆历史到 input.Context
  3. 调用底层 agent 的 RunGenerator(如果支持)
  4. 在每个步骤后保存记忆(如果配置了 memory)

使用示例:

executor := executor.NewAgentExecutor(config)
for output, err := range executor.RunGenerator(ctx, input) {
    if err != nil {
        log.Error("step failed", err)
        continue
    }
    fmt.Printf("Step: %s\n", output.Message)
    if output.Status == interfaces.StatusSuccess {
        break  // 任务完成
    }
}

注意:如果底层 agent 不支持 RunGenerator(即不实现 GeneratorAgent 接口), 将回退到调用 Invoke 并产生单个结果

func (*AgentExecutor) SetMemory

func (e *AgentExecutor) SetMemory(mem Memory)

SetMemory 设置记忆系统

func (*AgentExecutor) SetVerbose

func (e *AgentExecutor) SetVerbose(verbose bool)

SetVerbose 设置详细输出

func (*AgentExecutor) Stream

Stream 流式执行

type ConversationChain

type ConversationChain struct {
	*AgentExecutor
	// contains filtered or unexported fields
}

ConversationChain 对话链

专门用于对话场景的执行器

func NewConversationChain

func NewConversationChain(agent agentcore.Agent, mem Memory) *ConversationChain

NewConversationChain 创建对话链

func (*ConversationChain) Chat

func (c *ConversationChain) Chat(ctx context.Context, message string, sessionID string) (string, error)

Chat 进行对话

func (*ConversationChain) ClearMemory

func (c *ConversationChain) ClearMemory(ctx context.Context, sessionID string) error

ClearMemory 清除对话记忆

func (*ConversationChain) GetHistory

func (c *ConversationChain) GetHistory(ctx context.Context, sessionID string) ([]map[string]interface{}, error)

GetHistory 获取对话历史

type ExecutorConfig

type ExecutorConfig struct {
	Agent               agentcore.Agent   // Agent 实例
	Tools               []interfaces.Tool // 可用工具
	Memory              Memory            // 记忆系统
	MaxIterations       int               // 最大迭代次数
	MaxExecutionTime    time.Duration     // 最大执行时间
	EarlyStoppingMethod string            // 早停方法: "force", "generate"
	HandleParsingErrors bool              // 是否处理解析错误
	ReturnIntermSteps   bool              // 是否返回中间步骤
	Verbose             bool              // 是否详细输出
}

ExecutorConfig 执行器配置

type GeneratorAgent added in v0.2.0

type GeneratorAgent interface {
	agentcore.Agent
	RunGenerator(ctx context.Context, input *agentcore.AgentInput) agentcore.Generator[*agentcore.AgentOutput]
}

GeneratorAgent 定义支持 RunGenerator 的 Agent 接口(可选)

如果底层 Agent 实现了此接口,ExecutorAgent.RunGenerator 将使用它进行流式执行。 否则,将回退到调用 Invoke 并产生单个结果。

type Memory

type Memory interface {
	// SaveContext 保存上下文
	SaveContext(ctx context.Context, sessionID string, input, output map[string]interface{}) error

	// LoadHistory 加载历史
	LoadHistory(ctx context.Context, sessionID string) ([]map[string]interface{}, error)

	// Clear 清除记忆
	Clear(ctx context.Context, sessionID string) error
}

Memory 定义记忆系统接口(简化版,适配 memory.Manager)

Jump to

Keyboard shortcuts

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