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
//! The AG-UI extension point (spec 3.3 §6.1 `genui`, §10 S2): what a host adds to the
//! protocol-native AG-UI stream without this crate knowing what it adds.
//!
//! A host sets one [`AgUiExtension`] with [`RuntimeController::with_ag_ui_extension`] or
//! `ServerBuilder::with_ag_ui_extension`. On a protocol-native `ag_ui` run it:
//!
//! | Hook | When |
//! |---|---|
//! | [`events_for`](AgUiExtension::events_for) | After each ADK event's own AG-UI events: extra events, such as `ACTIVITY_SNAPSHOT` |
//! | [`keeps_activity`](AgUiExtension::keeps_activity) | For each `activity` message a client sends back: whether it reaches the stream, the session state and the messages snapshot |
//! | [`action_in`](AgUiExtension::action_in), [`requested_call`](AgUiExtension::requested_call) | When the run's `forwardedProps` carry an action: the call a person asked for, which the run's agent holds for their decision, so the run ends with a `tool_call` interrupt |
//! | [`features`](AgUiExtension::features) | Added to the `ag_ui` features of `GET /api/ui/capabilities` |
//!
//! [`RuntimeController::with_ag_ui_extension`]: super::RuntimeController::with_ag_ui_extension
use ;
use async_trait;
use Value;
use AgUiInputMessage;
/// A run's forwarded action, as an [`AgUiExtension`] reads it (spec 3.3 §7.4 rule 5).
/// What a host adds to protocol-native AG-UI runs (spec 3.3 §6.1 `genui`, §10 S2). Every
/// hook has a default that adds nothing, so a run without an extension, or with one that
/// overrides nothing, is unchanged.
///
/// # Example
///
/// ```rust
/// use adk_server::rest::controllers::ag_ui_extension::AgUiExtension;
/// use serde_json::{Value, json};
///
/// /// Says "hello" in an activity after every event.
/// struct Hello;
///
/// impl AgUiExtension for Hello {
/// fn features(&self) -> Vec<&'static str> {
/// vec!["hello_activity"]
/// }
///
/// fn events_for(&self, event: &adk_core::Event) -> Vec<Value> {
/// vec![json!({
/// "type": "ACTIVITY_SNAPSHOT",
/// "messageId": format!("hello-{}", event.id),
/// "activityType": "hello",
/// "content": {},
/// })]
/// }
/// }
///
/// assert_eq!(Hello.events_for(&adk_core::Event::new("inv-1"))[0]["activityType"], "hello");
/// ```