From 9e48ef6e5ee68c180748e3d1676478f879c819bc Mon Sep 17 00:00:00 2001 From: admtracksteel Date: Wed, 27 May 2026 17:35:06 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20feat:=20adicionado=20fallback=20Min?= =?UTF-8?q?iMax-M2.7=20no=20Smart=20Router=20e=20formatador=20de=20relat?= =?UTF-8?q?=C3=B3rios?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) 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);