{
  "name": "cos4-router-transformer",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "id": "d2bac001-0061-4ef9-bf26-2138dc1de7d6",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        400
      ],
      "webhookId": "d872eebe-64bd-4af5-ae36-2e78498bfde8"
    },
    {
      "parameters": {
        "jsCode": "const msg = $input.first().json.chatInput ?? '';\nconst sessionId = $input.first().json.sessionId ?? 'default';\nlet route = 'summarize';\nconst lower = msg.toLowerCase();\nif (lower.includes('extract') || lower.includes('key fact') || lower.includes('bullet') || lower.includes('list')) {\n  route = 'extract';\n}\nreturn [{ json: { userMessage: msg, sessionId, route } }];"
      },
      "id": "d3a4cec9-940e-4d17-992c-54ca537cd883",
      "name": "Router",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        400
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "e8493526-26da-4101-9b89-aa6cc44e4ac5",
              "leftValue": "={{ $json.route }}",
              "rightValue": "summarize",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "1bbd3735-e5c3-4752-b68b-4d1c6a29c662",
      "name": "Summarize or Extract?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        680,
        400
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "9b9d7f12-f606-4fb9-972d-57a2be62f584",
              "name": "prompt",
              "value": "={{ 'Summarize the following in 3-5 sentences. Focus on key findings.\\n\\nText: ' + $json.userMessage }}",
              "type": "string"
            },
            {
              "id": "15cd3a90-76c3-442f-9070-0e7520945ad1",
              "name": "sessionId",
              "value": "={{ $json.sessionId }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "57f591b6-6b4e-4d8c-b975-53ddd6f59560",
      "name": "Build Summarize Prompt",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        900,
        260
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "38a23de0-a60a-4d7c-aba6-4255b3a94102",
              "name": "prompt",
              "value": "={{ 'Extract the key facts from the following text as a numbered list. Each item: one clear sentence.\\n\\nText: ' + $json.userMessage }}",
              "type": "string"
            },
            {
              "id": "eedef820-b5e4-4e3b-8664-4ca7cc9c2b7b",
              "name": "sessionId",
              "value": "={{ $json.sessionId }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "69cf2e50-0cdd-4c1e-b758-3b129a1a9d87",
      "name": "Build Extract Prompt",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        900,
        540
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.prompt }}",
        "options": {
          "systemMessage": "You are a concise scientific writing assistant."
        }
      },
      "id": "37c0e44f-3ef1-4690-8d2e-0d7555ce3aa2",
      "name": "AI Summarize",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1120,
        260
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.prompt }}",
        "options": {
          "systemMessage": "You are a concise scientific writing assistant."
        }
      },
      "id": "2e601752-c773-46ab-922c-3fe58be6d823",
      "name": "AI Extract",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1120,
        540
      ]
    },
    {
      "parameters": {
        "model": "llama-3.3-70b-versatile",
        "options": {
          "temperature": 0.3
        }
      },
      "id": "ea18dca8-e71d-419f-8b9a-3c6e0a28e7a4",
      "name": "Groq (summarize)",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "typeVersion": 1,
      "position": [
        1120,
        80
      ],
      "credentials": {
        "groqApi": {
          "id": "groq-shared-cred",
          "name": "Groq (shared, students)"
        }
      }
    },
    {
      "parameters": {
        "model": "llama-3.3-70b-versatile",
        "options": {
          "temperature": 0.3
        }
      },
      "id": "01b3f102-a449-4bae-a764-7090de8f7709",
      "name": "Groq (extract)",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "typeVersion": 1,
      "position": [
        1120,
        720
      ],
      "credentials": {
        "groqApi": {
          "id": "groq-shared-cred",
          "name": "Groq (shared, students)"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('When chat message received').first().json.sessionId }}",
        "contextWindowLength": 6
      },
      "id": "589fe1e2-96f7-46fc-a2ca-29eaecb66953",
      "name": "Memory (summarize)",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        1320,
        80
      ]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('When chat message received').first().json.sessionId }}",
        "contextWindowLength": 6
      },
      "id": "c3d432b3-840c-40eb-b3f9-02718df53ab4",
      "name": "Memory (extract)",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        1320,
        720
      ]
    },
    {
      "parameters": {
        "content": "## Session 04 — AI that routes and transforms\n\n**How it works:**\nThe **Router** node reads your message and routes:\n- 'extract', 'key facts', 'bullet', 'list' → **extract branch** (numbered facts)\n- Everything else → **summarize branch** (3-5 sentences)\n\n**Try it:** paste any text and say 'summarize this', then try 'extract key facts from this'\n\n**Extend it:** add a 3rd branch by duplicating the extract side and adding a new IF condition.",
        "height": 280,
        "width": 420
      },
      "id": "5d68d6a8-6b02-4295-9441-5d74fb30d7bf",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        240,
        80
      ]
    }
  ],
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "Router",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Router": {
      "main": [
        [
          {
            "node": "Summarize or Extract?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize or Extract?": {
      "main": [
        [
          {
            "node": "Build Summarize Prompt",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Extract Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Summarize Prompt": {
      "main": [
        [
          {
            "node": "AI Summarize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Extract Prompt": {
      "main": [
        [
          {
            "node": "AI Extract",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Groq (summarize)": {
      "ai_languageModel": [
        [
          {
            "node": "AI Summarize",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Groq (extract)": {
      "ai_languageModel": [
        [
          {
            "node": "AI Extract",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Memory (summarize)": {
      "ai_memory": [
        [
          {
            "node": "AI Summarize",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Memory (extract)": {
      "ai_memory": [
        [
          {
            "node": "AI Extract",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}