mhome-core-api 1.10.0

Shared wire types and service contracts for the mHome core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{AuthRequest, AuthenticatedSession, ServiceCoreInput, ServiceCoreOutput};
use std::collections::HashMap;

pub const EXTERNAL_CORE_PROTOCOL_VERSION: u32 = 15;
pub const ARTIFACT_CONTENT_PATH_PREFIX: &str = "/artifact/v1/content/";
pub const ARTIFACT_UPLOAD_PATH_PREFIX: &str = "/artifact/v1/upload/";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExternalCoreRequest {
    pub id: String,
    pub method: ExternalCoreMethod,
    pub payload: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExternalCoreResponse {
    pub id: String,
    pub ok: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<ExternalCoreError>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExternalCoreError {
    pub code: String,
    pub message: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExternalCoreMethod {
    HandleCoreInput,
    RegisterLocalAppClient,
    RegisterNodeConnection,
    CleanupWsState,
    UpdateCallbackBase,
    PutArtifact,
    OpenArtifact,
    AuthorizeArtifactRead,
    AuthorizeArtifactUpload,
    CommitArtifactUpload,
    DeviceIdentity,
    CommissionFingerprint,
    CommissionPublicKeyBase64,
    ListMdnsRecords,
    PollEvents,
    CompleteEvents,
    CommissionChallengePayload,
    PairingStartPayload,
    SetupStartPayload,
    GeneralWebhookPayload,
    InvokeWasmPayload,
    Health,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HandleCoreInputRequest {
    pub input: ServiceCoreInput,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HandleCoreInputResponse {
    pub output: ServiceCoreOutput,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegisterLocalAppClientRequest {
    pub ws_id: String,
    pub auth_request: AuthRequest,
    pub session: AuthenticatedSession,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegisterNodeConnectionRequest {
    pub ws_id: String,
    pub session_id: String,
    pub node_type: String,
    pub node_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub host_name: Option<String>,
    pub tenant_id: String,
    pub scope_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CleanupWsStateRequest {
    pub ws_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateCallbackBaseRequest {
    pub callback_base: String,
    pub host: String,
    pub port: u16,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalCoreDeviceIdentity {
    pub device_id: String,
    pub device_name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ListMdnsRecordsRequest {
    pub port: u16,
    pub addresses: Vec<String>,
    pub host_type: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalCoreMdnsRecord {
    pub service_type: String,
    pub instance_name: String,
    pub port: u16,
    pub properties: HashMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PollExternalCoreEventsRequest {
    pub consumer_id: String,
    pub max_events: u16,
    pub timeout_ms: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PollExternalCoreEventsResponse {
    pub events: Vec<ExternalCoreEvent>,
    pub timed_out: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CompleteExternalCoreEventsRequest {
    pub consumer_id: String,
    pub completions: Vec<ExternalCoreEventCompletion>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CompleteExternalCoreEventsResponse {
    pub completed_event_ids: Vec<String>,
    pub missing_event_ids: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalCoreEvent {
    pub event_id: String,
    pub kind: ExternalCoreEventKind,
    pub payload: Value,
    pub expects_response: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum ExternalCoreEventKind {
    MdnsRecordsChanged,
    DeliveryRequested,
    ConnectionControlRequested,
    HostRuntimeRequest,
    ServiceAppFacadeRequest,
    ScopeOwnedDataPurgeRequested,
    ArtifactDeliveryProjectionRequested,
    ArtifactUploadProjectionRequested,
}

/// Messaging-only Core -> Host request. A successful response means delivery was accepted by
/// the provider implementation, not merely queued in the Core event pump or read by a user.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct MessagingDeliveryRequest {
    pub tenant_id: String,
    pub scope_id: String,
    pub surface_id: String,
    pub target: String,
    pub payload: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct MessagingDeliveryResponse {
    pub outcome: crate::DeliveryOutcome,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExternalArtifactRequestContext {
    pub tenant_id: String,
    pub scope_id: String,
    pub actor_user_id: String,
    pub client_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExternalPutArtifactRequest {
    pub context: ExternalArtifactRequestContext,
    pub artifact: artifact_api::PutArtifactRequest,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExternalOpenArtifactRequest {
    pub context: ExternalArtifactRequestContext,
    pub uri: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExternalAuthorizeArtifactReadRequest {
    pub grant: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExternalAuthorizeArtifactUploadRequest {
    pub grant: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExternalCommitArtifactUploadRequest {
    pub grant: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExternalArtifactReadDescriptor {
    pub path: String,
    pub kind: artifact_api::ArtifactKind,
    pub mime_type: String,
    pub size_bytes: u64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub width: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub height: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration_millis: Option<u64>,
    pub sha256: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ExternalArtifactUploadDescriptor {
    pub upload_id: String,
    pub path: String,
    pub kind: artifact_api::ArtifactKind,
    pub mime_type: String,
    pub size_bytes: u64,
    pub sha256: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration_millis: Option<u64>,
    pub expires_at_unix_ms: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ArtifactDeliveryProjectionRequest {
    pub grant: String,
    pub expires_at_unix_ms: u64,
    pub client_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ArtifactDeliveryProjectionResponse {
    pub url: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ArtifactUploadProjectionRequest {
    pub grant: String,
    pub expires_at_unix_ms: u64,
    pub client_id: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ArtifactUploadProjectionResponse {
    pub url: String,
}

/// An App Facade request delegated by an externally hosted Core to the
/// application service that owns the target's business logic.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalServiceAppFacadeRequest {
    pub target: String,
    pub tenant_id: String,
    pub scope_id: String,
    pub user_id: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,
    pub payload: Value,
}

/// A platform capability request emitted by an externally hosted Core.
///
/// Core owns the onboarding workflow. The native host owns LAN discovery and
/// transport to a discovered candidate, so Android can satisfy this contract
/// without moving application logic into the mobile shell.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalHostRuntimeRequest {
    pub method: ExternalHostRuntimeMethod,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub candidate_id: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub action: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub payload: Option<Value>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum ExternalHostRuntimeMethod {
    Discovery,
    CandidateRequest,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct ScopeOwnedDataPurgeRequest {
    pub tenant_id: String,
    pub scope_id: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalCoreEventCompletion {
    pub event_id: String,
    pub ok: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub response: Option<Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<ExternalCoreError>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HttpPayloadRequest {
    pub payload: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EndpointPayloadRequest {
    pub endpoint_id: String,
    pub payload: Value,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalCoreRuntimeMetadata {
    pub backend: String,
    pub instance_id: String,
    pub pid: u32,
    pub state: String,
    pub started_at: String,
    pub protocol_version: u32,
    pub binary_version: String,
    pub socket_path: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalCoreHealth {
    pub ok: bool,
    pub ready: bool,
    pub backend: String,
    pub binary_version: String,
    pub agent_version: String,
    pub protocol_version: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub instance_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pid: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub started_at: Option<String>,
    #[serde(default)]
    pub capabilities: Vec<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn messaging_delivery_is_a_closed_request_response_contract() {
        let request = serde_json::json!({"tenantId":"t", "scopeId":"s", "surfaceId":"m",
            "target":"/chat/event", "payload":"{}"});
        let decoded: MessagingDeliveryRequest = serde_json::from_value(request.clone()).unwrap();
        assert_eq!(serde_json::to_value(decoded).unwrap(), request);
        let mut invalid = request;
        invalid["unknown"] = serde_json::json!(true);
        assert!(serde_json::from_value::<MessagingDeliveryRequest>(invalid).is_err());
        assert!(
            serde_json::from_value::<MessagingDeliveryResponse>(serde_json::json!({})).is_err()
        );
        assert_eq!(
            serde_json::to_value(ExternalCoreEventKind::DeliveryRequested).unwrap(),
            "deliveryRequested"
        );
    }

    #[test]
    fn external_events_use_the_v15_wire_shape() {
        let event = ExternalCoreEvent {
            event_id: "event-1".to_string(),
            kind: ExternalCoreEventKind::ScopeOwnedDataPurgeRequested,
            payload: serde_json::to_value(ScopeOwnedDataPurgeRequest {
                tenant_id: "tenant-1".to_string(),
                scope_id: "scope-1".to_string(),
            })
            .unwrap(),
            expects_response: true,
        };

        let value = serde_json::to_value(event).unwrap();
        assert_eq!(EXTERNAL_CORE_PROTOCOL_VERSION, 15);
        assert_eq!(value["kind"], "scopeOwnedDataPurgeRequested");
        assert_eq!(value["payload"]["tenantId"], "tenant-1");
        assert_eq!(value["payload"]["scopeId"], "scope-1");
        assert_eq!(value["expectsResponse"], true);

        let facade = ExternalCoreEvent {
            event_id: "event-2".to_string(),
            kind: ExternalCoreEventKind::ServiceAppFacadeRequest,
            payload: serde_json::to_value(ExternalServiceAppFacadeRequest {
                target: "/app/messaging/provider/list".to_string(),
                tenant_id: "tenant-1".to_string(),
                scope_id: "scope-1".to_string(),
                user_id: "user-1".to_string(),
                client_id: None,
                payload: serde_json::json!({"placement": "local"}),
            })
            .unwrap(),
            expects_response: true,
        };
        let value = serde_json::to_value(facade).unwrap();
        assert_eq!(value["kind"], "serviceAppFacadeRequest");
        assert_eq!(value["payload"]["target"], "/app/messaging/provider/list");
        assert_eq!(value["payload"]["userId"], "user-1");
        assert!(value["payload"].get("clientId").is_none());
    }

    #[test]
    fn artifact_host_contract_uses_canonical_wire_types() {
        let descriptor = ExternalArtifactReadDescriptor {
            path: "/runtime/artifacts/blob".to_string(),
            kind: artifact_api::ArtifactKind::Audio,
            mime_type: "audio/ogg".to_string(),
            size_bytes: 42,
            width: None,
            height: None,
            duration_millis: Some(1_500),
            sha256: "a".repeat(64),
        };
        let value = serde_json::to_value(descriptor).unwrap();
        assert_eq!(value["kind"], "AUDIO");
        assert_eq!(value["mimeType"], "audio/ogg");
        assert_eq!(value["durationMillis"], 1_500);
        assert!(value.get("width").is_none());

        let request = ArtifactDeliveryProjectionRequest {
            grant: "payload.signature".to_string(),
            expires_at_unix_ms: 123,
            client_id: "L:client".to_string(),
        };
        let value = serde_json::to_value(request).unwrap();
        assert_eq!(value["expiresAtUnixMs"], 123);
        assert_eq!(value["clientId"], "L:client");
    }

    #[test]
    fn playground_uses_only_the_general_webhook_contract() {
        let general = serde_json::to_value(ExternalCoreMethod::GeneralWebhookPayload).unwrap();
        assert_eq!(general, serde_json::json!("GeneralWebhookPayload"));
        assert!(
            serde_json::from_value::<ExternalCoreMethod>(serde_json::json!(
                "PlaygroundWebhookPayload"
            ))
            .is_err()
        );
    }
}