{
  "name": "Prompt Engine 2.0 Workflow",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "generate-prompt",
        "options": {}
      },
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "query",
              "value": "={{ $json.body.query }}"
            },
            {
              "name": "style",
              "value": "={{ $json.body.style || 'Silicon Valley' }}"
            },
            {
              "name": "modelTarget",
              "value": "={{ $json.body.modelTarget || 'Claude' }}"
            },
            {
              "name": "version",
              "value": "={{ $json.body.version || 'full' }}"
            }
          ]
        },
        "options": {}
      },
      "name": "Extract Parameters",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.query }}",
              "operation": "isNotEmpty"
            }
          ]
        }
      },
      "name": "Validate Input",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    },
    {
      "parameters": {
        "filePath": "=/root/prompt-engine-2.0/system-message{{ $json.version === 'short' ? '-short' : $json.version === 'minimal' ? '-minimal' : '' }}.txt"
      },
      "name": "Load System Message",
      "type": "n8n-nodes-base.readBinaryFile",
      "typeVersion": 1,
      "position": [
        850,
        200
      ]
    },
    {
      "parameters": {
        "content": "={{ $json.data.toString('utf8') }}",
        "options": {}
      },
      "name": "Convert to Text",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1050,
        200
      ]
    },
    {
      "parameters": {
        "authentication": "headerAuth",
        "url": "https://api.anthropic.com/v1/messages",
        "options": {
          "headers": {
            "entries": [
              {
                "name": "anthropic-version",
                "value": "2023-06-01"
              }
            ]
          }
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"claude-3-5-sonnet-latest\",\n  \"max_tokens\": 4000,\n  \"temperature\": 0.2,\n  \"system\": \"{{ $node['Convert to Text'].json.content }}\",\n  \"messages\": [\n    {\n      \"role\": \"user\",\n      \"content\": \"{{ $node['Extract Parameters'].json.query }}\\n\\nParameters:\\n- Style: {{ $node['Extract Parameters'].json.style }}\\n- Target model: {{ $node['Extract Parameters'].json.modelTarget }}\\n- Output: 2 variants + comparison + recommendations\\n- Reasoning: provide brief bullet reasoning\"\n    }\n  ]\n}"
      },
      "name": "Call Anthropic API",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.1,
      "position": [
        1250,
        200
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "generatedPrompt",
              "value": "={{ $json.content[0].text }}"
            },
            {
              "name": "model",
              "value": "={{ $json.model }}"
            },
            {
              "name": "tokensInput",
              "value": "={{ $json.usage.input_tokens }}"
            },
            {
              "name": "tokensOutput",
              "value": "={{ $json.usage.output_tokens }}"
            },
            {
              "name": "stopReason",
              "value": "={{ $json.stop_reason }}"
            }
          ]
        },
        "options": {}
      },
      "name": "Format Response",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        1450,
        200
      ]
    },
    {
      "parameters": {
        "content": "={\n  \"success\": true,\n  \"data\": {\n    \"prompt\": {{ $json.generatedPrompt }},\n    \"metadata\": {\n      \"model\": {{ $json.model }},\n      \"tokens\": {\n        \"input\": {{ $json.tokensInput }},\n        \"output\": {{ $json.tokensOutput }}\n      },\n      \"stopReason\": {{ $json.stopReason }}\n    }\n  }\n}",
        "options": {}
      },
      "name": "Success Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1650,
        200
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"success\": false,\n  \"error\": \"Invalid request\",\n  \"message\": \"Query parameter is required\"\n}",
        "options": {
          "responseCode": 400
        }
      },
      "name": "Error - Missing Query",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        850,
        400
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={\n  \"success\": false,\n  \"error\": \"API Error\",\n  \"message\": \"{{ $json.message || 'Failed to generate prompt' }}\",\n  \"details\": \"{{ $json.description }}\"\n}",
        "options": {
          "responseCode": 500
        }
      },
      "name": "Error - API Failure",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        1450,
        400
      ]
    },
    {
      "parameters": {
        "operation": "append",
        "fileId": {
          "__rl": true,
          "value": "your-google-sheet-id",
          "mode": "id"
        },
        "values": {
          "value": "={{ [\n  {\n    timestamp: new Date().toISOString(),\n    query: $node['Extract Parameters'].json.query,\n    style: $node['Extract Parameters'].json.style,\n    modelTarget: $node['Extract Parameters'].json.modelTarget,\n    tokensInput: $json.tokensInput,\n    tokensOutput: $json.tokensOutput,\n    model: $json.model\n  }\n] }}"
        },
        "options": {}
      },
      "name": "Log to Google Sheets (Optional)",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4,
      "position": [
        1650,
        350
      ],
      "disabled": true,
      "notes": "Enable this node to log all requests to Google Sheets for analytics"
    },
    {
      "parameters": {
        "operation": "sendMessage",
        "chatId": "your-slack-channel-id",
        "text": "=New Prompt Generated!\n\nQuery: {{ $node['Extract Parameters'].json.query }}\nStyle: {{ $node['Extract Parameters'].json.style }}\nTokens: {{ $json.tokensOutput }}\n\nPrompt preview:\n{{ $json.generatedPrompt.substring(0, 200) }}...",
        "otherOptions": {}
      },
      "name": "Notify Slack (Optional)",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.1,
      "position": [
        1650,
        500
      ],
      "disabled": true,
      "notes": "Enable this node to get Slack notifications for each generation"
    }
  ],
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Extract Parameters",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Parameters": {
      "main": [
        [
          {
            "node": "Validate Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate Input": {
      "main": [
        [
          {
            "node": "Load System Message",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Error - Missing Query",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Load System Message": {
      "main": [
        [
          {
            "node": "Convert to Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to Text": {
      "main": [
        [
          {
            "node": "Call Anthropic API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call Anthropic API": {
      "main": [
        [
          {
            "node": "Format Response",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Error - API Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Response": {
      "main": [
        [
          {
            "node": "Success Response",
            "type": "main",
            "index": 0
          },
          {
            "node": "Log to Google Sheets (Optional)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Notify Slack (Optional)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [
    {
      "name": "Prompt Engine",
      "id": "1"
    },
    {
      "name": "AI",
      "id": "2"
    }
  ],
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "versionId": "1.0.0"
}