All files / api/src/ai-providers/fireworks-ai image-generate.ts

0% Statements 0/94
0% Branches 0/1
0% Functions 0/1
0% Lines 0/94

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                                                                                                                                                                                                               
import { generateInvalidProviderResponseError } from '@api/utils/ai-provider';
import type {
  AIProviderFunctionConfig,
  ResponseTransformFunction,
} from '@shared/types/ai-providers/config';
 
import type { GenerateImageRequestBody } from '@shared/types/api/routes/images-api';
import { AIProvider } from '@shared/types/constants';
import { fireworksAIErrorResponseTransform } from './chat-complete';
 
export const FireworksAIImageGenerateConfig: AIProviderFunctionConfig = {
  prompt: {
    param: 'text_prompts',
    required: true,
    transform: (saRequestBody: GenerateImageRequestBody) => {
      return [
        {
          text: saRequestBody.prompt,
          weight: 1,
        },
      ];
    },
  },
  model: {
    param: 'model',
    required: true,
    default: 'stable-diffusion-xl-1024-v1-0',
  },
  size: [
    {
      param: 'height',
      transform: (saRequestBody: GenerateImageRequestBody): number =>
        parseInt(saRequestBody.size?.toLowerCase().split('x')[1] ?? '1024', 10),
      min: 512,
      max: 1024,
      default: 1024,
    },
    {
      param: 'width',
      transform: (saRequestBody: GenerateImageRequestBody): number =>
        parseInt(saRequestBody.size?.toLowerCase().split('x')[0] ?? '1024', 10),
      min: 512,
      max: 1024,
      default: 1024,
    },
  ],
  cfg_scale: {
    param: 'cfg_scale',
    default: 7,
  },
  sampler: {
    param: 'sampler',
  },
  n: {
    param: 'samples',
    min: 1,
    max: 10,
    default: 1,
  },
  seed: {
    param: 'seed',
    min: 0,
    max: 4294967295,
  },
  steps: {
    param: 'steps',
    min: 10,
    max: 150,
    default: 50,
  },
  safety_check: {
    param: 'safety_check',
  },
  lora_adapter_name: {
    param: 'lora_adapter_name',
  },
  lora_weight_filename: {
    param: 'lora_weight_filename',
  },
};
 
export const FireworksAIImageGenerateResponseTransform: ResponseTransformFunction =
  (aiProviderResponseBody, aiProviderResponseStatus) => {
    if (aiProviderResponseStatus !== 200) {
      return fireworksAIErrorResponseTransform(aiProviderResponseBody);
    }
    if (Array.isArray(aiProviderResponseBody)) {
      return {
        created: Math.floor(Date.now() / 1000), // Corrected method call
        data: aiProviderResponseBody?.map((r) => ({
          b64_json: r.base64,
          seed: r.seed,
          finishReason: r.finishReason,
        })), // Corrected object creation within map
        provider: AIProvider.FIREWORKS_AI,
      };
    }
 
    return generateInvalidProviderResponseError(
      aiProviderResponseBody,
      AIProvider.FIREWORKS_AI,
    );
  };