feat(events): add generic Event Bus and /events/wait endpoint for agent wakeups
This commit is contained in:
1 parent
b8d58b2e64
commit
efcab4a7b4
5 files changed
+69
No files matched your search
@@ -0,0 +1,44 @@
|
||||
use crate::AppState;
|
||||
use axum::extract::{Query, State};
|
||||
use axum::response::IntoResponse;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use crate::state::GenericEvent;
|
||||
|
||||
pub async fn wait_for_event_handler(
|
||||
State(state): State<Arc<AppState>>,
|
||||
Query(params): Query<HashMap<String, String>>,
|
||||
) -> impl IntoResponse {
|
||||
let topic = params.get("topic").cloned();
|
||||
let session_id = params.get("session_id").cloned();
|
||||
|
||||
let mut rx = state.handler.state.event_bus_tx.subscribe();
|
||||
|
||||
loop {
|
||||
match rx.recv().await {
|
||||
Ok(event) => {
|
||||
let topic_matches = topic.as_ref().map_or(true, |t| t == &event.topic);
|
||||
let session_matches = session_id.as_ref().map_or(true, |s| Some(s) == event.session_id.as_ref());
|
||||
|
||||
if topic_matches && session_matches {
|
||||
return axum::Json(event);
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
return axum::Json(GenericEvent {
|
||||
topic: "error".to_string(),
|
||||
session_id: None,
|
||||
payload: serde_json::json!({"error": "Event bus lagged or closed"}),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn post_event_handler(
|
||||
State(state): State<Arc<AppState>>,
|
||||
axum::Json(event): axum::Json<GenericEvent>,
|
||||
) -> impl IntoResponse {
|
||||
let _ = state.handler.state.event_bus_tx.send(event);
|
||||
axum::Json(serde_json::json!({"status": "ok"}))
|
||||
}
|
||||
Reference in new issue
Block a user