Skip to main content

RedisChatMessageStore

Struct RedisChatMessageStore 

Source
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

Source

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.

Source

pub fn with_key_prefix(self, key_prefix: impl Into<String>) -> Self

Namespace Redis keys under key_prefix (builder style). Defaults to "chat_messages".

Source

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).

Source

pub fn session_id(&self) -> &str

This store’s session id (auto-generated if not supplied to Self::new).

Source

pub fn key_prefix(&self) -> &str

The configured key prefix.

Source

pub fn max_messages(&self) -> Option<usize>

The configured message limit, if any.

Source

pub fn redis_key(&self) -> String

The Redis key holding this session’s messages: {key_prefix}:{session_id}.

Source

pub async fn clear(&self) -> Result<()>

Remove all messages for this session (DEL on Self::redis_key).

Source

pub async fn ping(&self) -> bool

Ping the Redis server, returning true on success. Equivalent to the Python store’s ping() convenience method.

Source

pub async fn list_messages(&self) -> Result<Vec<Message>>

The stored messages, in chronological order (LRANGE 0 -1).

Source

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.

Source

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.

Source

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

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,

Called before the model is invoked; mutate ctx to inject instructions, messages, and/or tools. Read ctx.input_messages / ctx.session_id.
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,

Called after an invocation completes, on BOTH success and failure. On success, error is None and response_messages holds the output. On failure, error is Some and response_messages is empty.
Source§

fn is_history_provider(&self) -> bool

Whether this provider manages conversation history (a 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.
Source§

impl HistoryProvider for RedisChatMessageStore

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more