All files / api/src/ai-providers/mistral-ai api.ts

90.47% Statements 57/63
80.76% Branches 21/26
100% Functions 3/3
90.47% Lines 57/63

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  1x   1x 1x 2x 2x 2x   2x 1x 1x   2x 2x 1x 5x 5x 4x 4x     4x 1x 1x     4x         4x 1x 1x     1x     2x 2x 2x 2x 4x         4x 4x 2x 2x 2x 4x 1x 5x 1x 5x 5x   5x 1x 1x   4x 5x 5x 2x 2x 5x 1x 1x 5x 1x 5x 5x 1x   1x  
import type { InternalProviderAPIConfig } from '@shared/types/ai-providers/config';
import { FunctionName } from '@shared/types/api/request';
 
export const mistralAIAPIConfig: InternalProviderAPIConfig = {
  headers: ({ saTarget }) => {
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
    };
 
    if (saTarget.api_key) {
      headers.Authorization = `Bearer ${saTarget.api_key}`;
    }
 
    return headers;
  },
  getBaseURL: ({ saTarget }) => {
    const customHost = saTarget.custom_host;
    if (customHost) {
      try {
        const url = new URL(customHost);
 
        // Validate protocol
        if (!['http:', 'https:'].includes(url.protocol)) {
          throw new Error('Only HTTP and HTTPS protocols are allowed');
        }
 
        // Validate hostname is not empty
        if (!url.hostname) {
          throw new Error('Hostname is required');
        }
 
        // Validate port if specified
        if (url.port) {
          const portNum = Number.parseInt(url.port, 10);
          if (Number.isNaN(portNum) || portNum < 1 || portNum > 65535) {
            throw new Error('Invalid port number');
          }
        }
 
        // Prevent path traversal in URL
        if (
          url.pathname &&
          url.pathname !== '/' &&
          url.pathname.includes('..')
        ) {
          throw new Error('Path traversal not allowed');
        }
 
        // Return the sanitized URL without query params or hash
        return `${url.protocol}//${url.host}${url.pathname === '/' ? '' : url.pathname}`;
      } catch (error) {
        const message = error instanceof Error ? error.message : 'Invalid URL';
        throw new Error(`Invalid custom_host URL: ${message}`);
      }
    }
    return 'https://api.mistral.ai/v1';
  },
  getEndpoint: ({ saRequestData, saTarget }) => {
    const mappedFn = saRequestData.functionName;
    const mistralFimCompletion = saTarget.mistral_fim_completion;
 
    if (mistralFimCompletion === 'true') {
      return '/fim/completions';
    }
 
    switch (mappedFn) {
      case FunctionName.CHAT_COMPLETE:
      case FunctionName.STREAM_CHAT_COMPLETE: {
        return '/chat/completions';
      }
      case FunctionName.EMBED: {
        return '/embeddings';
      }
      default:
        return '';
    }
  },
};
 
export default mistralAIAPIConfig;