{
  "name": "AI Image Generation Pipeline v2",
  "nodes": [
    {
      "parameters": {},
      "id": "e6740f92-579c-4860-9118-a6644f198144",
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -260,
        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",
        "prompt": "Analyze the two images provided. The 'productImage' contains a product. The 'referenceImage' contains an artistic style. \n\nYour task is to create a new, highly-detailed prompt for an image generation model (like DALL-E 3) that renders the product from the 'productImage' in the exact artistic style, mood, lighting, and color palette of the 'referenceImage'.\n\n- **Product Image:** {{ $('1. Webhook Intake').item.binary.productImage }}\n- **Reference Image:** {{ $('1. Webhook Intake').item.binary.referenceImage }}\n\nOutput ONLY the generated prompt string.",
        "options": {}
      },
      "id": "52e1ac50-e883-4a1b-85d0-fd6da381f9b1",
      "name": "2. AI: Analyze & Create Prompt",
      "type": "n8n-nodes-base.googleGemini",
      "typeVersion": 1.1,
      "position": [
        160,
        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": [
        600,
        320
      ],
      "credentials": {
        "headerAuth": {
          "id": "YOUR_OPENAI_CREDENTIAL_ID",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "model": "gemini-1.5-pro-latest",
        "jsonMode": true,
        "prompt": "You are an Image Quality Assurance agent. Evaluate the 'Generated Image' based on the 'Original Product Image' and the 'Reference Style Image'.\n\n1.  **Product Accuracy:** Does the Generated Image accurately show the product from the Original Product Image?\n2.  **Style Adherence:** Does the Generated Image match the aesthetic, lighting, and mood of the Reference Style Image?\n\nRespond with a JSON object: `{\"decision\": \"approved\"}` or `{\"decision\": \"declined\"}`.\n\n- **Generated Image:** {{ $('Convert to Binary').item.binary.data }}\n- **Original Product Image:** {{ $('1. Webhook Intake').item.binary.productImage }}\n- **Reference Style Image:** {{ $('1. Webhook Intake').item.binary.referenceImage }}",
        "options": {}
      },
      "id": "270d740c-d400-4b36-a51b-252a9263a436",
      "name": "4. AI Evaluator",
      "type": "n8n-nodes-base.googleGemini",
      "typeVersion": 1.1,
      "position": [
        1060,
        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": [
        1300,
        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": [
        820,
        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": [
        1520,
        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": [
        1760,
        220
      ],
      "credentials": {
        "telegramApi": {
          "id": "YOUR_TELEGRAM_CREDENTIAL_ID",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "chatId": "YOUR_TELEGRAM_CHAT_ID",
        "text": "=⚠️ Image Declined by AI Evaluator.\n\nPlease 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": [
        1520,
        420
      ],
      "credentials": {
        "telegramApi": {
          "id": "YOUR_TELEGRAM_CREDENTIAL_ID",
          "name": "Telegram account"
        }
      }
    },
    {
      "parameters": {
        "options": {
          "from": "={{ $('8a. Ask for Feedback').item.json.from.id }}"
        }
      },
      "id": "d04e4c27-ac10-4ed3-b459-204130f14d9b",
      "name": "8b. Wait for Feedback",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1,
      "position": [
        1260,
        620
      ]
    },
    {
      "parameters": {
        "model": "gemini-1.5-pro-latest",
        "prompt": "You are an expert prompt revision specialist. You will be given the 'Original Prompt' that produced a failed image, and 'User Feedback'. Your task is to rewrite the 'Original Prompt' to incorporate the 'User Feedback'. Maintain the core subject and style, but modify the specifics as requested. Output ONLY the new, revised prompt string.\n\n**Original Prompt:**\n{{ $('Merge Data').item.json.original_prompt }}\n\n**User Feedback:**\n{{ $('Merge Data').item.json.user_feedback }}",
        "options": {}
      },
      "id": "9814407b-8919-4f73-9a72-f8d956897df4",
      "name": "8c. AI: Revise Prompt",
      "type": "n8n-nodes-base.googleGemini",
      "typeVersion": 1.1,
      "position": [
        1040,
        620
      ],
      "credentials": {
        "googleGeminiApi": {
          "id": "YOUR_GEMINI_CREDENTIAL_ID",
          "name": "Google Gemini API account"
        }
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "original_prompt",
              "value": "={{ $('2. AI: Analyze & Create Prompt').item.json.text }}"
            },
            {
              "name": "user_feedback",
              "value": "={{ $('8b. Wait for Feedback').item.json.message.text }}"
            }
          ]
        },
        "options": {}
      },
      "id": "ac1c2a8c-4573-42e7-9d7a-42cd63b15488",
      "name": "Merge Data for Revision",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1260,
        420
      ]
    },
    {
      "parameters": {
        "jsCode": "// We are replacing the original prompt with the revised one to loop back\nconst newItem = { ...$item }; // Create a copy to avoid modifying the original item directly\nnewItem.json.text = $('8c. AI: Revise Prompt').item.json.text;\nreturn newItem;"
      },
      "id": "e98e4c76-5de3-4927-b50a-f0f7f2b1a9c8",
      "name": "Set Revised Prompt",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        820,
        620
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "1. Webhook Intake",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "1. Webhook Intake": {
      "main": [
        [
          {
            "node": "2. AI: Analyze & Create Prompt",
            "type": "main",
            "index": 0
          },
          {
            "node": "4. AI Evaluator",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "2. AI: Analyze & Create Prompt": {
      "main": [
        [
          {
            "node": "3. Generate Image (OpenAI)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge Data for Revision",
            "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": "Merge Data for Revision",
            "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": "5. & 6. Notify on Success",
            "type": "main",
            "index": 0
          },
          {
            "node": "8a. Ask for Feedback",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "7. Upload to Google Drive": {
      "main": [
        [
          {
            "node": "5. & 6. Notify on Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "8a. Ask for Feedback": {
      "main": [
        [
          {
            "node": "8b. Wait for Feedback",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "8b. Wait for Feedback": {
      "main": [
        [
          {
            "node": "Merge Data for Revision",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "8c. AI: Revise Prompt": {
      "main": [
        [
          {
            "node": "Set Revised Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Data for Revision": {
      "main": [
        [
          {
            "node": "8c. AI: Revise Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Revised Prompt": {
      "main": [
        [
          {
            "node": "3. Generate Image (OpenAI)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "versionId": "48b64e0a-dd41-4770-bfe3-94c64d8a1768"
}