adk_surface/voice/mod.rs
1//! Voice and video (spec 3.3 §5.6; feature `voice`).
2//!
3//! A spoken answer is one more way to give the answer a click or a key gives, under the same
4//! rules: the person says the label of the engine's action — "allow once", "deny", "cancel",
5//! "stop all" (`spoken`) — and the engine decides it, never the model and never the realtime
6//! session. What voice may decide is the voice row of §7.2: **Allow once** for everyday calls,
7//! after a cancel window, and **Deny** for any call. A high-risk or money call needs the
8//! person to confirm it is them, which voice cannot do, so the view says "Decide on screen".
9//!
10//! | Part | What | Feature |
11//! |---|---|---|
12//! | `spoken` | The words that answer, and nothing else | `voice` |
13//! | `VisualInputs` | The camera and the screen reach the model only while their switches are on, no faster than it takes them | `voice` |
14//! | `VoiceSource` | Sends a decision on a call a live voice conversation holds back to that conversation, never to the text runner | `voice` |
15//! | `VoiceHub` | Opens voice conversations (`VoiceLink`): one realtime session of the harness each, in its own session | `voice` |
16//! | `router`, `socket_routes` | `GET /surface/voice`, `POST /surface/voice/sessions` and the voice socket | `voice` and `web` |
17//! | The wire types ([`VoiceModels`], [`VoiceTicket`], [`VoiceUpdate`], ...) | What the view and the server say, in the OpenAPI document of every `web` build | `web` or `voice` |
18//!
19//! # How a spoken answer binds to a decision
20//!
21//! One card is prompted at a time: the conversation's own call just held, else the oldest
22//! open decision voice may answer (`VoiceHub::open_in_thread`: only its own). A spoken
23//! answer applies to the card that was prompted when the person started speaking, and only
24//! when it had been on screen for a second by then; transcripts are matched to utterances in
25//! order, so one that arrives late keeps its own card. If the card or its version changed
26//! before the transcript arrived, nothing is decided. Only the person's own completed
27//! transcript counts, never the model's words, and speech that started while the model's
28//! audio was playing is ignored — after a barge-in, until the transport says it flushed that
29//! audio. Another agent's decision is shown on screen and never sent to the model. **Stop
30//! all** ends the conversation, whose calls check the harness runner's kill switch.
31//!
32//! # Records
33//!
34//! Every `surface.decide` record of a spoken answer carries `transcriptDigest`, the SHA-256 of
35//! the transcript, and none holds audio, frames or the transcript's text (§5.6: "a note of the
36//! answer, never the recording"). The conversation itself is the session, as typed text is:
37//! the person's utterances stay in it as their messages.
38
39#[cfg(feature = "voice")]
40mod hub;
41#[cfg(feature = "voice")]
42mod session;
43#[cfg(feature = "voice")]
44mod source;
45#[cfg(feature = "voice")]
46pub mod spoken;
47#[cfg(feature = "voice")]
48mod visual;
49#[cfg(all(feature = "voice", feature = "web"))]
50mod web;
51mod wire;
52
53#[cfg(feature = "voice")]
54pub use hub::{VoiceHub, VoiceHubBuilder};
55#[cfg(feature = "voice")]
56pub use session::{VoiceInput, VoiceLink, VoiceOutput};
57#[cfg(feature = "voice")]
58pub use source::{LiveVoice, VoiceSource};
59#[cfg(feature = "voice")]
60pub use visual::{FramePolicy, VisualInputs};
61#[cfg(all(feature = "voice", feature = "web"))]
62pub use web::{SOCKET_PATH, TICKET_TTL, router, socket_routes};
63pub use wire::{
64 FRAME_AUDIO, FRAME_CAMERA, FRAME_SCREEN, InputSource, VisualSource, VoiceClientMessage,
65 VoiceModel, VoiceModels, VoiceTicket, VoiceTicketRequest, VoiceUpdate,
66};
67
68/// The route paths of voice, relative to the `/api` prefix, and the socket's root path
69/// (spec 3.3 §5.6).
70pub mod paths {
71 /// `GET`: the models the person can talk on.
72 pub const VOICE: &str = "/surface/voice";
73 /// `POST`: a ticket for the voice socket.
74 pub const SESSIONS: &str = "/surface/voice/sessions";
75 /// The voice socket, a root route.
76 pub const SOCKET: &str = "/voice/socket";
77}
78
79#[cfg(feature = "voice")]
80pub use error::VoiceError;
81
82/// What [`SurfaceEngine::decide_spoken`](crate::SurfaceEngine::decide_spoken) did with a
83/// spoken answer (spec 3.3 §5.6).
84#[cfg(feature = "voice")]
85#[derive(Debug)]
86pub struct SpokenAnswer {
87 /// What the answer did now: [`Pending`](crate::CommandOutcome::Pending) for "allow once"
88 /// inside its cancel window, otherwise the answer's outcome.
89 pub outcome: crate::CommandOutcome,
90 /// For a pending answer, how its cancel window ended: the outcome of recording it, or its
91 /// error. The sender is dropped, without a value, when the answer was cancelled — by
92 /// **Cancel**, by ending the conversation or by **Stop all**.
93 pub settled: Option<tokio::sync::oneshot::Receiver<crate::Result<crate::CommandOutcome>>>,
94}
95
96#[cfg(feature = "voice")]
97mod error {
98 use adk_core::{AdkError, ErrorCategory, ErrorComponent};
99
100 use crate::error::SurfaceError;
101
102 /// Why voice refused or failed (spec 3.3 §5.6). Each converts into a [`SurfaceError`] with
103 /// its `surface.voice.*` code.
104 #[derive(Debug, thiserror::Error)]
105 pub enum VoiceError {
106 /// No model of that id is offered.
107 #[error("no voice model '{0}' is offered here; pick one GET /api/surface/voice lists")]
108 UnknownModel(String),
109 /// The session named is not one of the caller's voice conversations (S-I8).
110 #[error(
111 "session '{0}' is not one of your voice conversations; start a new one without a sessionId"
112 )]
113 NotYourSession(String),
114 /// The ticket is unknown, used, expired or another caller's.
115 #[error(
116 "the voice ticket is not valid: ask POST /api/surface/voice/sessions for a new one"
117 )]
118 Ticket,
119 /// The realtime provider could not be reached or failed.
120 #[error("the voice model failed: {0}")]
121 Provider(String),
122 /// The harness could not open its voice session.
123 #[error("the voice conversation could not start: {0}")]
124 Harness(String),
125 /// The conversation already ended.
126 #[error("the voice conversation has ended; start a new one")]
127 Ended,
128 }
129
130 impl VoiceError {
131 /// The error code: `surface.voice.unknown_model`, `surface.voice.not_your_session`,
132 /// `surface.voice.ticket`, `surface.voice.provider`, `surface.voice.harness` or
133 /// `surface.voice.ended`.
134 pub fn code(&self) -> &'static str {
135 match self {
136 Self::UnknownModel(_) => "surface.voice.unknown_model",
137 Self::NotYourSession(_) => "surface.voice.not_your_session",
138 Self::Ticket => "surface.voice.ticket",
139 Self::Provider(_) => "surface.voice.provider",
140 Self::Harness(_) => "surface.voice.harness",
141 Self::Ended => "surface.voice.ended",
142 }
143 }
144
145 fn category(&self) -> ErrorCategory {
146 match self {
147 Self::UnknownModel(_) => ErrorCategory::InvalidInput,
148 Self::NotYourSession(_) => ErrorCategory::Forbidden,
149 Self::Ticket => ErrorCategory::Unauthorized,
150 Self::Provider(_) => ErrorCategory::Unavailable,
151 Self::Harness(_) => ErrorCategory::Internal,
152 Self::Ended => ErrorCategory::NotFound,
153 }
154 }
155 }
156
157 impl From<VoiceError> for SurfaceError {
158 fn from(error: VoiceError) -> Self {
159 SurfaceError::Source(AdkError::new(
160 ErrorComponent::Server,
161 error.category(),
162 error.code(),
163 error.to_string(),
164 ))
165 }
166 }
167}