Skip to main content

lc_langgraph/
state.rs

1// crates/lc-langgraph/src/state.rs
2//! State management for LangGraph
3//!
4//! This module provides the state abstraction for graph execution.
5//! States are data structures that flow through nodes in the graph.
6
7use crate::errors::{GraphError, GraphResult};
8use serde::{Deserialize, Serialize};
9use std::collections::HashMap;
10use std::fmt::Debug;
11
12/// State Schema trait
13pub trait StateSchema:
14    Clone + Send + Sync + 'static + Serialize + for<'de> Deserialize<'de> + Debug
15{
16    /// Create initial state from input
17    fn from_input(input: Self) -> Self {
18        input
19    }
20
21    /// Get state as JSON for debugging/checkpointing.
22    ///
23    /// Returns a `Result` instead of silently producing `Value::Null` (Q2):
24    /// a serialization failure is data corruption in the state machine and
25    /// must surface at the call site, not be swallowed.
26    fn to_json(&self) -> GraphResult<serde_json::Value> {
27        serde_json::to_value(self).map_err(|e| {
28            GraphError::StateError(format!("Failed to serialize state to JSON: {}", e))
29        })
30    }
31}
32
33/// State update representation
34///
35/// Nodes return StateUpdate which contains partial updates to the state.
36/// The reducer pattern determines how updates are merged into the full state.
37#[derive(Debug, Clone, Serialize)]
38pub struct StateUpdate<S: StateSchema> {
39    /// Full or partial state update
40    pub update: Option<S>,
41
42    /// Additional metadata (for debugging/tracing)
43    pub metadata: HashMap<String, serde_json::Value>,
44}
45
46impl<S: StateSchema> StateUpdate<S> {
47    /// Create a full state update
48    pub fn full(state: S) -> Self {
49        Self {
50            update: Some(state),
51            metadata: HashMap::new(),
52        }
53    }
54
55    /// Create update with metadata
56    pub fn with_metadata(state: S, metadata: HashMap<String, serde_json::Value>) -> Self {
57        Self {
58            update: Some(state),
59            metadata,
60        }
61    }
62
63    /// Create a no-change update (for nodes that don't modify state)
64    pub fn unchanged() -> Self {
65        Self {
66            update: None,
67            metadata: HashMap::new(),
68        }
69    }
70
71    /// Add metadata entry
72    pub fn add_metadata(&mut self, key: String, value: serde_json::Value) {
73        self.metadata.insert(key, value);
74    }
75}
76
77/// Reducer trait for merging state updates
78///
79/// Reducers define how state updates are merged into the current state.
80/// This enables patterns like `add_messages` which appends rather than replaces.
81pub trait Reducer<S: StateSchema>: Send + Sync {
82    /// Reduce current state with an update
83    fn reduce(&self, current: &S, update: &S) -> S;
84}
85
86/// Default reducer that replaces state entirely
87pub struct ReplaceReducer;
88
89impl<S: StateSchema> Reducer<S> for ReplaceReducer {
90    fn reduce(&self, _current: &S, update: &S) -> S {
91        update.clone()
92    }
93}
94
95/// Append reducer for vector fields (like add_messages pattern)
96///
97/// This reducer appends new items to vector fields in the state.
98/// Useful for message history, steps history, etc.
99pub struct AppendReducer<S: StateSchema, T: Clone + Send + Sync> {
100    pub field_accessor: fn(&S) -> &[T],
101    pub field_mutator: fn(&mut S, Vec<T>),
102}
103
104impl<S: StateSchema, T: Clone + Send + Sync> Reducer<S> for AppendReducer<S, T> {
105    fn reduce(&self, current: &S, update: &S) -> S {
106        let current_items = (self.field_accessor)(current);
107        let update_items = (self.field_accessor)(update);
108
109        let mut merged: Vec<T> = current_items.to_vec();
110        merged.extend(update_items.iter().cloned());
111
112        let mut result = current.clone();
113        (self.field_mutator)(&mut result, merged);
114        result
115    }
116}
117
118/// AgentState Messages Reducer - Appends messages instead of replacing
119pub struct AppendMessagesReducer;
120
121impl Reducer<AgentState> for AppendMessagesReducer {
122    fn reduce(&self, current: &AgentState, update: &AgentState) -> AgentState {
123        let mut result = update.clone();
124        result.messages = current.messages.clone();
125        result.messages.extend(update.messages.iter().cloned());
126        result
127    }
128}
129
130/// AgentState Steps Reducer - Appends steps instead of replacing
131pub struct AppendStepsReducer;
132
133impl Reducer<AgentState> for AppendStepsReducer {
134    fn reduce(&self, current: &AgentState, update: &AgentState) -> AgentState {
135        let mut result = update.clone();
136        result.steps = current.steps.clone();
137        result.steps.extend(update.steps.iter().cloned());
138        result
139    }
140}
141
142/// Common state with messages (agent-style)
143///
144/// This provides a pre-built state schema for agent-style graphs
145/// that track messages through the execution.
146#[derive(Debug, Clone, Serialize, Deserialize)]
147pub struct AgentState {
148    /// Input query
149    pub input: String,
150
151    /// Chat messages history
152    pub messages: Vec<MessageEntry>,
153
154    /// Intermediate steps
155    pub steps: Vec<StepEntry>,
156
157    /// Output result
158    pub output: Option<String>,
159}
160
161impl StateSchema for AgentState {}
162
163impl AgentState {
164    /// Create new agent state with input
165    pub fn new(input: String) -> Self {
166        let msg = MessageEntry::human(input.clone());
167        Self {
168            input,
169            messages: vec![msg],
170            steps: vec![],
171            output: None,
172        }
173    }
174
175    /// Add a message to history
176    pub fn add_message(&mut self, message: MessageEntry) {
177        self.messages.push(message);
178    }
179
180    /// Add a step to history
181    pub fn add_step(&mut self, step: StepEntry) {
182        self.steps.push(step);
183    }
184
185    /// Set output
186    pub fn set_output(&mut self, output: String) {
187        self.output = Some(output);
188    }
189}
190
191/// Message entry for agent state
192#[derive(Debug, Clone, Serialize, Deserialize)]
193pub struct MessageEntry {
194    pub role: MessageRole,
195    pub content: String,
196}
197
198impl MessageEntry {
199    pub fn human(content: String) -> Self {
200        Self {
201            role: MessageRole::Human,
202            content,
203        }
204    }
205
206    pub fn ai(content: String) -> Self {
207        Self {
208            role: MessageRole::AI,
209            content,
210        }
211    }
212
213    pub fn system(content: String) -> Self {
214        Self {
215            role: MessageRole::System,
216            content,
217        }
218    }
219
220    pub fn tool(content: String) -> Self {
221        Self {
222            role: MessageRole::Tool,
223            content,
224        }
225    }
226}
227
228/// Message role types
229#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
230pub enum MessageRole {
231    System,
232    Human,
233    AI,
234    Tool,
235}
236
237/// Step entry for intermediate execution steps
238#[derive(Debug, Clone, Serialize, Deserialize)]
239pub struct StepEntry {
240    pub action: String,
241    pub observation: String,
242}
243
244impl StepEntry {
245    pub fn new(action: String, observation: String) -> Self {
246        Self {
247            action,
248            observation,
249        }
250    }
251}
252
253#[cfg(test)]
254mod tests {
255    use super::*;
256
257    #[test]
258    fn test_append_messages_reducer() {
259        let mut current = AgentState::new("Hello".to_string());
260        current.add_message(MessageEntry::ai("Response 1".to_string()));
261
262        let mut update = AgentState::new("Hello".to_string());
263        update.add_message(MessageEntry::ai("Response 2".to_string()));
264        update.set_output("Done".to_string());
265
266        let reducer = AppendMessagesReducer;
267        let result = reducer.reduce(&current, &update);
268
269        assert_eq!(result.messages.len(), 4);
270        assert_eq!(result.output, Some("Done".to_string()));
271    }
272
273    #[test]
274    fn test_append_steps_reducer() {
275        let mut current = AgentState::new("Test".to_string());
276        current.add_step(StepEntry::new(
277            "Action 1".to_string(),
278            "Result 1".to_string(),
279        ));
280
281        let mut update = AgentState::new("Test".to_string());
282        update.add_step(StepEntry::new(
283            "Action 2".to_string(),
284            "Result 2".to_string(),
285        ));
286
287        let reducer = AppendStepsReducer;
288        let result = reducer.reduce(&current, &update);
289
290        assert_eq!(result.steps.len(), 2);
291    }
292}