{
  "meta": { "instanceId": "aibn-ai-support-bot" },
  "nodes": [
    {
      "parameters": { "httpMethod": "POST", "path": "chat", "options": {} },
      "name": "Chat Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [200, 300],
      "id": "c1b2c3d4-0001-4000-8000-000000000001",
      "webhookId": "chat-webhook"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            { "id": "1", "name": "message", "value": "={{ $json.body.message }}", "type": "string" },
            { "id": "2", "name": "userId", "value": "={{ $json.body.userId }}", "type": "string" }
          ]
        },
        "options": {}
      },
      "name": "Parse Request",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [400, 300],
      "id": "c1b2c3d4-0002-4000-8000-000000000002"
    },
    {
      "parameters": {
        "operation": "get",
        "key": "={{ 'history:' + $json.userId }}"
      },
      "name": "Load Chat History",
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [600, 300],
      "id": "c1b2c3d4-0003-4000-8000-000000000003"
    },
    {
      "parameters": {
        "resource": "chat",
        "model": "gpt-4o-mini",
        "messages": {
          "values": [
            {
              "content": "you are a helpful support agent for a vibe-coded saas product. be concise and actionable."
            },
            { "content": "={{ $json.message }}", "role": "user" }
          ]
        },
        "options": { "temperature": 0.7 }
      },
      "name": "OpenAI Chat",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.4,
      "position": [800, 300],
      "id": "c1b2c3d4-0004-4000-8000-000000000004"
    },
    {
      "parameters": {
        "operation": "set",
        "key": "={{ 'history:' + $('Parse Request').item.json.userId }}",
        "value": "={{ JSON.stringify($json) }}"
      },
      "name": "Save History",
      "type": "n8n-nodes-base.redis",
      "typeVersion": 1,
      "position": [1000, 300],
      "id": "c1b2c3d4-0005-4000-8000-000000000005"
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"reply\": {{ JSON.stringify($json.message.content) }} }"
      },
      "name": "Respond",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [1200, 300],
      "id": "c1b2c3d4-0006-4000-8000-000000000006"
    }
  ],
  "connections": {
    "Chat Webhook": { "main": [[{ "node": "Parse Request", "type": "main", "index": 0 }]] },
    "Parse Request": { "main": [[{ "node": "Load Chat History", "type": "main", "index": 0 }]] },
    "Load Chat History": { "main": [[{ "node": "OpenAI Chat", "type": "main", "index": 0 }]] },
    "OpenAI Chat": { "main": [[{ "node": "Save History", "type": "main", "index": 0 }]] },
    "Save History": { "main": [[{ "node": "Respond", "type": "main", "index": 0 }]] }
  },
  "pinData": {}
}
