systemprompt_events/services/
routing.rs1use std::sync::LazyLock;
2use systemprompt_identifiers::UserId;
3use tracing::debug;
4
5use super::{A2ABroadcaster, AgUiBroadcaster, AnalyticsBroadcaster, ContextBroadcaster};
6use crate::Broadcaster;
7use systemprompt_models::{A2AEvent, AgUiEvent, AnalyticsEvent, ContextEvent, SystemEvent};
8
9pub static CONTEXT_BROADCASTER: LazyLock<ContextBroadcaster> =
10 LazyLock::new(ContextBroadcaster::new);
11pub static AGUI_BROADCASTER: LazyLock<AgUiBroadcaster> = LazyLock::new(AgUiBroadcaster::new);
12pub static A2A_BROADCASTER: LazyLock<A2ABroadcaster> = LazyLock::new(A2ABroadcaster::new);
13pub static ANALYTICS_BROADCASTER: LazyLock<AnalyticsBroadcaster> =
14 LazyLock::new(AnalyticsBroadcaster::new);
15
16#[derive(Debug, Clone, Copy)]
17pub struct EventRouter;
18
19impl EventRouter {
20 pub async fn route_agui(user_id: &UserId, event: AgUiEvent) -> (usize, usize) {
21 let event_type = event.event_type();
22 let agui_count = AGUI_BROADCASTER.broadcast(user_id, event.clone()).await;
23 let context_count = CONTEXT_BROADCASTER
24 .broadcast(user_id, ContextEvent::AgUi(event))
25 .await;
26 debug!(
27 event_type = ?event_type,
28 user_id = %user_id,
29 agui_count = agui_count,
30 context_count = context_count,
31 "EventRouter: routed AG-UI event"
32 );
33 (agui_count, context_count)
34 }
35
36 pub async fn route_a2a(user_id: &UserId, event: A2AEvent) -> (usize, usize) {
37 let a2a_count = A2A_BROADCASTER.broadcast(user_id, event.clone()).await;
38 let context_count = CONTEXT_BROADCASTER.broadcast(user_id, event.into()).await;
39 (a2a_count, context_count)
40 }
41
42 pub async fn route_system(user_id: &UserId, event: SystemEvent) -> usize {
43 CONTEXT_BROADCASTER
44 .broadcast(user_id, ContextEvent::System(event))
45 .await
46 }
47
48 pub async fn route_analytics(user_id: &UserId, event: AnalyticsEvent) -> usize {
49 ANALYTICS_BROADCASTER.broadcast(user_id, event).await
50 }
51}