@@ -18,11 +18,20 @@ export async function processStrReplace(
1818 | { tool : 'str_replace' ; path : string ; error : string }
1919> {
2020 const initialContent = await initialContentPromise
21+ if ( initialContent === null ) {
22+ return {
23+ tool : 'str_replace' ,
24+ path,
25+ error :
26+ 'The file does not exist, skipping. Please use the write_file tool to create the file.' ,
27+ }
28+ }
2129
2230 // Process each old/new string pair
2331 let currentContent = initialContent
2432 let allPatches : string [ ] = [ ]
2533 let messages : string [ ] = [ ]
34+ const lineEnding = currentContent . includes ( '\r\n' ) ? '\r\n' : '\n'
2635
2736 for ( const { old : oldStr , new : newStr , allowMultiple } of replacements ) {
2837 // Regular case: require oldStr for replacements
@@ -32,14 +41,7 @@ export async function processStrReplace(
3241 )
3342 continue
3443 }
35- if ( currentContent === null ) {
36- messages . push (
37- 'The file does not exist, skipping. Please use the write_file tool to create the file.' ,
38- )
39- continue
40- }
4144
42- const lineEnding = currentContent . includes ( '\r\n' ) ? '\r\n' : '\n'
4345 const normalizeLineEndings = ( str : string ) => str . replace ( / \r \n / g, '\n' )
4446 const normalizedCurrentContent = normalizeLineEndings ( currentContent )
4547 const normalizedOldStr = normalizeLineEndings ( oldStr )
@@ -64,20 +66,11 @@ export async function processStrReplace(
6466 ? normalizedCurrentContent
6567 : normalizedCurrentContent . replaceAll ( updatedOldStr , newStr )
6668
67- let patch = createPatch ( path , normalizedCurrentContent , updatedContent )
68- const lines = patch . split ( '\n' )
69- const hunkStartIndex = lines . findIndex ( ( line ) => line . startsWith ( '@@' ) )
70- if ( hunkStartIndex !== - 1 ) {
71- patch = lines . slice ( hunkStartIndex ) . join ( '\n' )
72- patch = patch . replaceAll ( '\n' , lineEnding )
73- allPatches . push ( patch )
74- }
75-
7669 // Update current content for next iteration
7770 currentContent = updatedContent . replaceAll ( '\n' , lineEnding )
7871 }
7972
80- if ( allPatches . length === 0 ) {
73+ if ( initialContent === currentContent ) {
8174 logger . debug (
8275 {
8376 path,
@@ -93,7 +86,15 @@ export async function processStrReplace(
9386 }
9487 }
9588
96- const finalPatch = allPatches . join ( '\n' )
89+ let patch = createPatch ( path , initialContent , currentContent )
90+ const lines = patch . split ( '\n' )
91+ const hunkStartIndex = lines . findIndex ( ( line ) => line . startsWith ( '@@' ) )
92+ if ( hunkStartIndex !== - 1 ) {
93+ patch = lines . slice ( hunkStartIndex ) . join ( '\n' )
94+ patch = patch . replaceAll ( '\n' , lineEnding )
95+ allPatches . push ( patch )
96+ }
97+ const finalPatch = patch
9798
9899 logger . debug (
99100 {
0 commit comments