pub struct NotificationService { /* private fields */ }Expand description
Notification policy service: classification + dedup + preference persistence.
Construct with NotificationService::new and share via Arc.
Implementations§
Source§impl NotificationService
impl NotificationService
Sourcepub const DEDUP_WINDOW: Duration
pub const DEDUP_WINDOW: Duration
Window within which a repeated dedup_key is coalesced (suppressed).
Sourcepub fn new(prefs_path: PathBuf) -> Self
pub fn new(prefs_path: PathBuf) -> Self
Creates a service, loading preferences from prefs_path.
A missing or unparsable file falls back to
NotificationPreferences::default (see NotificationPreferences::load).
The dedup map and relay set start empty.
Sourcepub fn notify(&self, session_id: &str, event: &AgentEvent) -> Option<AgentEvent>
pub fn notify(&self, session_id: &str, event: &AgentEvent) -> Option<AgentEvent>
Classifies event and, if notification-worthy and not a recent
duplicate, materialises an AgentEvent::Notification.
Returns None when the policy declines the event (disabled, gated, or
not notification-worthy) or when an identical dedup_key fired within
Self::DEDUP_WINDOW. On a successful (non-deduped) emission the dedup
map is opportunistically pruned of entries older than the window.
Sourcepub fn mint_custom(
&self,
session_id: &str,
title: impl Into<String>,
body: impl Into<String>,
priority: NotificationPriority,
) -> Option<AgentEvent>
pub fn mint_custom( &self, session_id: &str, title: impl Into<String>, body: impl Into<String>, priority: NotificationPriority, ) -> Option<AgentEvent>
Mints a custom notification for the notify tool.
This is a passthrough classification (see
policy::classify_custom): it does not consult a per-category
preference — there isn’t one for arbitrary agent-chosen content — only
the master enabled switch and the shared dedup window apply. Callers
choose priority; title/body are used verbatim (no truncation).
Sourcepub fn notify_schedule_run(
&self,
session_id: &str,
success: bool,
title: impl Into<String>,
body: impl Into<String>,
) -> Option<AgentEvent>
pub fn notify_schedule_run( &self, session_id: &str, success: bool, title: impl Into<String>, body: impl Into<String>, ) -> Option<AgentEvent>
Mints a schedule-run completion/failure notification (see
policy::classify_schedule_run for the full no-double-fire
rationale): it shares the SAME category/priority/dedup key that
Self::notify would derive from the raw AgentEvent::Complete/
Error a scheduled run’s agent loop also emits, so this is safe to
call unconditionally alongside the always-on relay — at most one of
the two ever survives this service’s dedup window.
Sourcepub fn preferences(&self) -> NotificationPreferences
pub fn preferences(&self) -> NotificationPreferences
Returns a snapshot clone of the current preferences.
Sourcepub fn set_preferences(&self, prefs: NotificationPreferences) -> Result<()>
pub fn set_preferences(&self, prefs: NotificationPreferences) -> Result<()>
Replaces the current preferences and persists them to disk.
The in-memory value is updated first, then written to the configured path; a write error is returned but the in-memory update still stands.
Sourcepub fn try_begin_relay(&self, session_id: &str) -> bool
pub fn try_begin_relay(&self, session_id: &str) -> bool
Marks session_id as having a running relay.
Returns true when the session was newly inserted — i.e. the caller is
the one that should spawn the relay. Returns false if a relay is
already active for this session.