set context formatting to Codestral style and fixed file-transition edit logging

This commit is contained in:
adarshramiyer 2025-06-20 01:48:35 -07:00
parent 26c501e9a1
commit 9dc60eeb63
3 changed files with 53 additions and 13 deletions

View file

@ -41,6 +41,7 @@ export class EditAggregator {
private fileStates: Map<string, FileState> = 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<void> {
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<void> {
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<void> {
const filePromises: Promise<void>[] = [];
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;
}
}

View file

@ -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<string> => {
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) {

View file

@ -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");
};