Skip to main content

second_brain_adapters/
claude_code.rs

1use std::collections::HashSet;
2use std::path::PathBuf;
3
4use anyhow::{Context, Result};
5use chrono::{DateTime, Utc};
6use uuid::Uuid;
7
8use second_brain_core::schema::{Conversation, Memory, MemoryType};
9
10use crate::{Adapter, ConversationSource, ExtractedEntity, IngestResult};
11
12pub struct ClaudeCodeAdapter {
13    base_path: PathBuf,
14    machine_id: String,
15}
16
17impl ClaudeCodeAdapter {
18    pub fn new(machine_id: String) -> Self {
19        let base_path = dirs_home().join(".claude");
20        Self {
21            base_path,
22            machine_id,
23        }
24    }
25
26    pub fn with_path(base_path: PathBuf, machine_id: String) -> Self {
27        Self {
28            base_path,
29            machine_id,
30        }
31    }
32
33    fn parse_conversation_file(
34        &self,
35        path: &PathBuf,
36    ) -> Result<(
37        Vec<Memory>,
38        Vec<(ExtractedEntity, Uuid)>,
39        Option<DateTime<Utc>>,
40    )> {
41        let content = std::fs::read_to_string(path)
42            .with_context(|| format!("reading conversation file: {}", path.display()))?;
43
44        let mut memories = Vec::new();
45        let mut all_entities: Vec<(ExtractedEntity, Uuid)> = Vec::new();
46        let mut seen_entities: HashSet<String> = HashSet::new();
47        let mut first_timestamp: Option<DateTime<Utc>> = None;
48        let source_id = path.to_string_lossy().to_string();
49
50        for line in content.lines() {
51            if line.trim().is_empty() {
52                continue;
53            }
54
55            let msg: serde_json::Value = match serde_json::from_str(line) {
56                Ok(v) => v,
57                Err(_) => continue,
58            };
59
60            let msg_type = msg.get("type").and_then(|t| t.as_str()).unwrap_or("");
61
62            if first_timestamp.is_none() {
63                if let Some(ts) = msg.get("timestamp").and_then(|t| t.as_str()) {
64                    first_timestamp = DateTime::parse_from_rfc3339(ts)
65                        .map(|dt| dt.with_timezone(&Utc))
66                        .ok();
67                }
68            }
69
70            if msg_type != "assistant" {
71                continue;
72            }
73
74            let message = match msg.get("message") {
75                Some(m) => m,
76                None => continue,
77            };
78
79            let content_blocks = match message.get("content").and_then(|c| c.as_array()) {
80                Some(blocks) => blocks,
81                None => continue,
82            };
83
84            for block in content_blocks {
85                let block_type = block.get("type").and_then(|t| t.as_str()).unwrap_or("");
86
87                let text = match block_type {
88                    "text" => block.get("text").and_then(|t| t.as_str()),
89                    _ => None,
90                };
91
92                if let Some(text) = text {
93                    let chunks = chunk_text(text);
94                    for chunk in chunks {
95                        if !is_memorable(&chunk) {
96                            continue;
97                        }
98                        let memory_type = classify_memory(&chunk);
99                        let memory = Memory::new(
100                            chunk.clone(),
101                            memory_type,
102                            "claude-code".to_string(),
103                            source_id.clone(),
104                        );
105                        let memory_id = memory.id;
106                        memories.push(memory);
107
108                        for entity in extract_entities(&chunk) {
109                            let key = entity.name.to_lowercase();
110                            if seen_entities.insert(key) {
111                                all_entities.push((entity, memory_id));
112                            }
113                        }
114                    }
115                }
116            }
117        }
118
119        Ok((memories, all_entities, first_timestamp))
120    }
121}
122
123impl Adapter for ClaudeCodeAdapter {
124    fn name(&self) -> &str {
125        "claude-code"
126    }
127
128    fn discover_conversations(&self) -> Result<Vec<ConversationSource>> {
129        let projects_dir = self.base_path.join("projects");
130        let mut sources = Vec::new();
131
132        if !projects_dir.exists() {
133            return Ok(sources);
134        }
135
136        let mut dirs = vec![projects_dir];
137        while let Some(dir) = dirs.pop() {
138            for entry in std::fs::read_dir(&dir)? {
139                let entry = entry?;
140                let path = entry.path();
141                if path.is_dir() {
142                    dirs.push(path);
143                } else if path.extension().is_some_and(|e| e == "jsonl") {
144                    sources.push(ConversationSource {
145                        id: path.to_string_lossy().to_string(),
146                        path: path.to_string_lossy().to_string(),
147                        source_name: "claude-code".to_string(),
148                        machine_id: self.machine_id.clone(),
149                    });
150                }
151            }
152        }
153
154        Ok(sources)
155    }
156
157    fn ingest_conversation(&self, source: &ConversationSource) -> Result<IngestResult> {
158        let path = PathBuf::from(&source.path);
159        let (memories, entities, started_at) = self.parse_conversation_file(&path)?;
160
161        let project_dir = path
162            .parent()
163            .and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()));
164
165        let conversation = Conversation {
166            id: Uuid::new_v4(),
167            source: "claude-code".to_string(),
168            machine_id: source.machine_id.clone(),
169            started_at: started_at.unwrap_or_else(Utc::now),
170            project_path: project_dir,
171        };
172
173        let entity_count = entities.len();
174
175        Ok(IngestResult {
176            conversation,
177            memories,
178            entities,
179            entities_extracted: entity_count,
180        })
181    }
182}
183
184fn dirs_home() -> PathBuf {
185    std::env::var("HOME")
186        .map(PathBuf::from)
187        .unwrap_or_else(|_| PathBuf::from("/tmp"))
188}
189
190fn chunk_text(text: &str) -> Vec<String> {
191    let mut chunks = Vec::new();
192    let mut current = String::new();
193
194    for paragraph in text.split("\n\n") {
195        let trimmed = paragraph.trim();
196        if trimmed.is_empty() {
197            continue;
198        }
199
200        if current.len() + trimmed.len() > 1500 && !current.is_empty() {
201            chunks.push(std::mem::take(&mut current));
202        }
203
204        if !current.is_empty() {
205            current.push_str("\n\n");
206        }
207        current.push_str(trimmed);
208
209        if current.len() >= 800 {
210            chunks.push(std::mem::take(&mut current));
211        }
212    }
213
214    if !current.is_empty() {
215        chunks.push(current);
216    }
217
218    if chunks.is_empty() && !text.trim().is_empty() {
219        chunks.push(text.trim().to_string());
220    }
221
222    chunks
223}
224
225fn is_memorable(text: &str) -> bool {
226    if text.len() < 50 {
227        return false;
228    }
229
230    let code_block_count = text.matches("```").count() / 2;
231    if code_block_count > 8 {
232        return false;
233    }
234
235    let non_code_lines = text
236        .lines()
237        .filter(|l| {
238            let trimmed = l.trim();
239            !trimmed.is_empty() && !trimmed.starts_with("```") && !trimmed.starts_with('\t')
240        })
241        .count();
242    non_code_lines >= 1
243}
244
245fn classify_memory(text: &str) -> MemoryType {
246    let lower = text.to_lowercase();
247    let decision_markers = [
248        "decided",
249        "chose",
250        "went with",
251        "picked",
252        "agreed",
253        "choosing",
254        "selected",
255        "switched to",
256        "migrated",
257        "deployed",
258        "shipped",
259        "found that",
260        "discovered",
261        "realized",
262        "confirmed",
263        "concluded",
264    ];
265    let procedural_markers = [
266        "to do this",
267        "steps:",
268        "step 1",
269        "first,",
270        "then,",
271        "finally,",
272        "run ",
273        "install ",
274        "execute",
275        "command:",
276        "configure",
277        "set up",
278        "to fix",
279        "solution:",
280        "you need to",
281        "you can ",
282        "you should",
283        "here's how",
284        "the fix",
285    ];
286
287    if decision_markers.iter().any(|m| lower.contains(m)) {
288        MemoryType::Episodic
289    } else if procedural_markers.iter().any(|m| lower.contains(m)) {
290        MemoryType::Procedural
291    } else {
292        MemoryType::Semantic
293    }
294}
295
296const TECH_NAMES: &[&str] = &[
297    "rust",
298    "python",
299    "javascript",
300    "typescript",
301    "go",
302    "java",
303    "c++",
304    "ruby",
305    "swift",
306    "kotlin",
307    "react",
308    "vue",
309    "angular",
310    "svelte",
311    "next.js",
312    "nuxt",
313    "django",
314    "flask",
315    "fastapi",
316    "express",
317    "axum",
318    "actix",
319    "tokio",
320    "serde",
321    "docker",
322    "kubernetes",
323    "k8s",
324    "terraform",
325    "ansible",
326    "nginx",
327    "apache",
328    "redis",
329    "postgres",
330    "postgresql",
331    "mysql",
332    "sqlite",
333    "mongodb",
334    "kuzudb",
335    "neo4j",
336    "elasticsearch",
337    "kafka",
338    "rabbitmq",
339    "nats",
340    "grpc",
341    "graphql",
342    "rest",
343    "websocket",
344    "ssh",
345    "tailscale",
346    "wireguard",
347    "git",
348    "github",
349    "gitlab",
350    "linux",
351    "ubuntu",
352    "debian",
353    "macos",
354    "windows",
355    "arm64",
356    "aarch64",
357    "x86",
358    "onnx",
359    "pytorch",
360    "tensorflow",
361    "huggingface",
362    "openai",
363    "anthropic",
364    "claude",
365    "gpt",
366    "llm",
367    "mcp",
368    "lsp",
369    "systemd",
370    "cron",
371    "tmux",
372    "vim",
373    "neovim",
374    "cargo",
375    "npm",
376    "pip",
377    "uv",
378    "bun",
379    "deno",
380    "node.js",
381    "node",
382    "aws",
383    "gcp",
384    "azure",
385    "s3",
386    "ec2",
387    "lambda",
388    "cloudflare",
389    "raspberry pi",
390    "pi",
391    "jetson",
392    "esp32",
393    "arduino",
394    "toml",
395    "yaml",
396    "json",
397    "jsonl",
398    "csv",
399    "protobuf",
400    "msgpack",
401];
402
403fn extract_entities(text: &str) -> Vec<ExtractedEntity> {
404    let mut entities = Vec::new();
405    let mut seen = HashSet::new();
406    let lower = text.to_lowercase();
407
408    for &tech in TECH_NAMES {
409        if lower.contains(tech) {
410            let key = tech.to_lowercase();
411            if seen.insert(key) {
412                entities.push(ExtractedEntity {
413                    name: tech.to_string(),
414                    entity_type: "technology".to_string(),
415                });
416            }
417        }
418    }
419
420    for word in text.split_whitespace() {
421        let clean = word.trim_matches(|c: char| {
422            !c.is_alphanumeric() && c != '/' && c != '.' && c != '-' && c != '_'
423        });
424        if clean.contains('/') && clean.contains('.') {
425            let ext_match = clean.rsplit('.').next().unwrap_or("");
426            let is_source_file = matches!(
427                ext_match,
428                "rs" | "py"
429                    | "ts"
430                    | "tsx"
431                    | "js"
432                    | "jsx"
433                    | "go"
434                    | "java"
435                    | "rb"
436                    | "sh"
437                    | "toml"
438                    | "json"
439                    | "yaml"
440                    | "yml"
441                    | "md"
442                    | "sql"
443                    | "html"
444                    | "css"
445            );
446            if is_source_file {
447                let key = clean.to_lowercase();
448                if seen.insert(key) {
449                    entities.push(ExtractedEntity {
450                        name: clean.to_string(),
451                        entity_type: "file_path".to_string(),
452                    });
453                }
454            }
455        }
456    }
457
458    entities
459}