{
  "name": "semantic_search",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "search-medicine",
        "responseMode": "lastNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2.1,
      "position": [
        0,
        0
      ],
      "id": "d03f3421-03be-4206-884f-81de23bcec1c",
      "name": "Webhook"
    },
    {
      "parameters": {
        "url": "https://byysargobxhyirurxkuy.supabase.co/rest/v1/medicines?select=*&user_id=is.null",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "{{SUPABASE_SERVICE_ROLE_KEY}}"
            },
            {
              "name": "Authorization",
              "value": "Bearer {{SUPABASE_SERVICE_ROLE_KEY}}"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "fullResponse": true
            }
          }
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        208,
        -80
      ],
      "id": "b4a2f593-abb5-42bf-99fd-ce6b0d441520",
      "name": "Get Legacy Medicines"
    },
    {
      "parameters": {
        "url": "=https://byysargobxhyirurxkuy.supabase.co/rest/v1/medicines?select=*&user_id=eq.{{ $json.body.user_id }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "apikey",
              "value": "{{SUPABASE_SERVICE_ROLE_KEY}}"
            },
            {
              "name": "Authorization",
              "value": "Bearer {{SUPABASE_SERVICE_ROLE_KEY}}"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "fullResponse": true
            }
          }
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        208,
        80
      ],
      "id": "f3c1a9d2-6b4e-4a17-9c2d-8e5f7a1b3c9d",
      "name": "Get Real Medicines"
    },
    {
      "parameters": {
        "mode": "append"
      },
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        416,
        0
      ],
      "id": "a8e2d5c7-1f3b-4d6a-9e8c-2b7f4a6d1c3e",
      "name": "Merge Legacy + Real"
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "let combined = [];\nfor (const item of $input.all()) {\n  combined = combined.concat(item.json.body || []);\n}\nreturn [{ json: { body: combined } }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        600,
        0
      ],
      "id": "5b6e8a1c-3d9f-4c2b-8a7e-1f4d6c9b2a3e",
      "name": "Flatten Search Results"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "d53152fa-e8cb-446d-b41b-b8fc7d82875d",
              "name": "searchQuery",
              "value": "={{ $('Webhook').item.json.body.query }}",
              "type": "string"
            },
            {
              "id": "0ac1f5d6-2e94-437b-ae29-4d84ca83cdf5",
              "name": "inventoryText",
              "value": "={{ JSON.stringify($json.body) }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        800,
        0
      ],
      "id": "09d9c80b-645b-4d09-a6d2-f30b8f60c828",
      "name": "Prepare Search Data"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://openrouter.ai/api/v1/chat/completions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{OPENROUTER_API_KEY}}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({\n  model: \"deepseek/deepseek-v4-flash\",\n  messages: [\n    { role: \"system\", content: \"You are a search assistant for a home medicine cabinet app. Match SEARCH QUERY to medicines in INVENTORY by meaning, not exact words. Only return medicines that exist in INVENTORY. If nothing matches, say so honestly. Reply with ONLY valid JSON: an object with one field called matches, a list of objects each with a name field and a reason field.\" },\n    { role: \"user\", content: \"SEARCH QUERY: \" + $json.searchQuery + \" INVENTORY: \" + $json.inventoryText }\n  ]\n}) }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1008,
        0
      ],
      "id": "7a30c05d-c891-4a95-b4fe-5330043598bb",
      "name": "Search Medicines by Meaning"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "3100ea0e-613a-4fc4-8755-aa8b33234ade",
              "name": "matches",
              "value": "={{ JSON.parse($json.choices[0].message.content.replace(/```json/g, '').replace(/```/g, '').trim()).matches }}",
              "type": "array"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1216,
        0
      ],
      "id": "026241cf-741f-41f9-8671-82b3e81da284",
      "name": "Prepare Search Response"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Get Legacy Medicines",
            "type": "main",
            "index": 0
          },
          {
            "node": "Get Real Medicines",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Legacy Medicines": {
      "main": [
        [
          {
            "node": "Merge Legacy + Real",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Real Medicines": {
      "main": [
        [
          {
            "node": "Merge Legacy + Real",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge Legacy + Real": {
      "main": [
        [
          {
            "node": "Flatten Search Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Flatten Search Results": {
      "main": [
        [
          {
            "node": "Prepare Search Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Search Data": {
      "main": [
        [
          {
            "node": "Search Medicines by Meaning",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Medicines by Meaning": {
      "main": [
        [
          {
            "node": "Prepare Search Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "nodeGroups": [],
  "id": "987DstgYaseuUY36",
  "tags": []
}