Hooks Configuration
Overview
Hooks are an event-driven mechanism in iFlow CLI that allows you to automatically execute custom commands when specific lifecycle events occur. By configuring Hooks, you can implement automated processing before and after tool calls, environment setup enhancement, cleanup operations when sessions stop, and more.
Key Features
- Tool Call Interception: Run custom logic before and after tool execution
- Environment Enhancement: Dynamically set environment information when sessions begin
- Lifecycle Management: Execute cleanup operations when sessions or subagents stop
- Flexible Configuration: Support hierarchical configuration at user and project levels
- Security Control: Can block tool execution or modify tool behavior
Hook Types
iFlow CLI supports the following 9 Hook types:
1. PreToolUse Hook
Trigger Time: Before tool execution Use Cases:
- Validate tool parameters
- Set execution environment
- Log tool calls
- Block unsafe operations
Example Configuration:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit",
"hooks": [
{
"type": "command",
"command": "echo 'File edit detected'"
}
]
}
]
}
}
2. PostToolUse Hook
Trigger Time: After tool execution Use Cases:
- Process tool execution results
- Clean up temporary files
- Send notifications
- Record execution statistics
Example Configuration:
{
"hooks": {
"PostToolUse": [
{
"matcher": "write_file",
"hooks": [
{
"type": "command",
"command": "echo 'File operation completed'"
}
]
}
]
}
}
3. SetUpEnvironment Hook
Trigger Time: At session start, during environment information setup phase Use Cases:
- Dynamically generate project information
- Set runtime environment variables
- Enhance AI context information
- Load project-specific configurations
Example Configuration:
{
"hooks": {
"SetUpEnvironment": [
{
"hooks": [
{
"type": "command",
"command": "echo 'Session environment initialized'"
}
]
}
]
}
}
4. Stop Hook
Trigger Time: When the main session ends Use Cases:
- Clean up session resources
- Save session information
- Send session summary
- Execute cleanup scripts
Example Configuration:
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "echo 'Main session ended'"
}
]
}
]
}
}
5. SubagentStop Hook
Trigger Time: When subagent session ends Use Cases:
- Clean up subagent resources
- Record subtask execution status
- Merge subtask results
- Execute post-subtask processing
Example Configuration:
{
"hooks": {
"SubagentStop": [
{
"hooks": [
{
"type": "command",
"command": "echo 'Subagent task completed'"
}
]
}
]
}
}
6. SessionStart Hook
Trigger Time: When session starts (startup, resume, clear, compress) Use Cases:
- Initialize session environment
- Set up logging
- Send session start notifications
- Execute startup preprocessing
Supports matcher: Yes - can match based on session start source (startup, resume, clear, compress)
Example Configuration:
{
"hooks": {
"SessionStart": [
{
"matcher": "startup",
"hooks": [
{
"type": "command",
"command": "echo 'New session started'"
}
]
}
]
}
}
7. SessionEnd Hook
Trigger Time: When session ends normally Use Cases:
- Generate session summary reports
- Backup session data
- Send session end notifications
- Execute session cleanup operations
Example Configuration:
{
"hooks": {
"SessionEnd": [
{
"hooks": [
{
"type": "command",
"command": "python3 ~/.iflow/hooks/session_report.py",
"timeout": 30
}
]
}
]
}
}
8. UserPromptSubmit Hook
Trigger Time: Before user submits prompt, before iFlow processing Use Cases:
- Content filtering and review
- Prompt preprocessing and enhancement
- Block inappropriate user input
- Log user interaction
Supports matcher: Yes - can match based on prompt content Special behavior: Can block prompt submission (return non-zero exit code)
Example Configuration:
{
"hooks": {
"UserPromptSubmit": [
{
"matcher": ".*sensitive.*",
"hooks": [
{
"type": "command",
"command": "python3 ~/.iflow/hooks/content_filter.py",
"timeout": 10
}
]
}
]
}
}
9. Notification Hook
Trigger Time: When iFlow sends notifications to user Use Cases:
- Notification content logging
- Third-party system integration
- Notification format conversion
- Custom notification handling
Supports matcher: Yes - can match based on notification message content Special behavior: Exit code 2 doesn't block notification, only displays stderr to user
Example Configuration:
{
"hooks": {
"Notification": [
{
"matcher": ".*permission.*",
"hooks": [
{
"type": "command",
"command": "echo 'Permission notification logged' >> ~/.iflow/permission.log"
}
]
}
]
}
}
Configuration Methods
1. Configuration Hierarchy
Hooks configuration follows iFlow CLI's hierarchical configuration system:
- User Configuration:
~/.iflow/settings.json - Project Configuration:
./.iflow/settings.json - System Configuration: System-level configuration files
Higher-level configurations merge with lower-level configurations, and project configurations supplement user configurations.
2. Configuration Format
Add the hooks configuration item in the settings.json file:
{
"hooks": {
"PreToolUse": [
{
"matcher": "tool_pattern",
"hooks": [
{
"type": "command",
"command": "your_command",
"timeout": 30
}
]
}
],
"PostToolUse": [
{
"matcher": "another_pattern",
"hooks": [
{
"type": "command",
"command": "cleanup_command"
}
]
}
],
"SetUpEnvironment": [
{
"hooks": [
{
"type": "command",
"command": "python ~/.iflow/hooks/env_enhancer.py",
"timeout": 30
}
]
}
],
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "echo 'Session ended'"
}
]
}
],
"SubagentStop": [
{
"hooks": [
{
"type": "command",
"command": "cleanup_subagent.sh"
}
]
}
],
"SessionStart": [
{
"matcher": "startup",
"hooks": [
{
"type": "command",
"command": "echo 'Session initialized'"
}
]
}
],
"SessionEnd": [
{
"hooks": [
{
"type": "command",
"command": "python ~/.iflow/hooks/session_summary.py"
}
]
}
],
"UserPromptSubmit": [
{
"matcher": ".*sensitive.*",
"hooks": [
{
"type": "command",
"command": "python ~/.iflow/hooks/content_filter.py"
}
]
}
],
"Notification": [
{
"matcher": ".*permission.*",
"hooks": [
{
"type": "command",
"command": "logger 'iFlow permission request'"
}
]
}
]
}
}
3. Hook Configuration Field Description
Each Hook type contains a configuration array, with each configuration item including:
Common Fields
hooks(required): Hook command array, each command includes:type: Command type, currently only supports"command"command: Command string to executetimeout: Timeout in seconds, optional, no timeout by default
Tool-related Hook (PreToolUse/PostToolUse) Specific Fields
matcher: Tool matching pattern, used to specify which tools should trigger the Hook
Matching Patterns
| Matching Pattern | Syntax Example | Description |
|---|---|---|
| Wildcard Match | "*" or "" | Match all tools (default behavior) |
| Exact Match | "Edit" | Only match tools or aliases named "Edit" |
| Regular Expression | `"Edit\ | MultiEdit\ |
| Pattern Match | ".*_file" | Match tool names ending with "_file" |
| MCP Tool Match | "mcp__.*" | Match all MCP tools |
| MCP Server Match | "mcp__github__.*" | Match all tools from a specific MCP server |
Matching Rules
- Case Sensitive: matcher matching is case-sensitive
- Regular Expression: Automatically recognized as regex when containing
|\\^$.*+?()[]{}characters - Tool Aliases: Both tool names and aliases are checked during matching
- Error Handling: Invalid regex patterns fall back to exact match mode
Hook Type and matcher Support
| Hook Type | Supports matcher | Description |
|---|---|---|
| PreToolUse | ✅ | Can specify matching specific tools |
| PostToolUse | ✅ | Can specify matching specific tools |
| SetUpEnvironment | ❌ | Always executes, doesn't support matcher |
| Stop | ❌ | Always executes, doesn't support matcher |
| SubagentStop | ❌ | Always executes, doesn't support matcher |
| SessionStart |