@@ -26,9 +26,7 @@ Register hooks when creating a session:
2626var hooks = new SessionHooks ()
2727 .setOnPreToolUse((input, invocation) - > {
2828 System . out. println(" Tool: " + input. getToolName());
29- return CompletableFuture . completedFuture(
30- new PreToolUseHookOutput (" allow" , null , null , null , null )
31- );
29+ return CompletableFuture . completedFuture(PreToolUseHookOutput . allow());
3230 })
3331 .setOnPostToolUse((input, invocation) - > {
3432 System . out. println(" Result: " + input. getToolResult());
@@ -83,21 +81,17 @@ var hooks = new SessionHooks()
8381 // Block file deletion
8482 if (tool. equals(" delete_file" )) {
8583 return CompletableFuture . completedFuture(
86- new PreToolUseHookOutput ( " deny " , " File deletion is not allowed" , null , null , null )
84+ PreToolUseHookOutput . deny( " File deletion is not allowed" )
8785 );
8886 }
8987
9088 // Require confirmation for shell commands
9189 if (tool. equals(" run_terminal_cmd" )) {
92- return CompletableFuture . completedFuture(
93- new PreToolUseHookOutput (" ask" , null , null , null , null )
94- );
90+ return CompletableFuture . completedFuture(PreToolUseHookOutput . ask());
9591 }
9692
9793 // Allow everything else
98- return CompletableFuture . completedFuture(
99- new PreToolUseHookOutput (" allow" , null , null , null , null )
100- );
94+ return CompletableFuture . completedFuture(PreToolUseHookOutput . allow());
10195 });
10296```
10397
@@ -116,12 +110,10 @@ var hooks = new SessionHooks()
116110 modifiedArgs. set(" query" , input. getToolArgs(). get(" query" ));
117111
118112 return CompletableFuture . completedFuture(
119- new PreToolUseHookOutput (" allow" , null , modifiedArgs, null , null )
113+ PreToolUseHookOutput . withModifiedArgs (" allow" , modifiedArgs)
120114 );
121115 }
122- return CompletableFuture . completedFuture(
123- new PreToolUseHookOutput (" allow" , null , null , null , null )
124- );
116+ return CompletableFuture . completedFuture(PreToolUseHookOutput . allow());
125117 });
126118```
127119
@@ -310,13 +302,11 @@ public class HooksExample {
310302 // Deny dangerous operations
311303 if (input. getToolName(). contains(" delete" )) {
312304 return CompletableFuture . completedFuture(
313- new PreToolUseHookOutput ( " deny " , " Deletion not allowed" , null , null , null )
305+ PreToolUseHookOutput . deny( " Deletion not allowed" )
314306 );
315307 }
316308
317- return CompletableFuture . completedFuture(
318- new PreToolUseHookOutput (" allow" , null , null , null , null )
319- );
309+ return CompletableFuture . completedFuture(PreToolUseHookOutput . allow());
320310 })
321311
322312 // Logging: track tool results
@@ -383,14 +373,12 @@ To handle errors gracefully in your hooks:
383373.setOnPreToolUse((input, invocation) - > {
384374 try {
385375 // Your logic here
386- return CompletableFuture . completedFuture(
387- new PreToolUseHookOutput (" allow" , null , null , null , null )
388- );
376+ return CompletableFuture . completedFuture(PreToolUseHookOutput . allow());
389377 } catch (Exception e) {
390378 logger. error(" Hook error" , e);
391379 // Fail-safe: deny if something goes wrong
392380 return CompletableFuture . completedFuture(
393- new PreToolUseHookOutput ( " deny " , " Internal error" , null , null , null )
381+ PreToolUseHookOutput . deny( " Internal error" )
394382 );
395383 }
396384})
0 commit comments