diff --git a/server.js b/server.js index 11582ce..7dadcb2 100644 --- a/server.js +++ b/server.js @@ -401,8 +401,37 @@ Retorne APENAS o JSON válido:`; } } + // Fallback para MiniMax se OpenRouter, Groq e Google Gemini falharem + if (!rawContent && process.env.MINIMAX_API_KEY) { + try { + console.log('[Smart Report] Usando MiniMax-M2.7 como fallback para gerar relatório...'); + const reportRes = await fetch('https://api.minimaxi.chat/v1/chat/completions', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.MINIMAX_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + model: 'MiniMax-M2.7', + messages: [{ role: 'user', content: reportPrompt }], + temperature: 0.5, + max_tokens: 1500 + }) + }); + + if (reportRes.ok) { + const reportData = await reportRes.json(); + rawContent = reportData.choices?.[0]?.message?.content || ''; + } else { + console.warn('[Smart Report] Falha no MiniMax:', await reportRes.text()); + } + } catch (err) { + console.error('[Smart Report] Erro de conexão com MiniMax:', err.message); + } + } + if (!rawContent) { - throw new Error('Falha ao processar o relatório pedagógico em todos os provedores de IA (OpenRouter, Groq e Google Gemini).'); + throw new Error('Falha ao processar o relatório pedagógico em todos os provedores de IA (OpenRouter, Groq, Google Gemini e MiniMax-M2.7).'); } // Parse JSON da resposta @@ -1643,6 +1672,29 @@ app.post('/api/chat', requireAuth, async (req, res) => { } } + // Se o OpenRouter, Groq e Google Gemini falharem ou retornarem erro, caímos no fallback do MiniMax M2.7 + if ((!chatResponse || !chatResponse.ok) && process.env.MINIMAX_API_KEY) { + console.log('[Smart Router] Usando MiniMax-M2.7 como fallback de streaming de texto...'); + providerUsed = 'MiniMax M2.7'; + try { + chatResponse = await fetch('https://api.minimaxi.chat/v1/chat/completions', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.MINIMAX_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + model: 'MiniMax-M2.7', + messages: enrichedMessages, + stream: true, + temperature + }) + }); + } catch (minimaxErr) { + console.error('[Smart Router] Erro crítico: Falha no fallback do MiniMax M2.7 também:', minimaxErr.message); + } + } + if (!chatResponse || !chatResponse.ok) { const errorText = chatResponse ? await chatResponse.text() : 'Sem conexão com os servidores de IA'; console.error(`Erro na API de IA (${providerUsed}):`, errorText);