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 (CopilotClient client = new CopilotClient()) {
client.start().get();
CopilotSession session = client.createSession(new SessionConfig().setModel("gpt-5")).get();
session.on(evt -> {
if (evt instanceof AssistantMessageEvent msg) {
System.out.println(msg.getData().getContent());
}
});
session.send(new MessageOptions().setPrompt("Hello!")).get();
}
- Since:
- 1.0.0
-
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()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 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.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.start()Starts the Copilot client and connects to the server.stop()Stops the client and closes all sessions.
-
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.- 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:
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-