Skip to main content

a2a_types/
lib.rs

1//! # A2A (Agent2Agent) Protocol Types
2//!
3//! This crate provides the Rust data structures for the Agent2Agent (A2A) protocol,
4//! generated from the canonical `proto/a2a.proto` definition via `prost` + `pbjson`.
5//!
6//! All types are available directly as `a2a_types::Foo`.
7
8use serde::{Deserialize, Serialize};
9
10// ============================================================================
11// Proto-generated types — included directly at the crate root.
12// No intermediate module; every generated type is a first-class member of
13// `a2a_types`.
14// ============================================================================
15
16include!(concat!(env!("OUT_DIR"), "/lf.a2a.v1.rs"));
17include!(concat!(env!("OUT_DIR"), "/lf.a2a.v1.serde.rs"));
18
19// ============================================================================
20// JSON-RPC 2.0 Wire Types
21//
22// JSON-RPC framing is not in the proto (it is a transport-level binding),
23// so these three types remain hand-written. Used by `a2a-client` and the
24// `web` layer in `radkit` to parse and build JSON-RPC envelopes.
25// ============================================================================
26
27/// Represents a JSON-RPC 2.0 identifier, which can be a string, number, or null.
28#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
29#[serde(untagged)]
30pub enum JSONRPCId {
31    String(String),
32    Integer(i64),
33    Null,
34}
35
36/// Represents a JSON-RPC 2.0 Error Response object.
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct JSONRPCErrorResponse {
39    /// The version of the JSON-RPC protocol. MUST be exactly "2.0".
40    pub jsonrpc: String,
41    /// An object describing the error that occurred.
42    pub error: JSONRPCError,
43    /// The identifier established by the client.
44    pub id: Option<JSONRPCId>,
45}
46
47/// Represents a JSON-RPC 2.0 Error object, included in an error response.
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct JSONRPCError {
50    /// A number that indicates the error type that occurred.
51    pub code: i32,
52    /// A string providing a short description of the error.
53    pub message: String,
54    /// A primitive or structured value containing additional information about the error.
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub data: Option<serde_json::Value>,
57}
58
59// ============================================================================
60// A2A Error Types
61//
62// Thin wrappers carrying well-known JSON-RPC error codes for the A2A protocol.
63// Used by `error_mapper.rs` to build `JSONRPCError` values from `AgentError`.
64// ============================================================================
65
66/// An error indicating that the JSON sent is not a valid Request object.
67#[derive(Debug, Clone, Serialize, Deserialize, Default)]
68#[serde(default)]
69pub struct InvalidRequestError {
70    pub code: i32,
71    pub message: String,
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub data: Option<serde_json::Value>,
74}
75
76impl InvalidRequestError {
77    #[must_use]
78    pub fn new() -> Self {
79        Self {
80            code: -32600,
81            message: "Request payload validation error".to_string(),
82            data: None,
83        }
84    }
85}
86
87/// An error indicating that the method parameters are invalid.
88#[derive(Debug, Clone, Serialize, Deserialize, Default)]
89#[serde(default)]
90pub struct InvalidParamsError {
91    pub code: i32,
92    pub message: String,
93    #[serde(skip_serializing_if = "Option::is_none")]
94    pub data: Option<serde_json::Value>,
95}
96
97impl InvalidParamsError {
98    #[must_use]
99    pub fn new() -> Self {
100        Self {
101            code: -32602,
102            message: "Invalid parameters".to_string(),
103            data: None,
104        }
105    }
106}
107
108/// An error indicating an internal error on the server.
109#[derive(Debug, Clone, Serialize, Deserialize, Default)]
110#[serde(default)]
111pub struct InternalError {
112    pub code: i32,
113    pub message: String,
114    #[serde(skip_serializing_if = "Option::is_none")]
115    pub data: Option<serde_json::Value>,
116}
117
118impl InternalError {
119    #[must_use]
120    pub fn new() -> Self {
121        Self {
122            code: -32603,
123            message: "Internal error".to_string(),
124            data: None,
125        }
126    }
127}
128
129/// An A2A-specific error indicating that the requested task ID was not found.
130#[derive(Debug, Clone, Serialize, Deserialize, Default)]
131#[serde(default)]
132pub struct TaskNotFoundError {
133    pub code: i32,
134    pub message: String,
135    #[serde(skip_serializing_if = "Option::is_none")]
136    pub data: Option<serde_json::Value>,
137}
138
139impl TaskNotFoundError {
140    #[must_use]
141    pub fn new() -> Self {
142        Self {
143            code: -32001,
144            message: "Task not found".to_string(),
145            data: None,
146        }
147    }
148}
149
150/// An A2A-specific error indicating that the requested operation is not supported.
151#[derive(Debug, Clone, Serialize, Deserialize, Default)]
152#[serde(default)]
153pub struct UnsupportedOperationError {
154    pub code: i32,
155    pub message: String,
156    #[serde(skip_serializing_if = "Option::is_none")]
157    pub data: Option<serde_json::Value>,
158}
159
160impl UnsupportedOperationError {
161    #[must_use]
162    pub fn new() -> Self {
163        Self {
164            code: -32004,
165            message: "This operation is not supported".to_string(),
166            data: None,
167        }
168    }
169}