Elffuss BraidHub

kikocis/continue public

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