pub struct Context { /* private fields */ }Expand description
Context for sharing data between tasks in a graph execution.
Provides thread-safe storage for workflow state and dedicated chat history
management. The context is shared across all tasks in a workflow execution
(cloning a Context is cheap and yields a handle to the same state).
All methods are synchronous - see the module docs for rationale.
§Examples
use graph_flow::Context;
let context = Context::new();
// Typed key-value state
context.set("user_id", 12345)?;
let user_id: Option<i32> = context.get("user_id");
// Chat history
context.add_user_message("Hello".to_string());
let history = context.get_chat_history();Implementations§
Source§impl Context
impl Context
Sourcepub fn with_max_chat_messages(max: usize) -> Self
pub fn with_max_chat_messages(max: usize) -> Self
Create a new context with a maximum chat history size.
When the chat history exceeds this size, older messages are automatically removed.
Sourcepub fn set(&self, key: impl Into<String>, value: impl Serialize) -> Result<()>
pub fn set(&self, key: impl Into<String>, value: impl Serialize) -> Result<()>
Set a value in the context.
Returns Err(GraphError::ContextError) if the value cannot be
serialized to JSON (e.g. a map with non-string keys).
§Examples
use graph_flow::Context;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct UserData {
id: u32,
name: String,
}
let context = Context::new();
// Store primitive types
context.set("count", 42)?;
context.set("name", "Alice".to_string())?;
// Store complex types
let user = UserData { id: 1, name: "Bob".to_string() };
context.set("user", user)?;Sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
Get a value from the context.
Returns None if the key doesn’t exist or if the stored value cannot
be deserialized to the requested type (a warning is logged for the
latter, since it usually indicates a type mismatch bug).
§Examples
use graph_flow::Context;
let context = Context::new();
context.set("count", 42)?;
let count: Option<i32> = context.get("count");
assert_eq!(count, Some(42));
let missing: Option<String> = context.get("missing");
assert_eq!(missing, None);Sourcepub fn remove(&self, key: &str) -> Option<Value>
pub fn remove(&self, key: &str) -> Option<Value>
Remove a value from the context.
Returns the removed value if it existed.
Sourcepub fn add_user_message(&self, content: String)
pub fn add_user_message(&self, content: String)
Add a user message to the chat history.
Sourcepub fn add_assistant_message(&self, content: String)
pub fn add_assistant_message(&self, content: String)
Add an assistant message to the chat history.
Sourcepub fn add_system_message(&self, content: String)
pub fn add_system_message(&self, content: String)
Add a system message to the chat history.
Sourcepub fn get_chat_history(&self) -> ChatHistory
pub fn get_chat_history(&self) -> ChatHistory
Get a clone of the current chat history.
Sourcepub fn clear_chat_history(&self)
pub fn clear_chat_history(&self)
Clear the chat history.
Sourcepub fn chat_history_len(&self) -> usize
pub fn chat_history_len(&self) -> usize
Get the number of messages in the chat history.
Sourcepub fn is_chat_history_empty(&self) -> bool
pub fn is_chat_history_empty(&self) -> bool
Check if the chat history is empty.
Sourcepub fn get_last_messages(&self, n: usize) -> Vec<SerializableMessage>
pub fn get_last_messages(&self, n: usize) -> Vec<SerializableMessage>
Get the last N messages from chat history, oldest first.
Sourcepub fn get_all_messages(&self) -> Vec<SerializableMessage>
pub fn get_all_messages(&self) -> Vec<SerializableMessage>
Get all messages from chat history, oldest first.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Context
impl<'de> Deserialize<'de> for Context
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Auto Trait Implementations§
impl !RefUnwindSafe for Context
impl !UnwindSafe for Context
impl Freeze for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl UnsafeUnpin for Context
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,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
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 more