Skip to main content

systemprompt_agent/models/a2a/
jsonrpc.rs

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