| 1 | import { RangeInFile } from ".."; |
| 2 | import { NextEditProvider } from "./NextEditProvider"; |
| 3 | import { NextEditOutcome } from "./types"; |
| 4 | |
| 5 | export interface ProcessedItem { |
| 6 | location: RangeInFile; |
| 7 | outcome: NextEditOutcome; // Result from the model |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Keeps a queue of the broken down diffs from a changed editable range, as determined in core/nextEdit/diff.ts |
| 12 | */ |
| 13 | /** |
| 14 | * This is where the chain is stored. Think of it as a regular queue, but being a singleton because we need one source of truth for the chain. |
| 15 | * I originally intended this to be a separate data structure to handle prefetching next edit outcomes from the model in the background. |
| 16 | * Due to subpar results, lack of satisfactory next edit location suggestion algorithms and token cost/latency issues, I scratched the idea. |
| 17 | */ |
| 18 | export class PrefetchQueue { |
| 19 | private static instance: PrefetchQueue | null = null; |
| 20 | |
| 21 | private unprocessedQueue: RangeInFile[] = []; |
| 22 | private processedQueue: ProcessedItem[] = []; |
| 23 | private prefetchLimit: number; |
| 24 | private abortController: AbortController; |
| 25 | |
| 26 | private usingFullFileDiff: boolean = true; |
| 27 | |
| 28 | private constructor(prefetchLimit: number = 3) { |
| 29 | this.prefetchLimit = prefetchLimit; |
| 30 | this.abortController = new AbortController(); |
| 31 | } |
| 32 | |
| 33 | public static getInstance(prefetchLimit: number = 3): PrefetchQueue { |
| 34 | if (!PrefetchQueue.instance) { |
| 35 | PrefetchQueue.instance = new PrefetchQueue(prefetchLimit); |
| 36 | } |
| 37 | |
| 38 | return PrefetchQueue.instance; |
| 39 | } |
| 40 | |
| 41 | initialize(usingFullFileDiff: boolean) { |
| 42 | this.usingFullFileDiff = usingFullFileDiff; |
| 43 | } |
| 44 | |
| 45 | // Queue management methods |
| 46 | enqueueUnprocessed(location: RangeInFile): void { |
| 47 | this.unprocessedQueue.push(location); |
| 48 | } |
| 49 | |
| 50 | private dequeueUnprocessed(): RangeInFile | undefined { |
| 51 | return this.unprocessedQueue.shift(); |
| 52 | } |
| 53 | |
| 54 | enqueueProcessed(item: ProcessedItem): void { |
| 55 | this.processedQueue.push(item); |
| 56 | } |
| 57 | |
| 58 | dequeueProcessed(): ProcessedItem | undefined { |
| 59 | return this.processedQueue.shift(); |
| 60 | } |
| 61 | |
| 62 | // Process items from unprocessed queue |
| 63 | async process(ctx: any): Promise<void> { |
| 64 | while ( |
| 65 | this.unprocessedQueue.length > 0 && |
| 66 | this.processedQueue.length < this.prefetchLimit && |
| 67 | !this.abortController.signal.aborted |
| 68 | ) { |
| 69 | const location = this.dequeueUnprocessed(); |
| 70 | console.log("processing:"); |
| 71 | console.log( |
| 72 | location?.range.start.line + " to " + location?.range.end.line, |
| 73 | ); |
| 74 | |
| 75 | if (!location) break; |
| 76 | |
| 77 | try { |
| 78 | const outcome = |
| 79 | await NextEditProvider.getInstance().provideInlineCompletionItemsWithChain( |
| 80 | ctx, |
| 81 | location, |
| 82 | this.abortController.signal, |
| 83 | this.usingFullFileDiff, |
| 84 | ); |
| 85 | |
| 86 | if (!outcome) { |
| 87 | console.log("outcome is undefined"); |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | this.enqueueProcessed({ |
| 92 | location, |
| 93 | outcome, |
| 94 | }); |
| 95 | |
| 96 | console.log( |
| 97 | "the length of processed queue after processing is:", |
| 98 | this.processedQueue.length, |
| 99 | ); |
| 100 | } catch (error) { |
| 101 | if (!this.abortController.signal.aborted) { |
| 102 | // Handle error |
| 103 | console.error("Error processing item:", error); |
| 104 | } |
| 105 | // If aborted, we just stop processing |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Abort all operations |
| 112 | abort(): void { |
| 113 | this.abortController.abort(); |
| 114 | this.clear(); |
| 115 | |
| 116 | // Create a new AbortController for future operations |
| 117 | this.abortController = new AbortController(); |
| 118 | } |
| 119 | |
| 120 | // Clear all queues |
| 121 | clear(): void { |
| 122 | this.unprocessedQueue = []; |
| 123 | this.processedQueue = []; |
| 124 | } |
| 125 | |
| 126 | // Additional helper methods |
| 127 | get unprocessedCount(): number { |
| 128 | return this.unprocessedQueue.length; |
| 129 | } |
| 130 | |
| 131 | get processedCount(): number { |
| 132 | return this.processedQueue.length; |
| 133 | } |
| 134 | |
| 135 | peekProcessed(): ProcessedItem | undefined { |
| 136 | return this.processedQueue[0]; |
| 137 | } |
| 138 | |
| 139 | peekThreeProcessed(): void { |
| 140 | const count = Math.min(3, this.processedQueue.length); |
| 141 | const firstThree = this.processedQueue.slice(0, count); |
| 142 | firstThree.forEach((item, index) => { |
| 143 | console.log( |
| 144 | `Item ${index + 1}: ${item.location.range.start.line} to ${item.location.range.end.line}`, |
| 145 | ); |
| 146 | }); |
| 147 | } |
| 148 | |
| 149 | setPreetchLimit(limit: number): void { |
| 150 | this.prefetchLimit = limit; |
| 151 | } |
| 152 | } |