cc-hooks-ts 2.0.42 → 2.0.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -33
- package/dist/index.d.mts +220 -159
- package/dist/index.mjs +13 -3
- package/package.json +13 -12
package/README.md
CHANGED
|
@@ -14,43 +14,22 @@ npx nypm add cc-hooks-ts
|
|
|
14
14
|
|
|
15
15
|
## Basic Usage
|
|
16
16
|
|
|
17
|
-
> [!NOTE]
|
|
18
|
-
> We highly recommend using Bun or Deno for automatic dependency downloading at runtime.
|
|
19
|
-
>
|
|
20
|
-
> - Bun: <https://bun.com/docs/runtime/autoimport>
|
|
21
|
-
> - Deno: <https://docs.deno.com/runtime/fundamentals/modules/#managing-third-party-modules-and-libraries>
|
|
22
|
-
|
|
23
17
|
### Define a Hook
|
|
24
18
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
```typescript
|
|
28
|
-
#!/usr/bin/env -S bun run --silent
|
|
29
|
-
import { defineHook, runHook } from "cc-hooks-ts";
|
|
30
|
-
|
|
31
|
-
// Session start hook
|
|
32
|
-
const sessionHook = defineHook({
|
|
33
|
-
trigger: { SessionStart: true },
|
|
34
|
-
run: (context) => {
|
|
35
|
-
// do something great
|
|
36
|
-
return context.success({
|
|
37
|
-
messageForUser: "Welcome to your coding session!"
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
await runHook(sessionHook);
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
Or with Deno:
|
|
19
|
+
Example of running a simple SessionStart hook on Bun:
|
|
46
20
|
|
|
47
21
|
```typescript
|
|
48
|
-
|
|
49
|
-
import { defineHook, runHook } from "npm:cc-hooks-ts";
|
|
22
|
+
import { defineHook } from "cc-hooks-ts";
|
|
50
23
|
|
|
51
|
-
// Session start hook
|
|
52
24
|
const sessionHook = defineHook({
|
|
53
|
-
trigger: {
|
|
25
|
+
trigger: {
|
|
26
|
+
// Specify the hook event to listen for
|
|
27
|
+
SessionStart: true,
|
|
28
|
+
PreToolUse: {
|
|
29
|
+
// PreToolUser and PostToolUse can be tool-specific. (affects type of context)
|
|
30
|
+
Read: true
|
|
31
|
+
}
|
|
32
|
+
},
|
|
54
33
|
run: (context) => {
|
|
55
34
|
// do something great
|
|
56
35
|
return context.success({
|
|
@@ -59,7 +38,11 @@ const sessionHook = defineHook({
|
|
|
59
38
|
}
|
|
60
39
|
});
|
|
61
40
|
|
|
62
|
-
|
|
41
|
+
// import.meta.main is available in Node.js 24.2+ and Bun and Deno
|
|
42
|
+
if (import.meta.main) {
|
|
43
|
+
const { runHook } = await import("cc-hooks-ts");
|
|
44
|
+
await runHook(sessionHook);
|
|
45
|
+
}
|
|
63
46
|
```
|
|
64
47
|
|
|
65
48
|
### Call from Claude Code
|
|
@@ -74,7 +57,7 @@ Then, load defined hooks in your Claude Code settings at `~/.claude/settings.jso
|
|
|
74
57
|
"hooks": [
|
|
75
58
|
{
|
|
76
59
|
"type": "command",
|
|
77
|
-
"command": "
|
|
60
|
+
"command": "bun run --silent path/to/your/sessionHook.ts"
|
|
78
61
|
}
|
|
79
62
|
]
|
|
80
63
|
}
|
|
@@ -149,6 +132,7 @@ const multiEventHook = defineHook({
|
|
|
149
132
|
PreToolUse: { Read: true, WebFetch: true },
|
|
150
133
|
PostToolUse: { Read: true }
|
|
151
134
|
},
|
|
135
|
+
// Optional: Define when the hook should run.
|
|
152
136
|
shouldRun: () => process.env.NODE_ENV === 'development',
|
|
153
137
|
run: (context) => {
|
|
154
138
|
// Handle different events and tools based on context.input
|
package/dist/index.d.mts
CHANGED
|
@@ -1,164 +1,11 @@
|
|
|
1
1
|
import * as v from "valibot";
|
|
2
2
|
import { AgentInput, BashInput, BashOutputInput, ExitPlanModeInput, FileEditInput, FileReadInput, FileWriteInput, GlobInput, GrepInput, KillShellInput, ListMcpResourcesInput, NotebookEditInput, ReadMcpResourceInput, TodoWriteInput, WebFetchInput, WebSearchInput } from "@anthropic-ai/claude-agent-sdk/sdk-tools";
|
|
3
3
|
|
|
4
|
-
//#region src/event.d.ts
|
|
5
|
-
declare const SUPPORTED_HOOK_EVENTS: readonly ["PreToolUse", "PostToolUse", "Notification", "UserPromptSubmit", "SessionStart", "SessionEnd", "Stop", "SubagentStop", "PreCompact"];
|
|
6
|
-
/**
|
|
7
|
-
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#hook-events}
|
|
8
|
-
*/
|
|
9
|
-
type SupportedHookEvent = (typeof SUPPORTED_HOOK_EVENTS)[number];
|
|
10
|
-
//#endregion
|
|
11
|
-
//#region src/output.d.ts
|
|
12
|
-
type HookOutput = {
|
|
13
|
-
PreToolUse: PreToolUseHookOutput;
|
|
14
|
-
PostToolUse: PostToolUseHookOutput;
|
|
15
|
-
UserPromptSubmit: UserPromptSubmitHookOutput;
|
|
16
|
-
Stop: StopHookOutput;
|
|
17
|
-
SubagentStop: SubagentStopHookOutput;
|
|
18
|
-
SessionStart: SessionStartHookOutput;
|
|
19
|
-
Notification: CommonHookOutputs;
|
|
20
|
-
PreCompact: CommonHookOutputs;
|
|
21
|
-
SessionEnd: CommonHookOutputs;
|
|
22
|
-
};
|
|
23
|
-
type ExtractHookOutput<TEvent$1 extends SupportedHookEvent> = HookOutput extends Record<SupportedHookEvent, unknown> ? HookOutput[TEvent$1] : never;
|
|
24
|
-
/**
|
|
25
|
-
* Common fields of hook outputs
|
|
26
|
-
*
|
|
27
|
-
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#common-json-fields}
|
|
28
|
-
*/
|
|
29
|
-
type CommonHookOutputs = {
|
|
30
|
-
/**
|
|
31
|
-
* Whether Claude should continue after hook execution
|
|
32
|
-
*
|
|
33
|
-
* If `continue` is false, Claude stops processing after the hooks run.
|
|
34
|
-
* @default true
|
|
35
|
-
*/
|
|
36
|
-
continue?: boolean;
|
|
37
|
-
/**
|
|
38
|
-
* Accompanies `continue` with a `reason` shown to the user, not shown to Claude.
|
|
39
|
-
*
|
|
40
|
-
* NOT FOR CLAUDE
|
|
41
|
-
*/
|
|
42
|
-
stopReason?: string;
|
|
43
|
-
/**
|
|
44
|
-
* If `true`, user cannot see the stdout of this hook.
|
|
45
|
-
*
|
|
46
|
-
* @default false
|
|
47
|
-
*/
|
|
48
|
-
suppressOutput?: boolean;
|
|
49
|
-
/**
|
|
50
|
-
* Optional warning message shown to the user
|
|
51
|
-
*/
|
|
52
|
-
systemMessage?: string;
|
|
53
|
-
/**
|
|
54
|
-
* Use `hookSpecificOutput` in appropriate hook events instead.
|
|
55
|
-
*
|
|
56
|
-
* @deprecated
|
|
57
|
-
*/
|
|
58
|
-
reason?: string;
|
|
59
|
-
/**
|
|
60
|
-
* Use `hookSpecificOutput` in appropriate hook events instead.
|
|
61
|
-
*
|
|
62
|
-
* @deprecated
|
|
63
|
-
*/
|
|
64
|
-
decision?: "approve" | "block";
|
|
65
|
-
};
|
|
66
|
-
/**
|
|
67
|
-
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#pretooluse-decision-control}
|
|
68
|
-
*/
|
|
69
|
-
interface PreToolUseHookOutput extends CommonHookOutputs {
|
|
70
|
-
hookSpecificOutput?: {
|
|
71
|
-
hookEventName: "PreToolUse";
|
|
72
|
-
/**
|
|
73
|
-
* - `allow` bypasses the permission system. `permissionDecisionReason` is shown to the user but not to Claude.
|
|
74
|
-
* - `deny` prevents the tool call from executing. `permissionDecisionReason` is shown to Claude.
|
|
75
|
-
* - `ask` asks the user to confirm the tool call in the UI. `permissionDecisionReason` is shown to the user but not to Claude.
|
|
76
|
-
*/
|
|
77
|
-
permissionDecision?: "allow" | "ask" | "deny";
|
|
78
|
-
permissionDecisionReason?: string;
|
|
79
|
-
updatedInput?: Record<string, unknown>;
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#posttooluse-decision-control}
|
|
84
|
-
*/
|
|
85
|
-
interface PostToolUseHookOutput extends CommonHookOutputs {
|
|
86
|
-
/**
|
|
87
|
-
* - `approve` has no effect; the tool response is processed normally.
|
|
88
|
-
* - `block` automatically prompts Claude with `reason`.
|
|
89
|
-
*/
|
|
90
|
-
decision?: "approve" | "block";
|
|
91
|
-
hookSpecificOutput?: {
|
|
92
|
-
hookEventName: "PostToolUse";
|
|
93
|
-
/**
|
|
94
|
-
* Adds context for Claude to consider.
|
|
95
|
-
*/
|
|
96
|
-
additionalContext?: string;
|
|
97
|
-
updatedMCPToolOutput?: unknown;
|
|
98
|
-
};
|
|
99
|
-
reason?: string;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#userpromptsubmit-decision-control}
|
|
103
|
-
*/
|
|
104
|
-
interface UserPromptSubmitHookOutput extends CommonHookOutputs {
|
|
105
|
-
/**
|
|
106
|
-
* - `approve` has no effect; the tool response is processed normally.
|
|
107
|
-
* - `block` prevents the prompt from being processed.
|
|
108
|
-
* The submitted prompt is erased from context. `reason` is shown to the user but not added to context.
|
|
109
|
-
*/
|
|
110
|
-
decision?: "approve" | "block";
|
|
111
|
-
hookSpecificOutput?: {
|
|
112
|
-
hookEventName: "UserPromptSubmit";
|
|
113
|
-
/**
|
|
114
|
-
* Adds the string to the context if not blocked.
|
|
115
|
-
*/
|
|
116
|
-
additionalContext?: string;
|
|
117
|
-
};
|
|
118
|
-
reason?: string;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#stop%2Fsubagentstop-decision-control}
|
|
122
|
-
*/
|
|
123
|
-
interface StopHookOutput extends CommonHookOutputs {
|
|
124
|
-
/**
|
|
125
|
-
* - `approve` has no effect; the tool response is processed normally.
|
|
126
|
-
* - `block` prevents Claude from stopping. You must populate `reason` for Claude to know how to proceed.
|
|
127
|
-
*/
|
|
128
|
-
decision?: "approve" | "block";
|
|
129
|
-
/**
|
|
130
|
-
* Reason for the decision.
|
|
131
|
-
*/
|
|
132
|
-
reason?: string;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#stop%2Fsubagentstop-decision-control}
|
|
136
|
-
*/
|
|
137
|
-
interface SubagentStopHookOutput extends CommonHookOutputs {
|
|
138
|
-
/**
|
|
139
|
-
* - `approve` has no effect; the tool response is processed normally.
|
|
140
|
-
* - `block` prevents Claude from stopping. You must populate `reason` for Claude to know how to proceed.
|
|
141
|
-
*/
|
|
142
|
-
/**
|
|
143
|
-
* Reason for the decision.
|
|
144
|
-
*/
|
|
145
|
-
reason?: string;
|
|
146
|
-
}
|
|
147
|
-
interface SessionStartHookOutput extends CommonHookOutputs {
|
|
148
|
-
hookSpecificOutput?: {
|
|
149
|
-
hookEventName: "SessionStart";
|
|
150
|
-
/**
|
|
151
|
-
* Adds the string to the context.
|
|
152
|
-
*/
|
|
153
|
-
additionalContext?: string;
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
//#endregion
|
|
157
4
|
//#region src/utils/types.d.ts
|
|
158
5
|
type Awaitable<T> = Promise<T> | T;
|
|
159
6
|
type AutoComplete<T extends string> = Record<string, never> & T;
|
|
160
7
|
//#endregion
|
|
161
|
-
//#region src/input/schemas.d.ts
|
|
8
|
+
//#region src/hooks/input/schemas.d.ts
|
|
162
9
|
/**
|
|
163
10
|
* @package
|
|
164
11
|
*/
|
|
@@ -172,6 +19,7 @@ declare const HookInputSchemas: {
|
|
|
172
19
|
} & {
|
|
173
20
|
tool_name: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.TransformAction<string, AutoComplete<string>>]>;
|
|
174
21
|
tool_input: v.UnknownSchema;
|
|
22
|
+
tool_use_id: v.StringSchema<undefined>;
|
|
175
23
|
}, undefined>;
|
|
176
24
|
readonly PostToolUse: v.ObjectSchema<{
|
|
177
25
|
readonly cwd: v.StringSchema<undefined>;
|
|
@@ -183,6 +31,7 @@ declare const HookInputSchemas: {
|
|
|
183
31
|
tool_name: v.SchemaWithPipe<readonly [v.StringSchema<undefined>, v.TransformAction<string, AutoComplete<string>>]>;
|
|
184
32
|
tool_input: v.UnknownSchema;
|
|
185
33
|
tool_response: v.UnknownSchema;
|
|
34
|
+
tool_use_id: v.StringSchema<undefined>;
|
|
186
35
|
}, undefined>;
|
|
187
36
|
readonly Notification: v.ObjectSchema<{
|
|
188
37
|
readonly cwd: v.StringSchema<undefined>;
|
|
@@ -213,6 +62,16 @@ declare const HookInputSchemas: {
|
|
|
213
62
|
} & {
|
|
214
63
|
stop_hook_active: v.BooleanSchema<undefined>;
|
|
215
64
|
}, undefined>;
|
|
65
|
+
readonly SubagentStart: v.ObjectSchema<{
|
|
66
|
+
readonly cwd: v.StringSchema<undefined>;
|
|
67
|
+
readonly permission_mode: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
68
|
+
readonly session_id: v.StringSchema<undefined>;
|
|
69
|
+
readonly transcript_path: v.StringSchema<undefined>;
|
|
70
|
+
readonly hook_event_name: v.LiteralSchema<"SubagentStart", undefined>;
|
|
71
|
+
} & {
|
|
72
|
+
agent_id: v.StringSchema<undefined>;
|
|
73
|
+
agent_type: v.StringSchema<undefined>;
|
|
74
|
+
}, undefined>;
|
|
216
75
|
readonly SubagentStop: v.ObjectSchema<{
|
|
217
76
|
readonly cwd: v.StringSchema<undefined>;
|
|
218
77
|
readonly permission_mode: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
@@ -252,9 +111,27 @@ declare const HookInputSchemas: {
|
|
|
252
111
|
} & {
|
|
253
112
|
reason: v.StringSchema<undefined>;
|
|
254
113
|
}, undefined>;
|
|
114
|
+
readonly PermissionRequest: v.ObjectSchema<{
|
|
115
|
+
readonly cwd: v.StringSchema<undefined>;
|
|
116
|
+
readonly permission_mode: v.ExactOptionalSchema<v.StringSchema<undefined>, undefined>;
|
|
117
|
+
readonly session_id: v.StringSchema<undefined>;
|
|
118
|
+
readonly transcript_path: v.StringSchema<undefined>;
|
|
119
|
+
readonly hook_event_name: v.LiteralSchema<"PermissionRequest", undefined>;
|
|
120
|
+
} & {
|
|
121
|
+
tool_input: v.UnknownSchema;
|
|
122
|
+
tool_name: v.StringSchema<undefined>;
|
|
123
|
+
}, undefined>;
|
|
255
124
|
};
|
|
256
125
|
//#endregion
|
|
257
|
-
//#region src/
|
|
126
|
+
//#region src/hooks/event.d.ts
|
|
127
|
+
/**
|
|
128
|
+
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#hook-events}
|
|
129
|
+
*
|
|
130
|
+
* @package
|
|
131
|
+
*/
|
|
132
|
+
type SupportedHookEvent = "PreToolUse" | "PostToolUse" | "Notification" | "UserPromptSubmit" | "SessionStart" | "SessionEnd" | "Stop" | "SubagentStart" | "SubagentStop" | "PreCompact" | "PermissionRequest";
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/hooks/input/types.d.ts
|
|
258
135
|
/**
|
|
259
136
|
* Internal type that combines base hook inputs with tool-specific inputs for PreToolUse events.
|
|
260
137
|
* For non-PreToolUse events, this is equivalent to BaseHookInputs.
|
|
@@ -266,7 +143,7 @@ declare const HookInputSchemas: {
|
|
|
266
143
|
* ```
|
|
267
144
|
* @package
|
|
268
145
|
*/
|
|
269
|
-
type
|
|
146
|
+
type HookInput = { [EventKey in SupportedHookEvent]: EventKey extends "PreToolUse" ? ToolSpecificPreToolUseInput & {
|
|
270
147
|
default: BaseHookInputs["PreToolUse"];
|
|
271
148
|
} : EventKey extends "PostToolUse" ? ToolSpecificPostToolUseInput & {
|
|
272
149
|
default: BaseHookInputs["PostToolUse"];
|
|
@@ -290,7 +167,7 @@ type HookInputs = { [EventKey in SupportedHookEvent]: EventKey extends "PreToolU
|
|
|
290
167
|
* ```
|
|
291
168
|
* @package
|
|
292
169
|
*/
|
|
293
|
-
type ExtractAllHookInputsForEvent<TEvent$1 extends SupportedHookEvent> = { [K in keyof
|
|
170
|
+
type ExtractAllHookInputsForEvent<TEvent$1 extends SupportedHookEvent> = { [K in keyof HookInput[TEvent$1]]: HookInput[TEvent$1][K] }[keyof HookInput[TEvent$1]];
|
|
294
171
|
/**
|
|
295
172
|
* Extracts the hook input type for a specific tool within a given event type.
|
|
296
173
|
* This type utility is used to get strongly-typed inputs for tool-specific hook handlers.
|
|
@@ -311,11 +188,11 @@ type ExtractAllHookInputsForEvent<TEvent$1 extends SupportedHookEvent> = { [K in
|
|
|
311
188
|
* ```
|
|
312
189
|
* @package
|
|
313
190
|
*/
|
|
314
|
-
type ExtractSpecificHookInputForEvent<TEvent$1 extends SupportedHookEvent, TSpecificKey extends ExtractExtendedSpecificKeys<TEvent$1>> = TSpecificKey extends keyof
|
|
191
|
+
type ExtractSpecificHookInputForEvent<TEvent$1 extends SupportedHookEvent, TSpecificKey extends ExtractExtendedSpecificKeys<TEvent$1>> = TSpecificKey extends keyof HookInput[TEvent$1] ? HookInput[TEvent$1][TSpecificKey] : never;
|
|
315
192
|
/**
|
|
316
193
|
* @package
|
|
317
194
|
*/
|
|
318
|
-
type ExtractExtendedSpecificKeys<TEvent$1 extends SupportedHookEvent> = Exclude<keyof
|
|
195
|
+
type ExtractExtendedSpecificKeys<TEvent$1 extends SupportedHookEvent> = Exclude<keyof HookInput[TEvent$1], "default">;
|
|
319
196
|
type BaseHookInputs = { [EventKey in SupportedHookEvent]: v.InferOutput<(typeof HookInputSchemas)[EventKey]> };
|
|
320
197
|
type ToolSpecificPreToolUseInput = { [K in keyof ToolSchema]: Omit<BaseHookInputs["PreToolUse"], "tool_input" | "tool_name"> & {
|
|
321
198
|
tool_input: ToolSchema[K]["input"];
|
|
@@ -327,6 +204,190 @@ type ToolSpecificPostToolUseInput = { [K in keyof ToolSchema]: Omit<BaseHookInpu
|
|
|
327
204
|
tool_response: ToolSchema[K]["response"];
|
|
328
205
|
} };
|
|
329
206
|
//#endregion
|
|
207
|
+
//#region src/hooks/output/index.d.ts
|
|
208
|
+
/**
|
|
209
|
+
* @package
|
|
210
|
+
*/
|
|
211
|
+
type HookOutput = {
|
|
212
|
+
PreToolUse: PreToolUseHookOutput;
|
|
213
|
+
PostToolUse: PostToolUseHookOutput;
|
|
214
|
+
UserPromptSubmit: UserPromptSubmitHookOutput;
|
|
215
|
+
Stop: StopHookOutput;
|
|
216
|
+
SubagentStart: SubagentStartHookOutput;
|
|
217
|
+
SubagentStop: SubagentStopHookOutput;
|
|
218
|
+
SessionStart: SessionStartHookOutput;
|
|
219
|
+
PermissionRequest: PermissionRequestHookOutput;
|
|
220
|
+
Notification: CommonHookOutputs;
|
|
221
|
+
PreCompact: CommonHookOutputs;
|
|
222
|
+
SessionEnd: CommonHookOutputs;
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* @package
|
|
226
|
+
*/
|
|
227
|
+
type ExtractHookOutput<TEvent$1 extends SupportedHookEvent> = HookOutput extends Record<SupportedHookEvent, unknown> ? HookOutput[TEvent$1] : never;
|
|
228
|
+
/**
|
|
229
|
+
* Common fields of hook outputs
|
|
230
|
+
*
|
|
231
|
+
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#common-json-fields}
|
|
232
|
+
*/
|
|
233
|
+
type CommonHookOutputs = {
|
|
234
|
+
/**
|
|
235
|
+
* Whether Claude should continue after hook execution
|
|
236
|
+
*
|
|
237
|
+
* If `continue` is false, Claude stops processing after the hooks run.
|
|
238
|
+
* @default true
|
|
239
|
+
*/
|
|
240
|
+
continue?: boolean;
|
|
241
|
+
/**
|
|
242
|
+
* Accompanies `continue` with a `reason` shown to the user, not shown to Claude.
|
|
243
|
+
*
|
|
244
|
+
* NOT FOR CLAUDE
|
|
245
|
+
*/
|
|
246
|
+
stopReason?: string;
|
|
247
|
+
/**
|
|
248
|
+
* If `true`, user cannot see the stdout of this hook.
|
|
249
|
+
*
|
|
250
|
+
* @default false
|
|
251
|
+
*/
|
|
252
|
+
suppressOutput?: boolean;
|
|
253
|
+
/**
|
|
254
|
+
* Optional warning message shown to the user
|
|
255
|
+
*/
|
|
256
|
+
systemMessage?: string;
|
|
257
|
+
/**
|
|
258
|
+
* Use `hookSpecificOutput` in appropriate hook events instead.
|
|
259
|
+
*
|
|
260
|
+
* @deprecated
|
|
261
|
+
*/
|
|
262
|
+
reason?: string;
|
|
263
|
+
/**
|
|
264
|
+
* Use `hookSpecificOutput` in appropriate hook events instead.
|
|
265
|
+
*
|
|
266
|
+
* @deprecated
|
|
267
|
+
*/
|
|
268
|
+
decision?: "approve" | "block";
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#pretooluse-decision-control}
|
|
272
|
+
*/
|
|
273
|
+
interface PreToolUseHookOutput extends CommonHookOutputs {
|
|
274
|
+
hookSpecificOutput?: {
|
|
275
|
+
hookEventName: "PreToolUse";
|
|
276
|
+
/**
|
|
277
|
+
* - `allow` bypasses the permission system. `permissionDecisionReason` is shown to the user but not to Claude.
|
|
278
|
+
* - `deny` prevents the tool call from executing. `permissionDecisionReason` is shown to Claude.
|
|
279
|
+
* - `ask` asks the user to confirm the tool call in the UI. `permissionDecisionReason` is shown to the user but not to Claude.
|
|
280
|
+
*/
|
|
281
|
+
permissionDecision?: "allow" | "ask" | "deny";
|
|
282
|
+
permissionDecisionReason?: string;
|
|
283
|
+
updatedInput?: Record<string, unknown>;
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#posttooluse-decision-control}
|
|
288
|
+
*/
|
|
289
|
+
interface PostToolUseHookOutput extends CommonHookOutputs {
|
|
290
|
+
/**
|
|
291
|
+
* - `approve` has no effect; the tool response is processed normally.
|
|
292
|
+
* - `block` automatically prompts Claude with `reason`.
|
|
293
|
+
*/
|
|
294
|
+
decision?: "approve" | "block";
|
|
295
|
+
hookSpecificOutput?: {
|
|
296
|
+
hookEventName: "PostToolUse";
|
|
297
|
+
/**
|
|
298
|
+
* Adds context for Claude to consider.
|
|
299
|
+
*/
|
|
300
|
+
additionalContext?: string;
|
|
301
|
+
updatedMCPToolOutput?: unknown;
|
|
302
|
+
};
|
|
303
|
+
reason?: string;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#userpromptsubmit-decision-control}
|
|
307
|
+
*/
|
|
308
|
+
interface UserPromptSubmitHookOutput extends CommonHookOutputs {
|
|
309
|
+
/**
|
|
310
|
+
* - `approve` has no effect; the tool response is processed normally.
|
|
311
|
+
* - `block` prevents the prompt from being processed.
|
|
312
|
+
* The submitted prompt is erased from context. `reason` is shown to the user but not added to context.
|
|
313
|
+
*/
|
|
314
|
+
decision?: "approve" | "block";
|
|
315
|
+
hookSpecificOutput?: {
|
|
316
|
+
hookEventName: "UserPromptSubmit";
|
|
317
|
+
/**
|
|
318
|
+
* Adds the string to the context if not blocked.
|
|
319
|
+
*/
|
|
320
|
+
additionalContext?: string;
|
|
321
|
+
};
|
|
322
|
+
reason?: string;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#stop%2Fsubagentstop-decision-control}
|
|
326
|
+
*/
|
|
327
|
+
interface StopHookOutput extends CommonHookOutputs {
|
|
328
|
+
/**
|
|
329
|
+
* - `approve` has no effect; the tool response is processed normally.
|
|
330
|
+
* - `block` prevents Claude from stopping. You must populate `reason` for Claude to know how to proceed.
|
|
331
|
+
*/
|
|
332
|
+
decision?: "approve" | "block";
|
|
333
|
+
/**
|
|
334
|
+
* Reason for the decision.
|
|
335
|
+
*/
|
|
336
|
+
reason?: string;
|
|
337
|
+
}
|
|
338
|
+
interface SubagentStartHookOutput extends CommonHookOutputs {
|
|
339
|
+
hookSpecificOutput?: {
|
|
340
|
+
hookEventName: "SubagentStart";
|
|
341
|
+
/**
|
|
342
|
+
* Adds the string to the context.
|
|
343
|
+
*/
|
|
344
|
+
additionalContext?: string;
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* @see {@link https://docs.anthropic.com/en/docs/claude-code/hooks#stop%2Fsubagentstop-decision-control}
|
|
349
|
+
*/
|
|
350
|
+
interface SubagentStopHookOutput extends CommonHookOutputs {
|
|
351
|
+
/**
|
|
352
|
+
* - `approve` has no effect; the tool response is processed normally.
|
|
353
|
+
* - `block` prevents Claude from stopping. You must populate `reason` for Claude to know how to proceed.
|
|
354
|
+
*/
|
|
355
|
+
/**
|
|
356
|
+
* Reason for the decision.
|
|
357
|
+
*/
|
|
358
|
+
reason?: string;
|
|
359
|
+
}
|
|
360
|
+
interface SessionStartHookOutput extends CommonHookOutputs {
|
|
361
|
+
hookSpecificOutput?: {
|
|
362
|
+
hookEventName: "SessionStart";
|
|
363
|
+
/**
|
|
364
|
+
* Adds the string to the context.
|
|
365
|
+
*/
|
|
366
|
+
additionalContext?: string;
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* @see {@link https://code.claude.com/docs/en/hooks#permissionrequest-decision-control}
|
|
371
|
+
*/
|
|
372
|
+
interface PermissionRequestHookOutput extends CommonHookOutputs {
|
|
373
|
+
hookSpecificOutput?: {
|
|
374
|
+
hookEventName: "PermissionRequest";
|
|
375
|
+
/**
|
|
376
|
+
* For `behavior: "allow"` you can also optionally pass in an `updatedInput` that modifies the tool’s input parameters before the tool executes.
|
|
377
|
+
*
|
|
378
|
+
* For `behavior: "deny"` you can also optionally pass in a `message` string that tells the model why the permission was denied, and a boolean `interrupt` which will stop Claude.
|
|
379
|
+
*/
|
|
380
|
+
decision: {
|
|
381
|
+
behavior: "allow";
|
|
382
|
+
updatedInput?: Record<string, unknown>;
|
|
383
|
+
} | {
|
|
384
|
+
behavior: "deny";
|
|
385
|
+
interrupt?: boolean;
|
|
386
|
+
message?: string;
|
|
387
|
+
};
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
//#endregion
|
|
330
391
|
//#region src/types.d.ts
|
|
331
392
|
type HookTrigger = Partial<{ [TEvent in SupportedHookEvent]: true | Partial<{ [SchemaKey in ExtractExtendedSpecificKeys<TEvent>]?: true }> }>;
|
|
332
393
|
type ExtractTriggeredHookInput<TTrigger extends HookTrigger> = { [EventKey in keyof TTrigger]: EventKey extends SupportedHookEvent ? TTrigger[EventKey] extends true ? ExtractAllHookInputsForEvent<EventKey> : TTrigger[EventKey] extends Record<PropertyKey, true> ? { [SpecificKey in keyof TTrigger[EventKey]]: SpecificKey extends ExtractExtendedSpecificKeys<EventKey> ? ExtractSpecificHookInputForEvent<EventKey, SpecificKey> : never }[keyof TTrigger[EventKey]] : never : never }[keyof TTrigger];
|
package/dist/index.mjs
CHANGED
|
@@ -44,12 +44,14 @@ function buildHookInputSchema(hook_event_name, entries) {
|
|
|
44
44
|
const HookInputSchemas = {
|
|
45
45
|
PreToolUse: buildHookInputSchema("PreToolUse", {
|
|
46
46
|
tool_name: v.pipe(v.string(), v.transform((s) => s)),
|
|
47
|
-
tool_input: v.unknown()
|
|
47
|
+
tool_input: v.unknown(),
|
|
48
|
+
tool_use_id: v.string()
|
|
48
49
|
}),
|
|
49
50
|
PostToolUse: buildHookInputSchema("PostToolUse", {
|
|
50
51
|
tool_name: v.pipe(v.string(), v.transform((s) => s)),
|
|
51
52
|
tool_input: v.unknown(),
|
|
52
|
-
tool_response: v.unknown()
|
|
53
|
+
tool_response: v.unknown(),
|
|
54
|
+
tool_use_id: v.string()
|
|
53
55
|
}),
|
|
54
56
|
Notification: buildHookInputSchema("Notification", {
|
|
55
57
|
message: v.string(),
|
|
@@ -58,6 +60,10 @@ const HookInputSchemas = {
|
|
|
58
60
|
}),
|
|
59
61
|
UserPromptSubmit: buildHookInputSchema("UserPromptSubmit", { prompt: v.string() }),
|
|
60
62
|
Stop: buildHookInputSchema("Stop", { stop_hook_active: v.boolean() }),
|
|
63
|
+
SubagentStart: buildHookInputSchema("SubagentStart", {
|
|
64
|
+
agent_id: v.string(),
|
|
65
|
+
agent_type: v.string()
|
|
66
|
+
}),
|
|
61
67
|
SubagentStop: buildHookInputSchema("SubagentStop", {
|
|
62
68
|
agent_id: v.string(),
|
|
63
69
|
agent_transcript_path: v.string(),
|
|
@@ -73,7 +79,11 @@ const HookInputSchemas = {
|
|
|
73
79
|
v.literal("clear"),
|
|
74
80
|
v.literal("compact")
|
|
75
81
|
]) }),
|
|
76
|
-
SessionEnd: buildHookInputSchema("SessionEnd", { reason: v.string() })
|
|
82
|
+
SessionEnd: buildHookInputSchema("SessionEnd", { reason: v.string() }),
|
|
83
|
+
PermissionRequest: buildHookInputSchema("PermissionRequest", {
|
|
84
|
+
tool_input: v.unknown(),
|
|
85
|
+
tool_name: v.string()
|
|
86
|
+
})
|
|
77
87
|
};
|
|
78
88
|
function isNonEmptyString(value) {
|
|
79
89
|
return typeof value === "string" && value.length > 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cc-hooks-ts",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.45",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Write claude code hooks with type safety",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -43,32 +43,33 @@
|
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@arethetypeswrong/core": "0.18.2",
|
|
46
|
-
"@biomejs/biome": "2.3.
|
|
47
|
-
"@types/node": "24.10.
|
|
46
|
+
"@biomejs/biome": "2.3.5",
|
|
47
|
+
"@types/node": "24.10.1",
|
|
48
48
|
"@typescript/native-preview": "^7.0.0-dev.20251108.1",
|
|
49
49
|
"@virtual-live-lab/eslint-config": "2.3.1",
|
|
50
50
|
"@virtual-live-lab/tsconfig": "2.1.21",
|
|
51
51
|
"eslint": "9.39.1",
|
|
52
|
-
"eslint-plugin-import-access": "3.
|
|
52
|
+
"eslint-plugin-import-access": "3.1.0",
|
|
53
53
|
"pkg-pr-new": "0.0.60",
|
|
54
54
|
"publint": "0.3.15",
|
|
55
55
|
"release-it": "19.0.6",
|
|
56
56
|
"release-it-pnpm": "4.6.6",
|
|
57
|
-
"tsdown": "0.16.
|
|
57
|
+
"tsdown": "0.16.4",
|
|
58
58
|
"type-fest": "5.2.0",
|
|
59
59
|
"typescript": "5.9.3",
|
|
60
|
-
"typescript-eslint": "8.46.
|
|
61
|
-
"unplugin-unused": "0.5.
|
|
62
|
-
"vitest": "4.0.
|
|
60
|
+
"typescript-eslint": "8.46.4",
|
|
61
|
+
"unplugin-unused": "0.5.6",
|
|
62
|
+
"vitest": "4.0.9"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@anthropic-ai/claude-agent-sdk": "0.1.
|
|
65
|
+
"@anthropic-ai/claude-agent-sdk": "0.1.45",
|
|
66
66
|
"valibot": "^1.1.0"
|
|
67
67
|
},
|
|
68
68
|
"scripts": {
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"format
|
|
69
|
+
"check": "pnpm run format:check && pnpm run lint && pnpm run typecheck && pnpm run test && pnpm run build",
|
|
70
|
+
"lint": "eslint --max-warnings 0",
|
|
71
|
+
"format": "biome format --write",
|
|
72
|
+
"format:check": "biome format --reporter=github",
|
|
72
73
|
"typecheck": "tsgo --noEmit",
|
|
73
74
|
"test": "vitest --run",
|
|
74
75
|
"build": "tsdown",
|