Class CopilotClient
- All Implemented Interfaces:
AutoCloseable
The CopilotClient manages the connection to the Copilot CLI server and provides methods to create and manage conversation sessions. It can either spawn a CLI server process or connect to an existing server.
Example usage:
try (var client = new CopilotClient()) {
client.start().get();
var session = client.createSession(new SessionConfig().setModel("gpt-5")).get();
session.on(AssistantMessageEvent.class, msg -> {
System.out.println(msg.getData().content());
});
session.send(new MessageOptions().setPrompt("Hello!")).get();
}
- Since:
- 1.0.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new CopilotClient with default options.CopilotClient(CopilotClientOptions options) Creates a new CopilotClient with the specified options. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this client using graceful shutdown semantics.Creates a new Copilot session with default configuration.createSession(SessionConfig config) Creates a new Copilot session with the specified configuration.deleteSession(String sessionId) Deletes a session by ID.Forces an immediate stop of the client without graceful cleanup.Gets current authentication status.Gets the ID of the session currently displayed in the TUI.Gets the ID of the most recently used session.getState()Gets the current connection state.Gets CLI status including version and protocol information.Lists available models with their metadata.Lists all available sessions.listSessions(SessionListFilter filter) Lists all available sessions with optional filtering.onLifecycle(SessionLifecycleHandler handler) Subscribes to all session lifecycle events.onLifecycle(String eventType, SessionLifecycleHandler handler) Subscribes to a specific session lifecycle event type.Pings the server to check connectivity.resumeSession(String sessionId) Resumes an existing session with default configuration.resumeSession(String sessionId, ResumeSessionConfig config) Resumes an existing Copilot session.setForegroundSessionId(String sessionId) Requests the TUI to switch to displaying the specified session.start()Starts the Copilot client and connects to the server.stop()Stops the client and closes all sessions.
-
Field Details
-
AUTOCLOSEABLE_TIMEOUT_SECONDS
public static final int AUTOCLOSEABLE_TIMEOUT_SECONDS- See Also:
-
-
Constructor Details
-
CopilotClient
public CopilotClient()Creates a new CopilotClient with default options. -
CopilotClient
Creates a new CopilotClient with the specified options.- Parameters:
options- Options for creating the client- Throws:
IllegalArgumentException- if mutually exclusive options are provided
-
-
Method Details
-
start
Starts the Copilot client and connects to the server.- Returns:
- A future that completes when the connection is established
-
stop
Stops the client and closes all sessions.- Returns:
- A future that completes when the client is stopped
-
forceStop
Forces an immediate stop of the client without graceful cleanup.- Returns:
- A future that completes when the client is stopped
-
createSession
Creates a new Copilot session with the specified configuration.The session maintains conversation state and can be used to send messages and receive responses. Remember to close the session when done.
- Parameters:
config- configuration for the session (model, tools, etc.)- Returns:
- a future that resolves with the created CopilotSession
- See Also:
-
createSession
Creates a new Copilot session with default configuration.- Returns:
- a future that resolves with the created CopilotSession
- See Also:
-
resumeSession
public CompletableFuture<CopilotSession> resumeSession(String sessionId, ResumeSessionConfig config) Resumes an existing Copilot session.This restores a previously saved session, allowing you to continue a conversation. The session's history is preserved.
- Parameters:
sessionId- the ID of the session to resumeconfig- configuration for the resumed session- Returns:
- a future that resolves with the resumed CopilotSession
- See Also:
-
resumeSession
Resumes an existing session with default configuration.- Parameters:
sessionId- the ID of the session to resume- Returns:
- a future that resolves with the resumed CopilotSession
- See Also:
-
getState
Gets the current connection state.- Returns:
- the current connection state
- See Also:
-
ping
Pings the server to check connectivity.This can be used to verify that the server is responsive and to check the protocol version.
- Parameters:
message- an optional message to echo back- Returns:
- a future that resolves with the ping response
- See Also:
-
getStatus
Gets CLI status including version and protocol information.- Returns:
- a future that resolves with the status response containing version and protocol version
- See Also:
-
getAuthStatus
Gets current authentication status.- Returns:
- a future that resolves with the authentication status
- See Also:
-
listModels
Lists available models with their metadata.Results are cached after the first successful call to avoid rate limiting. The cache is cleared when the client disconnects.
- Returns:
- a future that resolves with a list of available models
- See Also:
-
getLastSessionId
Gets the ID of the most recently used session.This is useful for resuming the last conversation without needing to list all sessions.
- Returns:
- a future that resolves with the last session ID, or
nullif no sessions exist - See Also:
-
deleteSession
Deletes a session by ID.This permanently removes the session and its conversation history.
- Parameters:
sessionId- the ID of the session to delete- Returns:
- a future that completes when the session is deleted
- Throws:
RuntimeException- if the deletion fails
-
listSessions
Lists all available sessions.Returns metadata about all sessions that can be resumed, including their IDs, start times, and summaries.
- Returns:
- a future that resolves with a list of session metadata
- See Also:
-
listSessions
Lists all available sessions with optional filtering.Returns metadata about all sessions that can be resumed, including their IDs, start times, summaries, and context information. Use the filter parameter to narrow down sessions by working directory, git repository, or branch.
Example Usage
// List all sessions var allSessions = client.listSessions().get(); // Filter by repository var filter = new SessionListFilter().setRepository("owner/repo"); var repoSessions = client.listSessions(filter).get();- Parameters:
filter- optional filter to narrow down sessions by context fields, ornullto list all sessions- Returns:
- a future that resolves with a list of session metadata
- See Also:
-
getForegroundSessionId
Gets the ID of the session currently displayed in the TUI.This is only available when connecting to a server running in TUI+server mode (--ui-server).
- Returns:
- a future that resolves with the session ID, or null if no foreground session is set
-
setForegroundSessionId
Requests the TUI to switch to displaying the specified session.This is only available when connecting to a server running in TUI+server mode (--ui-server).
- Parameters:
sessionId- the ID of the session to display in the TUI- Returns:
- a future that completes when the operation is done
- Throws:
RuntimeException- if the operation fails
-
onLifecycle
Subscribes to all session lifecycle events.Lifecycle events are emitted when sessions are created, deleted, updated, or change foreground/background state (in TUI+server mode).
- Parameters:
handler- a callback that receives lifecycle events- Returns:
- an AutoCloseable that, when closed, unsubscribes the handler
-
onLifecycle
Subscribes to a specific session lifecycle event type.- Parameters:
eventType- the event type to listen for (useSessionLifecycleEventTypesconstants)handler- a callback that receives events of the specified type- Returns:
- an AutoCloseable that, when closed, unsubscribes the handler
-
close
public void close()Closes this client using graceful shutdown semantics.This method is intended for
try-with-resourcesusage and blocks while waiting forstop()to complete, up toAUTOCLOSEABLE_TIMEOUT_SECONDSseconds. If shutdown fails or times out, the error is logged atLevel.FINEand the method returns.This method is idempotent.
- Specified by:
closein interfaceAutoCloseable- See Also:
-