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 = Result<(), AgentError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait;
fn get_value(&self, key: &str) -> Option<Value>;
fn keys(&self) -> Vec<&str>;
fn to_json(&self) -> Result<Value, AgentError>;
fn from_json(value: Value) -> Result<Self, AgentError>;
// Provided methods
fn apply_updates<'life0, 'life1, 'async_trait>(
&'life0 mut self,
updates: &'life1 [StateUpdate],
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>
where 'life0: 'async_trait,
'life1: 'async_trait,
Self: '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§
Sourcefn apply_update<'life0, 'life1, 'async_trait>(
&'life0 mut self,
key: &'life1 str,
value: Value,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn apply_update<'life0, 'life1, 'async_trait>(
&'life0 mut self,
key: &'life1 str,
value: Value,
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: '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.
Sourcefn get_value(&self, key: &str) -> Option<Value>
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.
Sourcefn to_json(&self) -> Result<Value, AgentError>
fn to_json(&self) -> Result<Value, AgentError>
Convert entire state to a JSON Value
Sourcefn from_json(value: Value) -> Result<Self, AgentError>
fn from_json(value: Value) -> Result<Self, AgentError>
Create state from a JSON Value
Provided Methods§
Sourcefn apply_updates<'life0, 'life1, 'async_trait>(
&'life0 mut self,
updates: &'life1 [StateUpdate],
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
fn apply_updates<'life0, 'life1, 'async_trait>(
&'life0 mut self,
updates: &'life1 [StateUpdate],
) -> Pin<Box<dyn Future<Output = Result<(), AgentError>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Self: 'async_trait,
Apply multiple updates
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.