use adk_core::{Content, Result};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct MemoryEntry {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
pub content: Content,
pub author: String,
pub timestamp: DateTime<Utc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub expires_at: Option<DateTime<Utc>>,
}
impl MemoryEntry {
pub fn new(content: Content, author: impl Into<String>) -> Self {
Self { id: None, content, author: author.into(), timestamp: Utc::now(), expires_at: None }
}
pub fn with_timestamp(mut self, timestamp: DateTime<Utc>) -> Self {
self.timestamp = timestamp;
self
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
pub fn with_expires_at(mut self, expires_at: DateTime<Utc>) -> Self {
self.expires_at = Some(crate::expiry::to_millis(expires_at));
self
}
pub fn with_ttl(self, ttl: Duration) -> Self {
let expires_at = crate::expiry::expiry_after(Utc::now(), ttl);
self.with_expires_at(expires_at)
}
pub fn with_artifact(mut self, uri: impl Into<String>, mime_type: impl Into<String>) -> Self {
self.content.parts.push(crate::text::ArtifactReference::new(uri, mime_type).to_part());
self
}
pub fn artifact_references(&self) -> Vec<crate::text::ArtifactReference> {
crate::text::artifact_references(&self.content)
}
pub fn is_expired_at(&self, now: DateTime<Utc>) -> bool {
crate::expiry::is_expired(self.expires_at, now)
}
}
#[derive(Debug, Clone, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UserMemory {
#[serde(flatten)]
pub entry: MemoryEntry,
#[serde(skip_serializing_if = "Option::is_none")]
pub session_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub project_id: Option<String>,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct SearchRequest {
pub query: String,
pub user_id: String,
pub app_name: String,
pub limit: Option<usize>,
pub min_score: Option<f32>,
#[serde(default)]
pub project_id: Option<String>,
}
#[derive(Debug, Clone)]
pub struct SearchResponse {
pub memories: Vec<MemoryEntry>,
}
pub fn validate_project_id(project_id: &str) -> Result<()> {
if project_id.is_empty() {
return Err(adk_core::AdkError::memory("project_id must not be empty"));
}
if project_id.len() > 256 {
return Err(adk_core::AdkError::memory(format!(
"project_id exceeds maximum length of 256 characters (got {})",
project_id.len()
)));
}
Ok(())
}
#[async_trait]
pub trait MemoryService: Send + Sync {
async fn add_session(
&self,
app_name: &str,
user_id: &str,
session_id: &str,
entries: Vec<MemoryEntry>,
) -> Result<()>;
async fn search(&self, req: SearchRequest) -> Result<SearchResponse>;
async fn delete_user(&self, app_name: &str, user_id: &str) -> Result<()> {
let _ = (app_name, user_id);
Err(adk_core::AdkError::memory("delete_user not implemented"))
}
async fn export_user(&self, app_name: &str, user_id: &str) -> Result<Vec<UserMemory>> {
let _ = (app_name, user_id);
Err(adk_core::subject::unsupported_error("memory", "export"))
}
async fn delete_session(&self, app_name: &str, user_id: &str, session_id: &str) -> Result<()> {
let _ = (app_name, user_id, session_id);
Err(adk_core::AdkError::memory("delete_session not implemented"))
}
async fn add_entry(&self, app_name: &str, user_id: &str, entry: MemoryEntry) -> Result<()> {
let _ = (app_name, user_id, entry);
Err(adk_core::AdkError::memory("add_entry not implemented"))
}
async fn delete_entries(&self, app_name: &str, user_id: &str, query: &str) -> Result<u64> {
let _ = (app_name, user_id, query);
Err(adk_core::AdkError::memory("delete_entries not implemented"))
}
async fn list_recent(
&self,
app_name: &str,
user_id: &str,
limit: usize,
) -> Result<Vec<MemoryEntry>> {
let _ = (app_name, user_id, limit);
Err(adk_core::AdkError::memory("list_recent not implemented"))
}
async fn delete_memory(&self, app_name: &str, user_id: &str, memory_id: &str) -> Result<u64> {
let _ = (app_name, user_id, memory_id);
Err(adk_core::AdkError::memory(
"delete_memory is not implemented by this memory backend; every adk-memory backend \
implements it, or remove the user's entries with delete_user",
))
}
async fn purge_expired(&self) -> Result<u64> {
Err(adk_core::AdkError::memory(
"purge_expired is not implemented by this memory backend; every adk-memory backend \
implements it",
))
}
async fn health_check(&self) -> Result<()> {
Ok(())
}
fn supports_project_scoping(&self) -> bool {
false
}
async fn add_session_to_project(
&self,
app_name: &str,
user_id: &str,
session_id: &str,
project_id: &str,
entries: Vec<MemoryEntry>,
) -> Result<()> {
let _ = (app_name, user_id, session_id, project_id, entries);
Err(project_scoping_unsupported("add_session_to_project"))
}
async fn add_entry_to_project(
&self,
app_name: &str,
user_id: &str,
project_id: &str,
entry: MemoryEntry,
) -> Result<()> {
let _ = (app_name, user_id, project_id, entry);
Err(project_scoping_unsupported("add_entry_to_project"))
}
async fn delete_entries_in_project(
&self,
app_name: &str,
user_id: &str,
project_id: &str,
query: &str,
) -> Result<u64> {
let _ = (app_name, user_id, project_id, query);
Err(project_scoping_unsupported("delete_entries_in_project"))
}
async fn delete_project(&self, app_name: &str, user_id: &str, project_id: &str) -> Result<u64> {
let _ = (app_name, user_id, project_id);
Err(adk_core::AdkError::memory("delete_project not implemented"))
}
}
fn project_scoping_unsupported(method: &str) -> adk_core::AdkError {
adk_core::AdkError::memory(format!(
"this memory backend does not implement project scoping, so `{method}` cannot honour \
the project boundary; use a backend whose `supports_project_scoping` returns true, or \
call the global method explicitly if global scope is intended"
))
}