diff --git a/src/globals.ts b/src/globals.ts index dce5f05e4..a98f256b6 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -77,6 +77,7 @@ export const DASHSCOPE: string = 'dashscope'; export const X_AI: string = 'x-ai'; export const SAGEMAKER: string = 'sagemaker'; export const NEBIUS: string = 'nebius'; +export const RECRAFTAI: string = 'recraft-ai'; export const VALID_PROVIDERS = [ ANTHROPIC, @@ -127,6 +128,7 @@ export const VALID_PROVIDERS = [ X_AI, SAGEMAKER, NEBIUS, + RECRAFTAI, ]; export const CONTENT_TYPES = { diff --git a/src/providers/index.ts b/src/providers/index.ts index ea4f3c242..f01d33bed 100644 --- a/src/providers/index.ts +++ b/src/providers/index.ts @@ -51,6 +51,7 @@ import XAIConfig from './x-ai'; import QdrantConfig from './qdrant'; import SagemakerConfig from './sagemaker'; import NebiusConfig from './nebius'; +import RecraftAIConfig from './recraft-ai'; const Providers: { [key: string]: ProviderConfigs } = { openai: OpenAIConfig, @@ -102,6 +103,7 @@ const Providers: { [key: string]: ProviderConfigs } = { qdrant: QdrantConfig, sagemaker: SagemakerConfig, nebius: NebiusConfig, + 'recraft-ai': RecraftAIConfig, }; export default Providers; diff --git a/src/providers/recraft-ai/api.ts b/src/providers/recraft-ai/api.ts new file mode 100644 index 000000000..2678d244e --- /dev/null +++ b/src/providers/recraft-ai/api.ts @@ -0,0 +1,19 @@ +import { ProviderAPIConfig } from '../types'; + +const RecraftAIAPIConfig: ProviderAPIConfig = { + getBaseURL: () => 'https://external.api.recraft.ai/v1', + headers: ({ providerOptions }) => ({ + Authorization: `Bearer ${providerOptions.apiKey}`, + 'Content-Type': 'application/json', + }), + getEndpoint: ({ fn }) => { + switch (fn) { + case 'imageGenerate': + return '/images/generations'; + default: + return ''; + } + }, +}; + +export default RecraftAIAPIConfig; diff --git a/src/providers/recraft-ai/imageGenerate.ts b/src/providers/recraft-ai/imageGenerate.ts new file mode 100644 index 000000000..a3f371758 --- /dev/null +++ b/src/providers/recraft-ai/imageGenerate.ts @@ -0,0 +1,80 @@ +import { RECRAFTAI } from '../../globals'; +import { ErrorResponse, ImageGenerateResponse, ProviderConfig } from '../types'; +import { generateErrorResponse } from '../utils'; + +interface RecraftAIImageObject { + b64_json?: string; + url?: string; +} + +interface RecraftAIImageGenerateResponse extends ImageGenerateResponse { + data: RecraftAIImageObject[]; +} + +export const RecraftAIImageGenerateConfig: ProviderConfig = { + prompt: { + param: 'prompt', + required: true, + }, + style: { + param: 'style', + default: 'realistic_image', + }, + style_id: { + param: 'style_id', + }, + n: { + param: 'n', + default: 1, + min: 1, + max: 2, + }, + size: { + param: 'size', + default: '1024x1024', + }, + response_format: { + param: 'response_format', + default: 'url', + }, + controls: { + param: 'controls', + }, + model: { + param: 'model', + }, + artistic_level: { + param: 'artistic_level', + }, + substyle: { + param: 'substyle', + }, +}; + +export const RecraftAIImageGenerateResponseTransform: ( + response: RecraftAIImageGenerateResponse | ErrorResponse, + responseStatus: number +) => ImageGenerateResponse | ErrorResponse = (response, responseStatus) => { + if (responseStatus !== 200 || 'error' in response) { + return RecraftAIErrorResponseTransform( + response as ErrorResponse, + RECRAFTAI + ); + } + return response; +}; + +export const RecraftAIErrorResponseTransform: ( + response: ErrorResponse, + provider: string +) => ErrorResponse = (response, provider) => { + return generateErrorResponse( + { + message: response.error?.message || 'Unknown error occurred', + type: response.error?.type || null, + param: response.error?.param || null, + code: response.error?.code || null, + }, + provider + ); +}; diff --git a/src/providers/recraft-ai/index.ts b/src/providers/recraft-ai/index.ts new file mode 100644 index 000000000..44fc085bc --- /dev/null +++ b/src/providers/recraft-ai/index.ts @@ -0,0 +1,16 @@ +import { ProviderConfigs } from '../types'; +import RecraftAIAPIConfig from './api'; +import { + RecraftAIImageGenerateConfig, + RecraftAIImageGenerateResponseTransform, +} from './imageGenerate'; + +const RecraftAIConfig: ProviderConfigs = { + imageGenerate: RecraftAIImageGenerateConfig, + api: RecraftAIAPIConfig, + responseTransforms: { + imageGenerate: RecraftAIImageGenerateResponseTransform, + }, +}; + +export default RecraftAIConfig; diff --git a/src/providers/recraft-ai/utils.ts b/src/providers/recraft-ai/utils.ts new file mode 100644 index 000000000..394625a9a --- /dev/null +++ b/src/providers/recraft-ai/utils.ts @@ -0,0 +1,17 @@ +import { ErrorResponse } from '../types'; +import { generateErrorResponse } from '../utils'; + +export const RecraftAIErrorResponseTransform: ( + response: ErrorResponse, + provider: string +) => ErrorResponse = (response, provider) => { + return generateErrorResponse( + { + message: response.error?.message || 'Unknown error occurred', + type: response.error?.type || null, + param: response.error?.param || null, + code: response.error?.code || null, + }, + provider + ); +};