pub struct SharedState { /* private fields */ }Expand description
Shared state for graph workflows
Nodes read/write arbitrary key-value pairs via get / set, and access structured
conversation history via messages(). Internally uses Arc<RwLock>, safe to share
across nodes and threads.
§Example
use echo_orchestration::workflow::SharedState;
let state = SharedState::new();
state.set("count", 42);
state.set("name", "echo");
assert_eq!(state.get::<i64>("count"), Some(42));
assert_eq!(state.get::<String>("name"), Some("echo".to_string()));Implementations§
Sourcepub fn new() -> SharedState
pub fn new() -> SharedState
Create an empty state
Sourcepub fn from_values(values: HashMap<String, Value>) -> SharedState
pub fn from_values(values: HashMap<String, Value>) -> SharedState
Create from existing data
Sourcepub fn from_snapshot(snapshot: &str) -> Result<SharedState, Error>
pub fn from_snapshot(snapshot: &str) -> Result<SharedState, Error>
Restore from a snapshot
Sourcepub fn set<T>(&self, key: impl Into<String>, value: T) -> Result<(), StateError>where
T: Serialize,
pub fn set<T>(&self, key: impl Into<String>, value: T) -> Result<(), StateError>where
T: Serialize,
Set a value (auto-serialized to JSON).
Returns Result (StateResult<()>) – no longer panics on serialization failure or lock poison.
For a backward-compatible void-returning API, use [Self::set_unwrap].
Sourcepub fn set_best_effort<T>(&self, key: impl Into<String>, value: T) -> Option<()>where
T: Serialize,
pub fn set_best_effort<T>(&self, key: impl Into<String>, value: T) -> Option<()>where
T: Serialize,
Backward-compatible: set value, returns Option<()> on failure (no panic)
Sourcepub fn get<T>(&self, key: &str) -> Option<T>where
T: for<'de> Deserialize<'de>,
pub fn get<T>(&self, key: &str) -> Option<T>where
T: for<'de> Deserialize<'de>,
Get a value (auto-deserialized)
Sourcepub fn push_message(&self, msg: Message) -> Result<(), StateError>
pub fn push_message(&self, msg: Message) -> Result<(), StateError>
Push a message, returns Result
Sourcepub fn message_count(&self) -> usize
pub fn message_count(&self) -> usize
Get message count
Sourcepub fn clear_messages(&self) -> Result<(), StateError>
pub fn clear_messages(&self) -> Result<(), StateError>
Clear messages, returns Result
Sourcepub fn current_node(&self) -> Option<String>
pub fn current_node(&self) -> Option<String>
Get the current node
Sourcepub fn fork(&self) -> Result<SharedState, StateError>
pub fn fork(&self) -> Result<SharedState, StateError>
Create an isolated copy for branch execution.
Sourcepub fn snapshot(&self) -> Result<String, StateError>
pub fn snapshot(&self) -> Result<String, StateError>
Export as JSON snapshot, returns Result (no longer panics)
Sourcepub fn snapshot_unwrap(&self) -> String
pub fn snapshot_unwrap(&self) -> String
Convenience method: export as JSON snapshot, unwraps on failure
Sourcepub fn to_json_value(&self) -> Result<Value, StateError>
pub fn to_json_value(&self) -> Result<Value, StateError>
Export as JSON Value, returns Result
Sourcepub fn merge(&self, other: &SharedState) -> Result<(), StateError>
pub fn merge(&self, other: &SharedState) -> Result<(), StateError>
Merge values from another state (does not overwrite existing keys), returns Result
Sourcepub fn merge_overwrite(&self, other: &SharedState) -> Result<(), StateError>
pub fn merge_overwrite(&self, other: &SharedState) -> Result<(), StateError>
Merge values from another state (overwrites existing keys), returns Result
Sourcepub fn deep_merge(&self, other: &SharedState) -> Result<(), StateError>
pub fn deep_merge(&self, other: &SharedState) -> Result<(), StateError>
Deep merge values from another state (recursively merge nested structures), returns Result
Unlike merge_overwrite: when a key’s value is a JSON object in both states,
this method recursively merges the two object’s fields rather than wholesale overwrite.
Useful for maintaining data consistency when parallel branches modify nested structures
in the same state.
Trait Implementations§
Source§fn clone(&self) -> SharedState
fn clone(&self) -> SharedState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§fn default() -> SharedState
fn default() -> SharedState
Auto Trait Implementations§
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request