syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Diagnostics tool for detecting code errors via IDE/LSP integration
//!
//! This tool queries the IDE's language servers (via MCP) or falls back to
//! running language-specific commands to detect errors in the code.
//!
//! ## Usage
//!
//! The agent can use this tool after writing or modifying files to check
//! for compilation errors, type errors, linting issues, etc.
//!
//! ## Supported Methods
//!
//! 1. **IDE Integration (preferred)**: If connected to an IDE via MCP,
//!    queries language servers directly (rust-analyzer, TypeScript, ESLint, etc.)
//!
//! 2. **Command Fallback**: If no IDE is connected, runs language-specific
//!    commands based on detected project type:
//!    - Rust: `cargo check`
//!    - JavaScript/TypeScript: `npm run lint` or `eslint`
//!    - Python: `python -m py_compile` or `pylint`
//!    - Go: `go build`

use crate::agent::ide::{Diagnostic, DiagnosticSeverity, DiagnosticsResponse, IdeClient};
use rig::completion::ToolDefinition;
use rig::tool::Tool;
use serde::Deserialize;
use serde_json::json;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::process::Command;
use tokio::sync::Mutex;

#[derive(Debug, Deserialize)]
pub struct DiagnosticsArgs {
    /// Optional file path to check. If not provided, checks all files.
    pub path: Option<String>,
    /// Whether to include warnings (default: true)
    pub include_warnings: Option<bool>,
    /// Maximum number of diagnostics to return (default: 50)
    pub limit: Option<usize>,
}

#[derive(Debug, thiserror::Error)]
#[error("Diagnostics error: {0}")]
pub struct DiagnosticsError(String);

#[derive(Debug, Clone)]
pub struct DiagnosticsTool {
    project_path: PathBuf,
    /// Optional IDE client for LSP integration
    ide_client: Option<Arc<Mutex<IdeClient>>>,
}

impl DiagnosticsTool {
    pub fn new(project_path: PathBuf) -> Self {
        Self {
            project_path,
            ide_client: None,
        }
    }

    /// Set the IDE client for LSP integration
    pub fn with_ide_client(mut self, ide_client: Arc<Mutex<IdeClient>>) -> Self {
        self.ide_client = Some(ide_client);
        self
    }

    /// Detect project type based on files present
    fn detect_project_type(&self) -> ProjectType {
        let cargo_toml = self.project_path.join("Cargo.toml");
        let package_json = self.project_path.join("package.json");
        let go_mod = self.project_path.join("go.mod");
        let pyproject_toml = self.project_path.join("pyproject.toml");
        let requirements_txt = self.project_path.join("requirements.txt");

        if cargo_toml.exists() {
            ProjectType::Rust
        } else if package_json.exists() {
            ProjectType::JavaScript
        } else if go_mod.exists() {
            ProjectType::Go
        } else if pyproject_toml.exists() || requirements_txt.exists() {
            ProjectType::Python
        } else {
            ProjectType::Unknown
        }
    }

    /// Get diagnostics from IDE via MCP
    async fn get_ide_diagnostics(&self, file_path: Option<&str>) -> Option<DiagnosticsResponse> {
        let client = self.ide_client.as_ref()?;
        let guard = client.lock().await;

        if !guard.is_connected() {
            return None;
        }

        guard.get_diagnostics(file_path).await.ok()
    }

    /// Run fallback command-based diagnostics
    async fn get_command_diagnostics(&self) -> Result<DiagnosticsResponse, DiagnosticsError> {
        let project_type = self.detect_project_type();

        match project_type {
            ProjectType::Rust => self.run_cargo_check().await,
            ProjectType::JavaScript => self.run_npm_lint().await,
            ProjectType::Go => self.run_go_build().await,
            ProjectType::Python => self.run_python_check().await,
            ProjectType::Unknown => Ok(DiagnosticsResponse {
                diagnostics: Vec::new(),
                total_errors: 0,
                total_warnings: 0,
            }),
        }
    }

