5
5
WORKERS_AI ,
6
6
HEADER_KEYS ,
7
7
POWERED_BY ,
8
- RESPONSE_HEADER_KEYS ,
9
8
GOOGLE_VERTEX_AI ,
10
9
OPEN_AI ,
11
10
AZURE_AI_INFERENCE ,
@@ -17,14 +16,13 @@ import {
17
16
FIREWORKS_AI ,
18
17
CORTEX ,
19
18
} from '../globals' ;
20
- import Providers from '../providers' ;
21
19
import { endpointStrings } from '../providers/types' ;
22
20
import { Options , Params , StrategyModes , Targets } from '../types/requestBody' ;
23
21
import { convertKeysToCamelCase } from '../utils' ;
24
22
import { retryRequest } from './retryHandler' ;
25
- import { env , getRuntimeKey } from 'hono/adapter' ;
23
+ import { env } from 'hono/adapter' ;
26
24
import { afterRequestHookHandler , responseHandler } from './responseHandlers' ;
27
- import { HookSpan , HooksManager } from '../middlewares/hooks' ;
25
+ import { HookSpan } from '../middlewares/hooks' ;
28
26
import { ConditionalRouter } from '../services/conditionalRouter' ;
29
27
import { RouterError } from '../errors/RouterError' ;
30
28
import { GatewayError } from '../errors/GatewayError' ;
@@ -86,7 +84,6 @@ function constructRequestHeaders(
86
84
requestHeaders,
87
85
endpoint : fn ,
88
86
honoContext : c ,
89
- provider : provider ,
90
87
} = requestContext ;
91
88
92
89
const proxyHeaders : Record < string , string > = { } ;
@@ -197,41 +194,6 @@ export function constructRequest(
197
194
return fetchOptions ;
198
195
}
199
196
200
- function getProxyPath (
201
- requestURL : string ,
202
- proxyProvider : string ,
203
- proxyEndpointPath : string ,
204
- baseURL : string ,
205
- providerOptions : Options
206
- ) {
207
- let reqURL = new URL ( requestURL ) ;
208
- let reqPath = reqURL . pathname ;
209
- const reqQuery = reqURL . search ;
210
- reqPath = reqPath . replace ( proxyEndpointPath , '' ) ;
211
-
212
- // NOTE: temporary support for the deprecated way of making azure requests
213
- // where the endpoint was sent in request path of the incoming gateway url
214
- if (
215
- proxyProvider === AZURE_OPEN_AI &&
216
- reqPath . includes ( '.openai.azure.com' )
217
- ) {
218
- return `https:/${ reqPath } ${ reqQuery } ` ;
219
- }
220
-
221
- if ( Providers [ proxyProvider ] ?. api ?. getProxyEndpoint ) {
222
- return `${ baseURL } ${ Providers [ proxyProvider ] . api . getProxyEndpoint ( { reqPath, reqQuery, providerOptions } ) } ` ;
223
- }
224
-
225
- let proxyPath = `${ baseURL } ${ reqPath } ${ reqQuery } ` ;
226
-
227
- // Fix specific for Anthropic SDK calls. Is this needed? - Yes
228
- if ( proxyProvider === ANTHROPIC ) {
229
- proxyPath = proxyPath . replace ( '/v1/v1/' , '/v1/' ) ;
230
- }
231
-
232
- return proxyPath ;
233
- }
234
-
235
197
/**
236
198
* Selects a provider based on their assigned weights.
237
199
* The weight is used to determine the probability of each provider being chosen.
@@ -342,12 +304,7 @@ export async function tryPost(
342
304
const hooksService = new HooksService ( requestContext ) ;
343
305
const providerContext = new ProviderContext ( requestContext . provider ) ;
344
306
const logsService = new LogsService ( c ) ;
345
- const responseService = new ResponseService (
346
- requestContext ,
347
- providerContext ,
348
- hooksService ,
349
- logsService
350
- ) ;
307
+ const responseService = new ResponseService ( requestContext , hooksService ) ;
351
308
const hookSpan : HookSpan = hooksService . hookSpan ;
352
309
353
310
// Set the requestURL in requestContext
@@ -837,57 +794,6 @@ export async function tryTargetsRecursively(
837
794
return response ;
838
795
}
839
796
840
- /**
841
- * @deprecated
842
- * Updates the response headers with the provided values.
843
- * @param {Response } response - The response object.
844
- * @param {string | number } currentIndex - The current index value.
845
- * @param {Record<string, any> } params - The parameters object.
846
- * @param {string } cacheStatus - The cache status value.
847
- * @param {number } retryAttempt - The retry attempt count.
848
- * @param {string } traceId - The trace ID value.
849
- */
850
- function updateResponseHeaders (
851
- response : Response ,
852
- currentIndex : string | number ,
853
- params : Record < string , any > ,
854
- cacheStatus : string | undefined ,
855
- retryAttempt : number ,
856
- traceId : string ,
857
- provider : string
858
- ) {
859
- response . headers . append (
860
- RESPONSE_HEADER_KEYS . LAST_USED_OPTION_INDEX ,
861
- currentIndex . toString ( )
862
- ) ;
863
-
864
- if ( cacheStatus ) {
865
- response . headers . append ( RESPONSE_HEADER_KEYS . CACHE_STATUS , cacheStatus ) ;
866
- }
867
- response . headers . append ( RESPONSE_HEADER_KEYS . TRACE_ID , traceId ) ;
868
- response . headers . append (
869
- RESPONSE_HEADER_KEYS . RETRY_ATTEMPT_COUNT ,
870
- retryAttempt . toString ( )
871
- ) ;
872
-
873
- // const contentEncodingHeader = response.headers.get('content-encoding');
874
- // if (contentEncodingHeader && contentEncodingHeader.indexOf('br') > -1) {
875
- // // Brotli compression causes errors at runtime, removing the header in that case
876
- // response.headers.delete('content-encoding');
877
- // }
878
- // if (getRuntimeKey() == 'node') {
879
- // response.headers.delete('content-encoding');
880
- // }
881
-
882
- // Delete content-length header to avoid conflicts with hono compress middleware
883
- // workerd environment handles this authomatically
884
- response . headers . delete ( 'content-length' ) ;
885
- // response.headers.delete('transfer-encoding');
886
- if ( provider && provider !== POWERED_BY ) {
887
- response . headers . append ( HEADER_KEYS . PROVIDER , provider ) ;
888
- }
889
- }
890
-
891
797
export function constructConfigFromRequestHeaders (
892
798
requestHeaders : Record < string , any >
893
799
) : Options | Targets {
@@ -1290,120 +1196,6 @@ export async function recursiveAfterRequestHookHandler(
1290
1196
} ;
1291
1197
}
1292
1198
1293
- /**
1294
- * Retrieves the cache options based on the provided cache configuration.
1295
- * @param cacheConfig - The cache configuration object or string.
1296
- * @returns An object containing the cache mode and cache max age.
1297
- */
1298
- function getCacheOptions ( cacheConfig : any ) {
1299
- // providerOption.cache needs to be sent here
1300
- let cacheMode : string | undefined ;
1301
- let cacheMaxAge : string | number = '' ;
1302
- let cacheStatus = 'DISABLED' ;
1303
-
1304
- if ( typeof cacheConfig === 'object' && cacheConfig ?. mode ) {
1305
- cacheMode = cacheConfig . mode ;
1306
- cacheMaxAge = cacheConfig . maxAge ;
1307
- } else if ( typeof cacheConfig === 'string' ) {
1308
- cacheMode = cacheConfig ;
1309
- }
1310
- return { cacheMode, cacheMaxAge, cacheStatus } ;
1311
- }
1312
-
1313
- async function cacheHandler (
1314
- c : Context ,
1315
- providerOption : Options ,
1316
- requestHeaders : Record < string , string > ,
1317
- fetchOptions : any ,
1318
- transformedRequestBody : any ,
1319
- hookSpanId : string ,
1320
- fn : endpointStrings
1321
- ) {
1322
- if (
1323
- [
1324
- 'uploadFile' ,
1325
- 'listFiles' ,
1326
- 'retrieveFile' ,
1327
- 'deleteFile' ,
1328
- 'retrieveFileContent' ,
1329
- 'createBatch' ,
1330
- 'retrieveBatch' ,
1331
- 'cancelBatch' ,
1332
- 'listBatches' ,
1333
- 'getBatchOutput' ,
1334
- 'listFinetunes' ,
1335
- 'createFinetune' ,
1336
- 'retrieveFinetune' ,
1337
- 'cancelFinetune' ,
1338
- ] . includes ( fn )
1339
- ) {
1340
- return {
1341
- cacheResponse : undefined ,
1342
- cacheStatus : 'DISABLED' ,
1343
- cacheKey : undefined ,
1344
- createdAt : new Date ( ) ,
1345
- executionTime : 0 ,
1346
- } ;
1347
- }
1348
- const start = new Date ( ) ;
1349
- const [ getFromCacheFunction , cacheIdentifier ] = [
1350
- c . get ( 'getFromCache' ) ,
1351
- c . get ( 'cacheIdentifier' ) ,
1352
- ] ;
1353
-
1354
- let cacheResponse , cacheKey ;
1355
- let cacheMode : string | undefined ,
1356
- cacheMaxAge : string | number | undefined ,
1357
- cacheStatus : string ;
1358
- ( { cacheMode, cacheMaxAge, cacheStatus } = getCacheOptions (
1359
- providerOption . cache
1360
- ) ) ;
1361
-
1362
- if ( getFromCacheFunction && cacheMode ) {
1363
- [ cacheResponse , cacheStatus , cacheKey ] = await getFromCacheFunction (
1364
- env ( c ) ,
1365
- { ...requestHeaders , ...fetchOptions . headers } ,
1366
- transformedRequestBody ,
1367
- fn ,
1368
- cacheIdentifier ,
1369
- cacheMode ,
1370
- cacheMaxAge
1371
- ) ;
1372
- }
1373
-
1374
- const hooksManager = c . get ( 'hooksManager' ) as HooksManager ;
1375
- const span = hooksManager . getSpan ( hookSpanId ) as HookSpan ;
1376
- const results = span . getHooksResult ( ) ;
1377
- const failedBeforeRequestHooks = results . beforeRequestHooksResult ?. filter (
1378
- ( h ) => ! h . verdict
1379
- ) ;
1380
-
1381
- let responseBody = cacheResponse ;
1382
-
1383
- const hasHookResults = results . beforeRequestHooksResult ?. length > 0 ;
1384
- const responseStatus = failedBeforeRequestHooks . length ? 246 : 200 ;
1385
-
1386
- if ( hasHookResults && cacheResponse ) {
1387
- responseBody = JSON . stringify ( {
1388
- ...JSON . parse ( cacheResponse ) ,
1389
- hook_results : {
1390
- before_request_hooks : results . beforeRequestHooksResult ,
1391
- } ,
1392
- } ) ;
1393
- }
1394
- return {
1395
- cacheResponse : ! ! cacheResponse
1396
- ? new Response ( responseBody , {
1397
- headers : { 'content-type' : 'application/json' } ,
1398
- status : responseStatus ,
1399
- } )
1400
- : undefined ,
1401
- cacheStatus,
1402
- cacheKey,
1403
- createdAt : start ,
1404
- } ;
1405
- }
1406
-
1407
1199
export async function beforeRequestHookHandler (
1408
1200
c : Context ,
1409
1201
hookSpanId : string
0 commit comments