{
  "nodes": [
    {
      "id": "6bdbfe7d-5763-4047-876a-c61aae517887",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "notes": "\ud83e\udd16 CORE AI AGENT\n\nThis is the main LangChain agent that:\n- Receives formatted conversation history\n- Uses vector database for context retrieval\n- Can search web when needed\n- Maintains conversation memory\n- Generates intelligent responses\n\nConnected Tools:\n\u2705 Vector Store (knowledge base)\n\u2705 SerpAPI (web search)\n\u2705 Memory buffer\n\u2705 OpenAI GPT-4",
      "position": [
        720,
        420
      ],
      "parameters": {},
      "typeVersion": 1.7
    },
    {
      "id": "07c33b29-da37-4283-a9b9-314ffd3247d0",
      "name": "Extract Message",
      "type": "n8n-nodes-base.set",
      "notes": "\ud83d\udcdd MESSAGE EXTRACTION\n\nExtracts key data from Google Sheets:\n- user_message: The actual question/request\n- chat_id: Unique identifier for this conversation\n\nThis data flows to:\n\u2192 Chat history retrieval\n\u2192 Response formatting",
      "position": [
        -80,
        420
      ],
      "parameters": {},
      "typeVersion": 3.4
    },
    {
      "id": "7c33317a-95a5-4f21-92b3-7c1dcf97bc43",
      "name": "Format Conversation",
      "type": "n8n-nodes-base.function",
      "notes": "\ud83d\udd04 CONVERSATION FORMATTER\n\nProcesses chat history into proper format:\n\n1. Takes chat history from Google Sheets\n2. Converts to OpenAI message format:\n   - role: 'system' | 'user' | 'assistant'\n   - content: message text\n3. Keeps last 10 messages for context\n4. Adds current user message\n5. Includes system prompt with instructions\n\nOutput: Properly formatted conversation array for AI agent",
      "position": [
        360,
        420
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "bfcd6c1b-2db4-49c0-9855-c8d5f3e979ab",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "notes": "\ud83e\udde0 MAIN LANGUAGE MODEL\n\nOpenAI GPT-4.1-nano for:\n- Natural conversation\n- Complex reasoning\n- Task completion\n- Creative responses\n\nSetup Required:\n\u2192 Add OpenAI API credentials\n\u2192 Ensure sufficient quota",
      "position": [
        580,
        640
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "eadfe308-8c1d-4a41-a218-a091c53df572",
      "name": "Embeddings OpenAI",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "notes": "\ud83c\udfaf VECTOR EMBEDDINGS\n\nConverts text to numerical vectors for:\n- Semantic similarity search\n- Context retrieval from knowledge base\n- Intelligent document matching\n\nUses OpenAI's text-embedding-3-small model",
      "position": [
        800,
        1040
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "141cb0cc-f004-495f-9ce9-2261daca70d7",
      "name": "Vector Store LLM",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "notes": "\ud83d\udd0d KNOWLEDGE BASE LLM\n\nDedicated model for vector store queries:\n- Processes retrieved documents\n- Summarizes relevant context\n- Filters information for main agent\n\nSeparate from main model for efficiency",
      "position": [
        1000,
        840
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "d48725fd-6f18-4ea6-8d26-4a2b9252c317",
      "name": "Vector Store Tool",
      "type": "@n8n/n8n-nodes-langchain.toolVectorStore",
      "notes": "\ud83d\uddc3\ufe0f KNOWLEDGE BASE TOOL\n\nSearches vector database for relevant context:\n- Retrieves top 8 most similar documents\n- Uses semantic similarity matching\n- Provides context to main AI agent\n\nConnected to Supabase vector store",
      "position": [
        820,
        640
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "d8859552-a2d7-4ada-a57e-2d542f80cbf1",
      "name": "Supabase Vector Store",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "notes": "\ud83d\udcbe VECTOR DATABASE\n\nSupabase vector store setup:\n\n1. Create 'documents' table in Supabase\n2. Enable vector extension (pgvector)\n3. Store embeddings + metadata\n4. Enable RLS policies\n\nTable columns needed:\n- id, content, metadata, embedding",
      "position": [
        720,
        840
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "544ca207-6c7d-4f70-85bc-e9954fb404a4",
      "name": "Conversation Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "notes": "\ud83e\udde0 CONVERSATION MEMORY\n\nMaintains context across interactions:\n- Remembers recent conversation\n- Uses custom session key\n- Provides continuity for AI agent\n\nMemory Type: Buffer Window\n(keeps recent messages in memory)",
      "position": [
        700,
        640
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "id": "1c4c501e-6d24-43ab-9781-1ca2b36898a1",
      "name": "Google Sheets Trigger",
      "type": "n8n-nodes-base.googleSheetsTrigger",
      "notes": "\ud83d\ude80 WORKFLOW TRIGGER\n\nMonitors Google Sheets for new messages:\n\nRequired Columns:\n- user_message (trigger column)\n- ai_respond (AI response)\n- id (unique identifier)\n\nSetup Steps:\n1. Create Google Sheet with columns\n2. Share with service account\n3. Connect credentials\n4. Set polling interval (every minute)\n\nTriggers when user_message column changes",
      "position": [
        -520,
        250
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "6f3c6a51-12d3-41af-8c2f-e84a7d442f80",
      "name": "Check If New Message",
      "type": "n8n-nodes-base.if",
      "notes": "\u2705 DUPLICATE PREVENTION\n\nPrevents processing already-answered messages:\n\n\u2713 If ai_respond is empty \u2192 Process message\n\u2717 If ai_respond has content \u2192 Skip\n\nThis ensures each user message is only processed once, avoiding infinite loops or duplicate responses.",
      "position": [
        -300,
        250
      ],
      "parameters": {},
      "typeVersion": 2.2
    },
    {
      "id": "7019c655-2740-420c-8fe6-b1342ae3dd13",
      "name": "Merge Response Data",
      "type": "n8n-nodes-base.merge",
      "notes": "\ud83d\udd17 DATA MERGER\n\nCombines data for final update:\n\nInput 1: Original message + timestamp ID\nInput 2: AI agent response\n\nOutput: Complete record ready for Google Sheets update with all required fields",
      "position": [
        1120,
        200
      ],
      "parameters": {},
      "typeVersion": 3.1
    },
    {
      "id": "47034580-420c-4b11-870e-07be8ea37436",
      "name": "Generate Timestamp ID",
      "type": "n8n-nodes-base.function",
      "notes": "\ud83d\udd50 UNIQUE ID GENERATOR\n\nGenerates unique timestamp-based ID:\n- Uses current timestamp (milliseconds)\n- Ensures each message has unique identifier\n- Prevents duplicate entries\n- Used for Google Sheets row identification\n\nField: myNewField \u2192 ID column",
      "position": [
        -20,
        180
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "575fc087-be86-40c1-b7d2-7d8e0d9fb7f5",
      "name": "Get Chat History",
      "type": "n8n-nodes-base.googleSheets",
      "notes": "\ud83d\udcda CHAT HISTORY RETRIEVAL\n\nRetrieves previous conversations:\n- Reads all rows from Google Sheets\n- Provides context for conversation formatting\n- Enables memory across sessions\n\nUsed by Format Conversation node to build proper chat history for AI agent",
      "position": [
        140,
        420
      ],
      "parameters": {},
      "typeVersion": 4.6
    },
    {
      "id": "7b4a3d27-03aa-4229-90b6-ba5180af0eea",
      "name": "Update AI Response",
      "type": "n8n-nodes-base.googleSheets",
      "notes": "\ud83d\udcbe RESPONSE STORAGE\n\nUpdates Google Sheets with AI response:\n\nOperation: appendOrUpdate\n- Matches by user_message column\n- Updates ai_respond with agent output\n- Adds unique ID for tracking\n\nThis completes the conversation loop and makes the response visible to users",
      "position": [
        1440,
        360
      ],
      "parameters": {},
      "typeVersion": 4.6
    },
    {
      "id": "892ac40d-b3dc-4cb5-b634-0afae0e91a71",
      "name": "Web Search Tool",
      "type": "@n8n/n8n-nodes-langchain.toolSerpApi",
      "notes": "\ud83c\udf10 WEB SEARCH CAPABILITY\n\nProvides real-time web search for AI agent:\n- Access to current information\n- Google search results\n- Supplements knowledge base\n\nSetup Required:\n\u2192 SerpAPI account & API key\n\u2192 Sufficient search quota\n\nAI agent automatically uses when needed",
      "position": [
        1120,
        640
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "8169a7cc-1585-44d7-80c2-e5ebdfd37532",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -580,
        140
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "32cb9197-527d-4d12-b67a-51cecf2b866e",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1240,
        140
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    },
    {
      "id": "0d2f99a7-8140-4679-802d-65c0336e351a",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1240,
        580
      ],
      "parameters": {
        "content": ""
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "AI Agent": {
      "main": [
        [
          {
            "node": "Merge Response Data",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Extract Message": {
      "main": [
        [
          {
            "node": "Get Chat History",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Web Search Tool": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Get Chat History": {
      "main": [
        [
          {
            "node": "Format Conversation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Vector Store LLM": {
      "ai_languageModel": [
        [
          {
            "node": "Vector Store Tool",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings OpenAI": {
      "ai_embedding": [
        [
          {
            "node": "Supabase Vector Store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Vector Store Tool": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Conversation Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Format Conversation": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Response Data": {
      "main": [
        [
          {
            "node": "Update AI Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check If New Message": {
      "main": [
        [
          {
            "node": "Extract Message",
            "type": "main",
            "index": 0
          },
          {
            "node": "Generate Timestamp ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Timestamp ID": {
      "main": [
        [
          {
            "node": "Merge Response Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets Trigger": {
      "main": [
        [
          {
            "node": "Check If New Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Supabase Vector Store": {
      "ai_vectorStore": [
        [
          {
            "node": "Vector Store Tool",
            "type": "ai_vectorStore",
            "index": 0
          }
        ]
      ]
    }
  }
}