    /// Run cargo check and parse output
    async fn run_cargo_check(&self) -> Result<DiagnosticsResponse, DiagnosticsError> {
        let output = Command::new("cargo")
            .args(["check", "--message-format=json"])
            .current_dir(&self.project_path)
            .output()
            .await
            .map_err(|e| DiagnosticsError(format!("Failed to run cargo check: {}", e)))?;

        let stdout = String::from_utf8_lossy(&output.stdout);
        let mut diagnostics = Vec::new();

        for line in stdout.lines() {
            if let Ok(msg) = serde_json::from_str::<serde_json::Value>(line)
                && msg.get("reason").and_then(|r| r.as_str()) == Some("compiler-message")
                && let Some(message) = msg.get("message")
                && let Some(diag) = self.parse_cargo_message(message)
            {
                diagnostics.push(diag);
            }
        }

        let total_errors = diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Error)
            .count() as u32;
        let total_warnings = diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Warning)
            .count() as u32;

        Ok(DiagnosticsResponse {
            diagnostics,
            total_errors,
            total_warnings,
        })
    }

    /// Parse a cargo compiler message into a Diagnostic
    fn parse_cargo_message(&self, message: &serde_json::Value) -> Option<Diagnostic> {
        let level = message.get("level")?.as_str()?;
        let msg = message.get("message")?.as_str()?;

        let severity = match level {
            "error" => DiagnosticSeverity::Error,
            "warning" => DiagnosticSeverity::Warning,
            "note" | "help" => DiagnosticSeverity::Hint,
            _ => DiagnosticSeverity::Information,
        };

        // Get the primary span
        let spans = message.get("spans")?.as_array()?;
        let span = spans
            .iter()
            .find(|s| {
                s.get("is_primary")
                    .and_then(|v| v.as_bool())
                    .unwrap_or(false)
            })
            .or_else(|| spans.first())?;

        let file = span.get("file_name")?.as_str()?;
        let line = span.get("line_start")?.as_u64()? as u32;
        let column = span.get("column_start")?.as_u64()? as u32;
        let end_line = span
            .get("line_end")
            .and_then(|v| v.as_u64())
            .map(|v| v as u32);
        let end_column = span
            .get("column_end")
            .and_then(|v| v.as_u64())
            .map(|v| v as u32);

        let code = message
            .get("code")
            .and_then(|c| c.get("code"))
            .and_then(|c| c.as_str())
            .map(|s| s.to_string());

        Some(Diagnostic {
            file: file.to_string(),
            line,
            column,
            end_line,
            end_column,
            severity,
            message: msg.to_string(),
            source: Some("rustc".to_string()),
            code,
        })
    }

    /// Run npm lint and parse output
    async fn run_npm_lint(&self) -> Result<DiagnosticsResponse, DiagnosticsError> {
        // Try npm run lint first
        let output = Command::new("npm")
            .args(["run", "lint", "--", "--format=json"])
            .current_dir(&self.project_path)
            .output()
            .await;

        if let Ok(output) = output
            && (output.status.success() || !output.stdout.is_empty())
        {
            let stdout = String::from_utf8_lossy(&output.stdout);
            if let Ok(results) = serde_json::from_str::<Vec<serde_json::Value>>(&stdout) {
                return Ok(self.parse_eslint_output(&results));
            }
        }

        // If that fails, try npx eslint
        let output = Command::new("npx")
            .args(["eslint", ".", "--format=json"])
            .current_dir(&self.project_path)
            .output()
            .await
            .map_err(|e| DiagnosticsError(format!("Failed to run eslint: {}", e)))?;

        let stdout = String::from_utf8_lossy(&output.stdout);
        if let Ok(results) = serde_json::from_str::<Vec<serde_json::Value>>(&stdout) {
            return Ok(self.parse_eslint_output(&results));
        }

        // Return empty if we couldn't parse
        Ok(DiagnosticsResponse {
            diagnostics: Vec::new(),
            total_errors: 0,
            total_warnings: 0,
        })
    }

    /// Parse ESLint JSON output
    fn parse_eslint_output(&self, results: &[serde_json::Value]) -> DiagnosticsResponse {
        let mut diagnostics = Vec::new();

        for file_result in results {
            let file = file_result
                .get("filePath")
                .and_then(|f| f.as_str())
                .unwrap_or("");

            if let Some(messages) = file_result.get("messages").and_then(|m| m.as_array()) {
                for msg in messages {
                    let severity = match msg.get("severity").and_then(|s| s.as_u64()) {
                        Some(2) => DiagnosticSeverity::Error,
                        Some(1) => DiagnosticSeverity::Warning,
                        _ => DiagnosticSeverity::Information,
                    };

                    let message = msg
                        .get("message")
                        .and_then(|m| m.as_str())
                        .unwrap_or("")
                        .to_string();
                    let line = msg.get("line").and_then(|l| l.as_u64()).unwrap_or(1) as u32;
                    let column = msg.get("column").and_then(|c| c.as_u64()).unwrap_or(1) as u32;
                    let end_line = msg
                        .get("endLine")
                        .and_then(|l| l.as_u64())
                        .map(|v| v as u32);
                    let end_column = msg
                        .get("endColumn")
                        .and_then(|c| c.as_u64())
                        .map(|v| v as u32);
                    let code = msg
                        .get("ruleId")
                        .and_then(|r| r.as_str())
                        .map(|s| s.to_string());

                    diagnostics.push(Diagnostic {
                        file: file.to_string(),
                        line,
                        column,
                        end_line,
                        end_column,
                        severity,
                        message,
                        source: Some("eslint".to_string()),
                        code,
                    });
                }
            }
        }

        let total_errors = diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Error)
            .count() as u32;
        let total_warnings = diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Warning)
            .count() as u32;

        DiagnosticsResponse {
            diagnostics,
            total_errors,
            total_warnings,
        }
    }

    /// Run go build and parse output
    async fn run_go_build(&self) -> Result<DiagnosticsResponse, DiagnosticsError> {
        let output = Command::new("go")
            .args(["build", "-o", "/dev/null", "./..."])
            .current_dir(&self.project_path)
            .output()
            .await
            .map_err(|e| DiagnosticsError(format!("Failed to run go build: {}", e)))?;

        let stderr = String::from_utf8_lossy(&output.stderr);
        let mut diagnostics = Vec::new();

        // Parse go build output: "file.go:line:col: message"
        for line in stderr.lines() {
            if let Some(diag) = self.parse_go_error(line) {
                diagnostics.push(diag);
            }
        }

        let total_errors = diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Error)
            .count() as u32;
        let total_warnings = diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Warning)
            .count() as u32;

        Ok(DiagnosticsResponse {
            diagnostics,
            total_errors,
            total_warnings,
        })
    }

    /// Parse a Go error line
    fn parse_go_error(&self, line: &str) -> Option<Diagnostic> {
        // Format: file.go:line:col: message
        let parts: Vec<&str> = line.splitn(4, ':').collect();
        if parts.len() < 4 {
            return None;
        }

        let file = parts[0].to_string();
        let line_num = parts[1].parse::<u32>().ok()?;
        let column = parts[2].parse::<u32>().ok()?;
        let message = parts[3].trim().to_string();

        Some(Diagnostic {
            file,
            line: line_num,
            column,
            end_line: None,
            end_column: None,
            severity: DiagnosticSeverity::Error,
            message,
            source: Some("go".to_string()),
            code: None,
        })
    }

    /// Run Python syntax check
    async fn run_python_check(&self) -> Result<DiagnosticsResponse, DiagnosticsError> {
        // Try pylint first
        let output = Command::new("pylint")
            .args(["--output-format=json", "."])
            .current_dir(&self.project_path)
            .output()
            .await;

        if let Ok(output) = output {
            let stdout = String::from_utf8_lossy(&output.stdout);
            if let Ok(results) = serde_json::from_str::<Vec<serde_json::Value>>(&stdout) {
                return Ok(self.parse_pylint_output(&results));
            }
        }

        // Fallback: just return empty
        Ok(DiagnosticsResponse {
            diagnostics: Vec::new(),
            total_errors: 0,
            total_warnings: 0,
        })
    }

    /// Parse pylint JSON output
    fn parse_pylint_output(&self, results: &[serde_json::Value]) -> DiagnosticsResponse {
        let mut diagnostics = Vec::new();

        for msg in results {
            let msg_type = msg.get("type").and_then(|t| t.as_str()).unwrap_or("");
            let severity = match msg_type {
                "error" | "fatal" => DiagnosticSeverity::Error,
                "warning" => DiagnosticSeverity::Warning,
                "convention" | "refactor" => DiagnosticSeverity::Hint,
                _ => DiagnosticSeverity::Information,
            };

            let file = msg
                .get("path")
                .and_then(|p| p.as_str())
                .unwrap_or("")
                .to_string();
            let line = msg.get("line").and_then(|l| l.as_u64()).unwrap_or(1) as u32;
            let column = msg.get("column").and_then(|c| c.as_u64()).unwrap_or(1) as u32;
            let message = msg
                .get("message")
                .and_then(|m| m.as_str())
                .unwrap_or("")
                .to_string();
            let code = msg
                .get("message-id")
                .and_then(|m| m.as_str())
                .map(|s| s.to_string());

            diagnostics.push(Diagnostic {
                file,
                line,
                column,
                end_line: None,
                end_column: None,
                severity,
                message,
                source: Some("pylint".to_string()),
                code,
            });
        }

        let total_errors = diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Error)
            .count() as u32;
        let total_warnings = diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Warning)
            .count() as u32;

        DiagnosticsResponse {
            diagnostics,
            total_errors,
            total_warnings,
        }
    }

    /// Filter diagnostics based on user preferences
    fn filter_diagnostics(
        &self,
        mut response: DiagnosticsResponse,
        include_warnings: bool,
        limit: usize,
        file_path: Option<&str>,
    ) -> DiagnosticsResponse {
        // Filter by file if specified
        if let Some(path) = file_path {
            response.diagnostics.retain(|d| d.file.contains(path));
        }

        // Filter out warnings if not requested
        if !include_warnings {
            response
                .diagnostics
                .retain(|d| d.severity == DiagnosticSeverity::Error);
        }

        // Apply limit
        response.diagnostics.truncate(limit);

        // Recalculate totals
        response.total_errors = response
            .diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Error)
            .count() as u32;
        response.total_warnings = response
            .diagnostics
            .iter()
            .filter(|d| d.severity == DiagnosticSeverity::Warning)
            .count() as u32;

        response
    }
}

