Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

工具执行模式

ToolExecutionMode 决定 LLM 在同一轮推理里返回多个 tool call 时,运行时如何执行这些调用。

ToolExecutionMode

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ToolExecutionMode {
    #[default]
    Sequential,
    ParallelBatchApproval,
    ParallelStreaming,
}

默认值是 Sequential

模式

Sequential

按顺序逐个执行 tool call。

  • 每个调用之间都会刷新状态快照。
  • 遇到挂起会在第一个挂起点停止后续调用。
  • 适合 tool 之间有数据依赖或顺序很重要的场景。

ParallelBatchApproval

并发执行所有 tool call,所有调用看到的是同一份冻结快照。

  • 对挂起决策采用批量回放。
  • 会做并行补丁冲突检查。
  • 不会因单个失败或挂起而提前停止其它调用。

ParallelStreaming

同样并发执行全部调用,但挂起决策一到就立刻回放,不等待其他挂起调用。

  • 适合独立工具很多、希望尽快恢复执行的场景。

对比

行为SequentialParallelBatchApprovalParallelStreaming
执行顺序串行全并发全并发
状态可见性每次调用前刷新冻结快照冻结快照
遇挂起是否停止
遇失败是否停止
decision 回放不适用批量即时
冲突检查

Executor trait

#[async_trait]
pub trait ToolExecutor: Send + Sync {
    async fn execute(
        &self,
        tools: &HashMap<String, Arc<dyn Tool>>,
        calls: &[ToolCall],
        base_ctx: &ToolCallContext,
    ) -> Result<Vec<ToolExecutionResult>, ToolExecutorError>;

    fn name(&self) -> &'static str;

    fn requires_incremental_state(&self) -> bool { false }
}

DecisionReplayPolicy

并行模式下,DecisionReplayPolicy 决定挂起 tool call 的恢复决策何时回放:

pub enum DecisionReplayPolicy {
    Immediate,
    BatchAllSuspended,
}

关键文件

  • crates/awaken-contract/src/contract/executor.rs
  • crates/awaken-runtime/src/execution/executor.rs

相关