{
  "name": "cos6-decision-pipeline",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "id": "60ab7c31-f57f-4275-b30c-d82464dd15f5",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        240,
        400
      ],
      "webhookId": "550994a1-3175-4370-8faf-24b555384156"
    },
    {
      "parameters": {
        "jsCode": "const msg = $input.first().json.chatInput ?? '';\nconst match = msg.match(/\\b(\\d{7,8})\\b/);\nreturn [{ json: { pmid: match ? match[1] : null, hasPmid: !!match, original: msg } }];"
      },
      "id": "783a9490-e22d-40c8-acb9-c26a676e203d",
      "name": "Extract PMID",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        460,
        400
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": false,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "4d04822a-a94d-4141-98f6-d74c98e06c78",
              "leftValue": "={{ $json.hasPmid }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "1c4aff61-b5a4-405c-a8ad-faf724d67415",
      "name": "Has PMID?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        680,
        400
      ]
    },
    {
      "parameters": {
        "url": "=https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id={{ $json.pmid }}&rettype=abstract&retmode=text",
        "options": {
          "response": {
            "response": {
              "responseFormat": "text"
            }
          }
        }
      },
      "id": "103d274f-a80f-4bd4-9cb4-10208857f803",
      "name": "Fetch Paper",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        900,
        240
      ],
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "7add4ca8-5ea5-4fe8-aad5-a45bf91c5079",
              "name": "abstract",
              "value": "={{ $json.data.substring(0, 6000) }}",
              "type": "string"
            },
            {
              "id": "e0ac9a54-829d-492e-b3b8-9130f28920d4",
              "name": "pmid",
              "value": "={{ $('Extract PMID').first().json.pmid }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "9beb6b21-962a-4a22-97f3-88dfc70c882a",
      "name": "Prep for AI",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        120
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ 'Summarise paper PMID ' + $json.pmid }}",
        "options": {
          "systemMessage": "You are a research assistant. Summarise the paper from this abstract in 3-4 sentences: research question, method, key finding, limitation.\n\nABSTRACT:\n{{ $json.abstract }}"
        }
      },
      "id": "23fd0d32-1b15-4f82-a710-4799f8b802c2",
      "name": "AI Summarize",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1340,
        120
      ]
    },
    {
      "parameters": {
        "model": "llama-3.3-70b-versatile",
        "options": {
          "temperature": 0.3
        }
      },
      "id": "d28e3398-fb2d-4c31-aad9-aea8194242a8",
      "name": "Groq",
      "type": "@n8n/n8n-nodes-langchain.lmChatGroq",
      "typeVersion": 1,
      "position": [
        1340,
        300
      ],
      "credentials": {
        "groqApi": {
          "id": "groq-shared-cred",
          "name": "Groq (shared, students)"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('When chat message received').first().json.sessionId }}",
        "contextWindowLength": 8
      },
      "id": "b388e817-0c1d-4acb-9bf5-7527bdc10a2a",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        1540,
        300
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "8d425480-5494-4afb-82db-98a11cced4e0",
              "name": "response",
              "value": "={{ 'PubMed could not find PMID ' + $('Extract PMID').first().json.pmid + '. Check the ID and try again.' }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "9bd4da88-fcf6-4f32-81d4-cfe1396b3ab5",
      "name": "HTTP Error",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1120,
        380
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "dfd6692f-e4dd-401e-90d7-78b4b82fc6b4",
              "name": "response",
              "value": "No PubMed ID found. Please include a 7-8 digit PMID. Example: 'Summarise paper 38167000'",
              "type": "string"
            }
          ]
        }
      },
      "id": "efe75ebf-8c40-4845-9220-26f4ac56b5ab",
      "name": "No PMID Guide",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        900,
        580
      ]
    },
    {
      "parameters": {
        "content": "## Session 06 — Decisions and Error Handling\n\n**How it works:**\n- IF node checks if message contains a 7-8 digit PMID\n- TRUE path: fetches paper from PubMed (`Continue on error` enabled)\n  - Success output → AI summarises\n  - Error output → returns helpful message\n- FALSE path → guides user to include a PMID\n\n**Try:** 'Summarise paper 38167000'\n**Then try:** 'Summarise 99999999' (bad PMID — error path fires)\n\n**Key:** Enable 'Continue on error' on any node to catch failures without crashing the workflow.",
        "height": 260,
        "width": 420
      },
      "id": "44d8d266-f7a3-464d-a77f-b3cc5b614f1b",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        240,
        80
      ]
    }
  ],
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "Extract PMID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract PMID": {
      "main": [
        [
          {
            "node": "Has PMID?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has PMID?": {
      "main": [
        [
          {
            "node": "Fetch Paper",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No PMID Guide",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch Paper": {
      "main": [
        [
          {
            "node": "Prep for AI",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prep for AI": {
      "main": [
        [
          {
            "node": "AI Summarize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Groq": {
      "ai_languageModel": [
        [
          {
            "node": "AI Summarize",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Summarize",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}