{
  "name": "Compliance-aware email triage agent",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "simple": true,
        "filters": {
          "labelIds": [
            "INBOX"
          ]
        }
      },
      "id": "2c7bc1b5-72b9-45da-a15b-880121b8743c",
      "name": "New email",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1.2,
      "position": [
        240,
        300
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "REPLACE_ME",
          "name": "Gmail"
        }
      }
    },
    {
      "parameters": {
        "inputText": "={{ $json.snippet }}",
        "categories": {
          "categories": [
            {
              "category": "routine",
              "description": "Standard request that can be answered from policy"
            },
            {
              "category": "compliance",
              "description": "Mentions legal, regulatory, complaints or personal data"
            },
            {
              "category": "urgent",
              "description": "Needs a person today"
            }
          ]
        },
        "options": {
          "multiClass": false,
          "fallback": "other"
        }
      },
      "id": "8f37fd3e-919a-4715-a536-a16c893c8443",
      "name": "Classify",
      "type": "@n8n/n8n-nodes-langchain.textClassifier",
      "typeVersion": 1,
      "position": [
        512,
        300
      ]
    },
    {
      "parameters": {
        "model": "llama3.1:8b",
        "options": {
          "temperature": 0
        }
      },
      "id": "b07a7eb0-7061-4806-a581-c32ff98a7c0f",
      "name": "Classifier model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        512,
        534
      ]
    },
    {
      "parameters": {
        "jsCode": "const cat = $json.category || 'other';\nconst confidence = $json.confidence ?? 0;\nreturn [{ json: { ...$json, draftable: cat === 'routine' && confidence >= 0.8 } }];"
      },
      "id": "38a0dbea-0a68-4919-a776-bd0c060a0de5",
      "name": "Safe to draft?",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        784,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "d1",
              "leftValue": "={{ $json.draftable }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "bbb638a2-ecc0-4dfa-ad2f-d544c9461d86",
      "name": "Draftable?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1056,
        300
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Draft a short, polite reply to this email using only company policy. Do not make commitments.\n\n{{ $(\"New email\").item.json.snippet }}"
      },
      "id": "63c1bdaa-db6b-459c-a854-4e1d5a0226ab",
      "name": "Draft reply",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        1328,
        198
      ]
    },
    {
      "parameters": {
        "model": "llama3.1:8b",
        "options": {
          "temperature": 0.3
        }
      },
      "id": "f559a42b-e099-461f-ad8a-d61342fd5a7b",
      "name": "Drafting model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        1328,
        431
      ]
    },
    {
      "parameters": {
        "resource": "draft",
        "operation": "create",
        "subject": "=Re: {{ $(\"New email\").item.json.subject }}",
        "message": "={{ $json.text }}",
        "options": {
          "threadId": "={{ $(\"New email\").item.json.threadId }}"
        }
      },
      "id": "aef40a21-e304-40d0-a7c9-97d68425d7d3",
      "name": "Save as Gmail draft",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1600,
        198
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "REPLACE_ME",
          "name": "Gmail"
        }
      }
    },
    {
      "parameters": {
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "email-review",
          "mode": "name"
        },
        "operation": "sendAndWait",
        "message": "Approve this draft reply?",
        "approvalOptions": {
          "values": {
            "approvalType": "double"
          }
        },
        "options": {
          "limitWaitTime": {
            "values": {
              "resumeAmount": 4,
              "resumeUnit": "hours"
            }
          }
        }
      },
      "id": "77e3a1b3-d8ed-496b-aad9-1fea56a0f995",
      "name": "Ask reviewer",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        1872,
        198
      ],
      "credentials": {
        "slackApi": {
          "id": "REPLACE_ME",
          "name": "Slack"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "p1",
              "leftValue": "={{ $json.data.approved }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "89096297-98a4-45ea-aa13-05418daf6055",
      "name": "Approved?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        2144,
        198
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "reply",
        "messageId": "={{ $(\"New email\").item.json.id }}",
        "message": "={{ $(\"Draft reply\").item.json.text }}",
        "options": {}
      },
      "id": "866d7cd9-f7d6-4379-a458-794ccf9d7876",
      "name": "Send reply",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        2416,
        96
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "REPLACE_ME",
          "name": "Gmail"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $(\"New email\").item.json.id }}",
        "labelIds": [
          "NEEDS_HUMAN"
        ]
      },
      "id": "cbb22a20-74c5-4c63-a494-f65da1bde11b",
      "name": "Label for a person",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1328,
        723
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "REPLACE_ME",
          "name": "Gmail"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $(\"New email\").item.json.id }}",
        "labelIds": [
          "NEEDS_HUMAN"
        ]
      },
      "id": "9267509a-c546-4602-a706-a4789e2e2d1c",
      "name": "Flag rejected thread",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        2416,
        329
      ],
      "credentials": {
        "gmailOAuth2": {
          "id": "REPLACE_ME",
          "name": "Gmail"
        }
      }
    }
  ],
  "connections": {
    "New email": {
      "main": [
        [
          {
            "node": "Classify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify": {
      "main": [
        [
          {
            "node": "Safe to draft?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Safe to draft?": {
      "main": [
        [
          {
            "node": "Draftable?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classifier model": {
      "ai_languageModel": [
        [
          {
            "node": "Classify",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Draftable?": {
      "main": [
        [
          {
            "node": "Draft reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Label for a person",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Drafting model": {
      "ai_languageModel": [
        [
          {
            "node": "Draft reply",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Draft reply": {
      "main": [
        [
          {
            "node": "Save as Gmail draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save as Gmail draft": {
      "main": [
        [
          {
            "node": "Ask reviewer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ask reviewer": {
      "main": [
        [
          {
            "node": "Approved?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Approved?": {
      "main": [
        [
          {
            "node": "Send reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Flag rejected thread",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false,
    "description": "The model drafts. A person decides what goes out."
  },
  "tags": []
}