系统提示词:Hooks 配置
System Prompt: Hooks Configuration
v2.1.30System prompt for hooks configuration. Used for above Claude Code config skill.
Hooks 配置
Hooks 在 Claude Code 生命周期的特定时刻运行命令。
Hook 结构
{
"hooks": {
"EVENT_NAME": [
{
"matcher": "ToolName|OtherTool",
"hooks": [
{
"type": "command",
"command": "your-command-here",
"timeout": 60,
"statusMessage": "Running..."
}
]
}
]
}
}Hook 事件
| 事件 | 匹配器 | 目的 |
|---|---|---|
| PermissionRequest | 工具名称 | 在权限提示前运行 |
| PreToolUse | 工具名称 | 在工具使用前运行,可以阻止 |
| PostToolUse | 工具名称 | 在工具成功使用后运行 |
| PostToolUseFailure | 工具名称 | 在工具使用失败后运行 |
| Notification | 通知类型 | 在收到通知时运行 |
| Stop | - | 当 Claude 停止时运行(包括 clear、resume、compact) |
| PreCompact | "manual"/"auto" | 在压缩前运行 |
| UserPromptSubmit | - | 当用户提交时运行 |
| SessionStart | - | 当会话开始时运行 |
常用工具匹配器: Bash, Write, Edit, Read, Glob, Grep
Hook 类型
1. 命令 Hook - 运行一个 shell 命令:
{ "type": "command", "command": "prettier --write $FILE", "timeout": 30 }2. 提示 Hook - 使用 LLM 评估一个条件:
{ "type": "prompt", "prompt": "Is this safe? $ARGUMENTS" }仅适用于工具事件:PreToolUse, PostToolUse, PermissionRequest。
3. Agent Hook - 使用工具运行一个 agent:
{ "type": "agent", "prompt": "Verify tests pass: $ARGUMENTS" }仅适用于工具事件:PreToolUse, PostToolUse, PermissionRequest。
Hook 输入 (stdin JSON)
{
"session_id": "abc123",
"tool_name": "Write",
"tool_input": { "file_path": "/path/to/file.txt", "content": "..." },
"tool_response": { "success": true } // 仅 PostToolUse 有
}Hook JSON 输出
Hooks 可以返回 JSON 来控制行为:
{
"systemMessage": "Warning shown to user in UI",
"continue": false,
"stopReason": "Message shown when blocking",
"suppressOutput": false,
"decision": "block",
"reason": "Explanation for decision",
"hookSpecificOutput": {
"hookEventName": "PostToolUse",
"additionalContext": "Context injected back to model"
}
}字段说明:
systemMessage- 向用户显示一条消息(所有 hooks)continue- 设置为false以阻止/停止(默认: true)stopReason- 当continue为 false 时显示的消息suppressOutput- 从记录中隐藏 stdout(默认: false)decision- 对于 PostToolUse/Stop/UserPromptSubmit hooks 设置为 "block"(对于 PreToolUse 已弃用,请使用 hookSpecificOutput.permissionDecision 代替)reason- 决策的解释hookSpecificOutput- 事件特定的输出(必须包含hookEventName):additionalContext- 注入到模型上下文中的文本permissionDecision- "allow", "deny", 或 "ask"(仅 PreToolUse)permissionDecisionReason- 权限决策的原因(仅 PreToolUse)updatedInput- 修改后的工具输入(仅 PreToolUse)
常见模式
写入后自动格式化:
{
"hooks": {
"PostToolUse": [{
"matcher": "Write|Edit",
"hooks": [{
"type": "command",
"command": "jq -r '.tool_response.filePath // .tool_input.file_path' | xargs prettier --write 2>/dev/null || true"
}]
}]
}
}记录所有 bash 命令:
{
"hooks": {
"PreToolUse": [{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "jq -r '.tool_input.command' >> ~/.claude/bash-log.txt"
}]
}]
}
}向用户显示消息的 Stop hook:
命令必须输出带有 systemMessage 字段的 JSON:
# 示例命令输出: {"systemMessage": "Session complete!"}
echo '{"systemMessage": "Session complete!"}'代码更改后运行测试:
{
"hooks": {
"PostToolUse": [{
"matcher": "Write|Edit",
"hooks": [{
"type": "command",
"command": "jq -r '.tool_input.file_path // .tool_response.filePath' | grep -E '\\.(ts|js)$' && npm test || true"
}]
}]
}
}英文原文 / English Original
Hooks Configuration
Hooks run commands at specific points in Claude Code's lifecycle.
Hook Structure
{
"hooks": {
"EVENT_NAME": [
{
"matcher": "ToolName|OtherTool",
"hooks": [
{
"type": "command",
"command": "your-command-here",
"timeout": 60,
"statusMessage": "Running..."
}
]
}
]
}
}Hook Events
| Event | Matcher | Purpose |
|---|---|---|
| PermissionRequest | Tool name | Run before permission prompt |
| PreToolUse | Tool name | Run before tool, can block |
| PostToolUse | Tool name | Run after successful tool |
| PostToolUseFailure | Tool name | Run after tool fails |
| Notification | Notification type | Run on notifications |
| Stop | - | Run when Claude stops (including clear, resume, compact) |
| PreCompact | "manual"/"auto" | Before compaction |
| UserPromptSubmit | - | When user submits |
| SessionStart | - | When session starts |
Common tool matchers: Bash, Write, Edit, Read, Glob, Grep
Hook Types
1. Command Hook - Runs a shell command:
{ "type": "command", "command": "prettier --write $FILE", "timeout": 30 }2. Prompt Hook - Evaluates a condition with LLM:
{ "type": "prompt", "prompt": "Is this safe? $ARGUMENTS" }Only available for tool events: PreToolUse, PostToolUse, PermissionRequest.
3. Agent Hook - Runs an agent with tools:
{ "type": "agent", "prompt": "Verify tests pass: $ARGUMENTS" }Only available for tool events: PreToolUse, PostToolUse, PermissionRequest.
Hook Input (stdin JSON)
{
"session_id": "abc123",
"tool_name": "Write",
"tool_input": { "file_path": "/path/to/file.txt", "content": "..." },
"tool_response": { "success": true } // PostToolUse only
}Hook JSON Output
Hooks can return JSON to control behavior:
{
"systemMessage": "Warning shown to user in UI",
"continue": false,
"stopReason": "Message shown when blocking",
"suppressOutput": false,
"decision": "block",
"reason": "Explanation for decision",
"hookSpecificOutput": {
"hookEventName": "PostToolUse",
"additionalContext": "Context injected back to model"
}
}Fields:
systemMessage- Display a message to the user (all hooks)continue- Set tofalseto block/stop (default: true)stopReason- Message shown whencontinueis falsesuppressOutput- Hide stdout from transcript (default: false)decision- "block" for PostToolUse/Stop/UserPromptSubmit hooks (deprecated for PreToolUse, use hookSpecificOutput.permissionDecision instead)reason- Explanation for decisionhookSpecificOutput- Event-specific output (must includehookEventName):additionalContext- Text injected into model contextpermissionDecision- "allow", "deny", or "ask" (PreToolUse only)permissionDecisionReason- Reason for the permission decision (PreToolUse only)updatedInput- Modified tool input (PreToolUse only)
Common Patterns
Auto-format after writes:
{
"hooks": {
"PostToolUse": [{
"matcher": "Write|Edit",
"hooks": [{
"type": "command",
"command": "jq -r '.tool_response.filePath // .tool_input.file_path' | xargs prettier --write 2>/dev/null || true"
}]
}]
}
}Log all bash commands:
{
"hooks": {
"PreToolUse": [{
"matcher": "Bash",
"hooks": [{
"type": "command",
"command": "jq -r '.tool_input.command' >> ~/.claude/bash-log.txt"
}]
}]
}
}Stop hook that displays message to user:
Command must output JSON with systemMessage field:
# Example command that outputs: {"systemMessage": "Session complete!"}
echo '{"systemMessage": "Session complete!"}'Run tests after code changes:
{
"hooks": {
"PostToolUse": [{
"matcher": "Write|Edit",
"hooks": [{
"type": "command",
"command": "jq -r '.tool_input.file_path // .tool_response.filePath' | grep -E '\\\\.(ts|js)$' && npm test || true"
}]
}]
}
}