pub struct CopilotClient { /* private fields */ }Expand description
Main client for interacting with the Copilot CLI.
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.
§Examples
// Create a client with default options (spawns CLI server via stdio)
let client = CopilotClient::new(CopilotClientOptions::default());
// Start the connection
client.start().await?;
// Create a session
let session = client.create_session(SessionConfig::default()).await?;
// Send messages and handle responses
let sub = session.on(|event| {
if let Some(content) = event.assistant_message_content() {
println!("Assistant: {}", content);
}
}).await;
session.send(MessageOptions {
prompt: "Hello!".to_string(),
attachments: None,
mode: None,
}).await?;
// Clean up
session.destroy().await?;
client.stop().await?;Implementations§
Source§impl CopilotClient
impl CopilotClient
Sourcepub fn new(options: CopilotClientOptions) -> Self
pub fn new(options: CopilotClientOptions) -> Self
Creates a new CopilotClient with the given options.
This does not start the connection. Call start() or use auto_start
(which triggers on first create_session()).
Sourcepub async fn get_state(&self) -> ConnectionState
pub async fn get_state(&self) -> ConnectionState
Returns the current connection state.
Sourcepub async fn start(&self) -> Result<(), CopilotError>
pub async fn start(&self) -> Result<(), CopilotError>
Starts the CLI server and establishes a connection.
If connecting to an external server (via cli_url), only establishes the connection.
Otherwise, spawns the CLI server process and then connects.
This method is called automatically when creating a session if auto_start is true.
Sourcepub async fn stop(&self) -> Result<Vec<CopilotError>, CopilotError>
pub async fn stop(&self) -> Result<Vec<CopilotError>, CopilotError>
Stops the CLI server and closes all active sessions.
Returns a list of errors encountered during cleanup.
Sourcepub async fn force_stop(&self)
pub async fn force_stop(&self)
Forcefully stops the client without graceful session cleanup.
Sourcepub async fn create_session(
&self,
config: SessionConfig,
) -> Result<Arc<CopilotSession>, CopilotError>
pub async fn create_session( &self, config: SessionConfig, ) -> Result<Arc<CopilotSession>, CopilotError>
Creates a new conversation session with the Copilot CLI.
Sourcepub async fn resume_session(
&self,
config: ResumeSessionConfig,
) -> Result<Arc<CopilotSession>, CopilotError>
pub async fn resume_session( &self, config: ResumeSessionConfig, ) -> Result<Arc<CopilotSession>, CopilotError>
Resumes an existing conversation session by its ID.
Sourcepub async fn get_last_session_id(&self) -> Result<Option<String>, CopilotError>
pub async fn get_last_session_id(&self) -> Result<Option<String>, CopilotError>
Gets the last session ID.
Sourcepub async fn delete_session(&self, session_id: &str) -> Result<(), CopilotError>
pub async fn delete_session(&self, session_id: &str) -> Result<(), CopilotError>
Deletes a session permanently.
Sourcepub async fn list_sessions(&self) -> Result<Vec<SessionMetadata>, CopilotError>
pub async fn list_sessions(&self) -> Result<Vec<SessionMetadata>, CopilotError>
Lists all available sessions.
Sourcepub async fn ping(
&self,
message: Option<&str>,
) -> Result<PingResponse, CopilotError>
pub async fn ping( &self, message: Option<&str>, ) -> Result<PingResponse, CopilotError>
Sends a ping request to the server.
Sourcepub async fn get_status(&self) -> Result<GetStatusResponse, CopilotError>
pub async fn get_status(&self) -> Result<GetStatusResponse, CopilotError>
Gets CLI status including version and protocol information.
Sourcepub async fn get_auth_status(
&self,
) -> Result<GetAuthStatusResponse, CopilotError>
pub async fn get_auth_status( &self, ) -> Result<GetAuthStatusResponse, CopilotError>
Gets current authentication status.
Sourcepub async fn list_models(&self) -> Result<Vec<ModelInfo>, CopilotError>
pub async fn list_models(&self) -> Result<Vec<ModelInfo>, CopilotError>
Lists available models with their metadata.
Results are cached after the first successful call.
Sourcepub async fn get_foreground_session_id(
&self,
) -> Result<Option<String>, CopilotError>
pub async fn get_foreground_session_id( &self, ) -> Result<Option<String>, CopilotError>
Gets the foreground session ID in TUI+server mode.
Sourcepub async fn set_foreground_session_id(
&self,
session_id: &str,
) -> Result<(), CopilotError>
pub async fn set_foreground_session_id( &self, session_id: &str, ) -> Result<(), CopilotError>
Sets the foreground session in TUI+server mode.
Sourcepub async fn on_lifecycle<F>(&self, handler: F) -> u64
pub async fn on_lifecycle<F>(&self, handler: F) -> u64
Subscribes to session lifecycle events.
Returns a handler ID that can be used to unsubscribe.
Sourcepub async fn off_lifecycle(&self, handler_id: u64)
pub async fn off_lifecycle(&self, handler_id: u64)
Unsubscribes a lifecycle event handler by its ID.