| 1 | import { IDE } from "core"; |
| 2 | import { |
| 3 | AutocompleteCodeSnippet, |
| 4 | AutocompleteSnippetType, |
| 5 | } from "core/autocomplete/snippets/types"; |
| 6 | import { PosthogFeatureFlag, Telemetry } from "core/util/posthog"; |
| 7 | import { LRUCache } from "lru-cache"; |
| 8 | import * as vscode from "vscode"; |
| 9 | |
| 10 | /** |
| 11 | * Service to keep track of recently visited ranges in files. |
| 12 | */ |
| 13 | export class RecentlyVisitedRangesService { |
| 14 | private cache: LRUCache< |
| 15 | string, |
| 16 | Array<AutocompleteCodeSnippet & { timestamp: number }> |
| 17 | >; |
| 18 | // Default value, we override in initWithPostHog |
| 19 | private numSurroundingLines = 20; |
| 20 | private maxRecentFiles = 3; |
| 21 | private maxSnippetsPerFile = 3; |
| 22 | private isEnabled = true; |
| 23 | |
| 24 | constructor(private readonly ide: IDE) { |
| 25 | this.cache = new LRUCache< |
| 26 | string, |
| 27 | Array<AutocompleteCodeSnippet & { timestamp: number }> |
| 28 | >({ |
| 29 | max: this.maxRecentFiles, |
| 30 | }); |
| 31 | |
| 32 | void this.initWithPostHog(); |
| 33 | } |
| 34 | |
| 35 | private async initWithPostHog() { |
| 36 | const recentlyVisitedRangesNumSurroundingLines = |
| 37 | await Telemetry.getValueForFeatureFlag( |
| 38 | PosthogFeatureFlag.RecentlyVisitedRangesNumSurroundingLines, |
| 39 | ); |
| 40 | |
| 41 | if (recentlyVisitedRangesNumSurroundingLines) { |
| 42 | this.isEnabled = true; |
| 43 | this.numSurroundingLines = recentlyVisitedRangesNumSurroundingLines; |
| 44 | } |
| 45 | |
| 46 | vscode.window.onDidChangeTextEditorSelection( |
| 47 | this.cacheCurrentSelectionContext, |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | private cacheCurrentSelectionContext = async ( |
| 52 | event: vscode.TextEditorSelectionChangeEvent, |
| 53 | ) => { |
| 54 | const filepath = event.textEditor.document.uri.toString(); |
| 55 | const line = event.selections[0].active.line; |
| 56 | const startLine = Math.max(0, line - this.numSurroundingLines); |
| 57 | const endLine = Math.min( |
| 58 | line + this.numSurroundingLines, |
| 59 | event.textEditor.document.lineCount - 1, |
| 60 | ); |
| 61 | |
| 62 | try { |
| 63 | const fileContents = await this.ide.readFile(filepath); |
| 64 | const lines = fileContents.split("\n"); |
| 65 | const relevantLines = lines |
| 66 | .slice(startLine, endLine + 1) |
| 67 | .join("\n") |
| 68 | .trim(); |
| 69 | |
| 70 | const snippet: AutocompleteCodeSnippet & { timestamp: number } = { |
| 71 | filepath, |
| 72 | content: relevantLines, |
| 73 | type: AutocompleteSnippetType.Code, |
| 74 | timestamp: Date.now(), |
| 75 | }; |
| 76 | |
| 77 | const existing = this.cache.get(filepath) || []; |
| 78 | const newSnippets = [...existing, snippet] |
| 79 | .sort((a, b) => b.timestamp - a.timestamp) |
| 80 | .slice(0, this.maxSnippetsPerFile); |
| 81 | |
| 82 | this.cache.set(filepath, newSnippets); |
| 83 | } catch (err) { |
| 84 | console.error( |
| 85 | "Error caching recently visited ranges for autocomplete: ", |
| 86 | err, |
| 87 | ); |
| 88 | return; |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * Returns up to {@link maxSnippetsPerFile} snippets from the {@link maxRecentFiles} most recently visited files. |
| 94 | * Excludes snippets from the currently active file. |
| 95 | * @returns Array of code snippets from recently visited files |
| 96 | */ |
| 97 | public getSnippets(): AutocompleteCodeSnippet[] { |
| 98 | if (!this.isEnabled) { |
| 99 | return []; |
| 100 | } |
| 101 | |
| 102 | const currentFilepath = |
| 103 | vscode.window.activeTextEditor?.document.uri.toString(); |
| 104 | let allSnippets: Array<AutocompleteCodeSnippet & { timestamp: number }> = |
| 105 | []; |
| 106 | |
| 107 | // Get most recent snippets from each file in cache |
| 108 | for (const filepath of Array.from(this.cache.keys())) { |
| 109 | const snippets = (this.cache.get(filepath) || []) |
| 110 | .sort((a, b) => b.timestamp - a.timestamp) |
| 111 | .slice(0, this.maxSnippetsPerFile); |
| 112 | allSnippets = [...allSnippets, ...snippets]; |
| 113 | } |
| 114 | |
| 115 | return allSnippets |
| 116 | .filter( |
| 117 | (s) => |
| 118 | !currentFilepath || |
| 119 | (s.filepath !== currentFilepath && |
| 120 | // Exclude Continue's own output as it makes it super-hard for users to test the autocomplete feature |
| 121 | // while looking at the prompts in the Continue's output |
| 122 | !s.filepath.startsWith( |
| 123 | "output:extension-output-Continue.continue", |
| 124 | )), |
| 125 | ) |
| 126 | .sort((a, b) => b.timestamp - a.timestamp) |
| 127 | .map(({ timestamp, ...snippet }) => snippet); |
| 128 | } |
| 129 | } |