use std::sync::Arc;
use agent_base::{Tool, ToolContext};
use crate::memory::MemoryStore;
mod memory_delete;
mod memory_list;
mod memory_write;
pub use memory_delete::MemoryDeleteTool;
pub use memory_list::MemoryListTool;
pub use memory_write::MemoryWriteTool;
pub const MEMORY_TOOLS_DESCRIPTION: &str = "\
The `memory_*` tools are one store:
- `memory_write(name, description, type, body)` — create or update one memory and sync the index.
- `memory_list()` — return the current index (refreshes the stale startup snapshot).
- `memory_delete(name)` — delete a memory and remove its index line.
- Reading is not a tool: the index row names the file — open it with `read_file`.";
pub fn create_memory_tools(store: Arc<MemoryStore>) -> Vec<Arc<dyn Tool>> {
vec![
Arc::new(MemoryWriteTool::new(Arc::clone(&store))),
Arc::new(MemoryListTool::new(Arc::clone(&store))),
Arc::new(MemoryDeleteTool::new(store)),
]
}
fn session_ref(ctx: &ToolContext) -> String {
ctx.session_id
.external_id
.clone()
.unwrap_or_else(|| ctx.session_id.id.to_string())
}
#[cfg(test)]
pub(crate) mod test_support {
use super::*;
use agent_base::Content;
pub(crate) fn first_text(out: &[Content]) -> String {
match out.first() {
Some(Content::Text { text }) => text.clone(),
_ => String::new(),
}
}
pub(crate) fn ctx_with_external_id(id: &str) -> ToolContext {
let mut ctx = ToolContext::for_test();
ctx.session_id.external_id = Some(id.to_string());
ctx
}
}
#[cfg(test)]
mod description_guard {
use super::*;
#[test]
fn memory_tool_descriptions_route_and_do_not_shrink() {
let store = Arc::new(crate::memory::MemoryStore::new(
std::env::temp_dir().join("mem-guard"),
"MEMORY.md",
));
let tools = create_memory_tools(store);
for tool in tools {
let d = tool.description();
assert!(
d.chars().count() >= 400,
"{}: description shrank to a one-liner ({} chars)",
tool.name(),
d.chars().count()
);
assert!(
d.contains("Do NOT") || d.contains("Do not") || d.contains("only when"),
"{}: must say when NOT to use it: {d}",
tool.name()
);
}
}
}