use smos_domain::{FactId, MemoryKey, SessionId};
use crate::errors::UseCaseError;
use crate::ports::{
Clock, Delay, EmbeddingProvider, FactRepository, LlmExtractor, SessionRepository,
};
use super::ExtractFactsFromResponse;
impl<'a, FR, SR, EP, LE, C, D> ExtractFactsFromResponse<'a, FR, SR, EP, LE, C, D>
where
FR: FactRepository,
SR: SessionRepository,
EP: EmbeddingProvider,
LE: LlmExtractor,
C: Clock,
D: Delay,
{
pub(crate) async fn persist_facts(
&self,
raw_facts: &[String],
memory_key: &MemoryKey,
session_id: &SessionId,
) -> Result<Vec<FactId>, UseCaseError> {
let refs: Vec<&str> = raw_facts.iter().map(String::as_str).collect();
let embeddings = self.embedder.embed_batch(&refs).await?;
let mut new_ids = Vec::new();
for (raw, embedding) in raw_facts.iter().zip(embeddings) {
let Some(vector) = embedding else { continue };
if let Some(id) = self
.persist_one_fact(raw, vector, memory_key, session_id)
.await?
{
new_ids.push(id);
}
}
Ok(new_ids)
}
}