feat: create prefetch queue
This commit is contained in:
parent
f6e5198cfa
commit
375b19a70c
1 changed files with 78 additions and 76 deletions
|
|
@ -1,91 +1,93 @@
|
|||
import { Position } from "..";
|
||||
import { NextEditOutcome } from "./types";
|
||||
import { NextEditProvider } from "./NextEditProvider";
|
||||
|
||||
export interface PrefetchQueueItem {
|
||||
editableRegionStartLine: number;
|
||||
editableRegionEndLine: number;
|
||||
cursorPosition: Position;
|
||||
outcome?: NextEditOutcome;
|
||||
completionId?: string;
|
||||
inProgress: boolean;
|
||||
}
|
||||
export class PrefetchQueue {
|
||||
private unprocessedQueue: EditLocation[] = [];
|
||||
private processedQueue: ProcessedItem[] = [];
|
||||
private prefetchLimit: number;
|
||||
private abortController: AbortController;
|
||||
|
||||
export class NextEditPrefetchQueue {
|
||||
private static _instance: NextEditPrefetchQueue;
|
||||
private queue: PrefetchQueueItem[] = [];
|
||||
private maxQueueSize: number = 3; // Limit queue size to prevent too many requests. We don't need to be too eager.
|
||||
|
||||
private constructor() {}
|
||||
|
||||
public static getInstance(): NextEditPrefetchQueue {
|
||||
if (!NextEditPrefetchQueue._instance) {
|
||||
NextEditPrefetchQueue._instance = new NextEditPrefetchQueue();
|
||||
}
|
||||
return NextEditPrefetchQueue._instance;
|
||||
constructor(prefetchLimit: number = 3) {
|
||||
this.prefetchLimit = prefetchLimit;
|
||||
this.abortController = new AbortController();
|
||||
}
|
||||
|
||||
public enqueue(item: PrefetchQueueItem): void {
|
||||
// Deduplication logic.
|
||||
if (
|
||||
this.queue.some(
|
||||
(queueItem) =>
|
||||
queueItem.editableRegionStartLine === item.editableRegionStartLine,
|
||||
)
|
||||
// Queue management methods
|
||||
enqueueUnprocessed(location: EditLocation): void {
|
||||
this.unprocessedQueue.push(location);
|
||||
}
|
||||
|
||||
dequeueUnprocessed(): EditLocation | undefined {
|
||||
return this.unprocessedQueue.shift();
|
||||
}
|
||||
|
||||
enqueueProcessed(item: ProcessedItem): void {
|
||||
this.processedQueue.push(item);
|
||||
}
|
||||
|
||||
dequeueProcessed(): ProcessedItem | undefined {
|
||||
return this.processedQueue.shift();
|
||||
}
|
||||
|
||||
// Process items from unprocessed queue
|
||||
async process(ctx: any): Promise<void> {
|
||||
let processCount = 0;
|
||||
|
||||
while (
|
||||
this.unprocessedQueue.length > 0 &&
|
||||
processCount < this.prefetchLimit &&
|
||||
!this.abortController.signal.aborted
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const location = this.dequeueUnprocessed();
|
||||
if (!location) break;
|
||||
|
||||
// Shift off oldest item in the queue.
|
||||
while (this.queue.length >= this.maxQueueSize) {
|
||||
this.queue.shift();
|
||||
}
|
||||
try {
|
||||
const outcome =
|
||||
await NextEditProvider.getInstance().provideInlineCompletionItemsWithChain(
|
||||
ctx,
|
||||
location,
|
||||
this.abortController.signal,
|
||||
);
|
||||
|
||||
this.queue.push(item);
|
||||
}
|
||||
this.enqueueProcessed({
|
||||
location,
|
||||
outcome,
|
||||
});
|
||||
|
||||
public dequeue(): PrefetchQueueItem | undefined {
|
||||
return this.queue.shift();
|
||||
}
|
||||
|
||||
public getNextUnprocessedItem(): PrefetchQueueItem | undefined {
|
||||
return this.queue.find((item) => !item.inProgress && !item.outcome);
|
||||
}
|
||||
|
||||
public updateItemWithOutcome(
|
||||
startLine: number,
|
||||
outcome: NextEditOutcome,
|
||||
completionId: string,
|
||||
): void {
|
||||
const item = this.queue.find(
|
||||
(item) => item.editableRegionStartLine === startLine,
|
||||
);
|
||||
if (item) {
|
||||
item.outcome = outcome;
|
||||
item.completionId = completionId;
|
||||
item.inProgress = false;
|
||||
processCount++;
|
||||
} catch (error) {
|
||||
if (!this.abortController.signal.aborted) {
|
||||
// Handle error
|
||||
console.error("Error processing item:", error);
|
||||
}
|
||||
// If aborted, we just stop processing
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public markItemInProgress(startLine: number): void {
|
||||
const item = this.queue.find(
|
||||
(item) => item.editableRegionStartLine === startLine,
|
||||
);
|
||||
if (item) {
|
||||
item.inProgress = true;
|
||||
}
|
||||
// Abort all operations
|
||||
abort(): void {
|
||||
this.abortController.abort();
|
||||
this.clear();
|
||||
|
||||
// Create a new AbortController for future operations
|
||||
this.abortController = new AbortController();
|
||||
}
|
||||
|
||||
public getItemByStartLine(startLine: number): PrefetchQueueItem | undefined {
|
||||
return this.queue.find(
|
||||
(item) => item.editableRegionStartLine === startLine,
|
||||
);
|
||||
}
|
||||
|
||||
public clear(): void {
|
||||
this.queue = [];
|
||||
}
|
||||
|
||||
public size(): number {
|
||||
return this.queue.length;
|
||||
// Clear all queues
|
||||
clear(): void {
|
||||
this.unprocessedQueue = [];
|
||||
this.processedQueue = [];
|
||||
}
|
||||
}
|
||||
|
||||
// Types
|
||||
interface EditLocation {
|
||||
// Define properties for edit location
|
||||
// e.g., file, position, etc.
|
||||
}
|
||||
|
||||
interface ProcessedItem {
|
||||
location: EditLocation;
|
||||
outcome: any; // Result from the model
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue