use crate::embedding::EmbeddingProvider;
use crate::service::*;
use adk_core::Result;
use async_trait::async_trait;
use chrono::Utc;
use mongodb::bson::{DateTime as BsonDateTime, Document, doc};
use mongodb::options::IndexOptions;
use mongodb::{Client, Database, IndexModel};
use std::sync::Arc;
use std::time::Duration;
use tracing::instrument;
pub struct MongoMemoryService {
db: Database,
embedding_provider: Option<Arc<dyn EmbeddingProvider>>,
default_ttl: Option<Duration>,
}
impl MongoMemoryService {
pub fn new(
client: Client,
database_name: &str,
embedding_provider: Option<Arc<dyn EmbeddingProvider>>,
) -> Result<Self> {
let db = client.database(database_name);
Ok(Self { db, embedding_provider, default_ttl: None })
}
pub fn with_default_ttl(mut self, ttl: Duration) -> Self {
self.default_ttl = Some(ttl);
self
}
fn document(
&self,
scope: &Document,
entry: MemoryEntry,
embedding: Option<&[f32]>,
now: chrono::DateTime<Utc>,
) -> Result<Document> {
let entry = crate::expiry::stamp(entry, self.default_ttl, now);
let content_json = serde_json::to_value(&entry.content)
.map_err(|e| adk_core::AdkError::memory(format!("serialization failed: {e}")))?;
let content_bson = mongodb::bson::to_bson(&content_json)
.map_err(|e| adk_core::AdkError::memory(format!("bson conversion failed: {e}")))?;
let mut document = scope.clone();
document.insert("memory_id", entry.id.unwrap_or_default());
document.insert("content", content_bson);
document.insert("content_text", crate::text::extract_text(&entry.content));
document.insert("author", entry.author);
document.insert("timestamp", BsonDateTime::from_millis(entry.timestamp.timestamp_millis()));
if let Some(expires_at) = entry.expires_at {
document.insert("expires_at", BsonDateTime::from_millis(expires_at.timestamp_millis()));
}
if let Some(embedding) = embedding {
let embedding: Vec<mongodb::bson::Bson> =
embedding.iter().map(|&v| mongodb::bson::Bson::Double(v as f64)).collect();
document.insert("embedding", embedding);
}
Ok(document)
}
const REGISTRY_COLLECTION: &'static str = "_adk_memory_migrations";
const MONGO_MEMORY_MIGRATIONS: &'static [(i64, &'static str)] = &[
(1, "create initial indexes"),
(2, "add project_id compound index"),
(3, "add memory_id index and expires_at TTL index"),
];
pub async fn migrate(&self) -> Result<()> {
self.db
.collection::<Document>(Self::REGISTRY_COLLECTION)
.create_index(
IndexModel::builder()
.keys(doc! { "version": 1 })
.options(
IndexOptions::builder()
.unique(true)
.name("idx_migration_version_unique".to_string())
.build(),
)
.build(),
)
.await
.map_err(|e| {
adk_core::AdkError::memory(format!("migration registry creation failed: {e}"))
})?;
let mut max_applied = self.read_max_applied_version().await?;
if max_applied == 0 {
let existing = self.detect_existing_tables().await?;
if existing && let Some(&(version, description)) = Self::MONGO_MEMORY_MIGRATIONS.first()
{
self.record_migration(version, description).await?;
max_applied = version;
}
}
let max_compiled = Self::MONGO_MEMORY_MIGRATIONS.last().map(|s| s.0).unwrap_or(0);
if max_applied > max_compiled {
return Err(adk_core::AdkError::memory(format!(
"schema version mismatch: database is at v{max_applied} \
but code only knows up to v{max_compiled}. \
Upgrade your ADK version."
)));
}
for &(version, description) in Self::MONGO_MEMORY_MIGRATIONS {
if version <= max_applied {
continue;
}
run_mongo_memory_step(&self.db, version).await.map_err(|e| {
adk_core::AdkError::memory(format!(
"{}",
crate::migration::MigrationError {
version,
description: description.to_string(),
cause: e.to_string(),
}
))
})?;
self.record_migration(version, description).await?;
}
Ok(())
}
pub async fn schema_version(&self) -> Result<i64> {
let collections =
self.db.list_collection_names().await.map_err(|e| {
adk_core::AdkError::memory(format!("schema version query failed: {e}"))
})?;
if !collections.contains(&Self::REGISTRY_COLLECTION.to_string()) {
return Ok(0);
}
self.read_max_applied_version().await
}
async fn read_max_applied_version(&self) -> Result<i64> {
use mongodb::options::FindOneOptions;
let registry = self.db.collection::<Document>(Self::REGISTRY_COLLECTION);
let opts = FindOneOptions::builder().sort(doc! { "version": -1 }).build();
let result = registry.find_one(doc! {}).with_options(opts).await.map_err(|e| {
adk_core::AdkError::memory(format!("migration registry read failed: {e}"))
})?;
match result {
Some(doc) => {
let version = doc.get_i64("version").unwrap_or(0);
Ok(version)
}
None => Ok(0),
}
}
async fn detect_existing_tables(&self) -> Result<bool> {
let failed = |e: mongodb::error::Error| {
adk_core::AdkError::memory(format!("baseline detection failed: {e}"))
};
let collections = self.db.list_collection_names().await.map_err(failed)?;
if !collections.iter().any(|name| name == "memory_entries") {
return Ok(false);
}
let indexes = self
.db
.collection::<Document>("memory_entries")
.list_index_names()
.await
.map_err(failed)?;
Ok(indexes.iter().any(|name| name == "idx_memory_entries_text"))
}
async fn record_migration(&self, version: i64, description: &str) -> Result<()> {
let registry = self.db.collection::<Document>(Self::REGISTRY_COLLECTION);
let now = BsonDateTime::from_millis(Utc::now().timestamp_millis());
let recorded = registry
.update_one(
doc! { "version": version },
doc! { "$setOnInsert": { "description": description, "applied_at": now } },
)
.upsert(true)
.await;
match recorded {
Ok(_) => Ok(()),
Err(e) if is_duplicate_key(&e) => Ok(()),
Err(e) => Err(e),
}
.map_err(|e| {
adk_core::AdkError::memory(format!(
"{}",
crate::migration::MigrationError {
version,
description: description.to_string(),
cause: format!("registry record failed: {e}"),
}
))
})?;
Ok(())
}
}
fn is_duplicate_key(error: &mongodb::error::Error) -> bool {
use mongodb::error::{ErrorKind, WriteFailure};
match &*error.kind {
ErrorKind::Write(WriteFailure::WriteError(write)) => write.code == 11000,
_ => false,
}
}
async fn run_mongo_memory_step(db: &Database, version: i64) -> Result<()> {
match version {
1 => mongo_memory_v1(db).await,
2 => mongo_memory_v2(db).await,
3 => mongo_memory_v3(db).await,
_ => Err(adk_core::AdkError::memory(format!("unknown migration version: {version}"))),
}
}
async fn mongo_memory_v1(db: &Database) -> Result<()> {
let collection = db.collection::<Document>("memory_entries");
collection
.create_index(
IndexModel::builder()
.keys(doc! { "app_name": 1, "user_id": 1 })
.options(
IndexOptions::builder().name("idx_memory_entries_app_user".to_string()).build(),
)
.build(),
)
.await
.map_err(|e| adk_core::AdkError::memory(format!("index creation failed: {e}")))?;
collection
.create_index(
IndexModel::builder()
.keys(doc! { "content_text": "text" })
.options(
IndexOptions::builder().name("idx_memory_entries_text".to_string()).build(),
)
.build(),
)
.await
.map_err(|e| adk_core::AdkError::memory(format!("text index creation failed: {e}")))?;
Ok(())
}
async fn mongo_memory_v2(db: &Database) -> Result<()> {
let collection = db.collection::<Document>("memory_entries");
collection
.create_index(
IndexModel::builder()
.keys(doc! { "app_name": 1, "user_id": 1, "project_id": 1 })
.options(
IndexOptions::builder()
.name("idx_memory_entries_app_user_project".to_string())
.build(),
)
.build(),
)
.await
.map_err(|e| {
adk_core::AdkError::memory(format!("project_id compound index creation failed: {e}"))
})?;
Ok(())
}
async fn mongo_memory_v3(db: &Database) -> Result<()> {
let collection = db.collection::<Document>("memory_entries");
collection
.create_index(
IndexModel::builder()
.keys(doc! { "app_name": 1, "user_id": 1, "memory_id": 1 })
.options(
IndexOptions::builder()
.name("idx_memory_entries_memory_id".to_string())
.build(),
)
.build(),
)
.await
.map_err(|e| adk_core::AdkError::memory(format!("memory_id index creation failed: {e}")))?;
collection
.create_index(
IndexModel::builder()
.keys(doc! { "expires_at": 1 })
.options(
IndexOptions::builder()
.name("idx_memory_entries_expires_at_ttl".to_string())
.expire_after(std::time::Duration::ZERO)
.build(),
)
.build(),
)
.await
.map_err(|e| adk_core::AdkError::memory(format!("TTL index creation failed: {e}")))?;
Ok(())
}
fn live_filter(now: chrono::DateTime<Utc>) -> Document {
doc! { "expires_at": { "$not": { "$lte": BsonDateTime::from_millis(now.timestamp_millis()) } } }
}
fn document_entry(doc: &Document) -> Option<MemoryEntry> {
let content_json: serde_json::Value =
mongodb::bson::from_bson(doc.get("content")?.clone()).ok()?;
let content: adk_core::Content = serde_json::from_value(content_json).ok()?;
let author = doc.get_str("author").ok()?.to_string();
let millis = |field: &str| {
doc.get_datetime(field)
.ok()
.and_then(|dt| chrono::DateTime::from_timestamp_millis(dt.timestamp_millis()))
};
let mut entry = MemoryEntry::new(content, author).with_timestamp(millis("timestamp")?);
entry.id = doc
.get_str("memory_id")
.ok()
.filter(|id| !id.is_empty())
.map(str::to_string)
.or_else(|| doc.get_object_id("_id").ok().map(|id| id.to_hex()));
entry.expires_at = millis("expires_at");
Some(entry)
}
#[async_trait]
impl MemoryService for MongoMemoryService {
#[instrument(skip_all, fields(app_name = %app_name, user_id = %user_id, session_id = %session_id, entry_count = entries.len()))]
async fn add_session(
&self,
app_name: &str,
user_id: &str,
session_id: &str,
entries: Vec<MemoryEntry>,
) -> Result<()> {
if entries.is_empty() {
return Ok(());
}
let collection = self.db.collection::<Document>("memory_entries");
let texts: Vec<String> =
entries.iter().map(|e| crate::text::extract_text(&e.content)).collect();
let embeddings = if let Some(provider) = &self.embedding_provider {
let non_empty_texts: Vec<String> = texts
.iter()
.map(|t| if t.is_empty() { " ".to_string() } else { t.clone() })
.collect();
Some(provider.embed(&non_empty_texts).await.map_err(|e| {
adk_core::AdkError::memory(format!("embedding generation failed: {e}"))
})?)
} else {
None
};
let now = Utc::now();
let scope = doc! {
"app_name": app_name,
"user_id": user_id,
"session_id": session_id,
"project_id": mongodb::bson::Bson::Null,
};
let mut documents = Vec::with_capacity(entries.len());
for (i, entry) in entries.into_iter().enumerate() {
let embedding = embeddings.as_ref().map(|embs| embs[i].as_slice());
documents.push(self.document(&scope, entry, embedding, now)?);
}
collection
.insert_many(documents)
.await
.map_err(|e| adk_core::AdkError::memory(format!("add_session failed: {e}")))?;
Ok(())
}
fn supports_project_scoping(&self) -> bool {
true
}
#[instrument(skip_all, fields(app_name = %app_name, user_id = %user_id, session_id = %session_id, project_id = %project_id, entry_count = entries.len()))]
async fn add_session_to_project(
&self,
app_name: &str,
user_id: &str,
session_id: &str,
project_id: &str,
entries: Vec<MemoryEntry>,
) -> Result<()> {
validate_project_id(project_id)?;
if entries.is_empty() {
return Ok(());
}
let collection = self.db.collection::<Document>("memory_entries");
let texts: Vec<String> =
entries.iter().map(|e| crate::text::extract_text(&e.content)).collect();
let embeddings = if let Some(provider) = &self.embedding_provider {
let non_empty_texts: Vec<String> = texts
.iter()
.map(|t| if t.is_empty() { " ".to_string() } else { t.clone() })
.collect();
Some(provider.embed(&non_empty_texts).await.map_err(|e| {
adk_core::AdkError::memory(format!("embedding generation failed: {e}"))
})?)
} else {
None
};
let now = Utc::now();
let scope = doc! {
"app_name": app_name,
"user_id": user_id,
"session_id": session_id,
"project_id": project_id,
};
let mut documents = Vec::with_capacity(entries.len());
for (i, entry) in entries.into_iter().enumerate() {
let embedding = embeddings.as_ref().map(|embs| embs[i].as_slice());
documents.push(self.document(&scope, entry, embedding, now)?);
}
collection.insert_many(documents).await.map_err(|e| {
adk_core::AdkError::memory(format!("add_session_to_project failed: {e}"))
})?;
Ok(())
}
#[instrument(skip_all, fields(app_name = %app_name, user_id = %user_id, project_id = %project_id))]
async fn add_entry_to_project(
&self,
app_name: &str,
user_id: &str,
project_id: &str,
entry: MemoryEntry,
) -> Result<()> {
validate_project_id(project_id)?;
let collection = self.db.collection::<Document>("memory_entries");
let content_text = crate::text::extract_text(&entry.content);
let embedding = match &self.embedding_provider {
Some(provider) => {
let text_for_embed =
if content_text.is_empty() { " ".to_string() } else { content_text.clone() };
let mut embeddings = provider.embed(&[text_for_embed]).await.map_err(|e| {
adk_core::AdkError::memory(format!("embedding generation failed: {e}"))
})?;
Some(embeddings.swap_remove(0))
}
None => None,
};
let scope = doc! { "app_name": app_name, "user_id": user_id, "project_id": project_id };
let document = self.document(&scope, entry, embedding.as_deref(), Utc::now())?;
collection
.insert_one(document)
.await
.map_err(|e| adk_core::AdkError::memory(format!("add_entry_to_project failed: {e}")))?;
Ok(())
}
#[instrument(skip_all, fields(app_name = %req.app_name, user_id = %req.user_id))]
async fn search(&self, req: SearchRequest) -> Result<SearchResponse> {
let collection = self.db.collection::<Document>("memory_entries");
let limit = req.limit.unwrap_or(10) as i64;
let docs = if let Some(ref provider) = self.embedding_provider {
let query_embedding = provider
.embed(std::slice::from_ref(&req.query))
.await
.map_err(|e| adk_core::AdkError::memory(format!("query embedding failed: {e}")))?;
let query_vec: Vec<mongodb::bson::Bson> =
query_embedding[0].iter().map(|&v| mongodb::bson::Bson::Double(v as f64)).collect();
let mut match_filter = doc! {
"app_name": &req.app_name,
"user_id": &req.user_id,
};
match &req.project_id {
None => {
match_filter.insert("project_id", mongodb::bson::Bson::Null);
}
Some(id) => {
match_filter.insert(
"$or",
vec![
doc! { "project_id": mongodb::bson::Bson::Null },
doc! { "project_id": id },
],
);
}
}
let match_filter = doc! { "$and": [match_filter, live_filter(Utc::now())] };
let pipeline = vec![
doc! {
"$vectorSearch": {
"index": "memory_embedding_index",
"path": "embedding",
"queryVector": &query_vec,
"numCandidates": 100,
"limit": limit,
}
},
doc! {
"$match": match_filter,
},
];
let mut cursor = collection.aggregate(pipeline).await.map_err(|e| {
let msg = e.to_string();
if msg.contains("PlanExecutor") || msg.contains("$vectorSearch") {
adk_core::AdkError::memory(
"vector search index not available: Atlas Vector Search index \
'memory_embedding_index' must be created via Atlas UI/API"
.to_string(),
)
} else {
adk_core::AdkError::memory(format!("search failed: {e}"))
}
})?;
let mut results = Vec::new();
while cursor
.advance()
.await
.map_err(|e| adk_core::AdkError::memory(format!("search cursor failed: {e}")))?
{
let doc = cursor.deserialize_current().map_err(|e| {
adk_core::AdkError::memory(format!("search deserialization failed: {e}"))
})?;
results.push(doc);
}
results
} else {
let mut filter = doc! {
"app_name": &req.app_name,
"user_id": &req.user_id,
"$text": { "$search": &req.query },
};
match &req.project_id {
None => {
filter.insert("project_id", mongodb::bson::Bson::Null);
}
Some(id) => {
filter.insert(
"$or",
vec![
doc! { "project_id": mongodb::bson::Bson::Null },
doc! { "project_id": id.as_str() },
],
);
}
}
filter.insert(
"expires_at",
live_filter(Utc::now()).get("expires_at").cloned().unwrap_or_default(),
);
let mut cursor = collection
.find(filter)
.sort(doc! { "score": { "$meta": "textScore" } })
.limit(limit)
.await
.map_err(|e| adk_core::AdkError::memory(format!("search failed: {e}")))?;
let mut results = Vec::new();
while cursor
.advance()
.await
.map_err(|e| adk_core::AdkError::memory(format!("search cursor failed: {e}")))?
{
let doc = cursor.deserialize_current().map_err(|e| {
adk_core::AdkError::memory(format!("search deserialization failed: {e}"))
})?;
results.push(doc);
}
results
};
let memories = docs.iter().filter_map(document_entry).collect();
Ok(SearchResponse { memories })
}
#[instrument(skip_all, fields(app_name = %app_name, user_id = %user_id, project_id = %project_id))]
async fn delete_entries_in_project(
&self,
app_name: &str,
user_id: &str,
project_id: &str,
query: &str,
) -> Result<u64> {
validate_project_id(project_id)?;
let collection = self.db.collection::<Document>("memory_entries");
let filter = doc! {
"app_name": app_name,
"user_id": user_id,
"project_id": project_id,
"$text": { "$search": query },
};
let result = collection.delete_many(filter).await.map_err(|e| {
adk_core::AdkError::memory(format!("delete_entries_in_project failed: {e}"))
})?;
Ok(result.deleted_count)
}
#[instrument(skip_all, fields(app_name = %app_name, user_id = %user_id, project_id = %project_id))]
async fn delete_project(&self, app_name: &str, user_id: &str, project_id: &str) -> Result<u64> {
validate_project_id(project_id)?;
let collection = self.db.collection::<Document>("memory_entries");
let filter = doc! {
"app_name": app_name,
"user_id": user_id,
"project_id": project_id,
};
let result = collection
.delete_many(filter)
.await
.map_err(|e| adk_core::AdkError::memory(format!("delete_project failed: {e}")))?;
Ok(result.deleted_count)
}
#[instrument(skip_all, fields(app_name = %app_name, user_id = %user_id))]
async fn export_user(&self, app_name: &str, user_id: &str) -> Result<Vec<UserMemory>> {
let failed = |e: mongodb::error::Error| {
adk_core::AdkError::memory(format!("export_user failed: {e}"))
};
let mut cursor = self
.db
.collection::<Document>("memory_entries")
.find(doc! { "app_name": app_name, "user_id": user_id })
.sort(doc! { "timestamp": 1, "_id": 1 })
.await
.map_err(failed)?;
let mut documents = Vec::new();
while cursor.advance().await.map_err(failed)? {
documents.push(cursor.deserialize_current().map_err(failed)?);
}
documents
.iter()
.map(|document| {
let corrupt =
|field: &str| adk_core::AdkError::memory(format!("stored {field} is corrupt"));
Ok(UserMemory {
entry: document_entry(document).ok_or_else(|| corrupt("entry"))?,
session_id: document.get_str("session_id").ok().map(str::to_string),
project_id: document.get_str("project_id").ok().map(str::to_string),
})
})
.collect()
}
#[instrument(skip_all, fields(app_name = %app_name, user_id = %user_id))]
async fn delete_user(&self, app_name: &str, user_id: &str) -> Result<()> {
let collection = self.db.collection::<Document>("memory_entries");
collection
.delete_many(doc! { "app_name": app_name, "user_id": user_id })
.await
.map_err(|e| adk_core::AdkError::memory(format!("delete_user failed: {e}")))?;
Ok(())
}
#[instrument(skip_all, fields(app_name = %app_name, user_id = %user_id, session_id = %session_id))]
async fn delete_session(&self, app_name: &str, user_id: &str, session_id: &str) -> Result<()> {
let collection = self.db.collection::<Document>("memory_entries");
collection
.delete_many(doc! {
"app_name": app_name,
"user_id": user_id,
"session_id": session_id,
})
.await
.map_err(|e| adk_core::AdkError::memory(format!("delete_session failed: {e}")))?;
Ok(())
}
#[instrument(skip_all, fields(app_name = %app_name, user_id = %user_id))]
async fn delete_memory(&self, app_name: &str, user_id: &str, memory_id: &str) -> Result<u64> {
let collection = self.db.collection::<Document>("memory_entries");
let mut filter = doc! { "app_name": app_name, "user_id": user_id, "memory_id": memory_id };
if let Ok(object_id) = mongodb::bson::oid::ObjectId::parse_str(memory_id) {
filter = doc! {
"app_name": app_name,
"user_id": user_id,
"$or": [{ "memory_id": memory_id }, { "_id": object_id, "memory_id": { "$exists": false } }],
};
}
let result = collection
.delete_many(filter)
.await
.map_err(|e| adk_core::AdkError::memory(format!("delete_memory failed: {e}")))?;
Ok(result.deleted_count)
}
#[instrument(skip_all)]
async fn purge_expired(&self) -> Result<u64> {
let now = BsonDateTime::from_millis(Utc::now().timestamp_millis());
let result = self
.db
.collection::<Document>("memory_entries")
.delete_many(doc! { "expires_at": { "$lte": now } })
.await
.map_err(|e| adk_core::AdkError::memory(format!("purge_expired failed: {e}")))?;
Ok(result.deleted_count)
}
#[instrument(skip_all)]
async fn health_check(&self) -> Result<()> {
self.db
.run_command(doc! { "ping": 1 })
.await
.map_err(|e| adk_core::AdkError::memory(format!("health check failed: {e}")))?;
Ok(())
}
}