Hooks 配置
概述
Hooks(钩子)是 iFlow CLI 中的事件驱动机制,允许您在特定的生命周期事件发生时自动执行自定义命令。通过配置 Hooks,您可以实现工具调用前后的自动化处理、环境设置增强、会话停止时的清理操作等功能。
主要功能
- 工具调用拦截:在工具执行前后运行自定义逻辑
- 环境增强:在会话开始时动态设置环境信息
- 生命周期管理:在会话或子代理停止时执行清理操作
- 灵活配置:支持用户级和项目级的分层配置
- 安全控制:可阻止工具执行或修改工具行为
Hook 类型
iFlow CLI 支持以下 9 种 Hook 类型:
1. PreToolUse Hook
触发时机:在工具执行之前 用途:
- 验证工具参数
- 设置执行环境
- 记录工具调用日志
- 阻止不安全的操作
示例配置:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit",
"hooks": [
{
"type": "command",
"command": "echo 'File edit detected'"
}
]
}
]
}
}
2. PostToolUse Hook
触发时机:在工具执行之后 用途:
- 处理工具执行结果
- 清理临时文件
- 发送通知
- 记录执行统计
示例配置:
{
"hooks": {
"PostToolUse": [
{
"matcher": "write_file",
"hooks": [
{
"type": "command",
"command": "echo 'File operation completed'"
}
]
}
]
}
}
3. SetUpEnvironment Hook
触发时机:会话开始时,环境信息设置阶段 用途:
- 动态生成项目信息
- 设置运行时环境变量
- 增强 AI 的上下文信息
- 加载项目特定配置
示例配置:
{
"hooks": {
"SetUpEnvironment": [
{
"hooks": [
{
"type": "command",
"command": "echo 'Session environment initialized'"
}
]
}
]
}
}
4. Stop Hook
触发时机:主会话结束时 用途:
- 清理会话资源
- 保存会话信息
- 发送会话总结
- 执行清理脚本
示例配置:
{
"hooks": {
"Stop": [
{
"hooks": [
{
"type": "command",
"command": "echo 'Main session ended'"
}
]
}
]
}
}
5. SubagentStop Hook
触发时机:子代理会话结束时 用途:
- 清理子代理资源
- 记录子任务执行情况
- 合并子任务结果
- 执行子任务后处理
示例配置:
{
"hooks": {
"SubagentStop": [
{
"hooks": [
{
"type": "command",
"command": "echo 'Subagent task completed'"
}
]
}
]
}
}
6. SessionStart Hook
触发时机:会话开始时(启动、恢复、清理、压缩) 用途:
- 初始化会话环境
- 设置日志记录
- 发送会话开始通知
- 执行启动时的预处理
支持matcher:是 - 可以根据会话启动来源进行匹配(startup、resume、clear、compress)
示例配置:
{
"hooks": {
"SessionStart": [
{
"matcher": "startup",
"hooks": [
{
"type": "command",
"command": "echo 'New session started'"
}
]
}
]
}
}
7. SessionEnd Hook
触发时机:会话正常结束时 用途:
- 生成会话总结报告
- 备份会话数据
- 发送会话结束通知
- 执行会话清理操作
示例配置:
{
"hooks": {
"SessionEnd": [
{
"hooks": [
{
"type": "command",
"command": "python3 ~/.iflow/hooks/session_report.py",
"timeout": 30
}
]
}
]
}
}
8. UserPromptSubmit Hook
触发时机:用户提交提示词之前,在iFlow处理之前 用途:
- 内容过滤和审核
- 提示词预处理和增强
- 阻止不当的用户输入
- 记录用户交互日志
支持matcher:是 - 可以根据提示词内容进行匹配 特殊行为:可以阻止提示词提交(返回非零退出码)
示例配置:
{
"hooks": {
"UserPromptSubmit": [
{
"matcher": ".*sensitive.*",
"hooks": [
{
"type": "command",
"command": "python3 ~/.iflow/hooks/content_filter.py",
"timeout": 10
}
]
}
]
}
}
9. Notification Hook
触发时机:当iFlow向用户发送通知时 用途:
- 通知内容记录
- 第三方系统集成
- 通知格式转换
- 自定义通知处理
支持matcher:是 - 可以根据通知消息内容进行匹配 特殊行为:退出码2不阻止通知,仅将stderr显示给用户
示例配置:
{
"hooks": {
"Notification": [
{
"matcher": ".*permission.*",
"hooks": [
{
"type": "command",
"command": "echo 'Permission notification logged' >> ~/.iflow/permission.log"
}
]
}
]
}
}
配置方式
1. 配置层级
Hooks 配置遵循 iFlow CLI 的分层配置系统:
- 用户配置:
~/.iflow/settings.json - 项目配置:
./.iflow/settings.json - 系统配置:系统级配置文件
高层级的配置会与低层级配置合并,项目配置会补充用户配置。
2. 配置格式
在 settings.json 文件中添加 hooks 配置项:
{
"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 配置项说明
每个 Hook 类型包含一个配置数组,每个配置项包含:
通用字段
hooks(必需):Hook 命令数组,每个命令包含:type:命令类型,目前仅支持"command"command:要执行的命令字符串timeout:超时时间(秒),可选,默认无超时
工具相关 Hook(PreToolUse/PostToolUse)专用字段
matcher:工具匹配模式,用于指定Hook应该在哪些工具执行时触发
匹配模式
| 匹配模式 | 语法示例 | 说明 |
|---|---|---|
| 通配符匹配 | "*" 或 "" | 匹配所有工具(默认行为) |
| 精确匹配 | "Edit" | 只匹配名为"Edit"的工具或别名 |
| 正则表达式 | "Edit|MultiEdit|Write" | 匹配多个工具名或别名 |
| 模式匹配 | ".*_file" | 匹配以"_file"结尾的工具名 |
| MCP工具匹配 | "mcp__.*" | 匹配所有MCP工具 |
| MCP服务器匹配 | "mcp__github__.*" | 匹配特定MCP服务器的所有工具 |
匹配规则
- 大小写敏感:matcher匹配是区分大小写的
- 正则表达式:包含
|\\^$.*+?()[]{}等字符时自动识别为正则表达式 - 工具别名:匹配时会同时检查工具名和别名
- 错误处理:无效的正则表达式会回退到精确匹配模式
Hook类型与matcher支持
| Hook类型 | 支持matcher | 说明 |
|---|---|---|
| PreToolUse | ✅ | 可以指定匹配特定工具 |
| PostToolUse | ✅ | 可以指定匹配特定工具 |
| SetUpEnvironment | ❌ | 始终执行,不支持matcher |
| Stop | ❌ | 始终执行,不支持matcher |
| SubagentStop | ❌ | 始终执行,不支持matcher |
| SessionStart | ✅ | 可以根据会话启动来源匹配(startup、resume、clear、compress) |
| SessionEnd | ❌ | 始终执行,不支持matcher |
| UserPromptSubmit | ✅ | 可以根据用户提示词内容匹配 |
| Notification | ✅ | 可以根据通知消息内容匹配 |
常用工具名称参考
| 工具类别 | 实际工具名 | 常用别名 |
|---|---|---|
| 文件编辑 | replace | Edit, edit, Write, write |
| 批量编辑 | multi_edit | MultiEdit, multiEdit |
| 文件写入 | write_file | write, create, save |
| 文件读取 | read_file | read |
| Shell执行 | run_shell_command | shell, Shell, bash, Bash |
| 文件搜索 | search_file_content | grep, search |
| 目录列表 | list_directory | ls, list |
特殊约束
- SetUpEnvironment Hook:不支持
matcher字段,对所有会话生效 - Stop/SubagentStop/SessionEnd Hook:不支持
matcher字段,在相应生命周期结束时执行 - UserPromptSubmit Hook:可通过返回非零退出码阻止提示词提交
- Notification Hook:退出码2有特殊含义,不阻止通知显示,仅将stderr内容显示给用户
复杂配置示例
1. 文件保护 Hook
Python脚本 (file_protection.py):
import json, sys
data = json.load(sys.stdin)
file_path = data.get('tool_input', {}).get('file_path', '')
sensitive_files = ['.env', 'package-lock.json', '.git/']
sys.exit(2 if any(p in file_path for p in sensitive_files) else 0)
功能描述:在文件编辑操作执行前进行安全检查,阻止对敏感文件的修改操作。
前置依赖:
- 系统需要安装
python3 - 确保Python能够正常执行并访问标准输入
具体功能:
- 监控所有文件编辑操作(Edit、MultiEdit、Write工具)
- 检查目标文件路径是否包含敏感文件(.env、package-lock.json、.git/目录)
- 如果检测到敏感文件,返回退出码2阻止工具执行
- 提供文件操作的安全防护,避免意外修改重要配置文件
Hook配置:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Edit|MultiEdit|Write",
"hooks": [
{
"type": "command",
"command": "python3 file_protection.py",
"timeout": 10
}
]
}
]
}
}
2. TypeScript 代码格式化
功能 描述:在文件编辑操作完成后自动对TypeScript文件进行代码格式化,确保代码风格的一致性。
前置依赖:
- 系统需要安装
jq工具(用于JSON数据处理) - 需要安装
prettier代码格式化工具(npm install -g prettier或项目本地安装) - 确保项目中有prettier配置文件或使用默认配置
具体功能:
- 监控文件编辑和写入操作(Edit、MultiEdit、write_file工具)
- 从工具参数中提取文件路径信息
- 检查文件是否为TypeScript文件(.ts扩展名)
- 对符合条件的文件自动执行prettier格式化
- 提升代码质量和团队协作效率
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|MultiEdit|Write",
"hooks": [
{
"type": "command",
"command": "bash -c 'path=$(jq -r \".tool_input.file_path\"); [[ $path == *.ts ]] && npx prettier --write \"$path\"'",
"timeout": 30
}
]
}
]
}
}