{
  "name": "Legacy Orchestrator Proxy",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "legacy-orchestrator-proxy",
        "responseMode": "responseNode",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              },
              {
                "name": "Access-Control-Allow-Methods",
                "value": "POST, OPTIONS"
              },
              {
                "name": "Access-Control-Allow-Headers",
                "value": "Content-Type"
              }
            ]
          }
        }
      },
      "id": "webhook-trigger",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Prepare payload for Apify actor\nconst input = $input.first().json.body;\n\nconst apifyPayload = {\n  systemType: input.systemType || 'web_portal',\n  connectionUrl: input.connectionUrl,\n  connectionPort: input.connectionPort || null,\n  task: input.task,\n  planningWebhook: 'https://n8n.jasonpellerinfreelance.com/webhook/legacy-planner',\n  maxSteps: input.maxSteps || 10,\n  timeout: input.timeout || 120,\n  stealthLevel: input.stealthLevel || 2,\n  enableVisionFallback: input.enableVisionFallback !== false,\n  debugMode: true,\n  captureScreenshots: false,\n  proxyConfiguration: { useApifyProxy: false }\n};\n\nreturn { json: { payload: apifyPayload } };"
      },
      "id": "prepare-payload",
      "name": "Prepare Payload",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.apify.com/v2/acts/ai_solutionist~legacy-orchestrator/runs",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json.payload }}",
        "options": {
          "timeout": 30000
        }
      },
      "id": "start-actor",
      "name": "Start Apify Actor",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        680,
        300
      ],
      "credentials": {
        "httpQueryAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Extract run ID from Apify response\nconst response = $input.first().json;\n\nif (!response.data || !response.data.id) {\n  throw new Error('Failed to start Apify actor: ' + JSON.stringify(response));\n}\n\nreturn {\n  json: {\n    runId: response.data.id,\n    status: response.data.status\n  }\n};"
      },
      "id": "extract-run-id",
      "name": "Extract Run ID",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.apify.com/v2/actor-runs/{{ $json.runId }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "options": {
          "timeout": 10000
        }
      },
      "id": "check-status",
      "name": "Check Run Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1120,
        300
      ],
      "credentials": {
        "httpQueryAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "running-check",
              "leftValue": "={{ $json.data.status }}",
              "rightValue": "RUNNING",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-running",
      "name": "Still Running?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1340,
        300
      ]
    },
    {
      "parameters": {
        "amount": 3,
        "unit": "seconds"
      },
      "id": "wait-3s",
      "name": "Wait 3s",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1560,
        200
      ]
    },
    {
      "parameters": {
        "jsCode": "// Pass through the runId for next status check\nconst runId = $('Extract Run ID').first().json.runId;\nreturn { json: { runId } };"
      },
      "id": "loop-back",
      "name": "Loop Back",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1780,
        200
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://api.apify.com/v2/actor-runs/{{ $('Extract Run ID').first().json.runId }}/dataset/items",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "options": {
          "timeout": 10000
        }
      },
      "id": "get-dataset",
      "name": "Get Dataset Items",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1560,
        400
      ],
      "credentials": {
        "httpQueryAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Format the final response\nconst statusData = $('Still Running?').first().json;\nconst datasetItems = $input.first().json;\n\n// Handle array response from Apify\nconst items = Array.isArray(datasetItems) ? datasetItems : [datasetItems];\nconst result = items[0] || {};\n\nreturn {\n  json: {\n    status: statusData.data?.status || 'COMPLETED',\n    runId: $('Extract Run ID').first().json.runId,\n    extractedData: result.extractedData || result,\n    stepsExecuted: result.stepsExecuted || 0,\n    totalTimeMs: result.totalTimeMs || 0,\n    systemType: result.systemType,\n    success: true\n  }\n};"
      },
      "id": "format-response",
      "name": "Format Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1780,
        400
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              }
            ]
          }
        }
      },
      "id": "respond-success",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        2000,
        400
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Prepare Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Payload": {
      "main": [
        [
          {
            "node": "Start Apify Actor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start Apify Actor": {
      "main": [
        [
          {
            "node": "Extract Run ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Run ID": {
      "main": [
        [
          {
            "node": "Check Run Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Run Status": {
      "main": [
        [
          {
            "node": "Still Running?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Still Running?": {
      "main": [
        [
          {
            "node": "Wait 3s",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get Dataset Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 3s": {
      "main": [
        [
          {
            "node": "Loop Back",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Back": {
      "main": [
        [
          {
            "node": "Check Run Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Dataset Items": {
      "main": [
        [
          {
            "node": "Format Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Response": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "versionId": "1",
  "triggerCount": 0
}