Skip to main content

StateGraph

Struct StateGraph 

Source
pub struct StateGraph<S: GraphState> { /* private fields */ }
Expand description

Builder for state graphs.

Use the fluent API to add nodes, edges, and compile the graph.

§Examples

ⓘ
use regula_core::{StateGraph, start, end, node_fn, router_fn, RouteOutput};

let graph = StateGraph::<MyState>::new()
    .add_node("agent", agent_node)
    .add_node("tools", tools_node)
    .add_edge(start(), "agent")
    .add_conditional_edges("agent", router_fn(|state| {
        if state.done {
            RouteOutput::end()
        } else {
            RouteOutput::one("tools")
        }
    }))
    .add_edge("tools", "agent")
    .compile(Default::default())?;

Implementations§

Source§

impl<S: GraphState> StateGraph<S>

Source

pub fn new() -> Self

Create a new empty state graph.

Source

pub fn add_node<N: Node<S> + 'static>( self, name: impl Into<NodeId>, node: N, ) -> Self

Add a node to the graph.

§Arguments
  • name: The unique name for this node.
  • node: The node implementation.
§Returns

The builder for chaining.

§Panics

Panics if a node with this name already exists. Use try_add_node for fallible insertion.

Source

pub fn try_add_node<N: Node<S> + 'static>( self, name: impl Into<NodeId>, node: N, ) -> Result<Self>

Try to add a node to the graph.

Returns an error if a node with this name already exists.

Source

pub fn add_edge(self, from: impl Into<NodeId>, to: impl Into<NodeId>) -> Self

Add a normal (fixed) edge between two nodes.

§Arguments
  • from: The source node (or start() for entry point).
  • to: The target node (or end() for termination).
Source

pub fn add_conditional_edges<R: EdgeRouter<S> + 'static>( self, from: impl Into<NodeId>, router: R, ) -> Self

Add conditional edges with a router function.

The router determines the next node(s) based on the current state.

§Arguments
  • from: The source node.
  • router: The routing logic.
Source

pub fn add_conditional_edges_with_map<R: EdgeRouter<S> + 'static>( self, from: impl Into<NodeId>, router: R, path_map: HashMap<String, NodeId>, ) -> Self

Add conditional edges with a path map.

The path map translates router string outputs to node IDs.

Source

pub fn set_entry_point(self, node: impl Into<NodeId>) -> Self

Set the entry point explicitly.

This is equivalent to add_edge(start(), node).

Source

pub fn set_finish_point(self, node: impl Into<NodeId>) -> Self

Set a finish point explicitly.

This is equivalent to add_edge(node, end()).

Source

pub fn interrupt_before(self, node: impl Into<NodeId>) -> Self

Add a node to interrupt before execution.

When the graph reaches this node, it will pause before executing and return to the caller.

Source

pub fn interrupt_after(self, node: impl Into<NodeId>) -> Self

Add a node to interrupt after execution.

When this node completes, the graph will pause and return to the caller.

Source

pub fn node_names(&self) -> impl Iterator<Item = &NodeId>

Get the list of node names.

Source

pub fn has_node(&self, name: &str) -> bool

Check if a node exists.

Source

pub fn compile(self, config: CompileConfig) -> Result<CompiledStateGraph<S>>

Compile the graph into an executable form.

This validates the graph structure and produces a CompiledStateGraph ready for execution.

Trait Implementations§

Source§

impl<S: Clone + GraphState> Clone for StateGraph<S>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: GraphState> Debug for StateGraph<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: GraphState> Default for StateGraph<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for StateGraph<S>

§

impl<S> !UnwindSafe for StateGraph<S>

§

impl<S> Freeze for StateGraph<S>
where IndexMap<NodeId, Arc<dyn Node<S>>>: Freeze, Vec<Edge<S>>: Freeze,

§

impl<S> Send for StateGraph<S>
where IndexMap<NodeId, Arc<dyn Node<S>>>: Send, Vec<Edge<S>>: Send,

§

impl<S> Sync for StateGraph<S>
where IndexMap<NodeId, Arc<dyn Node<S>>>: Sync, Vec<Edge<S>>: Sync,

§

impl<S> Unpin for StateGraph<S>
where IndexMap<NodeId, Arc<dyn Node<S>>>: Unpin, Vec<Edge<S>>: Unpin,

§

impl<S> UnsafeUnpin for StateGraph<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more