{
  "name": "Improve AI support email drafts with Gmail, OpenAI and PostgreSQL",
  "nodes": [
    {
      "id": "5c7d4f79-b479-4c40-a092-0934e2e87169",
      "name": "⏰ Schedule - Every 3 Hours",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -2352,
        640
      ]
    },
    {
      "id": "fc095fb7-cdad-4f45-8384-d533101cbfe7",
      "name": "🗄️ DB - Get Last Watermark",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -2176,
        640
      ]
    },
    {
      "id": "43c7801f-2e51-4970-a498-c90de6cb3283",
      "name": "⚙️ Set Watermark",
      "type": "n8n-nodes-base.code",
      "position": [
        -1952,
        640
      ]
    },
    {
      "id": "1fbc336f-2d9a-45ff-8448-11a585b2ed94",
      "name": "🗄️ DB - Start Run Log",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -1728,
        640
      ]
    },
    {
      "id": "424f2c18-4a90-414d-85af-00f6ec627dfa",
      "name": "⚙️ Carry Run Context",
      "type": "n8n-nodes-base.code",
      "position": [
        -1504,
        640
      ]
    },
    {
      "id": "08bf8015-b08f-42a2-9c58-4c5719fd0723",
      "name": "📧 Gmail - Fetch Sent Emails",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -1280,
        640
      ]
    },
    {
      "id": "d21b20f9-d259-440f-a426-00b08189b29c",
      "name": "🔄 Loop - Sent Emails",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -1056,
        640
      ]
    },
    {
      "id": "8d74b94a-7d0f-4575-8de6-883e8e72ce2c",
      "name": "🗄️ DB - Match Thread ID",
      "type": "n8n-nodes-base.postgres",
      "position": [
        16,
        912
      ]
    },
    {
      "id": "4f9b5c5b-e2b2-48dc-9e22-794577d816da",
      "name": "❓ IF - Draft Match Found?",
      "type": "n8n-nodes-base.if",
      "position": [
        224,
        912
      ]
    },
    {
      "id": "fb1cd898-8b4c-4d4f-9945-7b03e1e489e2",
      "name": "❓ IF - Already Processed?",
      "type": "n8n-nodes-base.if",
      "position": [
        -784,
        288
      ]
    },
    {
      "id": "f982e930-4766-4015-b4e5-1f4882351e62",
      "name": "🤖 AI - Compare Draft vs Sent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -576,
        272
      ]
    },
    {
      "id": "421e6376-4c88-45c8-b1ea-3127e51682fd",
      "name": "OpenAI Chat Model - Compare",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -320,
        144
      ]
    },
    {
      "id": "6e4090dc-8610-45f7-af03-df16ba65a569",
      "name": "⚙️ Parse AI Comparison",
      "type": "n8n-nodes-base.code",
      "position": [
        -224,
        272
      ]
    },
    {
      "id": "a287218e-4a5e-4dab-99ea-0e33da8b1873",
      "name": "❓ IF - Approved As-Is?",
      "type": "n8n-nodes-base.if",
      "position": [
        0,
        272
      ]
    },
    {
      "id": "0155b7f2-bcd4-4819-a00e-e7d07b06485a",
      "name": "🗄️ DB - Mark Approved As-Is",
      "type": "n8n-nodes-base.postgres",
      "position": [
        224,
        256
      ]
    },
    {
      "id": "c8362985-ba49-4170-a9ae-bb10647966fd",
      "name": "🔢 Generate Embedding - Human Sent",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        64,
        528
      ]
    },
    {
      "id": "37c6deac-6deb-4b41-9270-3e817766f76b",
      "name": "⚙️ Extract Embedding",
      "type": "n8n-nodes-base.code",
      "position": [
        288,
        528
      ]
    },
    {
      "id": "2e318078-fee1-4729-a59b-1816a7db3188",
      "name": "🗄️ DB - Save Correction",
      "type": "n8n-nodes-base.postgres",
      "position": [
        512,
        528
      ]
    },
    {
      "id": "33d6c979-c122-4efc-9e54-21ece23e788e",
      "name": "🗄️ DB - Mark Draft Processed",
      "type": "n8n-nodes-base.postgres",
      "position": [
        736,
        528
      ]
    },
    {
      "id": "afffe5e1-9615-4342-939a-4202a3dc163f",
      "name": "❓ IF - KB Update Needed?",
      "type": "n8n-nodes-base.if",
      "position": [
        944,
        528
      ]
    },
    {
      "id": "ac859ee7-262e-4f49-897c-0b8ee107248e",
      "name": "🗄️ DB - Fetch KB Entry to Update",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1168,
        512
      ]
    },
    {
      "id": "87e04269-1321-4be0-9466-eb806f54b907",
      "name": "🤖 AI - Rewrite KB Answer",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        1360,
        512
      ]
    },
    {
      "id": "d3eb84c2-227a-466f-80f2-57dbf087e1cf",
      "name": "🗄️ DB - Update KB Entry",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1696,
        512
      ]
    },
    {
      "id": "ca26d98e-853e-4d13-9dc0-4b8cfd229624",
      "name": "🗄️ DB - Mark KB Updated",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1888,
        624
      ]
    },
    {
      "id": "a3437287-ee6e-46bb-b8a4-f5f2bc4795c3",
      "name": "🗄️ DB - Complete Run Log",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -784,
        560
      ]
    },
    {
      "id": "554c5627-7ed7-4234-864b-498f40054607",
      "name": "📧 Gmail - Fetch Full Message",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -384,
        912
      ]
    },
    {
      "id": "5bd58454-bc56-4850-9f90-e8c53cbeaeeb",
      "name": "⚙️ Parse Full Message Body",
      "type": "n8n-nodes-base.code",
      "position": [
        -192,
        912
      ]
    },
    {
      "id": "b47598a7-6bbd-4cb8-a30f-d6e3415949d6",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3168,
        32
      ],
      "parameters": {
        "width": 656,
        "height": 848,
        "content": "## Self-learning feedback loop for AI email drafts\n\nThis workflow runs every 3 hours and checks which AI-generated \nemail drafts have been reviewed and sent by the support team. \nIt compares the AI dr"
      }
    },
    {
      "id": "2ca9131b-ae98-41be-ac18-2e94c02d6155",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2496,
        480
      ],
      "parameters": {
        "width": 1360,
        "height": 400,
        "content": "## Schedule & Fetch Emails\n\nRuns every 3 hours. Fetches last_processed_sent_at from the \nmost recent completed run as a watermark — so only new sent \nemails are fetched each time. First-ever run defau"
      }
    },
    {
      "id": "4e9841ac-1ea9-4fae-a97e-8cca892f5771",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -864,
        832
      ],
      "parameters": {
        "width": 1280,
        "height": 336,
        "content": "## Loop & thread matching\n\nProcesses one sent email at a time. Full message body is fetched \nvia Gmail API separately — the Sent folder list only returns a \nsnippet. Each email is matched to an AI dra"
      }
    },
    {
      "id": "2aebc058-79a7-4de6-9fd7-16593f15919a",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -864,
        -16
      ],
      "parameters": {
        "width": 1280,
        "height": 480,
        "content": "## AI comparison & branching\n\nGPT-4o-mini compares the AI draft vs human-sent email and returns \nedit type, a plain-English diff summary, and whether a KB update \nis needed.\n\nApproved as-is → mark dra"
      }
    },
    {
      "id": "d41b364f-5010-4a01-95dd-564bf474b2e5",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -464,
        480
      ],
      "parameters": {
        "width": 2528,
        "height": 336,
        "content": "## Corrections & KB update\n\nHuman-edited pairs are embedded and saved to the corrections \ntable — this is what Workflow 1 queries for similarity search \nwhen drafting future replies.\n\nIf the edit cont"
      }
    }
  ],
  "connections": {
    "⚙️ Set Watermark": {
      "main": [
        [
          {
            "node": "🗄️ DB - Start Run Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🔄 Loop - Sent Emails": {
      "main": [
        [
          {
            "node": "🗄️ DB - Complete Run Log",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "📧 Gmail - Fetch Full Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "⚙️ Carry Run Context": {
      "main": [
        [
          {
            "node": "📧 Gmail - Fetch Sent Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "⚙️ Extract Embedding": {
      "main": [
        [
          {
            "node": "🗄️ DB - Save Correction",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "❓ IF - Approved As-Is?": {
      "main": [
        [
          {
            "node": "🗄️ DB - Mark Approved As-Is",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "🔢 Generate Embedding - Human Sent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "⚙️ Parse AI Comparison": {
      "main": [
        [
          {
            "node": "❓ IF - Approved As-Is?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "❓ IF - KB Update Needed?": {
      "main": [
        [
          {
            "node": "🗄️ DB - Fetch KB Entry to Update",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "🔄 Loop - Sent Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🗄️ DB - Start Run Log": {
      "main": [
        [
          {
            "node": "⚙️ Carry Run Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model - Compare": {
      "ai_languageModel": [
        [
          {
            "node": "🤖 AI - Compare Draft vs Sent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "❓ IF - Already Processed?": {
      "main": [
        [
          {
            "node": "🤖 AI - Compare Draft vs Sent",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "🔄 Loop - Sent Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "❓ IF - Draft Match Found?": {
      "main": [
        [
          {
            "node": "❓ IF - Already Processed?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "🔄 Loop - Sent Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🤖 AI - Rewrite KB Answer": {
      "main": [
        [
          {
            "node": "🗄️ DB - Update KB Entry",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "⏰ Schedule - Every 3 Hours": {
      "main": [
        [
          {
            "node": "🗄️ DB - Get Last Watermark",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🗄️ DB - Mark KB Updated": {
      "main": [
        [
          {
            "node": "🔄 Loop - Sent Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🗄️ DB - Match Thread ID": {
      "main": [
        [
          {
            "node": "❓ IF - Draft Match Found?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🗄️ DB - Save Correction": {
      "main": [
        [
          {
            "node": "🗄️ DB - Mark Draft Processed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🗄️ DB - Update KB Entry": {
      "main": [
        [
          {
            "node": "🗄️ DB - Mark KB Updated",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "⚙️ Parse Full Message Body": {
      "main": [
        [
          {
            "node": "🗄️ DB - Match Thread ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "📧 Gmail - Fetch Sent Emails": {
      "main": [
        [
          {
            "node": "🔄 Loop - Sent Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "📧 Gmail - Fetch Full Message": {
      "main": [
        [
          {
            "node": "⚙️ Parse Full Message Body",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🗄️ DB - Get Last Watermark": {
      "main": [
        [
          {
            "node": "⚙️ Set Watermark",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🤖 AI - Compare Draft vs Sent": {
      "main": [
        [
          {
            "node": "⚙️ Parse AI Comparison",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🗄️ DB - Mark Approved As-Is": {
      "main": [
        [
          {
            "node": "🔄 Loop - Sent Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🗄️ DB - Mark Draft Processed": {
      "main": [
        [
          {
            "node": "❓ IF - KB Update Needed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🔢 Generate Embedding - Human Sent": {
      "main": [
        [
          {
            "node": "⚙️ Extract Embedding",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "🗄️ DB - Fetch KB Entry to Update": {
      "main": [
        [
          {
            "node": "🤖 AI - Rewrite KB Answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}