Expand description
Context and state management for workflows.
This module provides thread-safe state management across workflow tasks, including regular data storage and specialized chat history management.
All Context methods are synchronous: the underlying storage is an
in-memory map guarded by short critical sections, so there is nothing to
await. Methods can be called freely from both async tasks and sync code
(e.g. edge-condition closures).
§Examples
§Basic Context Usage
use graph_flow::Context;
let context = Context::new();
// Store different types of data
context.set("user_id", 12345)?;
context.set("name", "Alice".to_string())?;
context.set("active", true)?;
// Retrieve data with type safety
let user_id: Option<i32> = context.get("user_id");
let name: Option<String> = context.get("name");
let active: Option<bool> = context.get("active");§Chat History Management
use graph_flow::Context;
let context = Context::new();
// Add messages to chat history
context.add_user_message("Hello, assistant!".to_string());
context.add_assistant_message("Hello! How can I help you?".to_string());
context.add_system_message("User session started".to_string());
// Get chat history
let history = context.get_chat_history();
let all_messages = context.get_all_messages();
let last_5 = context.get_last_messages(5);
// Check history status
let count = context.chat_history_len();
let is_empty = context.is_chat_history_empty();§Context with Message Limits
use graph_flow::Context;
// Create context with maximum 100 messages
let context = Context::with_max_chat_messages(100);
// Messages will be automatically pruned when limit is exceeded
for i in 0..150 {
context.add_user_message(format!("Message {}", i));
}
// Only the last 100 messages are kept
assert_eq!(context.chat_history_len(), 100);§LLM Integration (with rig feature)
use graph_flow::Context;
let context = Context::new();
context.add_user_message("What is the capital of France?".to_string());
context.add_assistant_message("The capital of France is Paris.".to_string());
// Get messages in rig format for LLM calls
let rig_messages = context.get_rig_messages();
let recent_messages = context.get_last_rig_messages(10);
// Use with rig's completion API
// let response = agent.chat(&user_input, rig_messages).await?;Structs§
- Chat
History - Container for managing chat history with serialization support.
- Context
- Context for sharing data between tasks in a graph execution.
- Serializable
Message - A serializable message that can be converted to/from rig::completion::Message.
Enums§
- Message
Role - Represents the role of a message in a conversation.