second-brain-adapters 0.12.0

Conversation parsers for second-brain: Claude Code and Cursor JSONL adapters
Documentation
use chrono::{DateTime, Utc};
use second_brain_core::schema::{Conversation, Memory, MemoryType};
use uuid::Uuid;

use crate::IngestResult;

pub(crate) fn v5(name: &str) -> Uuid {
    Uuid::new_v5(&Uuid::NAMESPACE_OID, name.as_bytes())
}

pub(crate) struct Document<'a> {
    pub content: String,
    pub memory_id: Uuid,
    pub memory_type: MemoryType,
    pub source: &'a str,
    pub source_id: String,
    pub conversation_id: Uuid,
    pub at: Option<DateTime<Utc>>,
    pub project: String,
    pub machine_id: &'a str,
}

impl Document<'_> {
    pub(crate) fn into_result(self) -> IngestResult {
        let conversation = Conversation {
            id: self.conversation_id,
            source: self.source.to_string(),
            machine_id: self.machine_id.to_string(),
            started_at: self.at.unwrap_or_else(Utc::now),
            project_path: Some(self.project.clone()),
        };
        let mut memory = Memory::new(
            self.content,
            self.memory_type,
            self.source.to_string(),
            self.source_id,
        )
        .with_project_path(Some(self.project))
        .with_machine_id(self.machine_id.to_string());
        memory.id = self.memory_id;
        memory.session_at = self.at;
        memory.conversation_id = Some(conversation.id);
        IngestResult {
            conversation,
            memories: vec![memory],
            entities: Vec::new(),
            entities_extracted: 0,
        }
    }
}

/// The id key of a document version: keyed also by the version `after` it
/// follows, so a text restored after an edit gets a fresh id rather than the
/// one its earlier appearance holds.
pub(crate) fn versioned(key: &str, after: Option<Uuid>) -> String {
    match after {
        Some(previous) => format!("{key}:after:{previous}"),
        None => key.to_string(),
    }
}