agentox_core/client/
transport.rs1use crate::error::TransportError;
4use crate::protocol::jsonrpc::{JsonRpcNotification, JsonRpcRequest, JsonRpcResponse};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct TransportCapabilities {
9 pub request_response: bool,
10 pub streaming_notifications: bool,
11}
12
13#[async_trait::async_trait]
15pub trait Transport: Send + Sync {
16 fn capabilities(&self) -> TransportCapabilities {
18 TransportCapabilities {
19 request_response: true,
20 streaming_notifications: false,
21 }
22 }
23
24 async fn write_raw(&mut self, message: &str) -> Result<(), TransportError>;
27
28 async fn request_raw(&mut self, message: &str) -> Result<Option<String>, TransportError>;
31
32 async fn send_request(
34 &mut self,
35 req: &JsonRpcRequest,
36 ) -> Result<JsonRpcResponse, TransportError> {
37 let raw = serde_json::to_string(req)?;
38 let response_str = self
39 .request_raw(&raw)
40 .await?
41 .ok_or(TransportError::NoResponse)?;
42 let response: JsonRpcResponse = serde_json::from_str(&response_str)?;
43 Ok(response)
44 }
45
46 async fn send_notification(
48 &mut self,
49 notif: &JsonRpcNotification,
50 ) -> Result<(), TransportError> {
51 let raw = serde_json::to_string(notif)?;
52 self.write_raw(&raw).await
53 }
54
55 async fn send_raw(&mut self, message: &str) -> Result<Option<String>, TransportError> {
57 self.request_raw(message).await
58 }
59
60 async fn shutdown(&mut self) -> Result<(), TransportError>;
62}