Skip to main content

CopilotClient

Struct CopilotClient 

Source
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

Source

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()).

Source

pub async fn get_state(&self) -> ConnectionState

Returns the current connection state.

Source

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.

Source

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.

Source

pub async fn force_stop(&self)

Forcefully stops the client without graceful session cleanup.

Source

pub async fn create_session( &self, config: SessionConfig, ) -> Result<Arc<CopilotSession>, CopilotError>

Creates a new conversation session with the Copilot CLI.

Source

pub async fn resume_session( &self, config: ResumeSessionConfig, ) -> Result<Arc<CopilotSession>, CopilotError>

Resumes an existing conversation session by its ID.

Source

pub async fn get_last_session_id(&self) -> Result<Option<String>, CopilotError>

Gets the last session ID.

Source

pub async fn delete_session(&self, session_id: &str) -> Result<(), CopilotError>

Deletes a session permanently.

Source

pub async fn list_sessions(&self) -> Result<Vec<SessionMetadata>, CopilotError>

Lists all available sessions.

Source

pub async fn ping( &self, message: Option<&str>, ) -> Result<PingResponse, CopilotError>

Sends a ping request to the server.

Source

pub async fn get_status(&self) -> Result<GetStatusResponse, CopilotError>

Gets CLI status including version and protocol information.

Source

pub async fn get_auth_status( &self, ) -> Result<GetAuthStatusResponse, CopilotError>

Gets current authentication status.

Source

pub async fn list_models(&self) -> Result<Vec<ModelInfo>, CopilotError>

Lists available models with their metadata.

Results are cached after the first successful call.

Source

pub async fn get_foreground_session_id( &self, ) -> Result<Option<String>, CopilotError>

Gets the foreground session ID in TUI+server mode.

Source

pub async fn set_foreground_session_id( &self, session_id: &str, ) -> Result<(), CopilotError>

Sets the foreground session in TUI+server mode.

Source

pub async fn on_lifecycle<F>(&self, handler: F) -> u64
where F: Fn(SessionLifecycleEvent) + Send + Sync + 'static,

Subscribes to session lifecycle events.

Returns a handler ID that can be used to unsubscribe.

Source

pub async fn off_lifecycle(&self, handler_id: u64)

Unsubscribes a lifecycle event handler by its ID.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more