use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use brainwires_core::WorkingSet;
use brainwires_tool_runtime::{ToolExecutor, ToolPreHook};
use crate::agent_hooks::AgentLifecycleHooks;
use brainwires_agent::communication::CommunicationHub;
use brainwires_agent::file_locks::FileLockManager;
#[non_exhaustive]
pub struct AgentContext {
pub working_directory: String,
pub tool_executor: Arc<dyn ToolExecutor>,
pub communication_hub: Arc<CommunicationHub>,
pub file_lock_manager: Arc<FileLockManager>,
pub working_set: Arc<RwLock<WorkingSet>>,
pub metadata: HashMap<String, String>,
pub pre_execute_hook: Option<Arc<dyn ToolPreHook>>,
pub lifecycle_hooks: Option<Arc<dyn AgentLifecycleHooks>>,
}
impl AgentContext {
pub fn new(
working_directory: impl Into<String>,
tool_executor: Arc<dyn ToolExecutor>,
communication_hub: Arc<CommunicationHub>,
file_lock_manager: Arc<FileLockManager>,
) -> Self {
Self {
working_directory: working_directory.into(),
tool_executor,
communication_hub,
file_lock_manager,
working_set: Arc::new(RwLock::new(WorkingSet::new())),
metadata: HashMap::new(),
pre_execute_hook: None,
lifecycle_hooks: None,
}
}
pub fn with_working_set(
working_directory: impl Into<String>,
tool_executor: Arc<dyn ToolExecutor>,
communication_hub: Arc<CommunicationHub>,
file_lock_manager: Arc<FileLockManager>,
working_set: Arc<RwLock<WorkingSet>>,
) -> Self {
Self {
working_directory: working_directory.into(),
tool_executor,
communication_hub,
file_lock_manager,
working_set,
metadata: HashMap::new(),
pre_execute_hook: None,
lifecycle_hooks: None,
}
}
pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.metadata.insert(key.into(), value.into());
self
}
pub fn with_pre_execute_hook(mut self, hook: Arc<dyn ToolPreHook>) -> Self {
self.pre_execute_hook = Some(hook);
self
}
pub fn with_lifecycle_hooks(mut self, hooks: Arc<dyn AgentLifecycleHooks>) -> Self {
self.lifecycle_hooks = Some(hooks);
self
}
}