{
  "id": "aihApproval",
  "name": "AIHotple - Review AI draft before local save",
  "active": false,
  "nodes": [
    {
      "id": "start",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "position": [
        0,
        0
      ]
    },
    {
      "id": "propose-reply-draft",
      "name": "Propose reply draft",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "http://127.0.0.1:5683/propose",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ {request:\"Fictional customer asks where the product setup instructions are. Draft a reply saying the team can provide setup instructions. Do not send it.\",action_id:\"draft-\"+$execution.id} }}",
        "options": {
          "timeout": 180000
        }
      },
      "position": [
        220,
        0
      ]
    },
    {
      "id": "freeze-proposed-action",
      "name": "Freeze proposed action",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "const p=$input.first().json;if(p.action!=='save_local_draft'||p.to!=='demo-customer@example.com'||typeof p.draft!=='string'||!p.draft.trim())throw Error('Invalid proposed action');return [{json:{...p,review_url:$execution.resumeFormUrl}}];"
      },
      "position": [
        440,
        0
      ]
    },
    {
      "id": "record-pending-review",
      "name": "Record pending review",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "http://127.0.0.1:5683/record",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ {kind:\"pending_review\",...$json} }}",
        "options": {
          "timeout": 180000
        }
      },
      "position": [
        660,
        0
      ]
    },
    {
      "id": "review-before-action",
      "name": "Review before action",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "parameters": {
        "resume": "form",
        "incomingAuthentication": "none",
        "formTitle": "Review fictional AI draft",
        "formDescription": "={{\"<p><strong>Action:</strong> Save a local demo draft only. No email will be sent.</p><p><strong>Recipient:</strong> demo-customer@example.com</p><p><strong>Draft:</strong> \"+$json.draft.replaceAll(\"&\",\"&amp;\").replaceAll(\"<\",\"&lt;\").replaceAll(\">\",\"&gt;\")+\"</p>\"}}",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Decision",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Deny"
                  },
                  {
                    "option": "Approve"
                  }
                ]
              },
              "requiredField": true
            }
          ]
        },
        "responseMode": "onReceived",
        "options": {
          "formResponse": "Thanks. The workflow will check your decision."
        }
      },
      "position": [
        880,
        0
      ],
      "webhookId": "aih-review-local"
    },
    {
      "id": "require-explicit-approval",
      "name": "Require explicit approval",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "const response=$input.first().json;const p=$('Freeze proposed action').first().json;const approved=response.Decision==='Approve';return [{json:{action_id:p.action_id,to:p.to,draft:p.draft,action:p.action,decision:response.Decision||'missing',approved,executed:false,status:approved?'approved_pending_local_save':'denied_no_action',kind:'approval_result'}}];"
      },
      "position": [
        1100,
        0
      ]
    },
    {
      "id": "record-decision",
      "name": "Record decision",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "http://127.0.0.1:5683/record",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json }}",
        "options": {
          "timeout": 180000
        }
      },
      "position": [
        1320,
        0
      ]
    },
    {
      "id": "approved-only",
      "name": "Approved only",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "leftValue": "={{$json.approved}}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "position": [
        1540,
        0
      ]
    },
    {
      "id": "save-local-draft",
      "name": "Save local draft",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "http://127.0.0.1:5683/save-draft",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json }}",
        "options": {
          "timeout": 180000
        }
      },
      "position": [
        1760,
        0
      ]
    },
    {
      "id": "denied---no-action",
      "name": "Denied - no action",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "return $input.all().map(x=>({json:{...x.json,executed:false,status:'denied_no_action'}}));"
      },
      "position": [
        1760,
        240
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Propose reply draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Propose reply draft": {
      "main": [
        [
          {
            "node": "Freeze proposed action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Freeze proposed action": {
      "main": [
        [
          {
            "node": "Record pending review",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Record pending review": {
      "main": [
        [
          {
            "node": "Review before action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Review before action": {
      "main": [
        [
          {
            "node": "Require explicit approval",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Require explicit approval": {
      "main": [
        [
          {
            "node": "Record decision",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Record decision": {
      "main": [
        [
          {
            "node": "Approved only",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Approved only": {
      "main": [
        [
          {
            "node": "Save local draft",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Denied - no action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "saveDataSuccessExecution": "all",
    "saveDataErrorExecution": "all"
  }
}
