All files / api/src/ai-providers/groq chat-complete.ts

96.03% Statements 121/126
78.94% Branches 30/38
100% Functions 2/2
96.03% Lines 121/126

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159  1x 1x           1x   1x 13x 13x 13x 13x 3x 3x 3x 3x 3x   13x         8x 8x 8x 8x             8x     8x 8x 7x 7x 8x 2x 2x 2x 2x 2x     6x 6x 6x 6x 6x 6x 6x 6x 6x     8x 1x 1x           6x   6x 6x   2x 2x 2x 2x 2x   1x 1x   10x 10x 10x 10x 10x 10x 2x 2x     8x 8x 8x 10x 1x 1x 1x 1x 1x 1x     10x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 10x 1x 1x 1x 1x 1x 4x 10x 10x  
import type { GroqStreamChunk } from '@api/ai-providers/groq/types';
import { groqErrorResponseTransform } from '@api/ai-providers/groq/utils';
import { generateInvalidProviderResponseError } from '@api/utils/ai-provider';
import type {
  ResponseChunkStreamTransformFunction,
  ResponseTransformFunction,
} from '@shared/types/ai-providers/config';
import type { ChatCompletionResponseBody } from '@shared/types/api/routes/chat-completions-api';
import { AIProvider } from '@shared/types/constants';
 
export const groqChatCompleteResponseTransform: ResponseTransformFunction = (
  aiProviderResponseBody,
  aiProviderResponseStatus,
) => {
  if ('error' in aiProviderResponseBody && aiProviderResponseStatus !== 200) {
    return groqErrorResponseTransform(
      aiProviderResponseBody,
      aiProviderResponseStatus,
    );
  }
 
  if ('choices' in aiProviderResponseBody) {
    // Build response object explicitly to avoid including service_tier which Groq doesn't support
    // and may include with invalid values that cause OpenAI SDK validation errors
 
    // Type guard for response body
    if (
      typeof aiProviderResponseBody !== 'object' ||
      aiProviderResponseBody === null
    ) {
      return generateInvalidProviderResponseError(
        aiProviderResponseBody,
        AIProvider.GROQ,
      );
    }
 
    const rawResponse = aiProviderResponseBody as Record<string, unknown>;
 
    // Validate required fields exist
    if (
      !rawResponse.id ||
      !rawResponse.choices ||
      !Array.isArray(rawResponse.choices)
    ) {
      return generateInvalidProviderResponseError(
        aiProviderResponseBody,
        AIProvider.GROQ,
      );
    }
 
    // Build the response object field by field, explicitly excluding service_tier
    const result: Record<string, unknown> = {
      id: rawResponse.id,
      object: rawResponse.object,
      created: rawResponse.created,
      model: rawResponse.model,
      provider: AIProvider.GROQ,
      choices: rawResponse.choices,
      usage: rawResponse.usage,
    };
 
    // Only add system_fingerprint if it exists
    if (rawResponse.system_fingerprint) {
      result.system_fingerprint = rawResponse.system_fingerprint;
    }
 
    // Explicitly ensure service_tier is NOT included
    // Groq returns service_tier with value "on_demand" which is incompatible with OpenAI SDK
    // The OpenAI SDK expects service_tier to be either "scale" or "default" only
    // Removing this field ensures compatibility with OpenAI client libraries
    delete result.service_tier;
 
    return result as ChatCompletionResponseBody;
  }
 
  return generateInvalidProviderResponseError(
    aiProviderResponseBody,
    AIProvider.GROQ,
  );
};
 
export const groqChatCompleteStreamChunkTransform: ResponseChunkStreamTransformFunction =
  (responseChunk) => {
    // Ensure responseChunk is a string
    const chunkStr =
      typeof responseChunk === 'string' ? responseChunk : String(responseChunk);
    let chunk = chunkStr.trim();
    chunk = chunk.replace(/^data: /, '');
    chunk = chunk.trim();
    if (chunk === '[DONE]') {
      return `data: ${chunk}\n\n`;
    }
 
    // Parse chunk with error handling
    let parsedChunk: GroqStreamChunk;
    try {
      parsedChunk = JSON.parse(chunk);
    } catch (error) {
      console.warn('Failed to parse Groq stream chunk:', {
        error: error instanceof Error ? error.message : String(error),
        chunkPreview: chunk.substring(0, 200),
      });
      return ''; // Return empty string to skip malformed chunks
    }
 
    // Handle usage metadata chunk (sent at end of stream)
    if (parsedChunk.x_groq?.usage && parsedChunk.choices?.[0]) {
      return `data: ${JSON.stringify({
        id: parsedChunk.id,
        object: parsedChunk.object,
        created: parsedChunk.created,
        model: parsedChunk.model,
        provider: AIProvider.GROQ,
        choices: [
          {
            index: parsedChunk.choices[0].index || 0,
            delta: {},
            logprobs: null,
            finish_reason: parsedChunk.choices[0].finish_reason,
          },
        ],
        usage: {
          prompt_tokens: parsedChunk.x_groq.usage.prompt_tokens || 0,
          completion_tokens: parsedChunk.x_groq.usage.completion_tokens || 0,
          total_tokens: parsedChunk.x_groq.usage.total_tokens || 0,
        },
      })}\n\n`;
    }
    return `data: ${JSON.stringify({
      id: parsedChunk.id,
      object: parsedChunk.object,
      created: parsedChunk.created,
      model: parsedChunk.model,
      provider: AIProvider.GROQ,
      choices:
        parsedChunk.choices && parsedChunk.choices.length > 0
          ? [
              {
                index: parsedChunk.choices[0].index || 0,
                delta: {
                  role: parsedChunk.choices[0].delta?.role || 'assistant',
                  content: parsedChunk.choices[0].delta?.content || '',
                  tool_calls: parsedChunk.choices[0].delta?.tool_calls || [],
                },
                logprobs: null,
                finish_reason: parsedChunk.choices[0].finish_reason || null,
              },
            ]
          : [],
      usage: parsedChunk.usage
        ? {
            prompt_tokens: parsedChunk.usage.prompt_tokens || 0,
            completion_tokens: parsedChunk.usage.completion_tokens || 0,
            total_tokens: parsedChunk.usage.total_tokens || 0,
          }
        : undefined,
    })}\n\n`;
  };