pub struct RedisChatMessageStore { /* private fields */ }Expand description
Redis-backed HistoryProvider: one Redis LIST per session,
JSON-serialized messages, optional automatic trimming.
use agent_framework_redis::RedisChatMessageStore;
let store = RedisChatMessageStore::new("redis://127.0.0.1:6379", None)?
.with_key_prefix("my_app")
.with_max_messages(100);
store.add_messages(vec![agent_framework_core::types::Message::user("hello")]).await?;
let history = store.list_messages().await?;
println!("{} messages for session {}", history.len(), store.session_id());Implementations§
Source§impl RedisChatMessageStore
impl RedisChatMessageStore
Sourcepub fn new(
redis_url: impl Into<String>,
session_id: Option<String>,
) -> Result<Self>
pub fn new( redis_url: impl Into<String>, session_id: Option<String>, ) -> Result<Self>
Create a store for redis_url, optionally pinned to an existing
session_id. When session_id is None a fresh id is generated as
thread_{uuid}, matching the Python store’s f"thread_{uuid4()}".
The connection to Redis is not established here; only the URL is
parsed. Errors if redis_url cannot be parsed as a Redis connection
string.
Sourcepub fn with_key_prefix(self, key_prefix: impl Into<String>) -> Self
pub fn with_key_prefix(self, key_prefix: impl Into<String>) -> Self
Namespace Redis keys under key_prefix (builder style). Defaults to
"chat_messages".
Sourcepub fn with_max_messages(self, max_messages: usize) -> Self
pub fn with_max_messages(self, max_messages: usize) -> Self
Automatically trim the list to the most recent max_messages
entries after every add_messages call (builder style).
Sourcepub fn session_id(&self) -> &str
pub fn session_id(&self) -> &str
This store’s session id (auto-generated if not supplied to Self::new).
Sourcepub fn key_prefix(&self) -> &str
pub fn key_prefix(&self) -> &str
The configured key prefix.
Sourcepub fn max_messages(&self) -> Option<usize>
pub fn max_messages(&self) -> Option<usize>
The configured message limit, if any.
Sourcepub fn redis_key(&self) -> String
pub fn redis_key(&self) -> String
The Redis key holding this session’s messages: {key_prefix}:{session_id}.
Sourcepub async fn clear(&self) -> Result<()>
pub async fn clear(&self) -> Result<()>
Remove all messages for this session (DEL on Self::redis_key).
Sourcepub async fn ping(&self) -> bool
pub async fn ping(&self) -> bool
Ping the Redis server, returning true on success. Equivalent to the
Python store’s ping() convenience method.
Sourcepub async fn list_messages(&self) -> Result<Vec<Message>>
pub async fn list_messages(&self) -> Result<Vec<Message>>
The stored messages, in chronological order (LRANGE 0 -1).
Sourcepub async fn add_messages(&self, messages: Vec<Message>) -> Result<()>
pub async fn add_messages(&self, messages: Vec<Message>) -> Result<()>
Append messages (RPUSH, chronological order), then trim to
Self::max_messages via LTRIM when configured. A no-op for an
empty messages.
Sourcepub fn to_dict(&self) -> Value
pub fn to_dict(&self) -> Value
Serialize this store’s configuration (session id, Redis URL, key
prefix, message limit) rather than the message contents — Redis
already persists the messages durably, so only the pointer back to
them needs to survive. This mirrors the Python store’s serialize()
/ RedisStoreState, including the "type" discriminator field. No
I/O is involved, so — like
AgentSession::to_dict —
this is a plain, synchronous call.
Sourcepub fn from_dict(state: &Value) -> Result<Self>
pub fn from_dict(state: &Value) -> Result<Self>
Reconstruct a store from a value previously produced by
Self::to_dict.
Trait Implementations§
Source§impl ContextProvider for RedisChatMessageStore
impl ContextProvider for RedisChatMessageStore
Source§fn before_run<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 mut SessionContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn before_run<'life0, 'life1, 'async_trait>(
&'life0 self,
ctx: &'life1 mut SessionContext,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn after_run<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
request_messages: &'life1 [Message],
response_messages: &'life2 [Message],
error: Option<&'life3 Error>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn after_run<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
request_messages: &'life1 [Message],
response_messages: &'life2 [Message],
error: Option<&'life3 Error>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Source§fn is_history_provider(&self) -> bool
fn is_history_provider(&self) -> bool
HistoryProvider). Agent
and WorkflowAgent use this to
detect an already-attached history provider among a session’s
context_providers and avoid auto-attaching a redundant
InMemoryHistoryProvider.
Defaults to false; history providers override it to true.