* 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>
126 lines
3.0 KiB
Rust
126 lines
3.0 KiB
Rust
use crate::memory::Memory;
|
|
use async_trait::async_trait;
|
|
use std::fmt::Write;
|
|
|
|
#[async_trait]
|
|
pub trait MemoryLoader: Send + Sync {
|
|
async fn load_context(&self, memory: &dyn Memory, user_message: &str)
|
|
-> anyhow::Result<String>;
|
|
}
|
|
|
|
pub struct DefaultMemoryLoader {
|
|
limit: usize,
|
|
}
|
|
|
|
impl Default for DefaultMemoryLoader {
|
|
fn default() -> Self {
|
|
Self { limit: 5 }
|
|
}
|
|
}
|
|
|
|
impl DefaultMemoryLoader {
|
|
pub fn new(limit: usize) -> Self {
|
|
Self {
|
|
limit: limit.max(1),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl MemoryLoader for DefaultMemoryLoader {
|
|
async fn load_context(
|
|
&self,
|
|
memory: &dyn Memory,
|
|
user_message: &str,
|
|
) -> anyhow::Result<String> {
|
|
let entries = memory.recall(user_message, self.limit, None).await?;
|
|
if entries.is_empty() {
|
|
return Ok(String::new());
|
|
}
|
|
|
|
let mut context = String::from("[Memory context]\n");
|
|
for entry in entries {
|
|
let _ = writeln!(context, "- {}: {}", entry.key, entry.content);
|
|
}
|
|
context.push('\n');
|
|
Ok(context)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::memory::{Memory, MemoryCategory, MemoryEntry};
|
|
|
|
struct MockMemory;
|
|
|
|
#[async_trait]
|
|
impl Memory for MockMemory {
|
|
async fn store(
|
|
&self,
|
|
_key: &str,
|
|
_content: &str,
|
|
_category: MemoryCategory,
|
|
_session_id: Option<&str>,
|
|
) -> anyhow::Result<()> {
|
|
Ok(())
|
|
}
|
|
|
|
async fn recall(
|
|
&self,
|
|
_query: &str,
|
|
limit: usize,
|
|
_session_id: Option<&str>,
|
|
) -> anyhow::Result<Vec<MemoryEntry>> {
|
|
if limit == 0 {
|
|
return Ok(vec![]);
|
|
}
|
|
Ok(vec![MemoryEntry {
|
|
id: "1".into(),
|
|
key: "k".into(),
|
|
content: "v".into(),
|
|
category: MemoryCategory::Conversation,
|
|
timestamp: "now".into(),
|
|
session_id: None,
|
|
score: None,
|
|
}])
|
|
}
|
|
|
|
async fn get(&self, _key: &str) -> anyhow::Result<Option<MemoryEntry>> {
|
|
Ok(None)
|
|
}
|
|
|
|
async fn list(
|
|
&self,
|
|
_category: Option<&MemoryCategory>,
|
|
_session_id: Option<&str>,
|
|
) -> anyhow::Result<Vec<MemoryEntry>> {
|
|
Ok(vec![])
|
|
}
|
|
|
|
async fn forget(&self, _key: &str) -> anyhow::Result<bool> {
|
|
Ok(true)
|
|
}
|
|
|
|
async fn count(&self) -> anyhow::Result<usize> {
|
|
Ok(0)
|
|
}
|
|
|
|
async fn health_check(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
fn name(&self) -> &str {
|
|
"mock"
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn default_loader_formats_context() {
|
|
let loader = DefaultMemoryLoader::default();
|
|
let context = loader.load_context(&MockMemory, "hello").await.unwrap();
|
|
assert!(context.contains("[Memory context]"));
|
|
assert!(context.contains("- k: v"));
|
|
}
|
|
}
|