Skip to main content

second_brain_core/
project_path.rs

1use std::path::Path;
2
3/// The project name Claude Code gives a working directory, and the
4/// `project_path` ingest stores for it: every character that is not an ASCII
5/// letter or digit becomes `-`. The MCP server applies the same rule itself,
6/// because it talks to the daemon over HTTP without depending on core.
7pub fn encode(dir: &str) -> String {
8    dir.chars()
9        .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
10        .collect()
11}
12
13/// The project directory of a transcript path: the component after the
14/// last `projects` component, mirroring `adapters::project_slug`. The last
15/// one is taken because a home directory may itself contain a `projects`
16/// segment, while nothing below the project directory is ever named that.
17pub fn from_transcript(path: &str) -> Option<String> {
18    let parts: Vec<String> = Path::new(path)
19        .components()
20        .map(|c| c.as_os_str().to_string_lossy().to_string())
21        .collect();
22    let root = parts.iter().rposition(|c| c == "projects")?;
23    // The slug must be a directory, so at least one component follows it.
24    if root + 2 >= parts.len() {
25        return None;
26    }
27    Some(parts[root + 1].clone())
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33
34    #[test]
35    fn every_non_alphanumeric_character_becomes_a_dash() {
36        assert_eq!(encode("/srv/my_app.v2"), "-srv-my-app-v2");
37        assert_eq!(encode("/home/a b/ünï"), "-home-a-b--n-");
38        assert_eq!(encode(""), "");
39    }
40
41    #[test]
42    fn a_transcript_path_names_the_directory_after_the_last_projects() {
43        assert_eq!(
44            from_transcript("/home/u/projects/x/.claude/projects/-srv-app/s.jsonl").as_deref(),
45            Some("-srv-app")
46        );
47        assert_eq!(from_transcript("/home/u/.claude/projects/s.jsonl"), None);
48        assert_eq!(from_transcript("/tmp/transcript.jsonl"), None);
49    }
50}