{
  "name": "Luzmo IQ + Agentic Webhook",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "agentic-chat",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "a37ad10d-d1a9-413c-8e01-ff436d253393",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        1984,
        384
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "chat-input",
              "name": "chatInput",
              "value": "={{ $json.prompt ?? $json.message ?? $json.body?.prompt ?? $json.body ?? '' }}",
              "type": "string"
            },
            {
              "id": "luzmo-host",
              "name": "luzmoApiHost",
              "value": "={{ $json.luzmoApiHost ?? $json.body?.luzmoApiHost ?? '' }}",
              "type": "string"
            },
            {
              "id": "embed-key-input",
              "name": "luzmoEmbedKey",
              "value": "={{ $json.luzmoEmbedKey ?? $json.body?.luzmoEmbedKey ?? '' }}",
              "type": "string"
            },
            {
              "id": "embed-token-input",
              "name": "luzmoEmbedToken",
              "value": "={{ $json.luzmoEmbedToken ?? $json.body?.luzmoEmbedToken ?? '' }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "b209450f-37e2-41dd-9d5c-fa685092fa71",
      "name": "Prepare Chat Input",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2208,
        384
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.chatInput }}",
        "options": {
          "systemMessage": "You are a helpful assistant with access to tools. You can:\n- get_burrito_recipe: for burrito recipe requests\n- data_analysis: for questions about the user's analytics or business data (sales, metrics, dashboards, etc.)\n\nFor questions unrelated to burrito recipes or data, respond directly that you can only help with burrito recipes and data questions. Do not call any tools in that case.",
          "maxIterations": 10
        }
      },
      "id": "5fa01f8d-8714-4737-9994-dfadad295ae9",
      "name": "Tools Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.7,
      "position": [
        2496,
        384
      ]
    },
    {
      "parameters": {
        "model": "gpt-5.4-mini",
        "options": {
          "temperature": 0.2
        }
      },
      "id": "4d0188bf-9fc0-4654-817a-536d276b8ae7",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.1,
      "position": [
        2432,
        608
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "name": "get_burrito_recipe",
        "description": "Get a burrito recipe. Use when the user asks for burrito ingredients, steps, or cooking tips.",
        "jsCode": "const requestedStyle = typeof query === 'string' && query.trim() ? query.trim() : 'classic';\nreturn `Burrito recipe (${requestedStyle}):\\n\\nIngredients:\\n- 2 large flour tortillas\\n- 1 cup cooked rice\\n- 1 cup black beans\\n- 1 cup cooked protein (chicken, beef, or tofu)\\n- 1/2 cup shredded cheese\\n- 1/2 cup salsa\\n- Optional: guacamole, sour cream, lettuce\\n\\nSteps:\\n1. Warm tortillas in a pan for 20-30 seconds per side.\\n2. Layer rice, beans, protein, cheese, and salsa in the center.\\n3. Fold sides inward, roll tightly, and seam-side down.\\n4. Toast in a dry pan for 1-2 minutes per side until lightly crisp.\\n5. Serve warm with optional toppings.`;"
      },
      "id": "a31b1797-daaf-4463-a84e-31918e86126d",
      "name": "Get Burrito Recipe",
      "type": "@n8n/n8n-nodes-langchain.toolCode",
      "typeVersion": 1,
      "position": [
        2560,
        608
      ]
    },
    {
      "parameters": {
        "name": "data_analysis",
        "description": "Query analytics/business data via Luzmo. Use when the user asks about sales, data, metrics, dashboards, charts, or business intelligence.",
        "jsCode": "const prompt = typeof query === 'string' ? query : (query?.prompt || '');\nconst responseMode =\n  typeof query === 'object' && query?.response_mode ? query.response_mode : 'text_only';\nconst localeId = typeof query === 'object' && query?.locale_id ? query.locale_id : 'en';\n\nconst key = $json.luzmoEmbedKey;\nconst token = $json.luzmoEmbedToken;\nconst host = $json.luzmoApiHost;\n\nif (!key || !token || !host) {\n  return 'Error: Missing Luzmo request properties. Required: luzmoEmbedKey, luzmoEmbedToken, luzmoApiHost.';\n}\n\ntry {\n  const response = await this.helpers.httpRequest({\n    url: `${host}/0.1.0/iqmessage`,\n    method: 'POST',\n    headers: {\n      'Content-Type': 'application/json',\n    },\n    body: {\n      action: 'create',\n      version: '0.1.0',\n      key,\n      token,\n      properties: {\n        prompt,\n        response_mode: responseMode,\n        locale_id: localeId,\n      },\n    },\n    // We want raw text because Luzmo can return newline-delimited chunks\n    encoding: 'text',\n    returnFullResponse: true,\n  });\n\n  const status = response?.statusCode ?? response?.status ?? 200;\n  const rawText =\n    typeof response?.body === 'string'\n      ? response.body\n      : typeof response?.data === 'string'\n      ? response.data\n      : '';\n\n  if (status >= 400) {\n    return `Luzmo API error ${status}: ${rawText || '(empty error body)'}`;\n  }\n\n  let result = '';\n  for (const line of rawText.split('\\n')) {\n    const clean = line.trim();\n    if (!clean) continue;\n    try {\n      const evt = JSON.parse(clean);\n      if (evt.chunk != null) result += String(evt.chunk);\n    } catch (_) {\n      // Ignore malformed/non-JSON lines.\n    }\n  }\n\n  return result || '(No data returned)';\n} catch (err) {\n  const status = err?.response?.statusCode ?? err?.response?.status ?? 'unknown';\n  const body =\n    typeof err?.response?.body === 'string'\n      ? err.response.body\n      : typeof err?.message === 'string'\n      ? err.message\n      : JSON.stringify(err);\n  return `Luzmo API error ${status}: ${body}`;\n}"
      },
      "id": "6672bc96-1584-4938-83a1-bf639e2fa444",
      "name": "Query Data (Luzmo IQ)",
      "type": "@n8n/n8n-nodes-langchain.toolCode",
      "typeVersion": 1,
      "position": [
        2688,
        608
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ { \"output\": $json.output ?? $json.text ?? $json.message ?? JSON.stringify($json) } }}",
        "options": {
          "responseCode": 200,
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "id": "d4fa9736-3a80-4992-8d8f-aaa313b6756d",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        2896,
        384
      ]
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Prepare Chat Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Chat Input": {
      "main": [
        [
          {
            "node": "Tools Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tools Agent": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Tools Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Get Burrito Recipe": {
      "ai_tool": [
        [
          {
            "node": "Tools Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Query Data (Luzmo IQ)": {
      "ai_tool": [
        [
          {
            "node": "Tools Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "5c423b92-4a45-44ae-8a47-2fdc10e67972",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "tags": []
}