feat: add ecc2 webhook notifications

This commit is contained in:
Affaan Mustafa
2026-04-09 21:14:09 -07:00
parent b45a6ca810
commit 5fb2e62216
7 changed files with 816 additions and 5 deletions

View File

@@ -1,12 +1,14 @@
use anyhow::Result;
use chrono::{DateTime, Local, Timelike};
use serde::{Deserialize, Serialize};
use serde_json::json;
#[cfg(not(test))]
use anyhow::Context;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NotificationEvent {
SessionStarted,
SessionCompleted,
SessionFailed,
BudgetAlert,
@@ -25,6 +27,7 @@ pub struct QuietHoursConfig {
#[serde(default)]
pub struct DesktopNotificationConfig {
pub enabled: bool,
pub session_started: bool,
pub session_completed: bool,
pub session_failed: bool,
pub budget_alerts: bool,
@@ -48,11 +51,43 @@ pub struct CompletionSummaryConfig {
pub delivery: CompletionSummaryDelivery,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WebhookProvider {
#[default]
Slack,
Discord,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct WebhookTarget {
pub provider: WebhookProvider,
pub url: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct WebhookNotificationConfig {
pub enabled: bool,
pub session_started: bool,
pub session_completed: bool,
pub session_failed: bool,
pub budget_alerts: bool,
pub approval_requests: bool,
pub targets: Vec<WebhookTarget>,
}
#[derive(Debug, Clone)]
pub struct DesktopNotifier {
config: DesktopNotificationConfig,
}
#[derive(Debug, Clone)]
pub struct WebhookNotifier {
config: WebhookNotificationConfig,
}
impl Default for QuietHoursConfig {
fn default() -> Self {
Self {
@@ -96,6 +131,7 @@ impl Default for DesktopNotificationConfig {
fn default() -> Self {
Self {
enabled: true,
session_started: false,
session_completed: true,
session_failed: true,
budget_alerts: true,
@@ -120,6 +156,7 @@ impl DesktopNotificationConfig {
}
match event {
NotificationEvent::SessionStarted => config.session_started,
NotificationEvent::SessionCompleted => config.session_completed,
NotificationEvent::SessionFailed => config.session_failed,
NotificationEvent::BudgetAlert => config.budget_alerts,
@@ -155,6 +192,68 @@ impl CompletionSummaryConfig {
}
}
impl Default for WebhookTarget {
fn default() -> Self {
Self {
provider: WebhookProvider::Slack,
url: String::new(),
}
}
}
impl WebhookTarget {
fn sanitized(self) -> Option<Self> {
let url = self.url.trim().to_string();
if url.starts_with("https://") || url.starts_with("http://") {
Some(Self { url, ..self })
} else {
None
}
}
}
impl Default for WebhookNotificationConfig {
fn default() -> Self {
Self {
enabled: false,
session_started: true,
session_completed: true,
session_failed: true,
budget_alerts: true,
approval_requests: false,
targets: Vec::new(),
}
}
}
impl WebhookNotificationConfig {
pub fn sanitized(self) -> Self {
Self {
targets: self
.targets
.into_iter()
.filter_map(WebhookTarget::sanitized)
.collect(),
..self
}
}
pub fn allows(&self, event: NotificationEvent) -> bool {
let config = self.clone().sanitized();
if !config.enabled || config.targets.is_empty() {
return false;
}
match event {
NotificationEvent::SessionStarted => config.session_started,
NotificationEvent::SessionCompleted => config.session_completed,
NotificationEvent::SessionFailed => config.session_failed,
NotificationEvent::BudgetAlert => config.budget_alerts,
NotificationEvent::ApprovalRequest => config.approval_requests,
}
}
}
impl DesktopNotifier {
pub fn new(config: DesktopNotificationConfig) -> Self {
Self {
@@ -192,6 +291,57 @@ impl DesktopNotifier {
}
}
impl WebhookNotifier {
pub fn new(config: WebhookNotificationConfig) -> Self {
Self {
config: config.sanitized(),
}
}
pub fn notify(&self, event: NotificationEvent, message: &str) -> bool {
match self.try_notify(event, message) {
Ok(sent) => sent,
Err(error) => {
tracing::warn!("Failed to send webhook notification: {error}");
false
}
}
}
fn try_notify(&self, event: NotificationEvent, message: &str) -> Result<bool> {
self.try_notify_with(event, message, send_webhook_request)
}
fn try_notify_with<F>(
&self,
event: NotificationEvent,
message: &str,
mut sender: F,
) -> Result<bool>
where
F: FnMut(&WebhookTarget, serde_json::Value) -> Result<()>,
{
if !self.config.allows(event) {
return Ok(false);
}
let mut delivered = false;
for target in &self.config.targets {
let payload = webhook_payload(target, message);
match sender(target, payload) {
Ok(()) => delivered = true,
Err(error) => tracing::warn!(
"Failed to deliver {:?} webhook notification to {}: {error}",
target.provider,
target.url
),
}
}
Ok(delivered)
}
}
fn notification_command(platform: &str, title: &str, body: &str) -> Option<(String, Vec<String>)> {
match platform {
"macos" => Some((
@@ -218,6 +368,20 @@ fn notification_command(platform: &str, title: &str, body: &str) -> Option<(Stri
}
}
fn webhook_payload(target: &WebhookTarget, message: &str) -> serde_json::Value {
match target.provider {
WebhookProvider::Slack => json!({
"text": message,
}),
WebhookProvider::Discord => json!({
"content": message,
"allowed_mentions": {
"parse": []
}
}),
}
}
#[cfg(not(test))]
fn run_notification_command(program: &str, args: &[String]) -> Result<()> {
let status = std::process::Command::new(program)
@@ -237,6 +401,29 @@ fn run_notification_command(_program: &str, _args: &[String]) -> Result<()> {
Ok(())
}
#[cfg(not(test))]
fn send_webhook_request(target: &WebhookTarget, payload: serde_json::Value) -> Result<()> {
let agent = ureq::AgentBuilder::new()
.timeout_connect(std::time::Duration::from_secs(5))
.timeout_read(std::time::Duration::from_secs(5))
.build();
let response = agent
.post(&target.url)
.send_json(payload)
.with_context(|| format!("POST {}", target.url))?;
if response.status() >= 200 && response.status() < 300 {
Ok(())
} else {
anyhow::bail!("{} returned {}", target.url, response.status());
}
}
#[cfg(test)]
fn send_webhook_request(_target: &WebhookTarget, _payload: serde_json::Value) -> Result<()> {
Ok(())
}
fn sanitize_osascript(value: &str) -> String {
value
.replace('\\', "")
@@ -247,10 +434,12 @@ fn sanitize_osascript(value: &str) -> String {
#[cfg(test)]
mod tests {
use super::{
notification_command, DesktopNotificationConfig, DesktopNotifier, NotificationEvent,
QuietHoursConfig,
notification_command, webhook_payload, CompletionSummaryDelivery,
DesktopNotificationConfig, DesktopNotifier, NotificationEvent, QuietHoursConfig,
WebhookNotificationConfig, WebhookNotifier, WebhookProvider, WebhookTarget,
};
use chrono::{Local, TimeZone};
use serde_json::json;
#[test]
fn quiet_hours_support_cross_midnight_ranges() {
@@ -285,6 +474,7 @@ mod tests {
assert!(!config.allows(NotificationEvent::SessionCompleted, now));
assert!(config.allows(NotificationEvent::BudgetAlert, now));
assert!(!config.allows(NotificationEvent::SessionStarted, now));
}
#[test]
@@ -329,4 +519,117 @@ mod tests {
assert_eq!(args[2], "ECC 2.0: Approval needed");
assert_eq!(args[3], "worker-123");
}
#[test]
fn webhook_notifications_require_enabled_targets_and_event() {
let mut config = WebhookNotificationConfig::default();
assert!(!config.allows(NotificationEvent::SessionCompleted));
config.enabled = true;
config.targets = vec![WebhookTarget {
provider: WebhookProvider::Slack,
url: "https://hooks.slack.test/services/abc".to_string(),
}];
assert!(config.allows(NotificationEvent::SessionCompleted));
assert!(config.allows(NotificationEvent::SessionStarted));
assert!(!config.allows(NotificationEvent::ApprovalRequest));
}
#[test]
fn webhook_sanitization_filters_invalid_urls() {
let config = WebhookNotificationConfig {
enabled: true,
targets: vec![
WebhookTarget {
provider: WebhookProvider::Slack,
url: "https://hooks.slack.test/services/abc".to_string(),
},
WebhookTarget {
provider: WebhookProvider::Discord,
url: "ftp://discord.invalid".to_string(),
},
],
..WebhookNotificationConfig::default()
}
.sanitized();
assert_eq!(config.targets.len(), 1);
assert_eq!(config.targets[0].provider, WebhookProvider::Slack);
}
#[test]
fn slack_webhook_payload_uses_text() {
let payload = webhook_payload(
&WebhookTarget {
provider: WebhookProvider::Slack,
url: "https://hooks.slack.test/services/abc".to_string(),
},
"*ECC 2.0* hello",
);
assert_eq!(payload, json!({ "text": "*ECC 2.0* hello" }));
}
#[test]
fn discord_webhook_payload_disables_mentions() {
let payload = webhook_payload(
&WebhookTarget {
provider: WebhookProvider::Discord,
url: "https://discord.test/api/webhooks/123".to_string(),
},
"```text\nsummary\n```",
);
assert_eq!(
payload,
json!({
"content": "```text\nsummary\n```",
"allowed_mentions": { "parse": [] }
})
);
}
#[test]
fn webhook_notifier_sends_to_each_target() {
let notifier = WebhookNotifier::new(WebhookNotificationConfig {
enabled: true,
targets: vec![
WebhookTarget {
provider: WebhookProvider::Slack,
url: "https://hooks.slack.test/services/abc".to_string(),
},
WebhookTarget {
provider: WebhookProvider::Discord,
url: "https://discord.test/api/webhooks/123".to_string(),
},
],
..WebhookNotificationConfig::default()
});
let mut sent = Vec::new();
let delivered = notifier
.try_notify_with(
NotificationEvent::SessionCompleted,
"payload text",
|target, payload| {
sent.push((target.provider, payload));
Ok(())
},
)
.unwrap();
assert!(delivered);
assert_eq!(sent.len(), 2);
assert_eq!(sent[0].0, WebhookProvider::Slack);
assert_eq!(sent[1].0, WebhookProvider::Discord);
}
#[test]
fn completion_summary_delivery_defaults_to_desktop() {
assert_eq!(
CompletionSummaryDelivery::default(),
CompletionSummaryDelivery::Desktop
);
}
}