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