lsp-diagnostics.ts 898 B

123456789101112131415161718192021222324252627
  1. import { z } from "zod"
  2. import { Tool } from "./tool"
  3. import path from "path"
  4. import { LSP } from "../lsp"
  5. import { App } from "../app/app"
  6. import DESCRIPTION from "./lsp-diagnostics.txt"
  7. export const LspDiagnosticTool = Tool.define("lsp_diagnostics", {
  8. description: DESCRIPTION,
  9. parameters: z.object({
  10. path: z.string().describe("The path to the file to get diagnostics."),
  11. }),
  12. execute: async (args) => {
  13. const app = App.info()
  14. const normalized = path.isAbsolute(args.path) ? args.path : path.join(app.path.cwd, args.path)
  15. await LSP.touchFile(normalized, true)
  16. const diagnostics = await LSP.diagnostics()
  17. const file = diagnostics[normalized]
  18. return {
  19. title: path.relative(app.path.root, normalized),
  20. metadata: {
  21. diagnostics,
  22. },
  23. output: file?.length ? file.map(LSP.Diagnostic.pretty).join("\n") : "No errors found",
  24. }
  25. },
  26. })