* feat(memory): add session_id isolation to Memory trait Add optional session_id parameter to store(), recall(), and list() methods across the Memory trait and all four backends (sqlite, markdown, lucid, none). This enables per-session memory isolation so different agent sessions cannot cross-read each other's stored memories. Changes: - traits.rs: Add session_id: Option<&str> to store/recall/list - sqlite.rs: Schema migration (ALTER TABLE ADD COLUMN session_id), index, persist/filter by session_id in all query paths - markdown.rs, lucid.rs, none.rs: Updated signatures - All callers pass None for backward compatibility - 5 new tests: session-filtered recall, cross-session isolation, session-filtered list, no-filter returns all, migration idempotency Closes #518 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix(channels): fix discord _channel_id typo and lark missing reply_to Pre-existing compilation errors on main after reply_to was added to ChannelMessage: discord.rs used _channel_id (underscore prefix) but referenced channel_id, and lark.rs was missing the reply_to field. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
146 lines
4.5 KiB
Rust
146 lines
4.5 KiB
Rust
use super::traits::{Tool, ToolResult};
|
|
use crate::memory::{Memory, MemoryCategory};
|
|
use async_trait::async_trait;
|
|
use serde_json::json;
|
|
use std::sync::Arc;
|
|
|
|
/// Let the agent store memories — its own brain writes
|
|
pub struct MemoryStoreTool {
|
|
memory: Arc<dyn Memory>,
|
|
}
|
|
|
|
impl MemoryStoreTool {
|
|
pub fn new(memory: Arc<dyn Memory>) -> Self {
|
|
Self { memory }
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl Tool for MemoryStoreTool {
|
|
fn name(&self) -> &str {
|
|
"memory_store"
|
|
}
|
|
|
|
fn description(&self) -> &str {
|
|
"Store a fact, preference, or note in long-term memory. Use category 'core' for permanent facts, 'daily' for session notes, 'conversation' for chat context."
|
|
}
|
|
|
|
fn parameters_schema(&self) -> serde_json::Value {
|
|
json!({
|
|
"type": "object",
|
|
"properties": {
|
|
"key": {
|
|
"type": "string",
|
|
"description": "Unique key for this memory (e.g. 'user_lang', 'project_stack')"
|
|
},
|
|
"content": {
|
|
"type": "string",
|
|
"description": "The information to remember"
|
|
},
|
|
"category": {
|
|
"type": "string",
|
|
"enum": ["core", "daily", "conversation"],
|
|
"description": "Memory category: core (permanent), daily (session), conversation (chat)"
|
|
}
|
|
},
|
|
"required": ["key", "content"]
|
|
})
|
|
}
|
|
|
|
async fn execute(&self, args: serde_json::Value) -> anyhow::Result<ToolResult> {
|
|
let key = args
|
|
.get("key")
|
|
.and_then(|v| v.as_str())
|
|
.ok_or_else(|| anyhow::anyhow!("Missing 'key' parameter"))?;
|
|
|
|
let content = args
|
|
.get("content")
|
|
.and_then(|v| v.as_str())
|
|
.ok_or_else(|| anyhow::anyhow!("Missing 'content' parameter"))?;
|
|
|
|
let category = match args.get("category").and_then(|v| v.as_str()) {
|
|
Some("daily") => MemoryCategory::Daily,
|
|
Some("conversation") => MemoryCategory::Conversation,
|
|
_ => MemoryCategory::Core,
|
|
};
|
|
|
|
match self.memory.store(key, content, category, None).await {
|
|
Ok(()) => Ok(ToolResult {
|
|
success: true,
|
|
output: format!("Stored memory: {key}"),
|
|
error: None,
|
|
}),
|
|
Err(e) => Ok(ToolResult {
|
|
success: false,
|
|
output: String::new(),
|
|
error: Some(format!("Failed to store memory: {e}")),
|
|
}),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::memory::SqliteMemory;
|
|
use tempfile::TempDir;
|
|
|
|
fn test_mem() -> (TempDir, Arc<dyn Memory>) {
|
|
let tmp = TempDir::new().unwrap();
|
|
let mem = SqliteMemory::new(tmp.path()).unwrap();
|
|
(tmp, Arc::new(mem))
|
|
}
|
|
|
|
#[test]
|
|
fn name_and_schema() {
|
|
let (_tmp, mem) = test_mem();
|
|
let tool = MemoryStoreTool::new(mem);
|
|
assert_eq!(tool.name(), "memory_store");
|
|
let schema = tool.parameters_schema();
|
|
assert!(schema["properties"]["key"].is_object());
|
|
assert!(schema["properties"]["content"].is_object());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn store_core() {
|
|
let (_tmp, mem) = test_mem();
|
|
let tool = MemoryStoreTool::new(mem.clone());
|
|
let result = tool
|
|
.execute(json!({"key": "lang", "content": "Prefers Rust"}))
|
|
.await
|
|
.unwrap();
|
|
assert!(result.success);
|
|
assert!(result.output.contains("lang"));
|
|
|
|
let entry = mem.get("lang").await.unwrap();
|
|
assert!(entry.is_some());
|
|
assert_eq!(entry.unwrap().content, "Prefers Rust");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn store_with_category() {
|
|
let (_tmp, mem) = test_mem();
|
|
let tool = MemoryStoreTool::new(mem.clone());
|
|
let result = tool
|
|
.execute(json!({"key": "note", "content": "Fixed bug", "category": "daily"}))
|
|
.await
|
|
.unwrap();
|
|
assert!(result.success);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn store_missing_key() {
|
|
let (_tmp, mem) = test_mem();
|
|
let tool = MemoryStoreTool::new(mem);
|
|
let result = tool.execute(json!({"content": "no key"})).await;
|
|
assert!(result.is_err());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn store_missing_content() {
|
|
let (_tmp, mem) = test_mem();
|
|
let tool = MemoryStoreTool::new(mem);
|
|
let result = tool.execute(json!({"key": "no_content"})).await;
|
|
assert!(result.is_err());
|
|
}
|
|
}
|