Skip to main content

rig_ecs/
lib.rs

1//! Rig's effect bus and agent runtime as entities and systems in a Bevy world.
2//!
3//! [`RigPlugin`] installs request assembly, dispatch, streaming, tool execution,
4//! memory, and retrieval. Hosts steer runs through components and save reflected
5//! state with [`checkpoint`]. Concrete recording and replay adapters live in
6//! `rig_cassette::ecs`; the optional `assets` feature loads prompts and tools.
7//!
8//! This crate is experimental: its API may change in minor releases. The
9//! runtime behaviour its tests pin is stated in
10//! [`CONTRACT.md`](https://github.com/0xPlaygrounds/rig/blob/main/crates/rig-ecs/CONTRACT.md).
11//!
12//! ```
13//! let mut app = bevy_app::App::new();
14//! app.add_plugins(rig_ecs::RigPlugin::default());
15//! app.update();
16//! ```
17
18pub mod agent;
19#[cfg(feature = "assets")]
20pub mod assets;
21pub mod bus;
22pub mod checkpoint;
23pub mod policy;
24pub mod prelude;
25pub mod reflect;
26pub mod systems;
27
28/// Installs bus and agent systems in [`bus::RigSchedule`] after `Update`, with
29/// task-driven wakeups and checkpoint type registration. Adds time and
30/// diagnostics plugins if absent.
31#[derive(Debug, Clone, Default)]
32pub struct RigPlugin {
33    /// The bus's configuration: the serving policy, the ambiguity level.
34    pub bus: bus::BusPlugin,
35}
36
37impl RigPlugin {
38    /// The runtime under `policy`.
39    pub fn with_policy(policy: rig_core::serve::ServingPolicy) -> Self {
40        Self {
41            bus: bus::BusPlugin::with_policy(policy),
42        }
43    }
44
45    /// Build the schedule with ambiguity detection at `level`.
46    #[must_use = "the setting applies to the returned value"]
47    pub fn ambiguity_detection(mut self, level: bevy_ecs::schedule::LogLevel) -> Self {
48        self.bus = self.bus.ambiguity_detection(level);
49        self
50    }
51}
52
53impl bevy_app::Plugin for RigPlugin {
54    fn build(&self, app: &mut bevy_app::App) {
55        if !app.is_plugin_added::<bevy_time::TimePlugin>() {
56            app.add_plugins(bevy_time::TimePlugin);
57        }
58        if !app.is_plugin_added::<bevy_diagnostic::DiagnosticsPlugin>() {
59            app.add_plugins(bevy_diagnostic::DiagnosticsPlugin);
60        }
61        app.add_plugins((self.bus.clone(), systems::AgentPlugin));
62        checkpoint::register_types(app.world_mut());
63    }
64}