import { defineTool } from "@earendil-works/pi-coding-agent"; import { Type } from "typebox"; import { loadRuntime } from "../config.js"; import { resolveRoot } from "../files.js"; import { runFix, textFromResult, textResult } from "../lsp-runner.js"; import { selectFixRoutes } from "../routes.js"; const STATUS_KEY = "lsp"; export const lspFixTool = defineTool({ name: "lsp_fix", label: "LSP: Fix", description: "Apply source fixes or import organization to one or more files using configured LSP server routes. " + "Files are grouped per server, so each LSP server spawns once for all of its files.", promptSnippet: "Apply configured LSP source fixes to one or more files", promptGuidelines: [ "Use lsp_fix for files handled by a configured LSP code-action server.", "Pass multiple files via paths to fix them in one call; each server spawns once for its files.", "Use kind when the server needs a specific source action kind such as source.organizeImports.", "Batch mode returns per-file summaries only; call with a single path to preview the full fixed text.", ], parameters: Type.Object({ path: Type.Optional( Type.String({ description: "Single file to process. The server is selected from configured file extensions.", }), ), paths: Type.Optional( Type.Array(Type.String(), { description: "Multiple files to process in one call (batch fix). Provide either path or paths, not both.", }), ), root: Type.Optional( Type.String({ description: "Workspace root for language servers. Defaults to cwd.", }), ), write: Type.Optional( Type.Boolean({ description: "Write changed text back to the files. Defaults to false.", }), ), server: Type.Optional( Type.String({ description: "Optional configured LSP server name. Defaults to extension-based inference.", }), ), kind: Type.Optional( Type.String({ description: "Source action kind. Defaults to source.fixAll.", }), ), autoInstall: Type.Optional( Type.Boolean({ description: "Set false to skip best-effort auto-install of a missing LSP server. Defaults to true.", }), ), }), async execute(_toolCallId, params, signal, _onUpdate, ctx) { const requestedRoot = resolveRoot(params.root); const hasPath = params.path !== undefined && params.path.trim().length > 0; const hasPaths = params.paths !== undefined && params.paths.length > 0; if (hasPath === hasPaths) { throw new Error( "Provide exactly one of 'path' (single file) or 'paths' (batch) for lsp_fix.", ); } const paths = hasPath ? [params.path as string] : (params.paths as string[]); const { adapters, timeoutMs } = loadRuntime(); const { root, routes } = selectFixRoutes(adapters, { root: requestedRoot, paths, server: params.server, }); const results: Array<{ route: (typeof routes)[number]; result: Awaited>; }> = []; const routeFailures: Array<{ server: string; message: string }> = []; for (const route of routes) { signal?.throwIfAborted(); try { results.push({ route, result: await runFix( route.adapter, { root, paths: route.files, kind: params.kind, write: params.write, autoInstall: params.autoInstall, }, timeoutMs, signal, ctx, STATUS_KEY, ), }); } catch (error) { // A caller cancel aborts everything; any other route failure is // isolated so earlier routes' results are never lost. if (signal?.aborted) throw error; routeFailures.push({ server: route.adapter.name, message: error instanceof Error ? error.message : String(error), }); } } if (results.length === 1 && routeFailures.length === 0) { return results[0].result; } const sections = [ ...results.map((entry) => textFromResult(entry.result)), ...routeFailures.map( (failure) => `${failure.server} LSP fix failed: ${failure.message}`, ), ]; return textResult(sections.join("\n\n---\n\n"), { root, routes: results.map(({ route, result }) => ({ server: route.adapter.name, files: route.files, details: result.details, })), routeFailures, }); }, });