use crate::types::EntityRef;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use klieo_core::error::MemoryError;
use klieo_core::ids::FactId;
use klieo_core::memory::{Fact, LongTermMemory, Scope};
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct IndexEntry {
#[allow(missing_docs)]
pub fact_id: FactId,
#[allow(missing_docs)]
pub entities: Vec<EntityRef>,
pub text: String,
pub valid_from: Option<DateTime<Utc>>,
}
impl IndexEntry {
#[allow(missing_docs)]
pub fn new(
fact_id: FactId,
entities: Vec<EntityRef>,
text: impl Into<String>,
valid_from: Option<DateTime<Utc>>,
) -> Self {
Self {
fact_id,
entities,
text: text.into(),
valid_from,
}
}
}
#[async_trait]
pub trait KnowledgeGraph: Send + Sync {
async fn index(
&self,
scope: Scope,
fact_id: &FactId,
entities: &[EntityRef],
text: &str,
valid_from: Option<DateTime<Utc>>,
) -> Result<(), MemoryError>;
async fn index_many(&self, scope: Scope, batch: &[IndexEntry]) -> Result<(), MemoryError> {
for entry in batch {
if entry.entities.is_empty() {
continue;
}
self.index(
scope.clone(),
&entry.fact_id,
&entry.entities,
&entry.text,
entry.valid_from,
)
.await?;
}
Ok(())
}
async fn neighbors(
&self,
scope: &Scope,
entities: &[EntityRef],
) -> Result<Vec<FactId>, MemoryError>;
async fn recall_paths(
&self,
scope: &Scope,
entities: &[EntityRef],
) -> Result<Vec<crate::RetrievalPath>, MemoryError> {
let _ = (scope, entities);
Ok(Vec::new())
}
async fn subgraph(&self, scope: &Scope, limit: usize) -> Result<crate::GraphView, MemoryError> {
let _ = (scope, limit);
Ok(crate::GraphView::default())
}
async fn forget(&self, scope: &Scope, fact_id: &FactId) -> Result<(), MemoryError> {
let _ = (scope, fact_id);
Ok(())
}
}
#[async_trait]
pub trait FilterableLongTermMemory: LongTermMemory {
async fn recall_filtered(
&self,
scope: Scope,
query: &str,
k: usize,
candidate_ids: &[FactId],
) -> Result<Vec<Fact>, MemoryError>;
fn embedder_id(&self) -> &str;
}
#[async_trait]
pub trait EntityExtractor: Send + Sync {
async fn extract(&self, text: &str, hints: &[EntityRef])
-> Result<Vec<EntityRef>, MemoryError>;
}