#![forbid(unsafe_code)]
mod dispatcher;
mod driver;
mod handle;
mod registrar;
pub use dispatcher::{BusId, DispatchOptions, Dispatcher, EffectStream, Pending};
pub use driver::BusDriver;
pub(crate) use driver::Recording;
pub(crate) use handle::wrap_stream;
pub use handle::{
Completion, DispatchScope, EmbedHandle, Handle, IndexHandle, MemoryHandle, ModelHandle,
RerankHandle, Retrieval, ToolAnswer, ToolCall, ToolHandle, Typed,
};
pub use registrar::Registrar;
use rig_core::serve::ServingPolicy;
use std::sync::Arc;
const _: () = {
const fn assert_send_sync_static<T: Send + Sync + 'static>() {}
const fn assert_send_static<T: Send + 'static>() {}
assert_send_sync_static::<ModelHandle>();
assert_send_sync_static::<Dispatcher>();
assert_send_static::<Pending>();
assert_send_static::<EffectStream>();
};
#[derive(Debug, Clone, Copy)]
pub struct Bus;
impl Bus {
pub fn channel() -> (Dispatcher, Registrar, BusDriver) {
Self::channel_with(ServingPolicy::default())
}
pub fn channel_with(config: ServingPolicy) -> (Dispatcher, Registrar, BusDriver) {
let shared = Arc::new(dispatcher::Shared::new(config));
let mailbox = Arc::new(registrar::Mailbox::new());
let dispatcher = Dispatcher::open(shared.clone(), config.stream_capacity.max(1));
let driver = BusDriver::new(shared, mailbox, config);
let registrar = driver.registrar();
(dispatcher, registrar, driver)
}
pub fn new_with(
config: ServingPolicy,
register: impl FnOnce(&mut BusDriver),
spawn: impl FnOnce(BusDriver),
) -> (Dispatcher, Registrar) {
let (dispatcher, registrar, mut driver) = Self::channel_with(config);
register(&mut driver);
spawn(driver);
(dispatcher, registrar)
}
}
#[cfg(all(test, rig_loom))]
mod loom_models;
#[cfg(all(test, not(rig_loom)))]
mod tests;