Class CopilotSession
- All Implemented Interfaces:
AutoCloseable
A session maintains conversation state, handles events, and manages tool
execution. Sessions are created via CopilotClient.createSession(com.github.copilot.sdk.json.SessionConfig) or
resumed via CopilotClient.resumeSession(java.lang.String, com.github.copilot.sdk.json.ResumeSessionConfig).
Example Usage
// Create a session
CopilotSession session = client.createSession(new SessionConfig().setModel("gpt-5")).get();
// Register event handlers
session.on(evt -> {
if (evt instanceof AssistantMessageEvent msg) {
System.out.println(msg.getData().getContent());
}
});
// Send messages
session.sendAndWait(new MessageOptions().setPrompt("Hello!")).get();
// Clean up
session.close();
-
Method Summary
Modifier and TypeMethodDescriptionabort()Aborts the currently processing message in this session.voidclose()Disposes the session and releases all associated resources.Gets the complete list of messages and events in the session.Gets the unique identifier for this session.Gets the path to the session workspace directory when infinite sessions are enabled.on(Consumer<AbstractSessionEvent> handler) Registers a callback for session events.send(MessageOptions options) Sends a message to the Copilot session.Sends a simple text message to the Copilot session.sendAndWait(MessageOptions options) Sends a message and waits until the session becomes idle with default 60 second timeout.sendAndWait(MessageOptions options, long timeoutMs) Sends a message and waits until the session becomes idle.sendAndWait(String prompt) Sends a simple text message and waits until the session becomes idle.
-
Method Details
-
getSessionId
Gets the unique identifier for this session.- Returns:
- the session ID
-
getWorkspacePath
Gets the path to the session workspace directory when infinite sessions are enabled.The workspace directory contains checkpoints/, plan.md, and files/ subdirectories.
- Returns:
- the workspace path, or
nullif infinite sessions are disabled
-
send
Sends a simple text message to the Copilot session.This is a convenience method equivalent to
send(new MessageOptions().setPrompt(prompt)).- Parameters:
prompt- the message text to send- Returns:
- a future that resolves with the message ID assigned by the server
- See Also:
-
sendAndWait
Sends a simple text message and waits until the session becomes idle.This is a convenience method equivalent to
sendAndWait(new MessageOptions().setPrompt(prompt)).- Parameters:
prompt- the message text to send- Returns:
- a future that resolves with the final assistant message event, or
nullif no assistant message was received - See Also:
-
send
Sends a message to the Copilot session.This method sends a message asynchronously and returns immediately. Use
sendAndWait(MessageOptions)to wait for the response.- Parameters:
options- the message options containing the prompt and attachments- Returns:
- a future that resolves with the message ID assigned by the server
- See Also:
-
sendAndWait
Sends a message and waits until the session becomes idle.This method blocks until the assistant finishes processing the message or until the timeout expires. It's suitable for simple request/response interactions where you don't need to process streaming events.
- Parameters:
options- the message options containing the prompt and attachmentstimeoutMs- timeout in milliseconds (0 or negative for no timeout)- Returns:
- a future that resolves with the final assistant message event, or
nullif no assistant message was received. The future completes exceptionally with a TimeoutException if the timeout expires. - See Also:
-
sendAndWait
Sends a message and waits until the session becomes idle with default 60 second timeout.- Parameters:
options- the message options containing the prompt and attachments- Returns:
- a future that resolves with the final assistant message event, or
nullif no assistant message was received - See Also:
-
on
Registers a callback for session events.The handler will be invoked for all events in this session, including assistant messages, tool calls, and session state changes.
Example:
Closeable subscription = session.on(evt -> { if (evt instanceof AssistantMessageEvent msg) { System.out.println(msg.getData().getContent()); } }); // Later, to unsubscribe: subscription.close();- Parameters:
handler- a callback to be invoked when a session event occurs- Returns:
- a Closeable that, when closed, unsubscribes the handler
- See Also:
-
getMessages
Gets the complete list of messages and events in the session.This retrieves the full conversation history, including all user messages, assistant responses, tool invocations, and other session events.
- Returns:
- a future that resolves with a list of all session events
- See Also:
-
abort
Aborts the currently processing message in this session.Use this to cancel a long-running operation or stop the assistant from continuing to generate a response.
- Returns:
- a future that completes when the abort is acknowledged
-
close
public void close()Disposes the session and releases all associated resources.This destroys the session on the server, clears all event handlers, and releases tool and permission handlers. After calling this method, the session cannot be used again.
- Specified by:
closein interfaceAutoCloseable
-