{
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        0,
        -96
      ],
      "id": "505a0241-76bc-48ce-ad92-a7482f4988ed",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=Please check the PDF file and write a short summarization of the content. \nIf the document contains a recipient address and a sender address, please also write it down. \n\nHere is the content:\n{{ $json.pages[0].markdown }}\n\nAvailable Tags (ID - Name):\n{{ $('Get Tags').item.json.results.map(tag => `${tag.id} - ${tag.name}`).join('\\n') }}\n\nAvailable Correspondents (ID - Name):\n{{ $('Get Correspondents').item.json.results.map(correspondent => `${correspondent.id} - ${correspondent.name}`).join('\\n') }}\n\nAvailable Document types (ID - Name):\n{{ $('Get Document Types').item.json.results.map(doc_type => `${doc_type.id} - ${doc_type.name}`).join('\\n') }}\n\nImportant: Only suggest tags and correspondents from the lists above using their IDs. Do not create new ones.\n\nPlease provide your response in the following format:\n\n**Summary:**\n[Your summary here]\n\n**Sender Address:**\n[Sender address if found, otherwise \"Not found\"]\n\n**Recipient Address:**\n[Recipient address if found, otherwise \"Not found\"]\n\n**Suggested Tag IDs:**\n[Comma-separated list of tag IDs, e.g., \"39, 42, 55\" or \"None\"]\n\n**Suggested Correspondent ID:**\n[Single correspondent ID, e.g., \"12\" or \"None\"]\n\n**Suggested Document Type ID:**\n[Single type ID, e.g., \"12\" or \"None\", please take care about words in content matching a document type name]\n\n\n**Document language:**\n[2 chars language code (f)]",
        "hasOutputParser": true,
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2.2,
      "position": [
        624,
        0
      ],
      "id": "4ce5c493-c740-4c72-be85-81426c0520f4",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.mistralAi",
      "typeVersion": 1,
      "position": [
        480,
        0
      ],
      "id": "3a828e52-08fe-477f-ad4f-8ffcae180c1c",
      "name": "Extract text",
      "credentials": {
        "mistralCloudApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-5.2",
          "mode": "list",
          "cachedResultName": "gpt-5.2"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        624,
        208
      ],
      "id": "46176422-2681-47c5-aaa9-e79dd024fa9d",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsonSchemaExample": "{\n   \"sender\": {\n     \"name\": \"SENDER_NAME\",\n     \"address\": \"SENDER_STREET\",\n     \"postcode\": \"SENDER_POSTCODE\",\n     \"city\": \"SENDER_CITY\",\n     \"country\": \"SENDER_COUNTRY\"\n   },\n   \"correspondent\": {\n     \"id\": \"CORRESPONDENT_ID\",\n     \"name\": \"CORRESPONENT_NAME\"\n   },\n   \"recipient\": {\n     \"name\": \"RECIPIENT_NAME\",\n     \"address\": \"RECIPIENT_STREET\",\n     \"postcode\": \"RECIPIENT_POSTCODE\",\n     \"city\": \"RECIPIENT_CITY\",\n     \"country\": \"RECIPIENT_COUNTRY\"\n   },\n   \"language\": \"DOCUMENT_LANG_CODE_(2chars)\",\n   \"subject\": \"SUBJECT_TEXT\",\n   \"summary\": \"SUMMARY_TEXT\",\n   \"priority\": \"PRIORITY\",\n    \"tags\": [\n      {\n        \"id\": \"TAG_ID\",\n        \"name\": \"TAG_NAME\"\n      }\n    ],\n    \"document_type\": {\n      \"id\": \"DOCUMENT_TYPE_ID\",\n      \"name\": \"DOCUMENT_TYPE_NAME\"\n    }\n}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.3,
      "position": [
        768,
        208
      ],
      "id": "43ba17d3-7b5d-407e-b5c4-49329cadf5c2",
      "name": "Structured Output Parser"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -928,
        -256
      ],
      "id": "923277bf-6046-4496-bf1b-02f56ffeeba0",
      "name": "When clicking \u2018Execute workflow\u2019"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "3bd9ab06-36c1-494f-a25e-031a6a74ca7e",
              "leftValue": "={{ $json.statusCode }}",
              "rightValue": "200",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        2048,
        -16
      ],
      "id": "6c3343ff-b536-4ccb-927d-c8e172914cc6",
      "name": "Upload erfolgreich"
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -928,
        -96
      ],
      "id": "a5c38966-7d10-44ef-8ab1-47b72676f0ad",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "url": "https://paperless.svenwagener.net/api/correspondents/",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Token 0867ad8a038b5677e194831f82a3124aab6ecaa3"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -704,
        -96
      ],
      "id": "e1a9ea26-4a78-4811-95b7-154750aa2555",
      "name": "Get Correspondents",
      "notesInFlow": false
    },
    {
      "parameters": {
        "url": "https://paperless.svenwagener.net/api/tags/?page_size=1000",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Token 0867ad8a038b5677e194831f82a3124aab6ecaa3"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -528,
        -96
      ],
      "id": "41835f32-58c6-443f-a06e-91c2e423b100",
      "name": "Get Tags",
      "notesInFlow": false
    },
    {
      "parameters": {
        "url": "https://paperless.svenwagener.net/api/document_types/?page_size=1000",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Token 0867ad8a038b5677e194831f82a3124aab6ecaa3"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -352,
        -96
      ],
      "id": "b1e3b7cb-9df3-475a-9c04-fc46ac890d79",
      "name": "Get Document Types",
      "notesInFlow": false
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "1",
              "leftValue": "={{ $json.output.correspondent?.id }}",
              "rightValue": "None",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              }
            },
            {
              "id": "2",
              "leftValue": "={{ $json.output.correspondent?.id }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        928,
        0
      ],
      "id": "d841cfe8-4500-40c5-a75d-f1ed6ed2e8b6",
      "name": "Hat g\u00fcltigen Korrespondent?"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "1",
              "leftValue": "={{ $json.output.sender?.name }}",
              "rightValue": "Not found",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              }
            },
            {
              "id": "2",
              "leftValue": "={{ $json.output.sender?.name }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1136,
        -192
      ],
      "id": "083de34d-3b19-4690-baa7-707a0fd55229",
      "name": "Kann Korrespondent erstellen?"
    },
    {
      "parameters": {
        "jsCode": "// Vorbereitung: Erstelle die Body-Parameter dynamisch\nconst output = $input.item.json.output;\n\n// Tags als Array von IDs vorbereiten\nconst tagIds = output.tags && Array.isArray(output.tags) \n  ? output.tags.map(tag => tag.id) \n  : [];\n\n// WICHTIG: Binary-Daten weitergeben!\nreturn {\n  json: {\n    ...output,\n    tagIds: tagIds  // Tags als separates Array\n  },\n  binary: $input.item.binary\n};"
      },
      "id": "f0d9ccce-c35e-4e85-a560-9a3fd2fb8c44",
      "name": "Prepare Parameters",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1696,
        -16
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://paperless.svenwagener.net/api/documents/post_document/",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Token 0867ad8a038b5677e194831f82a3124aab6ecaa3"
            }
          ]
        },
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "parameterType": "formBinaryData",
              "name": "document",
              "inputDataFieldName": "data"
            },
            {
              "name": "title",
              "value": "={{ $json.subject || '' }}"
            },
            {
              "name": "={{ $json.correspondent?.id != 'None' ? 'correspondent' : 'x' }}",
              "value": "={{ $json.correspondent?.id || '' }}"
            },
            {
              "name": "={{ $json.document_type?.id ? 'document_type' : 'x' }}",
              "value": "={{ $json.document_type?.id || '' }}"
            },
            {
              "name": "={{ $json.tags && $json.tags[0]?.id ? 'tags' : 'x' }}",
              "value": "={{ $json.tags && $json.tags[0]?.id ? $json.tags[0].id : '' }}"
            },
            {
              "name": "={{ $json.tags && $json.tags[1]?.id ? 'tags' : 'x' }}",
              "value": "={{ $json.tags && $json.tags[1]?.id ? $json.tags[1].id : '' }}"
            },
            {
              "name": "={{ $json.tags && $json.tags[2]?.id ? 'tags' : 'x' }}",
              "value": "={{ $json.tags && $json.tags[2]?.id ? $json.tags[2].id : '' }}"
            },
            {
              "name": "={{ $json.tags && $json.tags[3]?.id ? 'tags' : 'x' }}",
              "value": "={{ $json.tags && $json.tags[3]?.id ? $json.tags[3].id : '' }}"
            },
            {
              "name": "={{ $json.tags && $json.tags[4]?.id ? 'tags' : 'x' }}",
              "value": "={{ $json.tags && $json.tags[4]?.id ? $json.tags[4].id : '' }}"
            }
          ]
        },
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "responseFormat": "text"
            }
          }
        }
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1872,
        -16
      ],
      "id": "fa5ddb4c-8498-4743-8313-567cfd007d58",
      "name": "Upload to Paperless"
    },
    {
      "parameters": {
        "resource": "folder",
        "operation": "list",
        "path": "/Ablage"
      },
      "type": "n8n-nodes-base.nextCloud",
      "typeVersion": 1,
      "position": [
        -176,
        -96
      ],
      "id": "8a2ac54d-d56e-4879-bacc-8b147cfd1607",
      "name": "List a folder",
      "credentials": {
        "nextCloudApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "download",
        "path": "={{ decodeURIComponent($json.path) }}"
      },
      "type": "n8n-nodes-base.nextCloud",
      "typeVersion": 1,
      "position": [
        336,
        0
      ],
      "id": "3d46634b-c0e0-4c41-9959-d0347dd59b96",
      "name": "Download a file",
      "credentials": {
        "nextCloudApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "download",
        "path": "={{ decodeURIComponent($('Download a file').item.json.path) }}"
      },
      "type": "n8n-nodes-base.nextCloud",
      "typeVersion": 1,
      "position": [
        1552,
        -16
      ],
      "id": "5e8ca540-e94f-4436-8b07-ae6e50ee515e",
      "name": "Download a file1",
      "credentials": {
        "nextCloudApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "move",
        "path": "={{ decodeURIComponent($('Loop Over Items').item.json.path) }}",
        "toPath": "=Ablage/Fertig/{{ decodeURIComponent($('Loop Over Items').item.json.path.split('/').pop()) }}"
      },
      "type": "n8n-nodes-base.nextCloud",
      "typeVersion": 1,
      "position": [
        2352,
        144
      ],
      "id": "6324e5b0-8b89-425b-86f0-eb6481312885",
      "name": "Move a file",
      "credentials": {
        "nextCloudApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 3
          },
          "conditions": [
            {
              "id": "c3241c46-6e8b-4517-83c1-e24fead9b1d7",
              "leftValue": "={{ $json.type }}",
              "rightValue": "file",
              "operator": {
                "type": "string",
                "operation": "equals",
                "name": "filter.operator.equals"
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        160,
        -96
      ],
      "id": "ab8294ec-7ee5-4a2c-98e6-3d84136770aa",
      "name": "If"
    }
  ],
  "connections": {
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Hat g\u00fcltigen Korrespondent?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract text": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Get Correspondents",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload erfolgreich": {
      "main": [
        [
          {
            "node": "Move a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Get Correspondents",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Correspondents": {
      "main": [
        [
          {
            "node": "Get Tags",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Tags": {
      "main": [
        [
          {
            "node": "Get Document Types",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Document Types": {
      "main": [
        [
          {
            "node": "List a folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Hat g\u00fcltigen Korrespondent?": {
      "main": [
        [
          {
            "node": "Download a file1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Kann Korrespondent erstellen?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Kann Korrespondent erstellen?": {
      "main": [
        [],
        [
          {
            "node": "Download a file1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Parameters": {
      "main": [
        [
          {
            "node": "Upload to Paperless",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload to Paperless": {
      "main": [
        [
          {
            "node": "Upload erfolgreich",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List a folder": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download a file": {
      "main": [
        [
          {
            "node": "Extract text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download a file1": {
      "main": [
        [
          {
            "node": "Prepare Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move a file": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Download a file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "meta": {
    "templateCredsSetupCompleted": true
  }
}