{
  "id": "g3q68zSOQvTcydLs",
  "name": "Calculate the Centroid of a Set of Vectors",
  "tags": [],
  "nodes": [
    {
      "id": "32a8aa56-aa7e-4c9e-a39e-f65234224bcf",
      "name": "Receive Vectors",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -440,
        20
      ],
      "parameters": {
        "path": "centroid",
        "options": {},
        "responseMode": "responseNode"
      },
      "notesInFlow": true,
      "typeVersion": 2
    },
    {
      "id": "a020a49a-cc9f-49af-aa95-829d9d16da04",
      "name": "Extract & Parse Vectors",
      "type": "n8n-nodes-base.set",
      "position": [
        360,
        20
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "3e1d9e72-7668-427d-958c-42bff7270a37",
              "name": "vectors",
              "type": "array",
              "value": "={{ $json.query.vectors }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "2f328de6-4ef1-4aac-8838-d616637f4b88",
      "name": "Validate & Compute Centroid",
      "type": "n8n-nodes-base.code",
      "position": [
        960,
        20
      ],
      "parameters": {
        "jsCode": "const input = items[0].json;\nconst vectors = input.vectors;\n\nif (!Array.isArray(vectors) || vectors.length === 0) {\n  return [{ json: { error: \"Invalid input: Expected an array of vectors.\" } }];\n}\n\nconst dimension = vectors[0].length;\nif (!vectors.every(v => v.length === dimension)) {\n  return [{ json: { error: \"Vectors have inconsistent dimensions.\" } }];\n}\n\nconst centroid = new Array(dimension).fill(0);\nvectors.forEach(vector => {\n  vector.forEach((val, index) => {\n    centroid[index] += val;\n  });\n});\n\nfor (let i = 0; i < dimension; i++) {\n  centroid[i] /= vectors.length;\n}\n\nreturn [{ json: { centroid } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "821bc173-3578-4cf2-9fd7-8ea9cba8dc3f",
      "name": "Return Centroid Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1640,
        20
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.1
    },
    {
      "id": "73964e7b-1217-422f-8078-09604fa2a3d7",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        20,
        -200
      ],
      "parameters": {
        "color": 3,
        "width": 620,
        "height": 420,
        "content": "\ud83d\udccc **Description:**  \nThis node extracts the `vectors` array from the **GET request** and converts it into a properly formatted array for processing.  \n- **Ensures `vectors` is a valid array.**  \n- **If the parameter is missing, it may generate an error.**  \n\n\ud83d\udd39 **Expected Output Example:**\n```json\n{\n  \"vectors\": [[2,3,4],[4,5,6],[6,7,8]]\n}\n```\n"
      },
      "typeVersion": 1
    },
    {
      "id": "e4793b20-bfa6-4b08-b46c-f92d1c9c2622",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        640,
        -280
      ],
      "parameters": {
        "color": 4,
        "width": 700,
        "height": 500,
        "content": "\ud83d\udccc **Description:**  \nThis node performs **vector validation** and **centroid computation**.  \n- **Validation:** Ensures all vectors have the same number of dimensions.  \n- **Computation:** Averages each dimension to determine the centroid.  \n- **If validation fails:** Returns an error message indicating inconsistent dimensions.  \n\n\ud83d\udd39 **Successful Output Example:**\n```json\n{\n  \"centroid\": [4,5,6]\n}\n```\n\ud83d\udd39 **Error Output Example:**\n```json\n{\n  \"error\": \"Vectors have inconsistent dimensions.\"\n}\n```\n"
      },
      "typeVersion": 1
    },
    {
      "id": "e0ac1c4d-0435-44d1-ba87-0cfc9dea207b",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1340,
        -200
      ],
      "parameters": {
        "color": 2,
        "width": 680,
        "height": 420,
        "content": "\ud83d\udccc **Description:**  \nThis node sends the **final response** back to the client that made the request.  \n- **If the computation is successful**, it returns the centroid.  \n- **If an error occurs**, it returns a descriptive error message.  \n\n\ud83d\udd39 **Example Response:**\n```json\n{\n  \"centroid\": [4, 5, 6]\n}\n```\n"
      },
      "typeVersion": 1
    },
    {
      "id": "2b4fbae7-c2e5-4666-ba9f-72a5313fc16f",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -820,
        -200
      ],
      "parameters": {
        "color": 4,
        "width": 840,
        "height": 420,
        "content": "\ud83d\udccc **Description:**  \nThis node acts as the **entry point** for the workflow, receiving a **GET request** containing an array of vectors in the `vectors` parameter.  \n- **Expected Input:** `vectors` parameter in JSON format.  \n- **Example Request:**  \n  ```plaintext\n  https://actions.singular-innovation.com/webhook-test/centroid?vectors=[[2,3,4],[4,5,6],[6,7,8]]\n  ```\n- **Output:** Passes the received data to the next node for processing.\n"
      },
      "typeVersion": 1
    }
  ],
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "f9c7fa52-264b-4967-ae7a-62247cce7a50",
  "connections": {
    "Receive Vectors": {
      "main": [
        [
          {
            "node": "Extract & Parse Vectors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract & Parse Vectors": {
      "main": [
        [
          {
            "node": "Validate & Compute Centroid",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate & Compute Centroid": {
      "main": [
        [
          {
            "node": "Return Centroid Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}