生成脚本:
scripts/gen-hooks-table.ts;source_commit:290fdc9481a70612bc5823aa4ed225c52c52aad3
- HOOK_EVENTS:27 个
- Hook command type:4 类
- 已补齐 trigger/payload:27/27
| 事件名 | 触发时机 | Payload 字段 | 源码位置 |
|---|---|---|---|
PreToolUse |
工具被调度执行之前;可用 exit 2 / decision:'block' 阻断本次工具调用,hook 输出会作为 system reminder 反馈给模型。 | session_idtranscript_pathcwdpermission_mode?agent_id?agent_type?hook_event_name='PreToolUse'tool_nametool_inputtool_use_id |
schema: entrypoints/sdk/coreSchemas.ts:414-423dispatch: utils/hooks.ts:3394-3436call: services/tools/toolHooks.ts (executePreToolHooks) |
PostToolUse |
工具调用成功完成之后;hook 可通过 decision:'block' 给模型注入反馈(不影响工具结果本身)。 | ...BaseHookInputhook_event_name='PostToolUse'tool_nametool_inputtool_responsetool_use_id |
schema: entrypoints/sdk/coreSchemas.ts:436-446dispatch: utils/hooks.ts:3450-3477call: services/tools/toolHooks.ts:56 |
PostToolUseFailure |
工具调用失败 / 被中断之后;payload 携带 error 字符串与 is_interrupt 标记,hook 可向模型注入修复建议。 | ...BaseHookInputhook_event_name='PostToolUseFailure'tool_nametool_inputtool_use_iderroris_interrupt? |
schema: entrypoints/sdk/coreSchemas.ts:448-459dispatch: utils/hooks.ts:3492-3527call: services/tools/toolHooks.ts:212 |
Notification |
需要向用户弹出系统通知前(待权限确认 / 待 elicitation / 输入空闲提醒等);hook 可替换或抑制通知。在 REPL 之外执行。 | ...BaseHookInputhook_event_name='Notification'messagetitle?notification_type |
schema: entrypoints/sdk/coreSchemas.ts:473-482dispatch: utils/hooks.ts:3570-3592call: services/notifier.ts:25call: cli/print.ts:1366call: services/mcp/elicitationHandler.ts:183call: services/mcp/elicitationHandler.ts:283call: services/mcp/elicitationHandler.ts:298call: services/mcp/elicitationHandler.ts:307 |
UserPromptSubmit |
用户在 REPL 中提交 prompt 之后、消息入队 QueryEngine 之前;hook 可改写 prompt 或注入额外 system 消息。 | ...BaseHookInputhook_event_name='UserPromptSubmit'prompt |
schema: entrypoints/sdk/coreSchemas.ts:484-491dispatch: utils/hooks.ts:3826-3855call: utils/processUserInput/processUserInput.ts:182 |
SessionStart |
会话开始或 resume/clear/compact 后重启 session;hook 可注入 system reminder(如自定义 agent 引导语)。 | ...BaseHookInputhook_event_name='SessionStart'source='startup'|'resume'|'clear'|'compact'agent_type?model? |
schema: entrypoints/sdk/coreSchemas.ts:493-502dispatch: utils/hooks.ts:3867-3892call: utils/sessionStart.ts:132 |
SessionEnd |
会话结束(logout / clear / resume 切换 / prompt_input_exit 等);同步触发,关停 Ink 之后直接写 stderr。默认 1.5s 超时(CLAUDE_CODE_SESSIONEND_HOOKS_TIMEOUT_MS 覆盖)。 | ...BaseHookInputhook_event_name='SessionEnd'reason ∈ EXIT_REASONS |
schema: entrypoints/sdk/coreSchemas.ts:758-765dispatch: utils/hooks.ts:4097-4141call: utils/gracefulShutdown.ts:473call: screens/REPL.tsx:1774call: commands/clear/conversation.ts:69 |
Stop |
主线程在一轮回合结束、准备进入 idle 之前;hook 可 exit 2 / decision:'block' 让模型继续执行,用于强制完成额外步骤。 | ...BaseHookInputhook_event_name='Stop'stop_hook_activelast_assistant_message? |
schema: entrypoints/sdk/coreSchemas.ts:513-527dispatch: utils/hooks.ts:3639-3697call: query/stopHooks.ts:180 |
StopFailure |
回合以 assistant 错误(API 错误 / 取消 / overload)收尾时,与 Stop 互斥;payload 携带最后一条 assistant message 与错误字段,主要用于审计 / 告警。 | ...BaseHookInputhook_event_name='StopFailure'error (SDKAssistantMessageError, 缺省 'unknown')error_details?last_assistant_message? |
schema: entrypoints/sdk/coreSchemas.ts:529-538dispatch: utils/hooks.ts:3594-3627call: query.ts:1174call: query.ts:1181call: query.ts:1263 |
SubagentStart |
AgentTool 创建并启动一个 subagent 之前;payload 给出 agent_id + agent_type,便于 hook 给子 agent 注入额外 system reminder。 | ...BaseHookInputhook_event_name='SubagentStart'agent_idagent_type |
schema: entrypoints/sdk/coreSchemas.ts:540-548dispatch: utils/hooks.ts:3932-3952call: tools/AgentTool/runAgent.ts:532 |
SubagentStop |
subagent 完成回合、准备返回结果给父 agent 之前;Stop 的 subagent 变体,executeStopHooks 在传入 subagentId 时切到此事件。可阻断让子 agent 继续工作。 | ...BaseHookInputhook_event_name='SubagentStop'stop_hook_activeagent_idagent_transcript_pathagent_typelast_assistant_message? |
schema: entrypoints/sdk/coreSchemas.ts:550-567dispatch: utils/hooks.ts:3639-3697call: query/stopHooks.ts:180 |
PreCompact |
压缩流程开始之前(手动 /compact 或 token 自动压缩);hook 可返回 newCustomInstructions 覆盖默认指令,并把 displayMessage 显示给用户。 | ...BaseHookInputhook_event_name='PreCompact'trigger='manual'|'auto'custom_instructions: string|null |
schema: entrypoints/sdk/coreSchemas.ts:569-577dispatch: utils/hooks.ts:3961-4025call: services/compact/compact.ts:413call: services/compact/compact.ts:818call: commands/compact/compact.ts:160 |
PostCompact |
压缩完成、summary 已写入会话之后;hook 输出仅作为 displayMessage 展示给用户,不参与后续模型上下文。 | ...BaseHookInputhook_event_name='PostCompact'trigger='manual'|'auto'compact_summary |
schema: entrypoints/sdk/coreSchemas.ts:579-589dispatch: utils/hooks.ts:4034-4089call: services/compact/compact.ts:723call: services/compact/compact.ts:1069 |
PermissionRequest |
权限引擎判定需要弹出权限对话框之前;hook 可程序化批准 / 拒绝,免去交互。在 REPL(含 SDK structuredIO 通道)与 PermissionContext 内都会触发。 | ...BaseHookInputhook_event_name='PermissionRequest'tool_nametool_inputpermission_suggestions?: PermissionUpdate[] |
schema: entrypoints/sdk/coreSchemas.ts:425-434dispatch: utils/hooks.ts:4157-4192call: utils/permissions/permissions.ts:409call: cli/structuredIO.ts:798call: hooks/toolPermission/PermissionContext.ts:222 |
PermissionDenied |
权限引擎判定一次工具调用被拒绝之后,先于错误结果返回模型;fire-and-forget,主要用于审计 / 告警。 | ...BaseHookInputhook_event_name='PermissionDenied'tool_nametool_inputtool_use_idreason |
schema: entrypoints/sdk/coreSchemas.ts:461-471dispatch: utils/hooks.ts:3529-3562call: services/tools/toolExecution.ts:1081 |
Setup |
会话初始化 / 周期性维护阶段,紧随 SessionStart 之后;payload trigger 区分 'init' 与 'maintenance'。 | ...BaseHookInputhook_event_name='Setup'trigger='init'|'maintenance' |
schema: entrypoints/sdk/coreSchemas.ts:504-511dispatch: utils/hooks.ts:3902-3922call: utils/sessionStart.ts:203 |
TeammateIdle |
teammate(多人协作中的另一个 agent)即将进入 idle 状态时;hook 可 exit 2 让其继续工作,用于强制完成轮询任务。 | ...BaseHookInputhook_event_name='TeammateIdle'teammate_nameteam_name |
schema: entrypoints/sdk/coreSchemas.ts:591-599dispatch: utils/hooks.ts:3709-3729call: query/stopHooks.ts:403 |
TaskCreated |
TaskCreateTool 创建任务前;hook 可阻断创建并向模型回馈拒绝理由(如校验任务标题命名规范)。 | ...BaseHookInputhook_event_name='TaskCreated'task_idtask_subjecttask_description?teammate_name?team_name? |
schema: entrypoints/sdk/coreSchemas.ts:601-612dispatch: utils/hooks.ts:3745-3773call: tools/TaskCreateTool/TaskCreateTool.ts:93 |
TaskCompleted |
TaskUpdateTool 将任务置为 completed 之前;hook 可阻断完成(例如强制要求附录 / 验收脚本通过)。 | ...BaseHookInputhook_event_name='TaskCompleted'task_idtask_subjecttask_description?teammate_name?team_name? |
schema: entrypoints/sdk/coreSchemas.ts:614-625dispatch: utils/hooks.ts:3789-3817call: tools/TaskUpdateTool/TaskUpdateTool.ts:235 |
Elicitation |
MCP 服务器发起 elicitation 请求、需要向用户弹出对话框前;hook 可程序化返回 accept/decline/cancel 与表单内容,跳过 UI。 | ...BaseHookInputhook_event_name='Elicitation'mcp_server_namemessagemode='form'|'url'?url?elicitation_id?requested_schema? |
schema: entrypoints/sdk/coreSchemas.ts:627-643dispatch: utils/hooks.ts:4470-4523call: services/mcp/elicitationHandler.ts:228 |
ElicitationResult |
用户已经响应 MCP elicitation、即将把结果发回服务器之前;hook 可观察或改写最终响应(含 decline 转 block)。 | ...BaseHookInputhook_event_name='ElicitationResult'mcp_server_nameelicitation_id?mode='form'|'url'?action='accept'|'decline'|'cancel'content? |
schema: entrypoints/sdk/coreSchemas.ts:645-660dispatch: utils/hooks.ts:4525-4575call: services/mcp/elicitationHandler.ts:273 |
ConfigChange |
配置文件 watcher 检测到 user/project/local/policy settings 或 skills 目录变化时;hook 可 exit 2 阻断本次变更生效(policy_settings 例外,hook 仅审计、不可阻断)。 | ...BaseHookInputhook_event_name='ConfigChange'source ∈ CONFIG_CHANGE_SOURCESfile_path? |
schema: entrypoints/sdk/coreSchemas.ts:670-678dispatch: utils/hooks.ts:4214-4239call: utils/settings/changeDetector.ts:292call: utils/settings/changeDetector.ts:344call: utils/skills/skillChangeDetector.ts:267 |
WorktreeCreate |
需要为本会话创建 worktree 时;若用户配置了该 hook,则替代默认 git worktree 流程,stdout 中的路径作为新 worktree 根目录。无配置时回退到 git worktree。 | ...BaseHookInputhook_event_name='WorktreeCreate'name (slug,已经过 validateWorktreeSlug) |
schema: entrypoints/sdk/coreSchemas.ts:709-716dispatch: utils/hooks.ts:4928-4958call: utils/worktree.ts:716call: utils/worktree.ts:913call: utils/worktree.ts:1262 |
WorktreeRemove |
会话结束 / 用户主动清理 worktree 时;若 hook 配置存在则替代默认 git worktree remove;返回 false 表示无 hook 配置、走默认路径。 | ...BaseHookInputhook_event_name='WorktreeRemove'worktree_path |
schema: entrypoints/sdk/coreSchemas.ts:718-725dispatch: utils/hooks.ts:4967-5003call: utils/worktree.ts:827call: utils/worktree.ts:968 |
InstructionsLoaded |
CLAUDE.md 或 .claude/rules/*.md 等指令文件被加载进上下文时(session_start / nested_traversal / path_glob_match / include / compact 五种场景);fire-and-forget,仅用于观察与审计,不能阻断。 | ...BaseHookInputhook_event_name='InstructionsLoaded'file_pathmemory_type ∈ {User, Project, Local, Managed}load_reason ∈ INSTRUCTIONS_LOAD_REASONSglobs?trigger_file_path?parent_file_path? |
schema: entrypoints/sdk/coreSchemas.ts:695-707dispatch: utils/hooks.ts:4335-4369call: utils/claudemd.ts:1060call: utils/attachments.ts:1760 |
CwdChanged |
会话 cwd 发生切换之后(如 /cd 命令、worktree 切换);hook 输出可声明 watchPaths 让 FileChanged watcher 重新订阅,并向 REPL 推 systemMessage。 | ...BaseHookInputhook_event_name='CwdChanged'old_cwdnew_cwd |
schema: entrypoints/sdk/coreSchemas.ts:727-735dispatch: utils/hooks.ts:4260-4276call: utils/hooks/fileChangedWatcher.ts:148 |
FileChanged |
FileChangedWatcher 监听到 watchPaths 内文件 add/change/unlink 之后;hook 可输出新的 watchPaths 动态扩缩订阅集,输出失败将作为系统消息提示用户。 | ...BaseHookInputhook_event_name='FileChanged'file_pathevent='change'|'add'|'unlink' |
schema: entrypoints/sdk/coreSchemas.ts:737-745dispatch: utils/hooks.ts:4278-4294call: utils/hooks/fileChangedWatcher.ts:85 |
| 类型 | 行号 |
|---|---|
agent |
129 |
command |
33 |
http |
98 |
prompt |
68 |