All files / api/src/utils cache.ts

5.45% Statements 3/55
100% Branches 0/0
0% Functions 0/1
5.45% Lines 3/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71  1x         1x 1x                                                                                                                              
import type { AppContext } from '@api/types/hono';
import {
  type CommonRequestOptions,
  type CreateResponseOptions,
  createResponse,
} from '@api/utils/super-agents/responses';
import { FunctionName } from '@shared/types/api/request';
import { CacheStatus } from '@shared/types/middleware/cache';
 
export async function getCachedResponse(
  c: AppContext,
  commonRequestOptions: CommonRequestOptions,
  aiProviderRequestBody:
    | Record<string, unknown>
    | ReadableStream
    | FormData
    | ArrayBuffer,
): Promise<Response | undefined> {
  // Unsupported functions are not cached
  if (
    [
      FunctionName.UPLOAD_FILE,
      FunctionName.LIST_FILES,
      FunctionName.RETRIEVE_FILE,
      FunctionName.DELETE_FILE,
      FunctionName.RETRIEVE_FILE_CONTENT,
      FunctionName.CREATE_BATCH,
      FunctionName.GET_BATCH_OUTPUT,
      FunctionName.CANCEL_BATCH,
      FunctionName.LIST_BATCHES,
      FunctionName.GET_BATCH_OUTPUT,
      FunctionName.LIST_FINE_TUNING_JOBS,
      FunctionName.CREATE_FINE_TUNING_JOB,
      FunctionName.RETRIEVE_FINE_TUNING_JOB,
      FunctionName.CANCEL_FINE_TUNING_JOB,
    ].includes(commonRequestOptions.saRequestData.functionName)
  ) {
    return;
  }
 
  const getAIProviderResponseFromCache = c.get(
    'getAIProviderResponseFromCache',
  );
 
  const cacheResult = await getAIProviderResponseFromCache(
    c,
    commonRequestOptions.cacheSettings,
    commonRequestOptions.saRequestData,
  );
 
  if (
    cacheResult.status === CacheStatus.HIT ||
    cacheResult.status === CacheStatus.SEMANTIC_HIT
  ) {
    const cacheHandlerOptions: CreateResponseOptions = {
      response: new Response(cacheResult.value, {
        headers: { 'content-type': 'application/json' },
        status: 200,
      }),
      responseTransformerFunctionName: undefined,
      cacheStatus: cacheResult.status,
      retryCount: undefined,
      cacheKey: cacheResult.key,
      aiProviderRequestBody,
      ...commonRequestOptions,
    };
 
    return createResponse(c, cacheHandlerOptions);
  }
}