{
  "name": "Trivia AI Mode Generator",
  "nodes": [
    {
      "parameters": {
        "path": "trivia-ai-generator",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1.1,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.z.ai/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{$credentials.apiKey}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"zai-pro\",\n  \"messages\": [\n    {\n      \"role\": \"system\",\n      \"content\": \"You are an expert trivia game creator. Generate engaging, educational, and accurate trivia questions. Ensure questions are factually correct and properly formatted.\"\n    },\n    {\n      \"role\": \"user\",\n      \"content\": \"Generate {{ $json.numQuestions }} trivia questions about {{ $json.topic }} at {{ $json.difficulty }} difficulty level.\\n\\nRequirements:\\n1. Include a mix of question types: multiple-choice, true-false, and text-input\\n2. For multiple-choice: Provide 4 options (a, b, c, d)\\n3. For text-input: Provide acceptable alternative answers\\n4. Each question must have a clear, concise explanation\\n5. Questions should cover different aspects of the topic\\n6. Ensure difficulty is appropriate for the specified level\\n\\nReturn response in this exact JSON format:\\n{\\n  \\\"topic\\\": \\\"{{ $json.topic }}\\\",\\n  \\\"difficulty\\\": \\\"{{ $json.difficulty }}\\\",\\n  \\\"questions\\\": [\\n    {\\n      \\\"id\\\": \\\"1\\\",\\n      \\\"type\\\": \\\"multiple-choice\\\",\\n      \\\"question\\\": \\\"Your question here\\\",\\n      \\\"correctAnswer\\\": \\\"a\\\",\\n      \\\"options\\\": [\\n        {\\\"id\\\": \\\"a\\\", \\\"text\\\": \\\"Option A\\\"},\\n        {\\\"id\\\": \\\"b\\\", \\\"text\\\": \\\"Option B\\\"},\\n        {\\\"id\\\": \\\"c\\\", \\\"text\\\": \\\"Option C\\\"},\\n        {\\\"id\\\": \\\"d\\\", \\\"text\\\": \\\"Option D\\\"}\\n      ],\\n      \\\"explanation\\\": \\\"Clear explanation here\\\"\\n    }\\n  ],\\n  \\\"total_questions\\\": {{ $json.numQuestions }}\\n}\"\n    }\n  ],\n  \"temperature\": 0.7,\n  \"max_tokens\": 4000\n}"
      },
      "id": "z-ai-llm",
      "name": "Z.AI LLM - Generate Questions",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const response = items[0].json;\n\n// Parse the AI response\nlet parsedData;\ntry {\n  const content = response.choices[0].message.content;\n  // Extract JSON from the response (in case there's extra text)\n  const jsonMatch = content.match(/\\{[\\s\\S]*\\}/);\n  if (jsonMatch) {\n    parsedData = JSON.parse(jsonMatch[0]);\n  } else {\n    parsedData = JSON.parse(content);\n  }\n} catch (error) {\n  return [{\n    json: {\n      error: 'Failed to parse AI response',\n      details: error.message,\n      rawResponse: response\n    }\n  }];\n}\n\n// Generate categories based on topic\nconst categories = [\n  {\n    name: 'General',\n    description: `General knowledge about ${parsedData.topic}`,\n    subcategories: ['Basics', 'Key Facts', 'Overview']\n  },\n  {\n    name: 'Deep Dive',\n    description: `In-depth questions about ${parsedData.topic}`,\n    subcategories: ['Advanced', 'Specific Topics', 'Details']\n  }\n];\n\n// Format questions\nconst formattedQuestions = parsedData.questions.map((q, index) => ({\n  id: q.id || (index + 1).toString(),\n  type: q.type,\n  question: q.question,\n  correctAnswer: q.correctAnswer,\n  explanation: q.explanation,\n  options: q.options,\n  acceptableAnswers: q.acceptableAnswers\n}));\n\n// Return structured game data\nreturn [{\n  json: {\n    topic: parsedData.topic || items[0].json.topic,\n    difficulty: parsedData.difficulty || items[0].json.difficulty,\n    categories: categories,\n    questions: formattedQuestions,\n    total_questions: formattedQuestions.length,\n    difficulty_distribution: {\n      [items[0].json.difficulty]: formattedQuestions.length\n    }\n  }\n}];"
      },
      "id": "format-response",
      "name": "Format Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        800,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}"
      },
      "id": "respond-to-webhook",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1100,
        300
      ]
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Z.AI LLM - Generate Questions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Z.AI LLM - Generate Questions": {
      "main": [
        [
          {
            "node": "Format Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Response": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 1,
  "updatedAt": "2024-01-22T12:00:00.000Z",
  "versionId": "1"
}