{
  "name": "AI Image Generation Pipeline",
  "nodes": [
    {
      "parameters": {},
      "id": "e6740f92-579c-4860-9118-a6644f198144",
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -100,
        320
      ]
    },
    {
      "parameters": {
        "path": "webhook-intake",
        "options": {
          "binaryData": true,
          "responseData": "allEntries"
        }
      },
      "id": "f5e954aa-252f-45a7-8c38-89c565cd2e82",
      "name": "1. Webhook Intake",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        80,
        320
      ],
      "webhookId": "b2c12579-24b5-4122-8703-a170868b4561"
    },
    {
      "parameters": {
        "model": "gemini-1.5-pro-latest",
        "options": {
          "system": "You are an expert prompt engineer for generative image models. Your task is to analyze two images provided by the user: a 'product image' and a 'reference style image'.\n\n1.  **Analyze the Product:** Identify the key subject, its features, material, and composition from the 'product image'.\n2.  **Analyze the Style:** Identify the artistic style, mood, lighting, color palette, camera angle, and overall vibe from the 'reference style image'.\n3.  **Create the Prompt:** Combine these analyses into a single, highly-detailed, and descriptive prompt for an image generation model like DALL-E 3 or Midjourney. The prompt should instruct the model to render the *product* from the first image in the *style* of the second image.\n\nOutput ONLY the generated prompt as a single string. Do not add any other text, greetings, or explanations."
        },
        "attachments": {
          "attachments": [
            {
              "attachment": "={{ $('1. Webhook Intake').item.binary.productImage }}"
            },
            {
              "attachment": "={{ $('1. Webhook Intake').item.binary.referenceImage }}"
            }
          ]
        }
      },
      "id": "52e1ac50-e883-4a1b-85d0-fd6da381f9b1",
      "name": "2. AI: Analyze & Create Prompt",
      "type": "n8n-nodes-base.googleGemini",
      "typeVersion": 1,
      "position": [
        320,
        320
      ],
      "credentials": {
        "googleGeminiApi": {
          "id": "YOUR_GEMINI_CREDENTIAL_ID",
          "name": "Google Gemini API account"
        }
      }
    },
    {
      "parameters": {
        "url": "https://api.openai.com/v1/images/generations",
        "authentication": "headerAuth",
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "dall-e-3"
            },
            {
              "name": "prompt",
              "value": "={{ $('2. AI: Analyze & Create Prompt').item.json.text }}"
            },
            {
              "name": "n",
              "value": 1
            },
            {
              "name": "size",
              "value": "1024x1024"
            },
            {
              "name": "quality",
              "value": "standard"
            },
            {
              "name": "response_format",
              "value": "b64_json"
            }
          ]
        },
        "options": {}
      },
      "id": "c760824b-6101-44da-b2c6-d446927bb867",
      "name": "3. Generate Image (OpenAI)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        560,
        320
      ],
      "credentials": {
        "headerAuth": {
          "id": "YOUR_OPENAI_CREDENTIAL_ID",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "model": "gemini-1.5-pro-latest",
        "jsonMode": true,
        "options": {
          "system": "You are an expert Image Quality Assurance agent. Your task is to evaluate a 'Generated Image' based on two criteria: the 'Original Product Image' and the 'Reference Style Image'.\n\n1.  **Product Accuracy:** Does the 'Generated Image' accurately represent the key features and details of the 'Original Product Image'?\n2.  **Style Adherence:** Does the 'Generated Image' successfully capture the mood, lighting, color palette, and overall aesthetic of the 'Reference Style Image'?\n\nBased on your evaluation, you MUST respond with a JSON object containing a single key, 'decision', with a value of either 'approved' or 'declined'.\n\nExample Output: {\"decision\": \"approved\"}"
        },
        "attachments": {
          "attachments": [
            {
              "inputKey": "Generated Image",
              "attachment": "={{ $('Convert to Binary').item.binary.data }}"
            },
            {
              "inputKey": "Original Product Image",
              "attachment": "={{ $('1. Webhook Intake').item.binary.productImage }}"
            },
            {
              "inputKey": "Reference Style Image",
              "attachment": "={{ $('1. Webhook Intake').item.binary.referenceImage }}"
            }
          ]
        }
      },
      "id": "270d740c-d400-4b36-a51b-252a9263a436",
      "name": "4. AI Evaluator",
      "type": "n8n-nodes-base.googleGemini",
      "typeVersion": 1,
      "position": [
        1020,
        320
      ],
      "credentials": {
        "googleGeminiApi": {
          "id": "YOUR_GEMINI_CREDENTIAL_ID",
          "name": "Google Gemini API account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.decision }}",
              "operation": "equal",
              "value2": "approved"
            }
          ]
        }
      },
      "id": "40b15112-a8c7-43ce-9ef6-e3d1acb0c511",
      "name": "Switch: Approved?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1260,
        320
      ]
    },
    {
      "parameters": {
        "jsCode": "const b64_json = $json.data[0].b64_json;\nconst binaryData = Buffer.from(b64_json, 'base64');\n\nreturn [{ \n  binary: {\n    data: binaryData\n  }\n}];"
      },
      "id": "f5f59bf5-c54d-4a11-b0e5-7917c8289417",
      "name": "Convert to Binary",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        780,
        320
      ]
    },
    {
      "parameters": {
        "resource": "file",
        "operation": "upload",
        "parent": "YOUR_GOOGLE_DRIVE_FOLDER_ID",
        "options": {}
      },
      "id": "0142b6e1-92b4-4b5c-a5d6-bd8b846e3ed8",
      "name": "7. Upload to Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1480,
        220
      ],
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "YOUR_GOOGLE_CREDENTIAL_ID",
          "name": "Google account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "YOUR_TELEGRAM_CHAT_ID",
        "text": "=✅ Image Approved & Uploaded!\n\nNew image has been generated and uploaded to Google Drive.\n\nDrive Link: {{ $('7. Upload to Google Drive').item.json.webViewLink }}\n\nPrompt Used:\n`{{ $('2. AI: Analyze & Create Prompt').item.json.text }}`",
        "file": {
          "file": "={{ $('Convert to Binary').item.binary.data }}"
        },
        "options": {}
      },
      "id": "787c9f80-0a2a-4375-b9b5-6a84c311c1d8",
      "name": "5. & 6. Notify on Success",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 2,
      "position": [
        1720,
        220
      ],
      "credentials": {
        "telegramApi": {
          "id": "YOUR_TELEGRAM_CREDENTIAL_ID",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "YOUR_TELEGRAM_CHAT_ID",
        "text": "=⚠️ Image Declined by AI Evaluator.\n\nThe generated image did not meet the quality standards. Please reply to this message with feedback for revision.\n\nExample: \"Make the lighting warmer and use a close-up shot.\"",
        "file": {
          "file": "={{ $('Convert to Binary').item.binary.data }}"
        },
        "options": {}
      },
      "id": "31b279d4-b772-4d51-897d-419b6727783d",
      "name": "8a. Ask for Feedback",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 2,
      "position": [
        1480,
        500
      ],
      "credentials": {
        "telegramApi": {
          "id": "YOUR_TELEGRAM_CREDENTIAL_ID",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "options": {
          "from": "={{ $('31b279d4-b772-4d51-897d-419b6727783d').item.json.from.id }}"
        }
      },
      "id": "d04e4c27-ac10-4ed3-b459-204130f14d9b",
      "name": "8b. Wait for Feedback",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        1260,
        700
      ]
    },
    {
      "parameters": {
        "model": "gemini-1.5-pro-latest",
        "options": {
          "system": "You are an expert prompt revision specialist. You will be given the 'Original Prompt' that produced a failed image, and 'User Feedback'.\n\nYour task is to rewrite the 'Original Prompt' to incorporate the 'User Feedback'. Maintain the core subject and style, but modify the specifics as requested by the user. \n\nOutput ONLY the new, revised prompt as a single string.",
          "prompt": "Original Prompt:\n```\n{{ $('Merge Data').item.json.original_prompt }}\n```\n\nUser Feedback:\n```\n{{ $('Merge Data').item.json.user_feedback }}\n```"
        }
      },
      "id": "9814407b-8919-4f73-9a72-f8d956897df4",
      "name": "8c. AI: Revise Prompt",
      "type": "n8n-nodes-base.googleGemini",
      "typeVersion": 1,
      "position": [
        1000,
        700
      ],
      "credentials": {
        "googleGeminiApi": {
          "id": "YOUR_GEMINI_CREDENTIAL_ID",
          "name": "Google Gemini API account"
        }
      }
    },
    {
      "parameters": {
        "mode": "mergeByIndex"
      },
      "id": "4525d84d-2947-4974-9815-16c49e1e2373",
      "name": "Merge for Loop",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 2,
      "position": [
        560,
        560
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "user_feedback",
              "value": "={{ $('8b. Wait for Feedback').item.json.message.text }}"
            },
            {
              "name": "original_prompt",
              "value": "={{ $('2. AI: Analyze & Create Prompt').item.json.text }}"
            }
          ]
        },
        "options": {}
      },
      "id": "ac1c2a8c-4573-42e7-9d7a-42cd63b15488",
      "name": "Merge Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3,
      "position": [
        1000,
        500
      ]
    },
    {
      "parameters": {
        "jsCode": "// The previous prompt from the initial generation needs to be replaced\n// with the new, revised prompt for the loop.\n$item.prompt_text = $('8c. AI: Revise Prompt').item.json.text;\nreturn $item;"
      },
      "id": "4a187a41-2b62-4217-be51-b040a454df7d",
      "name": "Set Revised Prompt",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        780,
        700
      ]
    }
  ],
  "connections": {
    "1. Webhook Intake": {
      "main": [
        [
          {
            "node": "2. AI: Analyze & Create Prompt",
            "type": "main",
            "index": 0
          },
          {
            "node": "ac1c2a8c-4573-42e7-9d7a-42cd63b15488",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "2. AI: Analyze & Create Prompt": {
      "main": [
        [
          {
            "node": "Merge for Loop",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "3. Generate Image (OpenAI)": {
      "main": [
        [
          {
            "node": "Convert to Binary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "4. AI Evaluator": {
      "main": [
        [
          {
            "node": "Switch: Approved?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch: Approved?": {
      "main": [
        [
          {
            "node": "7. Upload to Google Drive",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "31b279d4-b772-4d51-897d-419b6727783d",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Convert to Binary": {
      "main": [
        [
          {
            "node": "4. AI Evaluator",
            "type": "main",
            "index": 0
          },
          {
            "node": "7. Upload to Google Drive",
            "type": "main",
            "index": 0
          },
          {
            "node": "31b279d4-b772-4d51-897d-419b6727783d",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "7. Upload to Google Drive": {
      "main": [
        [
          {
            "node": "5. & 6. Notify on Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "31b279d4-b772-4d51-897d-419b6727783d": {
      "main": [
        [
          {
            "node": "8b. Wait for Feedback",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "8b. Wait for Feedback": {
      "main": [
        [
          {
            "node": "ac1c2a8c-4573-42e7-9d7a-42cd63b15488",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "8c. AI: Revise Prompt": {
      "main": [
        [
          {
            "node": "Set Revised Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge for Loop": {
      "main": [
        [
          {
            "node": "3. Generate Image (OpenAI)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ac1c2a8c-4573-42e7-9d7a-42cd63b15488": {
      "main": [
        [
          {
            "node": "8c. AI: Revise Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Revised Prompt": {
      "main": [
        [
          {
            "node": "Merge for Loop",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "1. Webhook Intake",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "versionId": "f7811909-ca57-4648-9df2-51a8309a4891"
}