{
  "name": "AI Email Triage — Label, Draft Replies & Flag Urgent",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": false,
        "filters": {
          "q": "in:inbox -category:promotions",
          "readStatus": "unread"
        },
        "options": {}
      },
      "id": "e1f2a3b4-1111-4e2f-8a3b-0c1d2e3f4a5b",
      "name": "Gmail Trigger: New Email",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "position": [-240, 300],
      "credentials": {
        "gmailOAuth2": {
          "id": "YOUR_GMAIL_CREDENTIAL",
          "name": "YOUR_GMAIL_CREDENTIAL"
        }
      }
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list",
          "cachedResultName": "gpt-4o-mini"
        },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are an executive email triage assistant. Classify each email into EXACTLY one of these 5 categories:\n- \"urgent\": time-sensitive, requires action within hours (deadlines, outages, angry client, legal, boss asking for something)\n- \"needs_reply\": expects a personal response but is not urgent (questions, intros, scheduling)\n- \"fyi\": informational, no response needed (reports, confirmations, CCs)\n- \"newsletter\": marketing emails, digests, product updates, promotions\n- \"spam_or_cold\": cold outreach, unsolicited sales pitches, suspicious mail\n\nIf category is \"needs_reply\", also write draft_reply: a short, polite reply in my voice (2-3 sentences, no subject line, sign off with just 'Best'). Otherwise draft_reply must be null.\n\nRespond ONLY with strict valid JSON, no markdown fences, exactly this shape:\n{\"category\": \"urgent\"|\"needs_reply\"|\"fyi\"|\"newsletter\"|\"spam_or_cold\", \"confidence\": number, \"one_line_summary\": string, \"draft_reply\": string|null}"
            },
            {
              "role": "user",
              "content": "=Classify this email:\nFrom: {{ $json.from?.value?.[0]?.address ?? $json.From }}\nSubject: {{ $json.subject ?? $json.Subject }}\nBody:\n{{ ($json.text ?? $json.snippet ?? '').slice(0, 3000) }}"
            }
          ]
        },
        "jsonOutput": true,
        "options": {
          "temperature": 0
        }
      },
      "id": "f2a3b4c5-2222-4f3a-9b4c-1d2e3f4a5b6c",
      "name": "OpenAI: Classify Email",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [0, 300],
      "credentials": {
        "openAiApi": {
          "id": "YOUR_OPENAI_CREDENTIAL",
          "name": "YOUR_OPENAI_CREDENTIAL"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Merge classification with the original Gmail message metadata\nconst ai = $input.first().json.message?.content ?? $input.first().json;\nconst mail = $('Gmail Trigger: New Email').first().json;\n\nreturn [{\n  json: {\n    category: ai.category,\n    confidence: ai.confidence,\n    summary: ai.one_line_summary,\n    draft_reply: ai.draft_reply,\n    message_id: mail.id,\n    thread_id: mail.threadId,\n    from: mail.from?.value?.[0]?.address ?? mail.From,\n    subject: mail.subject ?? mail.Subject,\n  },\n}];"
      },
      "id": "a3b4c5d6-3333-4a4b-8c5d-2e3f4a5b6c7d",
      "name": "Code: Merge Classification",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [240, 300]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "b4c5d6e7-4441-4b5c-9d6e-3f4a5b6c7d8e",
                    "leftValue": "={{ $json.category }}",
                    "rightValue": "urgent",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Urgent"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "b4c5d6e7-4442-4b5c-9d6e-3f4a5b6c7d8f",
                    "leftValue": "={{ $json.category }}",
                    "rightValue": "needs_reply",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Needs Reply"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "b4c5d6e7-4443-4b5c-9d6e-3f4a5b6c7d90",
                    "leftValue": "={{ $json.category }}",
                    "rightValue": "urgent",
                    "operator": {
                      "type": "string",
                      "operation": "notEquals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Label Only"
            }
          ]
        },
        "options": {}
      },
      "id": "b4c5d6e7-4444-4b5c-9d6e-3f4a5b6c7d80",
      "name": "Switch: By Category",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [480, 300]
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "#inbox-urgent",
          "mode": "name"
        },
        "text": "=:rotating_light: *Urgent email* from {{ $json.from }}\nSubject: {{ $json.subject }}\nSummary: {{ $json.summary }}\nOpen: https://mail.google.com/mail/u/0/#inbox/{{ $json.thread_id }}",
        "otherOptions": {}
      },
      "id": "c5d6e7f8-5555-4c6d-8e7f-4a5b6c7d8e9f",
      "name": "Slack: Urgent Alert",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [740, 120],
      "credentials": {
        "slackApi": {
          "id": "YOUR_SLACK_CREDENTIAL",
          "name": "YOUR_SLACK_CREDENTIAL"
        }
      }
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "=Re: {{ $json.subject }}",
        "emailType": "text",
        "message": "={{ $json.draft_reply }}",
        "options": {
          "threadId": "={{ $json.thread_id }}",
          "sendTo": "={{ $json.from }}"
        }
      },
      "id": "d6e7f8a9-6666-4d7e-9f8a-5b6c7d8e9f0a",
      "name": "Gmail: Create Draft Reply",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [740, 300],
      "credentials": {
        "gmailOAuth2": {
          "id": "YOUR_GMAIL_CREDENTIAL",
          "name": "YOUR_GMAIL_CREDENTIAL"
        }
      }
    },
    {
      "parameters": {
        "operation": "addLabels",
        "messageId": "={{ $json.message_id }}",
        "labelIds": "={{ ({ needs_reply: ['YOUR_LABEL_ID_NEEDS_REPLY'], fyi: ['YOUR_LABEL_ID_FYI'], newsletter: ['YOUR_LABEL_ID_NEWSLETTER'], spam_or_cold: ['YOUR_LABEL_ID_COLD'] })[$json.category] ?? ['YOUR_LABEL_ID_FYI'] }}"
      },
      "id": "e7f8a9b0-7777-4e8f-8a9b-6c7d8e9f0a1b",
      "name": "Gmail: Apply Label",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [740, 480],
      "credentials": {
        "gmailOAuth2": {
          "id": "YOUR_GMAIL_CREDENTIAL",
          "name": "YOUR_GMAIL_CREDENTIAL"
        }
      }
    }
  ],
  "connections": {
    "Gmail Trigger: New Email": {
      "main": [
        [
          {
            "node": "OpenAI: Classify Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI: Classify Email": {
      "main": [
        [
          {
            "node": "Code: Merge Classification",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Merge Classification": {
      "main": [
        [
          {
            "node": "Switch: By Category",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch: By Category": {
      "main": [
        [
          {
            "node": "Slack: Urgent Alert",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Gmail: Create Draft Reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Gmail: Apply Label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
