{
  "name": "SlideAI \u2014 08 Chat Revision",
  "active": false,
  "nodes": [
    {
      "id": "wh-chat",
      "name": "On Chat Revision Request",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        0
      ],
      "parameters": {
        "path": "chat-revision",
        "httpMethod": "POST",
        "responseMode": "responseNode",
        "options": {}
      }
    },
    {
      "id": "resp-chat",
      "name": "Respond 200 OK",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1,
      "position": [
        280,
        0
      ],
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({status:'ok',project_id:$json.body?.project_id??$json.project_id??'received'}) }}"
      }
    },
    {
      "id": "set-chat",
      "name": "Extract Chat Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3,
      "position": [
        560,
        0
      ],
      "parameters": {
        "mode": "manual",
        "fields": {
          "values": [
            {
              "name": "project_id",
              "value": "={{ $json.body.project_id }}"
            },
            {
              "name": "slide_id",
              "value": "={{ $json.body.slide_id }}"
            },
            {
              "name": "message",
              "value": "={{ $json.body.message }}"
            }
          ]
        }
      }
    },
    {
      "id": "get-slide-rev",
      "name": "Get Slide to Revise",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        840,
        0
      ],
      "parameters": {
        "url": "=https://yqpvjtwldnkoersgqoti.supabase.co/rest/v1/slides?id=eq.{{ $json.slide_id }}&select=id,topic,slide_type,content,speaker_notes",
        "method": "GET",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Prefer",
              "value": "return=representation"
            }
          ]
        },
        "options": {}
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "get-history",
      "name": "Get Chat History (last 10)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1120,
        0
      ],
      "parameters": {
        "url": "=https://yqpvjtwldnkoersgqoti.supabase.co/rest/v1/chat_history?project_id=eq.{{ $('Extract Chat Data').first().json.project_id }}&order=created_at.desc&limit=10",
        "method": "GET",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Prefer",
              "value": "return=representation"
            }
          ]
        },
        "options": {}
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "chat-rev-agent",
      "name": "Chat Revision Agent \u2014 Groq",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1400,
        0
      ],
      "parameters": {
        "url": "https://api.groq.com/openai/v1/chat/completions",
        "method": "POST",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "groqApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "string",
        "body": "={{ JSON.stringify({ model: 'llama-3.3-70b-versatile', temperature: 0.7, max_tokens: 4096, messages: [{ role: 'system', content: \"You are a slide revision assistant. Modify only what is asked. Return JSON only: {\\\"content\\\":{\\\"title\\\":\\\"\\\",\\\"bullets\\\":[]},\\\"speaker_notes\\\":\\\"\\\"}\" }, { role: 'user', content: `Current slide: ${JSON.stringify($('Get Slide to Revise').first().json)}\\nUser request: ${$('Extract Chat Data').first().json.message}\\nMake the requested change and return the updated slide content.` }] }) }}",
        "options": {}
      },
      "credentials": {
        "groqApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "parse-rev",
      "name": "Parse Revision Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1680,
        0
      ],
      "parameters": {
        "jsCode": "\nconst raw = items[0].json.choices?.[0]?.message?.content??'{}';\nlet revised;\ntry { revised=JSON.parse(raw.replace(/```json?\\n?/g,'').replace(/```/g,'').trim()); }\ncatch(e){ revised={ content:{title:'Revised',bullets:[]}, speaker_notes:'' }; }\nconst d = $('Extract Chat Data').first().json;\nreturn [{ json:{...revised, slide_id:d.slide_id, project_id:d.project_id, user_message:d.message} }];\n",
        "mode": "runOnceForAllItems"
      }
    },
    {
      "id": "upd-revised",
      "name": "Update Revised Slide",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        1960,
        0
      ],
      "parameters": {
        "url": "=https://yqpvjtwldnkoersgqoti.supabase.co/rest/v1/slides?id=eq.{{ $json.slide_id }}",
        "method": "PATCH",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Prefer",
              "value": "return=representation"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "string",
        "body": "={{ JSON.stringify({content:$json.content, speaker_notes:$json.speaker_notes}) }}",
        "options": {}
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "id": "save-chat-msg",
      "name": "Save Chat Message",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [
        2240,
        0
      ],
      "parameters": {
        "url": "https://yqpvjtwldnkoersgqoti.supabase.co/rest/v1/chat_history",
        "method": "POST",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "supabaseApi",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            },
            {
              "name": "Prefer",
              "value": "return=representation"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "string",
        "body": "={{ JSON.stringify({project_id:$json.project_id, role:'user', message:$json.user_message, slide_id:$json.slide_id}) }}",
        "options": {}
      },
      "credentials": {
        "supabaseApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "On Chat Revision Request": {
      "main": [
        [
          {
            "node": "Respond 200 OK",
            "type": "main",
            "index": 0
          },
          {
            "node": "Extract Chat Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Chat Data": {
      "main": [
        [
          {
            "node": "Get Slide to Revise",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Slide to Revise": {
      "main": [
        [
          {
            "node": "Get Chat History (last 10)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Chat History (last 10)": {
      "main": [
        [
          {
            "node": "Chat Revision Agent \u2014 Groq",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chat Revision Agent \u2014 Groq": {
      "main": [
        [
          {
            "node": "Parse Revision Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Revision Response": {
      "main": [
        [
          {
            "node": "Update Revised Slide",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Revised Slide": {
      "main": [
        [
          {
            "node": "Save Chat Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}