Skip to content

Commit 8c1af34

Browse files
committed
Removed unused vars
1 parent 88509bc commit 8c1af34

File tree

5 files changed

+7
-223
lines changed

5 files changed

+7
-223
lines changed

src/handlers/handlerUtils.ts

Lines changed: 3 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
WORKERS_AI,
66
HEADER_KEYS,
77
POWERED_BY,
8-
RESPONSE_HEADER_KEYS,
98
GOOGLE_VERTEX_AI,
109
OPEN_AI,
1110
AZURE_AI_INFERENCE,
@@ -17,14 +16,13 @@ import {
1716
FIREWORKS_AI,
1817
CORTEX,
1918
} from '../globals';
20-
import Providers from '../providers';
2119
import { endpointStrings } from '../providers/types';
2220
import { Options, Params, StrategyModes, Targets } from '../types/requestBody';
2321
import { convertKeysToCamelCase } from '../utils';
2422
import { retryRequest } from './retryHandler';
25-
import { env, getRuntimeKey } from 'hono/adapter';
23+
import { env } from 'hono/adapter';
2624
import { afterRequestHookHandler, responseHandler } from './responseHandlers';
27-
import { HookSpan, HooksManager } from '../middlewares/hooks';
25+
import { HookSpan } from '../middlewares/hooks';
2826
import { ConditionalRouter } from '../services/conditionalRouter';
2927
import { RouterError } from '../errors/RouterError';
3028
import { GatewayError } from '../errors/GatewayError';
@@ -86,7 +84,6 @@ function constructRequestHeaders(
8684
requestHeaders,
8785
endpoint: fn,
8886
honoContext: c,
89-
provider: provider,
9087
} = requestContext;
9188

9289
const proxyHeaders: Record<string, string> = {};
@@ -197,41 +194,6 @@ export function constructRequest(
197194
return fetchOptions;
198195
}
199196

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-
235197
/**
236198
* Selects a provider based on their assigned weights.
237199
* The weight is used to determine the probability of each provider being chosen.
@@ -342,12 +304,7 @@ export async function tryPost(
342304
const hooksService = new HooksService(requestContext);
343305
const providerContext = new ProviderContext(requestContext.provider);
344306
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);
351308
const hookSpan: HookSpan = hooksService.hookSpan;
352309

353310
// Set the requestURL in requestContext
@@ -837,57 +794,6 @@ export async function tryTargetsRecursively(
837794
return response;
838795
}
839796

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-
891797
export function constructConfigFromRequestHeaders(
892798
requestHeaders: Record<string, any>
893799
): Options | Targets {
@@ -1290,120 +1196,6 @@ export async function recursiveAfterRequestHookHandler(
12901196
};
12911197
}
12921198

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-
14071199
export async function beforeRequestHookHandler(
14081200
c: Context,
14091201
hookSpanId: string

src/handlers/services/hooksService.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// hooksService.ts
22

3-
import { HookSpan } from '../../middlewares/hooks';
3+
import { HookSpan, HooksManager } from '../../middlewares/hooks';
44
import { RequestContext } from './requestContext';
5-
import { HooksManager } from '../../middlewares/hooks';
65
import { AllHookResults } from '../../middlewares/hooks/types';
76

87
export class HooksService {

src/handlers/services/providerContext.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import {
77
} from '../../providers/types';
88
import Providers from '../../providers';
99
import { RequestContext } from './requestContext';
10-
import { ANTHROPIC } from '../../globals';
11-
import { AZURE_OPEN_AI } from '../../globals';
10+
import { ANTHROPIC, AZURE_OPEN_AI } from '../../globals';
1211

1312
export class ProviderContext {
1413
constructor(private provider: string) {

src/handlers/services/requestContext.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { HooksManager } from '../../middlewares/hooks';
1414
import { transformToProviderRequest } from '../../services/transformToProviderRequest';
1515

1616
export class RequestContext {
17-
private originalRequestParams: any;
1817
private _params: Params | null = null;
1918
private _transformedRequestBody: any;
2019
public readonly providerOption: Options;

src/handlers/services/responseService.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
// responseService.ts
22

3-
import { HEADER_KEYS, POWERED_BY } from '../../globals';
4-
import { RESPONSE_HEADER_KEYS } from '../../globals';
3+
import { HEADER_KEYS, POWERED_BY, RESPONSE_HEADER_KEYS } from '../../globals';
54
import { responseHandler } from '../responseHandlers';
65
import { HooksService } from './hooksService';
7-
import { ProviderContext } from './providerContext';
86
import { RequestContext } from './requestContext';
9-
import { LogsService } from './logsService';
107

118
interface CreateResponseOptions {
129
response: Response;
@@ -27,9 +24,7 @@ interface CreateResponseOptions {
2724
export class ResponseService {
2825
constructor(
2926
private context: RequestContext,
30-
private providerContext: ProviderContext,
31-
private hooksService: HooksService,
32-
private logsService: LogsService
27+
private hooksService: HooksService
3328
) {}
3429

3530
async create(options: CreateResponseOptions): Promise<{

0 commit comments

Comments
 (0)