1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! The node abstraction: units of work within a workflow.
//!
//! This module defines the [`AgentNode`] trait, the fundamental extension point
//! of the engine. Each node encapsulates a discrete piece of logic that reads
//! from and writes to the shared [`AgentState`]. Nodes are registered on a
//! [`StateGraphBuilder`](crate::StateGraphBuilder) and invoked by the execution
//! engine — either in batch ([`StateGraph::invoke`](crate::StateGraph::invoke))
//! or streamed ([`StateGraph::stream`](crate::StateGraph::stream)).
//!
//! Nodes at the same step may run **concurrently**, so implementations must be
//! thread-safe (`Send + Sync`).
use crateAgentState;
use crateLangGraphError;
use async_trait;
use Arc;
/// Trait for defining executable nodes in a workflow graph.
///
/// Implement this trait to create custom nodes that can be added to a [`StateGraphBuilder`](crate::StateGraphBuilder).
/// Each node represents a discrete unit of work in your workflow and has access to
/// the shared state.
///
/// # Type Parameters
///
/// - `S`: The state type that this node operates on. Must implement [`AgentState`].
///
/// # Requirements
///
/// - `S: AgentState`: The state must support get/set operations
/// - `S: Send`: State must be safe to transfer between threads
/// - `S: Sync`: State must be safe to share between threads
///
/// # Example
///
/// ```rust
/// use langgraph4rust::*;
/// use std::sync::Arc;
///
/// #[derive(Clone)]
/// struct CounterNode;
///
/// #[async_trait]
/// impl AgentNode<DefaultMemoryState> for CounterNode {
/// async fn apply(&self, state: Arc<DefaultMemoryState>) -> Result<(), LangGraphError> {
/// let count: i32 = state.get("count").await?.unwrap_or(0);
/// state.set("count", count + 1).await?;
/// Ok(())
/// }
/// }
/// ```
///
/// # Error Handling
///
/// Return a [`LangGraphError`] if the node encounters an error during execution.
/// This will propagate up and stop the graph execution.
///
/// # Concurrency Note
///
/// Nodes may be executed concurrently with other nodes (when they are at the same
/// level in the graph). Ensure that your implementation is thread-safe when accessing
/// shared resources beyond the provided state.