Skip to main content

adk_memory/
lib.rs

1//! # adk-memory
2//!
3//! Semantic memory and search for ADK agents.
4//!
5//! ## Overview
6//!
7//! This crate provides long-term memory capabilities:
8//!
9//! - [`InMemoryMemoryService`] - Simple in-memory memory storage
10//! - [`MemoryService`] - Trait for custom backends
11//! - [`MemoryEntry`] - Structured memory with metadata
12//! - [`MemoryServiceAdapter`] - Bridge to [`adk_core::Memory`] with optional project scope
13//! - [`validate_project_id`] - Project identifier validation
14//!
15//! ## Quick Start
16//!
17//! ```rust
18//! use adk_core::Content;
19//! use adk_memory::{InMemoryMemoryService, MemoryEntry, MemoryService, SearchRequest};
20//!
21//! # #[tokio::main(flavor = "current_thread")]
22//! # async fn main() -> adk_core::Result<()> {
23//! let service = InMemoryMemoryService::new();
24//! let entry = MemoryEntry::new(Content::new("user").with_text("Hometown: Nairobi."), "user");
25//! service.add_session("app", "user", "session", vec![entry]).await?;
26//!
27//! let found = service
28//!     .search(SearchRequest {
29//!         query: "hometown".into(),
30//!         user_id: "user".into(),
31//!         app_name: "app".into(),
32//!         limit: None,
33//!         min_score: None,
34//!         project_id: None,
35//!     })
36//!     .await?;
37//! assert_eq!(found.memories.len(), 1);
38//! # Ok(())
39//! # }
40//! ```
41//!
42//! The runner attaches a memory service to each invocation (`ToolContext::search_memory`);
43//! `adk-tool`'s `LoadMemoryTool` and `PreloadMemoryTool` (feature `memory-tools`) expose it
44//! to the model.
45//!
46//! ## Project-Scoped Memory
47//!
48//! Memories can be isolated by project within a user. The isolation key is
49//! `(app_name, user_id, project_id?)`:
50//!
51//! - **Global entries** (`project_id = None`): visible in all contexts.
52//! - **Project entries** (`project_id = Some(id)`): visible only within that project.
53//! - **Project search**: returns global + matching project entries.
54//! - **Global search**: returns only global entries.
55//!
56//! ```rust,ignore
57//! use adk_memory::{InMemoryMemoryService, MemoryService, MemoryServiceAdapter};
58//! use std::sync::Arc;
59//!
60//! let service = Arc::new(InMemoryMemoryService::new());
61//!
62//! // Store entries in a project
63//! service.add_session_to_project("app", "user", "sess", "my-project", entries).await?;
64//!
65//! // Adapter scoped to a project
66//! let adapter = MemoryServiceAdapter::new(service, "app", "user")
67//!     .with_project_id("my-project");
68//! ```
69//!
70//! ## Features
71//!
72//! - Per-user and per-project memory isolation
73//! - Keyword search, and vector search with an `EmbeddingProvider` on PostgreSQL, MongoDB,
74//!   and Neo4j
75//! - Backends: InMemory, SQLite, PostgreSQL, Redis, MongoDB, Neo4j, Vertex AI Memory Bank,
76//!   and the SQLite knowledge graph `GraphMemoryService`
77//! - Versioned schema migrations
78//! - `delete_user` and `export_user` across all projects, and [`MemorySubjectData`] for
79//!   `adk_core::subject::SubjectRights`
80//! - Expiry on every backend: a per-entry expiry or a per-service default TTL, enforced on
81//!   read, purged by [`MemoryService::purge_expired`] (see [`expiry`]), and
82//!   [`MemoryService::delete_memory`] by id
83//!
84//! ## Feature flags
85//!
86//! The in-memory backend is always compiled; no feature is on by default.
87//!
88//! | Feature | Enables |
89//! |---------|---------|
90//! | `sqlite-memory` | The SQLite backend |
91//! | `database-memory` | The PostgreSQL backend, with pgvector semantic search |
92//! | `redis-memory` | The Redis backend |
93//! | `mongodb-memory` | The MongoDB backend |
94//! | `neo4j-memory` | The Neo4j backend |
95//! | `vertex-memory` | Vertex AI Memory Bank on the Gemini Enterprise Agent Platform |
96//! | `graph-memory` | `GraphMemoryService`, the SQLite knowledge graph; implies `sqlite-memory` |
97//! | `embedding-trait` | The `EmbeddingProvider` trait without a vector backend |
98//! | `conformance` | The `conformance` module and `memory_conformance_tests!`, the id and expiry suite for a backend's tests |
99
100pub mod adapter;
101#[cfg(feature = "conformance")]
102pub mod conformance;
103pub mod consent_gate;
104pub mod expiry;
105pub mod inmemory;
106pub mod migration;
107pub mod service;
108pub mod subject;
109pub mod text;
110
111#[cfg(any(
112    feature = "database-memory",
113    feature = "mongodb-memory",
114    feature = "neo4j-memory",
115    feature = "embedding-trait"
116))]
117pub mod embedding;
118#[cfg(feature = "graph-memory")]
119pub mod graph;
120#[cfg(any(feature = "redis-memory", feature = "neo4j-memory"))]
121mod key;
122#[cfg(feature = "mongodb-memory")]
123pub mod mongodb;
124#[cfg(feature = "neo4j-memory")]
125pub mod neo4j;
126#[cfg(feature = "database-memory")]
127pub mod postgres;
128#[cfg(feature = "redis-memory")]
129pub mod redis;
130#[cfg(feature = "sqlite-memory")]
131pub mod sqlite;
132#[cfg(feature = "vertex-memory")]
133pub mod vertex;
134
135pub use adapter::MemoryServiceAdapter;
136pub use consent_gate::ConsentGatedMemoryService;
137pub use inmemory::InMemoryMemoryService;
138pub use service::{
139    MemoryEntry, MemoryService, SearchRequest, SearchResponse, UserMemory, validate_project_id,
140};
141pub use subject::MemorySubjectData;
142
143#[cfg(any(
144    feature = "database-memory",
145    feature = "mongodb-memory",
146    feature = "neo4j-memory",
147    feature = "embedding-trait"
148))]
149pub use embedding::EmbeddingProvider;
150#[cfg(feature = "graph-memory")]
151pub use graph::{
152    CreateEntityInput, CreateRelationInput, Entity, GraphMemoryService, GraphSearchResult,
153    Observation, Relation,
154};
155#[cfg(feature = "mongodb-memory")]
156pub use mongodb::MongoMemoryService;
157#[cfg(feature = "neo4j-memory")]
158pub use neo4j::Neo4jMemoryService;
159#[cfg(feature = "database-memory")]
160pub use postgres::{PostgresMemoryService, PostgresMemoryServiceBuilder, VectorIndexType};
161#[cfg(feature = "redis-memory")]
162pub use redis::{RedisMemoryConfig, RedisMemoryService};
163#[cfg(feature = "sqlite-memory")]
164pub use sqlite::SqliteMemoryService;
165#[cfg(feature = "vertex-memory")]
166pub use vertex::{VertexAiMemoryBankService, VertexAiMemoryConfig};