adk-surface 3.0.0

Agent surfaces for ADK-Rust: one engine that turns a kernel's sessions, decisions and activity into plain words for every surface, and the surfaces that show them
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
//! Decisions on calls a voice conversation holds go back to that conversation (spec 3.3 §5.6,
//! §7.3).
//!
//! A call the realtime session holds is in the session store like any other held call, so the
//! source's own resume — a turn of the harness on its text runner — would run it there too.
//! [`VoiceSource`] sends the decision of a call held by a live voice conversation to that
//! conversation instead, which resumes it on its realtime session, once.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use adk_core::{ApprovalDecision, ApprovalRequest, Event};
use adk_harness::HarnessMode;
use adk_kernel::QuietHours;
use adk_kernel::proc::LedgerEntry;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use tokio::sync::Notify;

use crate::error::Result;
use crate::origin::Caller;
use crate::source::{AgentState, AlwaysAllowOffer, RuleCommit, Source, SpendToday};

/// The voice conversations open in this process, by user and session: where a decision on
/// one of their held calls is sent (spec 3.3 §5.6).
///
/// Cheap to clone; every clone sees the same conversations.
#[derive(Clone, Default)]
pub struct LiveVoice {
    sessions: Arc<Mutex<Conversations>>,
}

/// The signal that wakes each live conversation, by user and session.
type Conversations = HashMap<(String, String), Arc<Notify>>;

impl std::fmt::Debug for LiveVoice {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let open = self.sessions.lock().unwrap_or_else(|e| e.into_inner()).len();
        f.debug_struct("LiveVoice").field("open", &open).finish()
    }
}

impl LiveVoice {
    /// No conversation open.
    pub fn new() -> Self {
        Self::default()
    }

    /// Whether `user`'s session `session` is a live voice conversation.
    pub fn is_live(&self, user: &str, session: &str) -> bool {
        self.sessions
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .contains_key(&(user.to_string(), session.to_string()))
    }

    /// Registers a live conversation; it is woken through the returned signal until the
    /// guard drops.
    pub(crate) fn register(&self, user: &str, session: &str) -> (Registration, Arc<Notify>) {
        let wake = Arc::new(Notify::new());
        let key = (user.to_string(), session.to_string());
        self.sessions.lock().unwrap_or_else(|e| e.into_inner()).insert(key.clone(), wake.clone());
        (Registration { live: self.clone(), key, wake: wake.clone() }, wake)
    }

    /// Wakes the live conversation of `user`'s session `session`; `false` when there is none.
    fn wake(&self, user: &str, session: &str) -> bool {
        let wake = self
            .sessions
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .get(&(user.to_string(), session.to_string()))
            .cloned();
        match wake {
            Some(wake) => {
                wake.notify_one();
                true
            }
            None => false,
        }
    }
}

/// A live conversation's place in [`LiveVoice`], removed when it drops.
pub(crate) struct Registration {
    live: LiveVoice,
    key: (String, String),
    wake: Arc<Notify>,
}

impl Drop for Registration {
    fn drop(&mut self) {
        let mut sessions = self.live.sessions.lock().unwrap_or_else(|e| e.into_inner());
        // A later conversation on the same session registered its own signal: keep it.
        if sessions.get(&self.key).is_some_and(|wake| Arc::ptr_eq(wake, &self.wake)) {
            sessions.remove(&self.key);
        }
    }
}

/// The engine's source with voice (spec 3.3 §5.6): it delegates everything to the source it
/// wraps, except that a decision resolving a call held by a live voice conversation goes to
/// that conversation, which resumes the call on its realtime session. The wrapped source
/// never resumes it on the text runner, so the call runs once.
///
/// A decision made outside the engine — in Telegram through the kernel, or with
/// `adk-rust approvals` — never reaches [`Source::resolved`]; a conversation with held calls
/// also looks for those on every change the source signals.
///
/// # Example
///
/// ```rust
/// use std::sync::Arc;
/// use adk_core::{InMemoryApprovalStore, StoreApprovalService};
/// use adk_session::InMemorySessionService;
/// use adk_surface::voice::VoiceSource;
/// use adk_surface::{LocalSource, SurfaceEngine};
///
/// let local = LocalSource::builder("adk-rust")
///     .sessions(Arc::new(InMemorySessionService::new()))
///     .approvals(Arc::new(StoreApprovalService::new(Arc::new(InMemoryApprovalStore::new()))))
///     .build()
///     .unwrap();
/// let source = VoiceSource::new(Arc::new(local));
/// let live = source.live();
/// let engine = SurfaceEngine::builder(Arc::new(source)).build();
/// # let _ = (engine, live);
/// ```
pub struct VoiceSource {
    inner: Arc<dyn Source>,
    live: LiveVoice,
}

