snippy 0.2.1

A command-line tool for that makes using LLMs for code generation a breeze
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
use crate::applier::{Applier, DiffApplier, FullContentApplier, SearchReplaceApplier};
use crate::errors::ClipboardError;
use crate::extractor::Extractor;
use crate::llm::{LLMClient, TokenUsage, MODEL_PRICING};
use crate::applier::utils::print_diff;
use arboard::Clipboard;
use std::path::PathBuf;
use tokio::signal;
use tokio::time::{self, Duration};
use tracing::{debug, error, info, trace, warn};
use serde::{Deserialize, Serialize};
use std::fs;
use std::collections::VecDeque;
use walkdir::WalkDir;
use std::time::Instant;
use glob::Pattern;
use std::collections::HashMap;

const MAX_HISTORY_SIZE: usize = 10;
const DEFAULT_IGNORE_PATTERNS: &[&str] = &[
    "target/**",
    "node_modules/**",
    ".git/**",
    "**/*.pyc",
    "**/__pycache__/**",
    ".DS_Store",
    "Cargo.lock",
    "package-lock.json",
    "yarn.lock",
    "dist/**",
    "build/**",
];

#[derive(Debug, Serialize, Deserialize)]
struct AIResponse {
    contains_code: bool,
    files: Vec<String>,
}

#[derive(Debug)]
struct FileHistory {
    path: String,
}

#[derive(Debug)]
struct ProcessingStats {
    file_path: String,
    total_time: Duration,
    llm_response_time: Duration,
    io_time: Duration,
    token_usage: TokenUsage,
}

pub struct WatcherConfig {
    pub watch_path: PathBuf,
    pub interval_ms: u64,
    pub first_line_identifier: String,
    pub ai_enabled: bool,
    pub model: String,
    pub ignore_patterns: Vec<String>,
    pub predictions_enabled: bool,
    pub store_enabled: bool,
    pub metadata: HashMap<String, String>,
    pub one_shot: bool,
}

impl Default for WatcherConfig {
    fn default() -> Self {
        Self {
            watch_path: PathBuf::from("."),
            interval_ms: 1000,
            first_line_identifier: "# Relevant Code".to_string(),
            ai_enabled: false,
            model: "gpt-4o-mini".to_string(),
            ignore_patterns: DEFAULT_IGNORE_PATTERNS.iter().map(|s| s.to_string()).collect(),
            predictions_enabled: false,
            store_enabled: false,
            metadata: {
                let mut m = HashMap::new();
                m.insert("tool".to_string(), "snippy".to_string());
                m
            },
            one_shot: false,
        }
    }
}

pub struct ClipboardWatcher<E: Extractor + Send + Sync> {
    config: WatcherConfig,
    extractor: E,
    modified_files: VecDeque<FileHistory>,
    llm_client: LLMClient,
    total_token_usage: TokenUsage,
    ignore_patterns: Vec<Pattern>,
}

impl<E: Extractor + Send + Sync> ClipboardWatcher<E> {
    pub fn new(config: WatcherConfig, extractor: E) -> Self {
        let ignore_patterns = config.ignore_patterns.iter()
            .filter_map(|p| match Pattern::new(p) {
                Ok(pattern) => Some(pattern),
                Err(e) => {
                    warn!("Invalid ignore pattern '{}': {}", p, e);
                    None
                }
            })
            .collect();

        ClipboardWatcher { 
            llm_client: LLMClient::new(
                config.model.clone(),
                config.store_enabled,
                config.metadata.clone()
            ),
            config, 
            extractor,
            modified_files: VecDeque::with_capacity(MAX_HISTORY_SIZE),
            total_token_usage: TokenUsage::default(),
            ignore_patterns,
        }
    }

    fn should_ignore(&self, path: &str) -> bool {
        self.ignore_patterns.iter().any(|pattern| pattern.matches(path))
    }

    fn add_to_history(&mut self, file_path: String) {
        if self.modified_files.len() >= MAX_HISTORY_SIZE {
            self.modified_files.pop_front();
        }
        self.modified_files.push_back(FileHistory {
            path: file_path,
        });
    }

    fn update_token_usage(&mut self, usage: TokenUsage) {
        self.total_token_usage = self.total_token_usage + usage;
    }

