Skip to main content

core_api/
lib.rs

1#![allow(clippy::derivable_impls, clippy::should_implement_trait)]
2
3mod external_rpc;
4
5pub use external_rpc::*;
6
7use serde::{Deserialize, Serialize};
8use std::str::FromStr;
9
10/// Shared MWS transport limits. Both websocket peers must apply these values so
11/// an envelope accepted by one side is never rejected solely due to asymmetric
12/// transport configuration.
13pub const MWS_MAX_MESSAGE_SIZE: usize = 16 * 1024 * 1024;
14pub const MWS_MAX_FRAME_SIZE: usize = 4 * 1024 * 1024;
15pub const MWS_MAX_WRITE_BUFFER_SIZE: usize = 32 * 1024 * 1024;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
18pub enum ExceptionCode {
19    Unsupported,
20    NullData,
21    ErrorSubscribe,
22    ErrorUnsubscribe,
23    ErrorProcessDataReport,
24    ErrorProcessDataPoll,
25    ErrorLock,
26    ErrorUnlock,
27    FunctionNoImpl,
28    Unreachable,
29    Unauthenticated,
30    Unauthorized,
31    PreconditionFail,
32    SessionExpired,
33    Internal,
34    Unknown,
35    NotFound,
36    AlreadyExists,
37    BadRequest,
38    InvalidWidgetDefinition,
39    DeadlineExceeded,
40    TemporaryUnavailable,
41    ResourceLocked,
42    WsConnectionLost,
43    BillAutomationExceed,
44    BillTokenExceed,
45    MissingContext,
46}
47
48impl ExceptionCode {
49    pub fn from_str(s: &str) -> Self {
50        match s {
51            "UNSUPPORTED" => Self::Unsupported,
52            "NULL_DATA" => Self::NullData,
53            "ERROR_SUBSCRIBE" => Self::ErrorSubscribe,
54            "ERROR_UNSUBSCRIBE" => Self::ErrorUnsubscribe,
55            "ERROR_PROCESS_DATA_REPORT" => Self::ErrorProcessDataReport,
56            "ERROR_PROCESS_DATA_POLL" => Self::ErrorProcessDataPoll,
57            "ERROR_LOCK" => Self::ErrorLock,
58            "ERROR_UNLOCK" => Self::ErrorUnlock,
59            "FUNCTION_NO_IMPL" => Self::FunctionNoImpl,
60            "UNREACHABLE" => Self::Unreachable,
61            "UNAUTHENTICATED" => Self::Unauthenticated,
62            "UNAUTHORIZED" => Self::Unauthorized,
63            "PRECONDITION_FAIL" => Self::PreconditionFail,
64            "SESSION_EXPIRED" => Self::SessionExpired,
65            "INTERNAL" => Self::Internal,
66            "NOT_FOUND" => Self::NotFound,
67            "ALREADY_EXISTS" => Self::AlreadyExists,
68            "BAD_REQUEST" => Self::BadRequest,
69            "INVALID_WIDGET_DEFINITION" => Self::InvalidWidgetDefinition,
70            "DEADLINE_EXCEEDED" => Self::DeadlineExceeded,
71            "TEMPORARY_UNAVAILABLE" => Self::TemporaryUnavailable,
72            "RESOURCE_LOCKED" => Self::ResourceLocked,
73            "WS_CONNECTION_LOST" => Self::WsConnectionLost,
74            "BILL_AUTOMATION_EXCEED" => Self::BillAutomationExceed,
75            "BILL_TOKEN_EXCEED" => Self::BillTokenExceed,
76            "MISSING_CONTEXT" => Self::MissingContext,
77            _ => Self::Unknown,
78        }
79    }
80
81    pub fn as_str(&self) -> &'static str {
82        match self {
83            Self::Unsupported => "UNSUPPORTED",
84            Self::NullData => "NULL_DATA",
85            Self::ErrorSubscribe => "ERROR_SUBSCRIBE",
86            Self::ErrorUnsubscribe => "ERROR_UNSUBSCRIBE",
87            Self::ErrorProcessDataReport => "ERROR_PROCESS_DATA_REPORT",
88            Self::ErrorProcessDataPoll => "ERROR_PROCESS_DATA_POLL",
89            Self::ErrorLock => "ERROR_LOCK",
90            Self::ErrorUnlock => "ERROR_UNLOCK",
91            Self::FunctionNoImpl => "FUNCTION_NO_IMPL",
92            Self::Unreachable => "UNREACHABLE",
93            Self::Unauthenticated => "UNAUTHENTICATED",
94            Self::Unauthorized => "UNAUTHORIZED",
95            Self::PreconditionFail => "PRECONDITION_FAIL",
96            Self::SessionExpired => "SESSION_EXPIRED",
97            Self::Internal => "INTERNAL",
98            Self::Unknown => "UNKNOWN",
99            Self::NotFound => "NOT_FOUND",
100            Self::AlreadyExists => "ALREADY_EXISTS",
101            Self::BadRequest => "BAD_REQUEST",
102            Self::InvalidWidgetDefinition => "INVALID_WIDGET_DEFINITION",
103            Self::DeadlineExceeded => "DEADLINE_EXCEEDED",
104            Self::TemporaryUnavailable => "TEMPORARY_UNAVAILABLE",
105            Self::ResourceLocked => "RESOURCE_LOCKED",
106            Self::WsConnectionLost => "WS_CONNECTION_LOST",
107            Self::BillAutomationExceed => "BILL_AUTOMATION_EXCEED",
108            Self::BillTokenExceed => "BILL_TOKEN_EXCEED",
109            Self::MissingContext => "MISSING_CONTEXT",
110        }
111    }
112}
113
114impl From<ExceptionCode> for String {
115    fn from(value: ExceptionCode) -> Self {
116        value.as_str().to_string()
117    }
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
121pub struct ErrorResponse {
122    pub error: String,
123    pub message: String,
124    #[serde(default, skip_serializing_if = "Option::is_none")]
125    pub details: Option<serde_json::Value>,
126}
127
128impl ErrorResponse {
129    pub fn new(error_code: impl Into<String>, message: impl Into<String>) -> Self {
130        Self {
131            error: error_code.into(),
132            message: message.into(),
133            details: None,
134        }
135    }
136
137    pub fn with_details(mut self, details: serde_json::Value) -> Self {
138        self.details = Some(details);
139        self
140    }
141}
142
143pub struct ClientIds;
144
145impl ClientIds {
146    pub fn from_cloud(peer_id: &str, ws_id: &str) -> String {
147        format!("C:{}:{}", peer_id, ws_id)
148    }
149
150    pub fn from_local(ws_id: &str) -> String {
151        format!("L:{}", ws_id)
152    }
153
154    pub fn from_telegram(bot_id: &str, chat_id: i64) -> String {
155        format!("M:telegram:{}:{}", bot_id, chat_id)
156    }
157
158    pub fn is_cloud(client_id: &str) -> bool {
159        client_id.starts_with("C:")
160    }
161
162    pub fn is_local(client_id: &str) -> bool {
163        client_id.starts_with("L:")
164    }
165
166    pub fn is_telegram(client_id: &str) -> bool {
167        client_id.starts_with("M:telegram:")
168    }
169
170    pub fn is_messaging(client_id: &str) -> bool {
171        client_id.starts_with("M:")
172    }
173
174    pub fn to_telegram_bot_id(client_id: &str) -> Option<i64> {
175        let parts: Vec<&str> = client_id.splitn(6, ':').collect();
176        parts.get(2)?.parse::<i64>().ok()
177    }
178
179    pub fn to_telegram_chat_id(client_id: &str) -> Option<i64> {
180        let parts: Vec<&str> = client_id.splitn(6, ':').collect();
181        parts.get(3)?.parse::<i64>().ok()
182    }
183
184    pub fn to_peer_id(client_id: &str) -> Option<String> {
185        let parts: Vec<&str> = client_id.splitn(3, ':').collect();
186        parts.get(1).map(|s| s.to_string())
187    }
188
189    pub fn to_device_id(client_id: &str) -> Option<String> {
190        let parts: Vec<&str> = client_id.splitn(3, ':').collect();
191        parts.get(1).map(|s| s.to_string())
192    }
193}
194
195pub struct MwsMessageType;
196
197impl MwsMessageType {
198    pub const HUB_REQ: &'static str = "hrq";
199    pub const HUB_RESP: &'static str = "hrp";
200    pub const HUB_DATA: &'static str = "hd";
201    pub const NODE_REQ: &'static str = "nrq";
202    pub const NODE_RESP: &'static str = "nrp";
203    pub const NODE_DATA: &'static str = "nd";
204    pub const AGENT_REQ: &'static str = "grq";
205    pub const AGENT_RESP: &'static str = "grp";
206    pub const AGENT_DATA: &'static str = "gd";
207    pub const CLOUD_REQ: &'static str = "crq";
208    pub const CLOUD_RESP: &'static str = "crp";
209    pub const CLOUD_DATA: &'static str = "cd";
210    pub const APP_REQ: &'static str = "arq";
211    pub const APP_RESP: &'static str = "arp";
212    pub const SERVER_REQ: &'static str = "srq";
213    pub const SERVER_RESP: &'static str = "srp";
214    pub const APP_DATA: &'static str = "ad";
215    pub const SERVER_DATA: &'static str = "sd";
216    pub const PING: &'static str = "pi";
217    pub const PONG: &'static str = "po";
218}
219
220pub struct MwsSource;
221
222impl MwsSource {
223    pub const IOS: &'static str = "ios";
224    pub const ANDROID: &'static str = "android";
225    pub const WINDOWS: &'static str = "windows";
226    pub const MAC: &'static str = "macos";
227    pub const LINUX: &'static str = "linux";
228    pub const WEB: &'static str = "web";
229    pub const PWA: &'static str = "pwa";
230    pub const MESSAGING: &'static str = "messaging";
231
232    pub fn is_desktop(source: &str) -> bool {
233        matches!(source, Self::MAC | Self::WINDOWS | Self::LINUX)
234    }
235}
236
237#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
238#[serde(rename_all = "camelCase")]
239pub struct MwsClientInfo {
240    #[serde(skip_serializing_if = "Option::is_none")]
241    pub client_id: Option<String>,
242    #[serde(skip_serializing_if = "Option::is_none")]
243    pub user_id: Option<String>,
244}
245
246impl MwsClientInfo {
247    pub fn new(client_id: String, user_id: String) -> Self {
248        Self {
249            client_id: Some(client_id),
250            user_id: Some(user_id),
251        }
252    }
253
254    pub fn from_ws_id(ws_id: String) -> Self {
255        Self {
256            client_id: Some(ClientIds::from_local(&ws_id)),
257            user_id: None,
258        }
259    }
260
261    pub fn from_client_id(client_id: String) -> Self {
262        Self {
263            client_id: Some(client_id),
264            user_id: None,
265        }
266    }
267
268    pub fn to_client_id(&self) -> String {
269        self.client_id
270            .clone()
271            .unwrap_or_else(|| ClientIds::from_local("unknown"))
272    }
273}
274
275#[derive(Debug, Clone, Serialize, Deserialize)]
276#[serde(rename_all = "camelCase")]
277pub struct MwsMessage {
278    #[serde(skip_serializing_if = "Option::is_none")]
279    pub scope_id: Option<String>,
280    #[serde(skip_serializing_if = "Option::is_none")]
281    pub from: Option<String>,
282    #[serde(skip_serializing_if = "Option::is_none")]
283    pub target: Option<String>,
284    #[serde(skip_serializing_if = "Option::is_none")]
285    pub sig: Option<String>,
286    #[serde(skip_serializing_if = "Option::is_none")]
287    pub r#type: Option<String>,
288    #[serde(skip_serializing_if = "Option::is_none")]
289    pub payload: Option<String>,
290    #[serde(skip_serializing_if = "Option::is_none")]
291    pub error: Option<ErrorResponse>,
292    #[serde(skip_serializing_if = "Option::is_none")]
293    pub client_info: Option<MwsClientInfo>,
294}
295
296impl MwsMessage {
297    pub fn create(
298        scope_id: Option<String>,
299        target: String,
300        sig: String,
301        payload: String,
302        msg_type: String,
303    ) -> Self {
304        Self {
305            scope_id,
306            target: Some(target),
307            sig: Some(sig),
308            payload: Some(payload),
309            r#type: Some(msg_type),
310            from: None,
311            error: None,
312            client_info: None,
313        }
314    }
315
316    pub fn dummy() -> Self {
317        Self {
318            scope_id: None,
319            from: None,
320            target: None,
321            sig: None,
322            r#type: None,
323            payload: None,
324            error: None,
325            client_info: None,
326        }
327    }
328
329    pub fn server_response(
330        target: String,
331        sig: String,
332        payload: String,
333        client_info: MwsClientInfo,
334    ) -> Self {
335        Self {
336            scope_id: None,
337            from: None,
338            target: Some(target),
339            sig: Some(sig),
340            r#type: Some(MwsMessageType::SERVER_RESP.to_string()),
341            payload: Some(payload),
342            error: None,
343            client_info: Some(client_info),
344        }
345    }
346
347    pub fn server_response_error(
348        target: String,
349        sig: String,
350        error: ErrorResponse,
351        client_info: MwsClientInfo,
352    ) -> Self {
353        Self {
354            scope_id: None,
355            from: None,
356            target: Some(target),
357            sig: Some(sig),
358            r#type: Some(MwsMessageType::SERVER_RESP.to_string()),
359            payload: None,
360            error: Some(error),
361            client_info: Some(client_info),
362        }
363    }
364
365    pub fn unscoped_server_data(
366        target: String,
367        payload: String,
368        client_info: MwsClientInfo,
369    ) -> Self {
370        Self {
371            scope_id: None,
372            from: None,
373            target: Some(target),
374            sig: None,
375            r#type: Some(MwsMessageType::SERVER_DATA.to_string()),
376            payload: Some(payload),
377            error: None,
378            client_info: Some(client_info),
379        }
380    }
381
382    pub fn scoped_server_data(
383        scope_id: String,
384        target: String,
385        payload: String,
386        client_info: Option<MwsClientInfo>,
387    ) -> Self {
388        Self {
389            scope_id: Some(scope_id),
390            from: None,
391            target: Some(target),
392            sig: None,
393            r#type: Some(MwsMessageType::SERVER_DATA.to_string()),
394            payload: Some(payload),
395            error: None,
396            client_info,
397        }
398    }
399
400    pub fn hub_response(
401        target: String,
402        sig: String,
403        payload: String,
404        client_info: MwsClientInfo,
405    ) -> Self {
406        Self {
407            scope_id: None,
408            from: None,
409            target: Some(target),
410            sig: Some(sig),
411            r#type: Some(MwsMessageType::HUB_RESP.to_string()),
412            payload: Some(payload),
413            error: None,
414            client_info: Some(client_info),
415        }
416    }
417
418    pub fn hub_response_error(
419        target: String,
420        sig: String,
421        error: ErrorResponse,
422        client_info: MwsClientInfo,
423    ) -> Self {
424        Self {
425            scope_id: None,
426            from: None,
427            target: Some(target),
428            sig: Some(sig),
429            r#type: Some(MwsMessageType::HUB_RESP.to_string()),
430            payload: None,
431            error: Some(error),
432            client_info: Some(client_info),
433        }
434    }
435
436    pub fn hub_request(
437        scope_id: Option<String>,
438        target: String,
439        sig: String,
440        payload: String,
441    ) -> Self {
442        Self {
443            scope_id,
444            from: None,
445            target: Some(target),
446            sig: Some(sig),
447            r#type: Some(MwsMessageType::HUB_REQ.to_string()),
448            payload: Some(payload),
449            error: None,
450            client_info: None,
451        }
452    }
453
454    pub fn hub_data(
455        scope_id: Option<String>,
456        target: String,
457        sig: String,
458        payload: String,
459    ) -> Self {
460        Self {
461            scope_id,
462            from: None,
463            target: Some(target),
464            sig: Some(sig),
465            r#type: Some(MwsMessageType::HUB_DATA.to_string()),
466            payload: Some(payload),
467            error: None,
468            client_info: None,
469        }
470    }
471
472    pub fn set_from(&mut self, from: String) {
473        self.from = Some(from);
474    }
475
476    pub fn set_target(&mut self, target: String) {
477        self.target = Some(target);
478    }
479
480    pub fn set_sig(&mut self, sig: String) {
481        self.sig = Some(sig);
482    }
483
484    pub fn set_payload(&mut self, payload: String) {
485        self.payload = Some(payload);
486    }
487
488    pub fn set_type(&mut self, msg_type: String) {
489        self.r#type = Some(msg_type);
490    }
491
492    pub fn set_client_info(&mut self, client_info: MwsClientInfo) {
493        self.client_info = Some(client_info);
494    }
495}
496
497impl Default for MwsMessage {
498    fn default() -> Self {
499        Self::dummy()
500    }
501}
502
503#[derive(Debug, Clone, Serialize, Deserialize)]
504#[serde(rename_all = "camelCase")]
505pub struct NodeAuthRequest {
506    pub hub_id: String,
507    pub token: String,
508    pub node_type: String,
509    #[serde(default)]
510    pub node_id: String,
511    #[serde(default)]
512    pub instance_id: String,
513    #[serde(default, skip_serializing_if = "Option::is_none")]
514    pub scope_id: Option<String>,
515    pub host_id: String,
516    #[serde(default, skip_serializing_if = "Option::is_none")]
517    pub host_name: Option<String>,
518    pub fingerprint: String,
519}
520
521#[derive(Debug, Clone, Serialize, Deserialize)]
522#[serde(rename_all = "camelCase")]
523pub struct NodeAuthResponse {
524    pub ok: bool,
525    #[serde(skip_serializing_if = "Option::is_none")]
526    pub session_id: Option<String>,
527    #[serde(skip_serializing_if = "Option::is_none")]
528    pub hub_id: Option<String>,
529    #[serde(skip_serializing_if = "Option::is_none")]
530    pub tenant_id: Option<String>,
531    #[serde(skip_serializing_if = "Option::is_none")]
532    pub scope_id: Option<String>,
533    #[serde(skip_serializing_if = "Option::is_none")]
534    pub error: Option<String>,
535    #[serde(skip_serializing_if = "Option::is_none")]
536    pub error_code: Option<String>,
537    #[serde(skip_serializing_if = "Option::is_none")]
538    pub recovery_action: Option<String>,
539    #[serde(skip_serializing_if = "Option::is_none")]
540    pub node_id: Option<String>,
541}
542
543#[derive(Debug, Clone, Serialize, Deserialize)]
544#[serde(rename_all = "camelCase")]
545pub struct NodeTokenIssueRequest {
546    pub node_type: String,
547    pub candidate_host_id: String,
548    pub candidate_fingerprint: String,
549    pub challenge: NodeChallengeEvidence,
550}
551
552#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
553#[serde(rename_all = "camelCase")]
554pub enum NodeInstancePolicy {
555    Multiple,
556    Singleton,
557    Shared,
558}
559
560impl NodeInstancePolicy {
561    pub fn as_str(self) -> &'static str {
562        match self {
563            Self::Multiple => "multiple",
564            Self::Singleton => "singleton",
565            Self::Shared => "shared",
566        }
567    }
568}
569
570impl Default for NodeInstancePolicy {
571    fn default() -> Self {
572        Self::Multiple
573    }
574}
575
576impl std::fmt::Display for NodeInstancePolicy {
577    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
578        f.write_str(self.as_str())
579    }
580}
581
582impl FromStr for NodeInstancePolicy {
583    type Err = String;
584
585    fn from_str(value: &str) -> Result<Self, Self::Err> {
586        match value.trim() {
587            "multiple" | "Multiple" => Ok(Self::Multiple),
588            "singleton" | "Singleton" => Ok(Self::Singleton),
589            "shared" | "Shared" => Ok(Self::Shared),
590            other => Err(format!("unsupported node instance policy: {other}")),
591        }
592    }
593}
594
595#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
596#[serde(rename_all = "camelCase")]
597pub enum NodeBindingStatus {
598    Pending,
599    Active,
600    Revoked,
601    Expired,
602    Failed,
603}
604
605impl NodeBindingStatus {
606    pub fn as_str(self) -> &'static str {
607        match self {
608            Self::Pending => "pending",
609            Self::Active => "active",
610            Self::Revoked => "revoked",
611            Self::Expired => "expired",
612            Self::Failed => "failed",
613        }
614    }
615}
616
617impl Default for NodeBindingStatus {
618    fn default() -> Self {
619        Self::Active
620    }
621}
622
623impl std::fmt::Display for NodeBindingStatus {
624    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
625        f.write_str(self.as_str())
626    }
627}
628
629impl FromStr for NodeBindingStatus {
630    type Err = String;
631
632    fn from_str(value: &str) -> Result<Self, Self::Err> {
633        match value.trim() {
634            "pending" | "Pending" => Ok(Self::Pending),
635            "active" | "Active" => Ok(Self::Active),
636            "revoked" | "Revoked" => Ok(Self::Revoked),
637            "expired" | "Expired" => Ok(Self::Expired),
638            "failed" | "Failed" => Ok(Self::Failed),
639            other => Err(format!("unsupported node binding status: {other}")),
640        }
641    }
642}
643
644pub const NODE_ONBOARDING_CHALLENGE_PROTOCOL: &str = "meow.node.onboarding.challenge";
645pub const NODE_ONBOARDING_CHALLENGE_AUDIENCE: &str = "meow-core:node-onboarding";
646pub const NODE_ONBOARDING_CHALLENGE_ALGORITHM: &str = "Ed25519";
647pub const NODE_ONBOARDING_TOKEN_ISSUER_PREFIX: &str = "meow-core:hub:";
648pub const NODE_ONBOARDING_TOKEN_AUDIENCE: &str = "meow-node:onboarding";
649pub const NODE_ONBOARDING_ES256_ALGORITHM: &str = "ES256";
650pub const NODE_ONBOARDING_TRANSACTION_TTL_SECONDS: u64 = 5 * 60;
651pub const HUB_CONNECTION_PROOF_TARGET: &str = "/identity/hub/prove";
652pub const HUB_CONNECTION_PROOF_PROTOCOL: &str = "meow.hub.connection.proof.v1";
653
654#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
655#[serde(rename_all = "camelCase")]
656pub struct HubConnectionProofRequest {
657    pub protocol: String,
658    pub hub_id: String,
659    pub tenant_id: String,
660    pub scope_id: String,
661    pub nonce: String,
662}
663
664#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
665#[serde(rename_all = "camelCase")]
666pub struct HubConnectionProofResponse {
667    pub protocol: String,
668    pub hub_id: String,
669    pub tenant_id: String,
670    pub scope_id: String,
671    pub nonce: String,
672    pub key_id: String,
673    pub signature: String,
674}
675
676/// Canonical, domain-separated bytes signed by the Hub for one physical WS connection.
677/// Length prefixes keep the encoding unambiguous without relying on JSON object ordering.
678pub fn hub_connection_proof_signing_payload(request: &HubConnectionProofRequest) -> Vec<u8> {
679    let fields = [
680        request.protocol.as_str(),
681        request.hub_id.as_str(),
682        request.tenant_id.as_str(),
683        request.scope_id.as_str(),
684        request.nonce.as_str(),
685    ];
686    let mut payload = Vec::new();
687    for field in fields {
688        payload.extend_from_slice(&(field.len() as u64).to_be_bytes());
689        payload.extend_from_slice(field.as_bytes());
690    }
691    payload
692}
693
694#[derive(Debug, Clone, Serialize, Deserialize)]
695#[serde(rename_all = "camelCase")]
696pub struct NodeChallengeEvidence {
697    pub protocol: String,
698    pub algorithm: String,
699    pub payload: String,
700    pub signature: String,
701    pub public_key: String,
702    pub fingerprint: String,
703}
704
705#[derive(Debug, Clone, Serialize, Deserialize)]
706#[serde(rename_all = "camelCase")]
707pub struct NodeChallengePayload {
708    pub protocol: String,
709    pub aud: String,
710    pub nonce: String,
711    pub scope_id: String,
712    pub node_type: String,
713    pub host_id: String,
714    pub fingerprint: String,
715    pub service_instance_id: String,
716    pub instance_policy: NodeInstancePolicy,
717    pub instance_slot: String,
718}
719
720#[derive(Debug, Clone, Serialize, Deserialize)]
721#[serde(rename_all = "camelCase")]
722pub struct NodeTokenIssueResponse {
723    pub token: String,
724    pub node_id: String,
725    pub expires_in: i64,
726    pub hub_id: String,
727}
728
729#[derive(Debug, Clone, Serialize, Deserialize)]
730#[serde(rename_all = "camelCase")]
731pub struct NodeTokenRevokeRequest {
732    pub node_type: String,
733    #[serde(default, skip_serializing_if = "Option::is_none")]
734    pub node_id: Option<String>,
735}
736
737#[derive(Debug, Clone, Serialize, Deserialize)]
738#[serde(rename_all = "camelCase")]
739pub struct NodeTokenRevokeResponse {
740    pub success: bool,
741}
742
743#[derive(Debug, Clone, Serialize, Deserialize)]
744#[serde(rename_all = "camelCase")]
745pub struct NodeTokenListItem {
746    pub node_id: String,
747    pub node_type: String,
748    pub hub_id: String,
749    pub host_id: String,
750    pub fingerprint: String,
751    pub service_instance_id: String,
752    pub instance_policy: NodeInstancePolicy,
753    pub instance_slot: String,
754    pub status: NodeBindingStatus,
755    pub issued_at: i64,
756    pub expires_at: i64,
757}
758
759#[derive(Debug, Clone, Serialize, Deserialize)]
760#[serde(rename_all = "camelCase")]
761pub struct NodeTokenListResponse {
762    #[serde(default)]
763    pub nodes: Vec<NodeTokenListItem>,
764}
765
766#[derive(Debug, Clone, Serialize, Deserialize)]
767#[serde(rename_all = "camelCase")]
768pub struct NodeInstanceListItem {
769    pub node_id: String,
770    pub node_type: String,
771    pub hub_id: String,
772    pub host_id: String,
773    #[serde(default, skip_serializing_if = "Option::is_none")]
774    pub host_name: Option<String>,
775    pub fingerprint: String,
776    pub service_instance_id: String,
777    pub instance_policy: NodeInstancePolicy,
778    pub instance_slot: String,
779    pub binding_status: NodeBindingStatus,
780    pub issued_at: i64,
781    pub expires_at: i64,
782    pub connected: bool,
783    pub runtime_status: String,
784}
785
786#[derive(Debug, Clone, Serialize, Deserialize)]
787#[serde(rename_all = "camelCase")]
788pub struct NodeInstanceListResponse {
789    #[serde(default)]
790    pub instances: Vec<NodeInstanceListItem>,
791}
792
793#[derive(Debug, Clone, Serialize, Deserialize)]
794#[serde(rename_all = "camelCase")]
795pub struct AuthenticatedSession {
796    pub tenant_id: String,
797    pub scope_id: String,
798    pub user_id: String,
799    pub is_test: bool,
800}
801
802#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
803#[serde(rename_all = "camelCase")]
804pub struct Image {
805    #[serde(skip_serializing_if = "Option::is_none")]
806    pub url: Option<String>,
807    #[serde(skip_serializing_if = "Option::is_none")]
808    pub base64: Option<String>,
809    #[serde(skip_serializing_if = "Option::is_none")]
810    pub mime_type: Option<String>,
811    #[serde(skip_serializing_if = "Option::is_none")]
812    pub created_at: Option<i64>,
813}
814
815impl Image {
816    pub fn from_base64(base64: impl Into<String>, mime_type: impl Into<String>) -> Self {
817        Self {
818            base64: Some(base64.into()),
819            mime_type: Some(mime_type.into()),
820            ..Self::default()
821        }
822    }
823}
824
825#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
826#[serde(rename_all = "camelCase")]
827pub struct ChatToolCallFunction {
828    pub name: String,
829    pub arguments: String,
830}
831
832#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
833#[serde(rename_all = "camelCase")]
834pub struct ChatToolCall {
835    pub id: String,
836    #[serde(rename = "type")]
837    pub call_type: String,
838    pub function: ChatToolCallFunction,
839}
840
841#[derive(Debug, Clone, Serialize, Deserialize, Default)]
842#[serde(rename_all = "camelCase")]
843pub struct ChatMessage {
844    pub role: String,
845    pub content: String,
846    #[serde(default, skip_serializing_if = "Vec::is_empty")]
847    pub images: Vec<Image>,
848    #[serde(default, skip_serializing_if = "Vec::is_empty")]
849    pub tool_calls: Vec<ChatToolCall>,
850    #[serde(default, skip_serializing_if = "Option::is_none")]
851    pub tool_call_id: Option<String>,
852    #[serde(default, skip_serializing_if = "Option::is_none")]
853    pub name: Option<String>,
854}
855
856#[derive(Debug, Clone, Serialize, Deserialize, Default)]
857#[serde(rename_all = "camelCase")]
858pub struct LlmGenerationOptions {
859    #[serde(default)]
860    pub temperature: Option<f32>,
861    #[serde(default)]
862    pub max_tokens: Option<u32>,
863}
864
865#[derive(Debug, Clone, Serialize, Deserialize, Default)]
866#[serde(rename_all = "camelCase")]
867pub struct LlmCompleteRequest {
868    #[serde(default)]
869    pub use_case: Option<String>,
870    #[serde(default)]
871    pub mode: Option<String>,
872    pub messages: Vec<ChatMessage>,
873    #[serde(default)]
874    pub tools: Option<serde_json::Value>,
875    #[serde(default)]
876    pub provider: Option<serde_json::Value>,
877    #[serde(default)]
878    pub response_format: Option<serde_json::Value>,
879    #[serde(default)]
880    pub options: LlmGenerationOptions,
881    #[serde(default)]
882    pub temperature: Option<f32>,
883    #[serde(default)]
884    pub max_tokens: Option<u32>,
885}
886
887#[derive(Debug, Clone, Serialize, Deserialize, Default)]
888#[serde(rename_all = "camelCase")]
889pub struct LlmRouteInfo {
890    pub provider: String,
891    pub model: String,
892    #[serde(default)]
893    pub mode: Option<String>,
894}
895
896#[derive(Debug, Clone, Serialize, Deserialize, Default)]
897#[serde(rename_all = "camelCase")]
898pub struct LlmCompleteResponse {
899    pub content: String,
900    #[serde(default)]
901    pub tool_calls: Vec<ProviderToolCall>,
902    #[serde(default)]
903    pub route: Option<LlmRouteInfo>,
904}
905
906#[derive(Debug, Clone, Serialize, Deserialize)]
907#[serde(rename_all = "camelCase")]
908pub struct ProviderToolCall {
909    pub id: String,
910    pub name: String,
911    pub arguments_json: String,
912}
913
914#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
915#[serde(rename_all = "snake_case")]
916pub enum ScopeAccessRequirement {
917    Member,
918    Owner,
919}
920
921#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
922#[serde(rename_all = "camelCase", deny_unknown_fields)]
923pub struct ScopeAuthorizationRequest {
924    pub tenant_id: String,
925    pub scope_id: String,
926    pub user_id: String,
927    pub requirement: ScopeAccessRequirement,
928}
929
930#[derive(Debug, Clone, Serialize, Deserialize)]
931pub enum ServiceCoreInput {
932    ClientAuth {
933        message: MwsMessage,
934        ws_id: String,
935        scope_id: String,
936    },
937    ClientRequest {
938        target: String,
939        payload: String,
940        ws_id: String,
941        tenant_id: String,
942        scope_id: String,
943        user_id: String,
944        is_test: bool,
945        #[serde(default, skip_serializing_if = "Option::is_none")]
946        surface_id: Option<String>,
947    },
948    /// Invokes the canonical Conversation application boundary without a
949    /// websocket/client transport identity.
950    ConversationRequest {
951        target: String,
952        payload: String,
953        tenant_id: String,
954        scope_id: String,
955        actor_user_id: String,
956        surface_id: String,
957        is_test: bool,
958    },
959    /// Authorizes a trusted headless actor before a non-Conversation operation
960    /// such as binding a shared messaging surface.
961    ScopeAuthorization {
962        request: ScopeAuthorizationRequest,
963    },
964    NodeAuth {
965        request: NodeAuthRequest,
966    },
967    HubConnectionProof {
968        request: HubConnectionProofRequest,
969    },
970    NodeRequest {
971        target: String,
972        payload: String,
973        tenant_id: String,
974        scope_id: String,
975        node_type: String,
976        instance_id: String,
977    },
978    BackendStatusChanged {
979        tenant_id: String,
980        scope_id: String,
981        target: String,
982        payload: String,
983    },
984    ClientResponse {
985        message: MwsMessage,
986    },
987    NodeResponse {
988        message: MwsMessage,
989    },
990    IssueLocalSessionToken {
991        tenant_id: String,
992        scope_id: String,
993        user_id: String,
994        app_client_id: String,
995    },
996    SetLocalAppClientFocus {
997        ws_id: String,
998        focused: bool,
999    },
1000    RefreshLocalAppClient {
1001        ws_id: String,
1002    },
1003}
1004
1005#[derive(Debug, Clone, Serialize, Deserialize)]
1006pub enum ServiceCoreResponse {
1007    ClientAuth {
1008        session: Option<AuthenticatedSession>,
1009        response: MwsMessage,
1010    },
1011    ClientRequest {
1012        response_payload: Option<String>,
1013        client_info: MwsClientInfo,
1014    },
1015    ConversationRequest {
1016        response_payload: Option<String>,
1017    },
1018    ScopeAuthorization {
1019        authorized: bool,
1020    },
1021    NodeAuth(NodeAuthResponse),
1022    HubConnectionProof(HubConnectionProofResponse),
1023    NodeRequest {
1024        response_payload: Option<String>,
1025    },
1026    LocalSessionToken(String),
1027    Ack {
1028        handled: bool,
1029    },
1030}
1031
1032#[derive(Debug, Clone, Serialize, Deserialize)]
1033pub enum ServiceCoreEffect {
1034    Local {
1035        client_id: String,
1036        message: MwsMessage,
1037    },
1038    Bridge {
1039        tenant_id: String,
1040        scope_id: String,
1041        message: MwsMessage,
1042    },
1043    Node {
1044        connection_key: String,
1045        message: MwsMessage,
1046    },
1047    NodeDisconnect {
1048        connection_key: String,
1049    },
1050    Surface {
1051        tenant_id: String,
1052        scope_id: String,
1053        surface_id: String,
1054        target: String,
1055        payload: String,
1056    },
1057}
1058
1059#[derive(Debug, Clone, Serialize, Deserialize, Default)]
1060pub struct ServiceCoreOutput {
1061    #[serde(skip_serializing_if = "Option::is_none")]
1062    pub response: Option<ServiceCoreResponse>,
1063    #[serde(default, skip_serializing_if = "Vec::is_empty")]
1064    pub effects: Vec<ServiceCoreEffect>,
1065}
1066
1067#[derive(Debug, Clone, Serialize, Deserialize)]
1068#[serde(rename_all = "camelCase")]
1069pub struct UserSetting {
1070    #[serde(default)]
1071    pub script_mode: bool,
1072    #[serde(default)]
1073    pub debug_mode: bool,
1074    #[serde(default)]
1075    pub eng_account: bool,
1076}
1077
1078impl UserSetting {
1079    pub fn new() -> Self {
1080        Self {
1081            script_mode: false,
1082            debug_mode: false,
1083            eng_account: false,
1084        }
1085    }
1086}
1087
1088impl Default for UserSetting {
1089    fn default() -> Self {
1090        Self::new()
1091    }
1092}
1093
1094#[derive(Debug, Clone, Serialize, Deserialize)]
1095#[serde(rename_all = "camelCase")]
1096pub struct UserInfo {
1097    #[serde(skip_serializing_if = "Option::is_none")]
1098    pub id: Option<String>,
1099    #[serde(skip_serializing_if = "Option::is_none")]
1100    pub name: Option<String>,
1101    #[serde(skip_serializing_if = "Option::is_none")]
1102    pub email: Option<String>,
1103    #[serde(skip_serializing_if = "Option::is_none")]
1104    pub setting: Option<UserSetting>,
1105    #[serde(default)]
1106    pub eng: bool,
1107}
1108
1109impl UserInfo {
1110    pub fn new(id: String) -> Self {
1111        Self {
1112            id: Some(id),
1113            name: None,
1114            email: None,
1115            setting: None,
1116            eng: false,
1117        }
1118    }
1119}
1120
1121#[derive(Debug, Clone, Serialize, Deserialize)]
1122#[serde(rename_all = "camelCase")]
1123pub struct ScopeMember {
1124    pub id: Option<String>,
1125    pub email: Option<String>,
1126    pub name: Option<String>,
1127    pub pending: bool,
1128    pub role: Option<String>,
1129}
1130
1131impl Default for ScopeMember {
1132    fn default() -> Self {
1133        Self {
1134            id: None,
1135            email: None,
1136            name: None,
1137            pending: false,
1138            role: None,
1139        }
1140    }
1141}
1142
1143#[derive(Debug, Clone, Serialize, Deserialize)]
1144#[serde(rename_all = "camelCase")]
1145pub struct ScopeInfo {
1146    pub name: Option<String>,
1147    pub id: Option<String>,
1148    pub pending: bool,
1149    pub members: Option<Vec<ScopeMember>>,
1150    pub execution_env: Option<String>,
1151    pub mode: Option<String>,
1152    pub connection_mode: Option<String>,
1153    pub agent_mode: Option<String>,
1154    pub default_active: bool,
1155    #[serde(default)]
1156    pub is_test: bool,
1157}
1158
1159impl Default for ScopeInfo {
1160    fn default() -> Self {
1161        Self {
1162            name: None,
1163            id: None,
1164            pending: false,
1165            members: None,
1166            execution_env: None,
1167            mode: None,
1168            connection_mode: None,
1169            agent_mode: None,
1170            default_active: false,
1171            is_test: false,
1172        }
1173    }
1174}
1175
1176#[derive(Debug, Clone, Serialize, Deserialize)]
1177#[serde(rename_all = "camelCase")]
1178pub struct AuthConfig {
1179    pub tenant_id: String,
1180    pub heartbeat_interval: i32,
1181    pub command_timeout: i32,
1182    pub user_info: UserInfo,
1183    pub scope: ScopeInfo,
1184    #[serde(skip_serializing_if = "Option::is_none")]
1185    pub hub_id: Option<String>,
1186    #[serde(skip_serializing_if = "Option::is_none")]
1187    pub jwt_token: Option<String>,
1188}
1189
1190impl AuthConfig {
1191    pub fn new(
1192        tenant_id: String,
1193        heartbeat_interval: i32,
1194        command_timeout: i32,
1195        user_info: UserInfo,
1196        scope: ScopeInfo,
1197    ) -> Self {
1198        Self {
1199            tenant_id,
1200            heartbeat_interval,
1201            command_timeout,
1202            user_info,
1203            scope,
1204            hub_id: None,
1205            jwt_token: None,
1206        }
1207    }
1208}
1209
1210impl Default for AuthConfig {
1211    fn default() -> Self {
1212        Self {
1213            tenant_id: String::new(),
1214            heartbeat_interval: 240,
1215            command_timeout: 10,
1216            user_info: UserInfo::new(String::new()),
1217            scope: ScopeInfo::default(),
1218            hub_id: None,
1219            jwt_token: None,
1220        }
1221    }
1222}
1223
1224#[derive(Debug, Clone, Serialize, Deserialize)]
1225#[serde(rename_all = "camelCase")]
1226pub struct AuthRequest {
1227    pub token: String,
1228    pub source: String,
1229    pub scope_id: String,
1230    pub device_id: String,
1231    pub client_source: String,
1232    #[serde(default)]
1233    pub tenant_id: Option<String>,
1234    #[serde(default)]
1235    pub hub_id: Option<String>,
1236}
1237
1238#[derive(Debug, Clone, Serialize, Deserialize)]
1239#[serde(rename_all = "camelCase")]
1240pub struct HubMdnsInstanceRecord {
1241    pub tenant_id: String,
1242    pub scope_id: String,
1243    pub hub_id: String,
1244    pub scope_name: String,
1245}
1246
1247#[cfg(test)]
1248mod tests {
1249    use super::{
1250        hub_connection_proof_signing_payload, HubConnectionProofRequest, MwsMessage,
1251        MwsMessageType, HUB_CONNECTION_PROOF_PROTOCOL,
1252    };
1253
1254    #[test]
1255    fn hub_connection_proof_payload_is_unambiguous_and_nonce_bound() {
1256        let request = HubConnectionProofRequest {
1257            protocol: HUB_CONNECTION_PROOF_PROTOCOL.to_string(),
1258            hub_id: "hub-1".to_string(),
1259            tenant_id: "tenant-1".to_string(),
1260            scope_id: "scope-1".to_string(),
1261            nonce: "nonce-1".to_string(),
1262        };
1263        let payload = hub_connection_proof_signing_payload(&request);
1264        let mut changed = request.clone();
1265        changed.nonce = "nonce-2".to_string();
1266
1267        assert_ne!(payload, hub_connection_proof_signing_payload(&changed));
1268        assert!(payload.starts_with(&(HUB_CONNECTION_PROOF_PROTOCOL.len() as u64).to_be_bytes()));
1269    }
1270
1271    #[test]
1272    fn scoped_server_data_builds_scope_envelope() {
1273        let message = MwsMessage::scoped_server_data(
1274            "scope-1".to_string(),
1275            "/dialog".to_string(),
1276            "{}".to_string(),
1277            None,
1278        );
1279
1280        assert_eq!(message.scope_id.as_deref(), Some("scope-1"));
1281        assert_eq!(message.target.as_deref(), Some("/dialog"));
1282        assert_eq!(message.r#type.as_deref(), Some(MwsMessageType::SERVER_DATA));
1283        assert_eq!(message.payload.as_deref(), Some("{}"));
1284        assert!(message.client_info.is_none());
1285    }
1286}