From c040baae118787cd0573e5b674a2a225f36d898c Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Tue, 27 May 2025 02:17:35 -0400 Subject: Refactor LSP tools and add hover functionality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Split diagnostics tool into separate lsp-diagnostics.ts file - Add new lsp-hover.ts tool for LSP hover information - Update tool exports and session integration - Remove old diagnostics.ts file 🤖 Generated with opencode Co-Authored-By: opencode --- js/src/tool/diagnostics.ts | 53 ------------------------------------------ js/src/tool/index.ts | 3 ++- js/src/tool/lsp-diagnostics.ts | 53 ++++++++++++++++++++++++++++++++++++++++++ js/src/tool/lsp-hover.ts | 38 ++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 54 deletions(-) delete mode 100644 js/src/tool/diagnostics.ts create mode 100644 js/src/tool/lsp-diagnostics.ts create mode 100644 js/src/tool/lsp-hover.ts (limited to 'js/src/tool') diff --git a/js/src/tool/diagnostics.ts b/js/src/tool/diagnostics.ts deleted file mode 100644 index 3610c7781..000000000 --- a/js/src/tool/diagnostics.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { z } from "zod"; -import { Tool } from "./tool"; -import path from "node:path"; -import { LSP } from "../lsp"; -import { App } from "../app"; - -export const DiagnosticsTool = Tool.define({ - name: "diagnostics", - description: `Get diagnostics for a file and/or project. - -WHEN TO USE THIS TOOL: -- Use when you need to check for errors or warnings in your code -- Helpful for debugging and ensuring code quality -- Good for getting a quick overview of issues in a file or project - -HOW TO USE: -- Provide a path to a file to get diagnostics for that file -- Results are displayed in a structured format with severity levels - -FEATURES: -- Displays errors, warnings, and hints -- Groups diagnostics by severity -- Provides detailed information about each diagnostic - -LIMITATIONS: -- Results are limited to the diagnostics provided by the LSP clients -- May not cover all possible issues in the code -- Does not provide suggestions for fixing issues - -TIPS: -- Use in conjunction with other tools for a comprehensive code review -- Combine with the LSP client for real-time diagnostics`, - parameters: z.object({ - path: z.string().describe("The path to the file to get diagnostics."), - }), - execute: async (args) => { - const app = await App.use(); - const normalized = path.isAbsolute(args.path) - ? args.path - : path.join(app.root, args.path); - await LSP.file(normalized); - const diagnostics = await LSP.diagnostics(); - const file = diagnostics[normalized]; - return { - metadata: { - diagnostics, - }, - output: file?.length - ? file.map(LSP.Diagnostic.pretty).join("\n") - : "No errors found", - }; - }, -}); diff --git a/js/src/tool/index.ts b/js/src/tool/index.ts index b18f85012..3930c87c4 100644 --- a/js/src/tool/index.ts +++ b/js/src/tool/index.ts @@ -5,4 +5,5 @@ export * from "./glob"; export * from "./grep"; export * from "./view"; export * from "./ls"; -export * from "./diagnostics"; +export * from "./lsp-diagnostics"; +export * from "./lsp-hover"; diff --git a/js/src/tool/lsp-diagnostics.ts b/js/src/tool/lsp-diagnostics.ts new file mode 100644 index 000000000..41c33f822 --- /dev/null +++ b/js/src/tool/lsp-diagnostics.ts @@ -0,0 +1,53 @@ +import { z } from "zod"; +import { Tool } from "./tool"; +import path from "node:path"; +import { LSP } from "../lsp"; +import { App } from "../app"; + +export const LspDiagnosticTool = Tool.define({ + name: "diagnostics", + description: `Get diagnostics for a file and/or project. + +WHEN TO USE THIS TOOL: +- Use when you need to check for errors or warnings in your code +- Helpful for debugging and ensuring code quality +- Good for getting a quick overview of issues in a file or project + +HOW TO USE: +- Provide a path to a file to get diagnostics for that file +- Results are displayed in a structured format with severity levels + +FEATURES: +- Displays errors, warnings, and hints +- Groups diagnostics by severity +- Provides detailed information about each diagnostic + +LIMITATIONS: +- Results are limited to the diagnostics provided by the LSP clients +- May not cover all possible issues in the code +- Does not provide suggestions for fixing issues + +TIPS: +- Use in conjunction with other tools for a comprehensive code review +- Combine with the LSP client for real-time diagnostics`, + parameters: z.object({ + path: z.string().describe("The path to the file to get diagnostics."), + }), + execute: async (args) => { + const app = await App.use(); + const normalized = path.isAbsolute(args.path) + ? args.path + : path.join(app.root, args.path); + await LSP.file(normalized); + const diagnostics = await LSP.diagnostics(); + const file = diagnostics[normalized]; + return { + metadata: { + diagnostics, + }, + output: file?.length + ? file.map(LSP.Diagnostic.pretty).join("\n") + : "No errors found", + }; + }, +}); diff --git a/js/src/tool/lsp-hover.ts b/js/src/tool/lsp-hover.ts new file mode 100644 index 000000000..9957920a2 --- /dev/null +++ b/js/src/tool/lsp-hover.ts @@ -0,0 +1,38 @@ +import { z } from "zod"; +import { Tool } from "./tool"; +import path from "node:path"; +import { LSP } from "../lsp"; +import { App } from "../app"; + +export const LspHoverTool = Tool.define({ + name: "lsp.hover", + description: ` + Looks up hover information for a given position in a source file using the Language Server Protocol (LSP). + This includes type information, documentation, or symbol details at the specified line and character. + Useful for providing code insights, explanations, or context-aware assistance based on the user's current cursor location. + `, + parameters: z.object({ + file: z.string().describe("The path to the file to get diagnostics."), + line: z.number().describe("The line number to get diagnostics."), + character: z.number().describe("The character number to get diagnostics."), + }), + execute: async (args) => { + console.log(args); + const app = await App.use(); + const file = path.isAbsolute(args.file) + ? args.file + : path.join(app.root, args.file); + await LSP.file(file); + const result = await LSP.hover({ + ...args, + file, + }); + console.log(result); + return { + metadata: { + result, + }, + output: JSON.stringify(result, null, 2), + }; + }, +}); -- cgit v1.2.3