    fn get_directory_tree(&self) -> Result<String, ClipboardError> {
        let mut tree = String::new();
        for entry in WalkDir::new(&self.config.watch_path)
            .min_depth(1)
            .max_depth(3)
            .into_iter()
            .filter_entry(|e| {
                !e.file_name().to_str().map_or(false, |s| s.starts_with('.')) && 
                !e.path().strip_prefix(&self.config.watch_path)
                    .ok()
                    .and_then(|p| p.to_str())
                    .map_or(false, |p| self.should_ignore(p))
            }) {
                match entry {
                    Ok(entry) => {
                        let path = entry.path().strip_prefix(&self.config.watch_path).unwrap_or(entry.path());
                        let depth = entry.depth();
                        let prefix = "  ".repeat(depth);
                        tree.push_str(&format!("{}{}\n", prefix, path.display()));
                    }
                    Err(e) => warn!("Error walking directory: {}", e),
                }
            }
        Ok(tree)
    }

    async fn process_with_ai(&mut self, content: &str) -> Result<(), ClipboardError> {
        let start_time = Instant::now();
        let mut processing_stats = Vec::new();
        
        let dir_tree = self.get_directory_tree()?;
        let recent_files = self.modified_files.iter()
            .map(|f| f.path.clone())
            .collect::<Vec<_>>()
            .join("\n");

        info!("Starting AI analysis of clipboard content ({} characters)", content.len());
        let check_prompt = format!(
            r#"Analyze the following content and determine if it contains code blocks or changes that need to be applied to files.
            This is from the user's clipboard. A lot of times the user can copy things that are not code, or are not relevant to the current project.
            Make sure to return true only if the content contains code that needs to be applied to files.
            
            Current working directory structure:
            {}

            Recently modified files:
            {}

            Important Notes:
            1. All file paths in your response MUST be relative to the current directory
            2. Only include files that need to be modified
            3. Make sure the files exist in the directory structure shown above or will be created as new files

            Respond in the following JSON format:
            {{
                "contains_code": true/false,
                "files": ["relative/path/to/file1.rs", "relative/path/to/file2.rs"]
            }}
            
            Content to analyze:
            {}
            "#, 
            dir_tree,
            if recent_files.is_empty() { "No recently modified files" } else { &recent_files },
            content
        );

        info!("Analyzing content with AI");
        match self.llm_client.call_with_json_response::<AIResponse>(&check_prompt).await {
            Ok((parsed_response, usage, analysis_time)) => {
                info!("Analysis token usage: {} (response time: {:?})", usage.format_details(&self.config.model), analysis_time);
                self.update_token_usage(usage);

                if !parsed_response.contains_code {
                    info!("AI analysis: No code changes detected in clipboard content");
                    return Ok(());
                }

                let files: Vec<_> = parsed_response.files.into_iter()
                    .filter(|f| !self.should_ignore(f))
                    .collect();

                if files.is_empty() {
                    info!("AI analysis: All detected files are in ignored paths");
                    return Ok(());
                }

                info!("AI analysis: Found {} files to process", files.len());
                for (i, file) in files.iter().enumerate() {
                    info!("File {}/{}: {}", i + 1, files.len(), file);
                }

                // Process each file
                for (i, file_path) in files.iter().enumerate() {
                    let file_start_time = Instant::now();
                    let mut io_time = Duration::default();
                    let full_path = self.config.watch_path.join(&file_path);
                    info!("Processing file {}/{}: {}", i + 1, files.len(), file_path);
                    debug!("Processing file: {:?}", full_path);

                    let io_start = Instant::now();
                    let (current_content, is_new_file) = if full_path.exists() {
                        (fs::read_to_string(&full_path)
                            .map_err(|e| ClipboardError::FileError(format!("Failed to read {}: {}", file_path, e)))?, false)
                    } else {
                        info!("File {} does not exist, will be created as new", file_path);
                        (String::new(), true)
                    };
                    io_time += io_start.elapsed();

                    let update_prompt = format!(
                        r#"Update or create the following file based on the provided changes.
                        Important:
                        1. Output ONLY the final content of the file
                        2. No markdown, no backticks, just the content
                        3. Think carefully about which comments to preserve:
                           - Keep meaningful documentation and code explanation comments
                           - Remove any temporary/instructional comments like "// ... rest of the code ..."
                           - Remove any comments that were meant to guide you
                        4. When deciding what changes to apply:
                           - ONLY apply changes that are meant for this specific file ({})
                           - Ignore any changes meant for other files
                           - Don't remove code that isn't being modified by the changes
                           - Think about the context and purpose of each section
                        5. Maintain consistent code style with the original file
                        
                        File status: {}
                        
                        Current content:
                        {}
                        
                        Changes to apply (ONLY apply changes meant for {}):
                        {}
                        "#,
                        file_path,
                        if is_new_file { "NEW FILE" } else { "EXISTING FILE" },
                        current_content,
                        file_path,
                        content
                    );

                    info!("Generating updated content for {}", file_path);
                    let prediction = if self.config.predictions_enabled && !is_new_file {
                        Some(current_content.as_str())
                    } else {
                        None
                    };

                    match self.llm_client.call(&update_prompt, prediction).await {
                        Ok(response) => {
                            info!("Generation token usage: {} (response time: {:?})", 
                                response.usage.format_details(&self.config.model), 
                                response.response_time
                            );
                            self.update_token_usage(response.usage);

                            let io_start = Instant::now();
                            print_diff(&file_path, &current_content, &response.content);

                            if let Some(parent) = full_path.parent() {
                                if !parent.exists() {
                                    fs::create_dir_all(parent)
                                        .map_err(|e| ClipboardError::FileError(format!("Failed to create directories for {}: {}", file_path, e)))?;
                                }
                            }

                            fs::write(&full_path, &response.content)
                                .map_err(|e| ClipboardError::FileError(format!("Failed to write to {}: {}", file_path, e)))?;
                            io_time += io_start.elapsed();

                            self.add_to_history(file_path.clone());
                            processing_stats.push(ProcessingStats {
                                file_path: file_path.clone(),
                                total_time: file_start_time.elapsed(),
                                llm_response_time: response.response_time,
                                io_time,
                                token_usage: response.usage,
                            });
                        }
                        Err(ClipboardError::Cancelled(_)) => {
                            info!("File processing cancelled by user");
                            return Ok(());
                        }
                        Err(e) => return Err(e),
                    }
                }

                let total_time = start_time.elapsed();
                let total_llm_time: Duration = processing_stats.iter()
                    .map(|s| s.llm_response_time)
                    .sum();
                let total_io_time: Duration = processing_stats.iter()
                    .map(|s| s.io_time)
                    .sum();

                info!("Processing completed in {:?}", total_time);
                info!("Time breakdown: LLM={:?}, IO={:?}, Other={:?}", 
                    total_llm_time,
                    total_io_time,
                    total_time - total_llm_time - total_io_time
                );
                info!("Files processed:");
                for stats in processing_stats {
                    info!("  {} (total={:?}, llm={:?}, io={:?}, tokens: {})",
                        stats.file_path,
                        stats.total_time,
                        stats.llm_response_time,
                        stats.io_time,
                        stats.token_usage.format_details(&self.config.model)
                    );
                }

                Ok(())
            }
            Err(ClipboardError::Cancelled(_)) => {
                info!("Analysis cancelled by user");
                Ok(())
            }
            Err(e) => Err(e),
        }
    }

