Skip to main content

Crate agent_event_bus

Crate agent_event_bus 

Source
Expand description

§agent-event-bus

Tiny in-process pub/sub for agent loop events. Sync-only Rust mirror of the Python agent-event-bus library.

Producers emit events by name; subscribers register a handler for the event name they care about. Pass "*" as the name to subscribe to every event (the “firehose”). One-shot handlers are supported via EventBus::on_once.

Subscribers are dispatched inline on the caller’s thread, in registration order. A handler that panics is caught with std::panic::catch_unwind so the bus keeps dispatching to the rest. An optional on_error callback is invoked with the offending Subscription and Event.

This crate is sync-only. The Python sibling also exposes an emit_async path; that is intentionally not mirrored here because it would pull in a runtime (tokio/async-std) and tangle the dispatch model. If you want async dispatch, spawn it on top of this bus.

§Quick example

use agent_event_bus::EventBus;
use std::sync::{Arc, Mutex};

let bus = EventBus::new();
let seen = Arc::new(Mutex::new(Vec::<String>::new()));

let seen_cl = Arc::clone(&seen);
bus.on("llm.call.start", move |event| {
    seen_cl.lock().unwrap().push(event.name.clone());
});

bus.emit("llm.call.start", serde_json::json!({"model": "claude-opus-4-7"}));
assert_eq!(seen.lock().unwrap().as_slice(), &["llm.call.start"]);

§Wildcard (firehose)

use agent_event_bus::EventBus;
use std::sync::{Arc, Mutex};

let bus = EventBus::new();
let counter = Arc::new(Mutex::new(0u32));
let cl = Arc::clone(&counter);
bus.on("*", move |_| { *cl.lock().unwrap() += 1; });
bus.emit("a", serde_json::Value::Null);
bus.emit("b", serde_json::Value::Null);
assert_eq!(*counter.lock().unwrap(), 2);

§Not a real message queue

No persistence, no cross-process delivery, no backpressure. Reach for Redis, NATS, or RabbitMQ if you need those. This crate is just clean wiring inside one Rust process.

Structs§

Event
A single dispatched event.
EventBus
In-process pub/sub bus.
Subscription
Opaque handle returned from EventBus::on / EventBus::on_once.

Constants§

WILDCARD
Wildcard event name that matches every emit.

Type Aliases§

OnError
Boxed error callback, invoked once per handler that panics during EventBus::emit.