//! JSON-RPC 2.0 envelope types with the Grok `session_id` / `seq` //! extensions. //! //! Two distinct id concepts coexist in this crate: //! //! - [`JsonRpcId`] (this module) is the JSON-RPC envelope `id` field — //! string OR number on the wire, per-connection, sender-allocated. //! - [`crate::RequestId`] is an opaque newtype wrapping a string, used //! internally as a correlator (e.g. to key in-flight maps). Convert //! between them via [`JsonRpcId::from_request_id`] / //! [`JsonRpcId::as_request_id`]. use std::fmt; use serde::{Deserialize, Deserializer, Serialize, Serializer, de}; use crate::{FrameSeq, IdError, RequestId, SessionId}; /// JSON-RPC 2.0 protocol version marker. /// /// Serializes as the literal string `"2.0"` and rejects any other value on /// deserialize. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub struct JsonRpcVersion; impl JsonRpcVersion { pub const VERSION: &'static str = "2.0"; } impl fmt::Display for JsonRpcVersion { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(Self::VERSION) } } impl Serialize for JsonRpcVersion { fn serialize(&self, serializer: S) -> Result { serializer.serialize_str(Self::VERSION) } } impl<'de> Deserialize<'de> for JsonRpcVersion { fn deserialize>(deserializer: D) -> Result { struct V; impl de::Visitor<'_> for V { type Value = JsonRpcVersion; fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "the literal string \"{}\"", JsonRpcVersion::VERSION) } fn visit_str(self, v: &str) -> Result { if v == JsonRpcVersion::VERSION { Ok(JsonRpcVersion) } else { Err(E::custom(format!( "expected jsonrpc \"{}\", got {v:?}", JsonRpcVersion::VERSION ))) } } } deserializer.deserialize_str(V) } } /// JSON-RPC 2.0 envelope `id` field. /// /// Per the spec the `id` MAY be a string, a number, or null. We accept the /// first two on deserialize and emit a string ourselves. Null ids are not /// produced and not modelled on the receive path. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(untagged)] pub enum JsonRpcId { String(String), Number(i64), } impl JsonRpcId { pub fn new_string(s: impl Into) -> Self { Self::String(s.into()) } /// Build a fresh UUID v7-backed id. pub fn new_uuid_v7() -> Self { Self::String(uuid::Uuid::now_v7().to_string()) } pub fn from_request_id(id: &RequestId) -> Self { Self::String(id.as_str().to_owned()) } /// Project to a [`RequestId`]. Numeric ids are stringified. Returns /// an error if the resulting string would be empty. pub fn as_request_id(&self) -> Result { match self { Self::String(s) => RequestId::new(s.as_str()), Self::Number(n) => RequestId::new(n.to_string()), } } } impl fmt::Display for JsonRpcId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::String(s) => f.write_str(s), Self::Number(n) => write!(f, "{n}"), } } } /// JSON-RPC 2.0 request envelope. /// /// Generic over `params` so callers can pin a concrete schema (e.g. /// [`crate::frames::ToolCallParams`]) without losing the envelope's /// invariants. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct JsonRpcRequest

{ pub jsonrpc: JsonRpcVersion, pub id: JsonRpcId, /// Grok extension: routing/sanity-check session id. #[serde(default, skip_serializing_if = "Option::is_none")] pub session_id: Option, pub method: String, pub params: P, } /// JSON-RPC 2.0 notification envelope. /// /// No `id` (notifications do not produce a response). `seq` is an /// optional per-connection monotonic counter so receivers can dedup and /// detect drops. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct JsonRpcNotification

{ pub jsonrpc: JsonRpcVersion, #[serde(default, skip_serializing_if = "Option::is_none")] pub session_id: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub seq: Option, pub method: String, pub params: P, } /// JSON-RPC error object. /// /// `code` is the numeric envelope code; `data` typically carries a /// serialized [`crate::error_wire::ToolErrorWire`] so receivers can switch /// on the stable string code rather than the numeric. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct JsonRpcError { pub code: i32, pub message: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub data: Option, } /// JSON-RPC 2.0 response envelope. /// /// Per the spec exactly one of `result` / `error` is present. The custom /// `Serialize` / `Deserialize` impls enforce that invariant: a payload /// containing both keys, or neither, fails to deserialize. #[derive(Debug, Clone, PartialEq)] pub struct JsonRpcResponse { pub jsonrpc: JsonRpcVersion, pub id: JsonRpcId, pub session_id: Option, pub outcome: ResponseOutcome, } /// Either a `result` payload (success) or a [`JsonRpcError`] (failure). #[derive(Debug, Clone, PartialEq)] pub enum ResponseOutcome { Result(R), Error(JsonRpcError), } impl Serialize for JsonRpcResponse { fn serialize(&self, serializer: S) -> Result { use serde::ser::SerializeMap; let mut len = 3; if self.session_id.is_some() { len += 1; } let mut map = serializer.serialize_map(Some(len))?; map.serialize_entry("jsonrpc", &self.jsonrpc)?; map.serialize_entry("id", &self.id)?; if let Some(sid) = &self.session_id { map.serialize_entry("session_id", sid)?; } match &self.outcome { ResponseOutcome::Result(r) => map.serialize_entry("result", r)?, ResponseOutcome::Error(e) => map.serialize_entry("error", e)?, } map.end() } } impl<'de, R: Deserialize<'de>> Deserialize<'de> for JsonRpcResponse { fn deserialize>(deserializer: D) -> Result { // `Option<...>` deserialises to `None` when missing without // `#[serde(default)]`, avoiding a `R: Default` bound on the // result type parameter. #[derive(Deserialize)] struct Flat { jsonrpc: JsonRpcVersion, id: JsonRpcId, session_id: Option, result: Option, error: Option, } let flat = Flat::::deserialize(deserializer)?; let outcome = match (flat.result, flat.error) { (Some(r), None) => ResponseOutcome::Result(r), (None, Some(e)) => ResponseOutcome::Error(e), (Some(_), Some(_)) => { return Err(de::Error::custom( "JSON-RPC response must contain `result` XOR `error`, got both", )); } (None, None) => { return Err(de::Error::custom( "JSON-RPC response must contain `result` or `error`", )); } }; Ok(Self { jsonrpc: flat.jsonrpc, id: flat.id, session_id: flat.session_id, outcome, }) } } impl JsonRpcResponse { pub fn ok(id: JsonRpcId, result: R) -> Self { Self { jsonrpc: JsonRpcVersion, id, session_id: None, outcome: ResponseOutcome::Result(result), } } pub fn err(id: JsonRpcId, error: JsonRpcError) -> Self { Self { jsonrpc: JsonRpcVersion, id, session_id: None, outcome: ResponseOutcome::Error(error), } } pub fn with_session(mut self, sid: SessionId) -> Self { self.session_id = Some(sid); self } }