mod handle;
mod parallel;
mod spawner;
mod subgraph_node;
pub use handle::{SubgraphHandle, SubgraphResult};
pub use parallel::{JoinStrategy, ParallelSubgraphs};
pub use spawner::SubgraphSpawner;
pub use subgraph_node::SubgraphNode;
use crate::state::AgentState;
pub type StateMapper = Box<dyn Fn(&AgentState) -> AgentState + Send + Sync>;
pub type ResultMerger = Box<dyn Fn(&mut AgentState, AgentState) + Send + Sync>;
pub fn clone_state() -> StateMapper {
Box::new(|parent| parent.clone())
}
pub fn extract_context(keys: Vec<String>) -> StateMapper {
Box::new(move |parent| {
let mut child = AgentState::new();
for key in &keys {
if let Some(value) = parent.context.get(key) {
child.context.insert(key.clone(), value.clone());
}
}
child.messages = parent.messages.clone();
child
})
}
pub fn merge_all_context() -> ResultMerger {
Box::new(|parent, child| {
for (key, value) in child.context {
parent.context.insert(key, value);
}
})
}
pub fn merge_context_keys(keys: Vec<String>) -> ResultMerger {
Box::new(move |parent, child| {
for key in &keys {
if let Some(value) = child.context.get(key) {
parent.context.insert(key.clone(), value.clone());
}
}
})
}
pub fn merge_under_namespace(namespace: String) -> ResultMerger {
Box::new(move |parent, child| {
if let Ok(child_json) = serde_json::to_value(&child.context) {
parent.context.insert(namespace.clone(), child_json);
}
})
}