* Dispatch an event. Hooks run sequentially in declaration order so users have * predictable ordering (e.g. lint before format). Returns aggregated results plus * an optional vetoMessage when a blocking PreToolUse hook exited non-zero.
(event: HookEvent, ctx: HookContext)
| 66 | * an optional vetoMessage when a blocking PreToolUse hook exited non-zero. |
| 67 | */ |
| 68 | async dispatch(event: HookEvent, ctx: HookContext): Promise<DispatchResult> { |
| 69 | const hooks = this.matching(event, ctx.toolName); |
| 70 | if (hooks.length === 0) { |
| 71 | return { outputs: [], ranCount: 0, runs: [] }; |
| 72 | } |
| 73 | |
| 74 | const runs = []; |
| 75 | const outputs: string[] = []; |
| 76 | const blockingMessages: string[] = []; |
| 77 | |
| 78 | for (const hook of hooks) { |
| 79 | const result = await runHook(hook, ctx); |
| 80 | runs.push(result); |
| 81 | |
| 82 | logger.info(`Hook [${event}] ${result.hookName}: exit=${result.exitCode} dur=${result.durationMs}ms${result.timedOut ? ' (TIMEOUT)' : ''}`); |
| 83 | |
| 84 | const out = result.stdout.trim(); |
| 85 | if (out) outputs.push(out); |
| 86 | |
| 87 | // Veto policy applies only to PreToolUse, and only when hook.blocking !== false |
| 88 | const isBlocking = event === 'PreToolUse' && (hook.blocking ?? true); |
| 89 | if (isBlocking && result.exitCode !== 0) { |
| 90 | const msg = out || result.stderr.trim() || `Hook ${result.hookName} exited ${result.exitCode}`; |
| 91 | blockingMessages.push(msg); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return { |
| 96 | outputs, |
| 97 | ranCount: runs.length, |
| 98 | runs, |
| 99 | ...(blockingMessages.length > 0 ? { vetoMessage: blockingMessages.join('\n---\n') } : {}), |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | /** Used by /hooks slash command to enumerate registered hooks. */ |
| 104 | list(): Array<{ event: HookEvent; index: number; config: HookConfig }> { |
no test coverage detected