code-mesh-core 0.1.0

High-performance, WASM-powered distributed swarm intelligence core library for concurrent code execution and neural mesh computing
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
//! File watching system for live updates
//! Provides real-time notifications when files or directories change

use async_trait::async_trait;
use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tokio::sync::{mpsc, RwLock};
use uuid::Uuid;

use super::{Tool, ToolContext, ToolResult, ToolError};

/// File watcher tool for monitoring file system changes
pub struct FileWatcherTool {
    active_watchers: Arc<RwLock<HashMap<String, WatcherInstance>>>,
}

#[derive(Debug, Deserialize)]
struct WatchParams {
    path: String,
    #[serde(default)]
    recursive: bool,
    #[serde(default)]
    patterns: Option<Vec<String>>,
    #[serde(default)]
    ignore_patterns: Option<Vec<String>>,
    #[serde(default)]
    debounce_ms: Option<u64>,
}

#[derive(Debug, Serialize, Clone)]
pub struct FileChangeEvent {
    pub event_id: String,
    pub timestamp: SystemTime,
    pub event_type: String,
    pub paths: Vec<PathBuf>,
    pub details: HashMap<String, Value>,
}

struct WatcherInstance {
    watcher_id: String,
    _watcher: RecommendedWatcher,
    event_sender: mpsc::UnboundedSender<FileChangeEvent>,
    patterns: Option<Vec<String>>,
    ignore_patterns: Option<Vec<String>>,
}

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

impl FileWatcherTool {
    pub fn new() -> Self {
        Self {
            active_watchers: Arc::new(RwLock::new(HashMap::new())),
        }
    }
    
    /// Start watching a path for changes
    pub async fn start_watching(
        &self,
        path: impl AsRef<Path>,
        recursive: bool,
        patterns: Option<Vec<String>>,
        ignore_patterns: Option<Vec<String>>,
    ) -> Result<(String, mpsc::UnboundedReceiver<FileChangeEvent>), ToolError> {
        let path = path.as_ref();
        let watcher_id = Uuid::new_v4().to_string();
        
        // Create event channel
        let (event_tx, event_rx) = mpsc::unbounded_channel();
        
        // Create the watcher
        let event_tx_clone = event_tx.clone();
        let mut watcher = notify::recommended_watcher(move |result: Result<Event, notify::Error>| {
            match result {
                Ok(event) => {
                    let file_event = FileChangeEvent {
                        event_id: Uuid::new_v4().to_string(),
                        timestamp: SystemTime::now(),
                        event_type: format!("{:?}", event.kind),
                        paths: event.paths,
                        details: HashMap::new(),
                    };
                    
                    if let Err(e) = event_tx_clone.send(file_event) {
                        tracing::warn!("Failed to send file change event: {}", e);
                    }
                }
                Err(e) => {
                    tracing::error!("File watcher error: {}", e);
                }
            }
        })
        .map_err(|e| ToolError::ExecutionFailed(format!("Failed to create watcher: {}", e)))?;
        
        // Start watching
        let mode = if recursive {
            RecursiveMode::Recursive
        } else {
            RecursiveMode::NonRecursive
        };
        
        watcher.watch(path, mode)
            .map_err(|e| ToolError::ExecutionFailed(format!("Failed to start watching: {}", e)))?;
        
        // Store the watcher instance
        let instance = WatcherInstance {
            watcher_id: watcher_id.clone(),
            _watcher: watcher,
            event_sender: event_tx,
            patterns: patterns.clone(),
            ignore_patterns: ignore_patterns.clone(),
        };
        
        {
            let mut watchers = self.active_watchers.write().await;
            watchers.insert(watcher_id.clone(), instance);
        }
        
        Ok((watcher_id, event_rx))
    }
    
    /// Stop watching a path
    pub async fn stop_watching(&self, watcher_id: &str) -> Result<(), ToolError> {
        let mut watchers = self.active_watchers.write().await;
        if watchers.remove(watcher_id).is_some() {
            Ok(())
        } else {
            Err(ToolError::ExecutionFailed(format!(
                "Watcher {} not found",
                watcher_id
            )))
        }
    }
    
