import type { Diagnostic } from "./types/code-workspace"; interface DiagnosticPattern { kind: Diagnostic["kind"]; regex: RegExp; build: (match: RegExpExecArray) => Diagnostic; } const PATTERNS: DiagnosticPattern[] = [ // TypeScript compiler: `src/foo.ts(12,34): error TS2345: message` { kind: "typecheck", regex: /^(?[^\s(][^(]*?)\((?\d+),(?\d+)\):\s*(?error|warning)\s+(?TS\d+):\s*(?.+)$/gm, build: (m) => ({ kind: "typecheck", severity: m.groups?.severity === "warning" ? "warning" : "error", file: m.groups?.file, line: Number(m.groups?.line), column: Number(m.groups?.column), code: m.groups?.code, message: m.groups?.message?.trim() ?? "", }), }, // Biome/ESLint-style: `src/foo.ts:12:34 error message rule-name` or with lint prefix lines { kind: "lint", regex: /^(?[^\s:][^:]*?):(?\d+):(?\d+)\s*[-:]?\s*(?error|warning)\b[:\s]+(?.+)$/gm, build: (m) => ({ kind: "lint", severity: m.groups?.severity === "warning" ? "warning" : "error", file: m.groups?.file, line: Number(m.groups?.line), column: Number(m.groups?.column), message: m.groups?.message?.trim() ?? "", }), }, // Rust compiler: `error[E0432]: message` followed by ` --> src/main.rs:10:5` { kind: "compiler", regex: /^error(?:\[(?E\d+)\])?:\s*(?.+)\n\s*-->\s*(?[^\s:]+):(?\d+):(?\d+)/gm, build: (m) => ({ kind: "compiler", severity: "error", file: m.groups?.file, line: Number(m.groups?.line), column: Number(m.groups?.column), code: m.groups?.code, message: m.groups?.message?.trim() ?? "", }), }, // pytest: `path/to/test_file.py:23: AssertionError: message` { kind: "test", regex: /^(?[^\s:][^:]*?\.py):(?\d+):\s*(?(?:AssertionError|Error|Exception)[^\n]*)$/gm, build: (m) => ({ kind: "test", severity: "error", file: m.groups?.file, line: Number(m.groups?.line), message: m.groups?.message?.trim() ?? "", }), }, // vitest/jest: `FAIL src/foo.test.ts` { kind: "test", regex: /^\s*FAIL\s+(?\S+\.(?:test|spec)\.(?:ts|tsx|js|jsx))\b(?.*)$/gm, build: (m) => ({ kind: "test", severity: "error", file: m.groups?.file, message: m.groups?.message?.trim() || "Test file failed", }), }, ]; const MAX_DIAGNOSTICS = 200; /** * Extracts structured diagnostics from raw compiler/typechecker/linter/test * output so the model can jump straight to file/line instead of re-parsing a * wall of text on every repair-loop turn. Best-effort: unmatched output is * simply not represented here, the raw stdout/stderr always remains available. */ export function parseDiagnostics(stdout: string, stderr: string): Diagnostic[] { const combined = [stdout, stderr].filter(Boolean).join("\n"); if (!combined.trim()) return []; const diagnostics: Diagnostic[] = []; for (const pattern of PATTERNS) { pattern.regex.lastIndex = 0; for (const match of combined.matchAll(pattern.regex)) { diagnostics.push(pattern.build(match as RegExpExecArray)); if (diagnostics.length >= MAX_DIAGNOSTICS) return diagnostics; } } return diagnostics; }