From 1d66d8d4cfabb51877b8edc23ec31d0a99f3b7e2 Mon Sep 17 00:00:00 2001 From: wenytang-ms Date: Wed, 6 May 2026 10:44:35 +0800 Subject: [PATCH] fix: adjust parametry to require the right name to file uri --- package.json | 4 ++-- .../instruments/javaLspContext.instructions.md | 2 +- resources/skills/java-lsp-tools/SKILL.md | 2 +- src/copilot/tools/javaContextTools.ts | 16 ++++++++++++---- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index aa3c84ce..765970b7 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ { "name": "lsp_java_getFileStructure", "toolReferenceName": "javaFileStructure", - "modelDescription": "Get the structure (classes, methods, fields, inner classes) of a Java file. Returns a hierarchical outline with symbol kinds and line ranges in ~100 tokens.\n\nWhen to use:\n- Before reading a Java file, to understand its layout and find specific line ranges\n- Instead of read_file on the entire file — use this first, then read_file on specific lines\n- When you need to know what methods/fields a class has\n\nWhen NOT to use:\n- For non-Java files (use read_file instead)\n- To search across multiple files (use lsp_java_findSymbol instead)", + "modelDescription": "Get the outline (classes, methods, fields) of a Java file with symbol kinds and line ranges.\n\nUse before read_file to find specific line ranges. For searching across files, use lsp_java_findSymbol instead.\n\nOnly use file paths confirmed from prior tool results or user input. If unsure, call lsp_java_findSymbol first.", "displayName": "Java: Get File Structure", "canBeReferencedInPrompt": true, "icon": "$(symbol-class)", @@ -62,7 +62,7 @@ "properties": { "uri": { "type": "string", - "description": "Java file path (workspace-relative or full URI)" + "description": "Workspace-relative path to a Java file. Must be a known path from prior tool results or user input — do not guess." } }, "required": [ diff --git a/resources/instruments/javaLspContext.instructions.md b/resources/instruments/javaLspContext.instructions.md index be0d015c..2c975a19 100644 --- a/resources/instruments/javaLspContext.instructions.md +++ b/resources/instruments/javaLspContext.instructions.md @@ -10,5 +10,5 @@ For Java files, two compiler-accurate `lsp_java_*` tools are available and retur These are deferred tools. Load them with `tool_search_tool_regex` using pattern `lsp_java_` before first use. -Prefer these over `grep_search`, `search_subagent`, `semantic_search`, `file_search`, or full-file `read_file` when navigating Java symbols. Fall back to `grep_search` if a tool returns empty or errors. +Prefer these over `grep_search`, `search_subagent`, `semantic_search`, `file_search`, or full-file `read_file` when navigating Java symbols. Always use `findSymbol` to discover file paths before passing them to `getFileStructure` — do not guess paths. Fall back to `grep_search` if a tool returns empty or errors. diff --git a/resources/skills/java-lsp-tools/SKILL.md b/resources/skills/java-lsp-tools/SKILL.md index 04936e92..56a74cac 100644 --- a/resources/skills/java-lsp-tools/SKILL.md +++ b/resources/skills/java-lsp-tools/SKILL.md @@ -17,7 +17,7 @@ Search for Java symbol definitions (classes, methods, fields) by name across the ### `lsp_java_getFileStructure` Get hierarchical outline of a Java file (classes, methods, fields) with line ranges. -- Input: `{ uri }` — workspace-relative path or full URI +- Input: `{ uri }` — workspace-relative path. Must be a known path from prior tool results or user input — do not guess - Output: symbol tree with `L start-end` ranges (~100 tokens) - **Use instead of** `read_file` full scan when you need to understand a file's layout diff --git a/src/copilot/tools/javaContextTools.ts b/src/copilot/tools/javaContextTools.ts index d5c584c8..7d8ba034 100644 --- a/src/copilot/tools/javaContextTools.ts +++ b/src/copilot/tools/javaContextTools.ts @@ -20,6 +20,7 @@ * - No classpath resolution, no dependency download */ +import * as path from "path"; import * as vscode from "vscode"; import { Commands } from "../../commands"; import { sendInfo } from "vscode-extension-telemetry-wrapper"; @@ -56,14 +57,14 @@ function resolveFileUri(input: string): vscode.Uri { let uri: vscode.Uri; - // Full URI — only allow the file: scheme - if (/^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(input) || /^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(input)) { + if (input.includes("://")) { + // URI string (e.g. "file:///home/user/project/src/Main.java") uri = vscode.Uri.parse(input); if (uri.scheme !== "file") { throw new Error(`Unsupported URI scheme "${uri.scheme}". Only file: URIs are allowed.`); } - } else if (input.startsWith("/") || /^[a-zA-Z]:[/\\]/.test(input)) { - // Absolute path (Unix or Windows) + } else if (path.isAbsolute(input)) { + // Absolute filesystem path (Unix or Windows) uri = vscode.Uri.file(input); } else { // Relative path — resolve against first workspace folder @@ -99,6 +100,13 @@ const fileStructureTool: vscode.LanguageModelTool = { let errorMessage = ""; try { const uri = resolveFileUri(options.input.uri); + try { + await vscode.workspace.fs.stat(uri); + } catch { + status = "error"; + errorMessage = `File not found: ${vscode.workspace.asRelativePath(uri)}`; + return toResult({ error: errorMessage }); + } const symbols = await vscode.commands.executeCommand( "vscode.executeDocumentSymbolProvider", uri, );