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
//! Server-Sent Events (SSE) support for MCP server
//!
//! Provides real-time streaming of directory changes and analysis results

use anyhow::Result;
use futures_util::StreamExt;
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::time::interval;

use super::{is_path_allowed, McpContext};
use crate::formatters::{ai::AiFormatter, hex::HexFormatter, quantum::QuantumFormatter, Formatter};
use crate::scanner::{FileNode, Scanner, ScannerConfig};

/// SSE event types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum SseEvent {
    /// Initial scan complete
    ScanComplete { path: String, stats: ScanStats },
    /// File or directory created
    Created { path: String, node: FileNode },
    /// File or directory modified
    Modified { path: String, node: FileNode },
    /// File or directory deleted
    Deleted { path: String },
    /// Directory analysis update
    Analysis {
        path: String,
        format: String,
        data: String,
    },
    /// Periodic statistics update
    Stats { path: String, stats: ScanStats },
    /// Error occurred
    Error { message: String },
    /// Heartbeat to keep connection alive
    Heartbeat,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScanStats {
    pub total_files: u64,
    pub total_dirs: u64,
    pub total_size: u64,
    pub scan_time_ms: u64,
}

/// SSE stream configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SseConfig {
    /// Path to watch
    pub path: PathBuf,
    /// Output format for analysis
    pub format: OutputFormat,
    /// Send heartbeat every N seconds
    pub heartbeat_interval: u64,
    /// Send stats update every N seconds
    pub stats_interval: u64,
    /// Include file contents in events
    pub include_content: bool,
    /// Maximum depth for recursive watching
    pub max_depth: Option<usize>,
    /// File patterns to include
    pub include_patterns: Vec<String>,
    /// File patterns to exclude
    pub exclude_patterns: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OutputFormat {
    Hex,
    Ai,
    Quantum,
    QuantumSemantic,
    Json,
    Summary,
}

impl Default for SseConfig {
    fn default() -> Self {
        Self {
            path: PathBuf::from("."),
            format: OutputFormat::Ai,
            heartbeat_interval: 30,
            stats_interval: 60,
            include_content: false,
            max_depth: None,
            include_patterns: vec![],
            exclude_patterns: vec![],
        }
    }
}

/// Handle SSE stream request
#[allow(dead_code)]
pub async fn handle_sse_stream(
    config: SseConfig,
    ctx: Arc<McpContext>,
) -> Result<impl futures_util::Stream<Item = Result<SseEvent>>> {
    // Validate path
    if !is_path_allowed(&config.path, &ctx.config) {
        anyhow::bail!("Path not allowed: {:?}", config.path);
    }

    let (tx, rx) = mpsc::channel::<SseEvent>(100);

    // Spawn watcher task
    let watcher_tx = tx.clone();
    let watcher_config = config.clone();
    let watcher_ctx = ctx.clone();
    tokio::spawn(async move {
        if let Err(e) = watch_directory(watcher_config, watcher_ctx, watcher_tx).await {
            eprintln!("Watcher error: {}", e);
        }
    });

    // Spawn heartbeat task
    let heartbeat_tx = tx.clone();
    let heartbeat_interval = config.heartbeat_interval;
    tokio::spawn(async move {
        let mut interval = interval(Duration::from_secs(heartbeat_interval));
        loop {
            interval.tick().await;
            if heartbeat_tx.send(SseEvent::Heartbeat).await.is_err() {
                break;
            }
        }
    });

    // Spawn stats task
    let stats_tx = tx;
    let stats_config = config.clone();
    let stats_interval = config.stats_interval;
    tokio::spawn(async move {
        let mut interval = interval(Duration::from_secs(stats_interval));
        loop {
            interval.tick().await;
            if let Ok(stats) = gather_stats(&stats_config.path).await {
                let event = SseEvent::Stats {
                    path: stats_config.path.display().to_string(),
                    stats,
                };
                if stats_tx.send(event).await.is_err() {
                    break;
                }
            }
        }
    });

    // Create stream from receiver
    Ok(tokio_stream::wrappers::ReceiverStream::new(rx).map(Ok))
}

/// Watch directory for changes
#[allow(dead_code)]
async fn watch_directory(
    config: SseConfig,
    _ctx: Arc<McpContext>,
    tx: mpsc::Sender<SseEvent>,
) -> Result<()> {
    // Initial scan
    let scanner_config = ScannerConfig {
        max_depth: config.max_depth.unwrap_or(usize::MAX),
        show_hidden: false,
        follow_symlinks: false,
        show_ignored: false,
        search_keyword: None,
        file_type_filter: None,
        ..Default::default()
    };

    let scanner = Scanner::new(&config.path, scanner_config)?;
    let start = std::time::Instant::now();
    let (nodes, stats) = scanner.scan()?;
    let scan_time_ms = start.elapsed().as_millis() as u64;

    // Send initial scan complete event
    tx.send(SseEvent::ScanComplete {
        path: config.path.display().to_string(),
        stats: ScanStats {
            total_files: stats.total_files,
            total_dirs: stats.total_dirs,
            total_size: stats.total_size,
            scan_time_ms,
        },
    })
    .await?;

    // Send initial analysis
    if let Ok(analysis) = format_nodes(&nodes, &stats, &config.path, &config.format) {
        tx.send(SseEvent::Analysis {
            path: config.path.display().to_string(),
            format: format!("{:?}", config.format),
            data: analysis,
        })
        .await?;
    }

    // Set up file watcher
    let (watcher_tx, mut watcher_rx) = mpsc::channel(100);
    let mut watcher = RecommendedWatcher::new(
        move |res: Result<Event, notify::Error>| {
            if let Ok(event) = res {
                let _ = watcher_tx.blocking_send(event);
            }
        },
        Config::default(),
    )?;

    watcher.watch(&config.path, RecursiveMode::Recursive)?;

    // Process file system events
    while let Some(event) = watcher_rx.recv().await {
        match event.kind {
            notify::EventKind::Create(_) => {
                for path in event.paths {
                    if let Ok(node) = scan_single_path(&path).await {
                        tx.send(SseEvent::Created {
                            path: path.display().to_string(),
                            node,
                        })
                        .await?;
                    }
                }
            }
            notify::EventKind::Modify(_) => {
                for path in event.paths {
                    if let Ok(node) = scan_single_path(&path).await {
                        tx.send(SseEvent::Modified {
                            path: path.display().to_string(),
                            node,
                        })
                        .await?;
                    }
                }
            }
            notify::EventKind::Remove(_) => {
                for path in event.paths {
                    tx.send(SseEvent::Deleted {
                        path: path.display().to_string(),
                    })
                    .await?;
                }
            }
            _ => {}
        }
    }

    Ok(())
}

