🔧 Tool Composition

工具组合的艺术——让1+1>2,让Agent会"十八般武艺"

"一个工具只能做一件事。但工具组合起来,就能做整个世界。就像乐高积木,单个只是方块,组合起来就是城堡。OpenClaw Tool Composition让Agent拥有这种魔法。"

什么是Tool Composition?

Tool Composition是让多个工具协同工作的技术——就像把不同的App串联起来,让数据在工具间流动。一个工具的输出,成为另一个工具的输入,形成强大的处理链。

典型场景:

工具组合模式

模式1:顺序链式(Sequential Chain)

Tool A Tool B Tool C
# 顺序链式组合
# 用户请求 → web_search → web_fetch → feishu_doc

user_request: "搜索OpenClaw最新动态,总结后发到飞书文档"

execution:
  1. web_search:
      query: "OpenClaw latest updates 2026"
      output: search_results
      
  2. web_fetch:
      url: search_results[0].link
      output: page_content
      
  3. feishu_doc:
      action: append
      content: summarize(page_content)

模式2:并行执行(Parallel)

Tool A Tool B Tool C 汇总
# 并行组合
# 同时调用多个工具,最后汇总结果

parallel_execution:
  tasks:
    - tool: web_search
      query: "AI news today"
    - tool: github_search
      query: "openclaw issues"
    - tool: rss_fetch
      sources: ["openai.com/blog"]
      
  aggregation: |
    合并三个来源的结果
    去重、排序、生成日报

模式3:条件分支(Conditional)

判断 条件A: Tool X 条件B: Tool Y
# 条件组合
# 根据内容类型选择不同处理工具

conditional_execution:
  if: content_type == "pdf"
  then:
    - tool: pdf_parse
    - tool: text_extract
  elif: content_type == "image"
  then:
    - tool: vision_analyze
  else:
    - tool: web_fetch

模式4:循环迭代(Iterative)

# 循环组合
# 对列表中每个元素执行相同工具链

iterative_execution:
  items: file_list
  workflow:
    - tool: read
    - tool: analyze
    - tool: write_report
  max_iterations: 10
  stop_condition: "error_count > 3"

实战:构建内容自动化流水线

场景:每日AI新闻自动收集

# 创建工具组合Skill
# ~/.openclaw/skills/daily-news-skill/SKILL.md

name: daily-news-pipeline
description: 每日AI新闻自动收集与发布

workflow:
  # 第一步:并行获取多源内容
  - stage: fetch
    parallel:
      - tool: web_search
        args:
          query: "AI news today"
          limit: 10
      - tool: web_fetch
        args:
          url: "https://openai.com/blog"
      - tool: web_fetch
        args:
          url: "https://www.anthropic.com/news"
          
  # 第二步:内容处理
  - stage: process
    steps:
      - tool: text_extract
        input: $fetch_results
      - tool: deduplicate
        input: $extracted_content
      - tool: summarize
        args:
          style: "妙趣风格"
          max_length: 500
          
  # 第三步:多渠道发布
  - stage: publish
    parallel:
      - tool: write
        args:
          path: "/var/www/miaoquai/news/${date}.html"
          content: $processed_content
      - tool: message
        args:
          channel: "feishu"
          content: $processed_content
      - tool: message
        args:
          channel: "discord"
          content: $summary

调用工具组合

# 用户调用组合Skill
prompt: |
  执行daily-news-pipeline,今天的AI新闻
  用妙趣风格生成日报
💡 Pro Tip:工具组合的关键是数据流设计——每个工具的输出格式必须兼容下一个工具的输入格式。建议先用1-2个工具跑通流程,再逐步扩展。

工具依赖管理

# 定义工具依赖
# TOOLS.md
tools:
  web_search:
    requires: []
    output: search_results[]
    
  web_fetch:
    requires: [url]
    output: page_content
    
  feishu_doc:
    requires: [content, doc_token]
    output: {success: bool, url: string}

# 验证工具链完整性
openclaw tools validate --chain "search→fetch→doc"

最佳实践

  1. 单一职责 - 每个工具做一件事,做好它
  2. 显式数据流 - 不要隐式依赖上下文
  3. 错误处理 - 每个环节都有失败策略
  4. 幂等设计 - 同样输入总产生同样输出
  5. 可观测性 - 每步记录日志和耗时
  6. 逐步组合 - 先跑通再优化,别一口气吃成胖子

相关资源