adk_surface/voice/wire.rs
1//! What the voice view and the server say to each other (spec 3.3 §5.6, §6).
2//!
3//! These types are plain serde and JSON Schema types with no realtime dependency, so they
4//! compile under `web` with the rest of the OpenAPI document, whether or not `voice` is on:
5//! the document is the same in every build. The routes that use them are served only with
6//! `voice`.
7//!
8//! The socket carries the person's microphone, camera and screen up and the model's audio down
9//! as binary frames, each with a one-byte tag ([`FRAME_AUDIO`], [`FRAME_CAMERA`],
10//! [`FRAME_SCREEN`]), and transient voice state as camelCase JSON text frames:
11//! [`VoiceClientMessage`] up, [`VoiceUpdate`] down. Decision cards never travel on it: the view
12//! reads them from `GET /surface/decisions?surface=voice` and the session feed (§6).
13
14use chrono::{DateTime, Utc};
15use schemars::JsonSchema;
16use serde::{Deserialize, Serialize};
17
18/// Tag of a binary frame of PCM16 mono audio: up at the model's input rate, down at its
19/// output rate.
20pub const FRAME_AUDIO: u8 = 0x01;
21/// Tag of a binary frame holding one camera JPEG.
22pub const FRAME_CAMERA: u8 = 0x02;
23/// Tag of a binary frame holding one screen JPEG.
24pub const FRAME_SCREEN: u8 = 0x03;
25
26/// A realtime model the person can talk on (spec 3.3 §5.6).
27#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
28#[serde(rename_all = "camelCase")]
29pub struct VoiceModel {
30 /// What a session asks for: `POST /surface/voice/sessions {model}`.
31 pub id: String,
32 /// The name the select shows, such as "Gemini Live".
33 pub name: String,
34 /// The sample rate of the PCM16 mono audio the model takes.
35 pub input_rate: u32,
36 /// The sample rate of the PCM16 mono audio the model speaks.
37 pub output_rate: u32,
38}
39
40/// `GET /surface/voice` answer: the models the person can talk on, first the default
41/// (spec 3.3 §5.6).
42#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
43#[serde(rename_all = "camelCase")]
44pub struct VoiceModels {
45 /// The agent the person talks with, by its display name: "Talking with Chief of staff".
46 pub agent: String,
47 /// The agent's name, as `GET /surface/agents` lists it: a conversation opens in Threads
48 /// under it when it ends.
49 pub agent_name: String,
50 /// The models, the default first.
51 pub models: Vec<VoiceModel>,
52}
53
54/// `POST /surface/voice/sessions` body: a ticket for the voice socket (spec 3.3 §5.6).
55#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
56#[serde(rename_all = "camelCase")]
57pub struct VoiceTicketRequest {
58 /// The model's `id`.
59 pub model: String,
60 /// Continue this voice conversation instead of starting one: only one of the caller's own
61 /// voice sessions (§11 S-I8).
62 #[serde(default, skip_serializing_if = "Option::is_none")]
63 pub session_id: Option<String>,
64}
65
66/// `POST /surface/voice/sessions` answer (spec 3.3 §5.6).
67///
68/// The ticket works once, within 30 seconds, for the caller it was issued to; the view sends
69/// it in its first message on the socket, never in a URL.
70#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
71#[serde(rename_all = "camelCase")]
72pub struct VoiceTicket {
73 /// The single-use ticket.
74 pub ticket: String,
75 /// The socket's path: `/voice/socket`.
76 pub socket: String,
77 /// The voice conversation's session, listed in Threads under the agent.
78 pub session_id: String,
79 /// The sample rate of the audio the view sends up.
80 pub input_rate: u32,
81 /// The sample rate of the audio the view plays.
82 pub output_rate: u32,
83}
84
85/// The camera or the screen.
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
87#[serde(rename_all = "camelCase")]
88pub enum VisualSource {
89 /// The camera.
90 Camera,
91 /// A shared screen or window.
92 Screen,
93}
94
95/// One of the three switches of "What it can see" (spec 3.3 §5.6).
96#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
97#[serde(rename_all = "camelCase")]
98pub enum InputSource {
99 /// The microphone.
100 Mic,
101 /// The camera.
102 Camera,
103 /// A shared screen or window.
104 Screen,
105}
106
107/// A text frame the view sends up the voice socket (spec 3.3 §5.6).
108///
109/// # Example
110///
111/// ```rust
112/// use adk_surface::voice::{InputSource, VoiceClientMessage};
113///
114/// let message: VoiceClientMessage =
115/// serde_json::from_str(r#"{"type":"switch","source":"screen","on":false}"#).unwrap();
116/// assert_eq!(message, VoiceClientMessage::Switch { source: InputSource::Screen, on: false });
117/// ```
118#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
119#[serde(tag = "type", rename_all = "camelCase")]
120pub enum VoiceClientMessage {
121 /// The first message: the ticket `POST /surface/voice/sessions` issued.
122 #[serde(rename_all = "camelCase")]
123 Hello {
124 /// The ticket.
125 ticket: String,
126 },
127 /// A switch of "What it can see" changed; sent before any later frame.
128 #[serde(rename_all = "camelCase")]
129 Switch {
130 /// Which.
131 source: InputSource,
132 /// On or off.
133 on: bool,
134 },
135 /// The model's audio finished playing, or was flushed.
136 PlaybackEnded,
137 /// End the conversation: a spoken answer in its cancel window is cancelled.
138 End,
139 /// **Cancel** on a spoken screen action in its cancel window (spec 3.3 §5.6).
140 #[serde(rename_all = "camelCase")]
141 CancelAction {
142 /// The action, as [`VoiceUpdate::PendingAction`] named it.
143 action_id: String,
144 },
145}
146
147/// A text frame the server sends down the voice socket: transient voice state only
148/// (spec 3.3 §5.6).
149///
150/// # Example
151///
152/// ```rust
153/// use adk_surface::voice::VoiceUpdate;
154///
155/// let update = VoiceUpdate::Heard { text: "allow once".into() };
156/// assert_eq!(serde_json::to_value(&update).unwrap(), serde_json::json!({ "type": "heard", "text": "allow once" }));
157/// ```
158#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
159#[serde(tag = "type", rename_all = "camelCase")]
160pub enum VoiceUpdate {
161 /// The provider session is open.
162 #[serde(rename_all = "camelCase")]
163 Ready {
164 /// The voice conversation's session.
165 session_id: String,
166 /// The sample rate of the audio the view sends up.
167 input_rate: u32,
168 /// The sample rate of the audio the view plays.
169 output_rate: u32,
170 },
171 /// The model waits for the person.
172 Listening,
173 /// The model speaks.
174 Speaking,
175 /// What the person said, as transcribed.
176 #[serde(rename_all = "camelCase")]
177 Heard {
178 /// The transcript.
179 text: String,
180 },
181 /// The decision a spoken answer would apply to now, or none.
182 #[serde(rename_all = "camelCase")]
183 Prompted {
184 /// The card's `id`.
185 #[serde(default, skip_serializing_if = "Option::is_none")]
186 decision_id: Option<String>,
187 /// The card's `version`.
188 #[serde(default, skip_serializing_if = "Option::is_none")]
189 version: Option<u64>,
190 },
191 /// A spoken "allow once" takes effect at `cancelUntil` unless cancelled.
192 #[serde(rename_all = "camelCase")]
193 Pending {
194 /// The decision.
195 decision_id: String,
196 /// When it takes effect.
197 cancel_until: DateTime<Utc>,
198 /// "Allowing once in 5 seconds".
199 text: String,
200 },
201 /// A spoken screen action runs at `cancelUntil` unless cancelled (spec 3.3 §5.6): "Snoozing
202 /// in 5 s", under the heading "Move News digest to 19:00".
203 #[serde(rename_all = "camelCase")]
204 PendingAction {
205 /// The action, for [`VoiceClientMessage::CancelAction`].
206 action_id: String,
207 /// When it runs.
208 cancel_until: DateTime<Utc>,
209 /// "Snoozing in 5 s".
210 text: String,
211 /// What it will do, in the kernel's words: "Move News digest to 19:00".
212 heading: String,
213 },
214 /// A spoken screen action ended, or was not taken: done, cancelled, refused, or words
215 /// that match several things (spec 3.3 §5.6).
216 #[serde(rename_all = "camelCase")]
217 ActionOutcome {
218 /// The action, when one was pending.
219 #[serde(default, skip_serializing_if = "Option::is_none")]
220 action_id: Option<String>,
221 /// It ran.
222 done: bool,
223 /// What happened, in plain words: "Cancelled · nothing changed".
224 text: String,
225 /// What it did or would have done: "Moved News digest to 19:00".
226 #[serde(default, skip_serializing_if = "Option::is_none")]
227 heading: Option<String>,
228 },
229 /// A spoken answer ended: read the card again for how.
230 #[serde(rename_all = "camelCase")]
231 Outcome {
232 /// The decision.
233 decision_id: String,
234 /// The answer was recorded.
235 decided: bool,
236 /// What happened, in plain words, when it was not recorded.
237 #[serde(default, skip_serializing_if = "Option::is_none")]
238 text: Option<String>,
239 },
240 /// The open decisions voice cannot approve: "Decide on screen".
241 #[serde(rename_all = "camelCase")]
242 NeedsScreen {
243 /// The cards' `id`s, oldest first.
244 decision_ids: Vec<String>,
245 },
246 /// The person spoke over the model: flush the audio not yet played.
247 BargeIn,
248 /// The conversation ended.
249 Ended,
250 /// Something failed or was refused.
251 #[serde(rename_all = "camelCase")]
252 Error {
253 /// The engine's `surface.*` code, or the source's.
254 code: String,
255 /// What happened, in plain words.
256 message: String,
257 },
258}