All files / api/src/ai-providers/azure-openai api.ts

100% Statements 76/76
100% Branches 16/16
100% Functions 3/3
100% Lines 76/76

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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86  1x 1x         1x 1x 1x 1x 1x 1x   1x 1x 3x   3x 1x 1x   2x 3x 1x 5x   5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 5x 1x 1x 5x 1x 1x 3x 5x 1x 6x 6x 6x 1x 1x 5x 6x 1x  
import type { InternalProviderAPIConfig } from '@shared/types/ai-providers/config';
import { FunctionName } from '@shared/types/api/request';
import {
  getAccessTokenFromEntraId,
  getAzureManagedIdentityToken,
} from './utils';
 
const EndpointMap: Partial<Record<FunctionName, string>> = {
  [FunctionName.COMPLETE]: '/openai/v1/completions',
  [FunctionName.CHAT_COMPLETE]: '/openai/v1/chat/completions',
  [FunctionName.EMBED]: '/openai/v1/models/embeddings',
  [FunctionName.CREATE_MODEL_RESPONSE]: '/openai/v1/responses',
};
 
export const azureOpenAIAPIConfig: InternalProviderAPIConfig = {
  getBaseURL: ({ saTarget }) => {
    const { azure_openai_config } = saTarget;
 
    if (!azure_openai_config) {
      throw new Error('`azure_openai_config` is required in target');
    }
 
    return azure_openai_config.url;
  },
  headers: async ({ saTarget, saRequestData }) => {
    const { api_key, azure_auth_mode } = saTarget;
 
    if (azure_auth_mode === 'entra') {
      const {
        azure_entra_tenant_id,
        azure_entra_client_id,
        azure_entra_client_secret,
      } = saTarget;
      if (
        azure_entra_tenant_id &&
        azure_entra_client_id &&
        azure_entra_client_secret
      ) {
        const scope = 'https://cognitiveservices.azure.com/.default';
        const accessToken = await getAccessTokenFromEntraId(
          azure_entra_tenant_id,
          azure_entra_client_id,
          azure_entra_client_secret,
          scope,
        );
        return {
          Authorization: `Bearer ${accessToken}`,
        };
      }
    }
    if (azure_auth_mode === 'managed') {
      const { azure_managed_client_id } = saTarget;
      const resource = 'https://cognitiveservices.azure.com/';
      const accessToken = await getAzureManagedIdentityToken(
        resource,
        azure_managed_client_id,
      );
      return {
        Authorization: `Bearer ${accessToken}`,
      };
    }
    const headersObj: Record<string, string> = {
      'api-key': `${api_key}`,
    };
    if (
      saRequestData.functionName === FunctionName.CREATE_TRANSCRIPTION ||
      saRequestData.functionName === FunctionName.CREATE_TRANSLATION ||
      saRequestData.functionName === FunctionName.UPLOAD_FILE
    ) {
      headersObj['Content-Type'] = 'multipart/form-data';
    }
    if (saTarget.openai_beta) {
      headersObj['OpenAI-Beta'] = saTarget.openai_beta;
    }
    return headersObj;
  },
  getEndpoint: ({ saRequestData }) => {
    const fn = saRequestData.functionName;
    const endpoint = EndpointMap[fn];
    if (!endpoint) {
      throw new Error(`Endpoint not found for function ${fn}`);
    }
    return endpoint;
  },
};