use std::sync::Arc;
use tokio::sync::broadcast;
use tokio::task::JoinHandle;
use aa_runtime::approval::ApprovalRequest;
use super::delivery::webhook_delivery_loop;
use super::webhook::WebhookTarget;
use crate::budget::BudgetAlert;
pub const WEBHOOK_URL_ENV: &str = "AA_WEBHOOK_URL";
pub fn maybe_spawn_webhook(
approval_queue: &Arc<aa_runtime::approval::ApprovalQueue>,
budget_alert_rx: broadcast::Receiver<BudgetAlert>,
) -> Option<JoinHandle<()>> {
let url = match std::env::var(WEBHOOK_URL_ENV) {
Ok(url) if !url.is_empty() => url,
_ => {
tracing::info!(
env = WEBHOOK_URL_ENV,
"webhook URL not configured, event notifications disabled"
);
return None;
}
};
tracing::info!(url = %url, "webhook delivery enabled");
let client = reqwest::Client::new();
let target = WebhookTarget::new(client, url);
let approval_rx: broadcast::Receiver<ApprovalRequest> = approval_queue.subscribe_events();
let handle = tokio::spawn(webhook_delivery_loop(target, approval_rx, budget_alert_rx));
Some(handle)
}