{/* 本页面由 website/scripts/generate-skill-docs.py 从技能 SKILL.md 自动生成。请编辑源 SKILL.md 而非本页面。 */}

Claude Code

将编码委托给 Claude Code CLI(功能、PR)。

技能元数据

来源内置(默认安装)
路径skills/autonomous-ai-agents/claude-code
版本2.2.0
作者Hermes Agent + Teknium
许可证MIT
平台linux, macos, windows
标签Coding-Agent, Claude, Anthropic, Code-Review, Refactoring, PTY, Automation
相关技能codex, hermes-agent, opencode

参考:完整 SKILL.md

:::info 以下是此技能被触发时 Hermes 加载的完整技能定义。这是技能激活时代理所看到的指令。 :::

Claude Code — Hermes 编排指南

将编码任务委托给 Claude Code(Anthropic 的自主编码代理 CLI),通过 Hermes 终端实现。Claude Code v2.x 可以自主读取文件、编写代码、运行 shell 命令、生成子代理和管理 git 工作流。

前提条件

  • 安装: npm install -g @anthropic-ai/claude-code
  • 认证: 运行一次 claude 登录(Pro/Max 的浏览器 OAuth,或设置 ANTHROPIC_API_KEY
  • 控制台认证: claude auth login --console 用于 API 密钥计费
  • SSO 认证: claude auth login --sso 用于企业版
  • 检查状态: claude auth status(JSON)或 claude auth status --text(人类可读)
  • 健康检查: claude doctor — 检查自动更新程序和安装健康
  • 版本检查: claude --version(需要 v2.x+)
  • 更新: claude updateclaude upgrade

两种编排模式

Hermes 以两种根本不同的方式与 Claude Code 交互。根据任务选择。

模式 1:打印模式(-p)— 非交互(大多数任务首选)

打印模式运行一次性任务,返回结果并退出。无需 PTY。无需交互式提示。这是最干净的集成路径。

terminal(command="claude -p 'Add error handling to all API calls in src/' --allowedTools 'Read,Edit' --max-turns 10", workdir="/path/to/project", timeout=120)

何时使用打印模式:

  • 一次性编码任务(修复 bug、添加功能、重构)
  • CI/CD 自动化和脚本
  • 使用 --json-schema 的结构化数据提取
  • 管道输入处理(cat file | claude -p "analyze this"
  • 任何不需要多轮对话的任务

打印模式跳过所有交互式对话框 — 无工作空间信任提示,无权限确认。使其成为自动化的理想选择。

模式 2:通过 tmux 的交互式 PTY — 多轮会话

交互式模式提供完整的对话式 REPL,你可以发送后续提示、使用斜杠命令并实时观察 Claude 工作。需要 tmux 编排。

# 启动 tmux 会话
terminal(command="tmux new-session -d -s claude-work -x 140 -y 40")

# 在其中启动 Claude Code
terminal(command="tmux send-keys -t claude-work 'cd /path/to/project && claude' Enter")

# 等待启动,然后发送任务
#(约 3-5 秒后)
terminal(command="sleep 5 && tmux send-keys -t claude-work 'Refactor the auth module to use JWT tokens' Enter")

# 通过捕获窗格监控进度
terminal(command="sleep 15 && tmux capture-pane -t claude-work -p -S -50")

# 发送后续任务
terminal(command="tmux send-keys -t claude-work 'Now add unit tests for the new JWT code' Enter")

# 完成时退出
terminal(command="tmux send-keys -t claude-work '/exit' Enter")

何时使用交互式模式:

  • 多轮迭代工作(重构 → 审查 → 修复 → 测试循环)
  • 需要人参与决策的任务
  • 探索性编码会话
  • 当需要使用 Claude 的斜杠命令时(/compact/review/model

PTY 对话框处理(交互式模式关键)

Claude Code 在首次启动时可能显示最多两个确认对话框。你必须通过 tmux send-keys 处理它们:

对话框 1:工作空间信任(首次进入目录)

❯ 1. Yes, I trust this folder    ← 默认(只需按 Enter)
  2. No, exit

处理: tmux send-keys -t <session> Enter — 默认选择正确。

对话框 2:绕过权限警告(仅与 —dangerously-skip-permissions 一起)

❯ 1. No, exit                    ← 默认(错误的选择!)
  2. Yes, I accept

处理: 必须先向下导航,然后 Enter:

tmux send-keys -t <session> Down && sleep 0.3 && tmux send-keys -t <session> Enter

稳健的对话框处理模式

# 使用权限绕过启动
terminal(command="tmux send-keys -t claude-work 'claude --dangerously-skip-permissions \"your task\"' Enter")

# 处理信任对话框(Enter 选择默认的"是")
terminal(command="sleep 4 && tmux send-keys -t claude-work Enter")

# 处理权限对话框(向下然后 Enter 选择"是,我接受")
terminal(command="sleep 3 && tmux send-keys -t claude-work Down && sleep 0.3 && tmux send-keys -t claude-work Enter")

# 现在等待 Claude 工作
terminal(command="sleep 15 && tmux capture-pane -t claude-work -p -S -60")

注意: 首次接受某个目录的信任后,信任对话框不再出现。仅在使用 --dangerously-skip-permissions 时权限对话框会每次出现。

CLI 子命令

子命令用途
claude启动交互式 REPL
claude "query"使用初始提示启动 REPL
claude -p "query"打印模式(非交互,完成后退出)
cat file | claude -p "query"将内容作为 stdin 上下文管道输入
claude -c继续此目录中最近的对话
claude -r "id"按 ID 或名称恢复特定会话
claude auth login登录(添加 --console 用于 API 计费,--sso 用于企业版)
claude auth status检查登录状态(返回 JSON;--text 用于人类可读)
claude mcp add <name> -- <cmd>添加 MCP 服务器
claude mcp list列出配置的 MCP 服务器
claude mcp remove <name>移除 MCP 服务器
claude agents列出配置的代理
claude doctor运行健康检查
claude update / claude upgrade将 Claude Code 更新到最新版本
claude remote-control启动服务器以从 claude.ai 或移动应用控制 Claude
claude install [target]安装原生构建版本
claude setup-token设置长期有效认证令牌
claude plugin / claude plugins管理 Claude Code 插件
claude auto-mode检查自动模式分类器配置

打印模式深入

结构化 JSON 输出

terminal(command="claude -p 'Analyze auth.py for security issues' --output-format json --max-turns 5", workdir="/project", timeout=120)

返回包含以下内容的 JSON 对象:

{
  "type": "result",
  "subtype": "success",
  "result": "The analysis text...",
  "session_id": "75e2167f-...",
  "num_turns": 3,
  "total_cost_usd": 0.0787,
  "duration_ms": 10276,
  "stop_reason": "end_turn",
  "terminal_reason": "completed",
  "usage": { "input_tokens": 5, "output_tokens": 603, ... },
  "modelUsage": { "claude-sonnet-4-6": { "costUSD": 0.078, "contextWindow": 200000 } }
}

关键字段: session_id 用于恢复,num_turns 用于代理循环计数,total_cost_usd 用于支出跟踪,subtype 用于成功/错误检测(successerror_max_turnserror_budget)。

管道输入

# 管道输入文件以供分析
terminal(command="cat src/auth.py | claude -p 'Review this code for bugs' --max-turns 1", timeout=60)

# 管道输入多个文件
terminal(command="cat src/*.py | claude -p 'Find all TODO comments' --max-turns 1", timeout=60)

# 管道输入命令输出
terminal(command="git diff HEAD~3 | claude -p 'Summarize these changes' --max-turns 1", timeout=60)

裸模式用于 CI/脚本

terminal(command="claude --bare -p 'Run all tests and report failures' --allowedTools 'Read,Bash' --max-turns 10", workdir="/project", timeout=180)

--bare 跳过钩子、插件、MCP 发现和 CLAUDE.md 加载。启动最快。需要 ANTHROPIC_API_KEY(跳过 OAuth)。

完整 CLI 标志参考

会话与环境

标志效果
-p, --print非交互式一次性模式(完成后退出)
-c, --continue恢复当前目录中最近的对话
-r, --resume <id>按 ID 或名称恢复特定会话
--fork-session恢复时创建新会话 ID 而非重用原始 ID
--session-id <uuid>为对话使用特定 UUID
--no-session-persistence不将会话保存到磁盘
--add-dir <paths...>授予 Claude 对其他工作目录的访问权限
-w, --worktree [name]在隔离的 git worktree 中运行
--tmux为 worktree 创建 tmux 会话(需要 --worktree
--ide自动连接到有效的 IDE
--from-pr [number]恢复链接到特定 GitHub PR 的会话

模型与性能

标志效果
--model <alias>模型选择:sonnetopushaiku 或完整名称
--effort <level>推理深度:lowmediumhighmaxauto
--max-turns <n>限制代理循环
--max-budget-usd <n>API 支出上限(美元)
--fallback-model <model>默认模型过载时自动回退

权限与安全

标志效果
--dangerously-skip-permissions自动批准所有工具使用
--allowedTools <tools...>白名单特定工具
--disallowedTools <tools...>黑名单特定工具
--permission-mode <mode>权限模式选择

系统提示与上下文

标志效果
--append-system-prompt <text>向默认系统提示追加文本
--bare跳过钩子、插件、MCP 发现、CLAUDE.md、OAuth

设置与配置

设置层级(优先级从高到低)

  1. CLI 标志 — 覆盖一切
  2. 本地项目: .claude/settings.local.json(个人,git 忽略)
  3. 项目: .claude/settings.json(共享,git 跟踪)
  4. 用户: ~/.claude/settings.json(全局)

内存文件(CLAUDE.md)层级

  1. 全局: ~/.claude/CLAUDE.md — 适用于所有项目
  2. 项目: ./CLAUDE.md — 项目特定上下文(git 跟踪)
  3. 本地: .claude/CLAUDE.local.md — 个人项目覆盖(git 忽略)

交互式会话:斜杠命令

会话与上下文

命令用途
/help显示所有命令
/compact [focus]压缩上下文以节省令牌
/clear清空对话历史以全新开始
/context可视化上下文使用情况
/cost查看令牌使用情况
/resume切换到或恢复不同会话
/rewind回滚到之前的检查点
/btw <question>询问旁侧问题
/exitCtrl+D结束会话

开发与审查

命令用途
/review请求代码审查
/security-review执行安全分析
/plan [description]进入计划模式
/loop [interval]在会话内安排重复任务
/batch为大型并行更改自动创建工作树

并行 Claude 实例

同时运行多个独立的 Claude 任务:

# 任务 1:修复后端
terminal(command="tmux new-session -d -s task1 -x 140 -y 40 && tmux send-keys -t task1 'cd ~/project && claude -p \"Fix the auth bug in src/auth.py\" --allowedTools \"Read,Edit\" --max-turns 10' Enter")

# 任务 2:编写测试
terminal(command="tmux new-session -d -s task2 -x 140 -y 40 && tmux send-keys -t task2 'cd ~/project && claude -p \"Write integration tests for the API endpoints\" --allowedTools \"Read,Write,Bash\" --max-turns 15' Enter")

# 任务 3:更新文档
terminal(command="tmux new-session -d -s task3 -x 140 -y 40 && tmux send-keys -t task3 'cd ~/project && claude -p \"Update README.md with the new API endpoints\" --allowedTools \"Read,Edit\" --max-turns 5' Enter")

# 监控所有
terminal(command="sleep 30 && for s in task1 task2 task3; do echo '=== '$s' ==='; tmux capture-pane -t $s -p -S -5 2>/dev/null; done")

CLAUDE.md — 项目上下文文件

Claude Code 自动从项目根目录加载 CLAUDE.md。用于持久化项目上下文:

# Project: My API
 
## Architecture
- FastAPI backend with SQLAlchemy ORM
- PostgreSQL database, Redis cache
- pytest for testing with 90% coverage target
 
## Key Commands
- `make test` — run full test suite
- `make lint` — ruff + mypy
- `make dev` — start dev server on :8000
 
## Code Standards
- Type hints on all public functions
- Docstrings in Google style
- 2-space indentation for YAML, 4-space for Python

自定义子代理

.claude/agents/(项目)、~/.claude/agents/(个人)或通过 --agents CLI 标志(会话)定义专门的代理:

创建代理

# .claude/agents/security-reviewer.md
---
name: security-reviewer
description: Security-focused code review
model: opus
tools: [Read, Bash]
---
You are a senior security engineer. Review code for:
- Injection vulnerabilities (SQL, XSS, command injection)
- Authentication/authorization flaws
- Secrets in code
- Unsafe deserialization

通过 @security-reviewer review the auth module 调用。

钩子(Hooks)— 事件自动化

.claude/settings.json(项目)或 ~/.claude/settings.json(全局)中配置:

{
  "hooks": {
    "PostToolUse": [{
      "matcher": "Write(*.py)",
      "hooks": [{"type": "command", "command": "ruff check --fix $CLAUDE_FILE_PATHS"}]
    }],
    "Stop": [{
      "hooks": [{"type": "command", "command": "echo 'Claude finished' >> /tmp/claude-activity.log"}]
    }]
  }
}

注意事项与陷阱

  1. 交互式模式需要 tmux — Claude Code 是一个完整的 TUI 应用。
  2. --dangerously-skip-permissions 对话框默认”不,退出” — 必须发送 Down 然后 Enter 以接受。
  3. --max-budget-usd 最低约 $0.05 — 仅系统提示缓存创建就花费这么多。
  4. --max-turns 是打印模式专用的 — 在交互式会话中被忽略。
  5. 会话恢复需要相同目录--continue 查找当前工作目录的最近会话。
  6. 上下文退化是真实的 — AI 输出质量在上下文窗口使用率超过 70% 时明显下降。
  7. 裸模式跳过 OAuth — 需要 ANTHROPIC_API_KEY 环境变量。

Hermes 代理的规则

  1. 首选打印模式(-p)用于单个任务 — 更干净,无对话框处理,结构化输出
  2. 使用 tmux 进行多轮交互式工作 — 编排 TUI 的唯一可靠方式
  3. 始终设置 workdir — 保持 Claude 专注于正确的项目目录
  4. 在打印模式中设置 --max-turns — 防止无限循环和失控成本
  5. 监控 tmux 会话 — 使用 tmux capture-pane 检查进度
  6. 查找 提示符 — 表示 Claude 正在等待输入
  7. 清理 tmux 会话 — 完成后杀死它们以避免资源泄漏
  8. 使用 --allowedTools — 将能力限制为任务实际需要的内容