All files / api/src/utils ai-provider.ts

70.73% Statements 29/41
100% Branches 2/2
66.66% Functions 2/3
70.73% Lines 29/41

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    1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x   1x 33x           33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x             1x                              
import type { ErrorResponseBody } from '@shared/types/api/response/body';
 
export function generateInvalidProviderResponseError(
  aiProviderResponseBody: Record<string, unknown>,
  provider: string,
): ErrorResponseBody {
  return {
    error: {
      message: `Invalid response received from ${provider}: ${JSON.stringify(
        aiProviderResponseBody,
      )}`,
    },
    provider: provider,
  } as ErrorResponseBody;
}
 
export function generateErrorResponse(
  errorDetails: {
    message: string;
    type?: string;
    param?: string;
    code?: string;
  },
  provider: string,
): ErrorResponseBody {
  const errorResponse: ErrorResponseBody = {
    error: {
      message: `${provider} error: ${errorDetails.message}`,
      type: errorDetails.type,
      param: errorDetails.param,
      code: errorDetails.code,
    },
    provider: provider,
  };
  return errorResponse;
}
 
type SplitResult = {
  before: string;
  after: string;
};
 
export function splitString(input: string, separator: string): SplitResult {
  const sepIndex = input.indexOf(separator);
 
  if (sepIndex === -1) {
    return {
      before: input,
      after: '',
    };
  }
 
  return {
    before: input.substring(0, sepIndex),
    after: input.substring(sepIndex + 1),
  };
}