@@ -8,6 +8,7 @@ import type { ProviderPackage } from "../provider-package.js"
88import { SystemOne } from "../protocols/system-one.js"
99import { OpenAIImages , type OpenAIImageSizing } from "../protocols/openai-images.js"
1010import { MediaRoute } from "../route/media.js"
11+ import { HttpTransport } from "../route/transport/index.js"
1112import { OpenAIChat } from "../protocols/openai-chat.js"
1213import { OpenResponses } from "../protocols/open-responses.js"
1314import { cacheControl } from "../protocols/utils/cache.js"
@@ -74,6 +75,9 @@ export interface OpenRouterOptions {
7475
7576export type OpenRouterProviderOptionsInput = OpenRouterOptions
7677
78+ export type OpenRouterResponsesProviderOptionsInput = OpenResponsesProviderOptionsInput &
79+ Pick < OpenRouterOptions , "debug" | "models" | "plugins" | "provider" | "user" >
80+
7781export interface OpenRouterDecisionOptions {
7882 readonly [ key : string ] : unknown
7983 readonly provider ?: OpenRouterProviderRouting
@@ -85,7 +89,7 @@ export interface OpenRouterDecisionOptions {
8589export type Config = Omit < RouteDefaultsInput , "providerOptions" > &
8690 ProviderAuthOption < "optional" > & {
8791 readonly baseURL ?: string
88- readonly providerOptions ?: OpenRouterProviderOptionsInput | OpenResponsesProviderOptionsInput
92+ readonly providerOptions ?: OpenRouterProviderOptionsInput | OpenRouterResponsesProviderOptionsInput
8993 }
9094
9195export type Settings < Options = OpenRouterProviderOptionsInput > = ProviderPackage . Settings &
@@ -94,14 +98,18 @@ export type Settings<Options = OpenRouterProviderOptionsInput> = ProviderPackage
9498 readonly baseURL ?: string
9599 }
96100
97- const Options = Schema . Struct ( {
98- usage : lenient ( Schema . Union ( [ Schema . Boolean , JsonObject ] ) ) ,
101+ const SharedOptions = Schema . Struct ( {
99102 models : lenient ( Schema . Array ( Schema . String ) ) ,
100103 provider : lenient ( JsonObject ) ,
101104 plugins : lenient ( Schema . Array ( JsonObject ) ) ,
102- web_search_options : lenient ( JsonObject ) ,
103105 debug : lenient ( JsonObject ) ,
104106 user : lenient ( Schema . String ) ,
107+ } )
108+
109+ const Options = Schema . Struct ( {
110+ ...SharedOptions . fields ,
111+ usage : lenient ( Schema . Union ( [ Schema . Boolean , JsonObject ] ) ) ,
112+ web_search_options : lenient ( JsonObject ) ,
105113 reasoning : lenient ( JsonObject ) ,
106114} )
107115const decodeOptions = Schema . decodeUnknownSync ( Options )
@@ -166,12 +174,8 @@ const bodyOptions = (body: OpenAIChat.OpenAIChatBody, input: unknown, maxTokens:
166174 ...options ,
167175 ...body ,
168176 ...( usage !== undefined ? { usage } : { } ) ,
169- ...( known . models !== undefined ? { models : known . models } : { } ) ,
170- ...( known . provider !== undefined ? { provider : known . provider } : { } ) ,
171- ...( known . plugins !== undefined ? { plugins : known . plugins } : { } ) ,
177+ ...sharedOptions ( known ) ,
172178 ...( known . web_search_options !== undefined ? { web_search_options : known . web_search_options } : { } ) ,
173- ...( known . debug !== undefined ? { debug : known . debug } : { } ) ,
174- ...( known . user !== undefined ? { user : known . user } : { } ) ,
175179 ...( known . reasoning !== undefined ? { reasoning : fitReasoning ( known . reasoning , maxTokens ) } : { } ) ,
176180 }
177181}
@@ -191,6 +195,32 @@ const imageSizing: OpenAIImageSizing = (request) => {
191195 }
192196}
193197
198+ const sharedOptions = ( known : typeof SharedOptions . Type ) => ( {
199+ ...( known . models !== undefined ? { models : known . models } : { } ) ,
200+ ...( known . provider !== undefined ? { provider : known . provider } : { } ) ,
201+ ...( known . plugins !== undefined ? { plugins : known . plugins } : { } ) ,
202+ ...( known . debug !== undefined ? { debug : known . debug } : { } ) ,
203+ ...( known . user !== undefined ? { user : known . user } : { } ) ,
204+ } )
205+
206+ const OpenRouterResponsesBody = Schema . Struct ( {
207+ ...OpenResponses . OpenResponsesBody . fields ,
208+ ...SharedOptions . fields ,
209+ } )
210+ type OpenRouterResponsesBody = Schema . Schema . Type < typeof OpenRouterResponsesBody >
211+
212+ const responsesProtocol = Protocol . make ( {
213+ id : "openrouter-responses" ,
214+ body : {
215+ schema : OpenRouterResponsesBody ,
216+ from : ( request ) =>
217+ OpenResponses . fromRequest ( request ) . pipe (
218+ Effect . map ( ( body ) => ( { ...body , ...sharedOptions ( decodeOptions ( request . providerOptions ?? { } ) ) } ) ) ,
219+ ) ,
220+ } ,
221+ stream : OpenResponses . protocol . stream ,
222+ } )
223+
194224export const route = Route . make ( {
195225 id : ADAPTER ,
196226 provider : id ,
@@ -204,9 +234,9 @@ const responsesRoute = Route.make({
204234 id : "openrouter-responses" ,
205235 provider : id ,
206236 providerMetadataKey : "openrouter" ,
207- protocol : OpenResponses . protocol ,
237+ protocol : responsesProtocol ,
208238 endpoint : Endpoint . path ( "/responses" , { baseURL } ) ,
209- transport : OpenResponses . httpTransport ,
239+ transport : HttpTransport . sseJson . with < OpenRouterResponsesBody > ( ) ,
210240 defaults : { providerOptions : { store : false , include : [ "reasoning.encrypted_content" ] } } ,
211241} )
212242
@@ -247,7 +277,7 @@ export const configure = (input: Config = {}) => {
247277 model : ( modelID : string | ModelID ) =>
248278 chatRoute . model < OpenRouterProviderOptionsInput > ( { id : modelID , compatibility : { supportsPromptCacheKey : true } } ) ,
249279 responses : ( modelID : string | ModelID ) =>
250- configuredResponses . model < OpenResponsesProviderOptionsInput > ( { id : modelID } ) ,
280+ configuredResponses . model < OpenRouterResponsesProviderOptionsInput > ( { id : modelID } ) ,
251281 image,
252282 decision,
253283 configure,
@@ -261,8 +291,8 @@ export const responses = provider.responses
261291export const model : ProviderPackage . Model < Settings , OpenRouterProviderOptionsInput > = ( modelID , settings ) =>
262292 configure ( config ( settings ) ) . model ( modelID )
263293export const responsesModel : ProviderPackage . Model <
264- Settings < OpenResponsesProviderOptionsInput > ,
265- OpenResponsesProviderOptionsInput
294+ Settings < OpenRouterResponsesProviderOptionsInput > ,
295+ OpenRouterResponsesProviderOptionsInput
266296> = ( modelID , settings ) => configure ( config ( settings ) ) . responses ( modelID )
267297export const imageModel = ( modelID : string , settings ?: Settings ) => {
268298 const input = config ( settings )
0 commit comments