{
  "name": "Gmail Task Extractor",
  "nodes": [
    {
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute",
              "minute": 5
            }
          ]
        }
      },
      "id": "gmail-trigger",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "typeVersion": 1,
      "position": [
        250,
        300
      ],
      "credentials": {
        "gmailOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "get",
        "messageId": "={{$json[\"id\"]}}",
        "options": {
          "format": "full"
        }
      },
      "id": "get-email-details",
      "name": "Get Email Details",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2,
      "position": [
        450,
        300
      ],
      "credentials": {
        "gmailOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "chat",
        "operation": "message",
        "modelId": "gpt-4",
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "You are an expert at extracting actionable tasks from emails. Return ONLY a JSON array of tasks."
            },
            {
              "role": "user",
              "content": "Extract tasks from this email:\\n\\nFrom: {{$json[\"payload\"][\"headers\"].find(h => h.name === 'From').value}}\\nSubject: {{$json[\"payload\"][\"headers\"].find(h => h.name === 'Subject').value}}\\nBody: {{$json[\"snippet\"]}}\\n\\nFor each task return:\\n{\\n  \"title\": \"Clear task description\",\\n  \"description\": \"Additional context\",\\n  \"priority\": 1-5,\\n  \"dueDate\": \"YYYY-MM-DD or null\",\\n  \"context\": \"Work/Personal/Family/Learning\"\\n}\\n\\nReturn empty array [] if no tasks found."
            }
          ]
        },
        "options": {
          "temperature": 0.1
        }
      },
      "id": "extract-tasks",
      "name": "Extract Tasks with OpenAI",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1,
      "position": [
        650,
        300
      ],
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "functionCode": "const tasksJson = $input.first().json.message.content;\nlet tasks = [];\n\ntry {\n  tasks = JSON.parse(tasksJson);\n} catch (e) {\n  // Try to extract JSON from response\n  const match = tasksJson.match(/\\[.*\\]/s);\n  if (match) {\n    tasks = JSON.parse(match[0]);\n  }\n}\n\n// Add email metadata to each task\nconst emailData = $input.all()[0].json;\nconst emailId = emailData.id;\nconst subject = emailData.payload.headers.find(h => h.name === 'Subject')?.value || '';\n\nreturn tasks.map(task => ({\n  json: {\n    ...task,\n    source: 'email',\n    emailId: emailId,\n    emailSubject: subject,\n    extractedAt: new Date().toISOString()\n  }\n}));"
      },
      "id": "process-tasks",
      "name": "Process Tasks",
      "type": "n8n-nodes-base.code",
      "typeVersion": 1,
      "position": [
        850,
        300
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "gmail-tasks",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "webhook",
      "name": "Send to App Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        1050,
        300
      ]
    }
  ],
  "connections": {
    "Gmail Trigger": {
      "main": [
        [
          {
            "node": "Get Email Details",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Email Details": {
      "main": [
        [
          {
            "node": "Extract Tasks with OpenAI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Tasks with OpenAI": {
      "main": [
        [
          {
            "node": "Process Tasks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Tasks": {
      "main": [
        [
          {
            "node": "Send to App Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}