Skip to main content

aidaemon_work/triggers/
mod.rs

1//! Events that wake work: what triggers and channels hand the scheduler, the
2//! bus that carries them, and the trigger sources (the email trigger behind
3//! the `email-trigger` feature).
4
5use std::fmt;
6
7use tokio::sync::broadcast;
8
9#[cfg(feature = "email-trigger")]
10mod email;
11#[cfg(feature = "email-trigger")]
12pub use email::TriggerManager;
13
14pub type EventSender = broadcast::Sender<TriggerEvent>;
15pub type EventReceiver = broadcast::Receiver<TriggerEvent>;
16
17/// Create a new event bus (broadcast channel).
18pub fn event_bus(capacity: usize) -> (EventSender, EventReceiver) {
19    broadcast::channel(capacity)
20}
21
22/// Which trigger sources run.
23#[derive(Debug, Clone, Default)]
24pub struct TriggerSettings {
25    pub email: Option<EmailTriggerSettings>,
26}
27
28/// An IMAP mailbox to watch for new mail.
29#[derive(Clone)]
30pub struct EmailTriggerSettings {
31    pub host: String,
32    pub port: u16,
33    pub username: String,
34    pub password: String,
35    pub folder: String,
36}
37
38impl fmt::Debug for EmailTriggerSettings {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        f.debug_struct("EmailTriggerSettings")
41            .field("host", &self.host)
42            .field("port", &self.port)
43            .field("username", &self.username)
44            .field("password", &"[REDACTED]")
45            .field("folder", &self.folder)
46            .finish()
47    }
48}
49
50/// The prefix of every session an automated trigger's turn runs in. A
51/// trigger's turn is an untrusted source by this typed id the daemon itself
52/// writes, never by what the source is called.
53pub const TRIGGER_SESSION_PREFIX: &str = "trigger:";
54
55/// The session a trigger from `source` runs its turns in.
56pub fn trigger_session_id(source: &str) -> String {
57    format!("{TRIGGER_SESSION_PREFIX}{source}")
58}
59
60/// Check if a session ID indicates it was triggered by an automated source
61/// (e.g., email trigger) rather than direct user interaction via Telegram.
62///
63/// Daemon-written id markers: every trigger turn runs in a session under
64/// [`TRIGGER_SESSION_PREFIX`] (the trigger handler
65/// puts it there whatever the source names it). The older `email_trigger`
66/// and `event_` ids, and any id with "trigger" in it, still count, so a
67/// stored session from before is never treated as the owner's.
68pub fn is_trigger_session(session_id: &str) -> bool {
69    session_id.starts_with(TRIGGER_SESSION_PREFIX)
70        || session_id.contains("trigger")
71        || session_id.starts_with("event_")
72}
73
74/// An event emitted by triggers or channels (not the event-sourcing store).
75#[derive(Debug, Clone)]
76pub struct TriggerEvent {
77    pub source: String,
78    pub session_id: String,
79    pub content: String,
80    /// Whether this event originates from an explicitly trusted source
81    /// (e.g., a scheduled task marked `trusted = true` in config).
82    pub trusted: bool,
83    /// Optional content-free structural signal for matching Autopilot mandates.
84    /// The human-readable `content` is never used for wake routing.
85    pub mandate_signal: Option<crate::mandates::MandateWakeSignal>,
86}
87
88#[cfg(test)]
89mod trigger_session_tests {
90    #[test]
91    fn every_trigger_session_id_is_a_trigger_session() {
92        for source in ["email", "webhook", "calendar"] {
93            let id = super::trigger_session_id(source);
94            assert!(super::is_trigger_session(&id), "{id}");
95        }
96        assert!(super::is_trigger_session("email_trigger"));
97        assert!(!super::is_trigger_session("telegram:synthetic-user-1"));
98    }
99}