| 9 | const ai = new GoogleGenAI({ apiKey: process.env.API_KEY }); |
| 10 | |
| 11 | export async function editImage( |
| 12 | base64ImageData: string, |
| 13 | mimeType: string, |
| 14 | prompt: string, |
| 15 | maskBase64: string | null, |
| 16 | secondaryImage: { base64: string; mimeType: string } | null |
| 17 | ): Promise<GeneratedContent> { |
| 18 | try { |
| 19 | let fullPrompt = prompt; |
| 20 | const parts: any[] = [ |
| 21 | { |
| 22 | inlineData: { |
| 23 | data: base64ImageData, |
| 24 | mimeType: mimeType, |
| 25 | }, |
| 26 | }, |
| 27 | ]; |
| 28 | |
| 29 | if (maskBase64) { |
| 30 | parts.push({ |
| 31 | inlineData: { |
| 32 | data: maskBase64, |
| 33 | mimeType: 'image/png', |
| 34 | }, |
| 35 | }); |
| 36 | fullPrompt = `Apply the following instruction only to the masked area of the image: "${prompt}". Preserve the unmasked area.`; |
| 37 | } |
| 38 | |
| 39 | if (secondaryImage) { |
| 40 | parts.push({ |
| 41 | inlineData: { |
| 42 | data: secondaryImage.base64, |
| 43 | mimeType: secondaryImage.mimeType, |
| 44 | }, |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | parts.push({ text: fullPrompt }); |
| 49 | |
| 50 | const response = await ai.models.generateContent({ |
| 51 | model: 'gemini-2.5-flash-image-preview', |
| 52 | contents: { parts }, |
| 53 | config: { |
| 54 | responseModalities: [Modality.IMAGE, Modality.TEXT], |
| 55 | }, |
| 56 | }); |
| 57 | |
| 58 | const result: GeneratedContent = { imageUrl: null, text: null }; |
| 59 | const responseParts = response.candidates?.[0]?.content?.parts; |
| 60 | |
| 61 | if (responseParts) { |
| 62 | for (const part of responseParts) { |
| 63 | if (part.text) { |
| 64 | result.text = (result.text ? result.text + "\n" : "") + part.text; |
| 65 | } else if (part.inlineData) { |
| 66 | result.imageUrl = `data:${part.inlineData.mimeType};base64,${part.inlineData.data}`; |
| 67 | } |
| 68 | } |