|
| 1 | +/** |
| 2 | + * SDK Hooks — Discord-integrated hook callbacks for Claude Code SDK. |
| 3 | + * |
| 4 | + * Hooks provide deep integration points that fire during query execution: |
| 5 | + * - PreToolUse: Log/audit tool usage before execution |
| 6 | + * - PostToolUse: Log completed tool usage |
| 7 | + * - PostToolUseFailure: Log tool failures |
| 8 | + * - Notification: Forward Claude's notifications to Discord |
| 9 | + * - TaskCompleted: Notify when background tasks finish |
| 10 | + * |
| 11 | + * All hooks are passive observers — they log to Discord but don't block execution. |
| 12 | + * They return `{ continue: true }` to let the SDK proceed normally. |
| 13 | + * |
| 14 | + * @module claude/hooks |
| 15 | + */ |
| 16 | + |
| 17 | +import type { |
| 18 | + HookCallbackMatcher, |
| 19 | + HookCallback, |
| 20 | + HookInput, |
| 21 | + PreToolUseHookInput, |
| 22 | + PostToolUseHookInput, |
| 23 | + PostToolUseFailureHookInput, |
| 24 | + NotificationHookInput, |
| 25 | + TaskCompletedHookInput, |
| 26 | + HookEvent, |
| 27 | + SyncHookJSONOutput, |
| 28 | +} from "@anthropic-ai/claude-agent-sdk"; |
| 29 | + |
| 30 | +/** |
| 31 | + * Configuration for which hooks to enable and how to route events. |
| 32 | + */ |
| 33 | +export interface HookConfig { |
| 34 | + /** Enable tool-use logging (PreToolUse + PostToolUse + PostToolUseFailure) */ |
| 35 | + logToolUse: boolean; |
| 36 | + /** Enable notification forwarding */ |
| 37 | + logNotifications: boolean; |
| 38 | + /** Enable task completion notifications */ |
| 39 | + logTaskCompletions: boolean; |
| 40 | + /** Callback to send hook events to Discord */ |
| 41 | + onHookEvent: (event: HookEvent_Discord) => void; |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Discord-formatted hook event for display. |
| 46 | + */ |
| 47 | +export interface HookEvent_Discord { |
| 48 | + type: 'tool_start' | 'tool_complete' | 'tool_failure' | 'notification' | 'task_completed'; |
| 49 | + toolName?: string; |
| 50 | + // deno-lint-ignore no-explicit-any |
| 51 | + toolInput?: any; |
| 52 | + // deno-lint-ignore no-explicit-any |
| 53 | + toolResponse?: any; |
| 54 | + error?: string; |
| 55 | + message?: string; |
| 56 | + title?: string; |
| 57 | + taskId?: string; |
| 58 | + taskSubject?: string; |
| 59 | + timestamp: number; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Build SDK hook callbacks based on the provided configuration. |
| 64 | + * Returns a Partial<Record<HookEvent, HookCallbackMatcher[]>> suitable |
| 65 | + * for passing directly to the SDK query `options.hooks`. |
| 66 | + */ |
| 67 | +export function buildHooks(config: HookConfig): Partial<Record<HookEvent, HookCallbackMatcher[]>> { |
| 68 | + const hooks: Partial<Record<HookEvent, HookCallbackMatcher[]>> = {}; |
| 69 | + |
| 70 | + if (config.logToolUse) { |
| 71 | + // PreToolUse — log when a tool is about to be used |
| 72 | + const preToolHook: HookCallback = async (input: HookInput) => { |
| 73 | + const preInput = input as PreToolUseHookInput; |
| 74 | + config.onHookEvent({ |
| 75 | + type: 'tool_start', |
| 76 | + toolName: preInput.tool_name, |
| 77 | + toolInput: preInput.tool_input, |
| 78 | + timestamp: Date.now(), |
| 79 | + }); |
| 80 | + // Passive: don't block, don't modify |
| 81 | + return { continue: true } satisfies SyncHookJSONOutput; |
| 82 | + }; |
| 83 | + |
| 84 | + hooks.PreToolUse = [{ |
| 85 | + hooks: [preToolHook], |
| 86 | + }]; |
| 87 | + |
| 88 | + // PostToolUse — log after a tool runs successfully |
| 89 | + const postToolHook: HookCallback = async (input: HookInput) => { |
| 90 | + const postInput = input as PostToolUseHookInput; |
| 91 | + config.onHookEvent({ |
| 92 | + type: 'tool_complete', |
| 93 | + toolName: postInput.tool_name, |
| 94 | + toolInput: postInput.tool_input, |
| 95 | + toolResponse: postInput.tool_response, |
| 96 | + timestamp: Date.now(), |
| 97 | + }); |
| 98 | + return { continue: true } satisfies SyncHookJSONOutput; |
| 99 | + }; |
| 100 | + |
| 101 | + hooks.PostToolUse = [{ |
| 102 | + hooks: [postToolHook], |
| 103 | + }]; |
| 104 | + |
| 105 | + // PostToolUseFailure — log tool failures |
| 106 | + const failureHook: HookCallback = async (input: HookInput) => { |
| 107 | + const failInput = input as PostToolUseFailureHookInput; |
| 108 | + config.onHookEvent({ |
| 109 | + type: 'tool_failure', |
| 110 | + toolName: failInput.tool_name, |
| 111 | + toolInput: failInput.tool_input, |
| 112 | + error: failInput.error, |
| 113 | + timestamp: Date.now(), |
| 114 | + }); |
| 115 | + return { continue: true } satisfies SyncHookJSONOutput; |
| 116 | + }; |
| 117 | + |
| 118 | + hooks.PostToolUseFailure = [{ |
| 119 | + hooks: [failureHook], |
| 120 | + }]; |
| 121 | + } |
| 122 | + |
| 123 | + if (config.logNotifications) { |
| 124 | + // Notification — forward Claude's notifications to Discord |
| 125 | + const notificationHook: HookCallback = async (input: HookInput) => { |
| 126 | + const notifInput = input as NotificationHookInput; |
| 127 | + config.onHookEvent({ |
| 128 | + type: 'notification', |
| 129 | + message: notifInput.message, |
| 130 | + title: notifInput.title, |
| 131 | + timestamp: Date.now(), |
| 132 | + }); |
| 133 | + return { continue: true } satisfies SyncHookJSONOutput; |
| 134 | + }; |
| 135 | + |
| 136 | + hooks.Notification = [{ |
| 137 | + hooks: [notificationHook], |
| 138 | + }]; |
| 139 | + } |
| 140 | + |
| 141 | + if (config.logTaskCompletions) { |
| 142 | + // TaskCompleted — notify when background tasks finish |
| 143 | + const taskHook: HookCallback = async (input: HookInput) => { |
| 144 | + const taskInput = input as TaskCompletedHookInput; |
| 145 | + config.onHookEvent({ |
| 146 | + type: 'task_completed', |
| 147 | + taskId: taskInput.task_id, |
| 148 | + taskSubject: taskInput.task_subject, |
| 149 | + message: taskInput.task_description, |
| 150 | + timestamp: Date.now(), |
| 151 | + }); |
| 152 | + return { continue: true } satisfies SyncHookJSONOutput; |
| 153 | + }; |
| 154 | + |
| 155 | + hooks.TaskCompleted = [{ |
| 156 | + hooks: [taskHook], |
| 157 | + }]; |
| 158 | + } |
| 159 | + |
| 160 | + return hooks; |
| 161 | +} |
0 commit comments