Elffuss BraidHub

kikocis/continue public

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