agentox_core/protocol/
jsonrpc.rs1use serde::{Deserialize, Serialize};
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
10#[serde(untagged)]
11pub enum RequestId {
12 Number(i64),
13 String(String),
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct JsonRpcRequest {
19 pub jsonrpc: String,
20 pub id: RequestId,
21 pub method: String,
22 #[serde(skip_serializing_if = "Option::is_none")]
23 pub params: Option<serde_json::Value>,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct JsonRpcResponse {
29 pub jsonrpc: String,
30 pub id: RequestId,
31 #[serde(skip_serializing_if = "Option::is_none")]
32 pub result: Option<serde_json::Value>,
33 #[serde(skip_serializing_if = "Option::is_none")]
34 pub error: Option<JsonRpcError>,
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct JsonRpcError {
40 pub code: i64,
41 pub message: String,
42 #[serde(skip_serializing_if = "Option::is_none")]
43 pub data: Option<serde_json::Value>,
44}
45
46#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct JsonRpcNotification {
49 pub jsonrpc: String,
50 pub method: String,
51 #[serde(skip_serializing_if = "Option::is_none")]
52 pub params: Option<serde_json::Value>,
53}
54
55impl JsonRpcRequest {
56 pub fn new(id: i64, method: impl Into<String>, params: Option<serde_json::Value>) -> Self {
58 Self {
59 jsonrpc: "2.0".to_string(),
60 id: RequestId::Number(id),
61 method: method.into(),
62 params,
63 }
64 }
65}
66
67impl JsonRpcNotification {
68 pub fn new(method: impl Into<String>, params: Option<serde_json::Value>) -> Self {
70 Self {
71 jsonrpc: "2.0".to_string(),
72 method: method.into(),
73 params,
74 }
75 }
76}