pub struct Session { /* private fields */ }Expand description
A Copilot conversation session.
Sessions maintain conversation state, handle events, and manage tool execution.
§Example
use copilot_sdk::{Client, SessionConfig, SessionEventData};
#[tokio::main]
async fn main() -> copilot_sdk::Result<()> {
let client = Client::builder().build()?;
client.start().await?;
let session = client.create_session(SessionConfig::default()).await?;
// Subscribe to events
let mut events = session.subscribe();
// Send a message
session.send("Hello!").await?;
// Process events
while let Ok(event) = events.recv().await {
match &event.data {
SessionEventData::AssistantMessage(msg) => println!("{}", msg.content),
SessionEventData::SessionIdle(_) => break,
_ => {}
}
}
client.stop().await;Implementations§
Source§impl Session
impl Session
Sourcepub fn new<F>(
session_id: String,
workspace_path: Option<String>,
invoke_fn: F,
) -> Self
pub fn new<F>( session_id: String, workspace_path: Option<String>, invoke_fn: F, ) -> Self
Create a new session.
This is typically called by the Client when creating a session.
Sourcepub fn session_id(&self) -> &str
pub fn session_id(&self) -> &str
Get the session ID.
Sourcepub fn workspace_path(&self) -> Option<&str>
pub fn workspace_path(&self) -> Option<&str>
Get the workspace path for infinite sessions.
Contains checkpoints/, plan.md, and files/ subdirectories. Returns None if infinite sessions are disabled.
Sourcepub fn subscribe(&self) -> EventSubscription
pub fn subscribe(&self) -> EventSubscription
Subscribe to session events.
Returns a receiver that will receive all session events.
Sourcepub async fn on<F>(&self, handler: F) -> impl FnOnce()
pub async fn on<F>(&self, handler: F) -> impl FnOnce()
Register a callback-based event handler.
Returns an unsubscribe closure. Call it to remove the handler.
Alternatively, use [off] with the internal handler ID.
Sourcepub async fn dispatch_event(&self, event: SessionEvent)
pub async fn dispatch_event(&self, event: SessionEvent)
Dispatch an event to all subscribers.
This is called by the Client when events are received.
Sourcepub async fn send(&self, options: impl Into<MessageOptions>) -> Result<String>
pub async fn send(&self, options: impl Into<MessageOptions>) -> Result<String>
Send a message to the session.
Returns the message ID.
Sourcepub async fn get_messages(&self) -> Result<Vec<SessionEvent>>
pub async fn get_messages(&self) -> Result<Vec<SessionEvent>>
Get all messages in the session.
Sourcepub async fn register_tool(&self, tool: Tool)
pub async fn register_tool(&self, tool: Tool)
Register a tool with this session.
Sourcepub async fn register_tool_with_handler(
&self,
tool: Tool,
handler: Option<ToolHandler>,
)
pub async fn register_tool_with_handler( &self, tool: Tool, handler: Option<ToolHandler>, )
Register a tool with a handler.
Sourcepub async fn register_tools(&self, tools: Vec<Tool>)
pub async fn register_tools(&self, tools: Vec<Tool>)
Register multiple tools.
Sourcepub async fn invoke_tool(
&self,
name: &str,
arguments: &Value,
) -> Result<ToolResultObject>
pub async fn invoke_tool( &self, name: &str, arguments: &Value, ) -> Result<ToolResultObject>
Invoke a tool handler.
Sourcepub async fn register_permission_handler<F>(&self, handler: F)
pub async fn register_permission_handler<F>(&self, handler: F)
Register a permission handler.
Sourcepub async fn handle_permission_request(
&self,
request: &PermissionRequest,
) -> PermissionRequestResult
pub async fn handle_permission_request( &self, request: &PermissionRequest, ) -> PermissionRequestResult
Handle a permission request.
Delegates to the registered permission handler, or denies by default if no handler is set.
Sourcepub async fn register_user_input_handler<F>(&self, handler: F)
pub async fn register_user_input_handler<F>(&self, handler: F)
Register a handler for user input requests from the server.
Sourcepub async fn handle_user_input_request(
&self,
request: &UserInputRequest,
) -> Result<UserInputResponse>
pub async fn handle_user_input_request( &self, request: &UserInputRequest, ) -> Result<UserInputResponse>
Handle a user input request from the server.
Sourcepub async fn has_user_input_handler(&self) -> bool
pub async fn has_user_input_handler(&self) -> bool
Check if a user input handler is registered.
Sourcepub async fn register_hooks(&self, hooks: SessionHooks)
pub async fn register_hooks(&self, hooks: SessionHooks)
Register session hooks.
Source§impl Session
impl Session
Sourcepub async fn wait_for_idle(
&self,
timeout: Option<Duration>,
) -> Result<Option<SessionEvent>>
pub async fn wait_for_idle( &self, timeout: Option<Duration>, ) -> Result<Option<SessionEvent>>
Wait for the session to become idle.
Returns the last assistant message event, or None if no message was received. Uses the specified timeout, or 60 seconds if None.
Sourcepub async fn send_and_wait(
&self,
options: impl Into<MessageOptions>,
timeout: Option<Duration>,
) -> Result<Option<SessionEvent>>
pub async fn send_and_wait( &self, options: impl Into<MessageOptions>, timeout: Option<Duration>, ) -> Result<Option<SessionEvent>>
Send a message and wait for the complete response.
Returns the last AssistantMessage event, or None if session
became idle without producing an assistant message.
Uses the specified timeout, or 60 seconds if None.
Sourcepub async fn send_and_collect(
&self,
options: impl Into<MessageOptions>,
timeout: Option<Duration>,
) -> Result<String>
pub async fn send_and_collect( &self, options: impl Into<MessageOptions>, timeout: Option<Duration>, ) -> Result<String>
Send a message and wait for the response content as a string.
Convenience method that collects all assistant message/delta content. Uses the specified timeout, or 60 seconds if None.
Auto Trait Implementations§
impl Freeze for Session
impl !RefUnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl UnsafeUnpin for Session
impl !UnwindSafe for Session
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more