{
  "name": "Vorlux AI | Agent Escalation Handler",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "agent-escalation",
        "options": {
          "responseMode": "lastNode"
        }
      },
      "id": "webhook-trigger",
      "name": "Escalation Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "const data = $input.first().json.body || $input.first().json;\nconst taskId = data.taskId || data.task_id;\nconst agentId = data.agentId || data.agent_id;\nconst reason = data.reason || 'Agent could not resolve task';\nconst severity = data.severity || 'medium';\n\nconst escalationLevels = {\n  low: { target: 'orchestrator', timeout: 3600, autoResolve: true },\n  medium: { target: 'chief-of-staff', timeout: 1800, autoResolve: false },\n  high: { target: 'main', timeout: 600, autoResolve: false },\n  critical: { target: 'main', timeout: 300, autoResolve: false, notifyOwner: true }\n};\n\nconst level = escalationLevels[severity] || escalationLevels.medium;\nreturn [{ json: { taskId, fromAgent: agentId, reason, severity, escalation: level, timestamp: new Date().toISOString() } }];"
      },
      "id": "classify-escalation",
      "name": "Classify Escalation",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "url": "http://host.docker.internal:3010/api/orchestrator/dispatch",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ agentId: $json.escalation.target, task: { id: $json.taskId, escalatedFrom: $json.fromAgent, reason: $json.reason, priority: 'high' } }) }}",
        "options": {
          "timeout": 15000
        }
      },
      "id": "dispatch-escalation",
      "name": "Dispatch to Escalation Target",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        720,
        200
      ]
    },
    {
      "parameters": {
        "url": "http://host.docker.internal:3010/api/incidents",
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ type: 'escalation', severity: $json.severity, agentId: $json.fromAgent, taskId: $json.taskId, reason: $json.reason }) }}",
        "options": {
          "timeout": 10000
        }
      },
      "id": "create-incident",
      "name": "Create Incident Record",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        720,
        400
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$env.DISCORD_OPS_WEBHOOK}}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ content: '\ud83d\udea8 **Escalation** [' + $json.severity.toUpperCase() + ']\\nFrom: ' + $json.fromAgent + '\\nTo: ' + $json.escalation.target + '\\nReason: ' + $json.reason + '\\nTask: ' + $json.taskId }) }}"
      },
      "id": "discord-notify",
      "name": "Discord Alert",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        980,
        300
      ]
    }
  ],
  "connections": {
    "Escalation Webhook": {
      "main": [
        [
          {
            "node": "Classify Escalation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify Escalation": {
      "main": [
        [
          {
            "node": "Dispatch to Escalation Target",
            "type": "main",
            "index": 0
          },
          {
            "node": "Create Incident Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dispatch to Escalation Target": {
      "main": [
        [
          {
            "node": "Discord Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Incident Record": {
      "main": [
        [
          {
            "node": "Discord Alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}