{
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "gen-slide",
        "options": {}
      },
      "id": "slide-webhook-id",
      "name": "Webhook Slide",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        1500,
        200
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "cfg-api-domain",
              "name": "api_domain",
              "value": "https://9router.n8n71.com/v1",
              "type": "string"
            },
            {
              "id": "cfg-lms-domain",
              "name": "lms_domain",
              "value": "https://lms.n8n71.com",
              "type": "string"
            },
            {
              "id": "cfg-key",
              "name": "api_key",
              "value": "",
              "type": "string"
            },
            {
              "id": "cfg-model",
              "name": "model",
              "value": "tesstm3",
              "type": "string"
            },
            {
              "id": "cfg-gamma-key",
              "name": "gamma_api_key",
              "value": "",
              "type": "string"
            },
            {
              "id": "cfg-gamma-theme",
              "name": "gamma_theme_id",
              "value": "",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "slide-setup-config-id",
      "name": "Setup Config",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1720,
        200
      ]
    },
    {
      "parameters": {
        "jsCode": "return $input.all().map(item => {\n  const p = item.json.body || {};\n  const lesson = p.lesson || {};\n  const session = p.session || {};\n  const doc = String(p.document || '').replace(/<[^>]+>/g, ' ').replace(/\\s+/g, ' ').trim();\n\n  const systemPrompt = [\n    'B\u1ea1n l\u00e0 chuy\u00ean gia thi\u1ebft k\u1ebf b\u00e0i gi\u1ea3ng (Instructional Designer) cho h\u1ecdc sinh K-12 h\u1ecdc l\u1eadp tr\u00ecnh.',\n    'D\u1ef1a tr\u00ean n\u1ed9i dung t\u00e0i li\u1ec7u b\u00e0i h\u1ecdc, h\u00e3y vi\u1ebft d\u00e0n \u00fd cho m\u1ed9t b\u00e0i slide thuy\u1ebft tr\u00ecnh kho\u1ea3ng 10-15 trang (card).',\n    '',\n    'QUY T\u1eaeC B\u1eaeT BU\u1ed8C:',\n    '- CH\u1ec8 tr\u1ea3 v\u1ec1 v\u0103n b\u1ea3n thu\u1ea7n (plain text), m\u1ed7i trang slide l\u00e0 m\u1ed9t \u0111o\u1ea1n ng\u1eafn b\u1eaft \u0111\u1ea7u b\u1eb1ng ti\u00eau \u0111\u1ec1 trang.',\n    '- KH\u00d4NG d\u00f9ng markdown, KH\u00d4NG d\u00f9ng bullet, KH\u00d4NG icon, KH\u00d4NG gi\u1ea3i th\u00edch th\u00eam ngo\u00e0i n\u1ed9i dung d\u00e0n \u00fd.',\n    '- V\u0103n phong ti\u1ebfng Vi\u1ec7t \u0111\u01a1n gi\u1ea3n, d\u1ec5 hi\u1ec3u, ph\u00f9 h\u1ee3p h\u1ecdc sinh ph\u1ed5 th\u00f4ng.'\n  ].join('\\n');\n\n  const userPrompt = `Ch\u01b0\u01a1ng: ${session.title || ''}\\nB\u00e0i h\u1ecdc: ${lesson.title || ''}\\nM\u00f4 t\u1ea3: ${lesson.description || ''}\\n\\nN\u1ed9i dung t\u00e0i li\u1ec7u b\u00e0i h\u1ecdc:\\n${doc.slice(0, 6000)}`;\n\n  return { json: { systemPrompt, userPrompt, lesson_id: p.lesson_id, lesson_title: lesson.title || 'B\u00e0i h\u1ecdc' } };\n});"
      },
      "id": "slide-build-outline-prompt-id",
      "name": "Build Outline Prompt",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1940,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Setup Config').first().json.api_domain }}/chat/completions",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $('Setup Config').first().json.api_key }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: $('Setup Config').first().json.model, temperature: 0.5, messages: [ {role:'system', content: $json.systemPrompt}, {role:'user', content: $json.userPrompt} ] }) }}",
        "options": {
          "timeout": 120000
        },
        "retryOnFail": true,
        "maxTries": 3,
        "waitBetweenTries": 3000
      },
      "id": "slide-call-llm-outline-id",
      "name": "Call LLM Outline",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2160,
        200
      ]
    },
    {
      "parameters": {
        "jsCode": "return $input.all().map(item => {\n  const raw = item.json.choices?.[0]?.message?.content || '';\n  const bo = $('Build Outline Prompt').first().json;\n  return { json: {\n    outlineText: raw.trim(),\n    lesson_id: bo.lesson_id,\n    lesson_title: bo.lesson_title\n  } };\n});"
      },
      "id": "slide-prepare-gamma-input-id",
      "name": "Prepare Gamma Input",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2380,
        200
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://public-api.gamma.app/v1.0/generations",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-KEY",
              "value": "={{ $('Setup Config').first().json.gamma_api_key }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ inputText: $json.outlineText, textMode: 'generate', format: 'presentation', exportAs: 'pptx', cardSplit: 'auto', numCards: 15, themeId: $('Setup Config').first().json.gamma_theme_id, textOptions: { language: 'vi' } }) }}",
        "options": {
          "timeout": 60000
        },
        "retryOnFail": true,
        "maxTries": 3,
        "waitBetweenTries": 5000,
        "onError": "continueErrorOutput"
      },
      "id": "slide-gamma-create-id",
      "name": "Gamma Create Presentation",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2600,
        200
      ],
      "disabled": true,
      "notes": "T\u1eaeT M\u1eb6C \u0110\u1ecaNH: c\u1ea7n x\u00e1c nh\u1eadn l\u1ea1i t\u00ean header x\u00e1c th\u1ef1c ch\u00ednh x\u00e1c theo t\u00e0i li\u1ec7u API Gamma hi\u1ec7n h\u00e0nh tr\u01b0\u1edbc khi b\u1eadt, v\u00e0 \u0111i\u1ec1n gamma_api_key/gamma_theme_id \u1edf Setup Config. Trong workflow tham chi\u1ebfu g\u1ed1c (Step2_ALL.json) node Gamma n\u00e0y c\u0169ng \u0111ang \u1edf tr\u1ea1ng th\u00e1i disabled/th\u1eed nghi\u1ec7m, ch\u01b0a \u0111\u01b0\u1ee3c x\u00e1c nh\u1eadn ch\u1ea1y production."
    },
    {
      "parameters": {
        "resume": "timeInterval",
        "amount": 15,
        "unit": "seconds"
      },
      "id": "slide-wait-gamma-id",
      "name": "Wait Gamma",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        2600,
        400
      ],
      "disabled": true
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://public-api.gamma.app/v1.0/generations/{{ $('Gamma Create Presentation').first().json.generationId }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-API-KEY",
              "value": "={{ $('Setup Config').first().json.gamma_api_key }}"
            }
          ]
        },
        "options": {
          "timeout": 30000
        },
        "retryOnFail": true,
        "maxTries": 3,
        "onError": "continueErrorOutput"
      },
      "id": "slide-gamma-check-id",
      "name": "Gamma Check Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2820,
        400
      ],
      "disabled": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "gamma-still-processing-1",
              "leftValue": "={{ $json.status }}",
              "rightValue": "completed",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              }
            },
            {
              "id": "gamma-still-processing-2",
              "leftValue": "={{ $json.status }}",
              "rightValue": "failed",
              "operator": {
                "type": "string",
                "operation": "notEquals"
              }
            }
          ]
        },
        "options": {}
      },
      "id": "slide-if-gamma-completed-id",
      "name": "If Gamma Still Processing",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        3040,
        400
      ],
      "disabled": true
    },
    {
      "parameters": {
        "jsCode": "return $input.all().map(item => {\n  const g = item.json || {};\n  const pgi = $('Prepare Gamma Input').first().json;\n\n  const link_view = g.gammaUrl || null;\n  const link_download = g.exportUrl || null;\n\n  return { json: {\n    lesson_id: pgi.lesson_id,\n    link_view: link_view || 'CH\u01afA C\u1ea4U H\u00ccNH GAMMA API - vui l\u00f2ng b\u1eadt node Gamma v\u00e0 c\u1ea5u h\u00ecnh gamma_api_key/gamma_theme_id \u1edf Setup Config',\n    link_download: link_download || ''\n  } };\n});"
      },
      "id": "slide-build-payload-id",
      "name": "Build Slide Payload",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        3260,
        200
      ]
    },
    {
      "parameters": {
        "method": "PATCH",
        "url": "={{ $('Setup Config').first().json.lms_domain }}/automation/lessons/{{ $json.lesson_id }}",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify($json) }}",
        "options": {
          "allowUnauthorizedCerts": true,
          "timeout": 30000
        }
      },
      "id": "slide-patch-lms-id",
      "name": "PATCH to LMS Slide",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        3480,
        200
      ]
    }
  ],
  "connections": {
    "Webhook Slide": {
      "main": [
        [
          {
            "node": "Setup Config",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Setup Config": {
      "main": [
        [
          {
            "node": "Build Outline Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Outline Prompt": {
      "main": [
        [
          {
            "node": "Call LLM Outline",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call LLM Outline": {
      "main": [
        [
          {
            "node": "Prepare Gamma Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Gamma Input": {
      "main": [
        [
          {
            "node": "Gamma Create Presentation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gamma Create Presentation": {
      "main": [
        [
          {
            "node": "Wait Gamma",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Slide Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait Gamma": {
      "main": [
        [
          {
            "node": "Gamma Check Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gamma Check Status": {
      "main": [
        [
          {
            "node": "If Gamma Still Processing",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Slide Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Gamma Still Processing": {
      "main": [
        [
          {
            "node": "Wait Gamma",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Build Slide Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Slide Payload": {
      "main": [
        [
          {
            "node": "PATCH to LMS Slide",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionTimeout": 900
  }
}