@@ -608,6 +608,224 @@ describe("OpenRouterHandler", () => {
608608 await expect ( iteration ) . rejects . toMatchObject ( { name : "AbortError" } )
609609 expect ( chunks ) . toContainEqual ( { type : "text" , text : "first" } )
610610 } )
611+ it ( "excludes reasoning for Gemini 2.5 Pro models by default" , async ( ) => {
612+ const handler = new OpenRouterHandler (
613+ makeApiHandlerOptions ( {
614+ ...mockOptions ,
615+ openRouterModelId : "google/gemini-2.5-pro-preview" ,
616+ } ) ,
617+ )
618+ const mockCreate = vitest
619+ . fn ( )
620+ . mockResolvedValue ( asyncStreamFrom ( [ { id : "1" , choices : [ { delta : { content : "ok" } } ] } ] ) )
621+ const client = handler [ "client" ] as unknown as { chat : { completions : { create : typeof mockCreate } } }
622+ client . chat = { completions : { create : mockCreate } }
623+
624+ const stream = handler . createMessage ( "system" , [ { role : "user" as const , content : "hi" } ] )
625+ await collectStream ( stream )
626+
627+ expect ( mockCreate ) . toHaveBeenCalledWith (
628+ expect . objectContaining ( { reasoning : { exclude : true } } ) ,
629+ expect . any ( Object ) ,
630+ )
631+ } )
632+
633+ it ( "uses user role for the system prompt with DeepSeek R1 models" , async ( ) => {
634+ const handler = new OpenRouterHandler (
635+ makeApiHandlerOptions ( {
636+ ...mockOptions ,
637+ openRouterModelId : "deepseek/deepseek-r1" ,
638+ } ) ,
639+ )
640+ const mockCreate = vitest
641+ . fn ( )
642+ . mockResolvedValue ( asyncStreamFrom ( [ { id : "1" , choices : [ { delta : { content : "ok" } } ] } ] ) )
643+ const client = handler [ "client" ] as unknown as { chat : { completions : { create : typeof mockCreate } } }
644+ client . chat = { completions : { create : mockCreate } }
645+
646+ const stream = handler . createMessage ( "system prompt" , [ { role : "user" as const , content : "hi" } ] )
647+ await collectStream ( stream )
648+
649+ const params = mockCreate . mock . calls [ 0 ] [ 0 ] as { messages : { role : string ; content : unknown } [ ] }
650+ expect ( params . messages [ 0 ] . role ) . toBe ( "user" )
651+ expect ( params . messages . map ( ( m ) => m . role ) ) . not . toContain ( "system" )
652+ } )
653+
654+ it ( "injects a fake encrypted reasoning block for Gemini tool calls without encrypted reasoning" , async ( ) => {
655+ const handler = new OpenRouterHandler (
656+ makeApiHandlerOptions ( {
657+ ...mockOptions ,
658+ openRouterModelId : "google/gemini-2.5-flash" ,
659+ } ) ,
660+ )
661+ const mockCreate = vitest
662+ . fn ( )
663+ . mockResolvedValue ( asyncStreamFrom ( [ { id : "1" , choices : [ { delta : { content : "ok" } } ] } ] ) )
664+ const client = handler [ "client" ] as unknown as { chat : { completions : { create : typeof mockCreate } } }
665+ client . chat = { completions : { create : mockCreate } }
666+
667+ // reasoning_details is an OpenRouter extension field round-tripped on assistant
668+ // messages; the Anthropic SDK types do not include it, hence the structural cast.
669+ const assistantMessage = {
670+ role : "assistant" as const ,
671+ content : [ { type : "tool_use" as const , id : "toolu_01" , name : "get_weather" , input : { city : "SF" } } ] ,
672+ reasoning_details : [ { type : "reasoning.text" , id : "toolu_01" , text : "thinking" , index : 0 } ] ,
673+ }
674+ const stream = handler . createMessage ( "system" , [
675+ assistantMessage as unknown as Anthropic . Messages . MessageParam ,
676+ ] )
677+ await collectStream ( stream )
678+
679+ const params = mockCreate . mock . calls [ 0 ] [ 0 ] as {
680+ messages : {
681+ role : string
682+ tool_calls ?: { id : string } [ ]
683+ reasoning_details ?: { type : string ; id : string ; data : string } [ ]
684+ } [ ]
685+ }
686+ const assistant = params . messages . find ( ( m ) => m . role === "assistant" )
687+ expect ( assistant ?. tool_calls ) . toHaveLength ( 1 )
688+ const encrypted = assistant ?. reasoning_details ?. find ( ( d ) => d . type === "reasoning.encrypted" )
689+ expect ( encrypted ) . toMatchObject ( {
690+ id : "toolu_01" ,
691+ data : "skip_thought_signature_validator" ,
692+ } )
693+ } )
694+
695+ it ( "accumulates and yields reasoning_details from streamed chunks" , async ( ) => {
696+ const handler = new OpenRouterHandler ( mockOptions )
697+ const mockCreate = vitest . fn ( ) . mockResolvedValue (
698+ asyncStreamFrom ( [
699+ { id : "1" , choices : [ { delta : { reasoning : "top-level thinking" } } ] } ,
700+ {
701+ id : "2" ,
702+ choices : [
703+ { delta : { reasoning_details : [ { type : "reasoning.text" , index : 0 , text : "thinking " } ] } } ,
704+ ] ,
705+ } ,
706+ {
707+ id : "3" ,
708+ choices : [
709+ {
710+ delta : {
711+ reasoning_details : [
712+ {
713+ type : "reasoning.text" ,
714+ index : 0 ,
715+ text : "more" ,
716+ id : "r1" ,
717+ format : "google-gemini-v1" ,
718+ signature : "sig" ,
719+ } ,
720+ ] ,
721+ } ,
722+ } ,
723+ ] ,
724+ } ,
725+ {
726+ id : "4" ,
727+ choices : [
728+ { delta : { reasoning_details : [ { type : "reasoning.summary" , index : 1 , summary : "sum" } ] } } ,
729+ ] ,
730+ } ,
731+ { id : "5" , choices : [ { delta : { content : "hello" } } ] } ,
732+ {
733+ id : "6" ,
734+ choices : [
735+ {
736+ delta : {
737+ reasoning_details : [ { type : "reasoning.summary" , index : 1 , summary : " more" } ] ,
738+ } ,
739+ } ,
740+ ] ,
741+ } ,
742+ {
743+ id : "7" ,
744+ choices : [
745+ { delta : { reasoning_details : [ { type : "reasoning.encrypted" , index : 2 , data : "enc-" } ] } } ,
746+ ] ,
747+ } ,
748+ {
749+ id : "8" ,
750+ choices : [
751+ {
752+ delta : {
753+ reasoning_details : [ { type : "reasoning.encrypted" , index : 2 , data : "rypted" } ] ,
754+ } ,
755+ } ,
756+ ] ,
757+ } ,
758+ ] ) ,
759+ )
760+ const client = handler [ "client" ] as unknown as { chat : { completions : { create : typeof mockCreate } } }
761+ client . chat = { completions : { create : mockCreate } }
762+
763+ const chunks = await collectStream (
764+ handler . createMessage ( "system" , [ { role : "user" as const , content : "hi" } ] ) ,
765+ )
766+
767+ expect ( chunks ) . toContainEqual ( { type : "reasoning" , text : "top-level thinking" } )
768+ expect ( chunks ) . toContainEqual ( { type : "reasoning" , text : "thinking " } )
769+ expect ( chunks ) . toContainEqual ( { type : "reasoning" , text : "sum" } )
770+ expect ( chunks ) . toContainEqual ( { type : "reasoning" , text : " more" } )
771+ expect ( chunks ) . toContainEqual ( { type : "text" , text : "hello" } )
772+
773+ const details = handler . getReasoningDetails ( )
774+ expect ( details ) . toHaveLength ( 3 )
775+ expect ( details ?. find ( ( d ) => d . type === "reasoning.summary" ) ?. summary ) . toBe ( "sum more" )
776+ expect ( details ?. find ( ( d ) => d . type === "reasoning.encrypted" ) ?. data ) . toBe ( "enc-rypted" )
777+ } )
778+
779+ it ( "rejects with AbortError when the external signal aborts during request creation" , async ( ) => {
780+ const handler = new OpenRouterHandler ( mockOptions )
781+ const controller = new AbortController ( )
782+
783+ const mockCreate = vitest
784+ . fn ( )
785+ . mockImplementation ( async ( _params : unknown , options ?: { signal ?: AbortSignal } ) => {
786+ // Emulate the OpenAI SDK: the pending request rejects when the signal aborts.
787+ await new Promise < void > ( ( resolve ) => {
788+ if ( options ?. signal ?. aborted ) {
789+ resolve ( )
790+ } else {
791+ options ?. signal ?. addEventListener ( "abort" , ( ) => resolve ( ) , { once : true } )
792+ }
793+ } )
794+ const abortError = new Error ( "The user aborted a request" )
795+ abortError . name = "AbortError"
796+ throw abortError
797+ } )
798+ const client = handler [ "client" ] as unknown as { chat : { completions : { create : typeof mockCreate } } }
799+ client . chat = { completions : { create : mockCreate } }
800+
801+ const metadata = makeCreateMessageMetadata ( { abortSignal : controller . signal } )
802+ const generator = handler . createMessage ( "system" , [ { role : "user" as const , content : "hi" } ] , metadata )
803+
804+ const nextPromise = generator . next ( )
805+ // Let the generator reach the pending create() call, then abort.
806+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
807+ controller . abort ( )
808+
809+ await expect ( nextPromise ) . rejects . toMatchObject ( { name : "AbortError" } )
810+ } )
811+
812+ it ( "reports OpenRouter structured errors in createMessage with telemetry" , async ( ) => {
813+ const handler = new OpenRouterHandler ( mockOptions )
814+ const mockCreate = vitest . fn ( ) . mockRejectedValueOnce ( {
815+ error : {
816+ message : "Model not found" ,
817+ code : 404 ,
818+ metadata : { raw : '{"message":"upstream: model not found"}' } ,
819+ } ,
820+ } )
821+ const client = handler [ "client" ] as unknown as { chat : { completions : { create : typeof mockCreate } } }
822+ client . chat = { completions : { create : mockCreate } }
823+
824+ const generator = handler . createMessage ( "system" , [ { role : "user" as const , content : "hi" } ] )
825+
826+ await expect ( generator . next ( ) ) . rejects . toThrow ( / c o m p l e t i o n e r r o r / )
827+ expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 )
828+ } )
611829 } )
612830
613831 describe ( "completePrompt" , ( ) => {
@@ -860,5 +1078,74 @@ describe("OpenRouterHandler", () => {
8601078
8611079 await expect ( promise ) . rejects . toMatchObject ( { name : "AbortError" } )
8621080 } )
1081+ it ( "rejects with AbortError when only a timeout is provided and it elapses" , async ( ) => {
1082+ // Non-Anthropic model: also exercises the no-beta-header branch of requestOptions.
1083+ const handler = new OpenRouterHandler (
1084+ makeApiHandlerOptions ( {
1085+ ...mockOptions ,
1086+ openRouterModelId : "openai/gpt-4o" ,
1087+ } ) ,
1088+ )
1089+ const mockCreate = vitest
1090+ . fn ( )
1091+ . mockImplementation ( async ( _params : unknown , options ?: { signal ?: AbortSignal } ) => {
1092+ // Emulate the OpenAI SDK: the in-flight request rejects when the signal times out.
1093+ await new Promise < void > ( ( resolve ) => {
1094+ if ( options ?. signal ?. aborted ) {
1095+ resolve ( )
1096+ } else {
1097+ options ?. signal ?. addEventListener ( "abort" , ( ) => resolve ( ) , { once : true } )
1098+ }
1099+ } )
1100+ const timeoutError = new Error ( "TimeoutError: Request timed out." )
1101+ timeoutError . name = "TimeoutError"
1102+ throw timeoutError
1103+ } )
1104+ const client = handler [ "client" ] as unknown as { chat : { completions : { create : typeof mockCreate } } }
1105+ client . chat = { completions : { create : mockCreate } }
1106+
1107+ await expect ( handler . completePrompt ( "test prompt" , { timeoutMs : 50 } ) ) . rejects . toMatchObject ( {
1108+ name : "AbortError" ,
1109+ } )
1110+ } )
1111+
1112+ it ( "rejects with AbortError when both an abort signal and a timeout are provided" , async ( ) => {
1113+ const handler = new OpenRouterHandler ( mockOptions )
1114+ const controller = new AbortController ( )
1115+
1116+ let requestSignal : AbortSignal | undefined
1117+ const mockCreate = vitest
1118+ . fn ( )
1119+ . mockImplementation ( async ( _params : unknown , options ?: { signal ?: AbortSignal } ) => {
1120+ requestSignal = options ?. signal
1121+ await new Promise < void > ( ( resolve ) => {
1122+ if ( options ?. signal ?. aborted ) {
1123+ resolve ( )
1124+ } else {
1125+ options ?. signal ?. addEventListener ( "abort" , ( ) => resolve ( ) , { once : true } )
1126+ }
1127+ } )
1128+ const abortError = new Error ( "The user aborted a request" )
1129+ abortError . name = "AbortError"
1130+ throw abortError
1131+ } )
1132+ const client = handler [ "client" ] as unknown as { chat : { completions : { create : typeof mockCreate } } }
1133+ client . chat = { completions : { create : mockCreate } }
1134+
1135+ const promise = handler . completePrompt ( "test prompt" , {
1136+ abortSignal : controller . signal ,
1137+ timeoutMs : 100_000 ,
1138+ } )
1139+ controller . abort ( )
1140+
1141+ await expect ( promise ) . rejects . toMatchObject ( { name : "AbortError" } )
1142+ // The SDK received a merged signal (not the caller's signal) plus the timeout.
1143+ expect ( requestSignal ) . toBeDefined ( )
1144+ expect ( requestSignal ) . not . toBe ( controller . signal )
1145+ expect ( mockCreate ) . toHaveBeenCalledWith (
1146+ expect . objectContaining ( { model : expect . any ( String ) } ) ,
1147+ expect . objectContaining ( { timeout : 100_000 } ) ,
1148+ )
1149+ } )
8631150 } )
8641151} )
0 commit comments