Skip to main content

mecha10_core/context/
mod.rs

1//! Node execution context
2//!
3//! Provides a high-level API for nodes to interact with the framework,
4//! including type-safe pub/sub, configuration access, and lifecycle management.
5//!
6//! This module has been refactored into smaller sub-modules for better organization:
7//! - `infra_config`: Infrastructure configuration types
8//! - `receiver`: Message receiver wrapper
9//! - `builder`: Builder for constructing a `Context`
10//! - `lifecycle`: Construction, instance naming, shutdown, logging span
11//! - `config`: Infrastructure configuration loading (Redis URLs, control plane)
12//! - `node_config`: Node-level configuration loading with environment fallback
13//! - `pubsub`: Type-safe and raw publish/subscribe methods
14//! - `aggregation`: Pattern-based topic discovery and aggregated subscriptions
15//! - `topics`: Convenience helpers for well-known topic categories
16//! - `state_mgmt`: Access to the per-node persistent state manager
17//! - `blackboard`: Access to the per-tree, non-persistent, in-memory scratch store
18
19mod aggregation;
20mod blackboard;
21mod builder;
22mod config;
23mod infra_config;
24mod lifecycle;
25mod node_config;
26mod pubsub;
27mod receiver;
28mod state_mgmt;
29mod topics;
30
31pub use builder::ContextBuilder;
32pub use infra_config::{BridgeConfig, InfrastructureConfig, MinioConfig, MlflowConfig, PostgresConfig, RedisConfig};
33pub use node_config::split_base_and_custom;
34pub use receiver::Receiver;
35
36use crate::blackboard::Blackboard;
37use std::sync::Arc;
38use tokio::sync::RwLock;
39
40pub struct Context {
41    node_id: String,
42
43    instance: Option<String>,
44
45    #[cfg(feature = "messaging")]
46    message_bus: Arc<RwLock<mecha10_messaging::MessageBus>>,
47
48    #[cfg(feature = "messaging")]
49    control_plane_bus: Arc<RwLock<Option<mecha10_messaging::MessageBus>>>,
50
51    #[cfg(feature = "messaging")]
52    redis_bridge: Arc<RwLock<Option<crate::redis_bridge::RedisBridge>>>,
53
54    shutdown: Arc<RwLock<bool>>,
55
56    state_manager: Arc<RwLock<Option<Arc<crate::state::ConcreteStateManager>>>>,
57
58    // Synchronous, non-persistent, in-memory scratch store shared by sibling nodes within a
59    // single running behavior tree. Deliberately separate from `state_manager` above, which is
60    // the persistent-across-restarts mechanism. See `crate::blackboard` for details.
61    blackboard: Blackboard,
62}
63impl Clone for Context {
64    fn clone(&self) -> Self {
65        Self {
66            node_id: self.node_id.clone(),
67            instance: self.instance.clone(),
68            #[cfg(feature = "messaging")]
69            message_bus: Arc::clone(&self.message_bus),
70            #[cfg(feature = "messaging")]
71            control_plane_bus: Arc::clone(&self.control_plane_bus),
72            #[cfg(feature = "messaging")]
73            redis_bridge: Arc::clone(&self.redis_bridge),
74            shutdown: Arc::clone(&self.shutdown),
75            state_manager: Arc::clone(&self.state_manager),
76            blackboard: self.blackboard.clone(),
77        }
78    }
79}