| 1 | import { IDE } from "../../index"; |
| 2 | import { findUriInDirs } from "../../util/uri"; |
| 3 | import { ContextRetrievalService } from "../context/ContextRetrievalService"; |
| 4 | import { GetLspDefinitionsFunction } from "../types"; |
| 5 | import { HelperVars } from "../util/HelperVars"; |
| 6 | import { openedFilesLruCache } from "../util/openedFilesLruCache"; |
| 7 | import { getDiffsFromCache } from "./gitDiffCache"; |
| 8 | |
| 9 | import { |
| 10 | AutocompleteClipboardSnippet, |
| 11 | AutocompleteCodeSnippet, |
| 12 | AutocompleteDiffSnippet, |
| 13 | AutocompleteSnippetType, |
| 14 | } from "./types"; |
| 15 | |
| 16 | const IDE_SNIPPETS_ENABLED = false; // ideSnippets is not used, so it's temporarily disabled |
| 17 | |
| 18 | export interface SnippetPayload { |
| 19 | rootPathSnippets: AutocompleteCodeSnippet[]; |
| 20 | importDefinitionSnippets: AutocompleteCodeSnippet[]; |
| 21 | ideSnippets: AutocompleteCodeSnippet[]; |
| 22 | recentlyEditedRangeSnippets: AutocompleteCodeSnippet[]; |
| 23 | recentlyVisitedRangesSnippets: AutocompleteCodeSnippet[]; |
| 24 | diffSnippets: AutocompleteDiffSnippet[]; |
| 25 | clipboardSnippets: AutocompleteClipboardSnippet[]; |
| 26 | recentlyOpenedFileSnippets: AutocompleteCodeSnippet[]; |
| 27 | } |
| 28 | |
| 29 | function racePromise<T>(promise: Promise<T[]>, timeout = 100): Promise<T[]> { |
| 30 | const timeoutPromise = new Promise<T[]>((resolve) => { |
| 31 | setTimeout(() => resolve([]), timeout); |
| 32 | }); |
| 33 | |
| 34 | return Promise.race([promise, timeoutPromise]); |
| 35 | } |
| 36 | |
| 37 | // Some IDEs might have special ways of finding snippets (e.g. JetBrains and VS Code have different "LSP-equivalent" systems, |
| 38 | // or they might separately track recently edited ranges) |
| 39 | async function getIdeSnippets( |
| 40 | helper: HelperVars, |
| 41 | ide: IDE, |
| 42 | getDefinitionsFromLsp: GetLspDefinitionsFunction, |
| 43 | ): Promise<AutocompleteCodeSnippet[]> { |
| 44 | const ideSnippets = await getDefinitionsFromLsp( |
| 45 | helper.input.filepath, |
| 46 | helper.fullPrefix + helper.fullSuffix, |
| 47 | helper.fullPrefix.length, |
| 48 | ide, |
| 49 | helper.lang, |
| 50 | ); |
| 51 | |
| 52 | if (helper.options.onlyMyCode) { |
| 53 | const workspaceDirs = await ide.getWorkspaceDirs(); |
| 54 | |
| 55 | return ideSnippets.filter((snippet) => |
| 56 | workspaceDirs.some( |
| 57 | (dir) => !!findUriInDirs(snippet.filepath, [dir]).foundInDir, |
| 58 | ), |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | return ideSnippets; |
| 63 | } |
| 64 | |
| 65 | function getSnippetsFromRecentlyEditedRanges( |
| 66 | helper: HelperVars, |
| 67 | ): AutocompleteCodeSnippet[] { |
| 68 | if (helper.options.useRecentlyEdited === false) { |
| 69 | return []; |
| 70 | } |
| 71 | |
| 72 | return helper.input.recentlyEditedRanges.map((range) => { |
| 73 | return { |
| 74 | filepath: range.filepath, |
| 75 | content: range.lines.join("\n"), |
| 76 | type: AutocompleteSnippetType.Code, |
| 77 | }; |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | const getClipboardSnippets = async ( |
| 82 | ide: IDE, |
| 83 | ): Promise<AutocompleteClipboardSnippet[]> => { |
| 84 | const content = await ide.getClipboardContent(); |
| 85 | |
| 86 | return [content].map((item) => { |
| 87 | return { |
| 88 | content: item.text, |
| 89 | copiedAt: item.copiedAt, |
| 90 | type: AutocompleteSnippetType.Clipboard, |
| 91 | }; |
| 92 | }); |
| 93 | }; |
| 94 | |
| 95 | const getDiffSnippets = async ( |
| 96 | ide: IDE, |
| 97 | ): Promise<AutocompleteDiffSnippet[]> => { |
| 98 | const diffs = await getDiffsFromCache(ide); |
| 99 | |
| 100 | return diffs.map((item) => { |
| 101 | return { |
| 102 | content: item, |
| 103 | type: AutocompleteSnippetType.Diff, |
| 104 | }; |
| 105 | }); |
| 106 | }; |
| 107 | |
| 108 | const getSnippetsFromRecentlyOpenedFiles = async ( |
| 109 | helper: HelperVars, |
| 110 | ide: IDE, |
| 111 | ): Promise<AutocompleteCodeSnippet[]> => { |
| 112 | if (helper.options.useRecentlyOpened === false) { |
| 113 | return []; |
| 114 | } |
| 115 | |
| 116 | try { |
| 117 | const currentFileUri = `${helper.filepath}`; |
| 118 | |
| 119 | // Get all file URIs excluding the current file |
| 120 | const fileUrisToRead = [...openedFilesLruCache.entriesDescending()] |
| 121 | .filter(([fileUri, _]) => fileUri !== currentFileUri) |
| 122 | .map(([fileUri, _]) => fileUri); |
| 123 | |
| 124 | // Create an array of promises that each read a file with timeout |
| 125 | const fileReadPromises = fileUrisToRead.map((fileUri) => { |
| 126 | // Create a promise that resolves to a snippet or null |
| 127 | const readPromise = new Promise<AutocompleteCodeSnippet | null>( |
| 128 | (resolve) => { |
| 129 | ide |
| 130 | .readFile(fileUri) |
| 131 | .then((fileContent) => { |
| 132 | if (!fileContent || fileContent.trim() === "") { |
| 133 | resolve(null); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | resolve({ |
| 138 | filepath: fileUri, |
| 139 | content: fileContent, |
| 140 | type: AutocompleteSnippetType.Code, |
| 141 | }); |
| 142 | }) |
| 143 | .catch((e) => { |
| 144 | console.error(`Failed to read file ${fileUri}:`, e); |
| 145 | resolve(null); |
| 146 | }); |
| 147 | }, |
| 148 | ); |
| 149 | // Cut off at 80ms via racing promises |
| 150 | return Promise.race([ |
| 151 | readPromise, |
| 152 | new Promise<null>((resolve) => setTimeout(() => resolve(null), 80)), |
| 153 | ]); |
| 154 | }); |
| 155 | |
| 156 | // Execute all file reads in parallel |
| 157 | const results = await Promise.all(fileReadPromises); |
| 158 | |
| 159 | // Filter out null results |
| 160 | return results.filter(Boolean) as AutocompleteCodeSnippet[]; |
| 161 | } catch (e) { |
| 162 | console.error("Error processing opened files cache:", e); |
| 163 | return []; |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | export const getAllSnippets = async ({ |
| 168 | helper, |
| 169 | ide, |
| 170 | getDefinitionsFromLsp, |
| 171 | contextRetrievalService, |
| 172 | }: { |
| 173 | helper: HelperVars; |
| 174 | ide: IDE; |
| 175 | getDefinitionsFromLsp: GetLspDefinitionsFunction; |
| 176 | contextRetrievalService: ContextRetrievalService; |
| 177 | }): Promise<SnippetPayload> => { |
| 178 | const recentlyEditedRangeSnippets = |
| 179 | getSnippetsFromRecentlyEditedRanges(helper); |
| 180 | |
| 181 | const [ |
| 182 | rootPathSnippets, |
| 183 | importDefinitionSnippets, |
| 184 | ideSnippets, |
| 185 | diffSnippets, |
| 186 | clipboardSnippets, |
| 187 | recentlyOpenedFileSnippets, |
| 188 | ] = await Promise.all([ |
| 189 | racePromise(contextRetrievalService.getRootPathSnippets(helper)), |
| 190 | racePromise( |
| 191 | contextRetrievalService.getSnippetsFromImportDefinitions(helper), |
| 192 | ), |
| 193 | IDE_SNIPPETS_ENABLED |
| 194 | ? racePromise(getIdeSnippets(helper, ide, getDefinitionsFromLsp)) |
| 195 | : [], |
| 196 | [], // racePromise(getDiffSnippets(ide)) // temporarily disabled, see https://github.com/continuedev/continue/pull/5882, |
| 197 | racePromise(getClipboardSnippets(ide)), |
| 198 | racePromise(getSnippetsFromRecentlyOpenedFiles(helper, ide)), // giving this one a little more time to complete |
| 199 | ]); |
| 200 | |
| 201 | return { |
| 202 | rootPathSnippets, |
| 203 | importDefinitionSnippets, |
| 204 | ideSnippets, |
| 205 | recentlyEditedRangeSnippets, |
| 206 | diffSnippets, |
| 207 | clipboardSnippets, |
| 208 | recentlyVisitedRangesSnippets: helper.input.recentlyVisitedRanges, |
| 209 | recentlyOpenedFileSnippets, |
| 210 | }; |
| 211 | }; |
| 212 | |
| 213 | export const getAllSnippetsWithoutRace = async ({ |
| 214 | helper, |
| 215 | ide, |
| 216 | getDefinitionsFromLsp, |
| 217 | contextRetrievalService, |
| 218 | }: { |
| 219 | helper: HelperVars; |
| 220 | ide: IDE; |
| 221 | getDefinitionsFromLsp: GetLspDefinitionsFunction; |
| 222 | contextRetrievalService: ContextRetrievalService; |
| 223 | }): Promise<SnippetPayload> => { |
| 224 | const recentlyEditedRangeSnippets = |
| 225 | getSnippetsFromRecentlyEditedRanges(helper); |
| 226 | |
| 227 | const [ |
| 228 | rootPathSnippets, |
| 229 | importDefinitionSnippets, |
| 230 | ideSnippets, |
| 231 | diffSnippets, |
| 232 | clipboardSnippets, |
| 233 | recentlyOpenedFileSnippets, |
| 234 | ] = await Promise.all([ |
| 235 | contextRetrievalService.getRootPathSnippets(helper), |
| 236 | contextRetrievalService.getSnippetsFromImportDefinitions(helper), |
| 237 | IDE_SNIPPETS_ENABLED |
| 238 | ? getIdeSnippets(helper, ide, getDefinitionsFromLsp) |
| 239 | : [], |
| 240 | [], // racePromise(getDiffSnippets(ide)) // temporarily disabled, see https://github.com/continuedev/continue/pull/5882, |
| 241 | getClipboardSnippets(ide), |
| 242 | getSnippetsFromRecentlyOpenedFiles(helper, ide), |
| 243 | ]); |
| 244 | |
| 245 | return { |
| 246 | rootPathSnippets, |
| 247 | importDefinitionSnippets, |
| 248 | ideSnippets, |
| 249 | recentlyEditedRangeSnippets, |
| 250 | diffSnippets, |
| 251 | clipboardSnippets, |
| 252 | recentlyVisitedRangesSnippets: helper.input.recentlyVisitedRanges, |
| 253 | recentlyOpenedFileSnippets, |
| 254 | }; |
| 255 | }; |