@@ -257,56 +257,70 @@ function createSearchTitle(query: string | null, path: string | null): string {
257257}
258258
259259async function createPatchContent ( change : FileUpdateChange ) : Promise < ToolCallContent | null > {
260- if ( change . kind . type === "add" && ! isUnifiedDiff ( change . diff ) ) {
261- // For new files, diff may contain raw file content instead of a patch.
262- return {
263- type : "diff" ,
264- oldText : null ,
265- newText : change . diff ,
266- path : change . path ,
267- _meta : {
268- kind : "add" ,
269- } ,
270- } ;
260+ switch ( change . kind . type ) {
261+ case "add" :
262+ return await createAddFileContent ( change ) ;
263+ case "delete" :
264+ return await createDeleteFileContent ( change ) ;
265+ case "update" :
266+ return await createUpdateFileContent ( change ) ;
271267 }
268+ }
272269
273- if ( change . kind . type === "delete" ) {
274- // If the patch deletes a file, the old content may be only available from the diff.
275- const oldContent = await readFile ( change . path , { encoding : "utf8" } ) . catch ( ( ) =>
276- isUnifiedDiff ( change . diff ) ? patchToDeletedContent ( change . diff ) : change . diff
277- ) ;
278-
279- return {
280- type : "diff" ,
281- oldText : oldContent ,
282- newText : "" ,
283- path : change . path ,
284- _meta : {
285- kind : "delete" ,
286- }
287- }
270+ async function createAddFileContent ( change : FileUpdateChange ) : Promise < ToolCallContent | null > {
271+ let newText ;
272+ if ( isUnifiedDiff ( change . diff ) ) {
273+ newText = applyPatch ( "" , change . diff )
274+ if ( ! newText ) return null ;
275+ } else {
276+ newText = change . diff ;
288277 }
278+ return {
279+ type : "diff" ,
280+ oldText : null ,
281+ newText : newText ,
282+ path : change . path ,
283+ _meta : {
284+ kind : "add" ,
285+ } ,
286+ } ;
287+ }
289288
290- const oldContent = change . kind . type === "add" ? "" : await readFile ( change . path , { encoding : "utf8" } ) . catch ( ( ) => null ) ;
291- if ( oldContent === null ) {
292- return null ;
293- }
289+ async function createUpdateFileContent ( change : FileUpdateChange ) : Promise < ToolCallContent | null > {
290+ const oldContent = await readFile ( change . path , { encoding : "utf8" } ) . catch ( ( ) => null ) ;
291+ if ( oldContent === null ) return null ;
294292
295293 const newContent = applyPatch ( oldContent , change . diff ) ;
296- if ( newContent === false ) {
297- return null ;
298- }
294+ if ( ! newContent ) return null ;
295+
299296 return {
300297 type : "diff" ,
301- oldText : change . kind . type === "add" ? null : oldContent ,
298+ oldText : oldContent ,
302299 newText : newContent ,
303300 path : change . path ,
304301 _meta : {
305- kind : change . kind . type ,
302+ kind : "update" ,
306303 } ,
307304 } ;
308305}
309306
307+ async function createDeleteFileContent ( change : FileUpdateChange ) : Promise < ToolCallContent > {
308+ // If the patch deletes a file, the old content may be only available from the diff.
309+ const oldContent = await readFile ( change . path , { encoding : "utf8" } ) . catch ( ( ) =>
310+ isUnifiedDiff ( change . diff ) ? patchToDeletedContent ( change . diff ) : change . diff
311+ ) ;
312+
313+ return {
314+ type : "diff" ,
315+ oldText : oldContent ,
316+ newText : "" ,
317+ path : change . path ,
318+ _meta : {
319+ kind : "delete" ,
320+ }
321+ }
322+ }
323+
310324function isUnifiedDiff ( content : string ) : boolean {
311325 return content . startsWith ( "--- " ) || content . includes ( "\n--- " ) ;
312326}
0 commit comments