{
  "name": "Website Live Chat AI",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "live-chat",
        "responseMode": "responseNode",
        "options": {
          "allowedOrigins": "*"
        }
      },
      "id": "d4e5f6a7-4444-4000-8000-000000000001",
      "name": "Chat Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "set-msg",
              "name": "userMessage",
              "value": "={{ $json.body.message }}",
              "type": "string"
            },
            {
              "id": "set-session",
              "name": "sessionId",
              "value": "={{ $json.body.sessionId || 'anonymous-' + Date.now() }}",
              "type": "string"
            },
            {
              "id": "set-page",
              "name": "currentPage",
              "value": "={{ $json.body.pageUrl || 'unknown' }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "d4e5f6a7-4444-4000-8000-000000000002",
      "name": "Parse Chat Input",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "message",
        "model": "gpt-4o",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are a live chat support agent for a website. Be helpful, professional, and concise. The user is currently on page: {{ $json.currentPage }}. If you cannot answer a question, offer to connect them with a human agent. Format responses in plain text suitable for a chat widget. Keep responses under 200 words."
            },
            {
              "role": "user",
              "content": "={{ $json.userMessage }}"
            }
          ]
        },
        "options": {
          "temperature": 0.6,
          "maxTokens": 500
        }
      },
      "id": "d4e5f6a7-4444-4000-8000-000000000003",
      "name": "OpenAI Chat Response",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.6,
      "position": [
        680,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ reply: $json.message.content, sessionId: $('Parse Chat Input').item.json.sessionId, timestamp: new Date().toISOString() }) }}",
        "options": {
          "responseCode": 200,
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              },
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "id": "d4e5f6a7-4444-4000-8000-000000000004",
      "name": "Respond to Chat",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        900,
        300
      ]
    }
  ],
  "connections": {
    "Chat Webhook": {
      "main": [
        [
          {
            "node": "Parse Chat Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Chat Input": {
      "main": [
        [
          {
            "node": "OpenAI Chat Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Response": {
      "main": [
        [
          {
            "node": "Respond to Chat",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null
}