{
  "name": "Agent Task Handoff",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "agent-handoff",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook - Handoff Request",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "operation": "executeQuery",
        "query": "SELECT id, name, status, capabilities FROM agents WHERE id = '{{ $json.to_agent_id }}' LIMIT 1",
        "options": {}
      },
      "id": "check-agent-availability",
      "name": "Check Agent Availability",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        450,
        300
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "agent-active",
              "leftValue": "={{ $json.status }}",
              "rightValue": "active",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "agent-available",
      "name": "Is Agent Available?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "operation": "insert",
        "schema": {
          "value": "public"
        },
        "table": {
          "value": "context_handovers"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "from_agent_id": "={{ $('Webhook - Handoff Request').item.json.from_agent_id }}",
            "to_agent_id": "={{ $('Webhook - Handoff Request').item.json.to_agent_id }}",
            "context": "={{ $('Webhook - Handoff Request').item.json.context }}",
            "status": "pending",
            "created_at": "={{ $now.toISO() }}"
          }
        },
        "options": {}
      },
      "id": "create-handover",
      "name": "Create Handover Record",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        850,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "topic": "prime-spark/agent/handoff",
        "message": "={{ JSON.stringify({\n  handover_id: $json.id,\n  from_agent: $('Webhook - Handoff Request').item.json.from_agent_id,\n  to_agent: $('Webhook - Handoff Request').item.json.to_agent_id,\n  context: $('Webhook - Handoff Request').item.json.context,\n  timestamp: $now.toISO()\n}) }}",
        "options": {}
      },
      "id": "notify-mqtt",
      "name": "Notify via MQTT",
      "type": "n8n-nodes-base.mqtt",
      "typeVersion": 1,
      "position": [
        1050,
        200
      ],
      "credentials": {
        "mqtt": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "insert",
        "schema": {
          "value": "public"
        },
        "table": {
          "value": "infrastructure_events"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "event_type": "agent.handoff.created",
            "category": "agent",
            "severity": "info",
            "node": "orchestrator",
            "details": "={{ JSON.stringify({\n  handover_id: $('Create Handover Record').item.json.id,\n  from_agent: $('Webhook - Handoff Request').item.json.from_agent_id,\n  to_agent: $('Webhook - Handoff Request').item.json.to_agent_id\n}) }}"
          }
        }
      },
      "id": "log-event",
      "name": "Log Infrastructure Event",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.4,
      "position": [
        1250,
        200
      ],
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({\n  success: true,\n  handover_id: $('Create Handover Record').item.json.id,\n  status: 'pending',\n  message: 'Handoff request created successfully'\n}) }}",
        "options": {}
      },
      "id": "success-response",
      "name": "Success Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1450,
        200
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseCode": 400,
        "responseBody": "={{ JSON.stringify({\n  success: false,\n  error: 'Agent not available',\n  agent_status: $json.status\n}) }}",
        "options": {}
      },
      "id": "error-response",
      "name": "Agent Unavailable Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        850,
        400
      ]
    }
  ],
  "connections": {
    "Webhook - Handoff Request": {
      "main": [
        [
          {
            "node": "Check Agent Availability",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Agent Availability": {
      "main": [
        [
          {
            "node": "Is Agent Available?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Agent Available?": {
      "main": [
        [
          {
            "node": "Create Handover Record",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Agent Unavailable Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Handover Record": {
      "main": [
        [
          {
            "node": "Notify via MQTT",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Notify via MQTT": {
      "main": [
        [
          {
            "node": "Log Infrastructure Event",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Infrastructure Event": {
      "main": [
        [
          {
            "node": "Success Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "agent",
      "id": "agent-tag"
    },
    {
      "name": "orchestration",
      "id": "orchestration-tag"
    },
    {
      "name": "prime-spark",
      "id": "prime-spark-tag"
    }
  ],
  "triggerCount": 1,
  "updatedAt": "2025-11-25T20:00:00.000Z",
  "versionId": "1"
}