pub struct SessionMemory { /* private fields */ }Expand description
Session Memory 实现
使用 HashMap 存储会话期间的记忆,支持可选的文件持久化。
特性:
- Drop 时自动保存(如果 dirty)
- 原子文件写入
- 版本迁移支持
Implementations§
Source§impl SessionMemory
impl SessionMemory
Sourcepub fn with_persistence(
session_id: impl Into<String>,
backend: Arc<dyn FileBackend>,
auto_save: AutoSaveConfig,
) -> Self
pub fn with_persistence( session_id: impl Into<String>, backend: Arc<dyn FileBackend>, auto_save: AutoSaveConfig, ) -> Self
创建带文件持久化的 Session Memory
Sourcepub fn with_persistent_storage(
session_id: impl Into<String>,
path: impl Into<PathBuf>,
auto_save_on_drop: bool,
) -> Self
pub fn with_persistent_storage( session_id: impl Into<String>, path: impl Into<PathBuf>, auto_save_on_drop: bool, ) -> Self
Sourcepub fn with_json_backend(
session_id: impl Into<String>,
path: impl Into<PathBuf>,
) -> Self
pub fn with_json_backend( session_id: impl Into<String>, path: impl Into<PathBuf>, ) -> Self
创建带 JSON 文件持久化的 Session Memory
Sourcepub async fn load_from_file(
session_id: impl Into<String>,
path: impl Into<PathBuf>,
) -> Layer3Result<Self>
pub async fn load_from_file( session_id: impl Into<String>, path: impl Into<PathBuf>, ) -> Layer3Result<Self>
从文件加载已保存的会话
如果文件不存在,创建新的空会话。 如果文件存在,加载其中的数据并支持版本迁移。
Sourcepub async fn load_or_create(
session_id: impl Into<String>,
path: impl Into<PathBuf>,
) -> Layer3Result<Self>
pub async fn load_or_create( session_id: impl Into<String>, path: impl Into<PathBuf>, ) -> Layer3Result<Self>
尝试从文件加载,失败则创建新会话
Sourcepub async fn save(&self) -> Layer3Result<()>
pub async fn save(&self) -> Layer3Result<()>
手动保存到文件
Sourcepub fn session_id(&self) -> &str
pub fn session_id(&self) -> &str
获取会话 ID
Sourcepub fn changes_since_save(&self) -> usize
pub fn changes_since_save(&self) -> usize
获取变更计数
Sourcepub fn persistence_path(&self) -> Option<&Path>
pub fn persistence_path(&self) -> Option<&Path>
获取文件后端路径(如果有)
Trait Implementations§
Source§impl Default for SessionMemory
impl Default for SessionMemory
Source§impl Drop for SessionMemory
impl Drop for SessionMemory
Source§impl MemoryStore for SessionMemory
impl MemoryStore for SessionMemory
Source§fn tier(&self) -> MemoryTier
fn tier(&self) -> MemoryTier
记忆层级
Source§fn store<'life0, 'async_trait>(
&'life0 self,
entry: MemoryEntry,
) -> Pin<Box<dyn Future<Output = Layer3Result<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn store<'life0, 'async_trait>(
&'life0 self,
entry: MemoryEntry,
) -> Pin<Box<dyn Future<Output = Layer3Result<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
存储记忆条目
Source§fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Layer3Result<Option<MemoryEntry>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn get<'life0, 'life1, 'async_trait>(
&'life0 self,
id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Layer3Result<Option<MemoryEntry>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
获取记忆条目
Source§fn delete<'life0, 'life1, 'async_trait>(
&'life0 self,
id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Layer3Result<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn delete<'life0, 'life1, 'async_trait>(
&'life0 self,
id: &'life1 str,
) -> Pin<Box<dyn Future<Output = Layer3Result<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
删除记忆条目
Source§fn query<'life0, 'life1, 'async_trait>(
&'life0 self,
query: &'life1 MemoryQuery,
) -> Pin<Box<dyn Future<Output = Layer3Result<Vec<MemoryEntry>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn query<'life0, 'life1, 'async_trait>(
&'life0 self,
query: &'life1 MemoryQuery,
) -> Pin<Box<dyn Future<Output = Layer3Result<Vec<MemoryEntry>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
查询记忆(按内容搜索)
Source§fn list<'life0, 'async_trait>(
&'life0 self,
limit: Option<usize>,
) -> Pin<Box<dyn Future<Output = Layer3Result<Vec<MemoryEntry>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn list<'life0, 'async_trait>(
&'life0 self,
limit: Option<usize>,
) -> Pin<Box<dyn Future<Output = Layer3Result<Vec<MemoryEntry>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
列出所有记忆(按时间排序)
Auto Trait Implementations§
impl !RefUnwindSafe for SessionMemory
impl !UnwindSafe for SessionMemory
impl Freeze for SessionMemory
impl Send for SessionMemory
impl Sync for SessionMemory
impl Unpin for SessionMemory
impl UnsafeUnpin for SessionMemory
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more