@@ -70,50 +70,10 @@ export const EditTool = Tool.defineEffect(
7070 let contentOld = ""
7171 let contentNew = ""
7272 yield * filetime . withLock ( filePath , async ( ) => {
73- if ( params . oldString === "" ) {
74- const existed = await Filesystem . exists ( filePath )
75- contentNew = params . newString
76- diff = trimDiff ( createTwoFilesPatch ( filePath , filePath , contentOld , contentNew ) )
77- await ctx . ask ( {
78- permission : "edit" ,
79- patterns : [ path . relative ( Instance . worktree , filePath ) ] ,
80- always : [ "*" ] ,
81- metadata : {
82- filepath : filePath ,
83- diff,
84- } ,
85- } )
86- await Filesystem . write ( filePath , params . newString )
87- await Format . file ( filePath )
88- Bus . publish ( File . Event . Edited , { file : filePath } )
89- await Bus . publish ( FileWatcher . Event . Updated , {
90- file : filePath ,
91- event : existed ? "change" : "add" ,
92- } )
93- await FileTime . read ( ctx . sessionID , filePath )
94- return
95- }
96-
97- const stats = Filesystem . stat ( filePath )
98- if ( ! stats ) throw new Error ( `File ${ filePath } not found` )
99- if ( stats . isDirectory ( ) ) throw new Error ( `Path is a directory, not a file: ${ filePath } ` )
100- await FileTime . assert ( ctx . sessionID , filePath )
101- contentOld = await Filesystem . readText ( filePath )
102-
103- const ending = detectLineEnding ( contentOld )
104- const old = convertToLineEnding ( normalizeLineEndings ( params . oldString ) , ending )
105- const next = convertToLineEnding ( normalizeLineEndings ( params . newString ) , ending )
106-
107- contentNew = replace ( contentOld , old , next , params . replaceAll )
108-
109- diff = trimDiff (
110- createTwoFilesPatch (
111- filePath ,
112- filePath ,
113- normalizeLineEndings ( contentOld ) ,
114- normalizeLineEndings ( contentNew ) ,
115- ) ,
116- )
73+ if ( params . oldString === "" ) {
74+ const existed = await Filesystem . exists ( filePath )
75+ contentNew = params . newString
76+ diff = trimDiff ( createTwoFilesPatch ( filePath , filePath , contentOld , contentNew ) )
11777 await ctx . ask ( {
11878 permission : "edit" ,
11979 patterns : [ path . relative ( Instance . worktree , filePath ) ] ,
@@ -123,25 +83,65 @@ export const EditTool = Tool.defineEffect(
12383 diff,
12484 } ,
12585 } )
126-
127- await Filesystem . write ( filePath , contentNew )
86+ await Filesystem . write ( filePath , params . newString )
12887 await Format . file ( filePath )
12988 Bus . publish ( File . Event . Edited , { file : filePath } )
13089 await Bus . publish ( FileWatcher . Event . Updated , {
13190 file : filePath ,
132- event : "change" ,
91+ event : existed ? "change" : "add ",
13392 } )
134- contentNew = await Filesystem . readText ( filePath )
135- diff = trimDiff (
136- createTwoFilesPatch (
137- filePath ,
138- filePath ,
139- normalizeLineEndings ( contentOld ) ,
140- normalizeLineEndings ( contentNew ) ,
141- ) ,
142- )
14393 await FileTime . read ( ctx . sessionID , filePath )
94+ return
95+ }
96+
97+ const stats = Filesystem . stat ( filePath )
98+ if ( ! stats ) throw new Error ( `File ${ filePath } not found` )
99+ if ( stats . isDirectory ( ) ) throw new Error ( `Path is a directory, not a file: ${ filePath } ` )
100+ await FileTime . assert ( ctx . sessionID , filePath )
101+ contentOld = await Filesystem . readText ( filePath )
102+
103+ const ending = detectLineEnding ( contentOld )
104+ const old = convertToLineEnding ( normalizeLineEndings ( params . oldString ) , ending )
105+ const next = convertToLineEnding ( normalizeLineEndings ( params . newString ) , ending )
106+
107+ contentNew = replace ( contentOld , old , next , params . replaceAll )
108+
109+ diff = trimDiff (
110+ createTwoFilesPatch (
111+ filePath ,
112+ filePath ,
113+ normalizeLineEndings ( contentOld ) ,
114+ normalizeLineEndings ( contentNew ) ,
115+ ) ,
116+ )
117+ await ctx . ask ( {
118+ permission : "edit" ,
119+ patterns : [ path . relative ( Instance . worktree , filePath ) ] ,
120+ always : [ "*" ] ,
121+ metadata : {
122+ filepath : filePath ,
123+ diff,
124+ } ,
125+ } )
126+
127+ await Filesystem . write ( filePath , contentNew )
128+ await Format . file ( filePath )
129+ Bus . publish ( File . Event . Edited , { file : filePath } )
130+ await Bus . publish ( FileWatcher . Event . Updated , {
131+ file : filePath ,
132+ event : "change" ,
144133 } )
134+ contentNew = await Filesystem . readText ( filePath )
135+ diff = trimDiff (
136+ createTwoFilesPatch (
137+ filePath ,
138+ filePath ,
139+ normalizeLineEndings ( contentOld ) ,
140+ normalizeLineEndings ( contentNew ) ,
141+ ) ,
142+ )
143+ await FileTime . read ( ctx . sessionID , filePath )
144+ } )
145145
146146 const filediff : Snapshot . FileDiff = {
147147 file : filePath ,
0 commit comments