66 lines
2.0 KiB
Rust
66 lines
2.0 KiB
Rust
use serde_json::{json, Value};
|
|||
|
|
use anyhow::{Result, anyhow};
|
||
|
|
use super::super::Tool;
|
||
|
|
use super::super::ToolCtx;
|
||
|
|
use crate::model::memory::Memory;
|
||
|
|
|
||
|
|
pub struct Recall;
|
||
|
|
|
||
|
|
impl Tool for Recall {
|
||
|
|
fn name(&self) -> &'static str {
|
||
|
|
"recall"
|
||
|
|
}
|
||
|
|
|
||
|
|
fn description(&self) -> &'static str {
|
||
|
|
"Read memory entries. Pass a name to read a specific entry, or omit name to list all entries in the memory index. The memory index is also automatically injected into your system prompt."
|
||
|
|
}
|
||
|
|
|
||
|
|
fn parameters(&self) -> Value {
|
||
|
|
json!({
|
||
|
|
"type": "object",
|
||
|
|
"properties": {
|
||
|
|
"name": {
|
||
|
|
"type": "string",
|
||
|
|
"description": "Optional: exact name of a specific memory entry to read. If omitted, lists all entries."
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
|
||
|
|
if let Some(name) = args.get("name").and_then(|v| v.as_str()) {
|
||
|
|
if name.is_empty() {
|
||
|
|
return list_all(ctx);
|
||
|
|
}
|
||
|
|
let memory = Memory::read(&ctx.memory_dir, name)
|
||
|
|
.map_err(|e| anyhow!("memory '{}' not found: {}", name, e))?;
|
||
|
|
Ok(format!(
|
||
|
|
"---\nname: {}\ndescription: {}\nkind: {}\nlifecycle: {}\n---\n\n{}",
|
||
|
|
memory.name,
|
||
|
|
memory.description,
|
||
|
|
memory.kind,
|
||
|
|
memory.lifecycle,
|
||
|
|
memory.content,
|
||
|
|
))
|
||
|
|
} else {
|
||
|
|
list_all(ctx)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn list_all(ctx: &ToolCtx) -> Result<String> {
|
||
|
|
let names = Memory::list(&ctx.memory_dir);
|
||
|
|
if names.is_empty() {
|
||
|
|
return Ok("(no memory entries)".to_string());
|
||
|
|
}
|
||
|
|
let mut lines = format!("Memory index ({} entries):\n", names.len());
|
||
|
|
for name in &names {
|
||
|
|
if let Ok(mem) = Memory::read(&ctx.memory_dir, name) {
|
||
|
|
lines.push_str(&format!("- {} [{}]: {}\n", name, mem.kind, mem.description));
|
||
|
|
} else {
|
||
|
|
lines.push_str(&format!("- {}\n", name));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Ok(lines)
|
||
|
|
}
|