{
  "id": "mYOyC65TtRuJhvzs",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Send Gmail replies by analyzing Thread ID",
  "tags": [],
  "nodes": [
    {
      "id": "c1469689-3e67-4a07-9d5e-374552d164e7",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        -464,
        -272
      ],
      "parameters": {
        "filters": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "072aba43-704d-4663-a6e7-a5f7e9ad942d",
      "name": "Get a thread",
      "type": "n8n-nodes-base.gmail",
      "position": [
        128,
        -64
      ],
      "parameters": {
        "options": {
          "returnOnlyMessages": true
        },
        "resource": "thread",
        "threadId": "={{ $json.threadId }}",
        "operation": "get"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "972b0ab6-a6a4-4409-bc7c-4b06f3f06255",
      "name": "Create a draft",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1232,
        -64
      ],
      "parameters": {
        "message": "={{ $json.output }}",
        "options": {
          "ccList": "={{ $ifEmpty($('Gmail Trigger').item.json.Cc, '') }}",
          "sendTo": "={{ $('Gmail Trigger').item.json.From }}",
          "threadId": "={{ $('Gmail Trigger').item.json.threadId }}"
        },
        "subject": "={{ $('Gmail Trigger').item.json.Subject }}",
        "resource": "draft",
        "emailType": "html"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "9b255334-0339-4d8c-932d-3e7b759476b4",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        960,
        -256
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineAll"
      },
      "typeVersion": 3.2
    },
    {
      "id": "67777b0c-98d3-46dd-bc8c-036946048aa1",
      "name": "OpenAI Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -304,
        144
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-5-nano",
          "cachedResultName": "gpt-5-nano"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "7e958566-cf0f-4b20-bb7a-1e5115aa0635",
      "name": "Email Classifier",
      "type": "@n8n/n8n-nodes-langchain.textClassifier",
      "position": [
        -224,
        -64
      ],
      "parameters": {
        "options": {
          "fallback": "discard",
          "enableAutoFixing": true,
          "systemPromptTemplate": "Please classify the text provided by the user into one of the following categories: {categories}, and use the provided formatting instructions below. Don't explain, and only output the json."
        },
        "inputText": "={{ $json.snippet }}",
        "categories": {
          "categories": [
            {
              "category": "ok",
              "description": "Non-advertising, non-newsletter, non-spam emails that deserve a response"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "84a4f669-17af-4dbb-8ccf-660751316e38",
      "name": "Replying email Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        560,
        -64
      ],
      "parameters": {
        "text": "={{ JSON.stringify($json.data) }}",
        "options": {
          "systemMessage": "You are an expert in replying to emails that arrive in my inbox.  \nYou will be provided with the conversation history.  \nAnalyze it carefully and draft a reply.  \n\n- Do not invent information.  \n- If some variables are missing or unknown, insert clear placeholders like {{variable}}.  \n- Your output must be ONLY the email reply in valid HTML format, without any preamble, notes, or explanations.  "
        },
        "promptType": "define"
      },
      "typeVersion": 2.2
    },
    {
      "id": "7496e0e8-05d8-4f37-b6a7-abe91d7fea73",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        336,
        -64
      ],
      "parameters": {
        "jsCode": "const results = [];\n\nfor (const item of $input.all()) {\n  let emails = item.json;\n\n  // Normalizza in array:\n  if (!emails) {\n    emails = [];\n  } else if (!Array.isArray(emails)) {\n    // Se \u00e8 avvolto in una propriet\u00e0 comune\n    if (Array.isArray(emails.emails)) {\n      emails = emails.emails;\n    } else if (Array.isArray(emails.messages)) {\n      emails = emails.messages;\n    } else if (typeof emails === 'object' && (emails.id || emails.snippet || emails.From || emails.To)) {\n      // singolo oggetto email -> mettilo in array\n      emails = [emails];\n    } else {\n      // fallback: non iterabile\n      emails = [];\n    }\n  }\n\n  for (const email of emails) {\n    results.push({\n      snippet: email.snippet ?? '',\n      from: email.From ?? email.from ?? '',\n      to: email.To ?? email.to ?? '',\n    });\n  }\n}\n\n// Output unico con propriet\u00e0 \"data\"\nreturn [\n  {\n    json: {\n      data: results,\n    },\n  },\n];\n\n\n"
      },
      "typeVersion": 2
    },
    {
      "id": "6d5cc052-a0b6-403d-a6ea-efaed97ab351",
      "name": "Anthropic Sonnet 4.5",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "position": [
        496,
        144
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "claude-sonnet-4-5-20250929",
          "cachedResultName": "Claude Sonnet 4.5"
        },
        "options": {}
      },
      "credentials": {
        "anthropicApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "a9070e89-5e15-4110-b9a7-3f88fdde7990",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -480,
        -848
      ],
      "parameters": {
        "color": 3,
        "width": 704,
        "height": 208,
        "content": "## AI-Powered Gmail Assistant: send replies by analyzing Thread ID with Sonnet 4.5\n \nThis workflow automates **analyzing Gmail threads and drafting AI-powered replies** with the new model Anthropic **Sonnet 4.5**.\n\nThis workflow automates the process of analyzing incoming emails and generating context-aware draft replies by examining the entire email thread."
      },
      "typeVersion": 1
    },
    {
      "id": "9c238691-41c5-4f32-98f6-6ac1239063b1",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -480,
        -608
      ],
      "parameters": {
        "width": 704,
        "height": 224,
        "content": "## STEP 1\n    \n*   **Gmail OAuth2 Credentials:** The workflow uses the same Gmail account for the trigger, fetching threads, and creating drafts. Configure this in the \"Gmail Trigger,\" \"Get a thread,\" and \"Create a draft\" nodes.\n *   **OpenAI API Credentials:** Required for both the \"Email Classifier\". Provide your API key in the respective OpenAI Chat Model nodes.\n  \n *   **Anthropic API Credentials:** Required for the main \"Replying email Agent.\" Provide your API key in the respective Antrhopic Chat Model nodes."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "9ddd07a7-fb73-4016-b1d1-90e59be8be84",
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Replying email Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Create a draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get a thread": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          },
          {
            "node": "Email Classifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email Classifier": {
      "main": [
        [
          {
            "node": "Get a thread",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Email Classifier",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Anthropic Sonnet 4.5": {
      "ai_languageModel": [
        [
          {
            "node": "Replying email Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Replying email Agent": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  }
}