Package com.github.copilot.sdk.json
Class SessionHooks
java.lang.Object
com.github.copilot.sdk.json.SessionHooks
Hook handlers configuration for a session.
Hooks allow you to intercept and modify various session events including tool execution, user prompts, and session lifecycle events.
Example Usage
var hooks = new SessionHooks().setOnPreToolUse((input, invocation) -> {
System.out.println("Tool being called: " + input.getToolName());
return CompletableFuture.completedFuture(new PreToolUseHookOutput().setPermissionDecision("allow"));
}).setOnPostToolUse((input, invocation) -> {
System.out.println("Tool result: " + input.getToolResult());
return CompletableFuture.completedFuture(null);
}).setOnUserPromptSubmitted((input, invocation) -> {
System.out.println("User prompt: " + input.getPrompt());
return CompletableFuture.completedFuture(null);
}).setOnSessionStart((input, invocation) -> {
System.out.println("Session started: " + input.getSource());
return CompletableFuture.completedFuture(null);
}).setOnSessionEnd((input, invocation) -> {
System.out.println("Session ended: " + input.getReason());
return CompletableFuture.completedFuture(null);
});
var session = client.createSession(new SessionConfig().setHooks(hooks)).get();
- Since:
- 1.0.6
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGets the post-tool-use handler.Gets the pre-tool-use handler.Gets the session-end handler.Gets the session-start handler.Gets the user-prompt-submitted handler.booleanhasHooks()Returns whether any hooks are registered.setOnPostToolUse(PostToolUseHandler onPostToolUse) Sets the handler called after a tool has been executed.setOnPreToolUse(PreToolUseHandler onPreToolUse) Sets the handler called before a tool is executed.setOnSessionEnd(SessionEndHandler onSessionEnd) Sets the handler called when a session ends.setOnSessionStart(SessionStartHandler onSessionStart) Sets the handler called when a session starts.setOnUserPromptSubmitted(UserPromptSubmittedHandler onUserPromptSubmitted) Sets the handler called when the user submits a prompt.
-
Constructor Details
-
SessionHooks
public SessionHooks()
-
-
Method Details
-
getOnPreToolUse
Gets the pre-tool-use handler.- Returns:
- the handler, or
nullif not set
-
setOnPreToolUse
Sets the handler called before a tool is executed.- Parameters:
onPreToolUse- the handler- Returns:
- this instance for method chaining
-
getOnPostToolUse
Gets the post-tool-use handler.- Returns:
- the handler, or
nullif not set
-
setOnPostToolUse
Sets the handler called after a tool has been executed.- Parameters:
onPostToolUse- the handler- Returns:
- this instance for method chaining
-
getOnUserPromptSubmitted
Gets the user-prompt-submitted handler.- Returns:
- the handler, or
nullif not set - Since:
- 1.0.7
-
setOnUserPromptSubmitted
Sets the handler called when the user submits a prompt.- Parameters:
onUserPromptSubmitted- the handler- Returns:
- this instance for method chaining
- Since:
- 1.0.7
-
getOnSessionStart
Gets the session-start handler.- Returns:
- the handler, or
nullif not set - Since:
- 1.0.7
-
setOnSessionStart
Sets the handler called when a session starts.- Parameters:
onSessionStart- the handler- Returns:
- this instance for method chaining
- Since:
- 1.0.7
-
getOnSessionEnd
Gets the session-end handler.- Returns:
- the handler, or
nullif not set - Since:
- 1.0.7
-
setOnSessionEnd
Sets the handler called when a session ends.- Parameters:
onSessionEnd- the handler- Returns:
- this instance for method chaining
- Since:
- 1.0.7
-
hasHooks
public boolean hasHooks()Returns whether any hooks are registered.- Returns:
trueif at least one hook handler is set
-