80 lines
2.9 KiB
Rust
80 lines
2.9 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 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))
|
||
|
|
}
|
||
|
|
}
|