@@ -294,12 +294,14 @@ export function multiply(a: number, b: number): number {
294294 }
295295 toolResultsCapture . push ( step . toolResult )
296296
297+ const firstResult = step . toolResult ?. [ 0 ]
298+ const unifiedDiff = firstResult ?. type === 'json' ? ( firstResult . value as { unifiedDiff ?: string } ) ?. unifiedDiff : undefined
297299 yield {
298300 toolName : 'set_output' ,
299301 input : {
300302 toolCalls : [ { toolName : 'propose_str_replace' , input : step } ] ,
301303 toolResults : step . toolResult ,
302- unifiedDiffs : step . toolResult ?. [ 0 ] ?. value ?. unifiedDiff ?? '' ,
304+ unifiedDiffs : unifiedDiff ?? '' ,
303305 } ,
304306 }
305307 yield { toolName : 'end_turn' , input : { } }
@@ -321,9 +323,10 @@ export function multiply(a: number, b: number): number {
321323 const toolResult = toolResultsCapture [ 0 ]
322324 expect ( toolResult ) . toBeDefined ( )
323325 expect ( toolResult [ 0 ] . type ) . toBe ( 'json' )
324- expect ( toolResult [ 0 ] . value . file ) . toBe ( 'src/utils.ts' )
325- expect ( toolResult [ 0 ] . value . unifiedDiff ) . toContain ( '+export function multiply' )
326- expect ( toolResult [ 0 ] . value . unifiedDiff ) . toContain ( 'return a * b' )
326+ const jsonResult = toolResult [ 0 ] as { type : 'json' ; value : { file : string ; unifiedDiff : string } }
327+ expect ( jsonResult . value . file ) . toBe ( 'src/utils.ts' )
328+ expect ( jsonResult . value . unifiedDiff ) . toContain ( '+export function multiply' )
329+ expect ( jsonResult . value . unifiedDiff ) . toContain ( 'return a * b' )
327330 } )
328331
329332 it ( 'should return error when string not found' , async ( ) => {
@@ -351,7 +354,8 @@ export function multiply(a: number, b: number): number {
351354
352355 expect ( toolResultsCapture ) . toHaveLength ( 1 )
353356 const toolResult = toolResultsCapture [ 0 ]
354- expect ( toolResult [ 0 ] . value . errorMessage ) . toContain ( 'String not found' )
357+ const jsonResult = toolResult [ 0 ] as { type : 'json' ; value : { errorMessage : string } }
358+ expect ( jsonResult . value . errorMessage ) . toContain ( 'String not found' )
355359 } )
356360
357361 it ( 'should stack multiple replacements on the same file' , async ( ) => {
@@ -396,8 +400,10 @@ export function multiply(a: number, b: number): number {
396400 expect ( toolResultsCapture ) . toHaveLength ( 2 )
397401
398402 // Both replacements should succeed
399- expect ( toolResultsCapture [ 0 ] . result [ 0 ] . value . unifiedDiff ) . toContain ( '// addition' )
400- expect ( toolResultsCapture [ 1 ] . result [ 0 ] . value . unifiedDiff ) . toContain ( '// subtraction' )
403+ const result0 = toolResultsCapture [ 0 ] . result [ 0 ] as { type : 'json' ; value : { unifiedDiff : string } }
404+ const result1 = toolResultsCapture [ 1 ] . result [ 0 ] as { type : 'json' ; value : { unifiedDiff : string } }
405+ expect ( result0 . value . unifiedDiff ) . toContain ( '// addition' )
406+ expect ( result1 . value . unifiedDiff ) . toContain ( '// subtraction' )
401407
402408 // Final file should have both changes
403409 expect ( mockFiles [ 'src/utils.ts' ] ) . toContain ( '// addition' )
@@ -431,9 +437,10 @@ export function multiply(a: number, b: number): number {
431437
432438 expect ( toolResultsCapture ) . toHaveLength ( 1 )
433439 const toolResult = toolResultsCapture [ 0 ]
434- expect ( toolResult [ 0 ] . value . file ) . toBe ( 'src/multiply.ts' )
435- expect ( toolResult [ 0 ] . value . message ) . toContain ( 'new file' )
436- expect ( toolResult [ 0 ] . value . unifiedDiff ) . toContain ( '+export function multiply' )
440+ const jsonResult = toolResult [ 0 ] as { type : 'json' ; value : { file : string ; message : string ; unifiedDiff : string } }
441+ expect ( jsonResult . value . file ) . toBe ( 'src/multiply.ts' )
442+ expect ( jsonResult . value . message ) . toContain ( 'new file' )
443+ expect ( jsonResult . value . unifiedDiff ) . toContain ( '+export function multiply' )
437444 } )
438445
439446 it ( 'should propose file edit and return unified diff' , async ( ) => {
@@ -469,9 +476,10 @@ export function multiply(a: number, b: number): number {
469476
470477 expect ( toolResultsCapture ) . toHaveLength ( 1 )
471478 const toolResult = toolResultsCapture [ 0 ]
472- expect ( toolResult [ 0 ] . value . file ) . toBe ( 'src/utils.ts' )
473- expect ( toolResult [ 0 ] . value . message ) . toContain ( 'changes' )
474- expect ( toolResult [ 0 ] . value . unifiedDiff ) . toContain ( '+export function multiply' )
479+ const jsonResult = toolResult [ 0 ] as { type : 'json' ; value : { file : string ; message : string ; unifiedDiff : string } }
480+ expect ( jsonResult . value . file ) . toBe ( 'src/utils.ts' )
481+ expect ( jsonResult . value . message ) . toContain ( 'changes' )
482+ expect ( jsonResult . value . unifiedDiff ) . toContain ( '+export function multiply' )
475483 } )
476484 } )
477485
@@ -498,10 +506,12 @@ export function multiply(a: number, b: number): number {
498506 } ] ,
499507 } ,
500508 }
509+ const step1First = step1 . toolResult ?. [ 0 ]
510+ const step1HasDiff = step1First ?. type === 'json' && ! ! ( step1First . value as { unifiedDiff ?: string } ) ?. unifiedDiff
501511 receivedToolResults . push ( {
502512 step : 1 ,
503513 toolResult : step1 . toolResult ,
504- hasUnifiedDiff : ! ! step1 . toolResult ?. [ 0 ] ?. value ?. unifiedDiff ,
514+ hasUnifiedDiff : step1HasDiff ,
505515 } )
506516
507517 // Second tool call - another propose_str_replace
@@ -516,10 +526,12 @@ export function multiply(a: number, b: number): number {
516526 } ] ,
517527 } ,
518528 }
529+ const step2First = step2 . toolResult ?. [ 0 ]
530+ const step2HasDiff = step2First ?. type === 'json' && ! ! ( step2First . value as { unifiedDiff ?: string } ) ?. unifiedDiff
519531 receivedToolResults . push ( {
520532 step : 2 ,
521533 toolResult : step2 . toolResult ,
522- hasUnifiedDiff : ! ! step2 . toolResult ?. [ 0 ] ?. value ?. unifiedDiff ,
534+ hasUnifiedDiff : step2HasDiff ,
523535 } )
524536
525537 // Third tool call - propose_write_file
@@ -531,10 +543,12 @@ export function multiply(a: number, b: number): number {
531543 content : 'export const newFile = true;' ,
532544 } ,
533545 }
546+ const step3First = step3 . toolResult ?. [ 0 ]
547+ const step3HasDiff = step3First ?. type === 'json' && ! ! ( step3First . value as { unifiedDiff ?: string } ) ?. unifiedDiff
534548 receivedToolResults . push ( {
535549 step : 3 ,
536550 toolResult : step3 . toolResult ,
537- hasUnifiedDiff : ! ! step3 . toolResult ?. [ 0 ] ?. value ?. unifiedDiff ,
551+ hasUnifiedDiff : step3HasDiff ,
538552 } )
539553
540554 yield { toolName : 'end_turn' , input : { } }
@@ -553,22 +567,25 @@ export function multiply(a: number, b: number): number {
553567 expect ( receivedToolResults [ 0 ] . step ) . toBe ( 1 )
554568 expect ( receivedToolResults [ 0 ] . toolResult ) . toBeDefined ( )
555569 expect ( receivedToolResults [ 0 ] . hasUnifiedDiff ) . toBe ( true )
556- expect ( receivedToolResults [ 0 ] . toolResult [ 0 ] . value . file ) . toBe ( 'src/utils.ts' )
557- expect ( receivedToolResults [ 0 ] . toolResult [ 0 ] . value . unifiedDiff ) . toContain ( 'first change' )
570+ const step1Result = receivedToolResults [ 0 ] . toolResult [ 0 ] as { type : 'json' ; value : { file : string ; unifiedDiff : string } }
571+ expect ( step1Result . value . file ) . toBe ( 'src/utils.ts' )
572+ expect ( step1Result . value . unifiedDiff ) . toContain ( 'first change' )
558573
559574 // Step 2: Should have received tool result with unified diff
560575 expect ( receivedToolResults [ 1 ] . step ) . toBe ( 2 )
561576 expect ( receivedToolResults [ 1 ] . toolResult ) . toBeDefined ( )
562577 expect ( receivedToolResults [ 1 ] . hasUnifiedDiff ) . toBe ( true )
563- expect ( receivedToolResults [ 1 ] . toolResult [ 0 ] . value . file ) . toBe ( 'src/utils.ts' )
564- expect ( receivedToolResults [ 1 ] . toolResult [ 0 ] . value . unifiedDiff ) . toContain ( 'second change' )
578+ const step2Result = receivedToolResults [ 1 ] . toolResult [ 0 ] as { type : 'json' ; value : { file : string ; unifiedDiff : string } }
579+ expect ( step2Result . value . file ) . toBe ( 'src/utils.ts' )
580+ expect ( step2Result . value . unifiedDiff ) . toContain ( 'second change' )
565581
566582 // Step 3: Should have received tool result with unified diff for new file
567583 expect ( receivedToolResults [ 2 ] . step ) . toBe ( 3 )
568584 expect ( receivedToolResults [ 2 ] . toolResult ) . toBeDefined ( )
569585 expect ( receivedToolResults [ 2 ] . hasUnifiedDiff ) . toBe ( true )
570- expect ( receivedToolResults [ 2 ] . toolResult [ 0 ] . value . file ) . toBe ( 'src/new-file.ts' )
571- expect ( receivedToolResults [ 2 ] . toolResult [ 0 ] . value . message ) . toContain ( 'new file' )
586+ const step3Result = receivedToolResults [ 2 ] . toolResult [ 0 ] as { type : 'json' ; value : { file : string ; message : string } }
587+ expect ( step3Result . value . file ) . toBe ( 'src/new-file.ts' )
588+ expect ( step3Result . value . message ) . toContain ( 'new file' )
572589 } )
573590
574591 it ( 'should collect tool calls and results for output' , async ( ) => {
@@ -607,8 +624,9 @@ export function multiply(a: number, b: number): number {
607624 toolName : 'propose_str_replace' ,
608625 input : step1 ,
609626 } )
610- if ( step1 . toolResult ?. [ 0 ] ?. value ) {
611- capturedToolResults . push ( step1 . toolResult [ 0 ] . value )
627+ const step1First = step1 . toolResult ?. [ 0 ]
628+ if ( step1First ?. type === 'json' && step1First . value ) {
629+ capturedToolResults . push ( step1First . value )
612630 }
613631
614632 // Generate unified diffs string from captured results
0 commit comments