Trim edits if possible.

- Since we're now LLM correcting a lot of problematic edits we need to also potentially trim bad edits (llms have a bad habbit of adding whitespace places).

Part of https://github.com/google-gemini/gemini-cli/issues/484
This commit is contained in:
Taylor Mullen 2025-05-25 13:59:05 -07:00 committed by N. Taylor Mullen
parent 2e3eeaf920
commit 48781272ee
1 changed files with 35 additions and 0 deletions

View File

@ -136,6 +136,14 @@ export async function ensureCorrectEdit(
}
}
const { targetString, pair } = trimPairIfPossible(
finalOldString,
finalNewString,
currentContent,
);
finalOldString = targetString;
finalNewString = pair;
// Final result construction
const result: CorrectedEditResult = {
params: {
@ -359,6 +367,33 @@ Return ONLY the corrected string in the specified JSON format with the key 'corr
}
}
function trimPairIfPossible(
target: string,
trimIfTargetTrims: string,
currentContent: string,
) {
const trimmedTargetString = target.trim();
if (target.length !== trimmedTargetString.length) {
const trimmedTargetOccurrences = countOccurrences(
currentContent,
trimmedTargetString,
);
if (trimmedTargetOccurrences === 1) {
const trimmedReactiveString = trimIfTargetTrims.trim();
return {
targetString: trimmedTargetString,
pair: trimmedReactiveString,
};
}
}
return {
targetString: target,
pair: trimIfTargetTrims,
};
}
/**
* Unescapes a string that might have been overly escaped by an LLM.
*/