pub trait Memory: Send + Sync {
// Required methods
fn store<'life0, 'life1, 'async_trait>(
&'life0 mut self,
key: &'life1 str,
value: MemoryValue,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn retrieve<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<Option<MemoryValue>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn remove<'life0, 'life1, 'async_trait>(
&'life0 mut self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn search<'life0, 'life1, 'async_trait>(
&'life0 self,
query: &'life1 str,
limit: usize,
) -> Pin<Box<dyn Future<Output = AgentResult<Vec<MemoryItem>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn clear<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn get_history<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<Vec<Message>>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn add_to_history<'life0, 'life1, 'async_trait>(
&'life0 mut self,
session_id: &'life1 str,
message: Message,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn clear_history<'life0, 'life1, 'async_trait>(
&'life0 mut self,
session_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
// Provided methods
fn contains<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
fn stats<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = AgentResult<MemoryStats>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait { ... }
fn memory_type(&self) -> &str { ... }
}Expand description
记忆组件 Trait
负责 Agent 的记忆存储和检索
§示例
ⓘ
use mofa_kernel::agent::components::memory::{Memory, MemoryValue, MemoryItem};
struct InMemoryStorage {
data: HashMap<String, MemoryValue>,
}
#[async_trait]
impl Memory for InMemoryStorage {
async fn store(&mut self, key: &str, value: MemoryValue) -> AgentResult<()> {
self.data.insert(key.to_string(), value);
Ok(())
}
async fn retrieve(&self, key: &str) -> AgentResult<Option<MemoryValue>> {
Ok(self.data.get(key).cloned())
}
// ... other methods
}Required Methods§
Sourcefn store<'life0, 'life1, 'async_trait>(
&'life0 mut self,
key: &'life1 str,
value: MemoryValue,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn store<'life0, 'life1, 'async_trait>(
&'life0 mut self,
key: &'life1 str,
value: MemoryValue,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
存储记忆项
Sourcefn retrieve<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<Option<MemoryValue>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn retrieve<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<Option<MemoryValue>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
检索记忆项
Sourcefn remove<'life0, 'life1, 'async_trait>(
&'life0 mut self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn remove<'life0, 'life1, 'async_trait>(
&'life0 mut self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
删除记忆项
Sourcefn search<'life0, 'life1, 'async_trait>(
&'life0 self,
query: &'life1 str,
limit: usize,
) -> Pin<Box<dyn Future<Output = AgentResult<Vec<MemoryItem>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn search<'life0, 'life1, 'async_trait>(
&'life0 self,
query: &'life1 str,
limit: usize,
) -> Pin<Box<dyn Future<Output = AgentResult<Vec<MemoryItem>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
语义搜索
Sourcefn clear<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn clear<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
清空所有记忆
Sourcefn get_history<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<Vec<Message>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get_history<'life0, 'life1, 'async_trait>(
&'life0 self,
session_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<Vec<Message>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
获取对话历史
Sourcefn add_to_history<'life0, 'life1, 'async_trait>(
&'life0 mut self,
session_id: &'life1 str,
message: Message,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn add_to_history<'life0, 'life1, 'async_trait>(
&'life0 mut self,
session_id: &'life1 str,
message: Message,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
添加对话消息
Sourcefn clear_history<'life0, 'life1, 'async_trait>(
&'life0 mut self,
session_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn clear_history<'life0, 'life1, 'async_trait>(
&'life0 mut self,
session_id: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
清空对话历史
Provided Methods§
Sourcefn contains<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn contains<'life0, 'life1, 'async_trait>(
&'life0 self,
key: &'life1 str,
) -> Pin<Box<dyn Future<Output = AgentResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
检查是否存在
Sourcefn stats<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = AgentResult<MemoryStats>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn stats<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = AgentResult<MemoryStats>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
获取记忆统计
Sourcefn memory_type(&self) -> &str
fn memory_type(&self) -> &str
记忆类型名称