@@ -485,4 +485,53 @@ describe("session.message-v2.fromError", () => {
485485 message : "An error occurred while processing your request." ,
486486 } )
487487 } )
488+
489+ test ( "converts nested OpenAI response.failed server errors to retryable APIError" , ( ) => {
490+ const cases = [ "server_error" , "server_is_overloaded" ]
491+
492+ cases . forEach ( ( code ) => {
493+ const body = {
494+ type : "response.failed" ,
495+ sequence_number : 2 ,
496+ response : {
497+ status : "failed" ,
498+ error : {
499+ code,
500+ message : `OpenAI ${ code } ` ,
501+ } ,
502+ } ,
503+ }
504+ const result = MessageV2 . fromError (
505+ { message : JSON . stringify ( body ) } ,
506+ { providerID : ProviderV2 . ID . make ( "openai" ) } ,
507+ )
508+
509+ expect ( SessionV1 . APIError . isInstance ( result ) ) . toBe ( true )
510+ if ( ! SessionV1 . APIError . isInstance ( result ) ) throw new Error ( "expected APIError" )
511+ expect ( result . data . isRetryable ) . toBe ( true )
512+ expect ( result . data . responseBody ) . toBe ( JSON . stringify ( body ) )
513+ expect ( SessionRetry . retryable ( result , retryProvider ) ) . toEqual ( { message : `OpenAI ${ code } ` } )
514+ } )
515+ } )
516+
517+ test ( "leaves other nested OpenAI response.failed errors terminal" , ( ) => {
518+ const result = MessageV2 . fromError (
519+ {
520+ message : JSON . stringify ( {
521+ type : "response.failed" ,
522+ response : {
523+ status : "failed" ,
524+ error : {
525+ code : "invalid_prompt" ,
526+ message : "Invalid prompt from test" ,
527+ } ,
528+ } ,
529+ } ) ,
530+ } ,
531+ { providerID : ProviderV2 . ID . make ( "openai" ) } ,
532+ )
533+
534+ expect ( SessionV1 . APIError . isInstance ( result ) ) . toBe ( false )
535+ expect ( SessionRetry . retryable ( result , retryProvider ) ) . toBeUndefined ( )
536+ } )
488537} )
0 commit comments