{
  "name": "M9 Supplier email with review",
  "nodes": [
    {
      "parameters": {
        "formTitle": "AI Caravan Ltd. - Supplier email with review",
        "formDescription": "Fictional company, for training only. A writer drafts, a critic checks, at most two revisions; then a person.",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Supplier",
              "requiredField": true,
              "placeholder": "Northwind Supplies"
            },
            {
              "fieldLabel": "Supplier email",
              "requiredField": true,
              "fieldType": "email",
              "placeholder": "lina.haddad@northwind.example"
            },
            {
              "fieldLabel": "Your email",
              "requiredField": true,
              "fieldType": "email",
              "placeholder": "where the escalation goes"
            },
            {
              "fieldLabel": "Purpose of email",
              "requiredField": true,
              "fieldType": "textarea",
              "placeholder": "e.g. ask for the updated 2027 price list"
            },
            {
              "fieldLabel": "Key facts",
              "requiredField": true,
              "fieldType": "textarea",
              "placeholder": "only what the email may say"
            },
            {
              "fieldLabel": "Deadline",
              "requiredField": false,
              "placeholder": "e.g. Friday 31 October"
            }
          ]
        },
        "responseMode": "onReceived",
        "options": {
          "respondWithOptions": {
            "values": {
              "formSubmittedText": "Received. The writer and the critic are at work; check the Review log."
            }
          }
        }
      },
      "id": "e0fe30de-f067-458a-9c4c-fb4ee9ec60e8",
      "name": "Supplier email with review",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.2,
      "position": [
        0,
        0
      ],
      "webhookId": "9a8fdbc8-7e05-412f-b7fc-4380312354a7"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e4028590-7199-43f6-b752-2ea3727757ad",
              "name": "supplier",
              "value": "={{ $json['Supplier'] }}",
              "type": "string"
            },
            {
              "id": "e9d58e85-cb47-4591-93fa-40aa5fcbede5",
              "name": "supplier_email",
              "value": "={{ $json['Supplier email'] }}",
              "type": "string"
            },
            {
              "id": "07a94c0f-ac0f-43c0-8884-f0c82f8e9635",
              "name": "your_email",
              "value": "={{ $json['Your email'] }}",
              "type": "string"
            },
            {
              "id": "938de885-6415-4ecd-9fb6-4f55817831bc",
              "name": "purpose",
              "value": "={{ $json['Purpose of email'] }}",
              "type": "string"
            },
            {
              "id": "a5fe979c-7dca-4e21-8116-dd4298b8648b",
              "name": "facts",
              "value": "={{ $json['Key facts'] }}",
              "type": "string"
            },
            {
              "id": "e95ff25a-a000-4387-8af6-48a215d7fa3e",
              "name": "deadline",
              "value": "={{ $json['Deadline'] || '' }}",
              "type": "string"
            },
            {
              "id": "d4ed525e-84eb-4a5a-8506-7651ee9031bb",
              "name": "revision",
              "value": 0,
              "type": "number"
            },
            {
              "id": "2bdd2bc4-7e13-43a1-b066-695911cdb4d9",
              "name": "feedback",
              "value": "",
              "type": "string"
            },
            {
              "id": "e8752e78-3b5c-4995-9399-3d6dc088ba4e",
              "name": "draft",
              "value": "",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "id": "ad6c9eb0-485b-4ef0-82fa-7006e0b517c4",
      "name": "Start",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        240,
        0
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Write a professional email to the supplier below on behalf of the Finance team of AI Caravan Ltd.\nUse only the facts given. Do not promise payment dates or amounts not listed. Under 150 words. Sign off as \"Finance Operations\".\nIf reviewer feedback is present, revise the previous draft to address every numbered point.\nReply with the email body only: no subject line, no commentary.\n\nSupplier: {{ $json.supplier }}\nPurpose: {{ $json.purpose }}\nFacts: {{ $json.facts }}\nDeadline: {{ $json.deadline || 'none given' }}\nReviewer feedback: {{ $json.feedback || 'none' }}\nPrevious draft: {{ $json.draft || 'none' }}",
        "batching": {}
      },
      "id": "797b34a8-89a6-413b-b0ed-72c276a0c111",
      "name": "Writer",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.7,
      "position": [
        480,
        0
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000
    },
    {
      "parameters": {
        "modelName": "models/gemini-2.5-flash",
        "options": {
          "temperature": 0
        }
      },
      "id": "6b3bbabf-5c1f-474f-8828-df364e0c3498",
      "name": "Gemini (writer)",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        440,
        220
      ],
      "credentials": {
        "googlePalmApi": {
          "id": null,
          "name": "",
          "__aiGatewayManaged": true
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "4b3f340c-a447-4697-a00a-2098ab9993b0",
              "name": "supplier",
              "value": "={{ $('Start').item.json.supplier }}",
              "type": "string"
            },
            {
              "id": "5928457c-3e65-4fee-868f-662b04a77e4f",
              "name": "supplier_email",
              "value": "={{ $('Start').item.json.supplier_email }}",
              "type": "string"
            },
            {
              "id": "a58dec29-a4d6-49d8-8286-cd8bab0b4c9b",
              "name": "your_email",
              "value": "={{ $('Start').item.json.your_email }}",
              "type": "string"
            },
            {
              "id": "f02c3bcd-3623-4a60-86c8-a46b70e1d0c5",
              "name": "purpose",
              "value": "={{ $('Start').item.json.purpose }}",
              "type": "string"
            },
            {
              "id": "22e01d47-ae1a-4238-8c0b-3e48e9750114",
              "name": "facts",
              "value": "={{ $('Start').item.json.facts }}",
              "type": "string"
            },
            {
              "id": "f07fc985-cb95-46a3-85ab-d45c2f69ff8a",
              "name": "deadline",
              "value": "={{ $('Start').item.json.deadline }}",
              "type": "string"
            },
            {
              "id": "f7beea04-68c6-4273-986d-4c9f10d6b3e2",
              "name": "revision",
              "value": "={{ $('Next revision').isExecuted ? $('Next revision').item.json.revision : 0 }}",
              "type": "number"
            },
            {
              "id": "3d12d1b7-fb90-4821-92e7-735e2f3a1dce",
              "name": "feedback",
              "value": "={{ $('Next revision').isExecuted ? $('Next revision').item.json.feedback : '' }}",
              "type": "string"
            },
            {
              "id": "7e5357a1-6f1f-4cfb-a674-71c1cdae1827",
              "name": "draft",
              "value": "={{ $json.text.trim() }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "id": "3b9b9f0f-9ecc-4789-9719-9769e3df9ddb",
      "name": "Keep draft",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        760,
        0
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=You review outgoing supplier emails for the Finance team. Check the draft against this list:\n1. Uses only the facts provided; nothing invented.\n2. No payment date or amount promised unless in the facts.\n3. Polite, professional, under 150 words.\n4. Mentions the deadline if one was given.\n5. No phone numbers, home addresses or other personal data; naming the people involved is fine.\n6. Does what the purpose asks; an email that avoids the task is not finished.\nReply with exactly APPROVED if all six pass. Otherwise reply with REVISE: followed by numbered points, each naming the failing item and the fix.\n\nPurpose of the email (the task the writer was given): {{ $json.purpose }}\nFacts: {{ $json.facts }}\nDeadline: {{ $json.deadline || 'none given' }}\nDraft: {{ $json.draft }}",
        "batching": {}
      },
      "id": "0dd34736-d7ea-4877-b677-1130f81ed402",
      "name": "Critic",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.7,
      "position": [
        1000,
        0
      ],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 3000
    },
    {
      "parameters": {
        "modelName": "models/gemini-2.5-pro",
        "options": {
          "temperature": 0
        }
      },
      "id": "3fb6e769-3df4-4522-9478-10580bb5ee11",
      "name": "Gemini (critic)",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "typeVersion": 1,
      "position": [
        960,
        220
      ],
      "credentials": {
        "googlePalmApi": {
          "id": null,
          "name": "",
          "__aiGatewayManaged": true
        }
      }
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "c6a8c1a3-1c45-4f80-aff6-f954a0d7b96c",
              "name": "verdict",
              "value": "={{ $json.text.trim() }}",
              "type": "string"
            },
            {
              "id": "487478e2-8d6f-4e11-acef-d8f2f35fe5ac",
              "name": "timestamp",
              "value": "={{ $now.toFormat('yyyy-MM-dd HH:mm:ss') }}",
              "type": "string"
            },
            {
              "id": "d657dd7b-9563-4463-813e-12fd1225a715",
              "name": "supplier",
              "value": "={{ $('Keep draft').item.json.supplier }}",
              "type": "string"
            },
            {
              "id": "26a583f0-4e20-4f05-995c-3476ae2b7ae9",
              "name": "supplier_email",
              "value": "={{ $('Keep draft').item.json.supplier_email }}",
              "type": "string"
            },
            {
              "id": "3ed37b16-71f7-4266-b8e9-0d3e155bb815",
              "name": "your_email",
              "value": "={{ $('Keep draft').item.json.your_email }}",
              "type": "string"
            },
            {
              "id": "e54e402c-c8ff-4872-89b5-631d04f66c52",
              "name": "purpose",
              "value": "={{ $('Keep draft').item.json.purpose }}",
              "type": "string"
            },
            {
              "id": "522909a3-afe8-4948-bd7b-52d8b62d896b",
              "name": "facts",
              "value": "={{ $('Keep draft').item.json.facts }}",
              "type": "string"
            },
            {
              "id": "bf16609f-c4e6-43d0-a148-8cd8377bda14",
              "name": "deadline",
              "value": "={{ $('Keep draft').item.json.deadline }}",
              "type": "string"
            },
            {
              "id": "403e896d-12ed-480e-a668-cc620691538f",
              "name": "revision",
              "value": "={{ $('Keep draft').item.json.revision }}",
              "type": "number"
            },
            {
              "id": "e9d66056-cfba-48b1-ae16-da9c6d6aaf90",
              "name": "draft",
              "value": "={{ $('Keep draft').item.json.draft }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "id": "5f266380-6225-49a1-ac6b-98dc018d7b53",
      "name": "Verdict",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1280,
        0
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "PICK-THE-REVIEW-LOG-SHEET"
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Review log"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "value": {
            "timestamp": "={{ $json.timestamp }}",
            "supplier": "={{ $json.supplier }}",
            "revision": "={{ $json.revision }}",
            "verdict": "={{ $json.verdict }}",
            "draft": "={{ $json.draft }}"
          },
          "schema": [
            {
              "id": "timestamp",
              "displayName": "timestamp",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "supplier",
              "displayName": "supplier",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "revision",
              "displayName": "revision",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "verdict",
              "displayName": "verdict",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "draft",
              "displayName": "draft",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            }
          ]
        },
        "options": {
          "cellFormat": "RAW"
        }
      },
      "id": "9a5bfbbc-b4a7-4933-a77b-1bd90a6d8bd6",
      "name": "Review log",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1520,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "e7045b98-6a96-4daa-9cd3-d5576ea4e217",
              "leftValue": "={{ $('Verdict').item.json.verdict }}",
              "rightValue": "APPROVED",
              "operator": {
                "type": "string",
                "operation": "startsWith"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "id": "c28d1dc9-57fe-4da5-9f4b-c3a8f8359b87",
      "name": "Approved?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1760,
        0
      ]
    },
    {
      "parameters": {
        "resource": "draft",
        "subject": "=AI Caravan Ltd. Finance: {{ $('Verdict').item.json.purpose.slice(0, 60) }}",
        "emailType": "text",
        "message": "={{ $('Verdict').item.json.draft }}",
        "options": {
          "sendTo": "={{ $('Verdict').item.json.supplier_email }}"
        }
      },
      "id": "ce5a28bf-da01-41ad-b948-a06f5d29d942",
      "name": "Gmail (create draft)",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        2020,
        -120
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "ce65ab05-ec61-4edd-8f5b-5334ab1eadea",
              "name": "supplier",
              "value": "={{ $('Verdict').item.json.supplier }}",
              "type": "string"
            },
            {
              "id": "0cf6aa56-cf7c-4667-b5af-f7d856d2f015",
              "name": "supplier_email",
              "value": "={{ $('Verdict').item.json.supplier_email }}",
              "type": "string"
            },
            {
              "id": "249ebc48-2463-47e2-89c6-b0cae07bdc0d",
              "name": "your_email",
              "value": "={{ $('Verdict').item.json.your_email }}",
              "type": "string"
            },
            {
              "id": "6bc24832-87c9-46f6-b6b4-62d10cb45b32",
              "name": "purpose",
              "value": "={{ $('Verdict').item.json.purpose }}",
              "type": "string"
            },
            {
              "id": "57a674c7-e179-4ad1-b988-39bd4858eaae",
              "name": "facts",
              "value": "={{ $('Verdict').item.json.facts }}",
              "type": "string"
            },
            {
              "id": "b6def7fe-633b-4089-8ecf-32a904190465",
              "name": "deadline",
              "value": "={{ $('Verdict').item.json.deadline }}",
              "type": "string"
            },
            {
              "id": "9c144365-efdd-4a81-bc85-1c83b35b42dc",
              "name": "draft",
              "value": "={{ $('Verdict').item.json.draft }}",
              "type": "string"
            },
            {
              "id": "86e43d4b-79c7-4d4b-9492-7fa7536fbcb9",
              "name": "revision",
              "value": "={{ $('Verdict').item.json.revision + 1 }}",
              "type": "number"
            },
            {
              "id": "6a588cf8-b803-4b4d-bb63-1f03f6e1f95d",
              "name": "feedback",
              "value": "={{ $('Verdict').item.json.verdict }}",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "id": "b1655739-84d0-4ecf-a5f5-ddd672b5b5d1",
      "name": "Next revision",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2020,
        140
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "d2862798-c8a7-400d-9c5b-c85b8a54113e",
              "leftValue": "={{ $json.revision }}",
              "rightValue": 2,
              "operator": {
                "type": "number",
                "operation": "lte"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "id": "655df371-25e7-41c2-ad3e-4ae88bb84ba5",
      "name": "Under the cap?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        2260,
        140
      ]
    },
    {
      "parameters": {
        "sendTo": "={{ $json.your_email }}",
        "subject": "=Needs a human: supplier email to {{ $json.supplier }} ({{ $json.revision - 1 }} revisions)",
        "emailType": "text",
        "message": "=The writer and the critic did not agree after {{ $json.revision - 1 }} revisions. Please finish this one yourself.\n\nPurpose: {{ $json.purpose }}\nFacts: {{ $json.facts }}\nDeadline: {{ $json.deadline || \"none\" }}\n\nLast draft:\n{{ $json.draft }}\n\nCritic's last verdict:\n{{ $json.feedback }}\n\n(Training demo, AI Caravan Ltd. is fictional.)",
        "options": {
          "appendAttribution": false
        }
      },
      "id": "9c33378b-e26e-49a4-a79f-920065623aaf",
      "name": "Gmail (escalate to a person)",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        2520,
        260
      ]
    },
    {
      "parameters": {
        "content": "## Writer, critic, reviser\n**Writer** drafts from the facts (Gemini). **Critic** checks six points on a stronger model (Gemini 2.5 Pro) and answers APPROVED or REVISE: 1. 2. 3. Every pass is appended to the **Review log**. Approved: a Gmail **draft** for a person to send. Not approved: **Next revision** adds 1 to the counter and hands the points back; **Under the cap?** loops to the Writer while revision <= 2, otherwise a person gets the last draft by e-mail.",
        "width": 700,
        "height": 240,
        "color": 5
      },
      "id": "43c01b14-d224-403f-9cec-d05d3e5be836",
      "name": "Note 61a7",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -300
      ]
    },
    {
      "parameters": {
        "content": "## Hand-offs\nA Basic LLM Chain outputs only its text, and the Google Sheets node outputs the row it wrote. **Keep draft** and **Verdict** put the task, the facts and the counter back on the item; the nodes after the log read them from Verdict (task, facts, constraints: what every hand-off must carry). Open the Review log after the three test cases: six rows, one escalation.",
        "width": 620,
        "height": 200,
        "color": 4
      },
      "id": "40bf99a3-68d9-4399-bc3b-7d4aaa68ea6c",
      "name": "Note 637a",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        720,
        -300
      ]
    }
  ],
  "connections": {
    "Supplier email with review": {
      "main": [
        [
          {
            "node": "Start",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Writer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Writer": {
      "main": [
        [
          {
            "node": "Keep draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini (writer)": {
      "ai_languageModel": [
        [
          {
            "node": "Writer",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Keep draft": {
      "main": [
        [
          {
            "node": "Critic",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Critic": {
      "main": [
        [
          {
            "node": "Verdict",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini (critic)": {
      "ai_languageModel": [
        [
          {
            "node": "Critic",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Verdict": {
      "main": [
        [
          {
            "node": "Review log",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Review log": {
      "main": [
        [
          {
            "node": "Approved?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Approved?": {
      "main": [
        [
          {
            "node": "Gmail (create draft)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Next revision",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Next revision": {
      "main": [
        [
          {
            "node": "Under the cap?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Under the cap?": {
      "main": [
        [
          {
            "node": "Writer",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Gmail (escalate to a person)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1",
    "timezone": "Asia/Amman"
  },
  "meta": {}
}