impl std::fmt::Debug for VoiceSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("VoiceSource")
            .field("app", &self.inner.app_name())
            .field("live", &self.live)
            .finish()
    }
}

impl VoiceSource {
    /// Wraps `inner`.
    pub fn new(inner: Arc<dyn Source>) -> Self {
        Self { inner, live: LiveVoice::new() }
    }

    /// The live conversations this source routes decisions to; give it to the
    /// [`VoiceHub`](super::VoiceHub).
    pub fn live(&self) -> LiveVoice {
        self.live.clone()
    }

    /// The source it wraps.
    pub fn inner(&self) -> &Arc<dyn Source> {
        &self.inner
    }
}

#[async_trait]
impl Source for VoiceSource {
    fn app_name(&self) -> &str {
        self.inner.app_name()
    }

    async fn transcript(&self, user: &str, session: &str) -> Result<Option<Vec<Event>>> {
        self.inner.transcript(user, session).await
    }

    async fn changed(&self, max: Duration) {
        self.inner.changed(max).await;
    }

    async fn requests(
        &self,
        caller: &Caller,
        session: Option<&str>,
        open_only: bool,
    ) -> Result<Vec<ApprovalRequest>> {
        self.inner.requests(caller, session, open_only).await
    }

    async fn request(&self, id: &str) -> Result<Option<ApprovalRequest>> {
        self.inner.request(id).await
    }

    async fn decide(
        &self,
        id: &str,
        expected_version: u64,
        decision: ApprovalDecision,
    ) -> Result<ApprovalRequest> {
        self.inner.decide(id, expected_version, decision).await
    }

    async fn resolved(&self, request: &ApprovalRequest) -> Result<()> {
        if self.live.wake(&request.scope.user_id, &request.scope.session_id) {
            return Ok(());
        }
        self.inner.resolved(request).await
    }

    async fn resolved_by(&self, caller: &Caller, request: &ApprovalRequest) -> Result<()> {
        if self.live.wake(&request.scope.user_id, &request.scope.session_id) {
            return Ok(());
        }
        self.inner.resolved_by(caller, request).await
    }

    async fn always_allow_offer(&self, request: &ApprovalRequest) -> Option<AlwaysAllowOffer> {
        self.inner.always_allow_offer(request).await
    }

    async fn prepare_always_allow(
        &self,
        caller: &Caller,
        request: &ApprovalRequest,
    ) -> Result<Box<dyn RuleCommit>> {
        self.inner.prepare_always_allow(caller, request).await
    }

    async fn agents(&self, user: &str) -> Result<Vec<AgentState>> {
        self.inner.agents(user).await
    }

    fn primary_agent(&self) -> Option<String> {
        self.inner.primary_agent()
    }

    fn rates_high(&self, request: &ApprovalRequest) -> bool {
        self.inner.rates_high(request)
    }

    fn moves_money(&self, request: &ApprovalRequest) -> bool {
        self.inner.moves_money(request)
    }

    async fn busy(&self, user: &str, session: &str) -> Result<bool> {
        self.inner.busy(user, session).await
    }

    async fn turn_failures(
        &self,
        user: &str,
        session: &str,
    ) -> Result<Vec<crate::source::TurnFailure>> {
        self.inner.turn_failures(user, session).await
    }

    async fn waiting_on_decision(&self, user: &str, session: &str) -> Result<bool> {
        self.inner.waiting_on_decision(user, session).await
    }

    async fn start_turn(&self, caller: &Caller, session: &str, text: &str) -> Result<()> {
        self.inner.start_turn(caller, session, text).await
    }

    async fn request_call(
        &self,
        caller: &Caller,
        session: &str,
        text: &str,
        call: adk_core::RequestedCall,
    ) -> Result<()> {
        self.inner.request_call(caller, session, text, call).await
    }

    async fn stop_all(&self, caller: &Caller, reason: &str) -> Result<()> {
        self.inner.stop_all(caller, reason).await
    }

    async fn resume_all(&self, caller: &Caller) -> Result<()> {
        self.inner.resume_all(caller).await
    }

    async fn stopped(&self) -> Result<bool> {
        self.inner.stopped().await
    }

    async fn mode(&self, user: &str, session: Option<&str>) -> Result<Option<HarnessMode>> {
        self.inner.mode(user, session).await
    }

    async fn set_mode(&self, caller: &Caller, session: &str, mode: HarnessMode) -> Result<()> {
        self.inner.set_mode(caller, session, mode).await
    }

    async fn spend(&self, user: &str) -> Result<Option<SpendToday>> {
        self.inner.spend(user).await
    }

