AgentSkillsCN

ai-hooks-integration

在各类 AI 编码工具(Claude Code、Gemini CLI、Cursor、OpenCode)以及任意 CLI 中集成生命周期钩子。 适用场景:(1) 安装或移除钩子;(2) 创建 OpenCode 插件;(3) 设置自动格式化、测试、通知、安全策略;(4) 在未提供钩子 API 的情况下,对 CLI 工具进行封装。 触发时机:“添加钩子”、“安装钩子”、“钩子集成”、“PreToolUse”、“PostToolUse”、“beforeShellExecution”、“自动格式化”、“完成时通知”、“封装 CLI”、“拦截命令”。

SKILL.md
--- frontmatter
name: ai-hooks-integration
description: |
  Integrate lifecycle hooks across AI coding tools (Claude Code, Gemini CLI, Cursor, OpenCode) and any CLI.
  Use when: (1) installing/removing hooks, (2) creating OpenCode plugins, (3) setting up auto-formatting,
  testing, notifications, security policies, (4) wrapping CLI tools without hooks API.
  Triggers on: "add hook", "install hook", "hook integration", "PreToolUse", "PostToolUse",
  "beforeShellExecution", "auto-format", "notify on complete", "wrap CLI", "intercept command".

AI Hooks Integration

Decision Tree

code
Does the target tool have a hooks API?
├── YES (Claude, Gemini CLI, Cursor, OpenCode)
│   └── Installing hooks for multiple tools at once?
│       ├── YES → Use install_all.py
│       │   └── Need cross-tool source detection?
│       │       ├── YES → --unified mode (recommended)
│       │       └── NO  → --command mode (classic)
│       └── NO  → Use single-tool scripts
│           ├── Claude/Gemini/Cursor → merge_hooks.py
│           └── OpenCode → install_opencode_plugin.py
└── NO (gh, aws, kubectl, docker, etc.)
    └── Use install_cli_wrapper.py

Quick Commands

bash
# Multi-tool: Unified mode (recommended)
scripts/install_all.py --unified --handler "/path/to/handler" --name my-hook

# Multi-tool: Classic mode
scripts/install_all.py --command "/path/to/hook" --name my-hook

# Single tool
scripts/merge_hooks.py --tool claude --path ~/.claude/settings.json --command "<hook>"
scripts/install_opencode_plugin.py --name my-hook --output ~/.config/opencode/plugins

# CLI without hooks API
scripts/install_cli_wrapper.py --cli gh --hook "/path/to/hook"

# Remove
scripts/remove_all.py --command "<hook>" --plugin ~/.config/opencode/plugins/my-hook.js
scripts/remove_cli_wrapper.py --cli gh

# Preview
--dry-run  # Add to any command

Mode Comparison

ModeWhen to UseSource DetectionEvent Filtering
UnifiedMultiple AI tools share config filesAuto (process tree)Yes
ClassicSingle tool or explicit routingManual (--source flag)No
CLI WrapperTools without hooks APIN/AN/A

Tool Support

ToolConfigHook KeyHas Hooks API
Claude~/.claude/settings.jsonPreToolUse, PostToolUse, StopYes
Gemini CLI~/.gemini/settings.jsonBeforeTool, AfterTool, SessionStart +8Yes
Cursor~/.cursor/hooks.jsonbeforeShellExecution, afterFileEditYes
OpenCode~/.config/opencode/plugins/*.jstool.execute.before/afterYes (plugin)
Gemini IDEN/AN/ANo
gh, aws, etc.N/AN/ANo → Use wrapper

Gemini CLI Events (11 total)

SessionStart, SessionEnd, BeforeAgent, AfterAgent, BeforeModel, AfterModel, BeforeToolSelection, BeforeTool, AfterTool, PreCompress, Notification

References

FileWhen to Read
references/tool-reference.mdConfig paths, events, payload formats, templates
references/use-cases.mdHook patterns: security, formatting, testing, notifications
references/unified-hook-usage.mdCross-tool interference, source detection, debug

Key Behaviors

  • Idempotent: Duplicates skipped, safe to re-run
  • Safe merge: Only adds; never overwrites existing hooks
  • Debug: HOOK_DEBUG=1 enables logging