Skip to main content

agentox_core/client/
transport.rs

1//! Transport trait for sending/receiving raw JSON-RPC messages.
2
3use crate::error::TransportError;
4use crate::protocol::jsonrpc::{JsonRpcNotification, JsonRpcRequest, JsonRpcResponse};
5
6/// Transport capabilities used by checks and future transport-specific logic.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct TransportCapabilities {
9    pub request_response: bool,
10    pub streaming_notifications: bool,
11}
12
13/// Trait for sending/receiving raw JSON-RPC messages over any transport.
14#[async_trait::async_trait]
15pub trait Transport: Send + Sync {
16    /// Capability metadata for this transport implementation.
17    fn capabilities(&self) -> TransportCapabilities {
18        TransportCapabilities {
19            request_response: true,
20            streaming_notifications: false,
21        }
22    }
23
24    /// Write a raw message string without reading a response.
25    /// Used for notifications and other one-way messages.
26    async fn write_raw(&mut self, message: &str) -> Result<(), TransportError>;
27
28    /// Write a raw message string and read back one response line.
29    /// Returns `None` if the response line is empty.
30    async fn request_raw(&mut self, message: &str) -> Result<Option<String>, TransportError>;
31
32    /// Send a typed JSON-RPC request and get the parsed response.
33    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    /// Send a JSON-RPC notification (write-only, no response expected).
47    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    /// Send a raw string and read one response. Convenience alias for the fuzzer.
56    async fn send_raw(&mut self, message: &str) -> Result<Option<String>, TransportError> {
57        self.request_raw(message).await
58    }
59
60    /// Shut down the transport gracefully.
61    async fn shutdown(&mut self) -> Result<(), TransportError>;
62}