@@ -736,17 +736,90 @@ describe('AgentBlockHandler', () => {
736736 } )
737737 } )
738738
739- it ( 'should throw an error for invalid JSON in responseFormat' , async ( ) => {
739+ it ( 'should handle invalid JSON in responseFormat gracefully' , async ( ) => {
740+ mockFetch . mockImplementationOnce ( ( ) => {
741+ return Promise . resolve ( {
742+ ok : true ,
743+ headers : {
744+ get : ( name : string ) => {
745+ if ( name === 'Content-Type' ) return 'application/json'
746+ if ( name === 'X-Execution-Data' ) return null
747+ return null
748+ } ,
749+ } ,
750+ json : ( ) =>
751+ Promise . resolve ( {
752+ content : 'Regular text response' ,
753+ model : 'mock-model' ,
754+ tokens : { prompt : 10 , completion : 20 , total : 30 } ,
755+ timing : { total : 100 } ,
756+ toolCalls : [ ] ,
757+ cost : undefined ,
758+ } ) ,
759+ } )
760+ } )
761+
740762 const inputs = {
741763 model : 'gpt-4o' ,
742764 userPrompt : 'Format this output.' ,
743765 apiKey : 'test-api-key' ,
744766 responseFormat : '{invalid-json' ,
745767 }
746768
747- await expect ( handler . execute ( mockBlock , inputs , mockContext ) ) . rejects . toThrow (
748- 'Invalid response'
749- )
769+ // Should not throw an error, but continue with default behavior
770+ const result = await handler . execute ( mockBlock , inputs , mockContext )
771+
772+ expect ( result ) . toEqual ( {
773+ content : 'Regular text response' ,
774+ model : 'mock-model' ,
775+ tokens : { prompt : 10 , completion : 20 , total : 30 } ,
776+ toolCalls : { list : [ ] , count : 0 } ,
777+ providerTiming : { total : 100 } ,
778+ cost : undefined ,
779+ } )
780+ } )
781+
782+ it ( 'should handle variable references in responseFormat gracefully' , async ( ) => {
783+ mockFetch . mockImplementationOnce ( ( ) => {
784+ return Promise . resolve ( {
785+ ok : true ,
786+ headers : {
787+ get : ( name : string ) => {
788+ if ( name === 'Content-Type' ) return 'application/json'
789+ if ( name === 'X-Execution-Data' ) return null
790+ return null
791+ } ,
792+ } ,
793+ json : ( ) =>
794+ Promise . resolve ( {
795+ content : 'Regular text response' ,
796+ model : 'mock-model' ,
797+ tokens : { prompt : 10 , completion : 20 , total : 30 } ,
798+ timing : { total : 100 } ,
799+ toolCalls : [ ] ,
800+ cost : undefined ,
801+ } ) ,
802+ } )
803+ } )
804+
805+ const inputs = {
806+ model : 'gpt-4o' ,
807+ userPrompt : 'Format this output.' ,
808+ apiKey : 'test-api-key' ,
809+ responseFormat : '<start.input>' ,
810+ }
811+
812+ // Should not throw an error, but continue with default behavior
813+ const result = await handler . execute ( mockBlock , inputs , mockContext )
814+
815+ expect ( result ) . toEqual ( {
816+ content : 'Regular text response' ,
817+ model : 'mock-model' ,
818+ tokens : { prompt : 10 , completion : 20 , total : 30 } ,
819+ toolCalls : { list : [ ] , count : 0 } ,
820+ providerTiming : { total : 100 } ,
821+ cost : undefined ,
822+ } )
750823 } )
751824
752825 it ( 'should handle errors from the provider request' , async ( ) => {
0 commit comments