use anyhow::Result;
use second_brain_adapters::claude_code::extract_entities;
use second_brain_core::schema::{Relation, RelationType};
use second_brain_core::store::StoreAdmin;
use uuid::Uuid;
const NAMED_ENTITY_TYPE: &str = "concept";
pub fn link_mentions(
store: &dyn StoreAdmin,
memory_id: Uuid,
content: &str,
related_to: &[String],
) -> Result<usize> {
let mut wanted: Vec<(String, String)> = extract_entities(content)
.into_iter()
.map(|e| (e.name, e.entity_type))
.collect();
for name in related_to
.iter()
.map(|n| n.trim())
.filter(|n| !n.is_empty())
{
if !wanted.iter().any(|(w, _)| w.eq_ignore_ascii_case(name)) {
wanted.push((name.to_string(), NAMED_ENTITY_TYPE.to_string()));
}
}
for (name, entity_type) in &wanted {
let entity = store.find_or_create_entity(name, entity_type)?;
store.store_relation(&Relation {
from_id: memory_id,
to_id: entity.id,
relation_type: RelationType::Mentions,
strength: 1.0,
context: None,
})?;
}
Ok(wanted.len())
}