lellm_graph/node/
node_core.rs1use std::sync::Arc;
29
30use async_trait::async_trait;
31
32pub use super::node_context::LeafContext;
33use super::node_context::NodeContext;
34use crate::error::GraphError;
35use crate::exec::execution_engine::ExecutionEngine;
36use crate::state::workflow_state::{MergeStrategy, WorkflowState};
37use crate::state::{State, StateMerge};
38
39pub use super::barrier_node::{BarrierDefaultAction, BarrierNode};
42pub use super::parallel_node::{ParallelErrorStrategy, ParallelNode, ParallelNodeBuilder};
43
44#[async_trait]
61pub trait LeafNode<S: WorkflowState = State>: Send + Sync {
62 async fn execute(&self, ctx: &mut LeafContext<'_, S>) -> Result<(), GraphError>;
64}
65
66#[async_trait]
76pub trait ExecutorOperation<S: WorkflowState = State>: Send + Sync {
77 async fn execute(&self, engine: &mut ExecutionEngine<'_, S>) -> Result<(), GraphError>;
79}
80
81#[deprecated(
97 since = "0.6.0",
98 note = "Use LeafNode (read-only state + mutations) or ExecutorOperation (composite control) instead."
99)]
100#[async_trait]
101pub trait FlowNode<S: WorkflowState = State>: Send + Sync {
102 async fn execute(&self, ctx: &mut NodeContext<'_, S>) -> Result<(), GraphError>;
104}
105
106#[allow(deprecated)]
118pub enum NodeKind<S: WorkflowState = State, M: MergeStrategy<S> = StateMerge> {
119 Task(TaskNode<S>),
121 Condition(ConditionNode<S>),
123 Barrier(BarrierNode<S>),
125 Parallel(ParallelNode<S, M>),
127 External(Arc<dyn FlowNode<S>>),
131 ExternalLeaf(Arc<dyn LeafNode<S>>),
133 Subgraph(super::compiled_subgraph::CompiledSubgraph<S>),
138}
139
140#[allow(deprecated)]
141impl<S: WorkflowState, M: MergeStrategy<S>> Clone for NodeKind<S, M> {
142 fn clone(&self) -> Self {
143 match self {
144 Self::Task(n) => Self::Task(n.clone()),
145 Self::Condition(n) => Self::Condition(n.clone()),
146 Self::Barrier(n) => Self::Barrier(n.clone()),
147 Self::Parallel(n) => Self::Parallel(n.clone()),
148 Self::External(n) => Self::External(n.clone()),
149 Self::ExternalLeaf(n) => Self::ExternalLeaf(n.clone()),
150 Self::Subgraph(n) => Self::Subgraph(n.clone()),
151 }
152 }
153}
154
155pub type TaskFn<S> = Arc<dyn Fn(&mut NodeContext<'_, S>) -> Result<(), GraphError> + Send + Sync>;
159
160#[derive(Clone)]
162pub struct TaskNode<S: WorkflowState = State> {
163 pub name: String,
164 pub func: TaskFn<S>,
165}
166
167impl<S: WorkflowState> TaskNode<S> {
168 pub fn new(
169 name: impl Into<String>,
170 func: impl Fn(&mut NodeContext<'_, S>) -> Result<(), GraphError> + Send + Sync + 'static,
171 ) -> Self {
172 Self {
173 name: name.into(),
174 func: Arc::new(func),
175 }
176 }
177}
178
179#[allow(deprecated)]
183#[async_trait]
184impl<S: WorkflowState> FlowNode<S> for TaskNode<S> {
185 async fn execute(&self, ctx: &mut NodeContext<'_, S>) -> Result<(), GraphError> {
186 (self.func)(ctx)
187 }
188}
189
190pub type BranchCondition<S> = Arc<dyn Fn(&S) -> bool + Send + Sync>;
194
195#[derive(Clone)]
197pub struct ConditionNode<S: WorkflowState = State> {
198 pub name: String,
199 pub branches: Vec<(String, BranchCondition<S>)>,
200}
201
202impl<S: WorkflowState> ConditionNode<S> {
203 pub fn builder(name: impl Into<String>) -> ConditionNodeBuilder<S> {
204 ConditionNodeBuilder {
205 name: name.into(),
206 branches: Vec::new(),
207 }
208 }
209}
210
211pub struct ConditionNodeBuilder<S: WorkflowState = State> {
213 name: String,
214 branches: Vec<(String, BranchCondition<S>)>,
215}
216
217impl<S: WorkflowState> ConditionNodeBuilder<S> {
218 pub fn branch(
219 mut self,
220 target: impl Into<String>,
221 condition: impl Fn(&S) -> bool + Send + Sync + 'static,
222 ) -> Self {
223 self.branches.push((target.into(), Arc::new(condition)));
224 self
225 }
226
227 pub fn build(self) -> ConditionNode<S> {
228 ConditionNode {
229 name: self.name,
230 branches: self.branches,
231 }
232 }
233}
234
235#[async_trait]
237impl<S: WorkflowState> LeafNode<S> for ConditionNode<S> {
238 async fn execute(&self, ctx: &mut LeafContext<'_, S>) -> Result<(), GraphError> {
239 let state = ctx.state();
240 for (target, condition) in &self.branches {
241 if condition(state) {
242 ctx.goto(target);
243 return Ok(());
244 }
245 }
246 Ok(())
247 }
248}
249
250#[allow(deprecated)]
252#[async_trait]
253impl<S: WorkflowState> FlowNode<S> for ConditionNode<S> {
254 async fn execute(&self, ctx: &mut NodeContext<'_, S>) -> Result<(), GraphError> {
255 let state = ctx.state();
256 for (target, condition) in &self.branches {
257 if condition(state) {
258 ctx.goto(target);
259 return Ok(());
260 }
261 }
262 Ok(())
263 }
264}
265
266#[allow(deprecated)]
270pub type GraphNode<S> = dyn FlowNode<S>;