{
  "nodes": [
    {
      "id": "e2dcc81c-253a-47d0-8329-09bf0907747a",
      "name": "WhatsApp Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -1200,
        -200
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "whatsapp-incoming",
        "responseMode": "lastNode",
        "options": {}
      },
      "webhookId": "f75b2e79-c099-4806-90e0-2c741f23d5f3"
    },
    {
      "id": "218fee2f-e13e-4f14-8319-1adc319790c8",
      "name": "Website Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -1200,
        0
      ],
      "parameters": {
        "httpMethod": "POST",
        "path": "flowramo-chatbot",
        "responseMode": "responseNode",
        "options": {}
      },
      "webhookId": "6da7ca75-3de1-4736-87d6-805cae98e81b"
    },
    {
      "id": "f1fcf696-3292-4ead-9674-9fe25d2d4b04",
      "name": "n8n Chat Trigger",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        -1200,
        -400
      ],
      "parameters": {
        "options": {}
      },
      "webhookId": "ac7cfc5f-72f9-45d9-ad8a-392b3c3a4358"
    },
    {
      "id": "94d1d458-6da1-42c3-987c-4be2d23cd867",
      "name": "Normalize WhatsApp",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -960,
        -200
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "0cd420b6-2a25-4541-8217-0aac5742b2e7",
              "name": "body",
              "type": "object",
              "value": "={{ { phone: ($json.body?.data?.key?.remoteJid || \"\").replace(\"@s.whatsapp.net\",\"\").replace(\"@c.us\",\"\"), message: ($json.body?.data?.message?.conversation || $json.body?.data?.message?.extendedTextMessage?.text || \"\"), messageType: ($json.body?.data?.message?.audioMessage ? \"audio\" : $json.body?.data?.message?.imageMessage ? \"image\" : \"text\"), source: \"whatsapp\", mediaUrl: ($json.body?.data?.message?.audioMessage?.url || $json.body?.data?.message?.imageMessage?.url || null), mimeType: ($json.body?.data?.message?.audioMessage?.mimetype || $json.body?.data?.message?.imageMessage?.mimetype || null) } }}"
            }
          ]
        }
      }
    },
    {
      "id": "e30fd73c-f328-4b65-a9ec-0db8e2c7c57f",
      "name": "Message Router",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        -720,
        -200
      ],
      "parameters": {
        "mode": "rules",
        "rules": {
          "rules": [
            {
              "outputKey": "0",
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "loose"
                },
                "conditions": [
                  {
                    "id": "5c6a6dcc-dd76-409f-b29a-97b1f25ddc6e",
                    "leftValue": "={{ $json.body.messageType }}",
                    "rightValue": "text",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "outputKey": "1",
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "loose"
                },
                "conditions": [
                  {
                    "id": "ba32426b-339e-4f8c-88b3-cfc00a5856ff",
                    "leftValue": "={{ $json.body.messageType }}",
                    "rightValue": "audio",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            },
            {
              "outputKey": "2",
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "loose"
                },
                "conditions": [
                  {
                    "id": "c518d1f0-26fb-4f09-8b48-846822832d0b",
                    "leftValue": "={{ $json.body.messageType }}",
                    "rightValue": "image",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            }
          ]
        },
        "fallbackOutput": "extra"
      }
    },
    {
      "id": "4e59460e-f152-451c-b0a7-28b89c2e2ac0",
      "name": "Download Audio",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -480,
        200
      ],
      "parameters": {
        "method": "GET",
        "url": "={{ $json.body.mediaUrl }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "flowramo-evolution-key-2026"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      }
    },
    {
      "id": "ae0cbc22-273c-44bb-bbc1-d505846b84f2",
      "name": "Whisper Transcribe",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -240,
        200
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/audio/transcriptions",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "name": "model",
              "value": "whisper-1"
            },
            {
              "name": "file",
              "parameterType": "formBinaryData",
              "inputDataFieldName": "data"
            },
            {
              "name": "response_format",
              "value": "json"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "2c7ff0b0-4ee5-47b9-8a0b-30e216eb4090",
      "name": "Set Audio Input",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        0,
        200
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "1e83deeb-7378-431f-88c2-a7470806275e",
              "name": "body",
              "type": "object",
              "value": "={{ { message: $json.text, messageType: \"audio\", phone: $(\"Normalize WhatsApp\").first().json.body.phone, source: \"whatsapp\", mediaUrl: $(\"Normalize WhatsApp\").first().json.body.mediaUrl, mimeType: $(\"Normalize WhatsApp\").first().json.body.mimeType } }}"
            }
          ]
        }
      }
    },
    {
      "id": "09268d23-cd6b-46a2-81cc-e9a64dc790b9",
      "name": "Vision Describe",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -480,
        400
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/chat/completions",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "REDACTED",
        "options": {}
      }
    },
    {
      "id": "05de036d-3887-479f-9cc1-8187160b9cf3",
      "name": "Set Vision Input",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -240,
        400
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "f56e2a57-02b0-4ff7-beaa-978b987927ae",
              "name": "body",
              "type": "object",
              "value": "={{ { message: $json.choices[0].message.content, messageType: \"image\", phone: $(\"Normalize WhatsApp\").first().json.body.phone, source: \"whatsapp\", mediaUrl: $(\"Normalize WhatsApp\").first().json.body.mediaUrl, mimeType: $(\"Normalize WhatsApp\").first().json.body.mimeType } }}"
            }
          ]
        }
      }
    },
    {
      "id": "cd3708a3-402d-4512-9552-5b884e99baba",
      "name": "Prepare Input",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        240,
        0
      ],
      "parameters": {
        "mode": "manual",
        "duplicateItem": false,
        "assignments": {
          "assignments": [
            {
              "id": "81231f9d-cda6-4c5e-939e-f06a15bce8c4",
              "name": "finalChatInput",
              "type": "string",
              "value": "={{ $json.body?.message || $json.body?.chatInput || $json.chatInput || \"\" }}"
            },
            {
              "id": "60b2d211-0e28-47e3-8e52-5c19aa30e5cb",
              "name": "finalPhone",
              "type": "string",
              "value": "={{ $json.body?.phone || \"\" }}"
            },
            {
              "id": "9e41e5e3-9cc9-49d2-941c-0a3eabd1c0a4",
              "name": "finalSessionId",
              "type": "string",
              "value": "={{ $json.body?.phone ? $json.body.phone + \"_\" + ($json.body?.source || \"whatsapp\") : $json.sessionId || $execution.id }}"
            },
            {
              "id": "57089ff3-a2ac-4d8e-8d4e-0790a7e28ed4",
              "name": "finalSource",
              "type": "string",
              "value": "={{ $json.body?.source || \"chatbot\" }}"
            },
            {
              "id": "fdebbf78-f8e3-46ac-9eba-e51ecb423543",
              "name": "finalMessageType",
              "type": "string",
              "value": "={{ $json.body?.messageType || \"text\" }}"
            },
            {
              "id": "90c7d104-011b-4fa4-97f1-a4cc719107d0",
              "name": "finalMediaUrl",
              "type": "string",
              "value": "={{ $json.body?.mediaUrl || null }}"
            },
            {
              "id": "dcefe744-5902-4214-ba86-ab701be3819f",
              "name": "finalVisionDescription",
              "type": "string",
              "value": "={{ $json.body?.messageType === \"image\" ? $json.body?.message : null }}"
            }
          ]
        }
      }
    },
    {
      "id": "f441e414-5e8f-43c0-b5c7-7cfc46eed093",
      "name": "Orchestrator Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        480,
        0
      ],
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.finalChatInput }}",
        "options": {}
      }
    },
    {
      "id": "83b48b59-275f-4f8c-a71b-de8f474c2c9d",
      "name": "Claude Sonnet 4.5",
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "typeVersion": 1.3,
      "position": [
        480,
        -300
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "id",
          "value": "claude-sonnet-4-5"
        },
        "options": {
          "maxTokensToSample": 2048
        }
      }
    },
    {
      "id": "5d07f405-2467-41dc-afd7-cd4e5a5441cd",
      "name": "Session Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1,
      "position": [
        480,
        300
      ],
      "parameters": {
        "sessionKey": "={{ $json.finalSessionId }}",
        "contextWindowLength": 50
      }
    },
    {
      "id": "97beaaa9-1e84-41a3-9f7a-2d75da991ad4",
      "name": "EmergencyAgent",
      "type": "@n8n/n8n-nodes-langchain.agentTool",
      "typeVersion": 1,
      "position": [
        720,
        600
      ],
      "parameters": {}
    },
    {
      "id": "48bb50a2-9936-44a0-94ac-ca79d3ca2c31",
      "name": "BookingAgent",
      "type": "@n8n/n8n-nodes-langchain.agentTool",
      "typeVersion": 1,
      "position": [
        720,
        960
      ],
      "parameters": {}
    },
    {
      "id": "81b9bb2d-73ab-4d06-8b66-be1953dcab67",
      "name": "InfoAgent",
      "type": "@n8n/n8n-nodes-langchain.agentTool",
      "typeVersion": 1,
      "position": [
        720,
        1320
      ],
      "parameters": {}
    },
    {
      "id": "6354d8a5-5ec1-4048-bdd8-47069a692658",
      "name": "CRMAgent",
      "type": "@n8n/n8n-nodes-langchain.agentTool",
      "typeVersion": 1,
      "position": [
        720,
        1680
      ],
      "parameters": {}
    },
    {
      "id": "cf4417f1-091f-4c87-a2e2-247cf1bf3c6c",
      "name": "VisionAgent",
      "type": "@n8n/n8n-nodes-langchain.agentTool",
      "typeVersion": 1,
      "position": [
        720,
        2040
      ],
      "parameters": {}
    },
    {
      "id": "c779a700-1d03-4976-84db-1e56b7020409",
      "name": "CalendarTool",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        960,
        960
      ],
      "parameters": {
        "toolDescription": "Check available slots (GET), create booking (POST), reschedule (PATCH), cancel appointments (DELETE) on Google Calendar. Input: action type, date, time, patient name, service type.",
        "method": "POST",
        "url": "https://www.googleapis.com/calendar/v3/calendars/primary/events",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleCalendarOAuth2Api",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "ce4cfb10-7c74-4e3e-8f0d-65316c920d1d",
      "name": "PatientManagerTool_Emergency",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        1200,
        600
      ],
      "parameters": {
        "toolDescription": "Get or update patient by phone. Actions: GET patient data by phone, POST create new patient, PATCH update lead_score/inquiry_type/preferred_name/discount. Always call at start of conversation to get preferred_name and personalize response.",
        "method": "POST",
        "url": "https://rfoebtyreltajblsryep.supabase.co/rest/v1/patients",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            },
            {
              "name": "Prefer"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "2bc980bb-760d-46a9-9a15-13cd7ff072be",
      "name": "PatientManagerTool_Booking",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        1200,
        960
      ],
      "parameters": {
        "toolDescription": "Get or update patient by phone. Actions: GET patient data by phone, POST create new patient, PATCH update lead_score/inquiry_type/preferred_name/discount. Always call at start of conversation to get preferred_name and personalize response.",
        "method": "POST",
        "url": "https://rfoebtyreltajblsryep.supabase.co/rest/v1/patients",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            },
            {
              "name": "Prefer"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "15433e05-20f1-4b5d-b42b-4e7e0e75b691",
      "name": "PatientManagerTool_Info",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        1200,
        1320
      ],
      "parameters": {
        "toolDescription": "Get or update patient by phone. Actions: GET patient data by phone, POST create new patient, PATCH update lead_score/inquiry_type/preferred_name/discount. Always call at start of conversation to get preferred_name and personalize response.",
        "method": "POST",
        "url": "https://rfoebtyreltajblsryep.supabase.co/rest/v1/patients",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            },
            {
              "name": "Prefer"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "ee8a0b5b-c1c1-4fad-8724-0ca60c806f32",
      "name": "PatientManagerTool_CRM",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        1200,
        1680
      ],
      "parameters": {
        "toolDescription": "Get or update patient by phone. Actions: GET patient data by phone, POST create new patient, PATCH update lead_score/inquiry_type/preferred_name/discount. Always call at start of conversation to get preferred_name and personalize response.",
        "method": "POST",
        "url": "https://rfoebtyreltajblsryep.supabase.co/rest/v1/patients",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            },
            {
              "name": "Prefer"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "d5a736fb-967d-4812-b761-9a273003aac0",
      "name": "PatientManagerTool_Vision",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        1200,
        2040
      ],
      "parameters": {
        "toolDescription": "Get or update patient by phone. Actions: GET patient data by phone, POST create new patient, PATCH update lead_score/inquiry_type/preferred_name/discount. Always call at start of conversation to get preferred_name and personalize response.",
        "method": "POST",
        "url": "https://rfoebtyreltajblsryep.supabase.co/rest/v1/patients",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            },
            {
              "name": "Prefer"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "ab2d1862-8c3e-492e-81aa-07d6e16d18eb",
      "name": "EscalationTool_Emergency",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        1440,
        600
      ],
      "parameters": {
        "toolDescription": "Send urgent escalation notification to clinic staff. Use for emergencies, angry patients, complaints, billing disputes, and cancellation requests. Provide urgency level and case details.",
        "method": "POST",
        "url": "https://gmail.googleapis.com/gmail/v1/users/me/messages/send",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "gmailOAuth2",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "46cd268b-0d1a-4b88-b1f6-8a0e483dbd20",
      "name": "EscalationTool_Booking",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        1440,
        960
      ],
      "parameters": {
        "toolDescription": "Send urgent escalation notification to clinic staff. Use for emergencies, angry patients, complaints, billing disputes, and cancellation requests. Provide urgency level and case details.",
        "method": "POST",
        "url": "https://gmail.googleapis.com/gmail/v1/users/me/messages/send",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "gmailOAuth2",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "5f25c860-4267-440e-9114-674792c787b0",
      "name": "EscalationTool_Vision",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        1440,
        2040
      ],
      "parameters": {
        "toolDescription": "Send urgent escalation notification to clinic staff. Use for emergencies, angry patients, complaints, billing disputes, and cancellation requests. Provide urgency level and case details.",
        "method": "POST",
        "url": "https://gmail.googleapis.com/gmail/v1/users/me/messages/send",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "gmailOAuth2",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "bd90e4f4-4e46-4e3a-8f91-218348741a88",
      "name": "VectorStoreTool",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreSupabase",
      "typeVersion": 1,
      "position": [
        1200,
        1320
      ],
      "parameters": {
        "mode": "retrieve-as-tool",
        "toolDescription": "Search the dental clinic knowledge base for FAQs, pricing, services, procedures, doctors, hours, and clinic information. Always use for pricing questions, service differences, and clinic details. Never invent prices or clinic details.",
        "tableName": "documents",
        "topK": 5
      }
    },
    {
      "id": "81a5272b-505c-45a2-bc13-fff6c28bdde5",
      "name": "RAG Embeddings",
      "type": "@n8n/n8n-nodes-langchain.embeddingsOpenAi",
      "typeVersion": 1.2,
      "position": [
        1200,
        1500
      ],
      "parameters": {
        "options": {}
      }
    },
    {
      "id": "f1b05650-7170-4852-bebf-4d728516694c",
      "name": "Log Analytics",
      "type": "n8n-nodes-base.supabase",
      "typeVersion": 1,
      "position": [
        720,
        0
      ],
      "parameters": {
        "operation": "insert",
        "tableId": "analytics_logs",
        "fieldsUi": {
          "fieldValues": [
            {
              "fieldId": "clinic_id",
              "fieldValue": "habibs-clinic"
            },
            {
              "fieldId": "session_id",
              "fieldValue": "={{ $('Prepare Input').first().json.finalSessionId }}"
            },
            {
              "fieldId": "source",
              "fieldValue": "={{ $('Prepare Input').first().json.finalSource }}"
            },
            {
              "fieldId": "phone",
              "fieldValue": "={{ $('Prepare Input').first().json.finalPhone }}"
            },
            {
              "fieldId": "user_message",
              "fieldValue": "={{ $('Prepare Input').first().json.finalChatInput }}"
            },
            {
              "fieldId": "bot_response",
              "fieldValue": "={{ $json.output }}"
            },
            {
              "fieldId": "media_type",
              "fieldValue": "={{ $('Prepare Input').first().json.finalMessageType }}"
            }
          ]
        }
      }
    },
    {
      "id": "f2250861-5c33-4fef-9335-9a844016d1e5",
      "name": "Reply Router",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        960,
        0
      ],
      "parameters": {
        "mode": "rules",
        "rules": {
          "rules": [
            {
              "outputKey": "0",
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "loose"
                },
                "conditions": [
                  {
                    "id": "0cc68a03-81ed-4b6d-86f8-81af6bdf64ce",
                    "leftValue": "={{ $('Prepare Input').first().json.finalSource }}",
                    "rightValue": "whatsapp",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            }
          ]
        },
        "fallbackOutput": "extra"
      }
    },
    {
      "id": "12ed557d-9a8f-4a24-8d8a-fabc94feb450",
      "name": "Audio Check",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        1200,
        -200
      ],
      "parameters": {
        "mode": "rules",
        "rules": {
          "rules": [
            {
              "outputKey": "0",
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "loose"
                },
                "conditions": [
                  {
                    "id": "e14e4526-163f-4fb2-90fd-b67e5d99ca57",
                    "leftValue": "={{ $('Prepare Input').first().json.finalMessageType }}",
                    "rightValue": "audio",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              }
            }
          ]
        },
        "fallbackOutput": "extra"
      }
    },
    {
      "id": "85d9403d-17f7-4591-82b2-86f724bd76bf",
      "name": "TTS Convert",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1440,
        -400
      ],
      "parameters": {
        "method": "POST",
        "url": "https://api.openai.com/v1/audio/speech",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "openAiApi",
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "REDACTED",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        }
      }
    },
    {
      "id": "457574f7-7778-477e-af32-f75b485af1e4",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1200,
        200
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ status: \"success\", response: $(\"Orchestrator Agent\").first().json.output, session_id: $(\"Prepare Input\").first().json.finalSessionId }) }}",
        "options": {
          "responseCode": 200
        }
      }
    },
    {
      "id": "c6eed34a-cd0d-4a44-861f-c144ed6fb276",
      "name": "Error Trigger",
      "type": "n8n-nodes-base.errorTrigger",
      "typeVersion": 1,
      "position": [
        -1200,
        600
      ],
      "parameters": {}
    },
    {
      "id": "75471c8c-e348-40b4-8179-a783e5b2603d",
      "name": "DoctorQuestionsAgent",
      "type": "@n8n/n8n-nodes-langchain.agentTool",
      "typeVersion": 1,
      "position": [
        2550,
        900
      ],
      "parameters": {}
    },
    {
      "id": "c0a5d73a-0862-459e-b607-4ab2d8b0677c",
      "name": "PatientManagerTool_DoctorQ",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        2750,
        1050
      ],
      "parameters": {
        "toolDescription": "Get or update patient by phone. Actions: GET patient data by phone, POST create new patient, PATCH update lead_score/inquiry_type/preferred_name. Call at start of conversation and after every interaction.",
        "method": "POST",
        "url": "https://rfoebtyreltajblsryep.supabase.co/rest/v1/patients",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            },
            {
              "name": "Prefer"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "797288bc-3413-42d7-81f4-16f8a5d3f0f5",
      "name": "EscalationTool_DoctorQ",
      "type": "@n8n/n8n-nodes-langchain.toolHttpRequest",
      "typeVersion": 1.1,
      "position": [
        2750,
        1200
      ],
      "parameters": {
        "toolDescription": "Send notifications to clinic doctor. Use for: forwarding patient questions that need doctor expertise, delivering doctor replies back to patients. Always include patient name, phone, question code, and full message.",
        "method": "POST",
        "url": "https://gmail.googleapis.com/gmail/v1/users/me/messages/send",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "gmailOAuth2",
        "sendHeaders": true,
        "parametersHeaders": {
          "values": [
            {
              "name": "Content-Type"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED"
      }
    },
    {
      "id": "16a64f1e-d736-40fc-b387-fe0603015cd1",
      "name": "Twilio Send Text",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        3200,
        800
      ],
      "parameters": {
        "method": "POST",
        "url": "=https://api.twilio.com/2010-04-01/Accounts/{{$credentials ? $credentials.accountSid : 'ACCOUNT_SID'}}/Messages.json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "contentType": "form-urlencoded",
        "bodyParameters": {
          "parameters": [
            {
              "name": "To",
              "value": "=whatsapp:+{{ $('Prepare Input').first().json.finalPhone }}"
            },
            {
              "name": "From",
              "value": "whatsapp:+14155238886"
            },
            {
              "name": "Body",
              "value": "={{ $('Orchestrator Agent').first().json.output }}"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "d1bcd641-0436-4401-8eb6-d71d2f733687",
      "name": "Twilio Send Audio",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        3500,
        600
      ],
      "parameters": {
        "method": "POST",
        "url": "=https://api.twilio.com/2010-04-01/Accounts/{{$credentials ? $credentials.accountSid : 'ACCOUNT_SID'}}/Messages.json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "contentType": "form-urlencoded",
        "bodyParameters": {
          "parameters": [
            {
              "name": "To",
              "value": "=whatsapp:+{{ $('Prepare Input').first().json.finalPhone }}"
            },
            {
              "name": "From",
              "value": "whatsapp:+14155238886"
            },
            {
              "name": "MediaUrl",
              "value": "={{ $json.data }}"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "ef6af55d-5889-4f4a-bec9-9af93e46239d",
      "name": "Telnyx Send Text",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        3200,
        1000
      ],
      "disabled": true,
      "parameters": {
        "method": "POST",
        "url": "https://api.telnyx.com/v2/messages",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer TELNYX_API_KEY"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "REDACTED",
        "options": {}
      }
    },
    {
      "id": "7dab8f7a-1a34-4cd3-a679-529742148f52",
      "name": "Telnyx Send Audio",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        3500,
        1000
      ],
      "disabled": true,
      "parameters": {
        "method": "POST",
        "url": "https://api.telnyx.com/v2/calls",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer TELNYX_API_KEY"
            }
          ]
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "WhatsApp Webhook": {
      "main": [
        [
          {
            "node": "Normalize WhatsApp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Website Webhook": {
      "main": [
        [
          {
            "node": "Prepare Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "n8n Chat Trigger": {
      "main": [
        [
          {
            "node": "Prepare Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize WhatsApp": {
      "main": [
        [
          {
            "node": "Message Router",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message Router": {
      "main": [
        [
          {
            "node": "Prepare Input",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Download Audio",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Vision Describe",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Prepare Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Audio": {
      "main": [
        [
          {
            "node": "Whisper Transcribe",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Whisper Transcribe": {
      "main": [
        [
          {
            "node": "Set Audio Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Audio Input": {
      "main": [
        [
          {
            "node": "Prepare Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Vision Describe": {
      "main": [
        [
          {
            "node": "Set Vision Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Vision Input": {
      "main": [
        [
          {
            "node": "Prepare Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Input": {
      "main": [
        [
          {
            "node": "Orchestrator Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Orchestrator Agent": {
      "main": [
        [
          {
            "node": "Log Analytics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Log Analytics": {
      "main": [
        [
          {
            "node": "Reply Router",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reply Router": {
      "main": [
        [
          {
            "node": "Audio Check",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Audio Check": {
      "main": [
        [
          {
            "node": "TTS Convert",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Twilio Send Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "TTS Convert": {
      "main": [
        [
          {
            "node": "Twilio Send Audio",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claude Sonnet 4.5": {
      "ai_languageModel": [
        [
          {
            "node": "Orchestrator Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Session Memory": {
      "ai_memory": [
        [
          {
            "node": "Orchestrator Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "EmergencyAgent": {
      "ai_tool": [
        [
          {
            "node": "Orchestrator Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "BookingAgent": {
      "ai_tool": [
        [
          {
            "node": "Orchestrator Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "InfoAgent": {
      "ai_tool": [
        [
          {
            "node": "Orchestrator Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "CRMAgent": {
      "ai_tool": [
        [
          {
            "node": "Orchestrator Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "VisionAgent": {
      "ai_tool": [
        [
          {
            "node": "Orchestrator Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "RAG Embeddings": {
      "ai_embedding": [
        [
          {
            "node": "VectorStoreTool",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "DoctorQuestionsAgent": {
      "ai_tool": [
        [
          {
            "node": "Orchestrator Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "CalendarTool": {
      "ai_tool": [
        [
          {
            "node": "BookingAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "PatientManagerTool_Emergency": {
      "ai_tool": [
        [
          {
            "node": "EmergencyAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "PatientManagerTool_Booking": {
      "ai_tool": [
        [
          {
            "node": "BookingAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "PatientManagerTool_Info": {
      "ai_tool": [
        [
          {
            "node": "InfoAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "PatientManagerTool_CRM": {
      "ai_tool": [
        [
          {
            "node": "CRMAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "PatientManagerTool_Vision": {
      "ai_tool": [
        [
          {
            "node": "VisionAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "PatientManagerTool_DoctorQ": {
      "ai_tool": [
        [
          {
            "node": "DoctorQuestionsAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "EscalationTool_Emergency": {
      "ai_tool": [
        [
          {
            "node": "EmergencyAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "EscalationTool_Booking": {
      "ai_tool": [
        [
          {
            "node": "BookingAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "EscalationTool_Vision": {
      "ai_tool": [
        [
          {
            "node": "VisionAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "EscalationTool_DoctorQ": {
      "ai_tool": [
        [
          {
            "node": "DoctorQuestionsAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "VectorStoreTool": {
      "ai_tool": [
        [
          {
            "node": "InfoAgent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "timezone": "Asia/Riyadh",
    "errorWorkflow": "9Tu4EE6dyzWhqH48",
    "saveDataSuccessExecution": "none",
    "saveDataErrorExecution": "all",
    "saveManualExecutions": true,
    "callerPolicy": "workflowsFromSameOwner",
    "availableInMCP": 0
  }
}