Skip to main content

rs_adk/tools/
load_memory.rs

1//! Load memory tool — allows agents to search their memory store.
2//!
3//! Mirrors ADK-Python's `load_memory_tool`. Provides the model with
4//! a tool to search session memory using a query string.
5
6use async_trait::async_trait;
7
8use crate::error::ToolError;
9use crate::tool::ToolFunction;
10
11/// Tool that searches the agent's memory store.
12///
13/// When the model needs to recall previously stored information,
14/// it can call this tool with a search query.
15#[derive(Debug, Clone, Default)]
16pub struct LoadMemoryTool;
17
18impl LoadMemoryTool {
19    /// Create a new load memory tool.
20    pub fn new() -> Self {
21        Self
22    }
23}
24
25#[async_trait]
26impl ToolFunction for LoadMemoryTool {
27    fn name(&self) -> &str {
28        "load_memory"
29    }
30
31    fn description(&self) -> &str {
32        "Search and load relevant information from the agent's memory. \
33         Call this function with a query to retrieve previously stored memories."
34    }
35
36    fn parameters(&self) -> Option<serde_json::Value> {
37        Some(serde_json::json!({
38            "type": "object",
39            "properties": {
40                "query": {
41                    "type": "string",
42                    "description": "The search query to find relevant memories."
43                }
44            },
45            "required": ["query"]
46        }))
47    }
48
49    async fn call(&self, args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
50        let query = args.get("query").and_then(|v| v.as_str()).unwrap_or("");
51
52        // The actual memory search is performed by the runtime context.
53        // This tool returns a placeholder indicating the query was received.
54        // In a real integration, the runtime intercepts this tool call
55        // and routes it to the MemoryService.
56        Ok(serde_json::json!({
57            "status": "memory_search_requested",
58            "query": query,
59            "results": []
60        }))
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67    use serde_json::json;
68
69    #[test]
70    fn tool_metadata() {
71        let tool = LoadMemoryTool::new();
72        assert_eq!(tool.name(), "load_memory");
73        assert!(tool.description().contains("memory"));
74        assert!(tool.parameters().is_some());
75    }
76
77    #[tokio::test]
78    async fn call_with_query() {
79        let tool = LoadMemoryTool::new();
80        let result = tool
81            .call(json!({"query": "user preferences"}))
82            .await
83            .unwrap();
84        assert_eq!(result["query"], "user preferences");
85    }
86}