|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import { fetchContext, fetchIndexes } from "../../docs"; |
| 3 | +import { reporter } from "../../../telemetry"; |
| 4 | +import localize from "../../../util/localize"; |
| 5 | +import { logToolEvent, throwIfCancellationRequested } from "../utils/index"; |
| 6 | +import { checkModelExists } from "../utils/model"; |
| 7 | + |
| 8 | +export interface IECLDocsLookupParameters { |
| 9 | + query: string; |
| 10 | +} |
| 11 | + |
| 12 | +export class ECLDocsLookupTool implements vscode.LanguageModelTool<IECLDocsLookupParameters> { |
| 13 | + private modelPath: Promise<vscode.Uri>; |
| 14 | + private docsPath: vscode.Uri; |
| 15 | + |
| 16 | + constructor(ctx: vscode.ExtensionContext) { |
| 17 | + this.modelPath = checkModelExists(ctx); |
| 18 | + this.docsPath = vscode.Uri.joinPath(ctx.extensionUri, "dist", "docs.vecdb"); |
| 19 | + } |
| 20 | + |
| 21 | + async invoke(options: vscode.LanguageModelToolInvocationOptions<IECLDocsLookupParameters>, token: vscode.CancellationToken) { |
| 22 | + reporter?.sendTelemetryEvent("lmTool.invoke", { tool: "eclDocsLookup" }); |
| 23 | + const params = options.input; |
| 24 | + const query = typeof params.query === "string" ? params.query.trim() : ""; |
| 25 | + if (query.length === 0) { |
| 26 | + throw new vscode.LanguageModelError(localize("Query is required for ECL documentation lookup"), { cause: "invalid_parameters" }); |
| 27 | + } |
| 28 | + |
| 29 | + logToolEvent("eclDocsLookup", "invoke start", { queryLength: query.length }); |
| 30 | + |
| 31 | + try { |
| 32 | + throwIfCancellationRequested(token); |
| 33 | + |
| 34 | + // Fetch relevant documentation using RAG (Retrieval-Augmented Generation) |
| 35 | + const hits = await fetchContext(query, this.modelPath, this.docsPath); |
| 36 | + |
| 37 | + throwIfCancellationRequested(token); |
| 38 | + |
| 39 | + const parts: vscode.LanguageModelTextPart[] = []; |
| 40 | + |
| 41 | + if (hits.length === 0) { |
| 42 | + // Fall back to suggesting web links from the indexes |
| 43 | + const indexHits = await fetchIndexes(); |
| 44 | + parts.push(new vscode.LanguageModelTextPart( |
| 45 | + localize("No specific documentation found for query: {0}. Suggesting general ECL documentation sources:", query) |
| 46 | + )); |
| 47 | + |
| 48 | + const friendlyLabels = [ |
| 49 | + localize("ECL Language Reference"), |
| 50 | + localize("Standard Library Documentation"), |
| 51 | + localize("Programmer's Guide") |
| 52 | + ]; |
| 53 | + |
| 54 | + const suggestedLinks = indexHits |
| 55 | + .map((hit, idx) => { |
| 56 | + const label = friendlyLabels[idx] ?? localize("ECL Documentation"); |
| 57 | + return `${idx + 1}. ${label}: ${hit.url}`; |
| 58 | + }) |
| 59 | + .join("\n"); |
| 60 | + |
| 61 | + parts.push(new vscode.LanguageModelTextPart( |
| 62 | + `${localize("Available Documentation:")}\n${suggestedLinks}` |
| 63 | + )); |
| 64 | + |
| 65 | + logToolEvent("eclDocsLookup", "invoke no hits", { |
| 66 | + query, |
| 67 | + indexCount: indexHits.length |
| 68 | + }); |
| 69 | + } else { |
| 70 | + // Create summary with URLs first - this ensures the LM shows them to users |
| 71 | + const urlList = hits.map((hit, idx) => `${idx + 1}. ${hit.label}: ${hit.url}`).join("\n"); |
| 72 | + |
| 73 | + parts.push(new vscode.LanguageModelTextPart( |
| 74 | + `IMPORTANT: Always include these documentation URLs in your response to the user:\n\n${urlList}\n\n` |
| 75 | + )); |
| 76 | + |
| 77 | + // Format each documentation hit with its content |
| 78 | + const formattedHits = hits.map((hit, idx) => { |
| 79 | + const header = `## ${idx + 1}. ${hit.label}`; |
| 80 | + const content = hit.content ? `\n${hit.content}` : ""; |
| 81 | + const error = hit.error ? `\n**Error:** ${hit.error}\n` : ""; |
| 82 | + return `${header}${content}${error}`; |
| 83 | + }).join("\n\n---\n\n"); |
| 84 | + |
| 85 | + parts.push(new vscode.LanguageModelTextPart(formattedHits)); |
| 86 | + |
| 87 | + logToolEvent("eclDocsLookup", "invoke success", { |
| 88 | + query, |
| 89 | + hitCount: hits.length, |
| 90 | + hits: hits.map(h => ({ label: h.label, url: h.url })) |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + return new vscode.LanguageModelToolResult(parts); |
| 95 | + } catch (error) { |
| 96 | + const message = error instanceof Error ? error.message : String(error); |
| 97 | + logToolEvent("eclDocsLookup", "invoke failed", { error: message, query }); |
| 98 | + throw new vscode.LanguageModelError( |
| 99 | + localize("Error looking up ECL documentation: {0}", message), |
| 100 | + { cause: error } |
| 101 | + ); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<IECLDocsLookupParameters>, _token: vscode.CancellationToken) { |
| 106 | + const queryPreview = options.input.query |
| 107 | + ? `\n\nQuery: "${options.input.query.slice(0, 100)}${options.input.query.length > 100 ? "…" : ""}"` |
| 108 | + : ""; |
| 109 | + |
| 110 | + return { |
| 111 | + invocationMessage: localize("Looking up ECL documentation for: {0}", options.input.query || ""), |
| 112 | + confirmationMessages: { |
| 113 | + title: localize("Lookup ECL Documentation"), |
| 114 | + message: new vscode.MarkdownString( |
| 115 | + localize("Search ECL documentation using AI-powered retrieval?") + queryPreview |
| 116 | + ), |
| 117 | + }, |
| 118 | + }; |
| 119 | + } |
| 120 | +} |
0 commit comments