Skip to main content

GraphState

Trait GraphState 

Source
pub trait GraphState:
    Clone
    + Send
    + Sync
    + 'static {
    // Required methods
    fn apply_update<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        key: &'life1 str,
        value: Value,
    ) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn get_value(&self, key: &str) -> Option<Value>;
    fn keys(&self) -> Vec<&str>;
    fn to_json(&self) -> AgentResult<Value>;
    fn from_json(value: Value) -> AgentResult<Self>;

    // Provided methods
    fn apply_updates<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        updates: &'life1 [StateUpdate],
    ) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn has_key(&self, key: &str) -> bool { ... }
}
Expand description

Graph state trait

Implement this trait to define custom state types for workflows. The trait provides methods for applying updates and serialization.

§Example

use serde::{Serialize, Deserialize};
use mofa_kernel::workflow::GraphState;

#[derive(Clone, Serialize, Deserialize)]
struct MyState {
    messages: Vec<String>,
    result: Option<String>,
}

impl GraphState for MyState {
    async fn apply_update(&mut self, key: &str, value: Value) -> AgentResult<()> {
        match key {
            "messages" => {
                if let Some(msg) = value.as_str() {
                    self.messages.push(msg.to_string());
                }
            }
            "result" => {
                self.result = value.as_str().map(|s| s.to_string());
            }
            _ => {}
        }
        Ok(())
    }

    fn get_value(&self, key: &str) -> Option<Value> {
        match key {
            "messages" => Some(serde_json::to_value(&self.messages).unwrap()),
            "result" => Some(serde_json::to_value(&self.result).unwrap()),
            _ => None,
        }
    }

    fn keys(&self) -> Vec<&str> {
        vec!["messages", "result"]
    }
}

Required Methods§

Source

fn apply_update<'life0, 'life1, 'async_trait>( &'life0 mut self, key: &'life1 str, value: Value, ) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Apply a state update

This method is called when a node returns state updates. The implementation should merge the update into the state.

Source

fn get_value(&self, key: &str) -> Option<Value>

Get a value by key

Returns the current value for a given key, or None if the key doesn’t exist.

Source

fn keys(&self) -> Vec<&str>

Get all keys in this state

Source

fn to_json(&self) -> AgentResult<Value>

Convert entire state to a JSON Value

Source

fn from_json(value: Value) -> AgentResult<Self>

Create state from a JSON Value

Provided Methods§

Source

fn apply_updates<'life0, 'life1, 'async_trait>( &'life0 mut self, updates: &'life1 [StateUpdate], ) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Apply multiple updates

Source

fn has_key(&self, key: &str) -> bool

Check if a key exists

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§