{
  "name": "IDS AI - Chat with PDF Support",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "ids-ai-chat",
        "responseMode": "responseNode",
        "options": {
          "allowedOrigins": "*"
        }
      },
      "id": "webhook-trigger",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        220,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Extract data from request\nconst body = $input.first().json.body || $input.first().json;\nconst message = body.message || '';\nconst context = body.context || '';\nconst conversationId = body.conversationId || 'default';\n\n// Check for exercise patterns in message\nconst exercisePatterns = [\n  /solve\\s+this/i, /answer\\s+these/i, /complete\\s+the/i,\n  /\\d+\\s*points?/i, /\\d+\\s*marks?/i,\n  /exercise\\s*\\d/i, /problem\\s*\\d/i, /question\\s*\\d/i,\n  /homework/i, /assignment/i, /quiz/i, /exam/i\n];\n\nlet isExercise = false;\nfor (const pattern of exercisePatterns) {\n  if (pattern.test(message) || pattern.test(context)) {\n    isExercise = true;\n    break;\n  }\n}\n\n// Prepare the prompt for AI\nlet fullPrompt = message;\nif (context && context.trim().length > 0) {\n  fullPrompt = `DOCUMENT CONTENT:\\n${context.substring(0, 15000)}\\n\\n---\\n\\nUSER QUESTION: ${message}`;\n}\n\nreturn [{\n  json: {\n    message,\n    context,\n    fullPrompt,\n    conversationId,\n    isExercise\n  }\n}];"
      },
      "id": "exercise-filter",
      "name": "Process Message",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        440,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.isExercise }}",
              "value2": true
            }
          ]
        }
      },
      "id": "if-exercise",
      "name": "Is Exercise?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        660,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ response: \"I'm sorry, but I cannot help with exercises, homework, quizzes, or exams. I'm here to help you understand concepts and learn. Would you like me to explain any concepts instead?\", conversationId: $json.conversationId, isBlocked: true }) }}",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "id": "reject-exercise",
      "name": "Reject Exercise",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        880,
        180
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.fullPrompt }}",
        "options": {
          "systemMessage": "You are IDS AI, an intelligent educational assistant. Help students understand concepts, summarize documents, and explain academic topics. NEVER help with exercises, homework, quizzes, or exams - politely decline those requests."
        }
      },
      "id": "ai-agent",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.6,
      "position": [
        880,
        420
      ]
    },
    {
      "parameters": {
        "model": "llama3.2:latest",
        "options": {}
      },
      "id": "ollama-chat",
      "name": "Ollama Chat",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        780,
        620
      ]
    },
    {
      "parameters": {
        "respondWith": "allIncomingItems",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "id": "send-response",
      "name": "Send Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1100,
        420
      ]
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Process Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Message": {
      "main": [
        [
          {
            "node": "Is Exercise?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Exercise?": {
      "main": [
        [
          {
            "node": "Reject Exercise",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Chat": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}