|
| 1 | +import * as path from "path" |
| 2 | +import * as vscode from "vscode" |
| 3 | + |
| 4 | +import { Task } from "../task/Task" |
| 5 | +import { diagnosticsToProblemsString } from "../../integrations/diagnostics" |
| 6 | +import type { ToolUse } from "../../shared/tools" |
| 7 | + |
| 8 | +import { BaseTool, ToolCallbacks } from "./BaseTool" |
| 9 | + |
| 10 | +const NO_EDITS_MESSAGE = |
| 11 | + "No files have been edited in this task yet. Edit a file, then use read_lints to see errors and warnings." |
| 12 | +const NO_PROBLEMS_MESSAGE = "No errors or warnings detected." |
| 13 | + |
| 14 | +interface ReadLintsParams { |
| 15 | + paths?: string[] |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Normalize a path to POSIX relative form for comparison with editedFilePaths. |
| 20 | + */ |
| 21 | +function toRelativePosix(cwd: string, absolutePath: string): string { |
| 22 | + return path.relative(cwd, absolutePath).toPosix() |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Check if a file URI is under a directory (both relative to cwd). |
| 27 | + */ |
| 28 | +function isUnderDir(fileRelPosix: string, dirRelPosix: string): boolean { |
| 29 | + if (dirRelPosix === "." || dirRelPosix === "") { |
| 30 | + return true |
| 31 | + } |
| 32 | + const norm = dirRelPosix.endsWith("/") ? dirRelPosix : dirRelPosix + "/" |
| 33 | + return fileRelPosix === dirRelPosix || fileRelPosix.startsWith(norm) |
| 34 | +} |
| 35 | + |
| 36 | +export class ReadLintsTool extends BaseTool<"read_lints"> { |
| 37 | + readonly name = "read_lints" as const |
| 38 | + |
| 39 | + async execute(params: ReadLintsParams, task: Task, callbacks: ToolCallbacks): Promise<void> { |
| 40 | + const { pushToolResult, handleError } = callbacks |
| 41 | + const { paths } = params |
| 42 | + const cwd = task.cwd |
| 43 | + |
| 44 | + try { |
| 45 | + const state = await task.providerRef.deref()?.getState() |
| 46 | + const includeDiagnosticMessages = state?.includeDiagnosticMessages ?? true |
| 47 | + const maxDiagnosticMessages = state?.maxDiagnosticMessages ?? 50 |
| 48 | + |
| 49 | + if (!includeDiagnosticMessages) { |
| 50 | + pushToolResult(NO_PROBLEMS_MESSAGE) |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + let diagnosticsTuples: [vscode.Uri, vscode.Diagnostic[]][] = [] |
| 55 | + |
| 56 | + if (paths === undefined || paths.length === 0) { |
| 57 | + // No paths: return diagnostics only for files edited in this task |
| 58 | + if (task.editedFilePaths.size === 0) { |
| 59 | + pushToolResult(NO_EDITS_MESSAGE) |
| 60 | + return |
| 61 | + } |
| 62 | + const allDiagnostics = vscode.languages.getDiagnostics() |
| 63 | + const editedSet = task.editedFilePaths |
| 64 | + for (const [uri, diags] of allDiagnostics) { |
| 65 | + const relPosix = toRelativePosix(cwd, uri.fsPath) |
| 66 | + if (editedSet.has(relPosix)) { |
| 67 | + diagnosticsTuples.push([uri, diags]) |
| 68 | + } |
| 69 | + } |
| 70 | + } else { |
| 71 | + // Paths provided: return diagnostics for those files/directories |
| 72 | + const allDiagnostics = vscode.languages.getDiagnostics() |
| 73 | + const dirPaths: string[] = [] |
| 74 | + const fileUris: vscode.Uri[] = [] |
| 75 | + |
| 76 | + for (const relPath of paths) { |
| 77 | + if (!relPath || typeof relPath !== "string") continue |
| 78 | + const absolutePath = path.resolve(cwd, relPath) |
| 79 | + const uri = vscode.Uri.file(absolutePath) |
| 80 | + try { |
| 81 | + const stat = await vscode.workspace.fs.stat(uri) |
| 82 | + if (stat.type === vscode.FileType.Directory) { |
| 83 | + dirPaths.push(toRelativePosix(cwd, absolutePath)) |
| 84 | + } else { |
| 85 | + fileUris.push(uri) |
| 86 | + } |
| 87 | + } catch { |
| 88 | + // Path may not exist; treat as file and try getDiagnostics(uri) |
| 89 | + fileUris.push(uri) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + const seenUri = new Set<string>() |
| 94 | + for (const uri of fileUris) { |
| 95 | + const diags = vscode.languages.getDiagnostics(uri) |
| 96 | + if (diags.length > 0) { |
| 97 | + diagnosticsTuples.push([uri, diags]) |
| 98 | + seenUri.add(uri.toString()) |
| 99 | + } |
| 100 | + } |
| 101 | + for (const [uri, diags] of allDiagnostics) { |
| 102 | + if (diags.length === 0 || seenUri.has(uri.toString())) continue |
| 103 | + const fileRelPosix = toRelativePosix(cwd, uri.fsPath) |
| 104 | + const included = dirPaths.some((dirRelPosix) => isUnderDir(fileRelPosix, dirRelPosix)) |
| 105 | + if (included) { |
| 106 | + diagnosticsTuples.push([uri, diags]) |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + const result = await diagnosticsToProblemsString( |
| 112 | + diagnosticsTuples, |
| 113 | + [vscode.DiagnosticSeverity.Error, vscode.DiagnosticSeverity.Warning], |
| 114 | + cwd, |
| 115 | + true, |
| 116 | + maxDiagnosticMessages, |
| 117 | + ) |
| 118 | + |
| 119 | + pushToolResult(result.trim() ? result.trim() : NO_PROBLEMS_MESSAGE) |
| 120 | + } catch (error) { |
| 121 | + await handleError("reading lints", error instanceof Error ? error : new Error(String(error))) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +export const readLintsTool = new ReadLintsTool() |
0 commit comments