    pub async fn run(&mut self) -> Result<(), ClipboardError> {
        // Check for OpenAI API key if AI is enabled
        if self.config.ai_enabled {
            if std::env::var("OPENAI_API_KEY").is_err() {
                error!("OpenAI API key not found in environment. Please set OPENAI_API_KEY environment variable.");
                return Err(ClipboardError::ConfigError("OPENAI_API_KEY environment variable not set".to_string()));
            }
        }

        let mut clipboard = Clipboard::new()
            .map_err(|e| ClipboardError::ClipboardInitError(e.to_string()))?;
        
        // Initialize last_content with current clipboard content to ignore initial state
        let mut last_content = clipboard.get_text()
            .unwrap_or_default();
        
        let mut interval = time::interval(Duration::from_millis(self.config.interval_ms));
        
        info!("Started {} clipboard at {:?}", 
            if self.config.one_shot { "one-shot processing" } else { "watching" },
            self.config.watch_path
        );
        info!("AI processing is {}", if self.config.ai_enabled { "enabled" } else { "disabled" });
        if self.config.ai_enabled {
            info!("Using OpenAI model: {} (input=${:.3}/1M, cached=${:.3}/1M, output=${:.3}/1M)", 
                self.config.model,
                MODEL_PRICING.get(self.config.model.as_str())
                    .map_or(0.0, |p| p.input_price),
                MODEL_PRICING.get(self.config.model.as_str())
                    .map_or(0.0, |p| p.cached_price),
                MODEL_PRICING.get(self.config.model.as_str())
                    .map_or(0.0, |p| p.output_price)
            );
            info!("Predictions are {}", if self.config.predictions_enabled { "enabled" } else { "disabled" });
            if self.config.store_enabled {
                info!("Data storage is enabled with metadata: {:?}", self.config.metadata);
            }
        }

        let start_time = Instant::now();

        loop {
            tokio::select! {
                _ = interval.tick() => {
                    trace!("Checking clipboard content");
                    match clipboard.get_text() {
                        Ok(content) => {
                            trace!("Clipboard content length: {}", content.len());
                            if content.starts_with(&self.config.first_line_identifier) {
                                trace!("Ignoring self-copied content to avoid recursion");
                                continue;
                            }

                            if content != last_content {
                                info!("New clipboard content detected");
                                
                                if self.config.ai_enabled {
                                    match self.process_with_ai(&content).await {
                                        Ok(_) => {
                                            info!("AI processing completed successfully");
                                            if self.config.one_shot {
                                                info!("One-shot processing completed in {:?}", start_time.elapsed());
                                                info!("Total token usage: {}", 
                                                    self.total_token_usage.format_details(&self.config.model)
                                                );
                                                return Ok(());
                                            }
                                        }
                                        Err(e) => {
                                            error!("AI processing failed: {}", e);
                                            warn!("Falling back to standard processing");
                                            if let Err(e) = self.process_standard(&content).await {
                                                error!("Standard processing also failed: {}", e);
                                            }
                                            if self.config.one_shot {
                                                return Err(e);
                                            }
                                        }
                                    }
                                } else {
                                    if let Err(e) = self.process_standard(&content).await {
                                        error!("Failed to process content: {}", e);
                                        if self.config.one_shot {
                                            return Err(e);
                                        }
                                    } else if self.config.one_shot {
                                        info!("One-shot processing completed in {:?}", start_time.elapsed());
                                        return Ok(());
                                    }
                                }
                                last_content = content;
                            } else if self.config.one_shot {
                                info!("No content to process");
                                return Ok(());
                            }
                        },
                        Err(e) => {
                            error!("Failed to read clipboard content: {}", e);
                            if self.config.one_shot {
                                return Err(ClipboardError::ClipboardReadError(e.to_string()));
                            }
                        }
                    }
                },
                _ = signal::ctrl_c() => {
                    info!("Received Ctrl+C, terminating clipboard watcher.");
                    info!("Total runtime: {:?}", start_time.elapsed());
                    info!("Total token usage: {}", 
                        self.total_token_usage.format_details(&self.config.model)
                    );
                    break;
                }
            }
        }

        Ok(())
    }

