lsp-diagnostics.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { z } from "zod";
  2. import { Tool } from "./tool";
  3. import path from "node:path";
  4. import { LSP } from "../lsp";
  5. import { App } from "../app";
  6. export const LspDiagnosticTool = Tool.define({
  7. name: "diagnostics",
  8. description: `Get diagnostics for a file and/or project.
  9. WHEN TO USE THIS TOOL:
  10. - Use when you need to check for errors or warnings in your code
  11. - Helpful for debugging and ensuring code quality
  12. - Good for getting a quick overview of issues in a file or project
  13. HOW TO USE:
  14. - Provide a path to a file to get diagnostics for that file
  15. - Results are displayed in a structured format with severity levels
  16. FEATURES:
  17. - Displays errors, warnings, and hints
  18. - Groups diagnostics by severity
  19. - Provides detailed information about each diagnostic
  20. LIMITATIONS:
  21. - Results are limited to the diagnostics provided by the LSP clients
  22. - May not cover all possible issues in the code
  23. - Does not provide suggestions for fixing issues
  24. TIPS:
  25. - Use in conjunction with other tools for a comprehensive code review
  26. - Combine with the LSP client for real-time diagnostics`,
  27. parameters: z.object({
  28. path: z.string().describe("The path to the file to get diagnostics."),
  29. }),
  30. execute: async (args) => {
  31. const app = await App.use();
  32. const normalized = path.isAbsolute(args.path)
  33. ? args.path
  34. : path.join(app.root, args.path);
  35. await LSP.file(normalized);
  36. const diagnostics = await LSP.diagnostics();
  37. const file = diagnostics[normalized];
  38. return {
  39. metadata: {
  40. diagnostics,
  41. },
  42. output: file?.length
  43. ? file.map(LSP.Diagnostic.pretty).join("\n")
  44. : "No errors found",
  45. };
  46. },
  47. });