{
  "name": "LogAnalyze AI Workflow - Corrigido",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "bugsnag-webhook",
        "authentication": "headerAuth",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "db8a8d6f-8690-4052-ad5b-33e61084df3a",
      "name": "Webhook Bugsnag",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [
        -440,
        100
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Extrair dados do payload do Bugsnag\nconst payload = $input.first().json;\n\n// Mapear dados para vari\u00e1veis estruturadas\nconst mappedData = {\n  title: payload.error?.message || 'Erro n\u00e3o identificado',\n  description: `**Erro:** ${payload.error?.message || 'N/A'}\\n\\n**Contexto:** ${payload.error?.context || 'N/A'}\\n\\n**Stack Trace:** ${payload.error?.stacktrace || 'N/A'}\\n\\n**URL:** ${payload.error?.url || 'N/A'}\\n\\n**Severidade:** ${payload.severity || 'error'}\\n\\n**Timestamp:** ${payload.receivedAt || new Date().toISOString()}`,\n  severity: payload.severity || 'error',\n  errorUrl: payload.error?.url || '',\n  projectId: payload.project?.id || '',\n  errorId: payload.error?.id || '',\n  timestamp: payload.receivedAt || new Date().toISOString(),\n  status: 'recebimento',\n  rawError: payload.error?.message || 'N/A',\n  stackTrace: payload.error?.stacktrace || 'N/A',\n  context: payload.error?.context || 'N/A'\n};\n\nreturn [{ json: mappedData }];"
      },
      "id": "dfea145d-01e2-4ac7-9d39-9435524ca0bd",
      "name": "Processar Dados Bugsnag",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        -220,
        100
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "key",
              "value": "={{ $env.GEMINI_API_KEY }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"contents\": [\n    {\n      \"parts\": [\n        {\n          \"text\": \"Analise este erro e retorne um JSON com:\\n- severity_level (1-5, onde 5 \u00e9 cr\u00edtico)\\n- classification (tipo do erro)\\n- priority (baixa, m\u00e9dia, alta, cr\u00edtica)\\n\\nErro: {{ $json.rawError }}\\nContexto: {{ $json.context }}\"\n        }\n      ]\n    }\n  ],\n  \"generationConfig\": {\n    \"maxOutputTokens\": 200\n  }\n}",
        "options": {}
      },
      "id": "c02db3aa-361d-4158-bb39-8bd54f2ba65a",
      "name": "AI Agente Recebimento (Gemini)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Processar resposta do AI Agent de Recebimento\nconst aiResponse = $input.first().json;\nconst originalData = $('Processar Dados Bugsnag').item.json;\n\n// Extrair dados da resposta do Gemini\ntry {\n  const aiText = aiResponse.candidates[0].content.parts[0].text;\n  const aiData = JSON.parse(aiText);\n  \n  return [{\n    json: {\n      ...originalData,\n      aiSeverityLevel: aiData.severity_level,\n      aiClassification: aiData.classification,\n      aiPriority: aiData.priority,\n      status: 'analise'\n    }\n  }];\n} catch (error) {\n  // Fallback se o parsing falhar\n  return [{\n    json: {\n      ...originalData,\n      aiSeverityLevel: 3,\n      aiClassification: 'Erro n\u00e3o classificado pelo AI',\n      aiPriority: 'm\u00e9dia',\n      status: 'analise'\n    }\n  }];\n}"
      },
      "id": "f22d9769-8e0a-4fc8-b6ab-bcb13b40f0be",
      "name": "Processar AI Recebimento",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://openrouter.ai/api/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.DEEPSEEK_API_KEY }}"
            },
            {
              "name": "model",
              "value": "={{ $env.DEEPSEEK_MODEL }}"
            },
            {
              "name": "messages",
              "value": "=[{\"role\":\"user\",\"content\":\"Analise tecnicamente este erro e forne\u00e7a:\\n1. Poss\u00edveis causas\\n2. Impacto no sistema\\n3. Sugest\u00f5es de solu\u00e7\u00e3o\\n4. C\u00f3digo de exemplo para corre\u00e7\u00e3o\\n\\nErro: {{ $json.rawError }}\\nContexto: {{ $json.context }}\\nStack Trace: {{ $json.stackTrace }}\"}]"
            },
            {
              "name": "max_tokens",
              "value": "={{ $env.DEEPSEEK_MAX_TOKENS }}"
            }
          ]
        },
        "options": {}
      },
      "id": "2f14176b-8f7c-4339-bd5b-923cfd3de620",
      "name": "AI Agente An\u00e1lise (DeepSeek)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Processar resposta do AI Agent de An\u00e1lise\nconst aiResponse = $input.first().json;\nconst previousData = $('Processar AI Recebimento').item.json;\n\nconst technicalAnalysis = aiResponse.choices[0].message.content;\n\nreturn [{\n  json: {\n    ...previousData,\n    technicalAnalysis: technicalAnalysis,\n    status: 'rascunho'\n  }\n}];"
      },
      "id": "d65fdcda-b4ef-4823-88e0-b4a779a39be9",
      "name": "Processar AI An\u00e1lise",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.groq.com/openai/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.GROQ_API_KEY }}"
            },
            {
              "name": "model",
              "value": "={{ $env.GROQ_MODEL }}"
            },
            {
              "name": "messages",
              "value": "=[{\"role\":\"user\",\"content\":\"Crie um rascunho de tarefa para este erro com:\\n1. T\u00edtulo claro e conciso\\n2. Descri\u00e7\u00e3o detalhada\\n3. Passos para reprodu\u00e7\u00e3o\\n4. Crit\u00e9rios de aceita\u00e7\u00e3o\\n\\nErro: {{ $json.rawError }}\\nAn\u00e1lise T\u00e9cnica: {{ $json.technicalAnalysis }}\\nClassifica\u00e7\u00e3o: {{ $json.aiClassification }}\"}]"
            },
            {
              "name": "max_tokens",
              "value": "={{ $env.GROQ_MAX_TOKENS }}"
            }
          ]
        },
        "options": {}
      },
      "id": "f21720d6-d2fe-418e-a0b9-7750ea09fd68",
      "name": "AI Agente Rascunho (Groq)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Processar resposta do AI Agent de Rascunho\nconst aiResponse = $input.first().json;\nconst previousData = $('Processar AI An\u00e1lise').item.json;\n\nconst draftContent = aiResponse.choices[0].message.content;\n\nreturn [{\n  json: {\n    ...previousData,\n    draftContent: draftContent,\n    status: 'revisao'\n  }\n}];"
      },
      "id": "d08185db-d3a6-4454-b204-b233b2bfe787",
      "name": "Processar AI Rascunho",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "key",
              "value": "={{ $env.GEMINI_API_KEY }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "contents",
              "value": "=[{\"parts\":[{\"text\":\"Revise e melhore este rascunho de tarefa. Retorne apenas o texto melhorado:\\n\\n{{ $json.draftContent }}\"}]}]"
            },
            {
              "name": "generationConfig",
              "value": "={\"maxOutputTokens\": {{ $env.GEMINI_MAX_TOKENS }}}"
            }
          ]
        },
        "options": {}
      },
      "id": "34cabebc-e096-4711-ac8b-c006ff3c1452",
      "name": "AI Agente Revis\u00e3o (Gemini)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Processar resposta do AI Agent de Revis\u00e3o\nconst aiResponse = $input.first().json;\nconst previousData = $('Processar AI Rascunho').item.json;\n\nconst reviewedContent = aiResponse.candidates[0].content.parts[0].text;\n\nreturn [{\n  json: {\n    ...previousData,\n    reviewedContent: reviewedContent,\n    status: 'refinamento'\n  }\n}];"
      },
      "id": "e2d425ba-0572-4664-9a1a-b351cba50ece",
      "name": "Processar AI Revis\u00e3o",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1540,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://openrouter.ai/api/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{ $env.DEEPSEEK_API_KEY }}"
            },
            {
              "name": "model",
              "value": "={{ $env.DEEPSEEK_MODEL }}"
            },
            {
              "name": "messages",
              "value": "=[{\"role\":\"user\",\"content\":\"Refine e finalize esta tarefa adicionando:\\n1. Labels/tags apropriados\\n2. Estimativa de tempo\\n3. Depend\u00eancias\\n4. Crit\u00e9rios de teste\\n\\nConte\u00fado: {{ $json.reviewedContent }}\\nAn\u00e1lise T\u00e9cnica: {{ $json.technicalAnalysis }}\"}]"
            },
            {
              "name": "max_tokens",
              "value": "={{ $env.DEEPSEEK_MAX_TOKENS }}"
            }
          ]
        },
        "options": {}
      },
      "id": "045715b9-5b51-4308-8a93-36887146ba9e",
      "name": "AI Agente Refinamento (DeepSeek)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1760,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "// Processar resposta do AI Agent de Refinamento\nconst aiResponse = $input.first().json;\nconst previousData = $('Processar AI Revis\u00e3o').item.json;\n\nconst refinedContent = aiResponse.choices[0].message.content;\n\nreturn [{\n  json: {\n    ...previousData,\n    refinedContent: refinedContent,\n    status: 'criacao'\n  }\n}];"
      },
      "id": "cd90c892-95f6-4c1c-bedc-2c6023b6b947",
      "name": "Processar AI Refinamento",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1980,
        0
      ]
    },
    {
      "parameters": {
        "team": "={{ $env.CLICKUP_WORKSPACE_ID }}",
        "space": "={{ $env.CLICKUP_SPACE_ID }}",
        "folder": "90112549965",
        "list": "={{ $env.CLICKUP_LIST_ID }}",
        "name": "={{ $json.title }}",
        "additionalFields": {
          "description": "={{ $json.refinedContent }}",
          "tags": [
            "bugsnag",
            "automacao",
            "ai-generated"
          ]
        }
      },
      "id": "fe164560-bccb-4022-ac05-5a30eccc7f7f",
      "name": "Criar Tarefa ClickUp",
      "type": "n8n-nodes-base.clickUp",
      "typeVersion": 1,
      "position": [
        2200,
        0
      ],
      "credentials": {
        "clickUpApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "key",
              "value": "={{ $env.GEMINI_API_KEY }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "contents",
              "value": "=[{\"parts\":[{\"text\":\"Crie uma notifica\u00e7\u00e3o concisa para Telegram sobre esta tarefa criada. Use emojis e formata\u00e7\u00e3o Markdown:\\n\\nT\u00edtulo: {{ $json.title }}\\nPrioridade: {{ $json.aiPriority }}\\nSeveridade: {{ $json.aiSeverityLevel }}/5\\nLink: https://app.clickup.com/t/{{ $('Criar Tarefa ClickUp').item.json.id }}\"}]}]"
            },
            {
              "name": "generationConfig",
              "value": "={\"maxOutputTokens\": 200}"
            }
          ]
        },
        "options": {}
      },
      "id": "f451304d-6741-4608-9376-bca609315c2c",
      "name": "AI Agente Notifica\u00e7\u00e3o (Gemini)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2420,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// Processar resposta do AI Agent de Notifica\u00e7\u00e3o\nconst aiResponse = $input.first().json;\nconst notificationText = aiResponse.candidates[0].content.parts[0].text;\n\nreturn [{\n  json: {\n    notificationText: notificationText\n  }\n}];"
      },
      "id": "8a29faab-5acb-41c3-9c6c-bc32342e8f00",
      "name": "Processar AI Notifica\u00e7\u00e3o",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2640,
        0
      ]
    },
    {
      "parameters": {
        "chatId": "={{ $env.TELEGRAM_CHAT_ID }}",
        "text": "={{ $json.notificationText }}",
        "additionalFields": {
          "parse_mode": "Markdown"
        }
      },
      "id": "41fa8589-24ed-4923-b78f-447dc3d31985",
      "name": "Enviar Notifica\u00e7\u00e3o Telegram",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        2860,
        0
      ],
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Webhook Bugsnag": {
      "main": [
        [
          {
            "node": "Processar Dados Bugsnag",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Processar Dados Bugsnag": {
      "main": [
        [
          {
            "node": "AI Agente Recebimento (Gemini)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agente Recebimento (Gemini)": {
      "main": [
        [
          {
            "node": "Processar AI Recebimento",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Processar AI Recebimento": {
      "main": [
        [
          {
            "node": "AI Agente An\u00e1lise (DeepSeek)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agente An\u00e1lise (DeepSeek)": {
      "main": [
        [
          {
            "node": "Processar AI An\u00e1lise",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Processar AI An\u00e1lise": {
      "main": [
        [
          {
            "node": "AI Agente Rascunho (Groq)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agente Rascunho (Groq)": {
      "main": [
        [
          {
            "node": "Processar AI Rascunho",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Processar AI Rascunho": {
      "main": [
        [
          {
            "node": "AI Agente Revis\u00e3o (Gemini)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agente Revis\u00e3o (Gemini)": {
      "main": [
        [
          {
            "node": "Processar AI Revis\u00e3o",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Processar AI Revis\u00e3o": {
      "main": [
        [
          {
            "node": "AI Agente Refinamento (DeepSeek)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agente Refinamento (DeepSeek)": {
      "main": [
        [
          {
            "node": "Processar AI Refinamento",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Processar AI Refinamento": {
      "main": [
        [
          {
            "node": "Criar Tarefa ClickUp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Criar Tarefa ClickUp": {
      "main": [
        [
          {
            "node": "AI Agente Notifica\u00e7\u00e3o (Gemini)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agente Notifica\u00e7\u00e3o (Gemini)": {
      "main": [
        [
          {
            "node": "Processar AI Notifica\u00e7\u00e3o",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Processar AI Notifica\u00e7\u00e3o": {
      "main": [
        [
          {
            "node": "Enviar Notifica\u00e7\u00e3o Telegram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "717b3e97-59dd-448b-926d-745d2324cdb3",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "3KZvo7wPNiLXPwnm",
  "tags": [
    {
      "createdAt": "2025-06-24T02:29:21.074Z",
      "updatedAt": "2025-06-24T02:29:21.074Z",
      "id": "N1paBfckoiZSEnJ6",
      "name": "LogAnalyze-AI"
    }
  ]
}