tower_a2a/codec/
mod.rs

1//! Serialization codecs for different protocol bindings
2
3pub mod json;
4pub mod jsonrpc;
5pub mod sse;
6
7pub use json::JsonCodec;
8pub use jsonrpc::JsonRpcCodec;
9pub use sse::{SseCodec, SseEvent};
10
11use crate::{
12    protocol::{error::A2AError, operation::A2AOperation},
13    service::response::A2AResponse,
14};
15use bytes::Bytes;
16
17/// Codec trait for encoding and decoding A2A protocol messages
18///
19/// Different codecs implement different protocol bindings (HTTP+JSON, gRPC+Protobuf, etc.)
20pub trait Codec: Send + Sync {
21    /// Serialize an A2A operation to bytes for transport
22    ///
23    /// # Arguments
24    ///
25    /// * `operation` - The A2A operation to encode
26    ///
27    /// # Returns
28    ///
29    /// The serialized bytes or an error
30    fn encode_request(&self, operation: &A2AOperation) -> Result<Bytes, A2AError>;
31
32    /// Deserialize transport response bytes to an A2A response
33    ///
34    /// # Arguments
35    ///
36    /// * `body` - The response body bytes
37    /// * `operation` - The original operation (for context)
38    ///
39    /// # Returns
40    ///
41    /// The deserialized A2A response or an error
42    fn decode_response(
43        &self,
44        body: &[u8],
45        operation: &A2AOperation,
46    ) -> Result<A2AResponse, A2AError>;
47
48    /// Get the content type for this codec
49    ///
50    /// # Returns
51    ///
52    /// The MIME type (e.g., "application/json", "application/protobuf")
53    fn content_type(&self) -> &str;
54}