{
  "name": "Master Context Append",
  "nodes": [
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "748edfbf-beb6-450d-9124-c735ab2342e5",
              "leftValue": "={{ $json.name }}",
              "rightValue": "CONTEXT_PENDING",
              "operator": {
                "type": "string",
                "operation": "startsWith"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.filter",
      "typeVersion": 2.3,
      "position": [
        -48,
        0
      ],
      "id": "fd9be1d2-0da3-4357-a06f-ba0bd3d1266a",
      "name": "Filter"
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "value": "={{ $('Loop Over Items').item.json.id }}",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        544,
        0
      ],
      "id": "82e0f6bf-1c4f-49fa-84c1-5360f9798b48",
      "name": "Download file",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "text",
        "options": {}
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1.1,
      "position": [
        720,
        0
      ],
      "id": "a6409f46-cf9c-4faa-966f-8e087bdb7a64",
      "name": "Extract from File"
    },
    {
      "parameters": {
        "text": "={{ $json.data }}",
        "attributes": {
          "attributes": [
            {
              "name": "action",
              "description": "Either ADD or REPLACE, the operation the file requests",
              "required": true
            },
            {
              "name": "section",
              "type": "number",
              "description": "The section number of the master context being targeted",
              "required": true
            },
            {
              "name": "search_text",
              "description": "For REPLACE only: the exact existing text to find. Empty for ADD"
            },
            {
              "name": "new_text",
              "description": "The exact full text to insert or to replace with, verbatim from the file",
              "required": true
            },
            {
              "name": "summary",
              "description": "One line describing the change in plain words",
              "required": true
            }
          ]
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.informationExtractor",
      "typeVersion": 1.2,
      "position": [
        576,
        208
      ],
      "id": "a859919a-3278-4e60-a784-d5abfe72e120",
      "name": "Information Extractor"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "claude-haiku-4-5-20251001",
          "mode": "list",
          "cachedResultName": "Claude Haiku 4.5"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatAnthropic",
      "typeVersion": 1.5,
      "position": [
        544,
        400
      ],
      "id": "34821f80-4573-4110-a4ab-b956c7fe7363",
      "name": "Anthropic Chat Model",
      "credentials": {
        "anthropicApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "copy",
        "fileId": {
          "__rl": true,
          "value": "MASTER_CONTEXT_DOC_ID",
          "mode": "id"
        },
        "name": "=MASTER_CONTEXT_backup_{{ $now.format('yyyy-MM-dd_HHmm') }}",
        "sameFolder": false,
        "driveId": {
          "__rl": true,
          "value": "My Drive",
          "mode": "list",
          "cachedResultName": "My Drive",
          "cachedResultUrl": "https://drive.google.com/drive/my-drive"
        },
        "folderId": {
          "__rl": true,
          "mode": "id",
          "value": "BACKUPS_FOLDER_ID"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1488,
        32
      ],
      "id": "25249143-020e-41dd-8084-11f6bae1cdb2",
      "name": "Copy file",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "sendAndWait",
        "sendTo": "you@example.com",
        "subject": "=Approve master context edit: {{ $('Build Replacement').item.json.summary }}",
        "message": "=Action: {{ $('Build Replacement').item.json.action }} to Section {{ $('Build Replacement').item.json.section }}\nWill find: {{ $('Build Replacement').item.json.searchFor }}\nWill replace with: {{ $('Build Replacement').item.json.replaceWith }}",
        "approvalOptions": {
          "values": {
            "approvalType": "double"
          }
        },
        "options": {}
      },
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.2,
      "position": [
        1104,
        208
      ],
      "id": "b9df518b-c589-4ad6-bf37-43c8da2f6fb5",
      "name": "Send message and wait for response",
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "7815dffb-a64e-42ab-8dc3-90367ee74d0b",
              "leftValue": "={{ $json.data.approved }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1280,
        208
      ],
      "id": "e548fdbc-d999-4004-acf3-90ca144b0c8a",
      "name": "If"
    },
    {
      "parameters": {
        "operation": "update",
        "documentURL": "MASTER_CONTEXT_DOC_ID",
        "actionsUi": {
          "actionFields": [
            {
              "action": "replaceAll",
              "text": "={{ $('Build Replacement').item.json.searchFor }}",
              "replaceText": "={{ $('Build Replacement').item.json.replaceWith }}"
            }
          ]
        }
      },
      "type": "n8n-nodes-base.googleDocs",
      "typeVersion": 2,
      "position": [
        2112,
        32
      ],
      "id": "ded152ac-c40c-4962-9ee2-1cc9b3682d32",
      "name": "Update a document",
      "credentials": {
        "googleDocsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const e = $input.first().json.output;\nconst clean = (t) => (t ?? '').replace(/\\*\\*/g, '');\n\nlet searchFor, replaceWith;\nif (e.action === 'ADD') {\n  const anchor = `[[S${e.section}]]`;\n  searchFor = anchor;\n  replaceWith = clean(e.new_text) + '\\n\\n' + anchor;\n} else {\n  searchFor = clean(e.search_text);\n  replaceWith = clean(e.new_text);\n}\n\nreturn [{ json: { searchFor, replaceWith,\n  summary: e.summary, action: e.action, section: e.section } }];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        928,
        208
      ],
      "id": "52f7e937-d168-4f58-af85-47436af96d37",
      "name": "Build Replacement"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        192,
        -16
      ],
      "id": "c1b28014-9cd1-4197-bbfd-16a6b5f90138",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 10
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.3,
      "position": [
        -400,
        0
      ],
      "id": "3918b856-1049-47d2-8b2a-da8d2a1f5199",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "returnAll": true,
        "filter": {
          "driveId": {
            "mode": "list",
            "value": "My Drive"
          },
          "folderId": {
            "__rl": true,
            "value": "=INBOX_FOLDER_ID",
            "mode": "id"
          }
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        -208,
        0
      ],
      "id": "7fac66ab-eb7d-42de-879c-affb2c674d74",
      "name": "List Inbox",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "move",
        "fileId": {
          "__rl": true,
          "value": "={{ $json.id }}",
          "mode": "id"
        },
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "=PROCESSING_FOLDER_ID",
          "mode": "id"
        }
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        384,
        0
      ],
      "id": "3d647aa4-fc53-4b5c-8719-8a747588c784",
      "name": "Claim File",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "move",
        "fileId": {
          "__rl": true,
          "value": "={{ $('Loop Over Items').item.json.id }}",
          "mode": "id"
        },
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "=DONE_FOLDER_ID",
          "mode": "id"
        }
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        2288,
        384
      ],
      "id": "94969a46-d715-47af-9d10-0d6ab0d69ee1",
      "name": "Move to Done",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "move",
        "fileId": {
          "__rl": true,
          "value": "={{ $('Loop Over Items').item.json.id }}",
          "mode": "id"
        },
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "value": "=DECLINED_FOLDER_ID",
          "mode": "id"
        }
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1488,
        352
      ],
      "id": "872dbae6-4728-4a1c-b498-5091a2a81dda",
      "name": "Move to Declined",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "returnAll": true,
        "filter": {
          "driveId": {
            "mode": "list",
            "value": "My Drive"
          },
          "folderId": {
            "__rl": true,
            "value": "=BACKUPS_FOLDER_ID",
            "mode": "id"
          }
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1664,
        208
      ],
      "id": "46e53f34-3113-4fa1-b91d-7f42f038701a",
      "name": "List Backups",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Keep the newest N backups of the master context Doc, delete the rest.\n// Backup names end in yyyy-MM-dd_HHmm, which sorts lexicographically in\n// chronological order, so there is no date parsing to get wrong.\n\nconst KEEP = 5;\n\nconst backups = $input.all()\n  .map(item => item.json)\n  .filter(f => String(f.name || '').startsWith('MASTER_CONTEXT_backup_'));\n\nbackups.sort((a, b) => String(b.name).localeCompare(String(a.name)));\n\nreturn backups.slice(KEEP).map(f => ({ json: { id: f.id, name: f.name } }));"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1856,
        208
      ],
      "id": "df228cc9-40e6-40c5-9715-03673c335053",
      "name": "Select Backups to Delete"
    },
    {
      "parameters": {
        "operation": "deleteFile",
        "fileId": {
          "__rl": true,
          "value": "={{  $json.id }}",
          "mode": "id"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        2048,
        208
      ],
      "id": "9e44459c-d6d0-4e65-a7ee-707c132b6fc0",
      "name": "Delete Backup",
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "onError": "continueRegularOutput"
    }
  ],
  "connections": {
    "Filter": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download file": {
      "main": [
        [
          {
            "node": "Extract from File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Anthropic Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Information Extractor",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Extract from File": {
      "main": [
        [
          {
            "node": "Information Extractor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Information Extractor": {
      "main": [
        [
          {
            "node": "Build Replacement",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Copy file": {
      "main": [
        [
          {
            "node": "Update a document",
            "type": "main",
            "index": 0
          },
          {
            "node": "List Backups",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send message and wait for response": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Copy file",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Move to Declined",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update a document": {
      "main": [
        [
          {
            "node": "Move to Done",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Replacement": {
      "main": [
        [
          {
            "node": "Send message and wait for response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Claim File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "List Inbox",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List Inbox": {
      "main": [
        [
          {
            "node": "Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Claim File": {
      "main": [
        [
          {
            "node": "Download file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move to Done": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Move to Declined": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List Backups": {
      "main": [
        [
          {
            "node": "Select Backups to Delete",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Select Backups to Delete": {
      "main": [
        [
          {
            "node": "Delete Backup",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false,
    "timeSavedMode": "fixed",
    "errorWorkflow": "rfnM81t8FWtURFQy",
    "callerPolicy": "workflowsFromSameOwner"
  },
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodeGroups": [],
  "tags": []
}