{
  "name": "MarketMind - Run Analysis (Webhook)",
  "nodes": [
    {
      "parameters": {
        "path": "marketmind/run",
        "httpMethod": "POST",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "WebhookTrigger",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        260,
        260
      ]
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "product_name",
              "value": "={{$json.body.product_name || 'EcoWave Smart Bottle'}}"
            },
            {
              "name": "industry",
              "value": "={{$json.body.industry || 'Consumer Goods'}}"
            },
            {
              "name": "geography",
              "value": "={{$json.body.geography || 'Global'}}"
            },
            {
              "name": "scale",
              "value": "={{$json.body.scale || 'SME'}}"
            },
            {
              "name": "OPENAI_API_KEY",
              "value": "={{$env.OPENAI_API_KEY}}"
            },
            {
              "name": "SERPER_API_KEY",
              "value": "={{$env.SERPER_API_KEY}}"
            },
            {
              "name": "repo_path",
              "value": "=/data/marketmind"
            }
          ]
        },
        "options": {}
      },
      "id": "SetInputs",
      "name": "Set Inputs",
      "type": "n8n-nodes-base.set",
      "typeVersion": 2,
      "position": [
        520,
        260
      ]
    },
    {
      "parameters": {
        "command": "bash",
        "arguments": [
          "-lc",
          "set -euo pipefail\ncd \"{{$json.repo_path}}\"\n\n# Clean outputs for a fresh run\nrm -rf outputs\nmkdir -p outputs\n\n# Export runtime env vars expected by your code\nexport PRODUCT_NAME=\"{{$json.product_name}}\"\nexport INDUSTRY=\"{{$json.industry}}\"\nexport GEOGRAPHY=\"{{$json.geography}}\"\nexport SCALE=\"{{$json.scale}}\"\n\n# API keys (set these as n8n environment variables or replace with fixed values)\nexport OPENAI_API_KEY=\"{{$json.OPENAI_API_KEY}}\"\nexport SERPER_API_KEY=\"{{$json.SERPER_API_KEY}}\"\n\n# (Recommended) ensure dependencies exist (optional)\n# python3 -m pip install -r requirements.txt\n\npython3 main.py\n\n# Print the final report to stdout so n8n can capture it\necho \"\\n---FINAL_REPORT_START---\"\nif [ -f outputs/final_market_strategy_report.md ]; then\n  cat outputs/final_market_strategy_report.md\nelse\n  echo \"(No final report generated: outputs/final_market_strategy_report.md not found.)\"\nfi\necho \"---FINAL_REPORT_END---\"\n"
        ],
        "options": {
          "maxBuffer": 10485760
        }
      },
      "id": "ExecuteMain",
      "name": "Execute main.py",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        780,
        260
      ]
    },
    {
      "parameters": {
        "functionCode": "const out = $json.stdout || '';\nconst start = out.indexOf('---FINAL_REPORT_START---');\nconst end = out.indexOf('---FINAL_REPORT_END---');\nlet report = '';\nif (start !== -1 && end !== -1 && end > start) {\n  report = out.slice(start + '---FINAL_REPORT_START---'.length, end).trim();\n}\nreturn [{\n  success: true,\n  product_name: $json.product_name,\n  industry: $json.industry,\n  geography: $json.geography,\n  scale: $json.scale,\n  report,\n  raw_stdout_tail: out.slice(-2000)\n}];"
      },
      "id": "ParseReport",
      "name": "Parse Final Report",
      "type": "n8n-nodes-base.function",
      "typeVersion": 2,
      "position": [
        1040,
        260
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"success\": $json.success,\n  \"product_name\": $json.product_name,\n  \"industry\": $json.industry,\n  \"geography\": $json.geography,\n  \"scale\": $json.scale,\n  \"final_report\": $json.report,\n  \"note\": \"If final_report is empty, check that outputs/final_market_strategy_report.md was created. raw_stdout_tail included for debugging.\",\n  \"raw_stdout_tail\": $json.raw_stdout_tail\n}",
        "options": {}
      },
      "id": "WebhookResponse",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1300,
        260
      ]
    },
    {
      "id": "28d620cb-9a7a-59d9-8593-8722bdd4406c",
      "name": "Publication · Overview and setup",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -800,
        0
      ],
      "parameters": {
        "content": "# MarketMind - Run Analysis (Webhook)\n\n## Purpose and structure\nThis folder contains the **MarketMind - Run Analysis (Webhook)** workflow. Its executable nodes use `executeCommand`, `function`, `respondToWebhook`, `set`, `webhook`. The table and connection map below are derived from the JSON, not an inferred guarantee of behavior.\n\n## Setup\nImport inactive. Configure your own credentials and replace REPLACE_WITH_* values. Review external destinations and Code nodes before running.\n\n## Entry points\nWebhook Trigger\n\n## Verification\nSanitized source candidate; not runtime-tested. This overview is generated from node metadata. Read the folder README for the connection map, credential types, and license.\n\n## Attribution\nhttps://github.com/Humanitariansai/Madison\nMIT; preserve source notices.",
        "width": 700,
        "height": 800,
        "color": 1
      }
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Set Inputs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Inputs": {
      "main": [
        [
          {
            "node": "Execute main.py",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute main.py": {
      "main": [
        [
          {
            "node": "Parse Final Report",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Final Report": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionTimeout": 3600
  }
}
