Skip to main content

enact_core/graph/node/
mod.rs

1//! Node module - units of execution in a graph
2
3mod function;
4mod llm;
5
6use async_trait::async_trait;
7use serde_json::Value;
8use std::sync::Arc;
9
10pub use function::FunctionNode;
11pub use llm::LlmNode;
12
13/// Node state - input/output passed between nodes
14#[derive(Debug, Clone, Default)]
15pub struct NodeState {
16    pub data: Value,
17}
18
19impl NodeState {
20    pub fn new() -> Self {
21        Self { data: Value::Null }
22    }
23
24    pub fn from_value(data: Value) -> Self {
25        Self { data }
26    }
27
28    /// Create a node from a string (use `FromStr` trait for `.parse()`)
29    pub fn from_string(s: &str) -> Self {
30        Self {
31            data: Value::String(s.to_string()),
32        }
33    }
34
35    pub fn as_str(&self) -> Option<&str> {
36        self.data.as_str()
37    }
38}
39
40/// Node trait - a single step in a graph
41#[async_trait]
42pub trait Node: Send + Sync {
43    /// Node name (unique within graph)
44    fn name(&self) -> &str;
45
46    /// Execute the node
47    async fn execute(&self, state: NodeState) -> anyhow::Result<NodeState>;
48}
49
50/// Boxed node for dynamic dispatch
51pub type DynNode = Arc<dyn Node>;