enact-core 0.0.2

Core agent runtime for Enact - Graph-Native AI agents
Documentation
//! Node module - units of execution in a graph

mod function;
mod llm;

use async_trait::async_trait;
use serde_json::Value;
use std::sync::Arc;

pub use function::FunctionNode;
pub use llm::LlmNode;

/// Node state - input/output passed between nodes
#[derive(Debug, Clone, Default)]
pub struct NodeState {
    pub data: Value,
}

impl NodeState {
    pub fn new() -> Self {
        Self { data: Value::Null }
    }

    pub fn from_value(data: Value) -> Self {
        Self { data }
    }

    /// Create a node from a string (use `FromStr` trait for `.parse()`)
    pub fn from_string(s: &str) -> Self {
        Self {
            data: Value::String(s.to_string()),
        }
    }

    pub fn as_str(&self) -> Option<&str> {
        self.data.as_str()
    }
}

/// Node trait - a single step in a graph
#[async_trait]
pub trait Node: Send + Sync {
    /// Node name (unique within graph)
    fn name(&self) -> &str;

    /// Execute the node
    async fn execute(&self, state: NodeState) -> anyhow::Result<NodeState>;
}

/// Boxed node for dynamic dispatch
pub type DynNode = Arc<dyn Node>;