use async_trait::async_trait;
use klieo_core::error::MemoryError;
use klieo_memory_graph::{EntityExtractor, EntityRef};
pub struct FallbackExtractor<P, S> {
pub primary: P,
pub secondary: S,
}
impl<P, S> FallbackExtractor<P, S> {
pub fn new(primary: P, secondary: S) -> Self {
Self { primary, secondary }
}
}
#[async_trait]
impl<P, S> EntityExtractor for FallbackExtractor<P, S>
where
P: EntityExtractor,
S: EntityExtractor,
{
async fn extract(
&self,
text: &str,
hints: &[EntityRef],
) -> Result<Vec<EntityRef>, MemoryError> {
let primary_hits = self.primary.extract(text, hints).await?;
if !primary_hits.is_empty() {
return Ok(primary_hits);
}
self.secondary.extract(text, hints).await
}
}