forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooksConfigManager.ts
More file actions
400 lines (387 loc) · 17.1 KB
/
Copy pathhooksConfigManager.ts
File metadata and controls
400 lines (387 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import memoize from 'lodash-es/memoize.js'
import type { HookEvent } from 'src/entrypoints/agentSdkTypes.js'
import { getRegisteredHooks } from '../../bootstrap/state.js'
import type { AppState } from '../../state/AppState.js'
import {
getAllHooks,
type IndividualHookConfig,
sortMatchersByPriority,
} from './hooksSettings.js'
export type MatcherMetadata = {
fieldToMatch: string
values: string[]
}
export type HookEventMetadata = {
summary: string
description: string
matcherMetadata?: MatcherMetadata
}
// Hook event metadata configuration.
// Resolver uses sorted-joined string key so that callers passing a fresh
// toolNames array each render (e.g. HooksConfigMenu) hit the cache instead
// of leaking a new entry per call.
export const getHookEventMetadata = memoize(
function (toolNames: string[]): Record<HookEvent, HookEventMetadata> {
return {
PreToolUse: {
summary: 'Before tool execution',
description:
'Input to command is JSON of tool call arguments.\nExit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to model and block tool call\nOther exit codes - show stderr to user only but continue with tool call',
matcherMetadata: {
fieldToMatch: 'tool_name',
values: toolNames,
},
},
PostToolUse: {
summary: 'After tool execution',
description:
'Input to command is JSON with fields "inputs" (tool call arguments) and "response" (tool call response).\nExit code 0 - stdout shown in transcript mode (ctrl+o)\nExit code 2 - show stderr to model immediately\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'tool_name',
values: toolNames,
},
},
PostToolUseFailure: {
summary: 'After tool execution fails',
description:
'Input to command is JSON with tool_name, tool_input, tool_use_id, error, error_type, is_interrupt, and is_timeout.\nExit code 0 - stdout shown in transcript mode (ctrl+o)\nExit code 2 - show stderr to model immediately\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'tool_name',
values: toolNames,
},
},
PermissionDenied: {
summary: 'After auto mode classifier denies a tool call',
description:
'Input to command is JSON with tool_name, tool_input, tool_use_id, and reason.\nReturn {"hookSpecificOutput":{"hookEventName":"PermissionDenied","retry":true}} to tell the model it may retry.\nExit code 0 - stdout shown in transcript mode (ctrl+o)\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'tool_name',
values: toolNames,
},
},
Notification: {
summary: 'When notifications are sent',
description:
'Input to command is JSON with notification message and type.\nExit code 0 - stdout/stderr not shown\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'notification_type',
values: [
'permission_prompt',
'idle_prompt',
'auth_success',
'elicitation_dialog',
'elicitation_complete',
'elicitation_response',
],
},
},
UserPromptSubmit: {
summary: 'When the user submits a prompt',
description:
'Input to command is JSON with original user prompt text.\nExit code 0 - stdout shown to Claude\nExit code 2 - block processing, erase original prompt, and show stderr to user only\nOther exit codes - show stderr to user only',
},
SessionStart: {
summary: 'When a new session is started',
description:
'Input to command is JSON with session start source.\nExit code 0 - stdout shown to Claude\nBlocking errors are ignored\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'source',
values: ['startup', 'resume', 'clear', 'compact'],
},
},
Stop: {
summary: 'Right before Claude concludes its response',
description:
'Exit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to model and continue conversation\nOther exit codes - show stderr to user only',
},
StopFailure: {
summary: 'When the turn ends due to an API error',
description:
'Fires instead of Stop when an API error (rate limit, auth failure, etc.) ended the turn. Fire-and-forget — hook output and exit codes are ignored.',
matcherMetadata: {
fieldToMatch: 'error',
values: [
'rate_limit',
'authentication_failed',
'billing_error',
'invalid_request',
'server_error',
'max_output_tokens',
'unknown',
],
},
},
SubagentStart: {
summary: 'When a subagent (Agent tool call) is started',
description:
'Input to command is JSON with agent_id and agent_type.\nExit code 0 - stdout shown to subagent\nBlocking errors are ignored\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'agent_type',
values: [], // Will be populated with available agent types
},
},
SubagentStop: {
summary:
'Right before a subagent (Agent tool call) concludes its response',
description:
'Input to command is JSON with agent_id, agent_type, and agent_transcript_path.\nExit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to subagent and continue having it run\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'agent_type',
values: [], // Will be populated with available agent types
},
},
PreCompact: {
summary: 'Before conversation compaction',
description:
'Input to command is JSON with compaction details.\nExit code 0 - stdout appended as custom compact instructions\nExit code 2 - block compaction\nOther exit codes - show stderr to user only but continue with compaction',
matcherMetadata: {
fieldToMatch: 'trigger',
values: ['manual', 'auto'],
},
},
PostCompact: {
summary: 'After conversation compaction',
description:
'Input to command is JSON with compaction details and the summary.\nExit code 0 - stdout shown to user\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'trigger',
values: ['manual', 'auto'],
},
},
SessionEnd: {
summary: 'When a session is ending',
description:
'Input to command is JSON with session end reason.\nExit code 0 - command completes successfully\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'reason',
values: ['clear', 'logout', 'prompt_input_exit', 'other'],
},
},
PermissionRequest: {
summary: 'When a permission dialog is displayed',
description:
'Input to command is JSON with tool_name, tool_input, and tool_use_id.\nOutput JSON with hookSpecificOutput containing decision to allow or deny.\nExit code 0 - use hook decision if provided\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'tool_name',
values: toolNames,
},
},
Setup: {
summary: 'Repo setup hooks for init and maintenance',
description:
'Input to command is JSON with trigger (init or maintenance).\nExit code 0 - stdout shown to Claude\nBlocking errors are ignored\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'trigger',
values: ['init', 'maintenance'],
},
},
TeammateIdle: {
summary: 'When a teammate is about to go idle',
description:
'Input to command is JSON with teammate_name and team_name.\nExit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to teammate and prevent idle (teammate continues working)\nOther exit codes - show stderr to user only',
},
TaskCreated: {
summary: 'When a task is being created',
description:
'Input to command is JSON with task_id, task_subject, task_description, teammate_name, and team_name.\nExit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to model and prevent task creation\nOther exit codes - show stderr to user only',
},
TaskCompleted: {
summary: 'When a task is being marked as completed',
description:
'Input to command is JSON with task_id, task_subject, task_description, teammate_name, and team_name.\nExit code 0 - stdout/stderr not shown\nExit code 2 - show stderr to model and prevent task completion\nOther exit codes - show stderr to user only',
},
Elicitation: {
summary: 'When an MCP server requests user input (elicitation)',
description:
'Input to command is JSON with mcp_server_name, message, and requested_schema.\nOutput JSON with hookSpecificOutput containing action (accept/decline/cancel) and optional content.\nExit code 0 - use hook response if provided\nExit code 2 - deny the elicitation\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'mcp_server_name',
values: [],
},
},
ElicitationResult: {
summary: 'After a user responds to an MCP elicitation',
description:
'Input to command is JSON with mcp_server_name, action, content, mode, and elicitation_id.\nOutput JSON with hookSpecificOutput containing optional action and content to override the response.\nExit code 0 - use hook response if provided\nExit code 2 - block the response (action becomes decline)\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'mcp_server_name',
values: [],
},
},
ConfigChange: {
summary: 'When configuration files change during a session',
description:
'Input to command is JSON with source (user_settings, project_settings, local_settings, policy_settings, skills) and file_path.\nExit code 0 - allow the change\nExit code 2 - block the change from being applied to the session\nOther exit codes - show stderr to user only',
matcherMetadata: {
fieldToMatch: 'source',
values: [
'user_settings',
'project_settings',
'local_settings',
'policy_settings',
'skills',
],
},
},
InstructionsLoaded: {
summary: 'When an instruction file (CLAUDE.md or rule) is loaded',
description:
'Input to command is JSON with file_path, memory_type (User, Project, Local, Managed), load_reason (session_start, nested_traversal, path_glob_match, include, compact), globs (optional — the paths: frontmatter patterns that matched), trigger_file_path (optional — the file Claude touched that caused the load), and parent_file_path (optional — the file that @-included this one).\nExit code 0 - command completes successfully\nOther exit codes - show stderr to user only\nThis hook is observability-only and does not support blocking.',
matcherMetadata: {
fieldToMatch: 'load_reason',
values: [
'session_start',
'nested_traversal',
'path_glob_match',
'include',
'compact',
],
},
},
WorktreeCreate: {
summary: 'Create an isolated worktree for VCS-agnostic isolation',
description:
'Input to command is JSON with name (suggested worktree slug).\nStdout should contain the absolute path to the created worktree directory.\nExit code 0 - worktree created successfully\nOther exit codes - worktree creation failed',
},
WorktreeRemove: {
summary: 'Remove a previously created worktree',
description:
'Input to command is JSON with worktree_path (absolute path to worktree).\nExit code 0 - worktree removed successfully\nOther exit codes - show stderr to user only',
},
CwdChanged: {
summary: 'After the working directory changes',
description:
'Input to command is JSON with old_cwd and new_cwd.\nCLAUDE_ENV_FILE is set — write bash exports there to apply env to subsequent BashTool commands.\nHook output can include hookSpecificOutput.watchPaths (array of absolute paths) to register with the FileChanged watcher.\nExit code 0 - command completes successfully\nOther exit codes - show stderr to user only',
},
FileChanged: {
summary: 'When a watched file changes',
description:
'Input to command is JSON with file_path and event (change, add, unlink).\nCLAUDE_ENV_FILE is set — write bash exports there to apply env to subsequent BashTool commands.\nThe matcher field specifies filenames to watch in the current directory (e.g. ".envrc|.env").\nHook output can include hookSpecificOutput.watchPaths (array of absolute paths) to dynamically update the watch list.\nExit code 0 - command completes successfully\nOther exit codes - show stderr to user only',
},
}
},
toolNames => toolNames.slice().sort().join(','),
)
// Group hooks by event and matcher
export function groupHooksByEventAndMatcher(
appState: AppState,
toolNames: string[],
): Record<HookEvent, Record<string, IndividualHookConfig[]>> {
const grouped: Record<HookEvent, Record<string, IndividualHookConfig[]>> = {
PreToolUse: {},
PostToolUse: {},
PostToolUseFailure: {},
PermissionDenied: {},
Notification: {},
UserPromptSubmit: {},
SessionStart: {},
SessionEnd: {},
Stop: {},
StopFailure: {},
SubagentStart: {},
SubagentStop: {},
PreCompact: {},
PostCompact: {},
PermissionRequest: {},
Setup: {},
TeammateIdle: {},
TaskCreated: {},
TaskCompleted: {},
Elicitation: {},
ElicitationResult: {},
ConfigChange: {},
WorktreeCreate: {},
WorktreeRemove: {},
InstructionsLoaded: {},
CwdChanged: {},
FileChanged: {},
}
const metadata = getHookEventMetadata(toolNames)
// Include hooks from settings files
getAllHooks(appState).forEach(hook => {
const eventGroup = grouped[hook.event]
if (eventGroup) {
// For events without matchers, use empty string as key
const matcherKey =
metadata[hook.event].matcherMetadata !== undefined
? hook.matcher || ''
: ''
if (!eventGroup[matcherKey]) {
eventGroup[matcherKey] = []
}
eventGroup[matcherKey].push(hook)
}
})
// Include registered hooks (e.g., plugin hooks)
const registeredHooks = getRegisteredHooks()
if (registeredHooks) {
for (const [event, matchers] of Object.entries(registeredHooks)) {
const hookEvent = event as HookEvent
const eventGroup = grouped[hookEvent]
if (!eventGroup) continue
for (const matcher of matchers) {
const matcherKey = matcher.matcher || ''
// Only PluginHookMatcher has pluginRoot; HookCallbackMatcher (internal
// callbacks like attributionHooks, sessionFileAccessHooks) does not.
if ('pluginRoot' in matcher) {
eventGroup[matcherKey] ??= []
for (const hook of matcher.hooks) {
eventGroup[matcherKey].push({
event: hookEvent,
config: hook,
matcher: matcher.matcher,
source: 'pluginHook',
pluginName: matcher.pluginId,
})
}
} else if (process.env.USER_TYPE === 'ant') {
eventGroup[matcherKey] ??= []
for (const _hook of matcher.hooks) {
eventGroup[matcherKey].push({
event: hookEvent,
config: {
type: 'command',
command: '[ANT-ONLY] Built-in Hook',
},
matcher: matcher.matcher,
source: 'builtinHook',
})
}
}
}
}
}
return grouped
}
// Get sorted matchers for a specific event
export function getSortedMatchersForEvent(
hooksByEventAndMatcher: Record<
HookEvent,
Record<string, IndividualHookConfig[]>
>,
event: HookEvent,
): string[] {
const matchers = Object.keys(hooksByEventAndMatcher[event] || {})
return sortMatchersByPriority(matchers, hooksByEventAndMatcher, event)
}
// Get hooks for a specific event and matcher
export function getHooksForMatcher(
hooksByEventAndMatcher: Record<
HookEvent,
Record<string, IndividualHookConfig[]>
>,
event: HookEvent,
matcher: string | null,
): IndividualHookConfig[] {
// For events without matchers, hooks are stored with empty string as key
// because the record keys must be strings.
const matcherKey = matcher ?? ''
return hooksByEventAndMatcher[event]?.[matcherKey] ?? []
}
// Get metadata for a specific event's matcher
export function getMatcherMetadata(
event: HookEvent,
toolNames: string[],
): MatcherMetadata | undefined {
return getHookEventMetadata(toolNames)[event].matcherMetadata
}