Skip to main content

NodeFunc

Trait NodeFunc 

Source
pub trait NodeFunc<S: GraphState>: Send + Sync {
    // Required methods
    fn call<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        state: &'life1 mut S,
        ctx: &'life2 RuntimeContext,
    ) -> Pin<Box<dyn Future<Output = AgentResult<Command>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn name(&self) -> &str;

    // Provided method
    fn description(&self) -> Option<&str> { ... }
}
Expand description

Node function trait

Implement this trait to define custom node behavior. Nodes receive the current state and runtime context, and return a Command that can update state and control flow.

§Example

use mofa_kernel::workflow::{NodeFunc, Command, RuntimeContext};

struct ProcessNode;

#[async_trait]
impl NodeFunc<MyState> for ProcessNode {
    async fn call(&self, state: &mut MyState, ctx: &RuntimeContext) -> AgentResult<Command> {
        // Process the state
        let input = state.messages.last().cloned().unwrap_or_default();

        // Return command with state update and control flow
        Ok(Command::new()
            .update("result", json!(format!("Processed: {}", input)))
            .goto("next_node"))
    }

    fn name(&self) -> &str {
        "process"
    }
}

Required Methods§

Source

fn call<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, state: &'life1 mut S, ctx: &'life2 RuntimeContext, ) -> Pin<Box<dyn Future<Output = AgentResult<Command>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Execute the node

§Arguments
  • state - Mutable reference to the current state
  • ctx - Runtime context with execution metadata
§Returns

A Command containing state updates and control flow directive

Source

fn name(&self) -> &str

Returns the node name/identifier

Provided Methods§

Source

fn description(&self) -> Option<&str>

Optional description of what this node does

Implementors§