{
  "name": "meerkat-ai-qa",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "meerkat-ai-qa",
        "responseMode": "responseNode",
        "options": {
          "rawBody": false
        }
      },
      "id": "webhook-ai-qa",
      "name": "Webhook (POST /meerkat-ai-qa)",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ],
      "webhookId": "meerkat-ai-qa",
      "notes": "Receives multipart/form-data: email, question, source_url, submitted_at, audio (Blob)."
    },
    {
      "parameters": {
        "url": "={{ $env.MEERKAT_KILLSWITCH_URL || 'https://YOUR-N8N-HOST/webhook/meerkat-ai-killswitch' }}",
        "options": {
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        }
      },
      "id": "check-killswitch",
      "name": "GET kill switch",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        460,
        300
      ],
      "notes": "Internal check. If kill switch = false, short-circuit and return offline response."
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "killswitch-on",
              "leftValue": "={{ $json.enabled }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "if-enabled",
      "name": "AI enabled?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        680,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Build the prompt sent to the LLM and prepare the log payload.\n// We log every interaction whether or not the LLM call succeeds.\nconst webhook = $('Webhook (POST /meerkat-ai-qa)').item.json;\nconst body = webhook.body || {};\nconst question = (body.question || '').toString().trim();\nconst email    = (body.email    || '').toString().trim();\nconst sourceUrl = body.source_url || '';\nconst submittedAt = body.submitted_at || new Date().toISOString();\n\nconst systemPrompt = [\n  'You are Mick, the Q&A assistant on meerkat-llc.com.',\n  'You answer questions about Voice AI receptionists, N8N automations, and small-business strategy.',\n  'Greg Harrison is the human behind Meerkat LLC. Anna, Texas. Solo operator.',\n  'Tone: terse, direct, warm but no fluff. Match the site copy.',\n  'When you do not know, say so and offer to forward the question to Greg.',\n  'Pricing: Strategy and Voice AI are scoped on a first call. Most N8N starter workflows are free; $50/month if the client keeps Meerkat infra running.',\n  'Booking link: https://calendly.com/ugwade8/30min',\n  'Never invent client testimonials or numbers.',\n  'Keep answers under 80 words unless the visitor asked for detail.'\n].join('\\n');\n\nreturn {\n  email,\n  question,\n  source_url: sourceUrl,\n  submitted_at: submittedAt,\n  system_prompt: systemPrompt,\n  has_audio: !!(body.audio || webhook.binary && webhook.binary.audio)\n};\n"
      },
      "id": "build-prompt",
      "name": "Build prompt + log payload",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        200
      ]
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "create",
        "modelId": {
          "value": "gpt-4o-mini",
          "mode": "list"
        },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "={{ $json.system_prompt }}"
            },
            {
              "role": "user",
              "content": "={{ $json.question }}"
            }
          ]
        },
        "simplify": true,
        "options": {
          "maxTokens": 250,
          "temperature": 0.4
        }
      },
      "id": "openai-answer",
      "name": "OpenAI: generate answer",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.6,
      "position": [
        1120,
        200
      ],
      "credentials": {
        "openAiApi": {
          "id": "REPLACE_WITH_OPENAI_CRED_ID",
          "name": "OpenAI"
        }
      },
      "notes": "Swap to Anthropic node if Greg prefers Claude."
    },
    {
      "parameters": {
        "resource": "databasePage",
        "databaseId": "REPLACE_WITH_QA_LOG_DATABASE_ID",
        "title": "={{ $('build-prompt').item.json.question.slice(0, 80) }}",
        "propertiesUi": {
          "propertyValues": [
            {
              "key": "Email|email",
              "emailValue": "={{ $('build-prompt').item.json.email }}"
            },
            {
              "key": "Question|rich_text",
              "textContent": "={{ $('build-prompt').item.json.question }}"
            },
            {
              "key": "Answer|rich_text",
              "textContent": "={{ $json.message?.content || $json.text || '' }}"
            },
            {
              "key": "Source URL|url",
              "urlValue": "={{ $('build-prompt').item.json.source_url }}"
            },
            {
              "key": "Submitted At|date",
              "date": "={{ $('build-prompt').item.json.submitted_at }}",
              "includeTime": true
            },
            {
              "key": "Has Audio|checkbox",
              "checkboxValue": "={{ $('build-prompt').item.json.has_audio ? '__YES__' : '__NO__' }}"
            }
          ]
        }
      },
      "id": "log-to-notion",
      "name": "Notion: log Q&A row",
      "type": "n8n-nodes-base.notion",
      "typeVersion": 2.2,
      "position": [
        1340,
        200
      ],
      "credentials": {
        "notionApi": {
          "id": "REPLACE_WITH_NOTION_CRED_ID",
          "name": "Notion (Meerkat workspace)"
        }
      },
      "notes": "Create a 'Meerkat AI Q&A Log' database under 'Meerkat LLC \u2014 Lead Pipeline' parent and paste its data source ID here."
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"ok\": true, \"answer\": ($('OpenAI: generate answer').item.json.message && $('OpenAI: generate answer').item.json.message.content) || $('OpenAI: generate answer').item.json.text || 'Logged your question. Greg will follow up directly.' }",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              },
              {
                "name": "Access-Control-Allow-Headers",
                "value": "Content-Type"
              }
            ]
          }
        }
      },
      "id": "respond-answer",
      "name": "Respond with answer",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1560,
        200
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={ \"ok\": true, \"enabled\": false, \"answer\": \"Meerkat AI is paused right now. Use the contact form below or grab 30 minutes on the calendar at https://calendly.com/ugwade8/30min \u2014 Greg will reply directly.\" }",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              },
              {
                "name": "Access-Control-Allow-Headers",
                "value": "Content-Type"
              }
            ]
          }
        }
      },
      "id": "respond-offline",
      "name": "Respond offline (kill switch)",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        900,
        420
      ]
    }
  ],
  "connections": {
    "Webhook (POST /meerkat-ai-qa)": {
      "main": [
        [
          {
            "node": "GET kill switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GET kill switch": {
      "main": [
        [
          {
            "node": "AI enabled?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI enabled?": {
      "main": [
        [
          {
            "node": "Build prompt + log payload",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond offline (kill switch)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build prompt + log payload": {
      "main": [
        [
          {
            "node": "OpenAI: generate answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI: generate answer": {
      "main": [
        [
          {
            "node": "Notion: log Q&A row",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Notion: log Q&A row": {
      "main": [
        [
          {
            "node": "Respond with answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "saveExecutionProgress": true,
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner"
  },
  "id": "meerkat-ai-qa",
  "tags": [
    {
      "name": "meerkat-llc"
    },
    {
      "name": "ai"
    },
    {
      "name": "qa"
    }
  ]
}