{
  "name": "AI Smart Reply Generator",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "filters": {
          "readStatus": "unread"
        },
        "options": {}
      },
      "id": "b1b2c3d4-2002-4000-8000-000000000001",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.labelIds }}",
              "operation": "notContains",
              "value2": "SENT"
            }
          ]
        }
      },
      "id": "b1b2c3d4-2002-4000-8000-000000000002",
      "name": "Filter Sent Emails",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "model": "gpt-4",
        "messages": {
          "values": [
            {
              "content": "You are a professional email assistant. Draft a reply to the following email. Be polite, concise, and professional. Match the tone of the original email.\n\nFrom: {{ $json.from }}\nSubject: {{ $json.subject }}\nBody: {{ $json.text || $json.snippet }}\n\nReturn JSON with keys:\n- replySubject (include Re: prefix)\n- replyBody (the drafted reply text)\n- tone (detected tone: formal, casual, urgent)\n- confidence (0-1 score of how confident the reply is appropriate)"
            }
          ]
        },
        "options": {
          "temperature": 0.6,
          "maxTokens": 1000
        }
      },
      "id": "b1b2c3d4-2002-4000-8000-000000000003",
      "name": "OpenAI Draft Reply",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.3,
      "position": [
        750,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const response = JSON.parse($input.first().json.message.content);\nconst emailData = $('Gmail Trigger').first().json;\nreturn [{\n  json: {\n    originalMessageId: emailData.id,\n    threadId: emailData.threadId,\n    to: emailData.from,\n    subject: response.replySubject,\n    body: response.replyBody,\n    tone: response.tone,\n    confidence: response.confidence\n  }\n}];"
      },
      "id": "b1b2c3d4-2002-4000-8000-000000000004",
      "name": "Parse Reply",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "resource": "draft",
        "operation": "create",
        "subject": "={{ $json.subject }}",
        "toList": "={{ $json.to }}",
        "message": "={{ $json.body }}",
        "options": {
          "threadId": "={{ $json.threadId }}"
        }
      },
      "id": "b1b2c3d4-2002-4000-8000-000000000005",
      "name": "Create Gmail Draft",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1250,
        300
      ]
    },
    {
      "parameters": {
        "channel": "#email-drafts",
        "text": ":email: Draft reply created\n*To:* {{ $json.to }}\n*Subject:* {{ $json.subject }}\n*Tone:* {{ $('Parse Reply').first().json.tone }}\n*Confidence:* {{ $('Parse Reply').first().json.confidence }}\n\nPlease review the draft in Gmail before sending.",
        "otherOptions": {}
      },
      "id": "b1b2c3d4-2002-4000-8000-000000000006",
      "name": "Slack Notify",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1500,
        300
      ]
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Filter Sent Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Sent Emails": {
      "main": [
        [
          {
            "node": "OpenAI Draft Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Draft Reply": {
      "main": [
        [
          {
            "node": "Parse Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Reply": {
      "main": [
        [
          {
            "node": "Create Gmail Draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Gmail Draft": {
      "main": [
        [
          {
            "node": "Slack Notify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}