import type { ImageProvider, OpenAIImageResponse } from '../types'; export class OpenAIProvider implements ImageProvider { private apiKey: string; constructor(apiKey?: string) { this.apiKey = apiKey || process.env.OPENAI_API_KEY || ''; if (!this.apiKey) { throw new Error('OPENAI_API_KEY environment variable is required'); } } async generate(prompt: string): Promise { const response = await fetch('https://api.openai.com/v1/images/generations', { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.apiKey}`, }, body: JSON.stringify({ model: 'dall-e-3', prompt: prompt, n: 1, size: '1024x1024', quality: 'standard', response_format: 'url', }), }); if (!response.ok) { const error = await response.text(); throw new Error(`OpenAI API error: ${response.status} - ${error}`); } const data = (await response.json()) as OpenAIImageResponse; if (!data.data || data.data.length === 0) { throw new Error('No image data returned from OpenAI API'); } const imageUrl = data.data[0].url; if (!imageUrl) { throw new Error('No image URL in OpenAI response'); } // Download the image const imageResponse = await fetch(imageUrl); if (!imageResponse.ok) { throw new Error(`Failed to download image: ${imageResponse.status}`); } const arrayBuffer = await imageResponse.arrayBuffer(); return Buffer.from(arrayBuffer); } }