    fn quiet_hours(&self) -> Option<QuietHours> {
        self.inner.quiet_hours()
    }

    fn serves_kernel_routes(&self) -> bool {
        self.inner.serves_kernel_routes()
    }

    fn keeps_ledger(&self) -> bool {
        self.inner.keeps_ledger()
    }

    async fn ledger(&self, since: DateTime<Utc>) -> Result<Vec<LedgerEntry>> {
        self.inner.ledger(since).await
    }
}

#[cfg(test)]
mod tests {
    use adk_core::{ApprovalPolicy, ApprovalScope, ApproverSelector, ToolConfirmationRequest};

    use super::*;
    use crate::error::SurfaceError;
    use crate::origin::{Origin, SurfaceKind};

    /// A source that records which sessions it was asked to resume.
    #[derive(Default)]
    struct Recording {
        resumed: Mutex<Vec<String>>,
    }

    #[async_trait]
    impl Source for Recording {
        fn app_name(&self) -> &str {
            "home"
        }

        async fn transcript(&self, _user: &str, _session: &str) -> Result<Option<Vec<Event>>> {
            Ok(None)
        }

        async fn requests(
            &self,
            _caller: &Caller,
            _session: Option<&str>,
            _open_only: bool,
        ) -> Result<Vec<ApprovalRequest>> {
            Ok(Vec::new())
        }

        async fn request(&self, _id: &str) -> Result<Option<ApprovalRequest>> {
            Ok(None)
        }

        async fn decide(
            &self,
            _id: &str,
            _expected_version: u64,
            _decision: ApprovalDecision,
        ) -> Result<ApprovalRequest> {
            Err(SurfaceError::unsupported("decide"))
        }

        async fn resolved(&self, request: &ApprovalRequest) -> Result<()> {
            self.resumed.lock().unwrap().push(request.scope.session_id.clone());
            Ok(())
        }

        /// Rates `pay_invoice` as a mounted driver's `financial` tool.
        fn rates_high(&self, request: &ApprovalRequest) -> bool {
            request.tool_name == "pay_invoice"
        }

        fn moves_money(&self, request: &ApprovalRequest) -> bool {
            request.tool_name == "pay_invoice"
        }
    }

    fn decided(session: &str) -> ApprovalRequest {
        let call = ToolConfirmationRequest {
            tool_name: "bash".into(),
            function_call_id: Some("call-1".into()),
            args: serde_json::json!({ "command": "ls" }),
            origin: None,
            acting: None,
            evidence: Vec::new(),
        };
        ApprovalRequest::new(
            format!("apr-{session}"),
            ApprovalScope::new("home", "ana", session),
            &call,
            ApprovalPolicy::new([ApproverSelector::group("owner")]),
        )
    }

    /// The wrapped source's rating of a call reaches the engine through the voice source, so a
    /// driver's `financial` call stays high risk and money with voice on (spec 3.3 §7.2, spec
    /// 3.2 K-I9).
    #[test]
    fn the_wrapped_source_rates_each_call() {
        let source = VoiceSource::new(Arc::new(Recording::default()));
        let mut pay = decided("typed");
        pay.tool_name = "pay_invoice".into();
        assert!(source.rates_high(&pay) && source.moves_money(&pay));
        let list = decided("typed");
        assert!(!source.rates_high(&list) && !source.moves_money(&list));
    }

    /// A decision on a live voice conversation's call wakes that conversation, never the
    /// wrapped source; any other session is the wrapped source's to resume.
    #[tokio::test]
    async fn a_live_conversation_is_woken_and_every_other_session_delegated() {
        let recording = Arc::new(Recording::default());
        let source = VoiceSource::new(recording.clone());
        let (registration, wake) = source.live().register("ana", "voice-1");
        let ana = Caller::human("ana", SurfaceKind::Web, Origin::Pointer);

        source.resolved_by(&ana, &decided("voice-1")).await.unwrap();
        source.resolved(&decided("voice-1")).await.unwrap();
        assert!(recording.resumed.lock().unwrap().is_empty());
        tokio::time::timeout(Duration::from_secs(1), wake.notified()).await.unwrap();

        source.resolved_by(&ana, &decided("typed")).await.unwrap();
        assert_eq!(*recording.resumed.lock().unwrap(), ["typed"]);

        // Once the conversation ends, its session is the wrapped source's again.
        drop(registration);
        assert!(!source.live().is_live("ana", "voice-1"));
        source.resolved(&decided("voice-1")).await.unwrap();
        assert_eq!(*recording.resumed.lock().unwrap(), ["typed", "voice-1"]);
    }
}