{
  "name": "PRAGMAS - Ingest Paper",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "pragmas/ingest-paper",
        "responseMode": "lastNode",
        "options": {}
      },
      "id": "1",
      "name": "Paper Ingest Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const body = $json.body ?? {};\nconst query = $json.query ?? {};\nconst arxivId = String(body.arxivId ?? query.arxivId ?? $json.arxivId ?? '').trim();\nif (!arxivId) {\n  throw new Error('arxivId is required');\n}\nconst rawDownload = body.downloadPdf ?? query.downloadPdf ?? $json.downloadPdf ?? false;\nconst downloadPdf = ['1', 'true', 'yes', 'on'].includes(String(rawDownload).toLowerCase());\nconst shellQuote = (value) => JSON.stringify(String(value)).replace(/[`$]/g, '\\\\$&');\nconst parts = ['python3 /workspace/scripts/analysis/ingest_arxiv_paper.py', '--vault-path', '/workspace/obsidian-vault', '--arxiv-id', arxivId];\nif (downloadPdf) {\n  parts.push('--download-pdf');\n}\nreturn [{\n  json: {\n    arxivId,\n    downloadPdf,\n    command: parts.map((part, index) => index === 0 || part === '--download-pdf' ? part : shellQuote(part)).join(' ')\n  }\n}];"
      },
      "id": "2",
      "name": "Prepare Request",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "command": "={{$json.command}}"
      },
      "id": "3",
      "name": "Create Paper Note",
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        780,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{$env.OBSIDIAN_API_BASE_URL + '/vault/search'}}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{'Bearer ' + $env.OBSIDIAN_API_TOKEN}}"
            }
          ]
        },
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "query",
              "value": "={{$('Prepare Request').item.json.arxivId}}"
            },
            {
              "name": "limit",
              "value": "3"
            }
          ]
        },
        "options": {}
      },
      "id": "4",
      "name": "Verify In Vault",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1040,
        300
      ]
    }
  ],
  "connections": {
    "Paper Ingest Webhook": {
      "main": [
        [
          {
            "node": "Prepare Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Request": {
      "main": [
        [
          {
            "node": "Create Paper Note",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Paper Note": {
      "main": [
        [
          {
            "node": "Verify In Vault",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "pragmas-ingest-paper-v2",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "pragmas-ingest-paper"
}