Skip to main content

systemprompt_agent/models/a2a/
jsonrpc.rs

1//! JSON-RPC 2.0 framing primitives: request identifiers and the request and
2//! response envelopes used by the A2A protocol.
3
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7pub const JSON_RPC_VERSION_2_0: &str = "2.0";
8
9#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
10#[serde(untagged)]
11pub enum RequestId {
12    String(String),
13    Number(i64),
14}
15
16pub type NumberOrString = RequestId;
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct Request<T> {
20    pub jsonrpc: String,
21    pub method: String,
22    pub params: T,
23    pub id: RequestId,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
27pub struct JsonRpcResponse<T> {
28    pub jsonrpc: String,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub result: Option<T>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub error: Option<JsonRpcError>,
33    pub id: RequestId,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
37pub struct JsonRpcError {
38    pub code: i32,
39    pub message: String,
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub data: Option<Value>,
42}
43
44impl JsonRpcError {
45    pub fn new(code: i32, message: impl Into<String>) -> Self {
46        Self {
47            code,
48            message: message.into(),
49            data: None,
50        }
51    }
52
53    pub fn with_data(code: i32, message: impl Into<String>, data: Value) -> Self {
54        Self {
55            code,
56            message: message.into(),
57            data: Some(data),
58        }
59    }
60
61    pub fn parse_error() -> Self {
62        Self::new(-32700, "Parse error")
63    }
64
65    pub fn invalid_request() -> Self {
66        Self::new(-32600, "Invalid Request")
67    }
68
69    pub fn method_not_found() -> Self {
70        Self::new(-32601, "Method not found")
71    }
72
73    pub fn invalid_params() -> Self {
74        Self::new(-32602, "Invalid params")
75    }
76
77    pub fn internal_error() -> Self {
78        Self::new(-32603, "Internal error")
79    }
80}