Skip to main content

Node

Trait Node 

Source
pub trait Node<S: State>: Send + Sync {
    // Required method
    fn invoke<'life0, 'life1, 'async_trait>(
        &'life0 self,
        state: S,
        config: &'life1 Config,
    ) -> Pin<Box<dyn Future<Output = Result<S>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

The core trait for graph nodes.

A node is a unit of computation that takes state and produces updated state. Nodes can be async functions, closures, or custom types implementing this trait.

§Example

use rust_langgraph::{Node, Config, Error};
use async_trait::async_trait;

#[derive(Clone)]
struct MyState {
    count: i32,
}

struct IncrementNode;

#[async_trait]
impl Node<MyState> for IncrementNode {
    async fn invoke(&self, mut state: MyState, _config: &Config) -> Result<MyState, Error> {
        state.count += 1;
        Ok(state)
    }
}

Required Methods§

Source

fn invoke<'life0, 'life1, 'async_trait>( &'life0 self, state: S, config: &'life1 Config, ) -> Pin<Box<dyn Future<Output = Result<S>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Execute the node with the given state and configuration.

§Arguments
  • state - The current state
  • config - Execution configuration
§Returns

The updated state or an error

Implementors§

Source§

impl<S, F, Fut> Node<S> for F
where S: State, F: Fn(S, &Config) -> Fut + Send + Sync, Fut: Future<Output = Result<S>> + Send,