{
  "id": "yAmWWzUXTwmC6hPO",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "2nd gmail draft and send",
  "tags": [],
  "nodes": [
    {
      "id": "04005997-5f22-40f8-b1c5-8791b106d277",
      "name": "Flag for Human Review",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -48,
        480
      ],
      "parameters": {
        "message": "={{ $json.output }}",
        "options": {
          "sendTo": "={{ $('Get a message').item.json.from.value[0].address }}",
          "threadId": "={{ $('Get a message').item.json.threadId }}"
        },
        "subject": "={{ $('Get a message').item.json.subject }}",
        "resource": "draft"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "57d5d8fd-3391-4af0-9305-2249d4885f5f",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -1648,
        240
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "55ee3c91-fa91-44eb-ad28-87c2a614f2a1",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -1168,
        416
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-2.5-flash-preview-05-20"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "af2b05c9-5229-4027-a514-0d210ea26fe7",
      "name": "AI Categorizer",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -1168,
        240
      ],
      "parameters": {
        "text": "=You are an AI assistant that drafts helpful email replies. For each incoming email, you will draft a reply and classify whether the email is 'simple' (can be auto-replied) or 'complex' (requires human review) or needs nothing to do.\n\nRules for classification:\n\n\nIf the email contains any sensitive topic (e.g: personal data such as biometric data, health records, racial or ethnic origin, religious or philosophical beliefs, political opinions, and sexual orientation, financial data (account numbers, credit card information), government identifiers (Social Security Numbers), and company-specific data such as trade secrets and strategic plans, etc.). classify it as \"draft\".\n\nAnd if the email does not contain any of the sensitive information and is a normal email and needs a reply, classify it as \"Reply\".\n\nAnd if an email does not contain any of the topic or information that does need reply (e.g, marketing or spam, automated notifications like \"do not reply\" emails or pickup alerts, transactional messages such as purchase receipts or account verification, and emails with no clear call to action that could bury important messages for recipients. In general, if a message is purely informational, doesn't ask for action, or is from an unknown source.) Classify it as \"Nothing\".\n\nProvide the Category Only: Respond with the category name and nothing else. Do not add any extra text, explanations, or commentary. Your response should be a single word. and you have 3 categories: \n\n1. Draft\n2. Reply\n3. Nothing\n\nHere is the Email:\n\n{{ $json.text }}",
        "options": {
          "batching": {
            "batchSize": 1,
            "delayBetweenBatches": 0
          },
          "passthroughBinaryImages": true,
          "returnIntermediateSteps": false
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "9a53a9bf-9665-4f5c-a0b9-74f5421509f5",
      "name": "Gemini Chat Draft",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -384,
        640
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-2.5-flash-preview-05-20"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "75bda556-8de8-462d-8ca6-a3fc30ca3436",
      "name": "Gemini Chat Reply",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -384,
        192
      ],
      "parameters": {
        "options": {},
        "modelName": "models/gemini-2.5-flash-preview-05-20"
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "5396f107-619c-426d-b759-ebece261871f",
      "name": "No Operation, do nothing",
      "type": "n8n-nodes-base.noOp",
      "position": [
        -688,
        560
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "dd50a20e-869e-4e9e-9c07-820be5fa9f95",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1712,
        48
      ],
      "parameters": {
        "width": 448,
        "height": 400,
        "content": "## Gmail Trigger + Get a message\n\nPurpose: Watches Gmail for new incoming messages, then fetches the full details (ID, subject, text, threadId, sender).\n\nThis provides the input data that downstream AI nodes will analyze and act on."
      },
      "typeVersion": 1
    },
    {
      "id": "1c0fdd25-c89d-4ee8-a83e-7f0b455921e8",
      "name": "Get a message",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -1424,
        240
      ],
      "parameters": {
        "simple": false,
        "options": {},
        "messageId": "={{ $json.id }}",
        "operation": "get"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "09f432ed-6285-4147-92d5-2b645912098d",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1232,
        -112
      ],
      "parameters": {
        "width": 400,
        "height": 704,
        "content": "## Google Gemini Chat Model + AI Categorizer\n\nPurpose: Uses Gemini to classify each email into one of three categories:\n\n\u29bf Reply: normal emails needing an auto-response\n\n\u29bf Draft: sensitive or complex emails that need human review\n\n\u29bf Nothing: spam, automated notices, or irrelevant messages\nOnly the single word category is returned."
      },
      "typeVersion": 1
    },
    {
      "id": "53e0e46b-f29f-45d0-a417-df2fdf1f56bb",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -800,
        -144
      ],
      "parameters": {
        "width": 320,
        "height": 608,
        "content": "## Reply, Draft, or Nothing (Switch)\n\nPurpose: Routes emails based on AI categorization:\n\n\u29bf Reply \u2192 Generate Reply Email \u2192 Reply (auto-response sent immediately)\n\n\u29bf Draft \u2192 Generate Draft Email \u2192 Flag for Human Review (reply is drafted but stored for human validation before sending)\n\n\u29bf Nothing \u2192 No Operation (message ignored, nothing happens)"
      },
      "typeVersion": 1
    },
    {
      "id": "c4d14987-f859-4983-9adc-7e326554262c",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -448,
        -240
      ],
      "parameters": {
        "width": 672,
        "height": 576,
        "content": "## Generate Email Reply + Gemini Chat Reply + Reply\n\nPurpose:\n\n\u29bf Gemini writes a warm, professional final reply using the subject + body of the incoming message.\n\n\u29bf Reply node sends this AI-written response directly back to the sender, keeping the thread intact."
      },
      "typeVersion": 1
    },
    {
      "id": "2664562a-abe4-4857-ab53-82b8726a57ce",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -800,
        496
      ],
      "parameters": {
        "width": 320,
        "height": 400,
        "content": "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n## No Operation, do nothing\n\nPurpose: Handles the \u201cNothing\u201d category gracefully. The workflow ends here without any email action."
      },
      "typeVersion": 1
    },
    {
      "id": "447df387-dbca-40eb-990e-2d22906a0447",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -448,
        432
      ],
      "parameters": {
        "width": 672,
        "height": 592,
        "content": "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n## Generate Draft Email + Gemini Chat Draft + Flag for Human Review\n\nPurpose:\n\n\u29bf Gemini drafts a professional reply in the same style, but does not send it.\n\n\u29bf Draft is created in Gmail (with the sender and subject filled in).\n\n\u29bf A human can then review/edit before sending. "
      },
      "typeVersion": 1
    },
    {
      "id": "6ec6fb16-1f87-408d-8bd2-9211384cea2b",
      "name": "Reply, Draft, or Nothing",
      "type": "n8n-nodes-base.switch",
      "position": [
        -736,
        224
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "68b68c40-f028-4431-9940-66543b04578a",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output }}",
                    "rightValue": "Reply"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "7df0c8a2-3616-4e84-9af3-14a2e637cb7c",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output }}",
                    "rightValue": "Draft"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "799f30a4-15c6-45cb-8617-d9e2fe148536",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output }}",
                    "rightValue": "Nothing"
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "a4e6e81d-de73-4562-b37e-dfab96ba5fc4",
      "name": "Generate Draft Email",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -384,
        480
      ],
      "parameters": {
        "text": "=Reply to the email I give you in a warm, polite, and professional tone. Write like a thoughtful human\u2014use natural phrasing, gentle transitions, and avoid robotic or overly formal wording. Address all points clearly, keep it concise, and maintain a friendly, respectful feel. Reply must be easy to read. and don't output subject line, only output the reply. My name is hashir. must keep an eye on one thing, don't add in the reply anything like this \"[Your Company Name]\" etc. its a final reply so don't add anything that that needs any manual addition or editing. Output the final reply.\n\nHere is the Subject line: {{ $('Get a message').item.json.headers.subject }}\n\nHere is the Email:\n\n{{ $('Get a message').item.json.text }}",
        "options": {
          "batching": {
            "batchSize": 1,
            "delayBetweenBatches": 0
          },
          "passthroughBinaryImages": true,
          "returnIntermediateSteps": false
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "65df0884-ef4d-483b-a8d4-a9ba0e252f94",
      "name": "Generate Email Reply",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -384,
        32
      ],
      "parameters": {
        "text": "=Reply to the email I give you in a warm, polite, and professional tone. Write like a thoughtful human\u2014use natural phrasing, gentle transitions, and avoid robotic or overly formal wording. Address all points clearly, keep it concise, and maintain a friendly, respectful feel. Reply must be easy to read. and don't output subject line, only output the reply. My name is hashir. must keep an eye on one thing, don't add in the reply anything like this \"[Your Company Name]\" etc. its a final reply so don't add anything that needs any manual addition or editing. Output the final reply.\n\nHere is the Subject line: {{ $('Get a message').item.json.headers.subject }}\n\nHere is the Email:\n\n{{ $('Get a message').item.json.text }}",
        "options": {
          "passthroughBinaryImages": true,
          "returnIntermediateSteps": false
        },
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "f45ae81c-8201-46cd-ba7f-955b69070108",
      "name": "Reply Email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -96,
        32
      ],
      "parameters": {
        "message": "={{ $json.output }}",
        "options": {
          "appendAttribution": true,
          "replyToSenderOnly": false
        },
        "emailType": "text",
        "messageId": "={{ $('Get a message').item.json.id }}",
        "operation": "reply"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    }
  ],
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "82f0e737-89c4-45cd-b18c-2d01f85218da",
  "connections": {
    "Get a message": {
      "main": [
        [
          {
            "node": "AI Categorizer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Get a message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Categorizer": {
      "main": [
        [
          {
            "node": "Reply, Draft, or Nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini Chat Draft": {
      "ai_languageModel": [
        [
          {
            "node": "Generate Draft Email",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Gemini Chat Reply": {
      "ai_languageModel": [
        [
          {
            "node": "Generate Email Reply",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Generate Draft Email": {
      "main": [
        [
          {
            "node": "Flag for Human Review",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Email Reply": {
      "main": [
        [
          {
            "node": "Reply Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Categorizer",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Reply, Draft, or Nothing": {
      "main": [
        [
          {
            "node": "Generate Email Reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Generate Draft Email",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Operation, do nothing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}