second-brain-core 0.13.5

Core library for second-brain: SQLite storage with vector and lexical indexes, BGE embeddings, and hybrid retrieval
Documentation
use std::path::Path;

/// The project name Claude Code gives a working directory, and the
/// `project_path` ingest stores for it: every character that is not an ASCII
/// letter or digit becomes `-`. The MCP server applies the same rule itself,
/// because it talks to the daemon over HTTP without depending on core.
pub fn encode(dir: &str) -> String {
    dir.chars()
        .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
        .collect()
}

/// The project directory of a transcript path: the component after the
/// last `projects` component, mirroring `adapters::project_slug`. The last
/// one is taken because a home directory may itself contain a `projects`
/// segment, while nothing below the project directory is ever named that.
pub fn from_transcript(path: &str) -> Option<String> {
    let parts: Vec<String> = Path::new(path)
        .components()
        .map(|c| c.as_os_str().to_string_lossy().to_string())
        .collect();
    let root = parts.iter().rposition(|c| c == "projects")?;
    // The slug must be a directory, so at least one component follows it.
    if root + 2 >= parts.len() {
        return None;
    }
    Some(parts[root + 1].clone())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn every_non_alphanumeric_character_becomes_a_dash() {
        assert_eq!(encode("/srv/my_app.v2"), "-srv-my-app-v2");
        assert_eq!(encode("/home/a b/ünï"), "-home-a-b--n-");
        assert_eq!(encode(""), "");
    }

    #[test]
    fn a_transcript_path_names_the_directory_after_the_last_projects() {
        assert_eq!(
            from_transcript("/home/u/projects/x/.claude/projects/-srv-app/s.jsonl").as_deref(),
            Some("-srv-app")
        );
        assert_eq!(from_transcript("/home/u/.claude/projects/s.jsonl"), None);
        assert_eq!(from_transcript("/tmp/transcript.jsonl"), None);
    }
}