forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooksSettings.ts
More file actions
271 lines (245 loc) · 8.31 KB
/
Copy pathhooksSettings.ts
File metadata and controls
271 lines (245 loc) · 8.31 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
import { resolve } from 'path'
import type { HookEvent } from 'src/entrypoints/agentSdkTypes.js'
import { getSessionId } from '../../bootstrap/state.js'
import type { AppState } from '../../state/AppState.js'
import type { EditableSettingSource } from '../settings/constants.js'
import { SOURCES } from '../settings/constants.js'
import {
getSettingsFilePathForSource,
getSettingsForSource,
} from '../settings/settings.js'
import type { HookCommand, HookMatcher } from '../settings/types.js'
import { DEFAULT_HOOK_SHELL } from '../shell/shellProvider.js'
import { getSessionHooks } from './sessionHooks.js'
export type HookSource =
| EditableSettingSource
| 'policySettings'
| 'pluginHook'
| 'sessionHook'
| 'builtinHook'
export interface IndividualHookConfig {
event: HookEvent
config: HookCommand
matcher?: string
source: HookSource
pluginName?: string
}
/**
* Check if two hooks are equal (comparing only command/prompt content, not timeout)
*/
export function isHookEqual(
a: HookCommand | { type: 'function'; timeout?: number },
b: HookCommand | { type: 'function'; timeout?: number },
): boolean {
if (a.type !== b.type) return false
// Use switch for exhaustive type checking
// Note: We only compare command/prompt content, not timeout
// `if` is part of identity: same command with different `if` conditions
// are distinct hooks (e.g., setup.sh if=Bash(git *) vs if=Bash(npm *)).
const sameIf = (x: { if?: string }, y: { if?: string }) =>
(x.if ?? '') === (y.if ?? '')
switch (a.type) {
case 'command':
// shell is part of identity: same command string with different
// shells are distinct hooks. Default 'bash' so undefined === 'bash'.
return (
b.type === 'command' &&
a.command === b.command &&
(a.shell ?? DEFAULT_HOOK_SHELL) === (b.shell ?? DEFAULT_HOOK_SHELL) &&
sameIf(a, b)
)
case 'prompt':
return b.type === 'prompt' && a.prompt === b.prompt && sameIf(a, b)
case 'agent':
return b.type === 'agent' && a.prompt === b.prompt && sameIf(a, b)
case 'http':
return b.type === 'http' && a.url === b.url && sameIf(a, b)
case 'function':
// Function hooks can't be compared (no stable identifier)
return false
}
}
/** Get the display text for a hook */
export function getHookDisplayText(
hook: HookCommand | { type: 'callback' | 'function'; statusMessage?: string },
): string {
// Return custom status message if provided
if ('statusMessage' in hook && hook.statusMessage) {
return hook.statusMessage
}
switch (hook.type) {
case 'command':
return hook.command
case 'prompt':
return hook.prompt
case 'agent':
return hook.prompt
case 'http':
return hook.url
case 'callback':
return 'callback'
case 'function':
return 'function'
}
}
export function getAllHooks(appState: AppState): IndividualHookConfig[] {
const hooks: IndividualHookConfig[] = []
// Check if restricted to managed hooks only
const policySettings = getSettingsForSource('policySettings')
const restrictedToManagedOnly = policySettings?.allowManagedHooksOnly === true
// If allowManagedHooksOnly is set, don't show any hooks in the UI
// (user/project/local are blocked, and managed hooks are intentionally hidden)
if (!restrictedToManagedOnly) {
// Get hooks from all editable sources
const sources = [
'userSettings',
'projectSettings',
'localSettings',
] as EditableSettingSource[]
// Track which settings files we've already processed to avoid duplicates
// (e.g., when running from home directory, userSettings and projectSettings
// both resolve to ~/.claude/settings.json)
const seenFiles = new Set<string>()
for (const source of sources) {
const filePath = getSettingsFilePathForSource(source)
if (filePath) {
const resolvedPath = resolve(filePath)
if (seenFiles.has(resolvedPath)) {
continue
}
seenFiles.add(resolvedPath)
}
const sourceSettings = getSettingsForSource(source)
if (!sourceSettings?.hooks) {
continue
}
for (const [event, matchers] of Object.entries(sourceSettings.hooks)) {
for (const matcher of matchers as HookMatcher[]) {
for (const hookCommand of matcher.hooks) {
hooks.push({
event: event as HookEvent,
config: hookCommand,
matcher: matcher.matcher,
source,
})
}
}
}
}
}
// Get session hooks
const sessionId = getSessionId()
const sessionHooks = getSessionHooks(appState, sessionId)
for (const [event, matchers] of sessionHooks.entries()) {
for (const matcher of matchers) {
for (const hookCommand of matcher.hooks) {
hooks.push({
event,
config: hookCommand,
matcher: matcher.matcher,
source: 'sessionHook',
})
}
}
}
return hooks
}
export function getHooksForEvent(
appState: AppState,
event: HookEvent,
): IndividualHookConfig[] {
return getAllHooks(appState).filter(hook => hook.event === event)
}
export function hookSourceDescriptionDisplayString(source: HookSource): string {
switch (source) {
case 'userSettings':
return 'User settings (~/.claude/settings.json)'
case 'projectSettings':
return 'Project settings (.claude/settings.json)'
case 'localSettings':
return 'Local settings (.claude/settings.local.json)'
case 'pluginHook':
// TODO: Get the actual plugin hook file paths instead of using glob pattern
// We should capture the specific plugin paths during hook registration and display them here
// e.g., "Plugin hooks (~/.claude/plugins/repos/source/example-plugin/example-plugin/hooks/hooks.json)"
return 'Plugin hooks (~/.claude/plugins/*/hooks/hooks.json)'
case 'sessionHook':
return 'Session hooks (in-memory, temporary)'
case 'builtinHook':
return 'Built-in hooks (registered internally by Claude Code)'
default:
return source as string
}
}
export function hookSourceHeaderDisplayString(source: HookSource): string {
switch (source) {
case 'userSettings':
return 'User Settings'
case 'projectSettings':
return 'Project Settings'
case 'localSettings':
return 'Local Settings'
case 'pluginHook':
return 'Plugin Hooks'
case 'sessionHook':
return 'Session Hooks'
case 'builtinHook':
return 'Built-in Hooks'
default:
return source as string
}
}
export function hookSourceInlineDisplayString(source: HookSource): string {
switch (source) {
case 'userSettings':
return 'User'
case 'projectSettings':
return 'Project'
case 'localSettings':
return 'Local'
case 'pluginHook':
return 'Plugin'
case 'sessionHook':
return 'Session'
case 'builtinHook':
return 'Built-in'
default:
return source as string
}
}
export function sortMatchersByPriority(
matchers: string[],
hooksByEventAndMatcher: Record<
string,
Record<string, IndividualHookConfig[]>
>,
selectedEvent: HookEvent,
): string[] {
// Create a priority map based on SOURCES order (lower index = higher priority)
const sourcePriority = SOURCES.reduce(
(acc, source, index) => {
acc[source] = index
return acc
},
{} as Record<EditableSettingSource, number>,
)
return [...matchers].sort((a, b) => {
const aHooks = hooksByEventAndMatcher[selectedEvent]?.[a] || []
const bHooks = hooksByEventAndMatcher[selectedEvent]?.[b] || []
const aSources = Array.from(new Set(aHooks.map(h => h.source)))
const bSources = Array.from(new Set(bHooks.map(h => h.source)))
// Sort by highest priority source first (lowest priority number)
// Plugin hooks get lowest priority (highest number)
const getSourcePriority = (source: HookSource) =>
source === 'pluginHook' || source === 'builtinHook'
? 999
: sourcePriority[source as EditableSettingSource]
const aHighestPriority = Math.min(...aSources.map(getSourcePriority))
const bHighestPriority = Math.min(...bSources.map(getSourcePriority))
if (aHighestPriority !== bHighestPriority) {
return aHighestPriority - bHighestPriority
}
// If same priority, sort by matcher name
return a.localeCompare(b)
})
}