🛠️ OpenClaw Skill Development

Skills开发——从零开始造轮子

什么是Skill?

世界上有一种工具叫"Skill",它就像Agent的"技能卡"。没有Skill的Agent就像没学过任何招式的格斗选手——只能乱打。

Skill定义了Agent在特定场景下的行为模式:什么时候触发、用什么工具、怎么处理输入输出、如何决策。一个好的Skill能让Agent从"菜鸟"进化成"专家"。

💡 Skill = 场景 + 触发条件 + 工具调用 + 决策逻辑 + 输出格式

Skill文件结构

# SKILLS/my_skill.md

# ═══════════════════════════════════════
# 基本信息
# ═══════════════════════════════════════
name: my_skill
version: 1.0.0
description: 这个Skill做什么,一句话说明
author: miaoquai
tags: [category, subcategory]

# ═══════════════════════════════════════
# 触发条件
# ═══════════════════════════════════════
trigger:
  type: keyword  # keyword | pattern | intent | always
  conditions:
    - "关键词1"
    - "关键词2"
  priority: 10  # 高优先级先匹配
  
  # 或使用意图匹配
  intent_match:
    patterns:
      - "帮我XX"
      - "我想XX"
    confidence: 0.8

# ═══════════════════════════════════════
# 输入定义
# ═══════════════════════════════════════
inputs:
  - name: user_message
    type: string
    required: true
    description: 用户输入的原始消息
    
  - name: context
    type: object
    required: false
    description: 上下文信息

# ═══════════════════════════════════════
# 工具调用
# ═══════════════════════════════════════
tools:
  - name: web_search
    when: "需要搜索外部信息"
    params:
      query: "${extracted_query}"
      
  - name: web_fetch
    when: "web_search有结果"
    params:
      url: "${web_search.top_result.url}"

# ═══════════════════════════════════════
# 处理流程
# ═══════════════════════════════════════
workflow:
  - step: parse_intent
    action: extract_user_intent
    
  - step: search
    condition: "intent == 'search'"
    tools: [web_search]
    
  - step: fetch
    condition: "search_results.length > 0"
    tools: [web_fetch]
    
  - step: synthesize
    action: generate_response
    model: gpt-4o

# ═══════════════════════════════════════
# 输出定义
# ═══════════════════════════════════════
output:
  type: structured
  format: markdown
  template: |
    ## 搜索结果摘要
    
    {{#each results}}
    - **{{title}}**: {{summary}}
    {{/each}}
    
  fallback: "抱歉,没有找到相关信息。"

# ═══════════════════════════════════════
# 错误处理
# ═══════════════════════════════════════
error_handling:
  - error: "web_search.timeout"
    action: retry
    max_retries: 3
    
  - error: "no_results"
    action: fallback_message
    
  - error: "*"
    action: notify_user

实战示例:新闻摘要Skill

# SKILLS/news_digest.md

name: news_digest
version: 2.0.0
description: 每日AI新闻摘要生成器
author: miaoquai

trigger:
  type: schedule
  schedule: "0 8 * * *"  # 每天8点
  
inputs:
  - name: date
    type: date
    default: "today"
    
workflow:
  - step: collect_news
    tools:
      - web_search:
          query: "AI人工智能 新闻 {{date}}"
          sources: ["techcrunch", "theverge", "wired"]
      - rss_fetch:
          feeds:
            - "https://openai.com/blog/rss.xml"
            - "https://anthropic.com/rss.xml"
    output: raw_news
    
  - step: filter_quality
    action: filter_by_relevance
    params:
      min_relevance: 0.7
      max_items: 10
    output: filtered_news
    
  - step: generate_summary
    model: gpt-4o
    prompt: |
      请用妙趣风格总结以下AI新闻:
      
      1. 每条新闻用一句话概括
      2. 加入幽默点评
      3. 标注新闻来源
      
      新闻列表:
      {{filtered_news}}
      
  - step: format_output
    action: apply_template
    
output:
  type: html
  file: /var/www/miaoquai/news/{{date}}.html
  
error_handling:
  - error: "no_news_found"
    action: generate_empty_report
    
  - error: "rss_fetch.fail"
    action: skip_feed
    continue: true

Skill测试方法

单元测试

# tests/news_digest_test.yaml

test_suite: news_digest

cases:
  - name: normal_flow
    input:
      date: "2026-05-07"
    expected:
      output_file_exists: true
      contains: ["AI", "新闻"]
      
  - name: no_news_day
    input:
      date: "2026-01-01"  # 假设这天没有新闻
    expected:
      output: "暂无新闻"
      
  - name: rss_fail
    mock:
      rss_fetch: error
    expected:
      status: success  # 应该继续执行
      contains: "部分来源不可用"

# 运行测试
# openclaw skill test news_digest

集成测试

# 集成测试 - 真实环境运行
integration_test:
  environment: staging
  duration: 24h
  metrics:
    - success_rate
    - output_quality

Skill发布流程

阶段操作要求
开发创建SKILLS文件文档完整
测试通过单元+集成测试成功率>95%
审核团队Review至少1人批准
Beta灰度发布10%无严重错误
Stable全量发布Beta表现正常

最佳实践

⚠️ 踩坑实录:有个Skill的触发条件写得太宽泛,导致它抢了其他Skill的活。结果同一个请求触发了3个Skill,互相打架,输出一团糟。教训:Skill边界要清晰,优先级要合理设置。

相关链接

#Skills开发 #触发条件 #工作流 #测试方法 #OpenClaw