{
  "name": "Legacy Orchestrator - Quick Start Planner",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "legacy-planner",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-receive",
      "name": "\ud83d\udce5 Receive Planning Request",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Prepare context for LLM\nconst input = $input.first().json;\nconst body = input.body || input;\n\nconst elements = body.currentState?.detectedElements || [];\nconst interactable = elements.filter(e => e.interactable);\n\nconst context = `\nTASK: ${body.task}\n\nSCREEN STATE:\n- System: ${body.currentState?.systemType || 'unknown'}\n- Step: ${body.currentState?.currentStep || 0} of ${body.currentState?.totalSteps || 50}\n- Error: ${body.currentState?.errorState || 'None'}\n\nVISIBLE ELEMENTS (${elements.length} total, ${interactable.length} interactive):\n${elements.slice(0, 20).map((e, i) => `${i+1}. [${e.type}] \"${e.label}\" ${e.interactable ? '(clickable)' : ''}`).join('\\n')}\n\nPREVIOUS ACTIONS:\n${(body.previousActions || []).slice(-5).map(a => `- ${a.type} \"${a.target}\": ${a.success ? 'OK' : 'FAILED'}`).join('\\n') || 'None'}\n`;\n\nreturn [{ json: { context, task: body.task, sessionId: body.sessionId } }];"
      },
      "id": "prepare-context",
      "name": "\ud83d\udd27 Prepare Context",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4o",
          "mode": "list"
        },
        "messages": {
          "values": [
            {
              "content": "=You are an AI agent navigating legacy interfaces. Analyze the screen and decide the next action.\n\n{{ $json.context }}\n\nRespond with JSON:\n```json\n{\n  \"actions\": [{\"type\": \"click|type|wait|scroll|extract\", \"target\": \"element label\", \"value\": \"text if typing\"}],\n  \"confidence\": 0.0-1.0,\n  \"reasoning\": \"why this action\",\n  \"isComplete\": false,\n  \"extractedData\": null\n}\n```\n\nRULES:\n- Max 3 actions per response\n- Use element labels from VISIBLE ELEMENTS\n- Set isComplete=true when task is done\n- Put extracted data in extractedData field"
            }
          ]
        },
        "jsonOutput": true,
        "options": {
          "maxTokens": 1000,
          "temperature": 0.2
        }
      },
      "id": "llm-planning",
      "name": "\ud83e\udde0 GPT-4o Planning",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        680,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Validate and sanitize LLM response\nconst input = $input.first().json;\n\nlet response;\ntry {\n  if (input.message?.content) {\n    const content = input.message.content;\n    response = typeof content === 'object' ? content : JSON.parse(content.replace(/```json?\\s*|```/g, ''));\n  } else if (input.actions) {\n    response = input;\n  } else {\n    throw new Error('Unknown format');\n  }\n} catch (e) {\n  response = { actions: [], confidence: 0.1, reasoning: 'Parse error: ' + e.message, isComplete: false, extractedData: null };\n}\n\n// Validate actions\nconst validTypes = ['click', 'type', 'wait', 'scroll', 'extract', 'navigate', 'select'];\nconst safeActions = (response.actions || []).filter(a => \n  validTypes.includes(a.type) && \n  (a.target || a.type === 'wait') &&\n  !(a.value?.includes('<script') || a.value?.includes('javascript:'))\n);\n\nreturn [{ json: {\n  actions: safeActions.slice(0, 3),\n  confidence: Math.max(0, Math.min(1, response.confidence || 0.5)),\n  reasoning: response.reasoning || 'No reasoning',\n  isComplete: response.isComplete === true,\n  extractedData: response.extractedData || null\n}}];"
      },
      "id": "validate-response",
      "name": "\u2705 Validate & Secure",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify($json) }}",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "X-Planning-Version",
                "value": "1.0"
              }
            ]
          }
        }
      },
      "id": "respond-webhook",
      "name": "\ud83d\udce4 Send Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1120,
        300
      ]
    }
  ],
  "connections": {
    "\ud83d\udce5 Receive Planning Request": {
      "main": [
        [
          {
            "node": "\ud83d\udd27 Prepare Context",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83d\udd27 Prepare Context": {
      "main": [
        [
          {
            "node": "\ud83e\udde0 GPT-4o Planning",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\ud83e\udde0 GPT-4o Planning": {
      "main": [
        [
          {
            "node": "\u2705 Validate & Secure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "\u2705 Validate & Secure": {
      "main": [
        [
          {
            "node": "\ud83d\udce4 Send Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}