use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;
use adk_core::Tool;
use adk_kernel::drivers::EffectClass;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use tokio::sync::Mutex as AsyncMutex;
use uuid::Uuid;
use crate::class::{AutonomyCap, Side, ToolSpec};
use crate::error::{DriverError, Result};
use crate::metrics::{KpiSource, unknown_metric};
use crate::outbound::{
ConsentCheck, Disclosure, InMemoryConsent, InMemorySuppressions, Limiter, RateLimit,
ReplyTemplates, Suppression, SuppressionList, SuppressionScope, ThreadReplier, fields_arg,
str_arg,
};
use crate::tools::DriverTool;
mod auth;
#[cfg(feature = "imap-smtp")]
mod imap;
#[cfg(feature = "imap-smtp")]
mod imap_smtp;
pub(crate) mod mime;
#[cfg(feature = "imap-smtp")]
mod smtp;
#[cfg(feature = "imap-smtp")]
mod tls;
#[cfg(feature = "system-dns")]
pub use auth::SystemTxtLookup;
pub use auth::{AuthCheck, DomainAuthCheck, DomainAuthReport, SenderPolicy, StaticTxt, TxtLookup};
#[cfg(feature = "imap-smtp")]
pub use imap_smtp::{ImapSmtpEmail, MailEndpoint, MailSecurity};
const AUTH_FRESHNESS: chrono::Duration = chrono::Duration::hours(24);
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Mailbox {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub address: String,
}
impl Mailbox {
pub fn new(address: impl Into<String>) -> Self {
Self { name: None, address: address.into().trim().to_string() }
}
#[must_use]
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum InboundKind {
Message,
Bounce,
Complaint,
Unsubscribe,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InboundEmail {
pub id: String,
pub thread: String,
pub from: Mailbox,
pub to: Vec<Mailbox>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub cc: Vec<Mailbox>,
pub subject: String,
pub text: String,
pub received_at: DateTime<Utc>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub in_reply_to: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub references: Vec<String>,
pub kind: InboundKind,
#[serde(default)]
pub auto_submitted: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub report_recipient: Option<String>,
}
impl InboundEmail {
pub fn message(from: Mailbox, subject: impl Into<String>, text: impl Into<String>) -> Self {
let id = format!("{}@inbound.invalid", Uuid::new_v4().simple());
Self {
thread: id.clone(),
id,
from,
to: Vec::new(),
cc: Vec::new(),
subject: subject.into(),
text: text.into(),
received_at: Utc::now(),
in_reply_to: None,
references: Vec::new(),
kind: InboundKind::Message,
auto_submitted: false,
report_recipient: None,
}
}
pub fn parse(raw: &[u8], received_at: DateTime<Utc>) -> Self {
mime::parse_message(
raw,
received_at,
&format!("{}@inbound.invalid", Uuid::new_v4().simple()),
)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct InboxPage {
pub messages: Vec<InboundEmail>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cursor: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct OutboundEmail {
pub message_id: String,
pub from: Mailbox,
pub to: Vec<Mailbox>,
pub subject: String,
pub text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub in_reply_to: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub references: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub headers: Vec<(String, String)>,
}
impl OutboundEmail {
pub fn to_wire(&self, date: DateTime<Utc>) -> Result<Vec<u8>> {
mime::render_message(self, date)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SentEmail {
pub message_id: String,
pub accepted: Vec<String>,
pub at: DateTime<Utc>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct DeliveryStats {
pub sent: u64,
pub rejected: u64,
pub bounced: u64,
pub complaints: u64,
}
#[async_trait]
pub trait EmailDriver: Send + Sync {
fn name(&self) -> &str;
async fn inbox(&self, cursor: Option<&str>, limit: usize) -> Result<InboxPage>;
async fn send(&self, message: &OutboundEmail) -> Result<SentEmail>;
async fn delivery_stats(&self, from: DateTime<Utc>, to: DateTime<Utc>)
-> Result<DeliveryStats>;
}
#[derive(Debug, Default)]
pub(crate) struct DeliveryLog {
events: Mutex<Vec<(DateTime<Utc>, DeliveryEvent)>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DeliveryEvent {
Sent,
Rejected,
Bounced,
Complained,
}
impl DeliveryLog {
pub(crate) fn record(&self, event: DeliveryEvent, at: DateTime<Utc>) {
self.events.lock().unwrap_or_else(PoisonError::into_inner).push((at, event));
}
pub(crate) fn record_reports(&self, messages: &[InboundEmail]) {
for message in messages {
match message.kind {
InboundKind::Bounce => self.record(DeliveryEvent::Bounced, message.received_at),
InboundKind::Complaint => {
self.record(DeliveryEvent::Complained, message.received_at)
}
InboundKind::Message | InboundKind::Unsubscribe => {}
}
}
}
pub(crate) fn stats(&self, from: DateTime<Utc>, to: DateTime<Utc>) -> DeliveryStats {
let events = self.events.lock().unwrap_or_else(PoisonError::into_inner);
let mut stats = DeliveryStats::default();
for (_, event) in events.iter().filter(|(at, _)| *at >= from && *at < to) {
match event {
DeliveryEvent::Sent => stats.sent += 1,
DeliveryEvent::Rejected => stats.rejected += 1,
DeliveryEvent::Bounced => stats.bounced += 1,
DeliveryEvent::Complained => stats.complaints += 1,
}
}
stats
}
}
#[derive(Debug, Default)]
pub struct InMemoryEmail {
inbox: Mutex<Vec<InboundEmail>>,
sent: Mutex<Vec<OutboundEmail>>,
refuse: Mutex<Vec<String>>,
log: DeliveryLog,
}
impl InMemoryEmail {
pub fn new() -> Self {
Self::default()
}
pub fn deliver(&self, message: InboundEmail) {
self.log.record_reports(std::slice::from_ref(&message));
self.inbox.lock().unwrap_or_else(PoisonError::into_inner).push(message);
}
pub fn sent(&self) -> Vec<OutboundEmail> {
self.sent.lock().unwrap_or_else(PoisonError::into_inner).clone()
}
pub fn refuse(&self, address: &str) {
self.refuse
.lock()
.unwrap_or_else(PoisonError::into_inner)
.push(address.to_ascii_lowercase());
}
}
#[async_trait]
impl EmailDriver for InMemoryEmail {
fn name(&self) -> &str {
"in-memory"
}
async fn inbox(&self, cursor: Option<&str>, limit: usize) -> Result<InboxPage> {
let after = match cursor {
Some(cursor) => cursor.parse::<usize>().map_err(|_| {
DriverError::invalid(format!("`{cursor}` is not a cursor this driver gave"))
})?,
None => 0,
};
let inbox = self.inbox.lock().unwrap_or_else(PoisonError::into_inner);
let messages: Vec<InboundEmail> = inbox.iter().skip(after).take(limit).cloned().collect();
Ok(InboxPage { cursor: Some((after + messages.len()).to_string()), messages })
}
async fn send(&self, message: &OutboundEmail) -> Result<SentEmail> {
let refused = self.refuse.lock().unwrap_or_else(PoisonError::into_inner).clone();
let at = Utc::now();
let mut accepted = Vec::new();
for recipient in &message.to {
if refused.contains(&recipient.address.to_ascii_lowercase()) {
self.log.record(DeliveryEvent::Rejected, at);
} else {
accepted.push(recipient.address.clone());
}
}
if accepted.is_empty() {
return Err(DriverError::refused("the server accepted no recipient"));
}
mime::render_message(message, at)?;
self.sent.lock().unwrap_or_else(PoisonError::into_inner).push(message.clone());
self.log.record(DeliveryEvent::Sent, at);
Ok(SentEmail { message_id: message.message_id.clone(), accepted, at })
}
async fn delivery_stats(
&self,
from: DateTime<Utc>,
to: DateTime<Utc>,
) -> Result<DeliveryStats> {
Ok(self.log.stats(from, to))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum SendKind {
Reply,
Sequence,
Newsletter,
}
impl SendKind {
pub fn is_bulk(self) -> bool {
matches!(self, Self::Sequence | Self::Newsletter)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct KnownThread {
sender: Mailbox,
subject: String,
last_message: String,
references: Vec<String>,
automatic: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct NewsletterReport {
pub sent: Vec<String>,
pub skipped: Vec<(String, String)>,
}
pub type UnsubscribeLink = Arc<dyn Fn(&str) -> Option<String> + Send + Sync>;
pub struct EmailOutbox {
driver: Arc<dyn EmailDriver>,
from: Mailbox,
disclosure: Disclosure,
suppressions: Arc<dyn SuppressionList>,
consent: Arc<dyn ConsentCheck>,
auth: Option<(DomainAuthCheck, SenderPolicy)>,
report: AsyncMutex<Option<DomainAuthReport>>,
templates: ReplyTemplates,
per_thread: Limiter,
replies: Limiter,
threads: Mutex<BTreeMap<String, KnownThread>>,
unsubscribe_link: Option<UnsubscribeLink>,
}
impl std::fmt::Debug for EmailOutbox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EmailOutbox")
.field("driver", &self.driver.name())
.field("from", &self.from)
.finish_non_exhaustive()
}
}
pub struct EmailOutboxBuilder {
outbox: EmailOutbox,
}
impl EmailOutboxBuilder {
#[must_use]
pub fn suppressions(mut self, suppressions: Arc<dyn SuppressionList>) -> Self {
self.outbox.suppressions = suppressions;
self
}
#[must_use]
pub fn consent(mut self, consent: Arc<dyn ConsentCheck>) -> Self {
self.outbox.consent = consent;
self
}
#[must_use]
pub fn domain_auth(mut self, lookup: Arc<dyn TxtLookup>, policy: SenderPolicy) -> Self {
self.outbox.auth = Some((DomainAuthCheck::new(lookup), policy));
self
}
#[must_use]
pub fn templates(mut self, templates: ReplyTemplates) -> Self {
self.outbox.templates = templates;
self
}
#[must_use]
pub fn reply_limits(mut self, per_thread: RateLimit, overall: RateLimit) -> Self {
self.outbox.per_thread = Limiter::new(per_thread);
self.outbox.replies = Limiter::new(overall);
self
}
#[must_use]
pub fn unsubscribe_link(mut self, link: UnsubscribeLink) -> Self {
self.outbox.unsubscribe_link = Some(link);
self
}
pub fn build(self) -> EmailOutbox {
self.outbox
}
}
impl EmailOutbox {
pub fn builder(
driver: Arc<dyn EmailDriver>,
from: Mailbox,
disclosure: Disclosure,
) -> EmailOutboxBuilder {
EmailOutboxBuilder {
outbox: Self {
driver,
from,
disclosure,
suppressions: Arc::new(InMemorySuppressions::new()),
consent: Arc::new(InMemoryConsent::new()),
auth: None,
report: AsyncMutex::new(None),
templates: ReplyTemplates::new(),
per_thread: Limiter::new(RateLimit::new(3, Duration::from_secs(3_600))),
replies: Limiter::new(RateLimit::new(60, Duration::from_secs(3_600))),
threads: Mutex::default(),
unsubscribe_link: None,
},
}
}
pub fn driver(&self) -> &Arc<dyn EmailDriver> {
&self.driver
}
pub fn suppressions(&self) -> &Arc<dyn SuppressionList> {
&self.suppressions
}
pub async fn read_inbox(&self, cursor: Option<&str>, limit: usize) -> Result<InboxPage> {
let page = self.driver.inbox(cursor, limit).await?;
for message in &page.messages {
match (message.kind, &message.report_recipient) {
(InboundKind::Unsubscribe, _) => {
self.suppressions
.suppress(Suppression::new(
&message.from.address,
SuppressionScope::Marketing,
"mailto",
))
.await?;
}
(InboundKind::Bounce, Some(recipient)) => {
self.suppressions
.suppress(Suppression::new(recipient, SuppressionScope::All, "bounce"))
.await?;
}
(InboundKind::Complaint, Some(recipient)) => {
self.suppressions
.suppress(Suppression::new(
recipient,
SuppressionScope::Marketing,
"complaint",
))
.await?;
}
_ => {}
}
if message.kind == InboundKind::Message {
let mut references = message.references.clone();
if !references.contains(&message.id) {
references.push(message.id.clone());
}
self.threads.lock().unwrap_or_else(PoisonError::into_inner).insert(
message.thread.clone(),
KnownThread {
sender: message.from.clone(),
subject: message.subject.clone(),
last_message: message.id.clone(),
references,
automatic: message.auto_submitted,
},
);
}
}
Ok(page)
}
pub async fn unsubscribe(
&self,
recipient: &str,
scope: SuppressionScope,
source: &str,
) -> Result<()> {
self.suppressions.suppress(Suppression::new(recipient, scope, source)).await
}
pub async fn check_domain(&self) -> Result<DomainAuthReport> {
let Some((check, policy)) = &self.auth else {
return Err(DriverError::refused(
"no domain check is configured for this outbox; give it the sending domain's policy",
));
};
let report = check.run(policy).await?;
*self.report.lock().await = Some(report.clone());
Ok(report)
}
pub async fn domain_report(&self) -> Option<DomainAuthReport> {
self.report.lock().await.clone()
}
async fn before_send(&self, kind: SendKind) -> Result<()> {
let report = self.report.lock().await.clone();
let stale =
report.as_ref().is_none_or(|report| Utc::now() - report.checked_at > AUTH_FRESHNESS);
let report =
if stale && self.auth.is_some() { Some(self.check_domain().await?) } else { report };
if kind.is_bulk() {
match report {
Some(report) if report.passes() => {}
Some(report) => {
return Err(DriverError::refused(format!(
"bulk sending waits until SPF, DKIM and DMARC pass for {}: {}",
report.domain,
report.failures().join("; ")
)));
}
None => {
return Err(DriverError::refused(
"bulk sending waits until the sending domain's SPF, DKIM and DMARC are checked and pass",
));
}
}
}
Ok(())
}
async fn check_suppression(&self, recipient: &str, kind: SendKind) -> Result<()> {
if let Some(suppression) = self.suppressions.lookup(recipient).await? {
let covers = suppression.scope == SuppressionScope::All || kind != SendKind::Reply;
if covers {
return Err(DriverError::refused(format!(
"{recipient} unsubscribed or revoked ({:?}, {}); nothing more goes to them",
suppression.scope, suppression.source
)));
}
}
Ok(())
}
fn new_message_id(&self) -> String {
let domain = self.from.address.rsplit('@').next().unwrap_or("site.invalid");
format!("{}@{domain}", Uuid::new_v4().simple())
}
fn unsubscribe_parts(&self, recipient: &str) -> (Vec<(String, String)>, String) {
let mailto = format!("mailto:{}?subject=unsubscribe", self.from.address);
let link = self.unsubscribe_link.as_ref().and_then(|link| link(recipient));
let mut headers = Vec::new();
let footer = match &link {
Some(link) => {
headers.push(("List-Unsubscribe".to_string(), format!("<{link}>, <{mailto}>")));
headers.push((
"List-Unsubscribe-Post".to_string(),
"List-Unsubscribe=One-Click".to_string(),
));
format!("Unsubscribe: {link}")
}
None => {
headers.push(("List-Unsubscribe".to_string(), format!("<{mailto}>")));
format!(
"Unsubscribe: reply with the subject \"unsubscribe\" or write to {}",
self.from.address
)
}
};
(headers, footer)
}
pub async fn reply(
&self,
thread: &str,
template: &str,
fields: &BTreeMap<String, String>,
) -> Result<SentEmail> {
let known = self
.threads
.lock()
.unwrap_or_else(PoisonError::into_inner)
.get(thread)
.cloned()
.ok_or_else(|| {
DriverError::refused(
"reply_in_thread answers only a thread the inbox read; it never starts one",
)
})?;
if known.automatic {
return Err(DriverError::refused(
"the thread's last message was sent automatically; it gets no reply",
));
}
let text = self.templates.render(template, fields)?;
self.check_suppression(&known.sender.address, SendKind::Reply).await?;
self.before_send(SendKind::Reply).await?;
self.per_thread.take(thread, "replies in one thread")?;
self.replies.take("all", "replies")?;
let subject = if known.subject.to_ascii_lowercase().starts_with("re:") {
known.subject.clone()
} else {
format!("Re: {}", known.subject)
};
let message = OutboundEmail {
message_id: self.new_message_id(),
from: self.from.clone(),
to: vec![known.sender.clone()],
subject,
text: self.disclosure.apply(&text),
in_reply_to: Some(known.last_message.clone()),
references: known.references.clone(),
headers: vec![("Auto-Submitted".into(), "auto-replied".into())],
};
self.driver.send(&message).await
}
pub async fn send_sequence_step(
&self,
to: &Mailbox,
subject: &str,
text: &str,
consent_scope: &str,
) -> Result<SentEmail> {
self.before_send(SendKind::Sequence).await?;
self.check_bulk_recipient(&to.address, consent_scope).await?;
let (headers, footer) = self.unsubscribe_parts(&to.address);
let message = OutboundEmail {
message_id: self.new_message_id(),
from: self.from.clone(),
to: vec![to.clone()],
subject: subject.to_string(),
text: self.disclosure.apply(&format!("{}\n\n{footer}", text.trim_end())),
in_reply_to: None,
references: Vec::new(),
headers,
};
self.driver.send(&message).await
}
async fn check_bulk_recipient(&self, address: &str, consent_scope: &str) -> Result<()> {
self.check_suppression(address, SendKind::Sequence).await?;
if !self.consent.is_live(address, consent_scope).await? {
return Err(DriverError::refused(format!(
"{address} holds no live consent for `{consent_scope}` (spec 3.5 A-I8)"
)));
}
Ok(())
}
pub async fn send_newsletter(
&self,
subject: &str,
text: &str,
recipients: &[Mailbox],
consent_scope: &str,
) -> Result<NewsletterReport> {
self.before_send(SendKind::Newsletter).await?;
let mut report = NewsletterReport { sent: Vec::new(), skipped: Vec::new() };
for recipient in recipients {
if let Err(error) = self.check_bulk_recipient(&recipient.address, consent_scope).await {
report.skipped.push((recipient.address.clone(), error.to_string()));
continue;
}
let (headers, footer) = self.unsubscribe_parts(&recipient.address);
let message = OutboundEmail {
message_id: self.new_message_id(),
from: self.from.clone(),
to: vec![recipient.clone()],
subject: subject.to_string(),
text: self.disclosure.apply(&format!("{}\n\n{footer}", text.trim_end())),
in_reply_to: None,
references: Vec::new(),
headers,
};
match self.driver.send(&message).await {
Ok(_) => report.sent.push(recipient.address.clone()),
Err(error) => report.skipped.push((recipient.address.clone(), error.to_string())),
}
}
Ok(report)
}
}
#[async_trait]
impl ThreadReplier for EmailOutbox {
fn prefix(&self) -> &str {
"email"
}
async fn reply_in_thread(
&self,
thread: &str,
template: &str,
fields: &BTreeMap<String, String>,
) -> Result<Value> {
let sent = self.reply(thread, template, fields).await?;
Ok(serde_json::to_value(sent).unwrap_or_default())
}
}
pub const EMAIL_METRICS: [&str; 6] = [
"email.sent",
"email.rejected",
"email.bounced",
"email.complaints",
"email.bounce_rate",
"email.complaint_rate",
];
impl EmailOutbox {
pub fn metrics(&self) -> Vec<String> {
EMAIL_METRICS.iter().map(|metric| (*metric).to_string()).collect()
}
pub async fn measure(
&self,
metric: &str,
from: DateTime<Utc>,
to: DateTime<Utc>,
) -> Result<Option<f64>> {
let stats = self.driver.delivery_stats(from, to).await?;
let rate = |count: u64| (stats.sent > 0).then(|| count as f64 / stats.sent as f64);
Ok(match metric {
"email.sent" => Some(stats.sent as f64),
"email.rejected" => Some(stats.rejected as f64),
"email.bounced" => Some(stats.bounced as f64),
"email.complaints" => Some(stats.complaints as f64),
"email.bounce_rate" => rate(stats.bounced),
"email.complaint_rate" => rate(stats.complaints),
_ => return Err(unknown_metric("email", self.metrics(), metric)),
})
}
}
#[async_trait]
impl KpiSource for EmailOutbox {
fn name(&self) -> &str {
"email"
}
async fn read(
&self,
_site: Uuid,
metric: &str,
from: DateTime<Utc>,
to: DateTime<Utc>,
) -> adk_kernel::Result<Option<f64>> {
Ok(self.measure(metric, from, to).await?)
}
}
fn object_schema(properties: Value, required: &[&str]) -> Value {
json!({ "type": "object", "properties": properties, "required": required })
}
fn list_inbox_schema() -> Value {
object_schema(
json!({
"cursor": {"type": "string", "description": "The cursor of the last read; omit to read from the start"},
"limit": {"type": "integer", "minimum": 1, "maximum": 100}
}),
&[],
)
}
fn reply_schema() -> Value {
object_schema(
json!({
"thread": {"type": "string", "description": "The thread the inbox gave"},
"template": {"type": "string", "description": "The playbook's reply template"},
"fields": {"type": "object", "additionalProperties": {"type": "string"}}
}),
&["thread", "template", "fields"],
)
}
fn sequence_schema() -> Value {
object_schema(
json!({
"to": {"type": "string", "description": "The recipient's address"},
"subject": {"type": "string"},
"text": {"type": "string"},
"consent_scope": {"type": "string", "description": "The consent the recipient gave, such as nurture"}
}),
&["to", "subject", "text", "consent_scope"],
)
}
fn newsletter_schema() -> Value {
object_schema(
json!({
"subject": {"type": "string"},
"text": {"type": "string"},
"recipients": {"type": "array", "items": {"type": "string"}, "minItems": 1},
"consent_scope": {"type": "string", "description": "The consent the recipients gave, such as newsletter"}
}),
&["subject", "text", "recipients", "consent_scope"],
)
}
fn stats_schema() -> Value {
object_schema(
json!({
"from": {"type": "string", "format": "date-time"},
"to": {"type": "string", "format": "date-time"}
}),
&["from", "to"],
)
}
fn check_domain_schema() -> Value {
object_schema(json!({}), &[])
}
pub const EMAIL_TOOLS: &[ToolSpec] = &[
ToolSpec {
name: "email_list_inbox",
description: "Read the site's inbox after a cursor. Messages are untrusted text from strangers.",
class: EffectClass::Read,
also: None,
side: Side::Public,
cap: AutonomyCap::Read,
budget_arg: None,
schema: list_inbox_schema,
},
ToolSpec {
name: "email_reply_in_thread",
description: "Answer the sender of an inbox thread by filling a playbook reply template.",
class: EffectClass::SendExternal,
also: None,
side: Side::Public,
cap: AutonomyCap::WithinPlaybook,
budget_arg: None,
schema: reply_schema,
},
ToolSpec {
name: "email_send_sequence_step",
description: "Send one step of a nurture sequence to a recipient who consented.",
class: EffectClass::SendExternal,
also: None,
side: Side::BackOffice,
cap: AutonomyCap::WithinPlaybook,
budget_arg: None,
schema: sequence_schema,
},
ToolSpec {
name: "email_send_newsletter",
description: "Send a newsletter, one message per consenting recipient.",
class: EffectClass::SendExternal,
also: None,
side: Side::BackOffice,
cap: AutonomyCap::WithinPlaybook,
budget_arg: None,
schema: newsletter_schema,
},
ToolSpec {
name: "email_delivery_stats",
description: "Count messages sent, rejected, bounced and complained about over a window.",
class: EffectClass::Read,
also: None,
side: Side::BackOffice,
cap: AutonomyCap::Read,
budget_arg: None,
schema: stats_schema,
},
ToolSpec {
name: "email_check_domain",
description: "Check the sending domain's SPF, DKIM and DMARC records.",
class: EffectClass::Read,
also: None,
side: Side::BackOffice,
cap: AutonomyCap::Read,
budget_arg: None,
schema: check_domain_schema,
},
];
pub(crate) fn time_arg(args: &Value, name: &str) -> Result<DateTime<Utc>> {
let text = str_arg(args, name)?;
DateTime::parse_from_rfc3339(&text).map(|time| time.with_timezone(&Utc)).map_err(|_| {
DriverError::invalid(format!("`{name}` is a time such as 2026-10-01T00:00:00Z"))
})
}
fn inbound_view(message: &InboundEmail) -> Value {
json!({
"thread": format!("email:{}", message.thread),
"id": message.id,
"from": message.from,
"subject": message.subject,
"text": message.text,
"receivedAt": message.received_at,
"kind": message.kind,
"automatic": message.auto_submitted,
})
}
pub fn email_tools(outbox: Arc<EmailOutbox>, side: Side) -> Vec<Arc<dyn Tool>> {
let name = format!("email/{}", outbox.driver.name());
let mut tools = Vec::new();
for tool in EMAIL_TOOLS.iter().filter(|tool| tool.side == side) {
let outbox = Arc::clone(&outbox);
let tool: Arc<dyn Tool> = match tool.name {
"email_list_inbox" => DriverTool::shared(tool, &name, move |args| {
let outbox = Arc::clone(&outbox);
Box::pin(async move {
let cursor = args.get("cursor").and_then(Value::as_str).map(str::to_string);
let limit =
args.get("limit").and_then(Value::as_u64).unwrap_or(20).clamp(1, 100)
as usize;
let page = outbox.read_inbox(cursor.as_deref(), limit).await?;
let messages: Vec<Value> = page.messages.iter().map(inbound_view).collect();
Ok(json!({ "messages": messages, "cursor": page.cursor }))
})
}),
"email_reply_in_thread" => DriverTool::shared(tool, &name, move |args| {
let outbox = Arc::clone(&outbox);
Box::pin(async move {
let thread = str_arg(&args, "thread")?;
let thread = thread.strip_prefix("email:").unwrap_or(&thread).to_string();
let sent = outbox
.reply(&thread, &str_arg(&args, "template")?, &fields_arg(&args)?)
.await?;
Ok(serde_json::to_value(sent).unwrap_or_default())
})
}),
"email_send_sequence_step" => DriverTool::shared(tool, &name, move |args| {
let outbox = Arc::clone(&outbox);
Box::pin(async move {
let to = Mailbox::new(str_arg(&args, "to")?);
let sent = outbox
.send_sequence_step(
&to,
&str_arg(&args, "subject")?,
&str_arg(&args, "text")?,
&str_arg(&args, "consent_scope")?,
)
.await?;
Ok(serde_json::to_value(sent).unwrap_or_default())
})
}),
"email_send_newsletter" => DriverTool::shared(tool, &name, move |args| {
let outbox = Arc::clone(&outbox);
Box::pin(async move {
let recipients: Vec<Mailbox> = args
.get("recipients")
.and_then(Value::as_array)
.ok_or_else(|| DriverError::invalid("`recipients` is a list of addresses"))?
.iter()
.filter_map(Value::as_str)
.map(Mailbox::new)
.collect();
let report = outbox
.send_newsletter(
&str_arg(&args, "subject")?,
&str_arg(&args, "text")?,
&recipients,
&str_arg(&args, "consent_scope")?,
)
.await?;
Ok(serde_json::to_value(report).unwrap_or_default())
})
}),
"email_delivery_stats" => DriverTool::shared(tool, &name, move |args| {
let outbox = Arc::clone(&outbox);
Box::pin(async move {
let stats = outbox
.driver
.delivery_stats(time_arg(&args, "from")?, time_arg(&args, "to")?)
.await?;
Ok(serde_json::to_value(stats).unwrap_or_default())
})
}),
_ => DriverTool::shared(tool, &name, move |_args| {
let outbox = Arc::clone(&outbox);
Box::pin(async move {
Ok(serde_json::to_value(outbox.check_domain().await?).unwrap_or_default())
})
}),
};
tools.push(tool);
}
tools
}