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
impl CopilotSession
Sourcepub fn session_id(&self) -> &str
pub fn session_id(&self) -> &str
Returns the session ID.
Sourcepub fn workspace_path(&self) -> Option<&str>
pub fn workspace_path(&self) -> Option<&str>
Returns the workspace path (when infinite sessions are enabled).
Sourcepub async fn send(
&self,
options: MessageOptions,
) -> Result<String, CopilotError>
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.
Sourcepub async fn send_and_wait(
&self,
options: MessageOptions,
timeout: Option<u64>,
) -> Result<Option<SessionEvent>, CopilotError>
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 optionstimeout- Optional timeout in milliseconds (defaults to 60000)
Sourcepub async fn on<F>(&self, handler: F) -> Subscription
pub async fn on<F>(&self, handler: F) -> Subscription
Subscribes to all events from this session.
Returns a Subscription that unsubscribes when dropped or when
unsubscribe() is called.
Sourcepub async fn on_event<F>(&self, event_type: &str, handler: F) -> Subscription
pub async fn on_event<F>(&self, event_type: &str, handler: F) -> Subscription
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
Sourcepub async fn register_tool(&self, name: &str, handler: ToolHandler)
pub async fn register_tool(&self, name: &str, handler: ToolHandler)
Registers a tool handler.
Sourcepub async fn register_tools(&self, tools: Vec<(String, ToolHandler)>)
pub async fn register_tools(&self, tools: Vec<(String, ToolHandler)>)
Registers multiple tool handlers.
Sourcepub async fn register_permission_handler(&self, handler: PermissionHandlerFn)
pub async fn register_permission_handler(&self, handler: PermissionHandlerFn)
Registers a permission request handler.
Sourcepub async fn register_user_input_handler(&self, handler: UserInputHandlerFn)
pub async fn register_user_input_handler(&self, handler: UserInputHandlerFn)
Registers a user input request handler.
Sourcepub async fn register_hooks_handler(&self, handler: HooksHandlerFn)
pub async fn register_hooks_handler(&self, handler: HooksHandlerFn)
Registers a hooks handler for all hook types.
Sourcepub async fn get_messages(&self) -> Result<Vec<SessionEvent>, CopilotError>
pub async fn get_messages(&self) -> Result<Vec<SessionEvent>, CopilotError>
Retrieves all events and messages from this session’s history.
Sourcepub async fn destroy(&self) -> Result<(), CopilotError>
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.
Sourcepub async fn abort(&self) -> Result<(), CopilotError>
pub async fn abort(&self) -> Result<(), CopilotError>
Aborts the currently processing message in this session.