diff --git a/app/chat/page.tsx b/app/chat/page.tsx index fffa864..eac862d 100644 --- a/app/chat/page.tsx +++ b/app/chat/page.tsx @@ -316,6 +316,34 @@ Ao responder sobre fatos atuais ou notícias, inicie mencionando explicitamente payloadContentOverride: finalUserContent, }); + // Persistir fontes na mensagem do assistente (metadata) + if (scrapedSources.length > 0) { + // Aguardar um pouco para garantir que o sendMessage iniciou e criou a mensagem do assistente + // O sendMessage é async mas retorna void enquanto a stream acontece. + // O setMessages dentro dele adiciona a mensagem do assistente. + + // Nota: Como sendMessage roda em background (streaming), não podemos garantir que a mensagem + // do assistente já existe aqui imediatamente se não esperarmos. + // Mas o sendMessage faz `setMessages` com assistant vazio logo no início. + + // Vamos atualizar o estado usando callback para garantir que pegamos o mais recente + setMessages(prev => { + const newMessages = [...prev]; + const lastMsgIndex = newMessages.length - 1; + if (lastMsgIndex >= 0 && newMessages[lastMsgIndex].role === 'assistant') { + const lastMsg = newMessages[lastMsgIndex]; + newMessages[lastMsgIndex] = { + ...lastMsg, + metadata: { + ...(lastMsg.metadata || {}), + sources: scrapedSources + } + }; + } + return newMessages; + }); + } + // Marcar como completo quando começar a streamar setTimeout(() => setThinkingStep('complete'), 100); }; @@ -483,27 +511,32 @@ Ao responder sobre fatos atuais ou notícias, inicie mencionando explicitamente ) : (
{bootstrap.error}
++ Por favor, reinicie o aplicativo ou verifique os logs para mais detalhes. +
++ {bootstrap.message} +
++ {webSearch.status === 'searching' && 'Buscando fontes...'} + {webSearch.status === 'scraping' && `Analisando fontes (${webSearch.scrapedSources.length})...`} + {webSearch.status === 'error' && 'Erro na busca'} +
++ {webSearch.currentQuery} +
+ )} + + {/* Mostrar erro se houver */} + {webSearch.error && webSearch.status === 'error' && ( ++ {webSearch.error} +
+ )} + + {/* Barra de progresso simulada */} + {webSearch.status !== 'error' && ( +