Skip to main content

second_brain_adapters/
lib.rs

1pub mod claude_code;
2pub mod claude_memory;
3pub mod cursor;
4mod document;
5pub mod git;
6pub mod markdown;
7mod persist;
8pub mod raw_text;
9pub mod repo_instructions;
10pub mod session_evidence;
11
12pub use persist::{IngestStore, StoreOutcome, store_document_result, store_ingest_result};
13
14use std::path::Path;
15
16use anyhow::Result;
17use second_brain_core::schema::{Conversation, Memory};
18
19/// The project a transcript belongs to is the directory directly under the
20/// adapter's `projects` root. Nothing below that level is a project: Claude
21/// Code nests subagent transcripts as `<session>/subagents/*.jsonl` and
22/// Cursor nests its own as `agent-transcripts/<session>/*.jsonl`, so the
23/// file's parent directory names the wrong thing for both.
24pub fn project_slug(projects_root: &Path, transcript: &Path) -> Option<String> {
25    transcript
26        .strip_prefix(projects_root)
27        .ok()?
28        .components()
29        .next()
30        .map(|c| c.as_os_str().to_string_lossy().to_string())
31}
32
33/// Claude Code's project slug for a working directory: every character that is
34/// not an ASCII letter or digit becomes `-`.
35///
36/// Claude Code replaces UTF-16 code units, so a character outside the Basic
37/// Multilingual Plane becomes two dashes.
38pub fn encode_project_dir(path: &Path) -> String {
39    path.to_string_lossy()
40        .chars()
41        .flat_map(|c| {
42            let dashes = if c.is_ascii_alphanumeric() {
43                0
44            } else {
45                c.len_utf16()
46            };
47            std::iter::repeat_n('-', dashes).chain((dashes == 0).then_some(c))
48        })
49        .collect()
50}
51
52pub trait Adapter: Send + Sync {
53    fn name(&self) -> &str;
54    fn discover_conversations(&self) -> Result<Vec<ConversationSource>>;
55    fn ingest_conversation(&self, source: &ConversationSource) -> Result<IngestResult>;
56    /// The key under which a copy of this file at another path counts as
57    /// the same conversation. None means files are told apart by path only.
58    fn session_key(&self, _path: &Path, _content: &str) -> Option<String> {
59        None
60    }
61}
62
63#[derive(Debug, Clone)]
64pub struct ConversationSource {
65    pub id: String,
66    pub path: String,
67    pub source_name: String,
68    pub machine_id: String,
69}
70
71#[derive(Debug, Clone)]
72pub struct ExtractedEntity {
73    pub name: String,
74    pub entity_type: String,
75}
76
77#[derive(Debug, Clone)]
78pub struct IngestResult {
79    pub conversation: Conversation,
80    pub memories: Vec<Memory>,
81    pub entities: Vec<(ExtractedEntity, uuid::Uuid)>,
82    pub entities_extracted: usize,
83}