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
use Arc;
use crate::;
use async_trait;
/// Starts an agent turn for a session, creating the session when it does not exist.
///
/// Callers that hand work to an agent from outside a conversation — a background trigger, a
/// queue consumer, a scheduler — need one operation: "run this content through the agent and
/// give me the events." They should not have to know which session service holds the session, or
/// that a session must be registered before a turn can start.
///
/// `adk-runner` implements this for `Runner`, so a caller can accept `Arc<dyn AgentInvoker>` and
/// stay independent of the runner's construction. Implementations are responsible for creating a
/// missing session rather than failing, because an external event has no opportunity to register
/// one first. Implementations that permit concurrent calls should also serialize turns targeting
/// the same session until the returned event stream completes or is dropped.
///
/// # Example
///
/// ```rust,ignore
/// use std::sync::Arc;
/// use adk_core::{AgentInvoker, Content};
///
/// async fn on_event(invoker: Arc<dyn AgentInvoker>) -> adk_core::Result<()> {
/// let mut events = invoker
/// .invoke("system", "nightly-sweep", Content::new("user").with_text("run the sweep"))
/// .await?;
/// while let Some(event) = futures::StreamExt::next(&mut events).await {
/// let _ = event?;
/// }
/// Ok(())
/// }
/// ```