adk_memory/adapter.rs
1//! Adapter bridging [`MemoryService`] to [`adk_core::Memory`].
2//!
3//! The runner expects `Arc<dyn adk_core::Memory>`, which has a simple
4//! `search(&str)` signature. [`MemoryService`] requires a [`SearchRequest`]
5//! with `app_name` and `user_id`. This adapter binds those fields at
6//! construction time so any `MemoryService` can be used as `adk_core::Memory`.
7//!
8//! # Example
9//!
10//! ```rust,ignore
11//! use adk_memory::{InMemoryMemoryService, MemoryServiceAdapter};
12//! use std::sync::Arc;
13//!
14//! let service = Arc::new(InMemoryMemoryService::new());
15//! let memory = Arc::new(MemoryServiceAdapter::new(service, "my-app", "user-1"));
16//! // memory implements adk_core::Memory and can be passed to RunnerConfig
17//! ```
18
19use std::sync::Arc;
20
21use async_trait::async_trait;
22
23use crate::{MemoryService, SearchRequest};
24
25/// Adapts any [`MemoryService`] into an [`adk_core::Memory`] implementation.
26///
27/// Binds `app_name` and `user_id` at construction so the runner's
28/// `search(query: &str)` calls are forwarded with full context.
29pub struct MemoryServiceAdapter {
30 inner: Arc<dyn MemoryService>,
31 app_name: String,
32 user_id: String,
33}
34
35impl MemoryServiceAdapter {
36 /// Create a new adapter binding a memory service to a specific app and user.
37 pub fn new(
38 inner: Arc<dyn MemoryService>,
39 app_name: impl Into<String>,
40 user_id: impl Into<String>,
41 ) -> Self {
42 Self { inner, app_name: app_name.into(), user_id: user_id.into() }
43 }
44}
45
46#[async_trait]
47impl adk_core::Memory for MemoryServiceAdapter {
48 async fn search(&self, query: &str) -> adk_core::Result<Vec<adk_core::MemoryEntry>> {
49 let resp = self
50 .inner
51 .search(SearchRequest {
52 query: query.to_string(),
53 app_name: self.app_name.clone(),
54 user_id: self.user_id.clone(),
55 limit: None,
56 min_score: None,
57 })
58 .await?;
59
60 Ok(resp
61 .memories
62 .into_iter()
63 .map(|m| adk_core::MemoryEntry { content: m.content, author: m.author })
64 .collect())
65 }
66
67 async fn health_check(&self) -> adk_core::Result<()> {
68 self.inner.health_check().await
69 }
70}