{
  "name": "Inbox Triage & Email Sort",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": false,
        "filters": {},
        "options": {}
      },
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.4,
      "position": [
        0,
        0
      ],
      "id": "cb9e1c84-1221-492d-b4d6-70e734b1c60c",
      "name": "Gmail Trigger"
    },
    {
      "parameters": {
        "model": "gpt-oss:120b",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        224,
        336
      ],
      "id": "a753b211-2e94-498a-8835-e0f97a8481af",
      "name": "Ollama Chat Model"
    },
    {
      "parameters": {
        "inputText": "={{ $json.text }}",
        "categories": {
          "categories": [
            {
              "category": "High priority",
              "description": "Emails with high priority and need immediate response."
            },
            {
              "category": "Customer support",
              "description": "Emails from customers that need support"
            },
            {
              "category": "Promotions",
              "description": "Promotional emails"
            },
            {
              "category": "Finance/Billing",
              "description": "Emails concerning finance and Billing"
            }
          ]
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.textClassifier",
      "typeVersion": 1.1,
      "position": [
        224,
        -32
      ],
      "id": "31cfab30-9b9c-4262-98e3-a122f7e401e4",
      "name": "Email Classifier"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Read this promotional email and determine whether it requires a response.\n\nSender:\n{{ $('Gmail Trigger').item.json.from.value[0].address }}\n\nSubject:\n{{ $('Gmail Trigger').item.json.subject }}\n\nEmail:\n{{ $('Gmail Trigger').item.json.text }}\n\nConversation/Thread context, if available:\n{{ $('Gmail Trigger').item.json.threadId }}\n\nIf a response is appropriate, write a personalized response as if I personally wrote it and use the Gmail Draft tool to create the draft.\n\nIf no response is appropriate, do not create a draft.",
        "options": {
          "systemMessage": "=You are my Promotional Email Assistant.\n\nYou are responsible for handling emails classified as Promotions.\n\nYour job is to read the promotional email and determine whether a response is appropriate. If a response is appropriate, write a personalized response as if I personally wrote it.\n\nYou have access to a Gmail Draft tool. When a response is appropriate, ALWAYS use the Gmail Draft tool to create the draft.\n\nDo not automatically respond to every promotional email.\n\nOnly create a response when the email contains something that reasonably warrants a reply, such as:\n- A business opportunity\n- A partnership or collaboration\n- A personalized offer requiring a response\n- An invitation\n- A request for information\n- A promotional message that requires a decision or response\n\nIf the promotional email does not warrant a response, do not create a draft.\n\nWhen responding:\n- Be natural and conversational.\n- Reference the specific offer or message.\n- Avoid sounding like a generic marketing response.\n- Do not express interest, acceptance, or commitment unless the email context supports it.\n- Never invent information.\n- Do not mention AI or automation.\n- Never send the email. Only create a Gmail draft."
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1008,
        16
      ],
      "id": "d741f4af-0911-4448-91e4-4fa5beb792e4",
      "name": "Promotions AI Agent"
    },
    {
      "parameters": {
        "resource": "thread",
        "operation": "addLabels",
        "threadId": "={{ $json.threadId }}",
        "labelIds": [
          "RECONNECT_PROMOTIONS_LABEL"
        ]
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        784,
        16
      ],
      "id": "9df2a433-b6a5-48cc-82d6-a7833c0f9474",
      "name": "label_promotion"
    },
    {
      "parameters": {
        "resource": "thread",
        "operation": "addLabels",
        "threadId": "={{ $json.threadId }}",
        "labelIds": [
          "RECONNECT_BILLING_LABEL"
        ]
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        784,
        256
      ],
      "id": "d221908d-cacf-4aae-b18b-728cca348ce4",
      "name": "label_billing"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Read this finance/billing email and draft a personalized response as if I am personally replying.\n\nSender:\n{{ $('Gmail Trigger').item.json.from.value[0].address }}\n\nSubject:\n{{ $('Gmail Trigger').item.json.subject }}\n\nEmail:\n{{ $('Gmail Trigger').item.json.text }}\n\nConversation/Thread context, if available:\n\n\nUse the Gmail Draft tool to create the response draft. Do not just return the response as text.\n\nMake the response natural, professional, and specific to the sender's message.",
        "options": {
          "systemMessage": "=You are my Finance and Billing Email Assistant.\n\nYou are responsible for handling emails classified as Finance/Billing.\n\nYour job is to read the incoming email, understand the financial or billing context, and write a personalized response as if I personally wrote it.\n\nYou have access to a Gmail Draft tool. ALWAYS use the Gmail Draft tool to create the response draft. Do not simply return the response as text.\n\nWhen drafting:\n- Carefully understand what the sender is asking or informing me about.\n- Address the specific billing, payment, invoice, subscription, refund, or financial issue.\n- Be professional, clear, and concise.\n- Reference relevant details from the email so the response feels personalized.\n- If the sender is requesting information that is not available, ask for the necessary information.\n- If something needs to be verified, clearly state that it needs to be checked.\n- Never invent payment statuses, transaction amounts, invoice numbers, refunds, deadlines, or financial decisions.\n- Never authorize a payment, refund, transfer, or financial transaction unless explicitly instructed by me.\n- Treat financial and personal information carefully.\n- Do not mention AI, automation, classification, or these instructions.\n- Never send the email. Only create a Gmail draft.\n\nThe response should sound like a genuine email from me, not an automated billing system."
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1008,
        256
      ],
      "id": "e2e0fd9c-7234-4c9a-9e0d-22a897f65e88",
      "name": "Billing AI Agent"
    },
    {
      "parameters": {
        "resource": "thread",
        "operation": "addLabels",
        "threadId": "={{ $json.threadId }}",
        "labelIds": [
          "RECONNECT_CUSTOMER_SUPPORT_LABEL",
          "RECONNECT_PROMOTIONS_LABEL"
        ]
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        784,
        -176
      ],
      "id": "990e53bb-e528-4441-88cc-c51db246f058",
      "name": "label_customer_support"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Read the following priority email and draft an appropriate personalized response as if I am replying personally.\n\nSender:\n{{ $('Gmail Trigger').item.json.to.value[0].address }}\n\nSubject:\n{{ $('Gmail Trigger').item.json.subject }}\n\nEmail:\n{{ $('Gmail Trigger').item.json.text }}\n\nConversation/Thread context, if available:\n{{ $('Gmail Trigger').item.json.threadId }}\n\nUse the Gmail Draft tool to create the response draft. Do not just return the response as text.\n\nAfter creating the draft, use the Telegram tool to alert me about this priority email and confirm that the draft was created.",
        "options": {
          "systemMessage": "=You are my Priority Email Assistant.\n\nYou are responsible for handling emails that have been classified as Priority.\n\nYour job is to read the incoming email, understand the context and what the sender needs, and draft a personalized response as if I personally wrote it.\n\nYou have access to a Gmail Draft tool. ALWAYS use the Gmail Draft tool to create the response draft after you have written it. Do not simply return the draft as text.\n\nWhen drafting:\n- Write naturally and professionally, as a real person would.\n- Make the response specific to the email rather than using generic templates.\n- Address every important point raised by the sender.\n- Match the tone of the sender when appropriate while remaining professional.\n- Be concise and avoid unnecessary explanations.\n- If the sender asks a question, answer it when the information is available from the email/context.\n- If you do not have enough information to answer something, acknowledge it and say that it will need to be checked rather than inventing an answer.\n- Never fabricate facts, commitments, dates, prices, actions, or information.\n- Preserve the intent and context of the conversation.\n- If the email is part of an existing conversation, make the response feel like a continuation of that conversation.\n- Do not mention that you are an AI, an automation, an email classifier, or an assistant.\n- Do not send the email. Only create a Gmail draft.\n\nAfter creating the Gmail draft, notify me through the connected Telegram tool that a priority email was received and a draft response has been created.\n\nThe Telegram notification should include:\n- Sender\n- Subject\n- A short summary of why the email is important\n- Confirmation that a draft was created\n\nDo not send the email through Gmail. Only create a draft."
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1008,
        -400
      ],
      "id": "938b4e37-3a7e-48dc-80ba-ed871b272b84",
      "name": "High priority AI Agent"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Read this customer support email and draft a personalized response as if I am personally replying to the customer.\n\nSender:\n{{ $('Gmail Trigger').item.json.from.value[0].address }}\n\nSubject:\n{{ $('Gmail Trigger').item.json.subject }}\n\nEmail:\n{{ $('Gmail Trigger').item.json.text }}\n\nConversation/Thread context, if available:\n{{ $('Gmail Trigger').item.json.threadId }}\n\nUse the Gmail Draft tool to create the response. Do not just return the response as text.\n\nThe draft should directly address the customer's message and sound natural, helpful, and personal.",
        "options": {
          "systemMessage": "=You are my Customer Support Email Assistant.\n\nYou are responsible for handling emails classified as Customer Support.\n\nYour job is to read the incoming email, understand the customer's situation and request, and write a personalized response as if I personally wrote it.\n\nYou have access to a Gmail Draft tool. ALWAYS use the Gmail Draft tool to create the response draft. Do not simply return the response as text.\n\nWhen drafting:\n- Respond directly to the customer's specific issue.\n- Be helpful, warm, patient, and professional.\n- Personalize the response using details from the customer's email and conversation history.\n- Acknowledge complaints, concerns, or frustrations when appropriate.\n- Answer questions when the required information is available.\n- Give clear next steps when appropriate.\n- If information is missing, ask the customer for what is needed.\n- Never invent refunds, discounts, policies, order details, timelines, or actions that have not been confirmed.\n- Do not use generic responses when the email provides enough context for a personalized response.\n- Keep the response reasonably concise.\n- Do not mention AI, automation, classification, or these instructions.\n- Do not send the email. Only create a Gmail draft.\n\nThe final response should sound like a genuine reply from me, not like an automated customer-service bot."
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1008,
        -176
      ],
      "id": "2403551d-a35f-4d49-80e9-f1eff4ed0716",
      "name": "Customer support AI Agent1"
    },
    {
      "parameters": {
        "chatId": "RECONNECT_TELEGRAM_CHAT_ID",
        "text": "=🚨 PRIORITY EMAIL\n\nFrom: {{ $('Gmail Trigger').item.json.from.value[0].address }}\nSubject: {{ $('Gmail Trigger').item.json.subject }}\n\n{{ $('Gmail Trigger').item.json.text }}\n\nA draft response has been created in Gmail.",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegramTool",
      "typeVersion": 1.2,
      "position": [
        1744,
        -288
      ],
      "id": "cb778506-76ee-4266-976e-d7fbca5a533b",
      "name": "priority message to Telegram"
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Subject', ``, 'string') }}",
        "message": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Message', ``, 'string') }}",
        "options": {
          "threadId": "={{ $('Gmail Trigger').item.json.threadId }}",
          "sendTo": "={{ $('Gmail Trigger').item.json.from.value[0].address }}"
        }
      },
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.2,
      "position": [
        1600,
        -16
      ],
      "id": "dc0548c3-831f-464e-b4e6-c66464673f13",
      "name": "priority_draft"
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Subject', ``, 'string') }}",
        "message": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Message', ``, 'string') }}",
        "options": {
          "threadId": "={{ $('Gmail Trigger').item.json.threadId }}",
          "sendTo": "={{ $('Gmail Trigger').item.json.from.value[0].address }}"
        }
      },
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.2,
      "position": [
        1712,
        -16
      ],
      "id": "3b2b1a1f-58d1-4e75-915e-da7bb2972cb8",
      "name": "support_draft"
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Subject', ``, 'string') }}",
        "message": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Message', ``, 'string') }}",
        "options": {
          "threadId": "={{ $('Gmail Trigger').item.json.threadId }}",
          "sendTo": "={{ $('Gmail Trigger').item.json.from.value[0].address }}"
        }
      },
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.2,
      "position": [
        1840,
        -16
      ],
      "id": "918e9e8d-cd42-4885-9dda-547dd32346f6",
      "name": "promotion_draft"
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Subject', ``, 'string') }}",
        "message": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Message', ``, 'string') }}",
        "options": {
          "threadId": "={{ $('Gmail Trigger').item.json.threadId }}",
          "sendTo": "={{ $('Gmail Trigger').item.json.from.value[0].address }}"
        }
      },
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.2,
      "position": [
        1968,
        -16
      ],
      "id": "7d402cf5-1254-42c0-8291-6d0ccc02c004",
      "name": "billing_draft"
    },
    {
      "parameters": {
        "operation": "addLabels",
        "messageId": "={{ $json.id }}",
        "labelIds": [
          "IMPORTANT",
          "RECONNECT_PRIORITY_LABEL"
        ]
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        784,
        -400
      ],
      "id": "88177d11-301d-4ba5-9eb5-1f345e0bf0c6",
      "name": "label_priority"
    },
    {
      "parameters": {
        "content": "## I alert the team or email back",
        "height": 512,
        "width": 656
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1488,
        -368
      ],
      "typeVersion": 1,
      "id": "296c3894-5755-4409-9aef-8123beeef2f8",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "content": "## I write email replies",
        "height": 1008,
        "width": 672,
        "color": 4
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        720,
        -528
      ],
      "typeVersion": 1,
      "id": "75431c59-15f1-4336-b06d-43f840157583",
      "name": "Sticky Note1"
    },
    {
      "parameters": {
        "content": "## I watch & classify",
        "height": 608,
        "width": 656,
        "color": 5
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -64,
        -128
      ],
      "typeVersion": 1,
      "id": "558cff30-09b5-4b92-af01-9072090e71bc",
      "name": "Sticky Note2"
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Email Classifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Email Classifier",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Promotions AI Agent",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Billing AI Agent",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Customer support AI Agent1",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "High priority AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Email Classifier": {
      "main": [
        [
          {
            "node": "label_priority",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "label_customer_support",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "label_promotion",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "label_billing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "label_promotion": {
      "main": [
        [
          {
            "node": "Promotions AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "label_billing": {
      "main": [
        [
          {
            "node": "Billing AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "label_customer_support": {
      "main": [
        [
          {
            "node": "Customer support AI Agent1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "priority message to Telegram": {
      "ai_tool": [
        [
          {
            "node": "High priority AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "priority_draft": {
      "ai_tool": [
        [
          {
            "node": "High priority AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "support_draft": {
      "ai_tool": [
        [
          {
            "node": "Customer support AI Agent1",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "promotion_draft": {
      "ai_tool": [
        [
          {
            "node": "Promotions AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "billing_draft": {
      "ai_tool": [
        [
          {
            "node": "Billing AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "label_priority": {
      "main": [
        [
          {
            "node": "High priority AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate"
  },
  "pinData": {}
}