    /// Get list of active watchers
    pub async fn list_watchers(&self) -> Vec<String> {
        let watchers = self.active_watchers.read().await;
        watchers.keys().cloned().collect()
    }
    
    /// Check if a file change matches the given patterns
    fn matches_patterns(
        path: &Path,
        patterns: &Option<Vec<String>>,
        ignore_patterns: &Option<Vec<String>>,
    ) -> bool {
        let path_str = path.to_string_lossy();
        
        // Check ignore patterns first
        if let Some(ignore) = ignore_patterns {
            for pattern in ignore {
                if glob::Pattern::new(pattern)
                    .map(|p| p.matches(&path_str))
                    .unwrap_or(false)
                {
                    return false;
                }
            }
        }
        
        // Check include patterns
        if let Some(include) = patterns {
            for pattern in include {
                if glob::Pattern::new(pattern)
                    .map(|p| p.matches(&path_str))
                    .unwrap_or(false)
                {
                    return true;
                }
            }
            false // If patterns specified but none match
        } else {
            true // No patterns specified, match all
        }
    }
}

#[async_trait]
impl Tool for FileWatcherTool {
    fn id(&self) -> &str {
        "file_watcher"
    }
    
    fn description(&self) -> &str {
        "Monitor file system changes with pattern matching and filtering"
    }
    
    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "path": {
                    "type": "string",
                    "description": "Path to watch for changes"
                },
                "recursive": {
                    "type": "boolean",
                    "description": "Watch subdirectories recursively",
                    "default": false
                },
                "patterns": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    },
                    "description": "Glob patterns to match files (e.g., ['*.rs', '*.js'])"
                },
                "ignorePatterns": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    },
                    "description": "Glob patterns to ignore (e.g., ['*.tmp', 'node_modules/**'])"
                },
                "debounceMs": {
                    "type": "number",
                    "description": "Debounce delay in milliseconds to group rapid changes",
                    "minimum": 0,
                    "maximum": 10000
                }
            },
            "required": ["path"]
        })
    }
    
    async fn execute(
        &self,
        args: Value,
        ctx: ToolContext,
    ) -> Result<ToolResult, ToolError> {
        let params: WatchParams = serde_json::from_value(args)
            .map_err(|e| ToolError::InvalidParameters(e.to_string()))?;
        
        // Resolve path
        let watch_path = if PathBuf::from(&params.path).is_absolute() {
            PathBuf::from(&params.path)
        } else {
            ctx.working_directory.join(&params.path)
        };
        
        // Validate path exists
        if !watch_path.exists() {
            return Err(ToolError::ExecutionFailed(format!(
                "Path does not exist: {}",
                watch_path.display()
            )));
        }
        
        // Start watching
        let (watcher_id, mut event_rx) = self.start_watching(
            &watch_path,
            params.recursive,
            params.patterns.clone(),
            params.ignore_patterns.clone(),
        ).await?;
        
        // For this demonstration, we'll watch for a short time and return events
        // In a real implementation, this would be managed differently
        let watch_duration = Duration::from_millis(params.debounce_ms.unwrap_or(1000));
        let start_time = SystemTime::now();
        let mut events = Vec::new();
        
        // Collect events for the specified duration
        while start_time.elapsed().unwrap_or_default() < watch_duration {
            if *ctx.abort_signal.borrow() {
                self.stop_watching(&watcher_id).await.ok();
                return Err(ToolError::Aborted);
            }
            
            match tokio::time::timeout(Duration::from_millis(100), event_rx.recv()).await {
                Ok(Some(event)) => {
                    // Filter event based on patterns
                    let matching_paths: Vec<_> = event.paths.iter()
                        .filter(|path| Self::matches_patterns(
                            path,
                            &params.patterns,
                            &params.ignore_patterns
                        ))
                        .cloned()
                        .collect();
                    
                    if !matching_paths.is_empty() {
                        let filtered_event = FileChangeEvent {
                            event_id: event.event_id,
                            timestamp: event.timestamp,
                            event_type: event.event_type,
                            paths: matching_paths,
                            details: event.details,
                        };
                        events.push(filtered_event);
                    }
                }
                Ok(None) => break, // Channel closed
                Err(_) => continue, // Timeout, continue watching
            }
        }
        
        // Stop watching
        self.stop_watching(&watcher_id).await.ok();
        
        // Calculate relative paths for display
        let relative_path = watch_path
            .strip_prefix(&ctx.working_directory)
            .unwrap_or(&watch_path)
            .to_string_lossy()
            .to_string();
        
        let metadata = json!({
            "watcher_id": watcher_id,
            "path": watch_path.to_string_lossy(),
            "relative_path": relative_path,
            "recursive": params.recursive,
            "patterns": params.patterns,
            "ignore_patterns": params.ignore_patterns,
            "watch_duration_ms": watch_duration.as_millis(),
            "events_collected": events.len(),
            "events": events.iter().map(|e| json!({
                "event_id": e.event_id,
                "timestamp": e.timestamp.duration_since(SystemTime::UNIX_EPOCH)
                    .unwrap_or_default().as_secs(),
                "event_type": e.event_type,
                "paths": e.paths.iter().map(|p| p.to_string_lossy()).collect::<Vec<_>>()
            })).collect::<Vec<_>>()
        });
        
        let output = if events.is_empty() {
            format!(
                "No file changes detected in {} during {}ms watch period",
                relative_path,
                watch_duration.as_millis()
            )
        } else {
            let mut output_lines = vec![
                format!(
                    "Detected {} file change{} in {} during {}ms watch period:",
                    events.len(),
                    if events.len() == 1 { "" } else { "s" },
                    relative_path,
                    watch_duration.as_millis()
                )
            ];
            
            for event in &events {
                output_lines.push(format!(
                    "  - {}: {}",
                    event.event_type,
                    event.paths.iter()
                        .map(|p| p.to_string_lossy())
                        .collect::<Vec<_>>()
                        .join(", ")
                ));
            }
            
            output_lines.join("\n")
        };
        
        Ok(ToolResult {
            title: format!("Watched {} for {}ms", relative_path, watch_duration.as_millis()),
            metadata,
            output,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;
    use tokio::fs;
    
    #[tokio::test]
    async fn test_file_watcher_creation() {
        let watcher = FileWatcherTool::new();
        assert!(watcher.list_watchers().await.is_empty());
    }
    
    #[tokio::test]
    async fn test_pattern_matching() {
        let path = Path::new("test.rs");
        let patterns = Some(vec!["*.rs".to_string()]);
        let ignore_patterns = Some(vec!["*.tmp".to_string()]);
        
        assert!(FileWatcherTool::matches_patterns(&path, &patterns, &ignore_patterns));
        
        let ignored_path = Path::new("test.tmp");
        assert!(!FileWatcherTool::matches_patterns(&ignored_path, &patterns, &ignore_patterns));
    }
    
    #[tokio::test]
    async fn test_file_watcher_tool() {
        let temp_dir = TempDir::new().unwrap();
        let temp_path = temp_dir.path().to_path_buf();
        
        let tool = FileWatcherTool::new();
        let params = json!({
            "path": temp_path.to_string_lossy(),
            "recursive": false,
            "patterns": ["*.txt"],
            "debounceMs": 500
        });
        
        let ctx = ToolContext {
            session_id: "test".to_string(),
            message_id: "test".to_string(),
            abort_signal: tokio::sync::watch::channel(false).1,
            working_directory: std::env::current_dir().unwrap(),
        };
        
        // Start watching in background
        let tool_clone = tool.clone();
        let params_clone = params.clone();
        let ctx_clone = ctx.clone();
        
        let watch_task = tokio::spawn(async move {
            tool_clone.execute(params_clone, ctx_clone).await
        });
        
        // Give watcher time to start
        tokio::time::sleep(Duration::from_millis(100)).await;
        
        // Create a file to trigger an event
        let test_file = temp_path.join("test.txt");
        fs::write(&test_file, "test content").await.unwrap();
        
        // Wait for watcher to complete
        let result = watch_task.await.unwrap();
        
        // Note: Due to timing, the test might not catch the file creation
        // In a real scenario, events would be handled asynchronously
        assert!(result.is_ok());
    }
}