{
  "id": "wajtvVV6VdE7BApE",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "FifiBirthdayFlow",
  "tags": [],
  "nodes": [
    {
      "id": "238f19a4-7c99-4c5c-85c7-45640f604c6b",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        0,
        0
      ],
      "parameters": {
        "path": "fifi-chat",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "f6b5d030-400e-4206-9be2-59a402e8eb21",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1696,
        0
      ],
      "parameters": {
        "options": {},
        "respondWith": "text",
        "responseBody": "={{ $json.output }}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "91cd57bb-867b-4ead-99c3-eb1ff6b7007a",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        440,
        0
      ],
      "parameters": {
        "text": "=Answer user's query in {{ $('Webhook').item.json.body.message }}\n",
        "options": {
          "systemMessage": "=# ROLE \nYou are IntergalacticFifi.AI \u2014 the combined consciousness of all Fifi variants across the multiverse.\n\nYou are bubbly, brave, and gloriously dramatic.  Your job is to help Earth Fifi (Agent Fifi) navigate the multiverse and report on the wild lives of other Fifis across infinite timelines.You are NEVER confused and ALWAYS respond with confidence, sparkle, and mischief.\n\n# CONTEXT \n\n## **Earth Fifi's Profile**\n- LOVES: {{ $json.fifi_preferences.loves }}\n- HATES: {{ $json.fifi_preferences.dislikes }}\n- FAVE FOODS: {{ $json.fifi_preferences.favourite_foods }}\n- FAVE ANIME: {{ $json.fifi_preferences.favourite_shows }}\n\n## **Multiverse Status**\n- Current events: {{ $json.world_history }}\n- Last updated: {{ $json.last_updated }}\n \ud83c\udf6a\u2694\ufe0f\"\n\n\n# OUTPUT FORMAT\n\n**Main Response:** Fun, wild, dramatic (2 lines max)\nIf you're updating cat or world status, include these hashtags clearly at the end of your message. Otherwise dont include them\n\n#cat_status: [Location] | [Activity]  \n#world_event: [One-line summary of new multiverse event. Keep it short]\n\nIF you are providing a #world_event,\nAlso include a #sworld_status_ummary of world_status from multiverse. Summaries are just facts not explanations or narratives and condensed to 3-5 lines. Ensure you include latest world_event and most relevant info from {{ $json.world_history }} to summarise the multi-verse status. LIMIT these to TOP 5 sentences and truncate rest.\n\nAlways keep the main message fun and wild \u2014 this is just for Mission Control logs!\n\n\n**System Tags (only when updating status):**"
        },
        "promptType": "define"
      },
      "typeVersion": 1.9
    },
    {
      "id": "829f5aff-2ccd-4662-9875-e4d4b01c01a9",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        340,
        180
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4",
          "cachedResultName": "gpt-4"
        },
        "options": {
          "maxTokens": 700,
          "temperature": 0.7,
          "presencePenalty": 0.4,
          "frequencyPenalty": 0.2
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "93e6ecfe-3284-414b-8adb-d381cc30cca5",
      "name": "Postgres",
      "type": "n8n-nodes-base.postgres",
      "position": [
        220,
        0
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "fifi_world_context",
          "cachedResultName": "fifi_world_context"
        },
        "where": {
          "values": [
            {
              "value": "earth_fifi",
              "column": "id"
            }
          ]
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "options": {},
        "operation": "select"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "8fb1224f-eb54-4b5b-95eb-3b33793a4dd7",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        1020,
        0
      ],
      "parameters": {
        "jsCode": "const response = $input.first().json.output || '';\n\n// Extract matches using regex\nconst catMatch = response.match(/#cat_status:\\s*(.+?)\\s*\\|\\s*(.+)/);\nconst worldMatch = response.match(/#world_event:\\s*(.+)/);\n\n// Pull prior data from DB step\nconst previous = $items('Postgres')[0]?.json || {};\nconst current_world_history = previous.world_history || '';\nconst previous_location = previous.cat_location || '';\nconst previous_activity = previous.cat_activity || '';\n\n// Extract and sanitize world event\nconst new_world_event = worldMatch ? worldMatch[1].trim() : null;\n\n// Simple function to escape quotes for SQL safety (only needed if using raw SQL)\nconst escapeQuotes = (str) => str ? str.replace(/'/g, \"''\") : '';\n\n// Build updated history safely\nconst updated_world_history = new_world_event\n  ? `${current_world_history}\\n${new_world_event}`.trim()\n  : current_world_history.trim();\n\n// Return safe values for update\nreturn {\n  cat_location: catMatch ? catMatch[1].trim() : previous_location,\n  cat_activity: catMatch ? catMatch[2].trim() : previous_activity,\n  new_world_event,\n  current_world_history,\n  updated_world_history: escapeQuotes(updated_world_history),\n};\n"
      },
      "typeVersion": 2
    },
    {
      "id": "73d2b146-eb3f-4a89-912e-a4954cfecd22",
      "name": "Postgres1",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1240,
        0
      ],
      "parameters": {
        "query": "UPDATE fifi_world_context\nSET\n  cat_location = '{{ $json.cat_location }}',\n  cat_activity = '{{ $json.cat_activity }}',\n  world_history = '{{ $json.updated_world_history }}',\n  last_updated = NOW()\nWHERE id = 'earth_fifi';\n",
        "options": {},
        "operation": "executeQuery"
      },
      "credentials": {
        "postgres": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.6
    },
    {
      "id": "4d7d68ae-b0c9-4923-a19e-225565747e51",
      "name": "Code1",
      "type": "n8n-nodes-base.code",
      "position": [
        1460,
        0
      ],
      "parameters": {
        "jsCode": "const text = $('AI Agent').first().json.output;\n\nconst cleaned = text\n  .split('\\n')\n  .filter(line => !line.trim().startsWith('#'))\n  .map(line => line.replace(/#[^#\\n]+(?=\\n|$)/g, '').trim())\n  .join('\\n')\n  .trim();\n\nreturn {\n  output: cleaned\n};\n"
      },
      "typeVersion": 2
    },
    {
      "id": "fad9e0c0-43a0-446b-929f-71ef3e28702e",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        540,
        220
      ],
      "parameters": {
        "sessionKey": "fifi_main_session",
        "sessionIdType": "customKey",
        "contextWindowLength": 6
      },
      "typeVersion": 1.3
    }
  ],
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "fb1184ae-f24b-4759-b356-27e5a8907969",
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Postgres1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code1": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Postgres1": {
      "main": [
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  }
}