{
  "name": "n8n Automation for Content Creation With a Brief Pipeline",
  "nodes": [
    {
      "id": "content-brief-form",
      "name": "Content Brief Form",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.2,
      "position": [
        0,
        300
      ],
      "parameters": {
        "formTitle": "Content Brief Pipeline",
        "formDescription": "Give us a topic and we will research, draft, critique and outline it with local AI.",
        "path": "content-brief-pipeline",
        "responseMode": "lastNode",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Topic",
              "requiredField": true
            },
            {
              "fieldLabel": "Audience",
              "requiredField": true
            },
            {
              "fieldLabel": "Goal",
              "requiredField": true
            },
            {
              "fieldLabel": "Source Material",
              "fieldType": "textarea"
            },
            {
              "fieldLabel": "Source File",
              "fieldType": "file",
              "multipleFiles": false
            },
            {
              "fieldLabel": "Approval",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Approve"
                  },
                  {
                    "option": "Hold for review"
                  }
                ]
              },
              "requiredField": true
            }
          ]
        }
      }
    },
    {
      "id": "normalize-inputs",
      "name": "Normalize Inputs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        300
      ],
      "parameters": {
        "jsCode": "const j = $input.first().json;\nconst clean = (v) => String(v ?? '').replace(/\\s+/g, ' ').trim();\nconst out = {\n  topic: clean(j['Topic']),\n  audience: clean(j['Audience']),\n  goal: clean(j['Goal']),\n  pastedSource: String(j['Source Material'] ?? '').trim(),\n  approval: clean(j['Approval']) === 'Approve' ? 'approved' : 'pending',\n  hasFile: Boolean($input.first().binary && Object.keys($input.first().binary).length),\n};\nconst item = { json: out };\nif (out.hasFile) item.binary = { data: Object.values($input.first().binary)[0] };\nreturn [item];"
      }
    },
    {
      "id": "has-source-file?",
      "name": "Has Source File?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        440,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "loose",
            "version": 2
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "c1",
              "leftValue": "={{ $json.hasFile }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ]
        }
      }
    },
    {
      "id": "tika-extract-text",
      "name": "Tika Extract Text",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "onError": "continueRegularOutput",
      "position": [
        660,
        200
      ],
      "parameters": {
        "method": "PUT",
        "url": "http://tika:9998/tika",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "text/plain"
            }
          ]
        },
        "sendBody": true,
        "contentType": "binaryData",
        "inputDataFieldName": "data",
        "options": {
          "timeout": 60000
        }
      }
    },
    {
      "id": "merge-source-text",
      "name": "Merge Source Text",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        300
      ],
      "parameters": {
        "jsCode": "const base = $('Normalize Inputs').first().json;\nlet fileText = '';\nconst t = $input.first().json;\nif (base.hasFile) fileText = String(t.data ?? t.body ?? '').trim();\nconst parts = [base.pastedSource, fileText].filter(Boolean);\nconst source = parts.join('\\n\\n---\\n\\n').slice(0, 12000);\nreturn [{ json: { ...base, fileText, source } }];"
      }
    },
    {
      "id": "create-content-brief",
      "name": "Create Content Brief",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        1100,
        300
      ],
      "parameters": {
        "promptType": "define",
        "text": "=You are a content strategist. Write a structured content brief in Markdown.\nTopic: {{ $json.topic }}\nAudience: {{ $json.audience }}\nGoal: {{ $json.goal }}\nSource material:\n{{ $json.source || '(none provided)' }}\n\nInclude: Working title, Target reader, Key message, 4-6 key points to cover, Tone, and 3 search questions worth researching. Keep it under 300 words."
      }
    },
    {
      "id": "ollama-model---brief",
      "name": "Ollama Model - Brief",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        1100,
        500
      ],
      "parameters": {
        "model": "gemma4:12b-it-qat",
        "options": {
          "numCtx": 8192,
          "temperature": 0.4
        }
      }
    },
    {
      "id": "searxng-search",
      "name": "SearXNG Search",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        300
      ],
      "parameters": {
        "method": "GET",
        "url": "http://searxng:8080/search",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "q",
              "value": "={{ $('Merge Source Text').first().json.topic + ' ' + $('Merge Source Text').first().json.audience }}"
            },
            {
              "name": "format",
              "value": "json"
            }
          ]
        },
        "options": {
          "timeout": 30000
        }
      }
    },
    {
      "id": "format-references",
      "name": "Format References",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1540,
        300
      ],
      "parameters": {
        "jsCode": "const res = ($input.first().json.results || []).slice(0, 6);\nconst seen = new Set();\nconst refs = [];\nfor (const r of res) {\n  if (!r.url || seen.has(r.url)) continue;\n  seen.add(r.url);\n  refs.push({ title: r.title, url: r.url, snippet: String(r.content || '').slice(0, 220) });\n}\nconst text = refs.map((r, i) => `${i + 1}. ${r.title} (${r.url}) - ${r.snippet}`).join('\\n');\nreturn [{ json: { ...$('Merge Source Text').first().json, brief: $('Create Content Brief').first().json.text, references: refs, referencesText: text || '(no results)' } }];"
      }
    },
    {
      "id": "generate-draft",
      "name": "Generate Draft",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        1760,
        300
      ],
      "parameters": {
        "promptType": "define",
        "text": "=Write a first draft of the piece described in this brief. Use the source material and the references where they help. Do not invent quotes or facts; name a reference by its number when you rely on it.\n\nBRIEF:\n{{ $json.brief }}\n\nSOURCE MATERIAL:\n{{ $json.source || '(none)' }}\n\nREFERENCES:\n{{ $json.referencesText }}\n\nWrite about 400 words in Markdown with headings."
      }
    },
    {
      "id": "ollama-model---draft",
      "name": "Ollama Model - Draft",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        1760,
        500
      ],
      "parameters": {
        "model": "gemma4:12b-it-qat",
        "options": {
          "numCtx": 8192,
          "temperature": 0.4
        }
      }
    },
    {
      "id": "critique-and-revision-plan",
      "name": "Critique and Revision Plan",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        1980,
        300
      ],
      "parameters": {
        "promptType": "define",
        "text": "=You are a tough but fair editor. Critique this draft against its goal ({{ $('Format References').first().json.goal }}) for this audience ({{ $('Format References').first().json.audience }}).\n\nDRAFT:\n{{ $json.text }}\n\nReply in Markdown with two sections: \"## Critique\" (3-5 specific problems) and \"## Revision Plan\" (a numbered checklist of concrete fixes)."
      }
    },
    {
      "id": "ollama-model---critique",
      "name": "Ollama Model - Critique",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        1980,
        500
      ],
      "parameters": {
        "model": "gemma4:12b-it-qat",
        "options": {
          "numCtx": 8192,
          "temperature": 0.4
        }
      }
    },
    {
      "id": "publish-ready-outline",
      "name": "Publish-Ready Outline",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "typeVersion": 1.5,
      "position": [
        2200,
        300
      ],
      "parameters": {
        "promptType": "define",
        "text": "=Turn the draft and the editor's revision plan into a final publish-ready outline in Markdown. Apply every fix from the revision plan. Include a title, a one-line hook, section headings with 1-2 bullets each, and a short \"Sources\" list taken only from these references:\n{{ $('Format References').first().json.referencesText }}\n\nDRAFT:\n{{ $('Generate Draft').first().json.text }}\n\nREVISION PLAN:\n{{ $json.text }}"
      }
    },
    {
      "id": "ollama-model---outline",
      "name": "Ollama Model - Outline",
      "type": "@n8n/n8n-nodes-langchain.lmChatOllama",
      "typeVersion": 1,
      "position": [
        2200,
        500
      ],
      "parameters": {
        "model": "gemma4:12b-it-qat",
        "options": {
          "numCtx": 8192,
          "temperature": 0.4
        }
      }
    },
    {
      "id": "assemble-record",
      "name": "Assemble Record",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2420,
        300
      ],
      "parameters": {
        "jsCode": "const b = $('Format References').first().json;\nconst outline = $('Publish-Ready Outline').first().json.text;\nconst critique = $('Critique and Revision Plan').first().json.text;\nconst doc = `# Approved Outline\\n\\n${outline}\\n\\n---\\n\\n# Critique and Revision Plan\\n\\n${critique}`;\nreturn [{ json: {\n  topic: b.topic, audience: b.audience, goal: b.goal, source_material: b.source,\n  brief: b.brief, draft: $('Generate Draft').first().json.text, critique,\n  outline, final_document: doc, status: b.approval,\n} }];"
      }
    },
    {
      "id": "create-table",
      "name": "Create Table",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        2640,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "CREATE TABLE IF NOT EXISTS content_pipeline (id serial PRIMARY KEY, topic text, audience text, goal text, source_material text, brief text, draft text, critique text, outline text, final_document text, status text, created_at timestamptz DEFAULT now())",
        "options": {}
      }
    },
    {
      "id": "save-pipeline-record",
      "name": "Save Pipeline Record",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.5,
      "position": [
        2860,
        300
      ],
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO content_pipeline (topic, audience, goal, source_material, brief, draft, critique, outline, final_document, status) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING id, status",
        "options": {
          "queryReplacement": "={{ (() => { const r = $('Assemble Record').first().json; return [r.topic, r.audience, r.goal, r.source_material, r.brief, r.draft, r.critique, r.outline, r.final_document, r.status]; })() }}"
        }
      }
    },
    {
      "id": "approved?",
      "name": "Approved?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        3080,
        300
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict",
            "version": 2
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "c2",
              "leftValue": "={{ $json.status }}",
              "rightValue": "approved",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ]
        }
      }
    },
    {
      "id": "publish-ready-result",
      "name": "Publish-Ready Result",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        3300,
        200
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "a1",
              "name": "status",
              "value": "approved",
              "type": "string"
            },
            {
              "id": "a2",
              "name": "record_id",
              "value": "={{ $('Save Pipeline Record').first().json.id }}",
              "type": "number"
            },
            {
              "id": "a3",
              "name": "message",
              "value": "=Outline stored in content_pipeline row {{ $('Save Pipeline Record').first().json.id }}",
              "type": "string"
            },
            {
              "id": "a4",
              "name": "document",
              "value": "={{ $('Assemble Record').first().json.final_document }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "needs-approval-result",
      "name": "Needs Approval Result",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        3300,
        420
      ],
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "b1",
              "name": "status",
              "value": "pending",
              "type": "string"
            },
            {
              "id": "b2",
              "name": "record_id",
              "value": "={{ $('Save Pipeline Record').first().json.id }}",
              "type": "number"
            },
            {
              "id": "b3",
              "name": "message",
              "value": "=Held for review. Set status to approved on content_pipeline row {{ $('Save Pipeline Record').first().json.id }} to release it.",
              "type": "string"
            }
          ]
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "Ollama Model - Brief": {
      "ai_languageModel": [
        [
          {
            "node": "Create Content Brief",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Model - Draft": {
      "ai_languageModel": [
        [
          {
            "node": "Generate Draft",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Model - Critique": {
      "ai_languageModel": [
        [
          {
            "node": "Critique and Revision Plan",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Ollama Model - Outline": {
      "ai_languageModel": [
        [
          {
            "node": "Publish-Ready Outline",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Content Brief Form": {
      "main": [
        [
          {
            "node": "Normalize Inputs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize Inputs": {
      "main": [
        [
          {
            "node": "Has Source File?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Source File?": {
      "main": [
        [
          {
            "node": "Tika Extract Text",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Merge Source Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tika Extract Text": {
      "main": [
        [
          {
            "node": "Merge Source Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Source Text": {
      "main": [
        [
          {
            "node": "Create Content Brief",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Content Brief": {
      "main": [
        [
          {
            "node": "SearXNG Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SearXNG Search": {
      "main": [
        [
          {
            "node": "Format References",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format References": {
      "main": [
        [
          {
            "node": "Generate Draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Draft": {
      "main": [
        [
          {
            "node": "Critique and Revision Plan",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Critique and Revision Plan": {
      "main": [
        [
          {
            "node": "Publish-Ready Outline",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Publish-Ready Outline": {
      "main": [
        [
          {
            "node": "Assemble Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Assemble Record": {
      "main": [
        [
          {
            "node": "Create Table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Table": {
      "main": [
        [
          {
            "node": "Save Pipeline Record",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Pipeline Record": {
      "main": [
        [
          {
            "node": "Approved?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Approved?": {
      "main": [
        [
          {
            "node": "Publish-Ready Result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Needs Approval Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "availableInMCP": true
  }
}