Class SessionHooks

java.lang.Object
com.github.copilot.sdk.json.SessionHooks

public class SessionHooks extends Object
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 Details

    • SessionHooks

      public SessionHooks()
  • Method Details

    • getOnPreToolUse

      public PreToolUseHandler getOnPreToolUse()
      Gets the pre-tool-use handler.
      Returns:
      the handler, or null if not set
    • setOnPreToolUse

      public SessionHooks setOnPreToolUse(PreToolUseHandler onPreToolUse)
      Sets the handler called before a tool is executed.
      Parameters:
      onPreToolUse - the handler
      Returns:
      this instance for method chaining
    • getOnPostToolUse

      public PostToolUseHandler getOnPostToolUse()
      Gets the post-tool-use handler.
      Returns:
      the handler, or null if not set
    • setOnPostToolUse

      public SessionHooks setOnPostToolUse(PostToolUseHandler onPostToolUse)
      Sets the handler called after a tool has been executed.
      Parameters:
      onPostToolUse - the handler
      Returns:
      this instance for method chaining
    • getOnUserPromptSubmitted

      public UserPromptSubmittedHandler getOnUserPromptSubmitted()
      Gets the user-prompt-submitted handler.
      Returns:
      the handler, or null if not set
      Since:
      1.0.7
    • setOnUserPromptSubmitted

      public SessionHooks setOnUserPromptSubmitted(UserPromptSubmittedHandler onUserPromptSubmitted)
      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

      public SessionStartHandler getOnSessionStart()
      Gets the session-start handler.
      Returns:
      the handler, or null if not set
      Since:
      1.0.7
    • setOnSessionStart

      public SessionHooks setOnSessionStart(SessionStartHandler onSessionStart)
      Sets the handler called when a session starts.
      Parameters:
      onSessionStart - the handler
      Returns:
      this instance for method chaining
      Since:
      1.0.7
    • getOnSessionEnd

      public SessionEndHandler getOnSessionEnd()
      Gets the session-end handler.
      Returns:
      the handler, or null if not set
      Since:
      1.0.7
    • setOnSessionEnd

      public SessionHooks setOnSessionEnd(SessionEndHandler onSessionEnd)
      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:
      true if at least one hook handler is set