#[derive(Debug, Clone, Copy)]
enum ProjectType {
    Rust,
    JavaScript,
    Go,
    Python,
    Unknown,
}

impl Tool for DiagnosticsTool {
    const NAME: &'static str = "diagnostics";

    type Error = DiagnosticsError;
    type Args = DiagnosticsArgs;
    type Output = String;

    async fn definition(&self, _prompt: String) -> ToolDefinition {
        ToolDefinition {
            name: Self::NAME.to_string(),
            description: r#"Check for code errors, warnings, and linting issues.

This tool queries language servers or runs language-specific commands to detect:
- Compilation errors
- Type errors
- Syntax errors
- Linting warnings
- Best practice violations

Use this tool after writing or modifying code to verify there are no errors.

The tool automatically detects the project type and uses appropriate checking:
- Rust: Uses rust-analyzer or cargo check
- JavaScript/TypeScript: Uses ESLint or TypeScript compiler
- Go: Uses gopls or go build
- Python: Uses pylint or pyright

Returns a list of diagnostics with file locations, severity, and messages."#
                .to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "path": {
                        "type": "string",
                        "description": "Optional file path to check. If not provided, checks all files in the project."
                    },
                    "include_warnings": {
                        "type": "boolean",
                        "description": "Whether to include warnings in addition to errors (default: true)"
                    },
                    "limit": {
                        "type": "integer",
                        "description": "Maximum number of diagnostics to return (default: 50)"
                    }
                }
            }),
        }
    }

    async fn call(&self, args: Self::Args) -> Result<Self::Output, Self::Error> {
        let include_warnings = args.include_warnings.unwrap_or(true);
        let limit = args.limit.unwrap_or(50);
        let file_path = args.path.as_deref();

        // Try IDE first (better real-time diagnostics)
        let response = if let Some(ide_response) = self.get_ide_diagnostics(file_path).await {
            ide_response
        } else {
            // Fall back to command-based diagnostics
            self.get_command_diagnostics().await?
        };

        // Filter and limit results
        let filtered = self.filter_diagnostics(response, include_warnings, limit, file_path);

        // Format output
        let result = if filtered.diagnostics.is_empty() {
            json!({
                "success": true,
                "message": "No errors or warnings found",
                "total_errors": 0,
                "total_warnings": 0,
                "diagnostics": []
            })
        } else {
            let formatted_diagnostics: Vec<serde_json::Value> = filtered
                .diagnostics
                .iter()
                .map(|d| {
                    json!({
                        "file": d.file,
                        "line": d.line,
                        "column": d.column,
                        "severity": d.severity.as_str(),
                        "message": d.message,
                        "source": d.source,
                        "code": d.code
                    })
                })
                .collect();

            json!({
                "success": filtered.total_errors == 0,
                "total_errors": filtered.total_errors,
                "total_warnings": filtered.total_warnings,
                "diagnostics": formatted_diagnostics
            })
        };

        serde_json::to_string_pretty(&result)
            .map_err(|e| DiagnosticsError(format!("Failed to serialize: {}", e)))
    }
}

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

    #[tokio::test]
    async fn test_diagnostics_tool_creation() {
        let tool = DiagnosticsTool::new(PathBuf::from("."));
        assert_eq!(tool.project_path, PathBuf::from("."));
    }

    #[test]
    fn test_project_type_detection() {
        // This test would need a proper test directory setup
        let tool = DiagnosticsTool::new(env::current_dir().unwrap());
        let project_type = tool.detect_project_type();
        // Current project is Rust
        assert!(matches!(project_type, ProjectType::Rust));
    }

    #[test]
    fn test_parse_go_error() {
        let tool = DiagnosticsTool::new(PathBuf::from("."));
        let line = "main.go:10:5: undefined: foo";
        let diag = tool.parse_go_error(line);
        assert!(diag.is_some());
        let diag = diag.unwrap();
        assert_eq!(diag.file, "main.go");
        assert_eq!(diag.line, 10);
        assert_eq!(diag.column, 5);
        assert_eq!(diag.message, "undefined: foo");
    }

    #[test]
    fn test_filter_diagnostics() {
        let tool = DiagnosticsTool::new(PathBuf::from("."));
        let response = DiagnosticsResponse {
            diagnostics: vec![
                Diagnostic {
                    file: "src/main.rs".to_string(),
                    line: 1,
                    column: 1,
                    end_line: None,
                    end_column: None,
                    severity: DiagnosticSeverity::Error,
                    message: "error".to_string(),
                    source: None,
                    code: None,
                },
                Diagnostic {
                    file: "src/lib.rs".to_string(),
                    line: 1,
                    column: 1,
                    end_line: None,
                    end_column: None,
                    severity: DiagnosticSeverity::Warning,
                    message: "warning".to_string(),
                    source: None,
                    code: None,
                },
            ],
            total_errors: 1,
            total_warnings: 1,
        };

        // Filter to errors only
        let filtered = tool.filter_diagnostics(response.clone(), false, 50, None);
        assert_eq!(filtered.diagnostics.len(), 1);
        assert_eq!(filtered.total_errors, 1);
        assert_eq!(filtered.total_warnings, 0);

        // Filter by file
        let filtered = tool.filter_diagnostics(response, true, 50, Some("main.rs"));
        assert_eq!(filtered.diagnostics.len(), 1);
        assert_eq!(filtered.diagnostics[0].file, "src/main.rs");
    }
}