Skip to main content

adk_agent/ambient/
event_source.rs

1use adk_core::Result;
2use async_trait::async_trait;
3use futures::stream::BoxStream;
4use serde_json::Value;
5
6/// A trigger event delivered by an [`EventSource`].
7///
8/// Contains the name of the source that produced it and an arbitrary JSON payload
9/// with source-specific metadata (e.g., matched file path, cron tick time, webhook body).
10///
11/// It carries no idempotency key: a consumer that deduplicates derives one from the payload,
12/// as the `adk-kernel` wake sources do (spec 3.2 §7.4, §13 Q7). The struct is
13/// `#[non_exhaustive]`; build it with [`new`](Self::new) and
14/// [`with_principal`](Self::with_principal).
15///
16/// # Example
17///
18/// ```rust
19/// use adk_agent::ambient::TriggerEvent;
20///
21/// let event = TriggerEvent::new("hook", serde_json::json!({"id": 7})).with_principal("partner");
22/// assert_eq!(event.principal.as_deref(), Some("partner"));
23/// assert_eq!(TriggerEvent::new("cron", serde_json::Value::Null).principal, None);
24/// ```
25#[derive(Debug, Clone)]
26#[non_exhaustive]
27pub struct TriggerEvent {
28    /// Human-readable name of the source that produced this event.
29    pub source: String,
30    /// Source-specific metadata.
31    pub payload: Value,
32    /// The verified principal this event is attributed to, when the source authenticates.
33    ///
34    /// `None` for sources with no notion of a caller — a cron tick or a file change. For
35    /// [`WebhookTrigger`](super::WebhookTrigger) this is whatever its verifier returned, so
36    /// a handler can tell an authorized trigger from an anonymous one instead of treating
37    /// every event as equally trusted.
38    pub principal: Option<String>,
39}
40
41impl TriggerEvent {
42    /// An event from `source` with `payload` and no principal.
43    pub fn new(source: impl Into<String>, payload: Value) -> Self {
44        Self { source: source.into(), payload, principal: None }
45    }
46
47    /// Attributes the event to `principal`, a subject the source verified.
48    #[must_use]
49    pub fn with_principal(mut self, principal: impl Into<String>) -> Self {
50        self.principal = Some(principal.into());
51        self
52    }
53}
54
55/// A source of trigger events for ambient agents.
56///
57/// Implementations produce an async stream of [`TriggerEvent`]s. The stream yields
58/// events until the source is stopped or the subscription is dropped.
59///
60/// # Built-in Implementations
61///
62/// - [`CronTrigger`](super::CronTrigger) — cron schedule
63/// - [`WebhookTrigger`](super::WebhookTrigger) — HTTP POST webhook
64/// - [`FileWatchTrigger`](super::FileWatchTrigger) — filesystem watcher
65#[async_trait]
66pub trait EventSource: Send + Sync {
67    /// Human-readable name for this event source.
68    fn name(&self) -> &str;
69
70    /// Subscribe to the event stream.
71    ///
72    /// Returns a stream that yields trigger events until the source is stopped
73    /// or the subscription is dropped.
74    async fn subscribe(&self) -> Result<BoxStream<'static, TriggerEvent>>;
75}