@@ -35,61 +35,77 @@ export const EditTool = Tool.define("edit", {
3535 }
3636
3737 const app = App . info ( )
38- const filepath = path . isAbsolute ( params . filePath ) ? params . filePath : path . join ( app . path . cwd , params . filePath )
39- if ( ! Filesystem . contains ( app . path . cwd , filepath ) ) {
40- throw new Error ( `File ${ filepath } is not in the current working directory` )
38+ const filePath = path . isAbsolute ( params . filePath ) ? params . filePath : path . join ( app . path . cwd , params . filePath )
39+ if ( ! Filesystem . contains ( app . path . cwd , filePath ) ) {
40+ throw new Error ( `File ${ filePath } is not in the current working directory` )
4141 }
4242
4343 const cfg = await Config . get ( )
44- if ( cfg . permission ?. edit === "ask" )
45- await Permission . ask ( {
46- id : "edit" ,
47- sessionID : ctx . sessionID ,
48- title : "Edit this file: " + filepath ,
49- metadata : {
50- filePath : filepath ,
51- oldString : params . oldString ,
52- newString : params . newString ,
53- } ,
54- } )
55-
44+ let diff = ""
5645 let contentOld = ""
5746 let contentNew = ""
5847 await ( async ( ) => {
5948 if ( params . oldString === "" ) {
6049 contentNew = params . newString
61- await Bun . write ( filepath , params . newString )
50+ diff = trimDiff ( createTwoFilesPatch ( filePath , filePath , contentOld , contentNew ) )
51+ if ( cfg . permission ?. edit === "ask" ) {
52+ await Permission . ask ( {
53+ id : "edit" ,
54+ sessionID : ctx . sessionID ,
55+ messageID : ctx . messageID ,
56+ toolCallID : ctx . toolCallID ,
57+ title : "Edit this file: " + filePath ,
58+ metadata : {
59+ filePath,
60+ diff,
61+ } ,
62+ } )
63+ }
64+ await Bun . write ( filePath , params . newString )
6265 await Bus . publish ( File . Event . Edited , {
63- file : filepath ,
66+ file : filePath ,
6467 } )
6568 return
6669 }
6770
68- const file = Bun . file ( filepath )
71+ const file = Bun . file ( filePath )
6972 const stats = await file . stat ( ) . catch ( ( ) => { } )
70- if ( ! stats ) throw new Error ( `File ${ filepath } not found` )
71- if ( stats . isDirectory ( ) ) throw new Error ( `Path is a directory, not a file: ${ filepath } ` )
72- await FileTime . assert ( ctx . sessionID , filepath )
73+ if ( ! stats ) throw new Error ( `File ${ filePath } not found` )
74+ if ( stats . isDirectory ( ) ) throw new Error ( `Path is a directory, not a file: ${ filePath } ` )
75+ await FileTime . assert ( ctx . sessionID , filePath )
7376 contentOld = await file . text ( )
74-
7577 contentNew = replace ( contentOld , params . oldString , params . newString , params . replaceAll )
78+
79+ diff = trimDiff ( createTwoFilesPatch ( filePath , filePath , contentOld , contentNew ) )
80+ if ( cfg . permission ?. edit === "ask" ) {
81+ await Permission . ask ( {
82+ id : "edit" ,
83+ sessionID : ctx . sessionID ,
84+ messageID : ctx . messageID ,
85+ toolCallID : ctx . toolCallID ,
86+ title : "Edit this file: " + filePath ,
87+ metadata : {
88+ filePath,
89+ diff,
90+ } ,
91+ } )
92+ }
93+
7694 await file . write ( contentNew )
7795 await Bus . publish ( File . Event . Edited , {
78- file : filepath ,
96+ file : filePath ,
7997 } )
8098 contentNew = await file . text ( )
8199 } ) ( )
82100
83- const diff = trimDiff ( createTwoFilesPatch ( filepath , filepath , contentOld , contentNew ) )
84-
85- FileTime . read ( ctx . sessionID , filepath )
101+ FileTime . read ( ctx . sessionID , filePath )
86102
87103 let output = ""
88- await LSP . touchFile ( filepath , true )
104+ await LSP . touchFile ( filePath , true )
89105 const diagnostics = await LSP . diagnostics ( )
90106 for ( const [ file , issues ] of Object . entries ( diagnostics ) ) {
91107 if ( issues . length === 0 ) continue
92- if ( file === filepath ) {
108+ if ( file === filePath ) {
93109 output += `\nThis file has errors, please fix\n<file_diagnostics>\n${ issues . map ( LSP . Diagnostic . pretty ) . join ( "\n" ) } \n</file_diagnostics>\n`
94110 continue
95111 }
@@ -104,7 +120,7 @@ export const EditTool = Tool.define("edit", {
104120 diagnostics,
105121 diff,
106122 } ,
107- title : `${ path . relative ( app . path . root , filepath ) } ` ,
123+ title : `${ path . relative ( app . path . root , filePath ) } ` ,
108124 output,
109125 }
110126 } ,
0 commit comments