second-brain-api 0.13.0

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
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;

/// The type given to an entity a caller names in `related_to` that the
/// gazetteer does not recognise.
const NAMED_ENTITY_TYPE: &str = "concept";

/// Links `memory_id` to every entity the ingest gazetteer finds in `content`
/// and to every name in `related_to`, creating entities that do not exist
/// yet. The edges are `mentions` because that is the one memory-to-entity
/// relation sync and the dangling-relation prune accept, and the one the
/// recall graph term reads. Returns how many distinct entities were linked.
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())
}