nenjo_eventbus/lib.rs
1//! # nenjo-eventbus
2//!
3//! Transport-agnostic event bus for the Nenjo agent platform.
4//!
5//! This crate provides [`EventBus`], a raw envelope transport for sending and
6//! receiving agent events. The underlying transport is pluggable via the
7//! [`Transport`] trait — enable the `nats` feature for a production-ready
8//! NATS JetStream implementation. For high-throughput workers, clone an
9//! [`EventBusPublisher`] from the bus and run outbound publishes separately
10//! from the inbound receive loop.
11//!
12//! ## Quick start (NATS)
13//!
14//! ```ignore
15//! use nenjo_eventbus::{EventBus, EventBusBuilder, Subscription};
16//! use nenjo_eventbus::nats::NatsTransport;
17//! use nenjo_events::Envelope;
18//!
19//! let transport = NatsTransport::builder()
20//! .urls(vec!["nats://localhost:4222".to_string()])
21//! .token("my-api-key")
22//! .build()
23//! .await?;
24//!
25//! let bus = EventBus::builder()
26//! .transport(transport)
27//! .subscription(Subscription::worker_commands(worker_id, capabilities))
28//! .build()
29//! .await?;
30//!
31//! // Send a raw envelope directly, or clone bus.publisher() for an outbound lane.
32//! let envelope = Envelope::new(user_id, serde_json::json!({ "type": "ping" }));
33//! bus.send_envelope("requests.chat", &envelope).await?;
34//!
35//! // Receive envelopes
36//! while let Some(received) = bus.recv_envelope().await? {
37//! println!("{:?}", received.envelope);
38//! received.ack().await?;
39//! }
40//! ```
41
42mod bus;
43mod error;
44mod transport;
45
46pub use bus::{EventBus, EventBusBuilder, EventBusPublisher, ReceivedEnvelope};
47pub use error::EventBusError;
48pub use transport::{AckHandle, Message, MessageSource, NoOpAck, Subscription, Transport};
49
50// Re-export event types for convenience.
51pub use nenjo_events::Envelope;
52
53#[cfg(feature = "nats")]
54pub mod nats;