/// Scan a single path and create FileNode
#[allow(dead_code)]
async fn scan_single_path(path: &Path) -> Result<FileNode> {
    let metadata = tokio::fs::metadata(path).await?;

    // Get Unix permissions
    #[cfg(unix)]
    let permissions = {
        use std::os::unix::fs::PermissionsExt;
        metadata.permissions().mode()
    };
    #[cfg(not(unix))]
    let permissions = 0o755;

    // Get Unix uid/gid
    #[cfg(unix)]
    let (uid, gid) = {
        use std::os::unix::fs::MetadataExt;
        (metadata.uid(), metadata.gid())
    };
    #[cfg(not(unix))]
    let (uid, gid) = (0, 0);

    let is_hidden = path
        .file_name()
        .and_then(|n| n.to_str())
        .map(|n| n.starts_with('.'))
        .unwrap_or(false);

    let file_type = if metadata.is_dir() {
        crate::scanner::FileType::Directory
    } else if metadata.file_type().is_symlink() {
        crate::scanner::FileType::Symlink
    } else if metadata.is_file() {
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            if metadata.permissions().mode() & 0o111 != 0 {
                crate::scanner::FileType::Executable
            } else {
                crate::scanner::FileType::RegularFile
            }
        }
        #[cfg(not(unix))]
        crate::scanner::FileType::RegularFile
    } else {
        crate::scanner::FileType::RegularFile
    };

    // Use Scanner's internal method for determining category
    // For now, we'll use Unknown since get_file_category is private
    let category = crate::scanner::FileCategory::Unknown;

    Ok(FileNode {
        path: path.to_path_buf(),
        is_dir: metadata.is_dir(),
        size: metadata.len(),
        permissions,
        uid,
        gid,
        modified: metadata.modified()?,
        is_symlink: metadata.file_type().is_symlink(),
        is_hidden,
        permission_denied: false,
        depth: 0,
        is_ignored: false,
        file_type,
        category,
        search_matches: None,
        filesystem_type: crate::scanner::FilesystemType::Unknown,
        git_branch: None,
        // Smart scanning fields
        traversal_context: None,
        interest: None,
        security_findings: Vec::new(),
        change_status: None,
        content_hash: None,
    })
}

/// Gather current statistics for a path
#[allow(dead_code)]
async fn gather_stats(path: &Path) -> Result<ScanStats> {
    let scanner_config = ScannerConfig::default();
    let scanner = Scanner::new(path, scanner_config)?;
    let start = std::time::Instant::now();
    let (_, stats) = scanner.scan()?;
    let scan_time_ms = start.elapsed().as_millis() as u64;

    Ok(ScanStats {
        total_files: stats.total_files,
        total_dirs: stats.total_dirs,
        total_size: stats.total_size,
        scan_time_ms,
    })
}

/// Format nodes using the specified output format
#[allow(dead_code)]
fn format_nodes(
    nodes: &[FileNode],
    stats: &crate::scanner::TreeStats,
    root_path: &Path,
    format: &OutputFormat,
) -> Result<String> {
    let mut output = Vec::new();

    match format {
        OutputFormat::Hex => {
            let formatter = HexFormatter::new(
                false,
                false,
                false,
                crate::formatters::PathDisplayMode::Off,
                false,
            );
            formatter.format(&mut output, nodes, stats, root_path)?;
        }
        OutputFormat::Ai => {
            let formatter = AiFormatter::new(false, crate::formatters::PathDisplayMode::Off);
            formatter.format(&mut output, nodes, stats, root_path)?;
        }
        OutputFormat::Quantum => {
            let formatter = QuantumFormatter::new();
            formatter.format(&mut output, nodes, stats, root_path)?;
        }
        OutputFormat::Json => {
            let json = serde_json::json!({
                "nodes": nodes.len(),
                "stats": {
                    "total_files": stats.total_files,
                    "total_dirs": stats.total_dirs,
                    "total_size": stats.total_size,
                },
                "root": root_path.display().to_string()
            });
            serde_json::to_writer_pretty(&mut output, &json)?;
        }
        _ => {
            // For other formats, use JSON as fallback
            let json = serde_json::json!({
                "nodes": nodes.len(),
                "stats": {
                    "total_files": stats.total_files,
                    "total_dirs": stats.total_dirs,
                    "total_size": stats.total_size,
                },
                "root": root_path.display().to_string()
            });
            serde_json::to_writer_pretty(&mut output, &json)?;
        }
    }

    Ok(String::from_utf8_lossy(&output).to_string())
}

/// Create SSE response format
#[allow(dead_code)]
pub fn format_sse_event(event: &SseEvent) -> Result<String> {
    let json = serde_json::to_string(event)?;
    Ok(format!("data: {}\n\n", json))
}