Skip to content

Agent 技能

什么是 Agent Skills?

Agent Skills 是预定义的专用任务流程,类似于"宏"或"快捷方式",让 Claude 能够快速执行常见的开发任务。

核心特点:

  • 🚀 快速执行:一键完成复杂任务
  • 🎯 专业化:针对特定场景优化
  • 🔄 可复用:标准化工作流程
  • 📝 可定制:支持自定义 Skills

内置技能

/commit - 代码提交

用途: 智能创建 Git 提交

使用:

bash
You: /commit

Claude: I'll create a commit with your changes.

[Analyzing changes...]
Modified files:
  - src/app.js (authentication logic)
  - src/utils.js (helper functions)
  - tests/app.test.js (new tests)

[Creating commit...]
$ git add src/app.js src/utils.js tests/app.test.js
$ git commit -m "feat: implement user authentication

- Add JWT token generation
- Create login/logout endpoints
- Add authentication middleware
- Include comprehensive tests

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>"

✓ Commit created: abc123def

/review-pr - PR 审查

用途: 审查 Pull Request

使用:

bash
You: /review-pr 123

Claude: Reviewing PR #123...

📋 PR Analysis
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Title: Add user profile feature
Changes: +450 -120 across 8 files

 Strengths:
- Good test coverage (87%)
- Follows project conventions
- Clear commit messages

 Issues Found:
1. Security: SQL injection risk (user.js:45)
2. Performance: N+1 query (profile.js:78)
3. Style: Missing error handling (api.js:12)

💡 Suggestions:
- Use parameterized queries
- Add database indexing
- Implement error middleware
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

/feature-dev - 功能开发

用途: 引导完整功能开发

使用:

bash
You: /feature-dev "用户评论系统"

Claude: Starting feature development workflow...

[Phase 1: Understanding Requirements]
Please answer these questions:
1. Should comments support replies/threading?
2. Need moderation features?
3. Support markdown formatting?

You: 1. Yes 2. Yes 3. Yes

[Phase 2: Exploring Codebase]
Analyzing existing patterns...
 Found similar feature in forum module

[Phase 3: Architecture Design]
Creating architecture blueprint...
 Database schema designed
 API endpoints planned
 Frontend components outlined

[Phase 4: Implementation]
Creating files...
 models/Comment.js
 routes/comments.js
 components/CommentSection.jsx
 middleware/moderation.js

[Phase 5: Testing]
Generating tests...
 24 tests created and passing

[Phase 6: Documentation]
 API documentation generated
 Component usage guide created

Feature development complete! 🎉

/code-review - 代码审查

用途: 审查代码质量

使用:

bash
You: /code-review src/api.js

Claude: Reviewing src/api.js...

🔍 Code Review Report
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
File: src/api.js (250 lines)

 Good Practices:
- Clear function names
- Consistent error handling
- Good separation of concerns

 Issues:
1. [High] Security: No input validation (lines 45-50)
2. [Medium] Performance: Inefficient loop (line 78)
3. [Low] Style: Magic numbers (line 120)

📝 Recommendations:
- Add input validation middleware
- Use Array.filter instead of manual loop
- Extract constants to config file

Overall Score: 7/10
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

自定义技能

创建自定义 Skill

~/.config/claude-code/skills/ 目录下创建 skill 定义:

yaml
# deploy.skill.yaml
name: deploy
description: Deploy application to production
version: 1.0.0

steps:
  - name: Run Tests
    action: bash
    command: npm test

  - name: Build Production
    action: bash
    command: npm run build

  - name: Deploy
    action: bash
    command: ./deploy.sh production

  - name: Verify Deployment
    action: bash
    command: curl https://myapp.com/health

prompts:
  - question: "Which environment?"
    options: ["staging", "production"]
    default: "staging"

使用自定义 Skill

bash
You: /deploy

Claude: Running deployment skill...

Which environment?
 staging
    production

You: production

[Step 1/4] Running tests...
 All tests passed

[Step 2/4] Building production...
 Build complete

[Step 3/4] Deploying...
 Deployed successfully

[Step 4/4] Verifying deployment...
 Health check passed

Deployment complete! 🚀

Skill 开发指南

Skill 配置结构

yaml
name: skill-name
description: Skill description
version: 1.0.0
author: Your Name

# 前置条件
prerequisites:
  - npm
  - git

# 参数定义
parameters:
  - name: environment
    type: string
    required: true
    options: ["dev", "staging", "prod"]

# 步骤定义
steps:
  - name: Step Name
    action: bash | read | write | edit
    command: command to run
    on_error: continue | abort
    condition: expression

# 提示用户
prompts:
  - question: Question text
    variable: var_name
    options: [option1, option2]
    default: option1

# 输出
outputs:
  - name: result
    description: Output description

高级示例

yaml
name: database-backup
description: Backup database with verification
version: 1.0.0

parameters:
  - name: db_name
    type: string
    required: true

  - name: backup_type
    type: string
    options: ["full", "incremental"]
    default: "full"

steps:
  - name: Create Backup Directory
    action: bash
    command: mkdir -p ./backups/$(date +%Y%m%d)

  - name: Dump Database
    action: bash
    command: |
      pg_dump $DB_NAME > ./backups/$(date +%Y%m%d)/${db_name}.sql
    env:
      DB_NAME: "{{db_name}}"

  - name: Compress Backup
    action: bash
    command: gzip ./backups/$(date +%Y%m%d)/${db_name}.sql

  - name: Verify Backup
    action: bash
    command: |
      if [ -f "./backups/$(date +%Y%m%d)/${db_name}.sql.gz" ]; then
        echo "Backup verified"
      else
        exit 1
      fi
    on_error: abort

  - name: Upload to S3
    action: bash
    command: |
      aws s3 cp ./backups/$(date +%Y%m%d)/${db_name}.sql.gz \
        s3://my-backups/$(date +%Y%m%d)/
    condition: "{{backup_type}} == 'full'"

notifications:
  - type: success
    message: "Backup completed: {{db_name}}"
  - type: error
    message: "Backup failed for {{db_name}}"

小结

在本章中,我们学习了:

  • ✅ Agent Skills 的概念
  • ✅ 内置技能使用(/commit, /review-pr, /feature-dev等)
  • ✅ 创建自定义 Skills
  • ✅ Skill 开发指南和高级示例

关键要点:

  • Skills 标准化常见工作流程
  • 可以创建和分享自定义 Skills
  • 支持复杂的步骤和条件逻辑
  • 提高开发效率