@@ -48,21 +48,28 @@ pub enum FsWrite {
48
48
path : String ,
49
49
file_text : Option < String > ,
50
50
new_str : Option < String > ,
51
+ summary : Option < String > ,
51
52
} ,
52
53
#[ serde( rename = "str_replace" ) ]
53
54
StrReplace {
54
55
path : String ,
55
56
old_str : String ,
56
57
new_str : String ,
58
+ summary : Option < String > ,
57
59
} ,
58
60
#[ serde( rename = "insert" ) ]
59
61
Insert {
60
62
path : String ,
61
63
insert_line : usize ,
62
64
new_str : String ,
65
+ summary : Option < String > ,
63
66
} ,
64
67
#[ serde( rename = "append" ) ]
65
- Append { path : String , new_str : String } ,
68
+ Append {
69
+ path : String ,
70
+ new_str : String ,
71
+ summary : Option < String > ,
72
+ } ,
66
73
}
67
74
68
75
impl FsWrite {
@@ -93,7 +100,9 @@ impl FsWrite {
93
100
write_to_file ( os, path, file_text) . await ?;
94
101
Ok ( Default :: default ( ) )
95
102
} ,
96
- FsWrite :: StrReplace { path, old_str, new_str } => {
103
+ FsWrite :: StrReplace {
104
+ path, old_str, new_str, ..
105
+ } => {
97
106
let path = sanitize_path_tool_arg ( os, path) ;
98
107
let file = os. fs . read_to_string ( & path) . await ?;
99
108
let matches = file. match_indices ( old_str) . collect :: < Vec < _ > > ( ) ;
@@ -119,6 +128,7 @@ impl FsWrite {
119
128
path,
120
129
insert_line,
121
130
new_str,
131
+ ..
122
132
} => {
123
133
let path = sanitize_path_tool_arg ( os, path) ;
124
134
let mut file = os. fs . read_to_string ( & path) . await ?;
@@ -143,7 +153,7 @@ impl FsWrite {
143
153
write_to_file ( os, & path, file) . await ?;
144
154
Ok ( Default :: default ( ) )
145
155
} ,
146
- FsWrite :: Append { path, new_str } => {
156
+ FsWrite :: Append { path, new_str, .. } => {
147
157
let path = sanitize_path_tool_arg ( os, path) ;
148
158
149
159
queue ! (
@@ -182,12 +192,17 @@ impl FsWrite {
182
192
} ;
183
193
let new = stylize_output_if_able ( os, & relative_path, & file_text) ;
184
194
print_diff ( output, & prev, & new, 1 ) ?;
195
+
196
+ // Display summary as purpose if available after the diff
197
+ super :: display_purpose ( self . get_summary ( ) , output) ?;
198
+
185
199
Ok ( ( ) )
186
200
} ,
187
201
FsWrite :: Insert {
188
202
path,
189
203
insert_line,
190
204
new_str,
205
+ ..
191
206
} => {
192
207
let path = sanitize_path_tool_arg ( os, path) ;
193
208
let relative_path = format_path ( cwd, & path) ;
@@ -206,9 +221,15 @@ impl FsWrite {
206
221
let old = stylize_output_if_able ( os, & relative_path, & old) ;
207
222
let new = stylize_output_if_able ( os, & relative_path, & new) ;
208
223
print_diff ( output, & old, & new, start_line) ?;
224
+
225
+ // Display summary as purpose if available after the diff
226
+ super :: display_purpose ( self . get_summary ( ) , output) ?;
227
+
209
228
Ok ( ( ) )
210
229
} ,
211
- FsWrite :: StrReplace { path, old_str, new_str } => {
230
+ FsWrite :: StrReplace {
231
+ path, old_str, new_str, ..
232
+ } => {
212
233
let path = sanitize_path_tool_arg ( os, path) ;
213
234
let relative_path = format_path ( cwd, & path) ;
214
235
let file = os. fs . read_to_string_sync ( & path) ?;
@@ -220,14 +241,21 @@ impl FsWrite {
220
241
let new_str = stylize_output_if_able ( os, & relative_path, new_str) ;
221
242
print_diff ( output, & old_str, & new_str, start_line) ?;
222
243
244
+ // Display summary as purpose if available after the diff
245
+ super :: display_purpose ( self . get_summary ( ) , output) ?;
246
+
223
247
Ok ( ( ) )
224
248
} ,
225
- FsWrite :: Append { path, new_str } => {
249
+ FsWrite :: Append { path, new_str, .. } => {
226
250
let path = sanitize_path_tool_arg ( os, path) ;
227
251
let relative_path = format_path ( cwd, & path) ;
228
252
let start_line = os. fs . read_to_string_sync ( & path) ?. lines ( ) . count ( ) + 1 ;
229
253
let file = stylize_output_if_able ( os, & relative_path, new_str) ;
230
254
print_diff ( output, & Default :: default ( ) , & file, start_line) ?;
255
+
256
+ // Display summary as purpose if available after the diff
257
+ super :: display_purpose ( self . get_summary ( ) , output) ?;
258
+
231
259
Ok ( ( ) )
232
260
} ,
233
261
}
@@ -246,7 +274,7 @@ impl FsWrite {
246
274
bail ! ( "The provided path must exist in order to replace or insert contents into it" )
247
275
}
248
276
} ,
249
- FsWrite :: Append { path, new_str } => {
277
+ FsWrite :: Append { path, new_str, .. } => {
250
278
if path. is_empty ( ) {
251
279
bail ! ( "Path must not be empty" )
252
280
} ;
@@ -299,6 +327,16 @@ impl FsWrite {
299
327
_ => String :: new ( ) ,
300
328
}
301
329
}
330
+
331
+ /// Returns the summary from any variant of the FsWrite enum
332
+ fn get_summary ( & self ) -> Option < & String > {
333
+ match self {
334
+ FsWrite :: Create { summary, .. } => summary. as_ref ( ) ,
335
+ FsWrite :: StrReplace { summary, .. } => summary. as_ref ( ) ,
336
+ FsWrite :: Insert { summary, .. } => summary. as_ref ( ) ,
337
+ FsWrite :: Append { summary, .. } => summary. as_ref ( ) ,
338
+ }
339
+ }
302
340
}
303
341
304
342
/// Writes `content` to `path`, adding a newline if necessary.
@@ -650,6 +688,40 @@ mod tests {
650
688
assert ! ( matches!( fw, FsWrite :: Append { .. } ) ) ;
651
689
}
652
690
691
+ #[ test]
692
+ fn test_fs_write_deserialize_with_summary ( ) {
693
+ let path = "/my-file" ;
694
+ let file_text = "hello world" ;
695
+ let summary = "Added hello world content" ;
696
+
697
+ // create with summary
698
+ let v = serde_json:: json!( {
699
+ "path" : path,
700
+ "command" : "create" ,
701
+ "file_text" : file_text,
702
+ "summary" : summary
703
+ } ) ;
704
+ let fw = serde_json:: from_value :: < FsWrite > ( v) . unwrap ( ) ;
705
+ assert ! ( matches!( fw, FsWrite :: Create { .. } ) ) ;
706
+ if let FsWrite :: Create { summary : s, .. } = & fw {
707
+ assert_eq ! ( s. as_ref( ) . unwrap( ) , summary) ;
708
+ }
709
+
710
+ // str_replace with summary
711
+ let v = serde_json:: json!( {
712
+ "path" : path,
713
+ "command" : "str_replace" ,
714
+ "old_str" : "prev string" ,
715
+ "new_str" : "new string" ,
716
+ "summary" : summary
717
+ } ) ;
718
+ let fw = serde_json:: from_value :: < FsWrite > ( v) . unwrap ( ) ;
719
+ assert ! ( matches!( fw, FsWrite :: StrReplace { .. } ) ) ;
720
+ if let FsWrite :: StrReplace { summary : s, .. } = & fw {
721
+ assert_eq ! ( s. as_ref( ) . unwrap( ) , summary) ;
722
+ }
723
+ }
724
+
653
725
#[ tokio:: test]
654
726
async fn test_fs_write_tool_create ( ) {
655
727
let os = setup_test_directory ( ) . await ;
0 commit comments