Skip to main content

hypen_server/remote/
types.rs

1use hypen_engine::Patch;
2use serde::{Deserialize, Serialize};
3
4/// All messages in the remote UI protocol.
5///
6/// Serializes with `"type"` discriminator in camelCase, matching the
7/// TypeScript/Go/Kotlin SDKs.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(tag = "type", rename_all = "camelCase")]
10pub enum RemoteMessage {
11    /// Client → Server: initiate session handshake.
12    Hello {
13        /// Resume an existing session (optional).
14        #[serde(skip_serializing_if = "Option::is_none", rename = "sessionId")]
15        session_id: Option<String>,
16        /// Client metadata (platform, version, etc.).
17        #[serde(skip_serializing_if = "Option::is_none")]
18        props: Option<serde_json::Value>,
19    },
20
21    /// Server → Client: confirm session creation/resumption.
22    SessionAck {
23        #[serde(rename = "sessionId")]
24        session_id: String,
25        #[serde(rename = "isNew")]
26        is_new: bool,
27        #[serde(rename = "isRestored")]
28        is_restored: bool,
29    },
30
31    /// Server → Client: full tree snapshot on connect.
32    InitialTree {
33        module: String,
34        state: serde_json::Value,
35        patches: Vec<Patch>,
36        revision: u64,
37    },
38
39    /// Server → Client: incremental UI patches after state change.
40    Patch {
41        module: String,
42        patches: Vec<Patch>,
43        revision: u64,
44    },
45
46    /// Server → Client: full state snapshot (for tooling/Studio).
47    StateUpdate {
48        module: String,
49        state: serde_json::Value,
50        revision: u64,
51    },
52
53    /// Client → Server: dispatch an action.
54    DispatchAction {
55        #[serde(default)]
56        module: String,
57        action: String,
58        #[serde(skip_serializing_if = "Option::is_none")]
59        payload: Option<serde_json::Value>,
60    },
61
62    /// Client → Server: request state subscription.
63    SubscribeState { module: String },
64
65    /// Server → Client: session expired.
66    SessionExpired {
67        #[serde(rename = "sessionId")]
68        session_id: String,
69        reason: String,
70    },
71}
72
73impl RemoteMessage {
74    /// Serialize to JSON string.
75    pub fn to_json(&self) -> Result<String, serde_json::Error> {
76        serde_json::to_string(self)
77    }
78
79    /// Deserialize from JSON string.
80    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
81        serde_json::from_str(json)
82    }
83}