weavegraph 0.7.0

Graph-driven, concurrent agent workflow framework with versioned state, deterministic barrier merges, and rich diagnostics.
Documentation
//! Edge types and routing predicates for conditional graph flow.

use crate::types::NodeKind;
use std::sync::Arc;

/// Predicate function for conditional edge routing.
///
/// Receives a [`StateSnapshot`](crate::state::StateSnapshot) and returns the names of target
/// nodes to execute next. Used with
/// [`GraphBuilder::add_conditional_edge`](crate::graphs::GraphBuilder::add_conditional_edge).
///
/// # Examples
///
/// ```
/// use weavegraph::graphs::EdgePredicate;
/// use weavegraph::types::NodeKind;
/// use std::sync::Arc;
///
/// let route: EdgePredicate = Arc::new(|snapshot| {
///     if snapshot.messages.len() > 5 {
///         vec![NodeKind::Custom("many_messages".into()).as_target()]
///     } else {
///         vec![NodeKind::Custom("few_messages".into()).as_target()]
///     }
/// });
/// ```
pub type EdgePredicate =
    Arc<dyn Fn(crate::state::StateSnapshot) -> Vec<String> + Send + Sync + 'static>;

/// A conditional edge that routes to nodes determined by a predicate.
///
/// When the scheduler encounters a conditional edge it calls the predicate
/// with the current state and dispatches to the returned target nodes.
///
/// # Examples
///
/// ```
/// use weavegraph::graphs::{ConditionalEdge, EdgePredicate};
/// use weavegraph::types::NodeKind;
/// use std::sync::Arc;
///
/// let predicate: EdgePredicate = Arc::new(|snapshot| {
///     if snapshot.messages.len() > 5 {
///         vec![NodeKind::Custom("many_messages".into()).as_target()]
///     } else {
///         vec![NodeKind::Custom("few_messages".into()).as_target()]
///     }
/// });
/// let edge = ConditionalEdge::new(NodeKind::Start, predicate);
/// ```
#[derive(Clone)]
pub struct ConditionalEdge {
    // Source node for this edge.
    from: NodeKind,
    // Routing predicate.
    predicate: EdgePredicate,
}

impl ConditionalEdge {
    /// Creates a conditional edge from `from` to the targets returned by `predicate`.
    pub fn new(from: impl Into<NodeKind>, predicate: EdgePredicate) -> Self {
        Self {
            from: from.into(),
            predicate,
        }
    }

    /// Returns the source node.
    pub fn from(&self) -> &NodeKind {
        &self.from
    }

    /// Returns the routing predicate.
    pub fn predicate(&self) -> &EdgePredicate {
        &self.predicate
    }
}