feat: add memory management tools and utility commands

This commit is contained in:
asepharyana
2026-07-11 20:44:15 +07:00
parent 08490532d2
commit fe82840c03
13 changed files with 535 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
use serde_json::{json, Value};
use anyhow::{Result, anyhow};
use super::super::Tool;
use super::super::ToolCtx;
use crate::model::memory::Memory;
pub struct Forget;
impl Tool for Forget {
fn name(&self) -> &'static str {
"forget"
}
fn description(&self) -> &'static str {
"Remove a specific memory entry by its name. Use recall first to find the exact name if unsure."
}
fn parameters(&self) -> Value {
json!({
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Name of the memory to remove (use recall to find exact names)"
}
},
"required": ["name"]
})
}
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
let name = args.get("name")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow!("missing required argument: name"))?;
Memory::remove(&ctx.memory_dir, name)
.map_err(|e| anyhow!("failed to remove memory '{}': {}", name, e))?;
Ok(format!("removed memory '{}'", name))
}
}
+3
View File
@@ -0,0 +1,3 @@
pub mod forget;
pub mod recall;
pub mod remember;
+65
View File
@@ -0,0 +1,65 @@
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)
}
+79
View File
@@ -0,0 +1,79 @@
use serde_json::{json, Value};
use anyhow::{Result, anyhow};
use super::super::Tool;
use super::super::ToolCtx;
use crate::model::memory::Memory;
pub struct Remember;
impl Tool for Remember {
fn name(&self) -> &'static str {
"remember"
}
fn description(&self) -> &'static str {
"Save a piece of information to persistent project memory. Memory entries are injected into future conversations via the system prompt, so use this to record conventions, preferences, and important context."
}
fn parameters(&self) -> Value {
json!({
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Short unique name for the memory (kebab-case, e.g. 'testing-conventions')"
},
"description": {
"type": "string",
"description": "One-line summary shown in the memory index"
},
"content": {
"type": "string",
"description": "The memory content body"
},
"kind": {
"type": "string",
"description": "Type of memory: 'project', 'reference', 'lesson', or 'feedback'",
"enum": ["project", "reference", "lesson", "feedback"]
}
},
"required": ["name", "description", "content", "kind"]
})
}
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
let name = args.get("name")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow!("missing required argument: name"))?;
let description = args.get("description")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow!("missing required argument: description"))?;
let content = args.get("content")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow!("missing required argument: content"))?;
let kind = args.get("kind")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow!("missing required argument: kind"))?;
if Memory::slugify(name).is_none() {
anyhow::bail!("invalid memory name: must produce a valid slug (alphanumeric + hyphens, 1-80 chars)");
}
let now = chrono::Utc::now().timestamp_millis();
let memory = Memory {
name: name.to_string(),
description: description.to_string(),
content: content.to_string(),
kind: kind.to_string(),
created_at: now,
updated_at: now,
outcome: None,
lifecycle: "new".to_string(),
};
memory.write(&ctx.memory_dir)
.map_err(|e| anyhow!("failed to write memory '{}': {}", name, e))?;
Ok(format!("saved memory '{}' ({})", name, kind))
}
}