diff --git a/core/nextEdit/context/aggregateEdits.ts b/core/nextEdit/context/aggregateEdits.ts index fd86fd602..e63df43b8 100644 --- a/core/nextEdit/context/aggregateEdits.ts +++ b/core/nextEdit/context/aggregateEdits.ts @@ -41,6 +41,7 @@ export class EditAggregator { private fileStates: Map = new Map(); private config: EditClusterConfig; private previousEditFinalCursorPosition: Position; + private lastProcessedFilePath: string | null = null; private onComparisonFinalized: ( diff: BeforeAfterDiff, beforeCursorPos: Position, @@ -75,6 +76,14 @@ export class EditAggregator { ): Promise { const filePath = edit.filepath; + // If we're switching to a different file, finalize all clusters from the previous file + if (this.lastProcessedFilePath && this.lastProcessedFilePath !== filePath) { + await this.finalizeClustersForFile(this.lastProcessedFilePath); + } + + // Update the last processed file path + this.lastProcessedFilePath = filePath; + if (!this.fileStates.has(filePath)) { this.fileStates.set(filePath, { activeClusters: [], @@ -273,17 +282,26 @@ export class EditAggregator { } } + /** + * Finalizes all clusters for a specific file + */ + private async finalizeClustersForFile(filePath: string): Promise { + const fileState = this.fileStates.get(filePath); + if (!fileState) return; + + // Create a copy of the clusters to finalize to avoid modifying array during iteration + const clustersToFinalize = [...fileState.activeClusters]; + + for (const cluster of clustersToFinalize) { + await this.finalizeCluster(filePath, cluster, fileState); + } + } + async finalizeAllClusters(): Promise { const filePromises: Promise[] = []; this.fileStates.forEach((fileState, filePath) => { - const filePromise = (async () => { - const clustersToFinalize = [...fileState.activeClusters]; - for (const cluster of clustersToFinalize) { - await this.finalizeCluster(filePath, cluster, fileState); - } - })(); - + const filePromise = this.finalizeClustersForFile(filePath); filePromises.push(filePromise); }); @@ -488,5 +506,6 @@ export class EditAggregator { resetState(): void { this.fileStates.clear(); + this.lastProcessedFilePath = null; } } diff --git a/core/nextEdit/context/autocompleteContextFetching.ts b/core/nextEdit/context/autocompleteContextFetching.ts index a77ee4292..47f6148d7 100644 --- a/core/nextEdit/context/autocompleteContextFetching.ts +++ b/core/nextEdit/context/autocompleteContextFetching.ts @@ -40,7 +40,7 @@ export const getAutocompleteContext = async ( recentlyVisitedRanges: AutocompleteCodeSnippet[], maxPromptTokens: number, manuallyPassFileContents: string, - autocompleteModel?: ILLM, + autocompleteModel?: ILLM | string, // eslint-disable-next-line max-params ): Promise => { if (!recentlyEditedRanges) recentlyEditedRanges = []; @@ -65,7 +65,20 @@ export const getAutocompleteContext = async ( // Use provided autocomplete model or fall back to configured autocomplete model let finalModel: ILLM; if (autocompleteModel) { - finalModel = autocompleteModel; + if (typeof autocompleteModel === "string") { + // If model name is provided as string, find it in the config + const foundModel = config.modelsByRole.autocomplete.find( + (m) => m.title === autocompleteModel, + ); + if (!foundModel) { + throw new Error( + `Autocomplete model with title "${autocompleteModel}" not found`, + ); + } + finalModel = foundModel; + } else { + finalModel = autocompleteModel; + } } else { const configuredModel = config.selectedModelByRole.autocomplete; if (!configuredModel) { diff --git a/core/nextEdit/context/processNextEditData.ts b/core/nextEdit/context/processNextEditData.ts index 2fbd4032a..0fc8a838c 100644 --- a/core/nextEdit/context/processNextEditData.ts +++ b/core/nextEdit/context/processNextEditData.ts @@ -4,6 +4,7 @@ import { GetLspDefinitionsFunction } from "../../autocomplete/types"; import { ConfigHandler } from "../../config/ConfigHandler"; import { RecentlyEditedRange } from "../types"; import { getAutocompleteContext } from "./autocompleteContextFetching"; +import { createDiff, DiffFormatType } from "./diffFormatting"; export const processNextEditData = async ( filePath: string, @@ -16,6 +17,7 @@ export const processNextEditData = async ( getDefinitionsFromLsp: GetLspDefinitionsFunction, recentlyEditedRanges: RecentlyEditedRange[], recentlyVisitedRanges: AutocompleteCodeSnippet[], + modelNameOrInstance?: string | undefined, // eslint-disable-next-line max-params ) => { const maxPromptTokens = 1024; @@ -23,8 +25,10 @@ export const processNextEditData = async ( // Get the actual configured autocomplete model - same as CompletionProvider const { config } = await configHandler.loadConfig(); const autocompleteModel = - config?.selectedModelByRole.autocomplete ?? undefined; - console.log(autocompleteModel); + (modelNameOrInstance || config?.selectedModelByRole.autocomplete) ?? + undefined; + + const modelName = "Codestral"; const autocompleteContext = await getAutocompleteContext( filePath, @@ -36,9 +40,13 @@ export const processNextEditData = async ( recentlyVisitedRanges, maxPromptTokens, beforeContent, - autocompleteModel, + modelName, ); - console.log("ACCESSED PREFIX:", autocompleteContext); + console.log( + createDiff(beforeContent, afterContent, filePath, DiffFormatType.Unified), + ); + + // console.log("ACCESSED PREFIX:\n", autocompleteContext); console.log("\n"); };