use crate::fact::{Fact, FactFilter, FactId, FactPatch};
use crate::scope::Scope;
use async_trait::async_trait;
use serde::Serialize;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum MemoryError {
#[error("database error: {0}")]
Database(String),
#[error("serialization error: {0}")]
Serialization(String),
#[error("not found: {0}")]
NotFound(String),
#[error("embedding error: {0}")]
Embedding(String),
#[error("graph error: {0}")]
Graph(String),
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct StoreStats {
pub total_facts: u64,
pub valid_facts: u64,
pub invalidated_facts: u64,
pub total_entities: u64,
pub total_relationships: u64,
}
#[async_trait]
pub trait FactStore: Send + Sync {
async fn insert_fact(&self, fact: Fact) -> Result<FactId, MemoryError>;
async fn get_fact(&self, id: FactId) -> Result<Fact, MemoryError>;
async fn update_fact(&self, id: FactId, patch: FactPatch) -> Result<Fact, MemoryError>;
async fn list_facts(&self, filter: &FactFilter) -> Result<Vec<Fact>, MemoryError>;
async fn invalidate_fact(&self, id: FactId) -> Result<(), MemoryError>;
async fn delete_scope_data(&self, scope: &Scope) -> Result<u64, MemoryError>;
async fn export(&self, filter: &FactFilter) -> Result<Vec<Fact>, MemoryError>;
async fn import(&self, facts: Vec<Fact>) -> Result<u64, MemoryError>;
async fn stats(&self) -> Result<StoreStats, MemoryError>;
async fn record_access(&self, id: FactId) -> Result<(), MemoryError>;
async fn keyword_search(
&self,
query: &str,
scope: &Scope,
top_k: usize,
) -> Result<Vec<Fact>, MemoryError>;
}