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