1use serde::{Deserialize, Serialize};
2
3pub const VERSION: &str = crate::node::V1;
4pub const NODE_TYPE: &str = "audiobridge";
5pub const APP_TARGET_PREFIX: &str = "/app/plugin/audiobridge/";
6pub const RUNTIME_TARGET_PREFIX: &str = "/audiobridge/app/";
7pub const DEVICE_SNAPSHOT: &str = "device/snapshot";
8pub const DEVICE_REFRESH: &str = "device/refresh";
9pub const DEVICE_PAIR: &str = "device/pair";
10pub const DEVICE_UNPAIR: &str = "device/unpair";
11pub const DEVICE_SETTINGS_GET: &str = "device/settings/get";
12pub const DEVICE_SETTINGS_UPDATE: &str = "device/settings/update";
13pub const SMART_SPEAKER_SNAPSHOT: &str = "smart-speaker/snapshot";
14pub const SMART_SPEAKER_CREATE: &str = "smart-speaker/create";
15pub const SMART_SPEAKER_UPDATE: &str = "smart-speaker/update";
16pub const SMART_SPEAKER_REMOVE: &str = "smart-speaker/remove";
17pub const AGENT_REPLY_TARGET: &str = "/audiobridge/agent/reply";
18
19#[derive(Debug, Clone, Default, Serialize, Deserialize)]
20#[serde(rename_all = "camelCase", deny_unknown_fields)]
21pub struct EmptyRequest {}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(rename_all = "camelCase", deny_unknown_fields)]
25pub struct DeviceRequest {
26 pub device_id: String,
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
30#[serde(rename_all = "camelCase", deny_unknown_fields)]
31pub struct DeviceSettings {
32 pub listen_enabled: bool,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
36#[serde(rename_all = "camelCase", deny_unknown_fields)]
37pub struct DeviceSettingsUpdateRequest {
38 pub device_id: String,
39 pub settings: DeviceSettings,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
43#[serde(rename_all = "camelCase", deny_unknown_fields)]
44pub struct DeviceSettingsResponse {
45 pub device_id: String,
46 pub settings: DeviceSettings,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
50#[serde(rename_all = "camelCase", deny_unknown_fields)]
51pub struct DeviceSettingsUpdateResponse {
52 pub changed: bool,
53 pub device_id: String,
54 pub settings: DeviceSettings,
55}
56
57#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
58#[serde(rename_all = "lowercase")]
59pub enum AudioDeviceFeature {
60 Speaker,
61 Microphone,
62}
63
64#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
65#[serde(rename_all = "camelCase")]
66pub enum MicrophonePermission {
67 NotDetermined,
68 Granted,
69 Denied,
70 Restricted,
71 Unavailable,
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
75#[serde(rename_all = "camelCase")]
76pub struct AudioBridgeDevice {
77 pub device_id: String,
78 pub display_name: String,
79 pub transport: String,
80 pub features: Vec<AudioDeviceFeature>,
81 pub online: bool,
82 pub settings: DeviceSettings,
83 pub listen_runtime: ListenRuntimeStatus,
84}
85
86#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
87#[serde(rename_all = "camelCase")]
88pub enum ListenRuntimeState {
89 Disabled,
90 Starting,
91 Listening,
92 Failed,
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
96#[serde(rename_all = "camelCase")]
97pub struct ListenRuntimeStatus {
98 pub state: ListenRuntimeState,
99 #[serde(skip_serializing_if = "Option::is_none")]
100 pub reason: Option<String>,
101}
102
103#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
104#[serde(rename_all = "camelCase")]
105pub struct AudioBridgeCandidate {
106 pub device_id: String,
107 pub display_name: String,
108 pub transport: String,
109 pub features: Vec<AudioDeviceFeature>,
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
113#[serde(rename_all = "camelCase")]
114pub struct DeviceSnapshot {
115 pub microphone_permission: MicrophonePermission,
116 #[serde(default)]
117 pub devices: Vec<AudioBridgeDevice>,
118 #[serde(default)]
119 pub candidates: Vec<AudioBridgeCandidate>,
120}
121
122#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
123#[serde(rename_all = "camelCase")]
124pub enum SmartSpeakerRuntimeState {
125 Disabled,
126 Starting,
127 Listening,
128 PlaybackMuted,
129 Capturing,
130 Submitting,
131 WaitingForReply,
132 ReplyPlaying,
133 FollowUpListening,
134 InputOffline,
135 OutputOffline,
136 PermissionDenied,
137 ModelError,
138 RuntimeError,
139}
140
141#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
142#[serde(rename_all = "camelCase", deny_unknown_fields)]
143pub struct SmartSpeaker {
144 pub id: String,
145 pub display_name: String,
146 pub input_device_id: String,
147 pub output_device_id: String,
148 pub enabled: bool,
149 pub online: bool,
150 pub state: SmartSpeakerRuntimeState,
151 #[serde(default, skip_serializing_if = "Option::is_none")]
152 pub reason: Option<String>,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
156#[serde(rename_all = "camelCase", deny_unknown_fields)]
157pub struct SmartSpeakerSnapshot {
158 #[serde(default)]
159 pub smart_speakers: Vec<SmartSpeaker>,
160}
161
162#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
163#[serde(rename_all = "camelCase", deny_unknown_fields)]
164pub struct SmartSpeakerCreateRequest {
165 pub display_name: String,
166 pub input_device_id: String,
167 pub output_device_id: String,
168 #[serde(default = "default_true")]
169 pub enabled: bool,
170}
171
172#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
173#[serde(rename_all = "camelCase", deny_unknown_fields)]
174pub struct SmartSpeakerUpdateRequest {
175 pub id: String,
176 #[serde(default, skip_serializing_if = "Option::is_none")]
177 pub display_name: Option<String>,
178 #[serde(default, skip_serializing_if = "Option::is_none")]
179 pub input_device_id: Option<String>,
180 #[serde(default, skip_serializing_if = "Option::is_none")]
181 pub output_device_id: Option<String>,
182 #[serde(default, skip_serializing_if = "Option::is_none")]
183 pub enabled: Option<bool>,
184}
185
186#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
187#[serde(rename_all = "camelCase", deny_unknown_fields)]
188pub struct SmartSpeakerRemoveRequest {
189 pub id: String,
190}
191
192#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
193#[serde(rename_all = "camelCase", deny_unknown_fields)]
194pub struct SmartSpeakerMutationResponse {
195 pub changed: bool,
196 #[serde(default, skip_serializing_if = "Option::is_none")]
197 pub smart_speaker: Option<SmartSpeaker>,
198}
199
200pub use super::agent_gateway::ReplyRequest as AgentReplyRequest;
201
202const fn default_true() -> bool {
203 true
204}
205
206#[derive(Debug, Clone, Serialize, Deserialize)]
207#[serde(rename_all = "camelCase")]
208pub struct PairingResponse {
209 pub changed: bool,
210 pub device: AudioBridgeDevice,
211}
212
213#[cfg(test)]
214mod tests {
215 use super::*;
216
217 #[test]
218 fn manifest_and_rust_audiobridge_contract_agree() {
219 let manifest: serde_json::Value =
220 serde_json::from_str(include_str!("../../../manifest/core.v1.json")).unwrap();
221 let contract = &manifest["protocols"]["audioBridgePlugin"];
222 assert_eq!(contract["version"], VERSION);
223 assert_eq!(contract["nodeType"], NODE_TYPE);
224 assert_eq!(contract["appTargetPrefix"], APP_TARGET_PREFIX);
225 assert_eq!(contract["runtimeTargetPrefix"], RUNTIME_TARGET_PREFIX);
226 assert_eq!(contract["routes"]["deviceSnapshot"], DEVICE_SNAPSHOT);
227 assert_eq!(contract["routes"]["deviceRefresh"], DEVICE_REFRESH);
228 assert_eq!(contract["routes"]["devicePair"], DEVICE_PAIR);
229 assert_eq!(contract["routes"]["deviceUnpair"], DEVICE_UNPAIR);
230 assert_eq!(contract["routes"]["deviceSettingsGet"], DEVICE_SETTINGS_GET);
231 assert_eq!(
232 contract["routes"]["deviceSettingsUpdate"],
233 DEVICE_SETTINGS_UPDATE
234 );
235 assert_eq!(
236 contract["routes"]["smartSpeakerSnapshot"],
237 SMART_SPEAKER_SNAPSHOT
238 );
239 assert_eq!(
240 contract["routes"]["smartSpeakerCreate"],
241 SMART_SPEAKER_CREATE
242 );
243 assert_eq!(
244 contract["routes"]["smartSpeakerUpdate"],
245 SMART_SPEAKER_UPDATE
246 );
247 assert_eq!(
248 contract["routes"]["smartSpeakerRemove"],
249 SMART_SPEAKER_REMOVE
250 );
251 assert_eq!(contract["agentReplyTarget"], AGENT_REPLY_TARGET);
252 assert_eq!(
253 manifest["protocols"]["agentGateway"]["submitTarget"],
254 crate::node::contracts::agent_gateway::SUBMIT_TARGET
255 );
256 }
257
258 #[test]
259 fn settings_accept_only_supported_explicit_values() {
260 let valid = serde_json::json!({"deviceId":"mic-1","settings":{"listenEnabled":false}});
261 assert!(serde_json::from_value::<DeviceSettingsUpdateRequest>(valid).is_ok());
262 for settings in [
263 serde_json::json!({}),
264 serde_json::json!({"listenEnabled":null}),
265 serde_json::json!({"listenEnabled":"false"}),
266 serde_json::json!({"listenEnabled":true,"volume":10}),
267 ] {
268 assert!(serde_json::from_value::<DeviceSettingsUpdateRequest>(
269 serde_json::json!({"deviceId":"mic-1","settings":settings})
270 )
271 .is_err());
272 }
273 }
274
275 #[test]
276 fn public_contract_does_not_expose_platform_identity() {
277 let value = serde_json::to_value(AudioBridgeDevice {
278 device_id: "device-1".to_string(),
279 display_name: "Living Room Speaker".to_string(),
280 transport: "bluetooth".to_string(),
281 features: vec![AudioDeviceFeature::Speaker, AudioDeviceFeature::Microphone],
282 online: true,
283 settings: DeviceSettings {
284 listen_enabled: false,
285 },
286 listen_runtime: ListenRuntimeStatus {
287 state: ListenRuntimeState::Disabled,
288 reason: None,
289 },
290 })
291 .expect("serialize device");
292 assert!(value.get("platformKey").is_none());
293 assert!(value.get("endpointId").is_none());
294 assert_eq!(value["listenRuntime"]["state"], "disabled");
295 assert_eq!(
296 value["features"],
297 serde_json::json!(["speaker", "microphone"])
298 );
299 }
300
301 #[test]
302 fn runtime_failures_are_not_model_or_device_failures() {
303 assert_eq!(
304 serde_json::to_value(SmartSpeakerRuntimeState::RuntimeError).unwrap(),
305 "runtimeError"
306 );
307 let status = ListenRuntimeStatus {
308 state: ListenRuntimeState::Failed,
309 reason: Some("input_stream_unavailable".into()),
310 };
311 let value = serde_json::to_value(&status).unwrap();
312 assert_eq!(value["state"], "failed");
313 assert_eq!(
314 serde_json::from_value::<ListenRuntimeStatus>(value).unwrap(),
315 status
316 );
317 }
318
319 #[test]
320 fn device_snapshot_is_the_direct_response_payload() {
321 let value = serde_json::to_value(DeviceSnapshot {
322 microphone_permission: MicrophonePermission::NotDetermined,
323 devices: Vec::new(),
324 candidates: Vec::new(),
325 })
326 .expect("serialize device snapshot");
327 assert!(value.get("devices").is_some());
328 assert!(value.get("candidates").is_some());
329 assert!(value.get("snapshot").is_none());
330 }
331
332 #[test]
333 fn smart_speaker_contract_keeps_input_and_output_bindings_explicit() {
334 let value = serde_json::to_value(SmartSpeakerCreateRequest {
335 display_name: "Kitchen".to_string(),
336 input_device_id: "microphone-1".to_string(),
337 output_device_id: "speaker-1".to_string(),
338 enabled: true,
339 })
340 .expect("serialize smart speaker request");
341 assert_eq!(value["inputDeviceId"], "microphone-1");
342 assert_eq!(value["outputDeviceId"], "speaker-1");
343 assert_eq!(value["enabled"], true);
344 }
345}