clm_rust_core/layer4/
gateway.rs1use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use std::collections::HashMap;
9
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
11pub struct GatewayRequest {
12 pub jsonrpc: String,
13 pub method: String,
14 #[serde(default)]
15 pub params: Option<Value>,
16 pub id: Value,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
20pub struct GatewayError {
21 pub code: i32,
22 pub message: String,
23 #[serde(skip_serializing_if = "Option::is_none")]
24 pub data: Option<Value>,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
28pub struct GatewayResponse {
29 pub jsonrpc: String,
30 #[serde(skip_serializing_if = "Option::is_none")]
31 pub result: Option<Value>,
32 #[serde(skip_serializing_if = "Option::is_none")]
33 pub error: Option<GatewayError>,
34 pub id: Value,
35}
36
37pub struct GatewayRouter {
38 card_registry: HashMap<String, Value>,
39}
40
41impl Default for GatewayRouter {
42 fn default() -> Self {
43 Self::new()
44 }
45}
46
47impl GatewayRouter {
48 pub fn new() -> Self {
49 Self {
50 card_registry: HashMap::new(),
51 }
52 }
53
54 pub fn register_card(&mut self, hash: &str, content: Value) {
55 self.card_registry.insert(hash.to_string(), content);
56 }
57
58 pub fn dispatch(&self, request: &GatewayRequest) -> GatewayResponse {
59 if request.jsonrpc != "2.0" {
60 return GatewayResponse {
61 jsonrpc: "2.0".to_string(),
62 result: None,
63 error: Some(GatewayError {
64 code: -32600,
65 message: "Invalid Request: jsonrpc must be '2.0'".to_string(),
66 data: None,
67 }),
68 id: request.id.clone(),
69 };
70 }
71
72 match request.method.as_str() {
73 "clm.status" => GatewayResponse {
74 jsonrpc: "2.0".to_string(),
75 result: Some(serde_json::json!({
76 "status": "ready",
77 "layers": [0, 1, 2, 3, 4, 5],
78 "registered_cards": self.card_registry.len()
79 })),
80 error: None,
81 id: request.id.clone(),
82 },
83 "clm.eval" => {
84 let expr = request
85 .params
86 .as_ref()
87 .and_then(|p| p.get("expression"))
88 .and_then(|e| e.as_str())
89 .unwrap_or("");
90 let hash = crate::layer0::hash::compute_blake3(expr.as_bytes());
91 GatewayResponse {
92 jsonrpc: "2.0".to_string(),
93 result: Some(serde_json::json!({
94 "evaluated": expr,
95 "content_hash": hash,
96 "success": true
97 })),
98 error: None,
99 id: request.id.clone(),
100 }
101 }
102 "clm.card_get" => {
103 let hash = request
104 .params
105 .as_ref()
106 .and_then(|p| p.get("hash"))
107 .and_then(|h| h.as_str())
108 .unwrap_or("");
109 if let Some(content) = self.card_registry.get(hash) {
110 GatewayResponse {
111 jsonrpc: "2.0".to_string(),
112 result: Some(content.clone()),
113 error: None,
114 id: request.id.clone(),
115 }
116 } else {
117 GatewayResponse {
118 jsonrpc: "2.0".to_string(),
119 result: None,
120 error: Some(GatewayError {
121 code: -32004,
122 message: format!("Card not found: {}", hash),
123 data: None,
124 }),
125 id: request.id.clone(),
126 }
127 }
128 }
129 _ => GatewayResponse {
130 jsonrpc: "2.0".to_string(),
131 result: None,
132 error: Some(GatewayError {
133 code: -32601,
134 message: format!("Method not found: {}", request.method),
135 data: None,
136 }),
137 id: request.id.clone(),
138 },
139 }
140 }
141}