|
| 1 | +import { exec } from "child_process"; |
| 2 | +import { resolve, relative } from "path"; |
| 3 | +import { BaseTool, ToolParameter, ToolResult } from "./base.js"; |
| 4 | + |
| 5 | +/** |
| 6 | + * get_diagnostics — runs the TypeScript compiler in noEmit mode and parses |
| 7 | + * the output into a structured list of errors/warnings, similar to the |
| 8 | + * VS Code "Problems" panel. The agent should call this tool after every |
| 9 | + * implementation to validate that no type errors were introduced. |
| 10 | + */ |
| 11 | +export class DiagnosticsTool extends BaseTool { |
| 12 | + name = "get_diagnostics"; |
| 13 | + description = |
| 14 | + "Runs a universal diagnostics check on the project. It combines: " + |
| 15 | + "1. Real-time diagnostics from the Language Server (LSP) for modified files. " + |
| 16 | + "2. Project-specific linter/checker discovery (npm lint, cargo check, go vet, etc). " + |
| 17 | + "Use this tool ALWAYS at the end of any implementation to catch problems like VS Code's 'Problems' panel."; |
| 18 | + |
| 19 | + parameters: ToolParameter[] = [ |
| 20 | + { |
| 21 | + name: "path", |
| 22 | + type: "string", |
| 23 | + description: |
| 24 | + "Root directory of the project to check. Defaults to the current working directory.", |
| 25 | + required: false, |
| 26 | + }, |
| 27 | + ]; |
| 28 | + |
| 29 | + async execute(args: Record<string, unknown>): Promise<ToolResult> { |
| 30 | + const cwd = resolve(process.cwd(), (args.path as string) || "."); |
| 31 | + |
| 32 | + // Detect what checker to run |
| 33 | + const { getTrackedFiles } = await import("../services/file-tracker.js"); |
| 34 | + const modifiedFiles = getTrackedFiles().filter(f => f.access === "modified"); |
| 35 | + |
| 36 | + // 1. Get diagnostics from the Language Server (LSP) — The most "Universal" way |
| 37 | + const lspProblems = await this.getLspDiagnostics(cwd, modifiedFiles); |
| 38 | + |
| 39 | + // 2. Discover and run project-specific diagnostic scripts (npm lint, cargo check, etc) |
| 40 | + const projectProblems = await this.runProjectDiagnostics(cwd); |
| 41 | + |
| 42 | + const allProblems = [...lspProblems, ...projectProblems]; |
| 43 | + |
| 44 | + if (allProblems.length === 0) { |
| 45 | + return this.success("✅ No problems found — project is clean."); |
| 46 | + } |
| 47 | + |
| 48 | + return this.success(`🔴 Found ${allProblems.length} diagnostic problem(s):\n\n${allProblems.join("\n")}`); |
| 49 | + } |
| 50 | + |
| 51 | + /** Gets real-time diagnostics from the LSP client */ |
| 52 | + private async getLspDiagnostics(cwd: string, files: any[]): Promise<string[]> { |
| 53 | + const { LSPClient } = await import("../services/lsp-client.js"); |
| 54 | + const client = new LSPClient(cwd); |
| 55 | + try { |
| 56 | + await client.start(); |
| 57 | + // Open all modified files so the server starts checking them |
| 58 | + for (const file of files) { |
| 59 | + await client.openFile(file.path); |
| 60 | + } |
| 61 | + // Wait for server to process and emit diagnostics |
| 62 | + await new Promise(resolve => setTimeout(resolve, 2000)); |
| 63 | + |
| 64 | + const diagnostics = client.getDiagnostics(); |
| 65 | + await client.stop(); |
| 66 | + |
| 67 | + const results: string[] = []; |
| 68 | + for (const item of diagnostics) { |
| 69 | + const relPath = relative(cwd, item.uri.replace("file:///", "")); |
| 70 | + for (const diag of item.diagnostics) { |
| 71 | + const severity = diag.severity === 1 ? "🔴" : "🟡"; |
| 72 | + results.push(`${severity} ${relPath}:${diag.range.start.line + 1}:${diag.range.start.character + 1} — ${diag.message}`); |
| 73 | + } |
| 74 | + } |
| 75 | + return results; |
| 76 | + } catch { |
| 77 | + return []; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /** Discovers and runs any project-native diagnostic tools */ |
| 82 | + private async runProjectDiagnostics(cwd: string): Promise<string[]> { |
| 83 | + const { readFile } = await import("fs/promises"); |
| 84 | + const { existsSync } = await import("fs"); |
| 85 | + const results: string[] = []; |
| 86 | + |
| 87 | + // Try finding lint/check scripts in meta files |
| 88 | + try { |
| 89 | + // Node.js |
| 90 | + if (existsSync(resolve(cwd, "package.json"))) { |
| 91 | + const pkg = JSON.parse(await readFile(resolve(cwd, "package.json"), "utf-8")); |
| 92 | + const scripts = pkg.scripts || {}; |
| 93 | + const diagScript = Object.keys(scripts).find(k => k.includes("lint") || k.includes("check") || k.includes("type-check")); |
| 94 | + if (diagScript) { |
| 95 | + const output = await this.execCommand(`npm run ${diagScript}`, cwd); |
| 96 | + if (output) results.push(`[npm run ${diagScript}]:\n${output}`); |
| 97 | + } |
| 98 | + } |
| 99 | + // Rust |
| 100 | + if (existsSync(resolve(cwd, "Cargo.toml"))) { |
| 101 | + const output = await this.execCommand("cargo check --message-format short", cwd); |
| 102 | + if (output) results.push(`[cargo check]:\n${output}`); |
| 103 | + } |
| 104 | + // Go |
| 105 | + if (existsSync(resolve(cwd, "go.mod"))) { |
| 106 | + const output = await this.execCommand("go vet ./...", cwd); |
| 107 | + if (output) results.push(`[go vet]:\n${output}`); |
| 108 | + } |
| 109 | + } catch {} |
| 110 | + |
| 111 | + return results; |
| 112 | + } |
| 113 | + |
| 114 | + private async execCommand(cmd: string, cwd: string): Promise<string | null> { |
| 115 | + return new Promise((resolve) => { |
| 116 | + exec(cmd, { cwd, maxBuffer: 5 * 1024 * 1024 }, (error, stdout, stderr) => { |
| 117 | + if (error) { |
| 118 | + resolve((stdout + stderr).trim()); |
| 119 | + } else { |
| 120 | + resolve(null); |
| 121 | + } |
| 122 | + }); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + /** Helper for tsc parsing as fallback */ |
| 127 | + private parseTscOutput(lines: string[], cwd: string): any { |
| 128 | + // keeping this helper if needed internally |
| 129 | + return {}; |
| 130 | + } |
| 131 | +} |
0 commit comments