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        module: String,
56        action: String,
57        #[serde(skip_serializing_if = "Option::is_none")]
58        payload: Option<serde_json::Value>,
59    },
60
61    /// Client → Server: request state subscription.
62    SubscribeState {
63        module: String,
64    },
65
66    /// Server → Client: session expired.
67    SessionExpired {
68        #[serde(rename = "sessionId")]
69        session_id: String,
70        reason: String,
71    },
72}
73
74impl RemoteMessage {
75    /// Serialize to JSON string.
76    pub fn to_json(&self) -> Result<String, serde_json::Error> {
77        serde_json::to_string(self)
78    }
79
80    /// Deserialize from JSON string.
81    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
82        serde_json::from_str(json)
83    }
84}