pub struct Context { /* private fields */ }Expand description
Shared workflow context.
Cheaply clonable handle to the shared state. Every step receives a
Context and can read/write state, emit events, and publish to the
external stream.
State values are stored as JSON internally, enabling serialization for
pause/resume/checkpoint functionality. Users can still use ergonomic
typed accessors (set/get) as long as their types implement
Serialize/DeserializeOwned.
Implementations§
Source§impl Context
impl Context
Sourcepub async fn set<T>(&self, key: &str, value: T)
pub async fn set<T>(&self, key: &str, value: T)
Store a typed value under key.
The value is serialized to JSON before storage. Overwrites any previous value stored under the same key regardless of its type.
§Panics
Panics if the value cannot be serialized to JSON. In practice this should never happen for well-formed serde types.
Sourcepub async fn get<T>(&self, key: &str) -> Option<T>
pub async fn get<T>(&self, key: &str) -> Option<T>
Retrieve a typed value previously stored under key.
The stored JSON is deserialized back into type T. Returns None
if the key does not exist or the stored JSON cannot be deserialized
into T.
Sourcepub async fn set_value(&self, key: &str, value: StateValue)
pub async fn set_value(&self, key: &str, value: StateValue)
Store a raw StateValue directly.
Used by language bindings for polymorphic dispatch (e.g. storing
platform-serialized opaque objects via StateValue::Native).
Sourcepub async fn get_value(&self, key: &str) -> Option<StateValue>
pub async fn get_value(&self, key: &str) -> Option<StateValue>
Retrieve the raw StateValue stored under key.
Returns None if the key does not exist. Unlike get,
this returns the value regardless of its variant.
Sourcepub async fn set_bytes(&self, key: &str, data: Vec<u8>)
pub async fn set_bytes(&self, key: &str, data: Vec<u8>)
Store raw binary data under key.
Useful for files, images, audio, and other binary artifacts that should not be JSON-serialized.
Sourcepub async fn get_bytes(&self, key: &str) -> Option<Vec<u8>>
pub async fn get_bytes(&self, key: &str) -> Option<Vec<u8>>
Retrieve raw binary data previously stored under key.
Returns None if the key does not exist or the stored value is
a JSON variant rather than bytes.
Sourcepub async fn send_event<E>(&self, event: E)
pub async fn send_event<E>(&self, event: E)
Emit an event into the internal routing queue.
The event will be picked up by the event loop and routed to any
step whose accepts list includes its event type.
Sourcepub async fn write_event_to_stream<E>(&self, event: E)
pub async fn write_event_to_stream<E>(&self, event: E)
Publish an event to the external broadcast stream.
Consumers that called crate::WorkflowHandler::stream_events will
receive this event. Unlike send_event, this does
not route the event through the internal step registry.
Sourcepub async fn collect_events<E>(&self, expected_count: usize) -> Option<Vec<E>>where
E: Event + DeserializeOwned,
pub async fn collect_events<E>(&self, expected_count: usize) -> Option<Vec<E>>where
E: Event + DeserializeOwned,
Accumulate events of type E until expected_count are available.
Returns Some(Vec<E>) when exactly expected_count events have been
collected, or None if not enough have arrived yet.
Once the threshold is reached the internal buffer for this type is cleared automatically so a subsequent call starts fresh.
Sourcepub async fn snapshot_state(&self) -> HashMap<String, StateValue>
pub async fn snapshot_state(&self) -> HashMap<String, StateValue>
Returns a clone of the entire state map.
Useful for checkpointing or pausing a workflow so it can be resumed later.
Sourcepub async fn restore_state(&self, state: HashMap<String, StateValue>)
pub async fn restore_state(&self, state: HashMap<String, StateValue>)
Replace the state map wholesale.
Used to restore state from a previous checkpoint. Any existing state is discarded.
Sourcepub async fn snapshot_collected(&self) -> HashMap<String, Vec<Value>>
pub async fn snapshot_collected(&self) -> HashMap<String, Vec<Value>>
Returns a clone of the collected events map (serialized as JSON).
Useful for checkpointing fan-in state alongside the main state map.
Sourcepub async fn restore_collected(&self, collected: HashMap<String, Vec<Value>>)
pub async fn restore_collected(&self, collected: HashMap<String, Vec<Value>>)
Replace the collected events map wholesale.
Used to restore fan-in state from a previous checkpoint. Any existing collected events are discarded.
Sourcepub async fn snapshot_metadata(&self) -> HashMap<String, Value>
pub async fn snapshot_metadata(&self) -> HashMap<String, Value>
Returns a clone of the metadata map.
Useful for checkpointing metadata alongside the main state map.