Skip to main content

agent_framework_redis/
lib.rs

1//! # agent-framework-redis
2//!
3//! Redis-backed [`HistoryProvider`](agent_framework_core::history::HistoryProvider)
4//! and [`ContextProvider`](agent_framework_core::memory::ContextProvider) for
5//! `agent-framework-rs`, porting `agent_framework_redis` from the Python
6//! Agent Framework.
7//!
8//! - [`RedisChatMessageStore`] — one Redis `LIST` per session,
9//!   JSON-serialized messages, optional automatic trimming. A close mirror
10//!   of the Python `RedisChatMessageStore`, adapted to the
11//!   [`HistoryProvider`](agent_framework_core::history::HistoryProvider)
12//!   contract (`before_run`/`after_run`) now that conversation history lives
13//!   in a context provider rather than on the session/thread itself.
14//! - [`RedisContextProvider`] — scoped long-term memory storage/retrieval.
15//!   Ports the Python `RedisProvider`'s *scoping* semantics
16//!   (application/agent/user/thread id). When the connected server has
17//!   RediSearch loaded (Redis Stack), retrieval is backed by a real
18//!   `FT.SEARCH` BM25 full-text index; otherwise it falls back to a
19//!   `SCAN`+token-match path over plain Redis. Vector/hybrid search is
20//!   **not** ported — see the [`context_provider`] module docs for the full
21//!   picture.
22//!
23//! Both types connect lazily: constructing them only parses the Redis URL
24//! (via [`redis::Client::open`]); the actual
25//! [`MultiplexedConnection`](redis::aio::MultiplexedConnection) is
26//! established on the first call that needs it.
27//!
28//! ```no_run
29//! use agent_framework_core::prelude::*;
30//! use agent_framework_redis::{RedisChatMessageStore, RedisContextProvider};
31//! use std::sync::Arc;
32//!
33//! # async fn demo(client: impl ChatClient + 'static) -> Result<()> {
34//! let store = RedisChatMessageStore::new("redis://127.0.0.1:6379", None)?
35//!     .with_max_messages(200);
36//!
37//! let memory = RedisContextProvider::new("redis://127.0.0.1:6379")?.with_user_id("user-42");
38//!
39//! let agent = Agent::builder(client)
40//!     .instructions("You are a helpful assistant.")
41//!     .context_provider(Arc::new(memory))
42//!     .build();
43//!
44//! let mut session = AgentSession::new().with_context_providers(vec![Arc::new(store)]);
45//! let response = agent
46//!     .run(vec![Message::user("Hello!")], Some(&mut session))
47//!     .await?;
48//! println!("{}", response.text());
49//! # Ok(())
50//! # }
51//! ```
52
53pub mod chat_message_store;
54pub mod context_provider;
55mod internal;
56
57pub use chat_message_store::{
58    RedisChatMessageStore, DEFAULT_KEY_PREFIX as DEFAULT_STORE_KEY_PREFIX,
59};
60pub use context_provider::{
61    RedisContextProvider, DEFAULT_CONTEXT_PROMPT,
62    DEFAULT_KEY_PREFIX as DEFAULT_PROVIDER_KEY_PREFIX, DEFAULT_LIMIT,
63};