Hooks 事件速查表
事件类型
| 事件 | 触发时机 | 可阻止 | stdin 数据 |
|---|---|---|---|
| PreToolUse | 工具执行前 | ✅ (exit 2) | tool_name, tool_input |
| PostToolUse | 工具执行后 | ❌ | tool_name, tool_input, tool_output |
| SessionStart | 会话启动 | ❌ | session_id, model |
| Stop | Claude 停止 | ❌ | -- |
| TaskCreated | 任务创建 | ❌ | task_id, task_subject |
| Notification | 通知事件 | ❌ | message |
| TeammateIdle | Teammate 空闲 | ❌ | teammate_id |
| TaskCompleted | 任务完成 | ❌ | task_id |
处理器类型
| 类型 | 语法 | 适用场景 |
|---|---|---|
| command | "command": "bash script.sh" | Shell 脚本 |
| http | "url": "https://..." | Webhook |
| prompt | "prompt": "Check if..." | 语义判断 |
| agent | "agent": "reviewer" | 子 Agent |
Exit Code
| Code | 含义 |
|---|---|
| 0 | 通过 |
| 1 | 脚本出错(不阻止) |
| 2 | 阻止工具执行 |
Matcher 模式
"Bash" → 精确匹配
"Write|Edit" → 匹配多个工具
"mcp__github__.*" → 正则匹配环境变量
$CLAUDE_PROJECT_DIR → 项目根目录
$CLAUDE_SESSION_ID → 当前会话 ID
$CLAUDE_MODEL → 当前模型配置示例
json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [{"type": "command", "command": "bash .claude/hooks/check.sh"}]
}
],
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [{"type": "command", "command": "bash .claude/hooks/lint.sh"}]
}
],
"Stop": [
{
"hooks": [{"type": "command", "command": "bash .claude/hooks/notify.sh"}]
}
]
}
}