-
Notifications
You must be signed in to change notification settings - Fork 13
feat: Add RAG documentaion tool #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import * as vscode from "vscode"; | ||
| import { fetchContext, fetchIndexes } from "../../docs"; | ||
| import { reporter } from "../../../telemetry"; | ||
| import localize from "../../../util/localize"; | ||
| import { logToolEvent, throwIfCancellationRequested } from "../utils/index"; | ||
| import { checkModelExists } from "../utils/model"; | ||
|
GordonSmith marked this conversation as resolved.
|
||
|
|
||
| export interface IECLDocsLookupParameters { | ||
| query: string; | ||
| } | ||
|
|
||
| export class ECLDocsLookupTool implements vscode.LanguageModelTool<IECLDocsLookupParameters> { | ||
| private modelPath: Promise<vscode.Uri>; | ||
| private docsPath: vscode.Uri; | ||
|
|
||
| constructor(ctx: vscode.ExtensionContext) { | ||
| this.modelPath = checkModelExists(ctx); | ||
| this.docsPath = vscode.Uri.joinPath(ctx.extensionUri, "dist", "docs.vecdb"); | ||
| } | ||
|
|
||
| async invoke(options: vscode.LanguageModelToolInvocationOptions<IECLDocsLookupParameters>, token: vscode.CancellationToken) { | ||
| reporter?.sendTelemetryEvent("lmTool.invoke", { tool: "eclDocsLookup" }); | ||
| const params = options.input; | ||
| const query = typeof params.query === "string" ? params.query.trim() : ""; | ||
| if (query.length === 0) { | ||
| throw new vscode.LanguageModelError(localize("Query is required for ECL documentation lookup"), { cause: "invalid_parameters" }); | ||
| } | ||
|
|
||
| logToolEvent("eclDocsLookup", "invoke start", { queryLength: query.length }); | ||
|
|
||
| try { | ||
| throwIfCancellationRequested(token); | ||
|
|
||
| // Fetch relevant documentation using RAG (Retrieval-Augmented Generation) | ||
| const hits = await fetchContext(query, this.modelPath, this.docsPath); | ||
|
|
||
|
GordonSmith marked this conversation as resolved.
|
||
| throwIfCancellationRequested(token); | ||
|
|
||
| const parts: vscode.LanguageModelTextPart[] = []; | ||
|
|
||
| if (hits.length === 0) { | ||
| // Fall back to suggesting web links from the indexes | ||
| const indexHits = await fetchIndexes(); | ||
| parts.push(new vscode.LanguageModelTextPart( | ||
| localize("No specific documentation found for query: {0}. Suggesting general ECL documentation sources:", query) | ||
| )); | ||
|
|
||
| const friendlyLabels = [ | ||
| localize("ECL Language Reference"), | ||
| localize("Standard Library Documentation"), | ||
| localize("Programmer's Guide") | ||
| ]; | ||
|
|
||
| const suggestedLinks = indexHits | ||
| .map((hit, idx) => { | ||
| const label = friendlyLabels[idx] ?? localize("ECL Documentation"); | ||
| return `${idx + 1}. ${label}: ${hit.url}`; | ||
| }) | ||
| .join("\n"); | ||
|
|
||
| parts.push(new vscode.LanguageModelTextPart( | ||
| `${localize("Available Documentation:")}\n${suggestedLinks}` | ||
| )); | ||
|
GordonSmith marked this conversation as resolved.
|
||
|
|
||
| logToolEvent("eclDocsLookup", "invoke no hits", { | ||
| query, | ||
| indexCount: indexHits.length | ||
| }); | ||
| } else { | ||
| // Create summary with URLs first - this ensures the LM shows them to users | ||
| const urlList = hits.map((hit, idx) => `${idx + 1}. ${hit.label}: ${hit.url}`).join("\n"); | ||
|
|
||
| parts.push(new vscode.LanguageModelTextPart( | ||
| `IMPORTANT: Always include these documentation URLs in your response to the user:\n\n${urlList}\n\n` | ||
| )); | ||
|
GordonSmith marked this conversation as resolved.
|
||
|
|
||
| // Format each documentation hit with its content | ||
| const formattedHits = hits.map((hit, idx) => { | ||
| const header = `## ${idx + 1}. ${hit.label}`; | ||
| const content = hit.content ? `\n${hit.content}` : ""; | ||
| const error = hit.error ? `\n**Error:** ${hit.error}\n` : ""; | ||
| return `${header}${content}${error}`; | ||
| }).join("\n\n---\n\n"); | ||
|
|
||
| parts.push(new vscode.LanguageModelTextPart(formattedHits)); | ||
|
|
||
| logToolEvent("eclDocsLookup", "invoke success", { | ||
| query, | ||
| hitCount: hits.length, | ||
| hits: hits.map(h => ({ label: h.label, url: h.url })) | ||
| }); | ||
| } | ||
|
|
||
| return new vscode.LanguageModelToolResult(parts); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| logToolEvent("eclDocsLookup", "invoke failed", { error: message, query }); | ||
| throw new vscode.LanguageModelError( | ||
| localize("Error looking up ECL documentation: {0}", message), | ||
| { cause: error } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<IECLDocsLookupParameters>, _token: vscode.CancellationToken) { | ||
| const queryPreview = options.input.query | ||
| ? `\n\nQuery: "${options.input.query.slice(0, 100)}${options.input.query.length > 100 ? "…" : ""}"` | ||
| : ""; | ||
|
|
||
| return { | ||
| invocationMessage: localize("Looking up ECL documentation for: {0}", options.input.query || ""), | ||
| confirmationMessages: { | ||
| title: localize("Lookup ECL Documentation"), | ||
| message: new vscode.MarkdownString( | ||
| localize("Search ECL documentation using AI-powered retrieval?") + queryPreview | ||
| ), | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.