second-brain-adapters 0.12.0

Conversation parsers for second-brain: Claude Code and Cursor JSONL adapters
Documentation
pub mod claude_code;
pub mod claude_memory;
pub mod cursor;
mod document;
pub mod git;
pub mod markdown;
mod persist;
pub mod raw_text;
pub mod repo_instructions;
pub mod session_evidence;

pub use persist::{IngestStore, StoreOutcome, store_document_result, store_ingest_result};

use std::path::Path;

use anyhow::Result;
use second_brain_core::schema::{Conversation, Memory};

/// The project a transcript belongs to is the directory directly under the
/// adapter's `projects` root. Nothing below that level is a project: Claude
/// Code nests subagent transcripts as `<session>/subagents/*.jsonl` and
/// Cursor nests its own as `agent-transcripts/<session>/*.jsonl`, so the
/// file's parent directory names the wrong thing for both.
pub fn project_slug(projects_root: &Path, transcript: &Path) -> Option<String> {
    transcript
        .strip_prefix(projects_root)
        .ok()?
        .components()
        .next()
        .map(|c| c.as_os_str().to_string_lossy().to_string())
}

/// Claude Code's project slug for a working directory: every character that is
/// not an ASCII letter or digit becomes `-`.
///
/// Claude Code replaces UTF-16 code units, so a character outside the Basic
/// Multilingual Plane becomes two dashes.
pub fn encode_project_dir(path: &Path) -> String {
    path.to_string_lossy()
        .chars()
        .flat_map(|c| {
            let dashes = if c.is_ascii_alphanumeric() {
                0
            } else {
                c.len_utf16()
            };
            std::iter::repeat_n('-', dashes).chain((dashes == 0).then_some(c))
        })
        .collect()
}

pub trait Adapter: Send + Sync {
    fn name(&self) -> &str;
    fn discover_conversations(&self) -> Result<Vec<ConversationSource>>;
    fn ingest_conversation(&self, source: &ConversationSource) -> Result<IngestResult>;
    /// The key under which a copy of this file at another path counts as
    /// the same conversation. None means files are told apart by path only.
    fn session_key(&self, _path: &Path, _content: &str) -> Option<String> {
        None
    }
}

#[derive(Debug, Clone)]
pub struct ConversationSource {
    pub id: String,
    pub path: String,
    pub source_name: String,
    pub machine_id: String,
}

#[derive(Debug, Clone)]
pub struct ExtractedEntity {
    pub name: String,
    pub entity_type: String,
}

#[derive(Debug, Clone)]
pub struct IngestResult {
    pub conversation: Conversation,
    pub memories: Vec<Memory>,
    pub entities: Vec<(ExtractedEntity, uuid::Uuid)>,
    pub entities_extracted: usize,
}