use std::sync::Arc;
use agent_client_protocol::{
Client, ConnectionTo, Error,
schema::v1::{
CreateTerminalRequest, CreateTerminalResponse, ReadTextFileRequest, ReadTextFileResponse,
RequestPermissionRequest, RequestPermissionResponse, SessionNotification,
},
};
type AgentCx = ConnectionTo<Client>;
#[derive(Clone)]
pub struct ConnectionHandle {
cx: AgentCx,
}
impl ConnectionHandle {
pub fn new(cx: AgentCx) -> Arc<Self> {
Arc::new(Self { cx })
}
pub fn cx(&self) -> &AgentCx {
&self.cx
}
pub fn send_session_notification(&self, notification: SessionNotification) -> Result<(), Error> {
self.cx.send_notification(notification)
}
pub async fn request_permission(
&self,
request: RequestPermissionRequest,
) -> Result<RequestPermissionResponse, Error> {
self.cx.send_request(request).block_task().await
}
pub async fn read_text_file(&self, request: ReadTextFileRequest) -> Result<ReadTextFileResponse, Error> {
self.cx.send_request(request).block_task().await
}
pub async fn create_terminal(&self, request: CreateTerminalRequest) -> Result<CreateTerminalResponse, Error> {
self.cx.send_request(request).block_task().await
}
}
impl std::fmt::Debug for ConnectionHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ConnectionHandle").finish_non_exhaustive()
}
}