@@ -52,12 +52,12 @@ public static class EndpointRouteBuilder
5252 /// Maps the <see cref="OpenAIClient"/> endpoints.
5353 /// </summary>
5454 /// <param name="endpoints"><see cref="IEndpointRouteBuilder"/>.</param>
55- /// <param name="openAIClient "><see cref="OpenAIClient"/>.</param>
55+ /// <param name="client "><see cref="OpenAIClient"/>.</param>
5656 /// <param name="authenticationFilter"><see cref="IAuthenticationFilter"/>.</param>
5757 /// <param name="routePrefix">Optional, custom route prefix. i.e. '/openai'.</param>
58- public static void MapOpenAIEndpoints ( this IEndpointRouteBuilder endpoints , OpenAIClient openAIClient , IAuthenticationFilter authenticationFilter , string routePrefix = "" )
58+ public static void MapOpenAIEndpoints ( this IEndpointRouteBuilder endpoints , OpenAIClient client , IAuthenticationFilter authenticationFilter , string routePrefix = "" )
5959 {
60- endpoints . Map ( $ "{ routePrefix } { openAIClient . Settings . BaseRequest } {{**endpoint}}", HandleRequest ) ;
60+ endpoints . Map ( $ "{ routePrefix } { client . Settings . BaseRequest } {{**endpoint}}", HandleRequest ) ;
6161 return ;
6262
6363 async Task HandleRequest ( HttpContext httpContext , string endpoint )
@@ -80,13 +80,13 @@ async Task HandleRequest(HttpContext httpContext, string endpoint)
8080 modifiedQuery [ pair . Key ] = pair . Value . FirstOrDefault ( ) ;
8181 }
8282
83- if ( openAIClient . Settings . IsAzureOpenAI )
83+ if ( client . Settings . IsAzureOpenAI )
8484 {
85- modifiedQuery [ "api-version" ] = openAIClient . Settings . ApiVersion ;
85+ modifiedQuery [ "api-version" ] = client . Settings . ApiVersion ;
8686 }
8787
8888 var uri = new Uri ( string . Format (
89- openAIClient . Settings . BaseRequestUrlFormat ,
89+ client . Settings . BaseRequestUrlFormat ,
9090 QueryHelpers . AddQueryString ( endpoint , modifiedQuery )
9191 ) ) ;
9292
@@ -98,8 +98,10 @@ async Task HandleRequest(HttpContext httpContext, string endpoint)
9898 request . Content . Headers . ContentType = System . Net . Http . Headers . MediaTypeHeaderValue . Parse ( httpContext . Request . ContentType ) ;
9999 }
100100
101- var proxyResponse = await openAIClient . Client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , httpContext . RequestAborted ) . ConfigureAwait ( false ) ;
101+ var proxyResponse = await client . Client . SendAsync ( request , HttpCompletionOption . ResponseHeadersRead , httpContext . RequestAborted ) . ConfigureAwait ( false ) ;
102102 httpContext . Response . StatusCode = ( int ) proxyResponse . StatusCode ;
103+ httpContext . Response . ContentLength = proxyResponse . Content . Headers . ContentLength ;
104+ httpContext . Response . ContentType = proxyResponse . Content . Headers . ContentType ? . ToString ( ) ;
103105
104106 foreach ( var ( key , value ) in proxyResponse . Headers )
105107 {
@@ -112,11 +114,10 @@ async Task HandleRequest(HttpContext httpContext, string endpoint)
112114 if ( excludedHeaders . Contains ( key ) ) { continue ; }
113115 httpContext . Response . Headers [ key ] = value . ToArray ( ) ;
114116 }
115-
116- httpContext . Response . ContentType = proxyResponse . Content . Headers . ContentType ? . ToString ( ) ?? string . Empty ;
117117 const string streamingContent = "text/event-stream" ;
118118
119- if ( httpContext . Response . ContentType . Equals ( streamingContent ) )
119+ if ( httpContext . Response . ContentType != null &&
120+ httpContext . Response . ContentType . Equals ( streamingContent , StringComparison . OrdinalIgnoreCase ) )
120121 {
121122 var stream = await proxyResponse . Content . ReadAsStreamAsync ( ) . ConfigureAwait ( false ) ;
122123 await WriteServerStreamEventsAsync ( httpContext , stream ) . ConfigureAwait ( false ) ;
@@ -185,13 +186,13 @@ async Task ProcessWebSocketRequest(HttpContext httpContext, string endpoint)
185186
186187 using var hostWebsocket = new ClientWebSocket ( ) ;
187188
188- foreach ( var header in openAIClient . WebsocketHeaders )
189+ foreach ( var header in client . WebsocketHeaders )
189190 {
190191 hostWebsocket . Options . SetRequestHeader ( header . Key , header . Value ) ;
191192 }
192193
193194 var uri = new Uri ( string . Format (
194- openAIClient . Settings . BaseWebSocketUrlFormat ,
195+ client . Settings . BaseWebSocketUrlFormat ,
195196 $ "{ endpoint } { httpContext . Request . QueryString } "
196197 ) ) ;
197198 await hostWebsocket . ConnectAsync ( uri , httpContext . RequestAborted ) . ConfigureAwait ( false ) ;
0 commit comments