🔗 Hooks 自动化
20+ 自动化钩子,在特定事件发生时自动执行检查和处理
🔴 高级什么是 Hooks?
Hooks 是在特定事件发生时自动执行的脚本。它们可以实现:
- 安全检查:阻止危险操作,检测敏感信息
- 自动格式化:保存文件时自动格式化代码
- 会话管理:自动保存和恢复会话状态
- 通知提醒:在关键操作前提醒用户
Hook 配置格式
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "node scripts/hooks/check-command.js"
}
]
}
]
}
}
Hook 类型
| Hook | 触发时机 | 用途 |
|---|---|---|
PreToolUse |
工具执行前 | 安全检查、敏感信息过滤、操作确认 |
PostToolUse |
工具执行后 | 代码格式化、类型检查、通知 |
SessionStart |
会话开始时 | 加载上下文、恢复状态、初始化 |
Stop |
会话结束时 | 保存会话摘要、持久化记忆 |
Notification |
收到通知时 | 处理系统通知 |
内置 Hooks
安全类 Hooks
block-no-verify
阻止绕过 Git hooks 的
--no-verify 标志
insaits-security
InsAIts AI 安全监控,检测恶意操作
governance-capture
捕获治理事件:敏感信息、策略违规
开发辅助 Hooks
auto-tmux-dev
自动在 tmux 中启动开发服务器
tmux-reminder
提醒使用 tmux 运行长时间命令
git-push-reminder
git push 前提醒审查变更
doc-file-warning
创建文档文件时的警告提示
suggest-compact
在适当时机建议执行 /compact
学习类 Hooks
observe
捕获工具使用观察,用于持续学习
配置 Hooks
Hook Profile(严格度配置)
# 设置 Hook 严格度
export ECC_HOOK_PROFILE=standard # minimal | standard | strict
| Profile | 说明 | 适用场景 |
|---|---|---|
minimal |
仅核心安全检查 | 快速原型开发 |
standard |
平衡功能和性能 | 日常开发(默认) |
strict |
所有检查,包括格式化 | 生产代码开发 |
禁用特定 Hook
# 临时禁用特定 Hook
export ECC_DISABLED_HOOKS="pre:bash:tmux-reminder,post:edit:typecheck"
创建自定义 Hook
示例:阻止 console.log
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "#!/bin/bash\nif grep -q 'console\\.log' \"$file_path\"; then\n echo '[Hook] Warning: Remove console.log before committing' >&2\nfi"
}
]
}
]
}
}
示例:自动格式化
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit",
"hooks": [
{
"type": "command",
"command": "npx prettier --write \"$file_path\""
}
]
}
]
}
}
Matcher 语法
| Matcher | 匹配的工具 |
|---|---|
"Bash" |
仅 Bash 命令 |
"Edit|Write" |
Edit 或 Write 工具 |
"*" |
所有工具 |
跨平台支持
ECC 的 Hooks 使用 Node.js 编写,确保在 Windows、macOS、Linux 上都能正常运行:
scripts/hooks/
├── session-start.js # 会话启动
├── session-end.js # 会话结束
├── pre-bash-tmux-reminder.js
├── pre-bash-git-push-reminder.js
├── suggest-compact.js
├── doc-file-warning.js
└── ...
ℹ️ 提示
避免在 Hooks 中使用平台特定的 shell 命令。使用 Node.js 的跨平台 API。