Files
agcore/src/memory/store.rs
T
徐涛 932a06f512 refactor(core): 完成 v0.3.2 Cargo features 拆分基础设施
- 定义 16 个 features + 4 个快捷组合,default = ["full"] 保持向后兼容
- 12 个重型依赖 optional 化(tokio/reqwest/rusqlite 等)
- 全模块 #[cfg(feature)] 门控注入(llm/tools/memory/agent/engine)
- 将 LlmProvider trait 及关联类型移出 provider 模块归属 llm(ADR-1)
- 为 session.rs bundle() 方法添加 engine feature 门控
- 更新 12 个内部文件 + 4 个示例文件的 import 路径
- 向后兼容:provider.rs 保留 pub use 重导出老路径
2026-07-19 07:58:04 +08:00

65 lines
1.8 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! MemoryStore 抽象接口与默认实现。
use async_trait::async_trait;
use crate::memory::error::MemoryError;
use crate::memory::types::{MemoryFilter, MemoryItem};
pub mod in_memory;
#[cfg(feature = "memory-sqlite")]
pub mod sqlite_store;
pub use in_memory::InMemoryStore;
#[cfg(feature = "memory-sqlite")]
pub use sqlite_store::SqliteStore;
/// 底层记忆存储抽象接口。
///
/// 下游可实现此 trait 以对接持久化后端(JSON 文件、SQLite、Redis 等)。
/// 默认实现 [`InMemoryStore`] 基于进程内 HashMap。
#[async_trait]
pub trait MemoryStore: Send + Sync {
/// 保存/覆盖一个 MemoryItemupsert 语义)。
/// - 如果 id 不存在,则插入新条目
/// - 如果 id 已存在,则覆盖旧条目
async fn save(&self, item: MemoryItem) -> Result<(), MemoryError>;
/// 根据 id 获取一个 MemoryItem。
async fn get(&self, id: &str) -> Result<Option<MemoryItem>, MemoryError>;
/// 根据 id 删除一个 MemoryItem。
async fn delete(&self, id: &str) -> Result<(), MemoryError>;
/// 根据 filter 列出 MemoryItem。
async fn list(&self, filter: &MemoryFilter) -> Result<Vec<MemoryItem>, MemoryError>;
}
/// 淘汰策略。
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum EvictionPolicy {
/// 不淘汰(默认)。
None,
/// 超过存活时间(秒)淘汰。
Ttl { ttl_secs: u64 },
/// 超过容量上限淘汰最旧(基于 created_at)。
Capacity { max_items: usize },
}
/// 淘汰配置。
#[derive(Debug, Clone)]
pub struct EvictionConfig {
pub policy: EvictionPolicy,
/// 每写入 N 条后检查一次淘汰条件。
pub check_interval: usize,
}
impl Default for EvictionConfig {
fn default() -> Self {
Self {
policy: EvictionPolicy::None,
check_interval: 64,
}
}
}