{
  "name": "TrustLoop + Signatrust: Human Approval Flow",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "ai_high_risk_decision",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "trigger-1",
      "name": "AI High-Risk Decision",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.trustloop.live/api/intercept",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "x-api-key",
              "value": "={{$env.TRUSTLOOP_API_KEY}}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\"tool_name\": \"high_risk_action\", \"arguments\": {{JSON.stringify($json.body)}}, \"agent_name\": \"n8n-approval-workflow\"}",
        "options": {}
      },
      "id": "trustloop-1",
      "name": "TrustLoop Policy Check",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        200,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{$json.allowed}}",
              "value2": true
            }
          ]
        }
      },
      "id": "if-1",
      "name": "Is Allowed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        400,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "number": [
            {
              "value1": "={{$('AI High-Risk Decision').item.json.body.risk_score || 0}}",
              "operation": "largerEqual",
              "value2": 0.7
            }
          ]
        }
      },
      "id": "risk-check-1",
      "name": "Risk Score >= 0.7?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        600,
        -100
      ]
    },
    {
      "parameters": {
        "jsCode": "const input = $('AI High-Risk Decision').item.json.body;\nreturn {\n  action: 'pending_human_review',\n  decision: input.decision || 'unknown',\n  risk_score: input.risk_score || 0,\n  escalation_reason: 'Risk score exceeds threshold (0.7)',\n  reviewer: input.reviewer || 'compliance-team',\n  timestamp: new Date().toISOString()\n};"
      },
      "id": "escalate-1",
      "name": "Escalate to Human",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        800,
        -200
      ]
    },
    {
      "parameters": {
        "jsCode": "const input = $('AI High-Risk Decision').item.json.body;\nreturn {\n  action: 'auto_approved',\n  decision: input.decision || 'unknown',\n  risk_score: input.risk_score || 0,\n  approval_type: 'automatic',\n  timestamp: new Date().toISOString()\n};"
      },
      "id": "auto-approve-1",
      "name": "Auto-Approve (Low Risk)",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        800,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://signatrust.net/api/v1/n8n/receipts",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{$env.SIGNATRUST_API_KEY}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{JSON.stringify({\"decision\": ($json.action === 'pending_human_review' ? 'ESCALATED: ' : 'AUTO-APPROVED: ') + ($json.decision || 'unknown action') + ' \u2014 risk_score: ' + ($json.risk_score || 0) + ' \u2014 governed by TrustLoop policy', \"agent_id\": \"n8n-approval-workflow\", \"action\": $json.action, \"metadata\": {\"governed_by\": \"TrustLoop\", \"risk_score\": $json.risk_score, \"approval_type\": $json.approval_type || 'human_review'}})}}",
        "options": {}
      },
      "id": "signatrust-1",
      "name": "Signatrust Seal Receipt",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1000,
        -100
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://signatrust.net/api/v1/n8n/receipts/{{$json.receipt_id}}/verify",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-Key",
              "value": "={{$env.SIGNATRUST_API_KEY}}"
            }
          ]
        },
        "options": {}
      },
      "id": "verify-1",
      "name": "Verify Receipt",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1200,
        -100
      ]
    },
    {
      "parameters": {
        "jsCode": "const receipt = $('Signatrust Seal Receipt').item.json;\nconst verification = $json;\nreturn {\n  status: 'completed',\n  receipt_id: receipt.receipt_id,\n  verified: verification.valid,\n  share_url: receipt.share_url,\n  hash: receipt.hash,\n  signature: receipt.signature\n};"
      },
      "id": "respond-1",
      "name": "Return Audit Trail",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1400,
        -100
      ]
    },
    {
      "parameters": {
        "jsCode": "return {\n  status: 'blocked',\n  reason: $('TrustLoop Policy Check').item.json.message || 'Policy violation detected',\n  action: 'high_risk_action_denied'\n};"
      },
      "id": "block-1",
      "name": "Handle Blocked Action",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        600,
        100
      ]
    }
  ],
  "connections": {
    "AI High-Risk Decision": {
      "main": [
        [
          {
            "node": "TrustLoop Policy Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TrustLoop Policy Check": {
      "main": [
        [
          {
            "node": "Is Allowed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Allowed?": {
      "main": [
        [
          {
            "node": "Risk Score >= 0.7?",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Handle Blocked Action",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Risk Score >= 0.7?": {
      "main": [
        [
          {
            "node": "Escalate to Human",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Auto-Approve (Low Risk)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Escalate to Human": {
      "main": [
        [
          {
            "node": "Signatrust Seal Receipt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Auto-Approve (Low Risk)": {
      "main": [
        [
          {
            "node": "Signatrust Seal Receipt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Signatrust Seal Receipt": {
      "main": [
        [
          {
            "node": "Verify Receipt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Verify Receipt": {
      "main": [
        [
          {
            "node": "Return Audit Trail",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "2.0"
}