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"
}
]
}
]
}
}