refactor: improve NewUnifiedDiffStrategy with enhanced formatting and error handling

- Cleaned up formatting in the parseUnifiedDiff method to ensure consistent indentation and spacing.
- Updated documentation to clarify format requirements for generating unified diffs.
- Added error handling for cases where no hunks are found in the provided diff, improving robustness.
- Enhanced overall readability of the code by standardizing comments and structure.
This commit is contained in:
Daniel Riccio
2025-01-14 12:20:52 -05:00
parent d25f370013
commit 534888af97

View File

@@ -129,7 +129,7 @@ If you need to move code within a file, create two hunks: one to delete the code
To create a new file, show a diff from "--- /dev/null" to "+++ path/to/new/file.ext". To create a new file, show a diff from "--- /dev/null" to "+++ path/to/new/file.ext".
Heres an example of the desired format: Format Requirements:
\`\`\`diff \`\`\`diff
--- mathweb/flask/app.py --- mathweb/flask/app.py
@@ -171,6 +171,10 @@ Heres an example of the desired format:
Be precise, consistent, and follow these rules carefully to generate correct diffs! Be precise, consistent, and follow these rules carefully to generate correct diffs!
Parameters:
- path: (required) The path of the file to apply the diff to (relative to the current working directory ${cwd})
- diff: (required) The diff content in unified format to apply to the file.
Usage: Usage:
<apply_diff> <apply_diff>
<path>File path here</path> <path>File path here</path>
@@ -191,6 +195,10 @@ Your diff here
const originalLines = originalContent.split("\n") const originalLines = originalContent.split("\n")
let result = [...originalLines] let result = [...originalLines]
if (!parsedDiff.hunks.length) {
return { success: false, error: "No hunks found in diff" }
}
for (const hunk of parsedDiff.hunks) { for (const hunk of parsedDiff.hunks) {
const contextStr = prepareSearchString(hunk.changes) const contextStr = prepareSearchString(hunk.changes)
const { index: matchPosition, confidence } = findBestMatch(contextStr, result) const { index: matchPosition, confidence } = findBestMatch(contextStr, result)