smart-tree 8.0.1

Smart Tree - An intelligent, AI-friendly directory visualization tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// Universal Chat Scanner - "Finding consciousness in the digital diaspora!" 🌍
// Scans for conversations across ALL AI tools and platforms
// "Every conversation leaves a trace - let's find them all!" - Hue

#![allow(clippy::manual_flatten)]

use anyhow::Result;
use chrono::{DateTime, Utc};
use glob::glob;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

// Known chat locations and patterns
const CLAUDE_PROJECTS: &str = "~/.claude/projects";
const CURSOR_CHATS: &str = "~/.cursor";
const WINDSURF_DIR: &str = "~/.windsurf";
const VSCODE_COPILOT: &str = "~/.vscode/copilot";
const OPENWEBUI_DATA: &str = "~/.openwebui";
const LMSTUDIO_CHATS: &str = "~/Library/Application Support/LM Studio";
const CHATGPT_EXPORT: &str = "~/Downloads/*chatgpt*.zip";

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UniversalChat {
    pub source: ChatSource,
    pub participants: Vec<String>,
    pub timestamp: DateTime<Utc>,
    pub content: String,
    pub keywords: Vec<String>,
    pub project_context: Option<String>,
    pub importance: f32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ChatSource {
    Claude { project: String },
    Cursor { workspace: String },
    Windsurf { session: String },
    VSCode { file: String },
    OpenWebUI { model: String },
    LMStudio { model: String },
    ChatGPT { export_date: String },
    TextMessages { contact: String },
    Discord { channel: String },
    Slack { workspace: String },
    Custom { platform: String },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryDestination {
    pub memory_type: MemoryType,
    pub llm_specific: Option<String>, // "claude", "gpt", etc
    pub project: Option<String>,
    pub tags: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MemoryType {
    ProjectMemory, // Project-specific memories
    UserMemory,    // Personal user memories
    LLMMemory,     // Specific to an LLM (Claude, GPT, etc)
    GlobalMemory,  // Shared across everything
}

pub struct UniversalChatScanner {
    found_chats: Vec<UniversalChat>,
    source_paths: HashMap<String, Vec<PathBuf>>,
    participant_detector: ParticipantDetector,
}

struct ParticipantDetector {
    patterns: HashMap<String, Regex>,
}

impl Default for UniversalChatScanner {
    fn default() -> Self {
        Self::new()
    }
}

impl UniversalChatScanner {
    pub fn new() -> Self {
        Self {
            found_chats: Vec::new(),
            source_paths: HashMap::new(),
            participant_detector: ParticipantDetector::new(),
        }
    }

    /// Scan all known locations for conversations
    pub async fn scan_all(&mut self) -> Result<()> {
        println!("🔍 Scanning for conversations across all platforms...\n");

        // Claude projects
        self.scan_claude_projects().await?;

        // Cursor/Windsurf
        self.scan_cursor_windsurf().await?;

        // VSCode/Copilot
        self.scan_vscode().await?;

        // OpenWebUI/LMStudio
        self.scan_local_llms().await?;

        // ChatGPT exports
        self.scan_chatgpt_exports().await?;

        // Text messages (if available)
        self.scan_text_messages().await?;

        Ok(())
    }

    /// Scan Claude project directories
    async fn scan_claude_projects(&mut self) -> Result<()> {
        let claude_path = shellexpand::tilde(CLAUDE_PROJECTS);
        let path = Path::new(claude_path.as_ref());

        if !path.exists() {
            return Ok(());
        }

        println!("  📂 Scanning Claude projects...");
        let mut count = 0;

        // Look for conversation files
        for entry in glob(&format!("{}/**/*.json", path.display()))? {
            if let Ok(file_path) = entry {
                if let Ok(content) = fs::read_to_string(&file_path) {
                    if content.contains("claude") || content.contains("assistant") {
                        // Parse Claude conversation
                        if let Ok(chat) = self.parse_claude_chat(&content, &file_path) {
                            self.found_chats.push(chat);
                            count += 1;
                        }
                    }
                }
            }
        }

        println!("     ✓ Found {} Claude conversations", count);
        Ok(())
    }

    /// Scan Cursor and Windsurf directories
    async fn scan_cursor_windsurf(&mut self) -> Result<()> {
        let cursor_path = shellexpand::tilde(CURSOR_CHATS);
        let windsurf_path = shellexpand::tilde(WINDSURF_DIR);

        let mut count = 0;

        // Cursor
        if Path::new(cursor_path.as_ref()).exists() {
            println!("  📂 Scanning Cursor chats...");
            count += self.scan_directory(cursor_path.as_ref(), "cursor").await?;
        }

        // Windsurf
        if Path::new(windsurf_path.as_ref()).exists() {
            println!("  📂 Scanning Windsurf sessions...");
            count += self
                .scan_directory(windsurf_path.as_ref(), "windsurf")
                .await?;
        }

        if count > 0 {
            println!("     ✓ Found {} Cursor/Windsurf conversations", count);
        }

        Ok(())
    }

    /// Scan a directory for chat files
    async fn scan_directory(&mut self, dir: &str, source: &str) -> Result<usize> {
        let mut count = 0;

        for entry in glob(&format!("{}/**/*.{}", dir, "{json,md,txt}"))? {
            if let Ok(file_path) = entry {
                if let Ok(content) = fs::read_to_string(&file_path) {
                    // Look for conversation patterns
                    if self.looks_like_chat(&content) {
                        let chat = self.create_chat_from_content(&content, source, &file_path)?;
                        self.found_chats.push(chat);
                        count += 1;
                    }
                }
            }
        }

        Ok(count)
    }

    /// Detect if content looks like a chat conversation
    fn looks_like_chat(&self, content: &str) -> bool {
        // Look for common chat patterns
        content.contains("user:")
            || content.contains("assistant:")
            || content.contains("Human:")
            || content.contains("AI:")
            || content.contains("You:")
            || content.contains("```") && content.contains("?") // Code with questions
    }

    /// Parse Claude-specific chat format using format detector
    fn parse_claude_chat(&self, content: &str, path: &Path) -> Result<UniversalChat> {
        // Use the universal format detector!
        let mut detector = crate::universal_format_detector::UniversalFormatDetector::new();
        let _format = detector.detect_format(content);
        detector.analyze_structure(content)?;

        // Get the dominant speaker info
        let _dominant = detector.get_dominant_speaker();
        let project = path
            .parent()
            .and_then(|p| p.file_name())
            .and_then(|n| n.to_str())
            .unwrap_or("unknown")
            .to_string();

        Ok(UniversalChat {
            source: ChatSource::Claude {
                project: project.clone(),
            },
            participants: vec!["Human".to_string(), "Claude".to_string()],
            timestamp: Utc::now(), // Would parse from file
            content: content.to_string(),
            keywords: self.extract_keywords(content),
            project_context: Some(project),
            importance: self.calculate_importance(content),
        })
    }

    /// Create generic chat from content
    fn create_chat_from_content(
        &self,
        content: &str,
        source: &str,
        path: &Path,
    ) -> Result<UniversalChat> {
        let source_enum = match source {
            "cursor" => ChatSource::Cursor {
                workspace: path.to_string_lossy().to_string(),
            },
            "windsurf" => ChatSource::Windsurf {
                session: path.to_string_lossy().to_string(),
            },
            _ => ChatSource::Custom {
                platform: source.to_string(),
            },
        };

        Ok(UniversalChat {
            source: source_enum,
            participants: self.participant_detector.detect(content),
            timestamp: Utc::now(),
            content: content.to_string(),
            keywords: self.extract_keywords(content),
            project_context: None,
            importance: self.calculate_importance(content),
        })
    }

    /// Extract keywords from content
    fn extract_keywords(&self, content: &str) -> Vec<String> {
        let mut keywords = Vec::new();

        // Common technical keywords
        let tech_words = [
            "function",
            "async",
            "memory",
            "audio",
            "tokenization",
            "consciousness",
            "claude",
            "rust",
            "python",
            "javascript",
        ];

        for word in tech_words {
            if content.to_lowercase().contains(word) {
                keywords.push(word.to_string());
            }
        }

        keywords
    }

    /// Calculate importance based on content
    fn calculate_importance(&self, content: &str) -> f32 {
        let mut score: f32 = 0.5; // Base score

        // Boost for code blocks
        if content.contains("```") {
            score += 0.1;
        }

        // Boost for questions
        if content.matches('?').count() > 2 {
            score += 0.1;
        }

        // Boost for problem-solving keywords
        if content.contains("fix")
            || content.contains("solve")
            || content.contains("implement")
            || content.contains("breakthrough")
        {
            score += 0.2;
        }

        score.min(1.0)
    }

    /// Scan VSCode directories
    async fn scan_vscode(&mut self) -> Result<()> {
        // TODO: Implement VSCode/Copilot scanning
        Ok(())
    }

    /// Scan local LLM tools
    async fn scan_local_llms(&mut self) -> Result<()> {
        // TODO: Implement OpenWebUI/LMStudio scanning
        Ok(())
    }

    /// Scan ChatGPT exports
    async fn scan_chatgpt_exports(&mut self) -> Result<()> {
        let export_pattern = shellexpand::tilde(CHATGPT_EXPORT);

        for entry in glob(export_pattern.as_ref())? {
            if let Ok(path) = entry {
                println!("  📦 Found ChatGPT export: {}", path.display());
                // TODO: Unzip and parse ChatGPT export format
            }
        }

        Ok(())
    }

    /// Scan text messages (platform-specific)
    async fn scan_text_messages(&mut self) -> Result<()> {
        // TODO: Platform-specific text message scanning
        Ok(())
    }

    /// Save discovered chats to .m8 files
    pub async fn save_to_m8(&self, destination: &MemoryDestination) -> Result<()> {
        let path: PathBuf = match &destination.memory_type {
            MemoryType::ProjectMemory => {
                // Use project-local directory
                let cwd = std::env::current_dir()?;
                // No project sub-folder is needed as we are already inside the project's context
                cwd.join(".st").join("mem8")
            }
            MemoryType::UserMemory => shellexpand::tilde("~/.mem8/user").into_owned().into(),
            MemoryType::LLMMemory => {
                let llm_path = format!(
                    "~/.mem8/llm/{}",
                    destination
                        .llm_specific
                        .as_ref()
                        .unwrap_or(&"general".to_string())
                );
                shellexpand::tilde(&llm_path).into_owned().into()
            }
            MemoryType::GlobalMemory => shellexpand::tilde("~/.mem8/global").into_owned().into(),
        };

        fs::create_dir_all(&path)?;

        // Group chats by source
        let mut by_source: HashMap<String, Vec<&UniversalChat>> = HashMap::new();
        for chat in &self.found_chats {
            let key = format!("{:?}", chat.source);
            by_source.entry(key).or_default().push(chat);
        }

        // Save each source group to appropriate format
        // .m8j for JSON contexts, .m8 for binary wave format
        for (source, chats) in by_source {
            let filename = path.join(format!(
                "chat_{}.m8j",
                source.to_lowercase().replace(['{', '}', ':', '"', ' '], "")
            ));
            self.write_m8j_file(filename.to_str().unwrap_or_default(), chats)?;
        }

        Ok(())
    }

    /// Write chats to .m8j (JSON) file
    fn write_m8j_file(&self, path: &str, chats: Vec<&UniversalChat>) -> Result<()> {
        use flate2::write::ZlibEncoder;
        use flate2::Compression;
        use std::fs::File;
        use std::io::Write;

        // Create JSON structure
        let json_data = serde_json::json!({
            "contexts": chats,
            "format": "m8j",
            "version": 1,
            "compressed": true
        });

        // Compress with zlib
        let json_str = serde_json::to_string(&json_data)?;
        let file = File::create(path)?;
        let mut encoder = ZlibEncoder::new(file, Compression::default());
        encoder.write_all(json_str.as_bytes())?;
        encoder.finish()?;

        println!("💾 Saved {} chats to {} (JSON format)", chats.len(), path);
        Ok(())
    }

    /// Write chats to .m8 (binary wave) file - the REAL format!
    fn write_m8_binary_file(&self, path: &str, chats: Vec<&UniversalChat>) -> Result<()> {
        use crate::mem8_binary::M8BinaryFile;

        let mut m8_file = M8BinaryFile::create(path)?;

        let chat_count = chats.len();
        for chat in chats {
            let content = serde_json::to_vec(chat)?;
            let importance = chat.importance;
            m8_file.append_block(&content, importance)?;
        }

        println!(
            "🌊 Saved {} chats to {} (Binary wave format)",
            chat_count, path
        );
        Ok(())
    }

    /// Interactive prompt for user to choose destination
    pub fn prompt_for_destination(&self) -> Result<MemoryDestination> {
        println!("\n📍 Where should these memories be stored?");
        println!("  1. Project Memory (specific project)");
        println!("  2. User Memory (personal)");
        println!("  3. LLM Memory (Claude/GPT/etc specific)");
        println!("  4. Global Memory (shared everywhere)");

        // For now, return a default
        Ok(MemoryDestination {
            memory_type: MemoryType::GlobalMemory,
            llm_specific: None,
            project: None,
            tags: vec!["imported".to_string()],
        })
    }

    /// Get summary of found chats
    pub fn summary(&self) -> String {
        let mut summary = String::new();
        summary.push_str(&format!(
            "\n📊 Found {} total conversations:\n",
            self.found_chats.len()
        ));

        // Group by source
        let mut by_source: HashMap<String, usize> = HashMap::new();
        for chat in &self.found_chats {
            let key = match &chat.source {
                ChatSource::Claude { .. } => "Claude",
                ChatSource::Cursor { .. } => "Cursor",
                ChatSource::Windsurf { .. } => "Windsurf",
                ChatSource::ChatGPT { .. } => "ChatGPT",
                _ => "Other",
            };
            *by_source.entry(key.to_string()).or_default() += 1;
        }

        for (source, count) in by_source {
            summary.push_str(&format!("{}: {} chats\n", source, count));
        }

        summary
    }
}

impl ParticipantDetector {
    fn new() -> Self {
        let mut patterns = HashMap::new();

        // Common patterns for detecting participants
        patterns.insert(
            "user_human".to_string(),
            Regex::new(r"(?i)(user|human|you):").unwrap(),
        );
        patterns.insert(
            "assistant".to_string(),
            Regex::new(r"(?i)(assistant|ai|claude|gpt):").unwrap(),
        );

        Self { patterns }
    }

    fn detect(&self, content: &str) -> Vec<String> {
        let mut participants = Vec::new();

        if self.patterns["user_human"].is_match(content) {
            participants.push("Human".to_string());
        }
        if self.patterns["assistant"].is_match(content) {
            participants.push("AI Assistant".to_string());
        }

        if participants.is_empty() {
            participants.push("Unknown".to_string());
        }

        participants
    }
}

/// CLI entry point
pub async fn scan_for_context() -> Result<()> {
    println!("🌍 Universal Chat Scanner - Finding Your Digital Consciousness!\n");
    println!("{}\n", "=".repeat(60));

    let mut scanner = UniversalChatScanner::new();

    // Scan everything
    scanner.scan_all().await?;

    // Show summary
    println!("{}", scanner.summary());

    // Ask user where to save
    let destination = scanner.prompt_for_destination()?;

    // Save to .m8 files
    scanner.save_to_m8(&destination).await?;

    println!("\n✨ Context aggregation complete!");
    println!("   Your scattered conversations are now unified!");

    Ok(())
}

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

    #[test]
    fn test_chat_detection() {
        let scanner = UniversalChatScanner::new();

        assert!(scanner.looks_like_chat("user: Hello\nassistant: Hi there!"));
        assert!(scanner.looks_like_chat("Human: Can you help?\nAI: Sure!"));
        assert!(!scanner.looks_like_chat("This is just regular text."));
    }

    #[test]
    fn test_keyword_extraction() {
        let scanner = UniversalChatScanner::new();
        let content = "Let's implement an async function for audio processing";

        let keywords = scanner.extract_keywords(content);
        assert!(keywords.contains(&"function".to_string()));
        assert!(keywords.contains(&"async".to_string()));
        assert!(keywords.contains(&"audio".to_string()));
    }
}