    async fn process_standard(&mut self, content: &str) -> Result<(), ClipboardError> {
        let start_time = Instant::now();
        let mut files_processed = Vec::new();

        match self.extractor.extract(content) {
            Ok(blocks) => {
                for block in blocks {
                    debug!("Applying block: {:?}", block);
                    let applier: Box<dyn Applier> = match block.block_type {
                        crate::extractor::BlockType::FullContent => {
                            Box::new(FullContentApplier::new(&self.config.watch_path))
                        }
                        crate::extractor::BlockType::UnifiedDiff => {
                            Box::new(DiffApplier::new(&self.config.watch_path))
                        }
                        crate::extractor::BlockType::SearchReplaceBlock => {
                            Box::new(SearchReplaceApplier::new(&self.config.watch_path))
                        }
                    };

                    let file_start_time = Instant::now();
                    if let Err(e) = applier.apply(&block).await {
                        error!("Failed to apply block: {}", e);
                        return Err(ClipboardError::ContentApplicationError(e.to_string()));
                    } else {
                        info!("Successfully applied block to {}", block.filename);
                        self.add_to_history(block.filename.clone());
                        files_processed.push((block.filename, file_start_time.elapsed()));
                    }
                }

                info!("Standard processing completed in {:?}", start_time.elapsed());
                info!("Files processed:");
                for (file, duration) in files_processed {
                    info!("  {} (took {:?})", file, duration);
                }

                Ok(())
            },
            Err(e) => Err(ClipboardError::ContentExtractionError(e.to_string()))
        }
    }
}