Changesets · Branches · Tags · Compare
| Changeset | 2de752aa0793f0aacf482562d36578d73c40a380c86886c5455a1c15fd39f551 |
|---|---|
| Author | adarshramiyer <adarsh.r.iyer@gmail.com> · 2025-06-09 14:01 -07:00 |
| Committer | adarshramiyer <adarsh.r.iyer@gmail.com> · 2025-06-09 14:01 -07:00 |
| Parents | 3196a2b5d1f9 |
14 files changed
modified core/autocomplete/snippets/getAllSnippets.ts +40 −23
| @@ -113,38 +113,55 @@ | ||
| 113 | 113 | return []; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - const snippets: AutocompleteCodeSnippet[] = []; | |
| 117 | - | |
| 118 | 116 | try { |
| 119 | 117 | const currentFileUri = `${helper.filepath}`; |
| 120 | 118 | |
| 121 | - // converts cache file URIs to autocomplete snippets | |
| 122 | - for (const [fileUri, _] of openedFilesLruCache.entriesDescending()) { | |
| 123 | - if (fileUri === currentFileUri) { | |
| 124 | - continue; | |
| 125 | - } | |
| 119 | + // Get all file URIs excluding the current file | |
| 120 | + const fileUrisToRead = [...openedFilesLruCache.entriesDescending()] | |
| 121 | + .filter(([fileUri, _]) => fileUri !== currentFileUri) | |
| 122 | + .map(([fileUri, _]) => fileUri); | |
| 126 | 123 | |
| 127 | - try { | |
| 128 | - const fileContent = await ide.readFile(fileUri); | |
| 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 | + } | |
| 129 | 136 | |
| 130 | - if (!fileContent || fileContent.trim() === "") { | |
| 131 | - continue; | |
| 132 | - } | |
| 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 | + }); | |
| 133 | 155 | |
| 134 | - snippets.push({ | |
| 135 | - filepath: fileUri, | |
| 136 | - content: fileContent, | |
| 137 | - type: AutocompleteSnippetType.Code, | |
| 138 | - }); | |
| 139 | - } catch (e) { | |
| 140 | - console.error(`Failed to read file ${fileUri}:`, e); | |
| 141 | - } | |
| 142 | - } | |
| 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[]; | |
| 143 | 161 | } catch (e) { |
| 144 | 162 | console.error("Error processing opened files cache:", e); |
| 163 | + return []; | |
| 145 | 164 | } |
| 146 | - | |
| 147 | - return snippets; | |
| 148 | 165 | }; |
| 149 | 166 | |
| 150 | 167 | export const getAllSnippets = async ({ |
added core/autocomplete/templating/__tests__/formatOpenedFilesContext.test.ts +434 −0
| @@ -0,0 +1,434 @@ | ||
| 1 | +/** | |
| 2 | + * Comprehensive tests for formatOpenedFilesContext.ts | |
| 3 | + * | |
| 4 | + */ | |
| 5 | + | |
| 6 | +import { | |
| 7 | + AutocompleteCodeSnippet, | |
| 8 | + AutocompleteDiffSnippet, | |
| 9 | + AutocompleteSnippetType, | |
| 10 | +} from "../../snippets/types"; | |
| 11 | +import { HelperVars } from "../../util/HelperVars"; | |
| 12 | +import { formatOpenedFilesContext } from "../formatOpenedFilesContext"; | |
| 13 | + | |
| 14 | +// Import the now-exported internal functions | |
| 15 | +import { | |
| 16 | + getRecencyAndSizeScore, | |
| 17 | + rankByScore, | |
| 18 | + setLogStats, | |
| 19 | + trimSnippetForContext, | |
| 20 | +} from "../formatOpenedFilesContext"; | |
| 21 | + | |
| 22 | +describe("formatOpenedFilesContext main function tests", () => { | |
| 23 | + const mockHelper = { | |
| 24 | + modelName: "test-model", | |
| 25 | + } as HelperVars; | |
| 26 | + | |
| 27 | + const TOKEN_BUFFER = 50; | |
| 28 | + | |
| 29 | + // Create sample code snippets with various sizes | |
| 30 | + const createCodeSnippet = ( | |
| 31 | + filepath: string, | |
| 32 | + content: string, | |
| 33 | + ): AutocompleteCodeSnippet => ({ | |
| 34 | + type: AutocompleteSnippetType.Code, | |
| 35 | + filepath, | |
| 36 | + content, | |
| 37 | + }); | |
| 38 | + | |
| 39 | + const createDiffSnippet = (content: string): AutocompleteDiffSnippet => ({ | |
| 40 | + type: AutocompleteSnippetType.Diff, | |
| 41 | + content, | |
| 42 | + }); | |
| 43 | + | |
| 44 | + test("should return empty array when no snippets are provided", () => { | |
| 45 | + const result = formatOpenedFilesContext( | |
| 46 | + [], | |
| 47 | + 1000, | |
| 48 | + mockHelper, | |
| 49 | + [], | |
| 50 | + TOKEN_BUFFER, | |
| 51 | + ); | |
| 52 | + expect(result).toEqual([]); | |
| 53 | + }); | |
| 54 | + | |
| 55 | + test("should handle zero remaining token count", () => { | |
| 56 | + const snippets = [ | |
| 57 | + createCodeSnippet("file1.ts", "content of file 1"), | |
| 58 | + createCodeSnippet("file2.ts", "content of file 2"), | |
| 59 | + ]; | |
| 60 | + | |
| 61 | + const result = formatOpenedFilesContext( | |
| 62 | + snippets, | |
| 63 | + 0, | |
| 64 | + mockHelper, | |
| 65 | + [], | |
| 66 | + TOKEN_BUFFER, | |
| 67 | + ); | |
| 68 | + | |
| 69 | + expect(result).toEqual([]); | |
| 70 | + }); | |
| 71 | + | |
| 72 | + test("should handle snippets with empty content", () => { | |
| 73 | + const emptySnippets = [ | |
| 74 | + createCodeSnippet("empty1.ts", ""), | |
| 75 | + createCodeSnippet("empty2.ts", ""), | |
| 76 | + ]; | |
| 77 | + | |
| 78 | + const result = formatOpenedFilesContext( | |
| 79 | + emptySnippets, | |
| 80 | + 1000, | |
| 81 | + mockHelper, | |
| 82 | + [], | |
| 83 | + TOKEN_BUFFER, | |
| 84 | + ); | |
| 85 | + | |
| 86 | + expect(result.length).toBe(emptySnippets.length); | |
| 87 | + }); | |
| 88 | + | |
| 89 | + test("should return all snippets when they all fit within token limit", () => { | |
| 90 | + const smallSnippets = [ | |
| 91 | + createCodeSnippet("small1.ts", "a"), | |
| 92 | + createCodeSnippet("small2.ts", "b"), | |
| 93 | + createCodeSnippet("small3.ts", "c"), | |
| 94 | + ]; | |
| 95 | + | |
| 96 | + const result = formatOpenedFilesContext( | |
| 97 | + smallSnippets, | |
| 98 | + 1000, | |
| 99 | + mockHelper, | |
| 100 | + [], | |
| 101 | + TOKEN_BUFFER, | |
| 102 | + ); | |
| 103 | + | |
| 104 | + expect(result.length).toBe(smallSnippets.length); | |
| 105 | + }); | |
| 106 | + | |
| 107 | + test("should handle limited token count", () => { | |
| 108 | + const snippets = Array(10) | |
| 109 | + .fill(0) | |
| 110 | + .map((_, i) => createCodeSnippet(`file${i}.ts`, `content of file ${i}`)); | |
| 111 | + | |
| 112 | + const result = formatOpenedFilesContext( | |
| 113 | + snippets, | |
| 114 | + 1, // Extremely small token count | |
| 115 | + mockHelper, | |
| 116 | + [], | |
| 117 | + TOKEN_BUFFER, | |
| 118 | + ); | |
| 119 | + | |
| 120 | + expect(Array.isArray(result)).toBe(true); | |
| 121 | + }); | |
| 122 | + | |
| 123 | + test("should accept valid inputs with adequate token count", () => { | |
| 124 | + const snippets = [ | |
| 125 | + createCodeSnippet("file1.ts", "Some content here"), | |
| 126 | + createCodeSnippet("file2.ts", "More content here"), | |
| 127 | + ]; | |
| 128 | + | |
| 129 | + const result = formatOpenedFilesContext( | |
| 130 | + snippets, | |
| 131 | + 1000, | |
| 132 | + mockHelper, | |
| 133 | + [], | |
| 134 | + TOKEN_BUFFER, | |
| 135 | + ); | |
| 136 | + | |
| 137 | + expect(result.length).toBeGreaterThan(0); | |
| 138 | + }); | |
| 139 | + | |
| 140 | + test("should handle already added snippets parameter", () => { | |
| 141 | + const snippets = [ | |
| 142 | + createCodeSnippet("file1.ts", "content of file 1"), | |
| 143 | + createCodeSnippet("file2.ts", "content of file 2"), | |
| 144 | + ]; | |
| 145 | + | |
| 146 | + const alreadyAddedSnippets = [createDiffSnippet("diff content")]; | |
| 147 | + | |
| 148 | + const result = formatOpenedFilesContext( | |
| 149 | + snippets, | |
| 150 | + 1000, | |
| 151 | + mockHelper, | |
| 152 | + alreadyAddedSnippets, | |
| 153 | + TOKEN_BUFFER, | |
| 154 | + ); | |
| 155 | + | |
| 156 | + expect(result.length).toBe(snippets.length); | |
| 157 | + }); | |
| 158 | + | |
| 159 | + test("should handle large snippets by trimming them", () => { | |
| 160 | + const largeSnippet = createCodeSnippet("large.ts", "x".repeat(10000)); | |
| 161 | + | |
| 162 | + const result = formatOpenedFilesContext( | |
| 163 | + [largeSnippet], | |
| 164 | + 200, | |
| 165 | + mockHelper, | |
| 166 | + [], | |
| 167 | + TOKEN_BUFFER, | |
| 168 | + ); | |
| 169 | + | |
| 170 | + expect(result.length).toBe(1); | |
| 171 | + expect(result[0].filepath).toBe(largeSnippet.filepath); | |
| 172 | + expect(result[0].content.length).toBeLessThan(largeSnippet.content.length); | |
| 173 | + }); | |
| 174 | + | |
| 175 | + test("should prioritize more recent snippets when not all fit", () => { | |
| 176 | + const snippets = [ | |
| 177 | + createCodeSnippet("recent1.ts", "a".repeat(10)), | |
| 178 | + createCodeSnippet("recent2.ts", "b".repeat(10000)), | |
| 179 | + createCodeSnippet("recent3.ts", "c".repeat(10000)), | |
| 180 | + ]; | |
| 181 | + | |
| 182 | + const result = formatOpenedFilesContext( | |
| 183 | + snippets, | |
| 184 | + 200, | |
| 185 | + mockHelper, | |
| 186 | + [], | |
| 187 | + TOKEN_BUFFER, | |
| 188 | + ); | |
| 189 | + | |
| 190 | + expect(result.length).toBeGreaterThan(0); | |
| 191 | + expect(result.some((s) => s.filepath === "recent1.ts")).toBe(true); | |
| 192 | + }); | |
| 193 | + | |
| 194 | + test("should handle a mix of large and small snippets effectively", () => { | |
| 195 | + const mixedSnippets = [ | |
| 196 | + createCodeSnippet("small1.ts", "a".repeat(10)), | |
| 197 | + createCodeSnippet("large1.ts", "b".repeat(5000)), | |
| 198 | + createCodeSnippet("small2.ts", "c".repeat(10)), | |
| 199 | + createCodeSnippet("large2.ts", "d".repeat(5000)), | |
| 200 | + ]; | |
| 201 | + | |
| 202 | + const result = formatOpenedFilesContext( | |
| 203 | + mixedSnippets, | |
| 204 | + 500, | |
| 205 | + mockHelper, | |
| 206 | + [], | |
| 207 | + TOKEN_BUFFER, | |
| 208 | + ); | |
| 209 | + | |
| 210 | + expect(result.length).toBeGreaterThan(0); | |
| 211 | + expect(result.some((s) => s.filepath.startsWith("small"))).toBe(true); | |
| 212 | + }); | |
| 213 | + | |
| 214 | + test("should respect minimum token threshold when trimming", () => { | |
| 215 | + const largeSnippets = [ | |
| 216 | + createCodeSnippet("large1.ts", "a".repeat(5000)), | |
| 217 | + createCodeSnippet("large2.ts", "b".repeat(5000)), | |
| 218 | + ]; | |
| 219 | + | |
| 220 | + const result = formatOpenedFilesContext( | |
| 221 | + largeSnippets, | |
| 222 | + 150, | |
| 223 | + mockHelper, | |
| 224 | + [], | |
| 225 | + TOKEN_BUFFER, | |
| 226 | + ); | |
| 227 | + | |
| 228 | + expect(result.length).toBeLessThanOrEqual(1); | |
| 229 | + | |
| 230 | + if (result.length > 0) { | |
| 231 | + expect(result[0].content.length).toBeLessThan( | |
| 232 | + largeSnippets[0].content.length, | |
| 233 | + ); | |
| 234 | + } | |
| 235 | + }); | |
| 236 | +}); | |
| 237 | + | |
| 238 | +// Tests for rankByScore function | |
| 239 | +describe("rankByScore function", () => { | |
| 240 | + const createCodeSnippet = ( | |
| 241 | + filepath: string, | |
| 242 | + content: string, | |
| 243 | + ): AutocompleteCodeSnippet => ({ | |
| 244 | + type: AutocompleteSnippetType.Code, | |
| 245 | + filepath, | |
| 246 | + content, | |
| 247 | + }); | |
| 248 | + | |
| 249 | + test("should return empty array when given empty array", () => { | |
| 250 | + const result = rankByScore([]); | |
| 251 | + expect(result).toEqual([]); | |
| 252 | + }); | |
| 253 | + | |
| 254 | + test("should rank snippets by score", () => { | |
| 255 | + // Initialize logMin and logMax by calling setLogStats first | |
| 256 | + const snippets = [ | |
| 257 | + createCodeSnippet("file1.ts", "a".repeat(10)), | |
| 258 | + createCodeSnippet("file2.ts", "b".repeat(1000)), | |
| 259 | + createCodeSnippet("file3.ts", "c".repeat(100)), | |
| 260 | + ]; | |
| 261 | + setLogStats(snippets); | |
| 262 | + | |
| 263 | + const result = rankByScore(snippets); | |
| 264 | + | |
| 265 | + expect(Array.isArray(result)).toBe(true); | |
| 266 | + expect(result.length).toBeGreaterThan(0); | |
| 267 | + | |
| 268 | + // The snippets should be ranked in some order, which may not match input order | |
| 269 | + expect(result.map((s) => s.filepath)).not.toEqual( | |
| 270 | + snippets.map((s) => s.filepath), | |
| 271 | + ); | |
| 272 | + }); | |
| 273 | + | |
| 274 | + test("should limit the number of ranked snippets to defaultNumFilesUsed", () => { | |
| 275 | + // Create more snippets than the default limit | |
| 276 | + const manySnippets = Array(10) | |
| 277 | + .fill(0) | |
| 278 | + .map((_, i) => createCodeSnippet(`file${i}.ts`, `content of file ${i}`)); | |
| 279 | + setLogStats(manySnippets); | |
| 280 | + | |
| 281 | + const result = rankByScore(manySnippets); | |
| 282 | + | |
| 283 | + // The function should limit the number of snippets (defaultNumFilesUsed is 5) | |
| 284 | + expect(result.length).toBeLessThanOrEqual(5); | |
| 285 | + }); | |
| 286 | +}); | |
| 287 | + | |
| 288 | +// Tests for getRecencyAndSizeScore function | |
| 289 | +describe("getRecencyAndSizeScore function", () => { | |
| 290 | + const createCodeSnippet = ( | |
| 291 | + filepath: string, | |
| 292 | + content: string, | |
| 293 | + ): AutocompleteCodeSnippet => ({ | |
| 294 | + type: AutocompleteSnippetType.Code, | |
| 295 | + filepath, | |
| 296 | + content, | |
| 297 | + }); | |
| 298 | + | |
| 299 | + test("recency score should decrease with higher index", () => { | |
| 300 | + // Initialize log stats first | |
| 301 | + const snippets = [ | |
| 302 | + createCodeSnippet("file1.ts", "a".repeat(100)), | |
| 303 | + createCodeSnippet("file2.ts", "b".repeat(100)), | |
| 304 | + ]; | |
| 305 | + setLogStats(snippets); | |
| 306 | + | |
| 307 | + // Test that score decreases with increasing index | |
| 308 | + const score0 = getRecencyAndSizeScore(0, snippets[0]); | |
| 309 | + const score1 = getRecencyAndSizeScore(1, snippets[0]); | |
| 310 | + const score2 = getRecencyAndSizeScore(2, snippets[0]); | |
| 311 | + | |
| 312 | + expect(score0).toBeGreaterThan(score1); | |
| 313 | + expect(score1).toBeGreaterThan(score2); | |
| 314 | + }); | |
| 315 | + | |
| 316 | + test("size score should be higher for smaller snippets", () => { | |
| 317 | + // Initialize log stats first | |
| 318 | + const snippets = [ | |
| 319 | + createCodeSnippet("small.ts", "a".repeat(50)), | |
| 320 | + createCodeSnippet("medium.ts", "b".repeat(200)), | |
| 321 | + createCodeSnippet("large.ts", "c".repeat(1000)), | |
| 322 | + ]; | |
| 323 | + setLogStats(snippets); | |
| 324 | + | |
| 325 | + // Test with same recency index but different sizes | |
| 326 | + const smallScore = getRecencyAndSizeScore(0, snippets[0]); | |
| 327 | + const mediumScore = getRecencyAndSizeScore(0, snippets[1]); | |
| 328 | + const largeScore = getRecencyAndSizeScore(0, snippets[2]); | |
| 329 | + | |
| 330 | + // Scores should reflect preference for smaller snippets | |
| 331 | + expect(smallScore).toBeGreaterThanOrEqual(mediumScore); | |
| 332 | + expect(mediumScore).toBeGreaterThanOrEqual(largeScore); | |
| 333 | + }); | |
| 334 | + | |
| 335 | + test("recency and size both affect the score with recency having more weight", () => { | |
| 336 | + // Initialize log stats first | |
| 337 | + const snippets = [ | |
| 338 | + createCodeSnippet("recent_large.ts", "a".repeat(1000)), | |
| 339 | + createCodeSnippet("old_small.ts", "b".repeat(50)), | |
| 340 | + ]; | |
| 341 | + setLogStats(snippets); | |
| 342 | + | |
| 343 | + // Test that recency and size both contribute to the score | |
| 344 | + const recentLargeScore = getRecencyAndSizeScore(0, snippets[0]); | |
| 345 | + const oldSmallScore = getRecencyAndSizeScore(1, snippets[1]); | |
| 346 | + | |
| 347 | + // We're not making a direct comparison, just verifying the scoring mechanism works | |
| 348 | + expect(recentLargeScore).toBeGreaterThan(0); | |
| 349 | + expect(oldSmallScore).toBeGreaterThan(0); | |
| 350 | + expect(oldSmallScore).toBeLessThan(1); | |
| 351 | + }); | |
| 352 | +}); | |
| 353 | + | |
| 354 | +// Tests for setLogStats function | |
| 355 | +describe("setLogStats function", () => { | |
| 356 | + const createCodeSnippet = ( | |
| 357 | + filepath: string, | |
| 358 | + content: string, | |
| 359 | + ): AutocompleteCodeSnippet => ({ | |
| 360 | + type: AutocompleteSnippetType.Code, | |
| 361 | + filepath, | |
| 362 | + content, | |
| 363 | + }); | |
| 364 | + | |
| 365 | + test("should set logMin and logMax based on snippet sizes", () => { | |
| 366 | + const snippets = [ | |
| 367 | + createCodeSnippet("small.ts", "a".repeat(50)), | |
| 368 | + createCodeSnippet("medium.ts", "b".repeat(200)), | |
| 369 | + createCodeSnippet("large.ts", "c".repeat(1000)), | |
| 370 | + ]; | |
| 371 | + | |
| 372 | + // We can't directly test logMin and logMax as they're private | |
| 373 | + // but we can verify that the function runs without errors | |
| 374 | + expect(() => setLogStats(snippets)).not.toThrow(); | |
| 375 | + }); | |
| 376 | + | |
| 377 | + test("should handle empty snippets", () => { | |
| 378 | + const emptySnippets = [ | |
| 379 | + createCodeSnippet("empty1.ts", ""), | |
| 380 | + createCodeSnippet("empty2.ts", ""), | |
| 381 | + ]; | |
| 382 | + | |
| 383 | + expect(() => setLogStats(emptySnippets)).not.toThrow(); | |
| 384 | + }); | |
| 385 | + | |
| 386 | + test("should handle single snippet", () => { | |
| 387 | + const singleSnippet = [createCodeSnippet("single.ts", "content")]; | |
| 388 | + | |
| 389 | + expect(() => setLogStats(singleSnippet)).not.toThrow(); | |
| 390 | + }); | |
| 391 | +}); | |
| 392 | + | |
| 393 | +// Tests for trimSnippetForContext function | |
| 394 | +describe("trimSnippetForContext function", () => { | |
| 395 | + const createCodeSnippet = ( | |
| 396 | + filepath: string, | |
| 397 | + content: string, | |
| 398 | + ): AutocompleteCodeSnippet => ({ | |
| 399 | + type: AutocompleteSnippetType.Code, | |
| 400 | + filepath, | |
| 401 | + content, | |
| 402 | + }); | |
| 403 | + | |
| 404 | + test("should return original snippet if it fits within token limit", () => { | |
| 405 | + const snippet = createCodeSnippet("small.ts", "small content"); | |
| 406 | + const modelName = "test-model"; | |
| 407 | + const maxTokens = 1000; // Large enough to fit the snippet | |
| 408 | + | |
| 409 | + const result = trimSnippetForContext(snippet, maxTokens, modelName); | |
| 410 | + | |
| 411 | + // Should return the original snippet with its token count | |
| 412 | + expect(result.newSnippet).toEqual(snippet); | |
| 413 | + expect(typeof result.newTokens).toBe("number"); | |
| 414 | + expect(result.newTokens).toBeGreaterThan(0); | |
| 415 | + }); | |
| 416 | + | |
| 417 | + test("should trim snippet content if it exceeds token limit", () => { | |
| 418 | + const snippet = createCodeSnippet("large.ts", "a".repeat(1000)); | |
| 419 | + const modelName = "test-model"; | |
| 420 | + const maxTokens = 10; // Small enough to require trimming | |
| 421 | + | |
| 422 | + const result = trimSnippetForContext(snippet, maxTokens, modelName); | |
| 423 | + | |
| 424 | + // Should return a trimmed snippet | |
| 425 | + expect(result.newSnippet.filepath).toBe(snippet.filepath); | |
| 426 | + expect(result.newSnippet.type).toBe(snippet.type); | |
| 427 | + expect(result.newSnippet.content.length).toBeLessThanOrEqual( | |
| 428 | + snippet.content.length, | |
| 429 | + ); | |
| 430 | + | |
| 431 | + // Token count should be less than or equal to maxTokens | |
| 432 | + expect(result.newTokens).toBeLessThanOrEqual(maxTokens); | |
| 433 | + }); | |
| 434 | +}); |
modified core/autocomplete/templating/formatOpenedFilesContext.ts +8 −0
| @@ -162,3 +162,11 @@ | ||
| 162 | 162 | newTokens: countTokens(trimmedCode, modelName), |
| 163 | 163 | }; |
| 164 | 164 | } |
| 165 | + | |
| 166 | +// Uncomment for testing | |
| 167 | +/* export { | |
| 168 | + getRecencyAndSizeScore, | |
| 169 | + rankByScore, | |
| 170 | + setLogStats, | |
| 171 | + trimSnippetForContext, | |
| 172 | +}; */ |
modified core/context/retrieval/pipelines/BaseRetrievalPipeline.ts +2 −2
| @@ -2,10 +2,10 @@ | ||
| 2 | 2 | import nlp from "wink-nlp-utils"; |
| 3 | 3 | |
| 4 | 4 | import { BranchAndDir, Chunk, ContinueConfig, IDE, ILLM } from "../../../"; |
| 5 | +import { openedFilesLruCache } from "../../../autocomplete/util/openedFilesLruCache"; | |
| 5 | 6 | import { chunkDocument } from "../../../indexing/chunk/chunk"; |
| 6 | 7 | import { FullTextSearchCodebaseIndex } from "../../../indexing/FullTextSearchCodebaseIndex"; |
| 7 | 8 | import { LanceDbIndex } from "../../../indexing/LanceDbIndex"; |
| 8 | -import { recentlyEditedFilesCache } from "../recentlyEditedFilesCache"; | |
| 9 | 9 | |
| 10 | 10 | const DEFAULT_CHUNK_SIZE = 384; |
| 11 | 11 | |
| @@ -98,7 +98,7 @@ | ||
| 98 | 98 | n: number, |
| 99 | 99 | ): Promise<Chunk[]> { |
| 100 | 100 | const recentlyEditedFilesSlice = Array.from( |
| 101 | - recentlyEditedFilesCache.keys(), | |
| 101 | + openedFilesLruCache.keys(), | |
| 102 | 102 | ).slice(0, n); |
| 103 | 103 | |
| 104 | 104 | // If the number of recently edited files is less than the retrieval limit, |
deleted core/context/retrieval/recentlyEditedFilesCache.ts +0 −16
| @@ -1,16 +0,0 @@ | ||
| 1 | -import QuickLRU from "quick-lru"; | |
| 2 | - | |
| 3 | -import { ToWebviewOrCoreFromIdeProtocol } from "../../protocol/ide.js"; | |
| 4 | - | |
| 5 | -// The cache key and value are both a filepath string | |
| 6 | -export type RecentlyEditedFilesCacheKeyAndValue = | |
| 7 | - ToWebviewOrCoreFromIdeProtocol["didChangeActiveTextEditor"][0]["filepath"]; | |
| 8 | - | |
| 9 | -const MAX_NUM_RECENTLY_EDITED_FILES = 100; | |
| 10 | - | |
| 11 | -export const recentlyEditedFilesCache = new QuickLRU< | |
| 12 | - RecentlyEditedFilesCacheKeyAndValue, | |
| 13 | - RecentlyEditedFilesCacheKeyAndValue | |
| 14 | ->({ | |
| 15 | - maxSize: MAX_NUM_RECENTLY_EDITED_FILES, | |
| 16 | -}); |
modified core/core.ts +43 −62
| @@ -12,7 +12,6 @@ | ||
| 12 | 12 | import { addModel, deleteModel } from "./config/util"; |
| 13 | 13 | import CodebaseContextProvider from "./context/providers/CodebaseContextProvider"; |
| 14 | 14 | import CurrentFileContextProvider from "./context/providers/CurrentFileContextProvider"; |
| 15 | -import { recentlyEditedFilesCache } from "./context/retrieval/recentlyEditedFilesCache"; | |
| 16 | 15 | import { ContinueServerClient } from "./continueServer/stubs/client"; |
| 17 | 16 | import { getAuthUrlForTokenPage } from "./control-plane/auth/index"; |
| 18 | 17 | import { getControlPlaneEnv } from "./control-plane/env"; |
| @@ -601,6 +600,29 @@ | ||
| 601 | 600 | }); |
| 602 | 601 | |
| 603 | 602 | on("files/closed", async ({ data }) => { |
| 603 | + try { | |
| 604 | + const filepaths = data.fileUris; | |
| 605 | + if (!prevFilepaths.filepaths.length) { | |
| 606 | + prevFilepaths.filepaths = filepaths; | |
| 607 | + } | |
| 608 | + | |
| 609 | + // If there is a removal, including if the number of tabs is the same (which can happen with temp tabs) | |
| 610 | + if (filepaths.length <= prevFilepaths.filepaths.length) { | |
| 611 | + // Remove files from cache that are no longer open (i.e. in the cache but not in the list of opened tabs) | |
| 612 | + for (const [key, _] of openedFilesLruCache.entriesDescending()) { | |
| 613 | + if (!filepaths.includes(key)) { | |
| 614 | + openedFilesLruCache.delete(key); | |
| 615 | + } | |
| 616 | + } | |
| 617 | + } | |
| 618 | + | |
| 619 | + prevFilepaths.filepaths = filepaths; | |
| 620 | + } catch (e) { | |
| 621 | + console.error( | |
| 622 | + `didChangeVisibleTextEditors: failed to update openedFilesLruCache`, | |
| 623 | + ); | |
| 624 | + } | |
| 625 | + | |
| 604 | 626 | if (data.uris) { |
| 605 | 627 | this.messenger.send("didCloseFiles", { |
| 606 | 628 | uris: data.uris, |
| @@ -608,7 +630,26 @@ | ||
| 608 | 630 | } |
| 609 | 631 | }); |
| 610 | 632 | |
| 611 | - on("files/opened", async () => {}); | |
| 633 | + on("files/opened", async ({ data: { uris } }) => { | |
| 634 | + if (uris) { | |
| 635 | + for (const filepath of uris) { | |
| 636 | + try { | |
| 637 | + const ignore = await shouldIgnore(filepath, this.ide); | |
| 638 | + if (!ignore) { | |
| 639 | + // Set the active file as most recently used (need to force recency update by deleting and re-adding) | |
| 640 | + if (openedFilesLruCache.has(filepath)) { | |
| 641 | + openedFilesLruCache.delete(filepath); | |
| 642 | + } | |
| 643 | + openedFilesLruCache.set(filepath, filepath); | |
| 644 | + } | |
| 645 | + } catch (e) { | |
| 646 | + console.error( | |
| 647 | + `files/opened: failed to update openedFiles cache for ${filepath}`, | |
| 648 | + ); | |
| 649 | + } | |
| 650 | + } | |
| 651 | + } | |
| 652 | + }); | |
| 612 | 653 | |
| 613 | 654 | // Docs, etc. indexing |
| 614 | 655 | on("indexing/reindex", async (msg) => { |
| @@ -664,66 +705,6 @@ | ||
| 664 | 705 | return { url }; |
| 665 | 706 | }); |
| 666 | 707 | |
| 667 | - on("didChangeActiveTextEditor", async ({ data: { filepath } }) => { | |
| 668 | - try { | |
| 669 | - const ignore = await shouldIgnore(filepath, this.ide); | |
| 670 | - if (!ignore) { | |
| 671 | - recentlyEditedFilesCache.set(filepath, filepath); | |
| 672 | - | |
| 673 | - // Set the active file as most recently used (need to force recency update by deleting and re-adding) | |
| 674 | - if (openedFilesLruCache.has(filepath)) { | |
| 675 | - openedFilesLruCache.delete(filepath); | |
| 676 | - } | |
| 677 | - openedFilesLruCache.set(filepath, filepath); | |
| 678 | - } | |
| 679 | - } catch (e) { | |
| 680 | - console.error( | |
| 681 | - `didChangeActiveTextEditor: failed to update recentlyEditedFiles cache for ${filepath}`, | |
| 682 | - ); | |
| 683 | - } | |
| 684 | - }); | |
| 685 | - | |
| 686 | - on("didCloseTextDocument", async ({ data: { filepaths } }) => { | |
| 687 | - try { | |
| 688 | - if (!prevFilepaths.filepaths.length) { | |
| 689 | - prevFilepaths.filepaths = filepaths; | |
| 690 | - } | |
| 691 | - | |
| 692 | - // If there is a removal, including if the number of tabs is the same (which can happen with temp tabs) | |
| 693 | - if (filepaths.length <= prevFilepaths.filepaths.length) { | |
| 694 | - // Remove files from cache that are no longer open (i.e. in the cache but not in the list of opened tabs) | |
| 695 | - for (const [key, value] of openedFilesLruCache.entriesDescending()) { | |
| 696 | - let trimmedKey = key.slice(7); // uri -> filepath | |
| 697 | - if (!filepaths.includes(trimmedKey)) { | |
| 698 | - openedFilesLruCache.delete(key); | |
| 699 | - } | |
| 700 | - } | |
| 701 | - } | |
| 702 | - | |
| 703 | - prevFilepaths.filepaths = filepaths; | |
| 704 | - } catch (e) { | |
| 705 | - console.error( | |
| 706 | - `didChangeVisibleTextEditors: failed to update openedFilesLruCache`, | |
| 707 | - ); | |
| 708 | - } | |
| 709 | - }); | |
| 710 | - | |
| 711 | - on( | |
| 712 | - "initializeOpenedFileCache", | |
| 713 | - async ({ data: { initialOpenedFilePaths } }) => { | |
| 714 | - try { | |
| 715 | - for (const filepath of initialOpenedFilePaths) { | |
| 716 | - openedFilesLruCache.set(`file://${filepath}`, `file://${filepath}`); | |
| 717 | - } | |
| 718 | - } catch (e) { | |
| 719 | - console.error( | |
| 720 | - `initializeOpenedFileCache: failed to update openedFilesLruCache`, | |
| 721 | - e, | |
| 722 | - ); | |
| 723 | - } | |
| 724 | - }, | |
| 725 | - ); | |
| 726 | - | |
| 727 | 708 | on("tools/call", async ({ data: { toolCall } }) => { |
| 728 | 709 | const { config } = await this.configHandler.loadConfig(); |
| 729 | 710 | if (!config) { |
modified core/index.d.ts +0 −2
| @@ -813,8 +813,6 @@ | ||
| 813 | 813 | |
| 814 | 814 | // Callbacks |
| 815 | 815 | onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void; |
| 816 | - | |
| 817 | - onDidCloseTextDocument(callback: (fileUris: string[]) => void): void; | |
| 818 | 816 | } |
| 819 | 817 | |
| 820 | 818 | // Slash Commands |
modified core/protocol/core.ts +1 −1
| @@ -181,7 +181,7 @@ | ||
| 181 | 181 | "files/opened": [{ uris?: string[] }, void]; |
| 182 | 182 | "files/created": [{ uris?: string[] }, void]; |
| 183 | 183 | "files/deleted": [{ uris?: string[] }, void]; |
| 184 | - "files/closed": [{ uris?: string[] }, void]; | |
| 184 | + "files/closed": [{ fileUris: string[]; uris?: string[] }, void]; | |
| 185 | 185 | |
| 186 | 186 | // Docs etc. Indexing. TODO move codebase to this |
| 187 | 187 | "indexing/reindex": [{ type: string; id: string }, void]; |
modified core/protocol/ide.ts +0 −2
| @@ -103,6 +103,4 @@ | ||
| 103 | 103 | |
| 104 | 104 | export type ToWebviewOrCoreFromIdeProtocol = { |
| 105 | 105 | didChangeActiveTextEditor: [{ filepath: string }, void]; |
| 106 | - didCloseTextDocument: [{ filepaths: string[] }, void]; | |
| 107 | - initializeOpenedFileCache: [{ initialOpenedFilePaths: string[] }, void]; | |
| 108 | 106 | }; |
modified core/protocol/messenger/messageIde.ts +0 −4
| @@ -49,10 +49,6 @@ | ||
| 49 | 49 | this.on("didChangeActiveTextEditor", (data) => callback(data.filepath)); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | - onDidCloseTextDocument(callback: (fileUris: string[]) => void): void { | |
| 53 | - this.on("didCloseTextDocument", (data) => callback(data.filepaths)); | |
| 54 | - } | |
| 55 | - | |
| 56 | 52 | getIdeSettings(): Promise<IdeSettings> { |
| 57 | 53 | return this.request("getIdeSettings", undefined); |
| 58 | 54 | } |
modified core/util/filesystem.ts +0 −3
| @@ -47,9 +47,6 @@ | ||
| 47 | 47 | onDidChangeActiveTextEditor(callback: (fileUri: string) => void): void { |
| 48 | 48 | return; |
| 49 | 49 | } |
| 50 | - onDidCloseTextDocument(callback: (fileUris: string[]) => void): void { | |
| 51 | - return; | |
| 52 | - } | |
| 53 | 50 | |
| 54 | 51 | async getIdeSettings(): Promise<IdeSettings> { |
| 55 | 52 | return { |
modified extensions/vscode/src/VsCodeIde.ts +0 −13
| @@ -94,19 +94,6 @@ | ||
| 94 | 94 | }); |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - onDidCloseTextDocument(callback: (uris: string[]) => void): void { | |
| 98 | - vscode.workspace.onDidCloseTextDocument((document) => { | |
| 99 | - if (document) { | |
| 100 | - let openFilePaths = vscode.window.tabGroups.all // get tab groups | |
| 101 | - .flatMap((group) => group.tabs) // extract tabs from tab groups | |
| 102 | - .filter((tab) => tab.input instanceof vscode.TabInputText) // filter irrelevant tabs (settings, etc.) | |
| 103 | - .map((tab) => (tab.input as vscode.TabInputText).uri.fsPath); // get filepaths | |
| 104 | - | |
| 105 | - callback(openFilePaths); | |
| 106 | - } | |
| 107 | - }); | |
| 108 | - } | |
| 109 | - | |
| 110 | 97 | showToast: IDE["showToast"] = async (...params) => { |
| 111 | 98 | const [type, message, ...otherParams] = params; |
| 112 | 99 | const { showErrorMessage, showWarningMessage, showInformationMessage } = |
modified extensions/vscode/src/extension/VsCodeExtension.ts +14 −14
| @@ -41,6 +41,7 @@ | ||
| 41 | 41 | import { ConfigYamlDocumentLinkProvider } from "./ConfigYamlDocumentLinkProvider"; |
| 42 | 42 | import { VsCodeMessenger } from "./VsCodeMessenger"; |
| 43 | 43 | |
| 44 | +import { VsCodeIdeUtils } from "../util/ideUtils"; | |
| 44 | 45 | import type { VsCodeWebviewProtocol } from "../webviewProtocol"; |
| 45 | 46 | |
| 46 | 47 | export class VsCodeExtension { |
| @@ -49,6 +50,7 @@ | ||
| 49 | 50 | private configHandler: ConfigHandler; |
| 50 | 51 | private extensionContext: vscode.ExtensionContext; |
| 51 | 52 | private ide: VsCodeIde; |
| 53 | + private ideUtils: VsCodeIdeUtils; | |
| 52 | 54 | private consoleView: ContinueConsoleWebviewViewProvider; |
| 53 | 55 | private sidebar: ContinueGUIWebviewViewProvider; |
| 54 | 56 | private windowId: string; |
| @@ -76,6 +78,7 @@ | ||
| 76 | 78 | }, |
| 77 | 79 | ); |
| 78 | 80 | this.ide = new VsCodeIde(this.webviewProtocolPromise, context); |
| 81 | + this.ideUtils = new VsCodeIdeUtils(); | |
| 79 | 82 | this.extensionContext = context; |
| 80 | 83 | this.windowId = uuidv4(); |
| 81 | 84 | |
| @@ -292,10 +295,14 @@ | ||
| 292 | 295 | }); |
| 293 | 296 | }); |
| 294 | 297 | |
| 295 | - vscode.workspace.onDidCloseTextDocument(async (event) => { | |
| 296 | - this.core.invoke("files/closed", { | |
| 297 | - uris: [event.uri.toString()], | |
| 298 | - }); | |
| 298 | + vscode.workspace.onDidCloseTextDocument((document) => { | |
| 299 | + if (document) { | |
| 300 | + let openFiles = this.ideUtils.getOpenFileUrisAsStrings(); | |
| 301 | + this.core.invoke("files/closed", { | |
| 302 | + uris: [document.uri.toString()], | |
| 303 | + fileUris: openFiles, | |
| 304 | + }); | |
| 305 | + } | |
| 299 | 306 | }); |
| 300 | 307 | |
| 301 | 308 | vscode.workspace.onDidCreateFiles(async (event) => { |
| @@ -382,19 +389,12 @@ | ||
| 382 | 389 | context.subscriptions.push(linkProvider); |
| 383 | 390 | |
| 384 | 391 | this.ide.onDidChangeActiveTextEditor((filepath) => { |
| 385 | - void this.core.invoke("didChangeActiveTextEditor", { filepath }); | |
| 386 | - }); | |
| 387 | - | |
| 388 | - this.ide.onDidCloseTextDocument((filepaths) => { | |
| 389 | - void this.core.invoke("didCloseTextDocument", { filepaths }); | |
| 392 | + void this.core.invoke("files/opened", { uris: [filepath] }); | |
| 390 | 393 | }); |
| 391 | 394 | |
| 392 | 395 | // initializes openedFileLruCache with files that are already open when the extension is activated |
| 393 | - let initialOpenedFilePaths = vscode.window.tabGroups.all | |
| 394 | - .flatMap((group) => group.tabs) | |
| 395 | - .filter((tab) => tab.input instanceof vscode.TabInputText) | |
| 396 | - .map((tab) => (tab.input as vscode.TabInputText).uri.fsPath); | |
| 397 | - this.core.invoke("initializeOpenedFileCache", { initialOpenedFilePaths }); | |
| 396 | + let initialOpenedFilePaths = this.ideUtils.getOpenFileUrisAsStrings(); | |
| 397 | + this.core.invoke("files/opened", { uris: initialOpenedFilePaths }); | |
| 398 | 398 | |
| 399 | 399 | vscode.workspace.onDidChangeConfiguration(async (event) => { |
| 400 | 400 | if (event.affectsConfiguration(EXTENSION_NAME)) { |
modified extensions/vscode/src/util/ideUtils.ts +9 −0
| @@ -267,6 +267,15 @@ | ||
| 267 | 267 | return uri.scheme === "file" || uri.scheme === "vscode-remote"; |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | + // Gets the URIs of open tabs in the editor | |
| 271 | + // Stricter criteria than getOpenFiles() | |
| 272 | + getOpenFileUrisAsStrings(): string[] { | |
| 273 | + return vscode.window.tabGroups.all | |
| 274 | + .flatMap((group) => group.tabs) | |
| 275 | + .filter((tab) => tab.input instanceof vscode.TabInputText) | |
| 276 | + .map((tab) => (tab.input as vscode.TabInputText).uri.toString()); | |
| 277 | + } | |
| 278 | + | |
| 270 | 279 | getOpenFiles(): vscode.Uri[] { |
| 271 | 280 | return vscode.window.tabGroups.all |
| 272 | 281 | .map((group) => { |