{
  "name": "Generate & test SQL code with GPT/OpenRouter AI and PostgreSQL sandbox",
  "nodes": [
    {
      "id": "47f2b3b4-12f0-4e9c-82f4-54d2c32b77f0",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -1824,
        128
      ]
    },
    {
      "id": "424b2831-425b-4ee7-a3d5-b2713bfdf343",
      "name": "GenerateErrorPrompt",
      "type": "n8n-nodes-base.code",
      "position": [
        5264,
        752
      ]
    },
    {
      "id": "07f2f95a-0958-4562-b5c4-1bb6504a02fd",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        1248,
        400
      ]
    },
    {
      "id": "7f42bf3b-5d99-41d4-94e7-0a34da39e285",
      "name": "AutoErrorFixing",
      "type": "n8n-nodes-base.if",
      "position": [
        3360,
        -64
      ]
    },
    {
      "id": "6e1fcee6-2c63-46e3-a48c-458180257b43",
      "name": "IfError",
      "type": "n8n-nodes-base.if",
      "position": [
        3072,
        32
      ]
    },
    {
      "id": "4f3fcdf7-fe96-4137-9356-165890ea57d1",
      "name": "Execute_AI_result",
      "type": "n8n-nodes-base.postgres",
      "position": [
        4288,
        432
      ]
    },
    {
      "id": "d4c60f3d-425e-4fef-923c-a2fa154f0a00",
      "name": "isAssistantExists",
      "type": "n8n-nodes-base.if",
      "position": [
        112,
        -464
      ]
    },
    {
      "id": "318a17bb-7566-4b2c-bd3f-d6e57daa1b95",
      "name": "isAssistantExistsCode",
      "type": "n8n-nodes-base.code",
      "position": [
        -208,
        -464
      ]
    },
    {
      "id": "ff979107-3937-4feb-aaf3-4ff9337325da",
      "name": "isOpenAI",
      "type": "n8n-nodes-base.if",
      "position": [
        5488,
        752
      ]
    },
    {
      "id": "6e2a2dd7-8ca7-4662-9c7d-5c7cab0dd314",
      "name": "assistant",
      "type": "n8n-nodes-base.set",
      "position": [
        592,
        -480
      ]
    },
    {
      "id": "af7314ea-60db-4873-838d-7a01dffc70ed",
      "name": "IfOpenAI",
      "type": "n8n-nodes-base.if",
      "position": [
        -1184,
        128
      ]
    },
    {
      "id": "db247d1d-8277-475b-98c8-4150326e0dfb",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1536,
        32
      ],
      "parameters": {
        "width": null,
        "height": 260,
        "content": "use this to get neccessary local variables, like: instruction to AI, sessionId, and all inputed parameters from previous node\n"
      }
    },
    {
      "id": "02dbf130-9e82-4d85-b743-2cb488fb0f46",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1904,
        -176
      ],
      "parameters": {
        "width": 260,
        "height": 460,
        "content": "Input parameters:\n1. sessionId: uuidv4\n2. threadId: nullable\n3. apiKey: string\n4. aiProvider: string\n5. model: string\n6. autoErrorFixing: boolean\n7. chatInput: string (users prompt)\n8. currentDbSchema"
      }
    },
    {
      "id": "e10c42db-8c9f-4052-8205-259b53ebdc3f",
      "name": "IsMaxAutoErrorReached",
      "type": "n8n-nodes-base.if",
      "position": [
        3760,
        -320
      ]
    },
    {
      "id": "1a3189fd-550e-4b1c-8720-65579fa39c44",
      "name": "OpenRouter Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "position": [
        1120,
        400
      ]
    },
    {
      "id": "20960f49-d573-45e0-a3e0-f5e181a8ef66",
      "name": "AgentName",
      "type": "n8n-nodes-base.set",
      "position": [
        -896,
        -464
      ]
    },
    {
      "id": "9f28f786-c999-4614-8d74-e67f945583e2",
      "name": "setOutputByProvider",
      "type": "n8n-nodes-base.set",
      "position": [
        2688,
        -208
      ]
    },
    {
      "id": "c1c4bc98-9f7f-459b-9c1e-8ce1de98f24c",
      "name": "OpenAIMainBrain",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        832,
        -480
      ]
    },
    {
      "id": "723fd28e-628e-417a-826c-2fd025a8a026",
      "name": "askUserHowToHandleError",
      "type": "n8n-nodes-base.set",
      "position": [
        3632,
        -16
      ]
    },
    {
      "id": "0e4af952-6e07-4cb0-a4ff-6705ed3bbfc0",
      "name": "maxAutoErrorLimitReached",
      "type": "n8n-nodes-base.set",
      "position": [
        4560,
        -352
      ]
    },
    {
      "id": "43a23e59-31f7-40fb-b911-9e3dd9bad912",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        -448,
        -464
      ]
    },
    {
      "id": "817ec035-7af7-41dd-b9f6-f51fff4b6c1d",
      "name": "wordsForUser1",
      "type": "n8n-nodes-base.set",
      "position": [
        1872,
        32
      ]
    },
    {
      "id": "1347b643-7109-4148-86bd-89b540861b06",
      "name": "isExecutable",
      "type": "n8n-nodes-base.if",
      "position": [
        1552,
        -176
      ]
    },
    {
      "id": "73bc7418-b260-4c38-bc15-ec3ed50086df",
      "name": "getAssistantsList",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        -656,
        -464
      ]
    },
    {
      "id": "84f8b40b-c563-4b28-a70c-c4c5b7323d1b",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1264,
        32
      ],
      "parameters": {
        "width": null,
        "height": 260,
        "content": "OpenAI has built-in assistant that handles chat history on their side. For open-router we should handle chat history on our side"
      }
    },
    {
      "id": "efca8f7c-1115-4086-9b53-111574a00cee",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1472,
        -304
      ],
      "parameters": {
        "width": null,
        "height": 260,
        "content": "Sometimes we can't answer with just code. This node is responsible for separation. If it is not a code for sandbox, than it will go to user as words."
      }
    },
    {
      "id": "a793ec0d-97fc-49aa-8c2a-f3746946ed95",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3696,
        -416
      ],
      "parameters": {
        "width": null,
        "height": 260,
        "content": "Error fixing loop will work only n times, defined in this node. It is done to prevent infinite loop"
      }
    },
    {
      "id": "d12ff01c-662d-4204-a1a7-1c91b122a5cd",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        3008,
        -128
      ],
      "parameters": {
        "width": 220,
        "height": 280,
        "content": "If the user has selected automatic error fixing, debugging will be performed automatically, otherwise the system will ask the user for further instruction"
      }
    },
    {
      "id": "39a21d61-816d-4414-8f0b-d87adda9f350",
      "name": "OpenRouterAgent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1072,
        128
      ]
    },
    {
      "id": "ec49bd8d-05cd-4438-bdb9-864a92580d5e",
      "name": "createOpenAiAssistant",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        352,
        -304
      ]
    },
    {
      "id": "1caf5dec-7cbd-4cbf-9687-48d4aa33cd35",
      "name": "localVariables",
      "type": "n8n-nodes-base.set",
      "position": [
        -1456,
        128
      ]
    },
    {
      "id": "e4025036-da4e-4e6b-ad3e-97c5242aa861",
      "name": "executedSQLQuery",
      "type": "n8n-nodes-base.set",
      "position": [
        5376,
        272
      ]
    },
    {
      "id": "ff239015-892a-4762-8de6-537396e1d1c3",
      "name": "issueOnOpenAiSide",
      "type": "n8n-nodes-base.set",
      "position": [
        -240,
        -256
      ]
    },
    {
      "id": "7743696c-4e2d-4195-8832-1624f4861075",
      "name": "mergeExecutionsResult",
      "type": "n8n-nodes-base.code",
      "position": [
        4816,
        352
      ]
    },
    {
      "id": "55302cce-e5ae-4d33-8200-b455e3ca795e",
      "name": "collectErrorLoopDetails",
      "type": "n8n-nodes-base.code",
      "position": [
        5072,
        224
      ]
    },
    {
      "id": "607ac294-c68c-4599-af02-a9a7fc265de9",
      "name": "setThreadId",
      "type": "n8n-nodes-base.set",
      "position": [
        1184,
        -480
      ]
    },
    {
      "id": "0db968fc-e44c-47df-a85b-97c08f7a3b20",
      "name": "collectErrorLoopDetails1",
      "type": "n8n-nodes-base.code",
      "position": [
        4208,
        -352
      ]
    },
    {
      "id": "2642435e-47db-4009-bc04-6a9014548669",
      "name": "Execute a SQL query",
      "type": "n8n-nodes-base.mySql",
      "position": [
        4304,
        608
      ]
    },
    {
      "id": "ab334954-c05c-403f-a688-aaa1824f47ea",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        3984,
        384
      ]
    }
  ],
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "isAssistantExistsCode",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "issueOnOpenAiSide",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Execute_AI_result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Execute a SQL query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IfError": {
      "main": [
        [
          {
            "node": "AutoErrorFixing",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IfOpenAI": {
      "main": [
        [
          {
            "node": "AgentName",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "OpenRouterAgent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "isOpenAI": {
      "main": [
        [
          {
            "node": "OpenAIMainBrain",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "OpenRouterAgent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AgentName": {
      "main": [
        [
          {
            "node": "getAssistantsList",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "assistant": {
      "main": [
        [
          {
            "node": "OpenAIMainBrain",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "setThreadId": {
      "main": [
        [
          {
            "node": "isExecutable",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "isExecutable": {
      "main": [
        [
          {
            "node": "setOutputByProvider",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "wordsForUser1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "OpenRouterAgent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "localVariables": {
      "main": [
        [
          {
            "node": "IfOpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AutoErrorFixing": {
      "main": [
        [
          {
            "node": "IsMaxAutoErrorReached",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "askUserHowToHandleError",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAIMainBrain": {
      "main": [
        [
          {
            "node": "setThreadId",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouterAgent": {
      "main": [
        [
          {
            "node": "isExecutable",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "executedSQLQuery": {
      "main": [
        []
      ]
    },
    "Execute_AI_result": {
      "main": [
        [
          {
            "node": "mergeExecutionsResult",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "GenerateErrorPrompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "getAssistantsList": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "isAssistantExists": {
      "main": [
        [
          {
            "node": "assistant",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "createOpenAiAssistant",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute a SQL query": {
      "main": [
        [
          {
            "node": "mergeExecutionsResult",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "GenerateErrorPrompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GenerateErrorPrompt": {
      "main": [
        [
          {
            "node": "isOpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "setOutputByProvider": {
      "main": [
        [
          {
            "node": "IfError",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IsMaxAutoErrorReached": {
      "main": [
        [
          {
            "node": "collectErrorLoopDetails1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "OpenRouterAgent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "createOpenAiAssistant": {
      "main": [
        [
          {
            "node": "assistant",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "isAssistantExistsCode": {
      "main": [
        [
          {
            "node": "isAssistantExists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "mergeExecutionsResult": {
      "main": [
        [
          {
            "node": "collectErrorLoopDetails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "collectErrorLoopDetails": {
      "main": [
        [
          {
            "node": "executedSQLQuery",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "collectErrorLoopDetails1": {
      "main": [
        [
          {
            "node": "maxAutoErrorLimitReached",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "maxAutoErrorLimitReached": {
      "main": [
        []
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "localVariables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}