Skip to main content

langgraph_core_rs/channels/
base.rs

1use serde_json::Value as JsonValue;
2use langgraph_checkpoint::error::ChannelError;
3
4/// The erased channel trait. All channel types implement this.
5///
6/// Values flow through as `serde_json::Value` for checkpoint compatibility.
7/// This is the critical type-erasure strategy that allows the Pregel engine
8/// to work with heterogeneous channel types.
9pub trait Channel: Send + Sync + 'static {
10    /// Return a serializable checkpoint of this channel's state.
11    /// Returns None if the channel is empty (MISSING).
12    fn checkpoint(&self) -> Option<JsonValue>;
13
14    /// Restore channel state from a checkpoint.
15    fn from_checkpoint(&self, checkpoint: Option<&JsonValue>) -> Box<dyn Channel>;
16
17    /// Apply a batch of updates. Returns true if the channel was modified.
18    fn update(&self, values: &[JsonValue]) -> Result<bool, ChannelError>;
19
20    /// Get the current value. Returns Err(EmptyChannel) if empty.
21    fn get(&self) -> Result<JsonValue, ChannelError>;
22
23    /// Notify that a subscribed task consumed the value.
24    /// Returns true if the channel was modified.
25    fn consume(&self) -> bool {
26        false
27    }
28
29    /// Notify that the Pregel run is finishing.
30    /// Returns true if the channel was modified.
31    fn finish(&self) -> bool {
32        false
33    }
34
35    /// Return true if the channel has a value available.
36    fn is_available(&self) -> bool;
37
38    /// Clone this channel (for checkpoint restoration).
39    fn clone_channel(&self) -> Box<dyn Channel>;
40
41    /// Return the name/key for this channel.
42    fn name(&self) -> &str;
43}