Skip to main content

CopilotSession

Struct CopilotSession 

Source
pub struct CopilotSession { /* private fields */ }
Expand description

Represents a single conversation session with the Copilot CLI.

A session maintains conversation state, handles events, and manages tool execution.

§Examples

let client = CopilotClient::new(CopilotClientOptions::default());
let session = client.create_session(SessionConfig::default()).await?;

// Subscribe to events
let sub = session.on(|event| {
    if event.is_assistant_message() {
        if let Some(content) = event.assistant_message_content() {
            println!("Assistant: {}", content);
        }
    }
}).await;

// Send a message and wait for completion
let response = session.send_and_wait(
    MessageOptions { prompt: "Hello!".into(), attachments: None, mode: None },
    None,
).await?;

// Clean up
session.destroy().await?;

Implementations§

Source§

impl CopilotSession

Source

pub fn session_id(&self) -> &str

Returns the session ID.

Source

pub fn workspace_path(&self) -> Option<&str>

Returns the workspace path (when infinite sessions are enabled).

Source

pub async fn send( &self, options: MessageOptions, ) -> Result<String, CopilotError>

Sends a message to this session.

The message is processed asynchronously. Subscribe to events via on() to receive streaming responses and other session events.

Returns the message ID.

Source

pub async fn send_and_wait( &self, options: MessageOptions, timeout: Option<u64>, ) -> Result<Option<SessionEvent>, CopilotError>

Sends a message and waits until the session becomes idle.

This combines send() with waiting for the session.idle event. Returns the last assistant.message event received, or None.

§Arguments
  • options - The message options
  • timeout - Optional timeout in milliseconds (defaults to 60000)
Source

pub async fn on<F>(&self, handler: F) -> Subscription
where F: Fn(SessionEvent) + Send + Sync + 'static,

Subscribes to all events from this session.

Returns a Subscription that unsubscribes when dropped or when unsubscribe() is called.

Source

pub async fn on_event<F>(&self, event_type: &str, handler: F) -> Subscription
where F: Fn(SessionEvent) + Send + Sync + 'static,

Subscribes to a specific event type from this session.

§Arguments
  • event_type - The event type string (e.g., “assistant.message”, “session.idle”)
  • handler - The callback function
Source

pub async fn register_tool(&self, name: &str, handler: ToolHandler)

Registers a tool handler.

Source

pub async fn register_tools(&self, tools: Vec<(String, ToolHandler)>)

Registers multiple tool handlers.

Source

pub async fn register_permission_handler(&self, handler: PermissionHandlerFn)

Registers a permission request handler.

Source

pub async fn register_user_input_handler(&self, handler: UserInputHandlerFn)

Registers a user input request handler.

Source

pub async fn register_hooks_handler(&self, handler: HooksHandlerFn)

Registers a hooks handler for all hook types.

Source

pub async fn get_messages(&self) -> Result<Vec<SessionEvent>, CopilotError>

Retrieves all events and messages from this session’s history.

Source

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

Destroys this session and releases all associated resources.

After calling this method, the session can no longer be used.

Source

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

Aborts the currently processing message in this session.

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