{
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "name": "Email Triage Agent (Metaworld demo)",
  "nodes": [
    {
      "id": "4dcfec75-9bd4-4870-b823-599e0b06d9b7",
      "name": "Setup and workflow guide",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -560,
        -120
      ],
      "parameters": {
        "width": 480,
        "height": 680,
        "content": "## Email Triage Agent\n\nClassifies inbound email into urgent, invoice, or routine paths. A confidence gate stops uncertain results for human review, and every completed path writes an audit record. This template never sends an email automatically.\n\n### Before you run it\n\n1. Connect IMAP and OpenAI credentials.\n2. Connect Slack, Google Drive, Google Sheets, and Gmail.\n3. Replace the placeholder sheet IDs, channel names, Drive destination, and Gmail label.\n4. Test each route with non-sensitive sample email.\n5. Confirm the failure workflow and credentialed nodes before activation.\n\n### Safe customization\n\nAdjust the categories and confidence threshold together. Keep the human-review branch for uncertain results and preserve the final audit step."
      },
      "typeVersion": 1
    },
    {
      "id": "1ad68673-82b5-4cc8-93fb-9952e73ed269",
      "name": "Read and classify guide",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -40,
        60
      ],
      "parameters": {
        "color": 5,
        "width": 440,
        "height": 190,
        "content": "## 1. Read and classify\n\nThe IMAP trigger reads new mail. OpenAI returns only a category, confidence score, and one-sentence reason. Use a dedicated mailbox or folder and test the JSON shape before activation."
      },
      "typeVersion": 1
    },
    {
      "id": "4ebbf793-a0dc-4cd3-b036-9f7721432e91",
      "name": "Human review guide",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        560,
        -80
      ],
      "parameters": {
        "color": 3,
        "width": 400,
        "height": 170,
        "content": "## 2. Human confidence gate\n\nScores below 0.70 stop here and alert `#triage-review`. A person decides what happens next; this branch does not send, label, or archive the message."
      },
      "typeVersion": 1
    },
    {
      "id": "119792ba-e975-4355-adc9-f24ed6acb146",
      "name": "Routing guide",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        620,
        680
      ],
      "parameters": {
        "color": 4,
        "width": 520,
        "height": 190,
        "content": "## 3. Route confident results\n\nUrgent messages alert `#ops-alerts`. Invoice items follow the Drive and ledger path. Routine mail receives the configured Gmail label. Replace every placeholder and run one sample through each branch."
      },
      "typeVersion": 1
    },
    {
      "id": "0dd01d0e-09fd-48f7-b291-0cddc8abf801",
      "name": "Audit guide",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1180,
        60
      ],
      "parameters": {
        "color": 6,
        "width": 400,
        "height": 170,
        "content": "## 4. Audit every outcome\n\nAll four branches converge here. Replace `YOUR_AUDIT_SHEET_ID`, confirm the `DecisionLog` column mapping, and retain only the metadata your policy allows."
      },
      "typeVersion": 1
    },
    {
      "name": "Email Trigger (IMAP)",
      "type": "n8n-nodes-base.emailReadImap",
      "position": [
        0,
        300
      ],
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "name": "AI Classify",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        220,
        300
      ],
      "parameters": {
        "modelId": "gpt-4o-mini",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are an email triage agent. Reply ONLY with JSON: {\"category\": \"urgent|invoice|routine\", \"confidence\": 0.0-1.0, \"reason\": \"one sentence\"}. urgent = outage, angry customer, legal, deadline today. invoice = bills, receipts, payment requests. routine = everything else."
            },
            {
              "role": "user",
              "content": "=Subject: {{ $json.subject }}\n\nBody:\n{{ $json.textPlain }}"
            }
          ]
        },
        "jsonOutput": true
      },
      "typeVersion": 1.8
    },
    {
      "name": "Confidence Gate",
      "type": "n8n-nodes-base.if",
      "position": [
        440,
        300
      ],
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{ $json.message.content.confidence }}",
              "value2": 0.7,
              "operation": "smaller"
            }
          ]
        }
      },
      "typeVersion": 2
    },
    {
      "name": "Route by Category",
      "type": "n8n-nodes-base.switch",
      "position": [
        660,
        380
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "urgent",
              "conditions": {
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.message.content.category }}",
                    "rightValue": "urgent"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "invoice",
              "conditions": {
                "conditions": [
                  {
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.message.content.category }}",
                    "rightValue": "invoice"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {
          "fallbackOutputRename": "routine"
        },
        "fallbackOutput": "extra"
      },
      "typeVersion": 3.2
    },
    {
      "name": "Alert Team Chat",
      "type": "n8n-nodes-base.slack",
      "position": [
        880,
        180
      ],
      "parameters": {
        "text": "=:rotating_light: Urgent email from {{ $('Email Trigger (IMAP)').item.json.from }} \u2014 {{ $('AI Classify').item.json.message.content.reason }}",
        "channel": "#ops-alerts"
      },
      "typeVersion": 2.2
    },
    {
      "name": "Save Attachment",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        880,
        380
      ],
      "parameters": {
        "name": "={{ $('Email Trigger (IMAP)').item.json.subject }}.pdf",
        "operation": "upload"
      },
      "typeVersion": 3
    },
    {
      "name": "Append to Ledger",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1100,
        380
      ],
      "parameters": {
        "columns": {
          "mappingMode": "autoMapInputData"
        },
        "operation": "append",
        "sheetName": "Invoices",
        "documentId": "YOUR_LEDGER_SHEET_ID"
      },
      "typeVersion": 4.5
    },
    {
      "name": "Label + Archive",
      "type": "n8n-nodes-base.gmail",
      "position": [
        880,
        560
      ],
      "parameters": {
        "labelIds": [
          "Label_routine"
        ],
        "operation": "addLabels"
      },
      "typeVersion": 2.1
    },
    {
      "name": "Human Review Queue",
      "type": "n8n-nodes-base.slack",
      "position": [
        660,
        120
      ],
      "parameters": {
        "text": "=:thinking_face: Low-confidence email needs a human: {{ $('Email Trigger (IMAP)').item.json.subject }} ({{ $json.message.content.confidence }})",
        "channel": "#triage-review"
      },
      "typeVersion": 2.2
    },
    {
      "name": "Audit Log",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1320,
        300
      ],
      "parameters": {
        "columns": {
          "mappingMode": "autoMapInputData"
        },
        "operation": "append",
        "sheetName": "DecisionLog",
        "documentId": "YOUR_AUDIT_SHEET_ID"
      },
      "typeVersion": 4.5
    }
  ],
  "settings": {
    "executionOrder": "v1"
  },
  "connections": {
    "AI Classify": {
      "main": [
        [
          {
            "node": "Confidence Gate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Alert Team Chat": {
      "main": [
        [
          {
            "node": "Audit Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Confidence Gate": {
      "main": [
        [
          {
            "node": "Human Review Queue",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Route by Category",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Label + Archive": {
      "main": [
        [
          {
            "node": "Audit Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Attachment": {
      "main": [
        [
          {
            "node": "Append to Ledger",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append to Ledger": {
      "main": [
        [
          {
            "node": "Audit Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Route by Category": {
      "main": [
        [
          {
            "node": "Alert Team Chat",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Save Attachment",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Label + Archive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Human Review Queue": {
      "main": [
        [
          {
            "node": "Audit Log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email Trigger (IMAP)": {
      "main": [
        [
          {
            "node": "AI Classify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}