1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4pub const JSONRPC_VERSION: &str = "2.0";
6pub const PROTOCOL_VERSION: &str = "1.0";
8
9pub const PARSE_ERROR: i32 = -32700;
11pub const INVALID_REQUEST: i32 = -32600;
13pub const METHOD_NOT_FOUND: i32 = -32601;
15pub const INVALID_PARAMS: i32 = -32602;
17pub const INTERNAL_ERROR: i32 = -32603;
19
20pub const TASK_NOT_FOUND: i32 = -32001;
22pub const TASK_NOT_CANCELABLE: i32 = -32002;
24pub const PUSH_NOTIFICATION_NOT_SUPPORTED: i32 = -32003;
26pub const UNSUPPORTED_OPERATION: i32 = -32004;
28pub const CONTENT_TYPE_NOT_SUPPORTED: i32 = -32005;
30pub const INVALID_AGENT_RESPONSE: i32 = -32006;
32pub const EXTENDED_AGENT_CARD_NOT_CONFIGURED: i32 = -32007;
34pub const EXTENSION_SUPPORT_REQUIRED: i32 = -32008;
36pub const VERSION_NOT_SUPPORTED: i32 = -32009;
38
39pub const METHOD_SEND_MESSAGE: &str = "SendMessage";
41pub const METHOD_SEND_STREAMING_MESSAGE: &str = "SendStreamingMessage";
43pub const METHOD_GET_TASK: &str = "GetTask";
45pub const METHOD_LIST_TASKS: &str = "ListTasks";
47pub const METHOD_CANCEL_TASK: &str = "CancelTask";
49pub const METHOD_SUBSCRIBE_TO_TASK: &str = "SubscribeToTask";
51pub const METHOD_CREATE_TASK_PUSH_NOTIFICATION_CONFIG: &str = "CreateTaskPushNotificationConfig";
53pub const METHOD_GET_TASK_PUSH_NOTIFICATION_CONFIG: &str = "GetTaskPushNotificationConfig";
55pub const METHOD_LIST_TASK_PUSH_NOTIFICATION_CONFIGS: &str = "ListTaskPushNotificationConfigs";
57pub const METHOD_DELETE_TASK_PUSH_NOTIFICATION_CONFIG: &str = "DeleteTaskPushNotificationConfig";
59pub const METHOD_GET_EXTENDED_AGENT_CARD: &str = "GetExtendedAgentCard";
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct JsonRpcRequest {
65 #[serde(default = "jsonrpc_version")]
66 pub jsonrpc: String,
68 pub method: String,
70 #[serde(default, skip_serializing_if = "Option::is_none")]
71 pub params: Option<Value>,
73 pub id: JsonRpcId,
75}
76
77#[derive(Debug, Clone, Serialize, Deserialize)]
79pub struct JsonRpcResponse {
80 #[serde(default = "jsonrpc_version")]
81 pub jsonrpc: String,
83 #[serde(skip_serializing_if = "Option::is_none")]
84 pub result: Option<Value>,
86 #[serde(skip_serializing_if = "Option::is_none")]
87 pub error: Option<JsonRpcError>,
89 pub id: JsonRpcId,
91}
92
93#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct JsonRpcError {
96 pub code: i32,
98 pub message: String,
100 #[serde(default, skip_serializing_if = "Option::is_none")]
101 pub data: Option<Value>,
103}
104
105#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
107#[serde(untagged)]
108pub enum JsonRpcId {
109 String(String),
111 Number(i64),
113 Null,
115}
116
117fn jsonrpc_version() -> String {
118 JSONRPC_VERSION.to_owned()
119}
120
121#[cfg(test)]
122mod tests {
123 use super::JsonRpcId;
124
125 #[test]
126 fn jsonrpc_id_null_serializes_as_null() {
127 let json = serde_json::to_string(&JsonRpcId::Null).expect("id should serialize");
128 assert_eq!(json, "null");
129
130 let round_trip: JsonRpcId = serde_json::from_str("null").expect("id should deserialize");
131 assert!(matches!(round_trip, JsonRpcId::Null));
132 }
133}