From c1d2cf6d799177505282a6724a23f3d94a8c763a Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Tue, 11 Nov 2025 20:04:58 +0000 Subject: [PATCH] Add conversation history to Test Prompt Implements persistent/voluminous chat history on the Testar Agente screen: - Prepare to store and load previous conversations (localStorage) for the selected agent - Create structure for conversations and messages - Load existing conversations on mount and save changes - Add support to create new conversations and load them when selected --- src/pages/TestPrompt.tsx | 225 ++++++++++++++++++++++++++++++++------- 1 file changed, 185 insertions(+), 40 deletions(-) diff --git a/src/pages/TestPrompt.tsx b/src/pages/TestPrompt.tsx index b70737e..fb7061f 100644 --- a/src/pages/TestPrompt.tsx +++ b/src/pages/TestPrompt.tsx @@ -4,7 +4,7 @@ import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { ScrollArea } from "@/components/ui/scroll-area"; -import { Send, Bot, User, RotateCcw } from "lucide-react"; +import { Send, Bot, User, RotateCcw, MessageSquare, Plus } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import { Select, @@ -13,6 +13,7 @@ import { SelectTrigger, SelectValue, } from "@/components/ui/select"; +import { Separator } from "@/components/ui/separator"; type Message = { id: string; @@ -20,11 +21,23 @@ type Message = { content: string; }; +type Conversation = { + id: string; + agentId: string; + agentName: string; + title: string; + messages: Message[]; + createdAt: string; + updatedAt: string; +}; + export default function TestPrompt() { const [input, setInput] = useState(""); const [messages, setMessages] = useState([]); const [isLoading, setIsLoading] = useState(false); const [selectedAgent, setSelectedAgent] = useState(""); + const [conversations, setConversations] = useState([]); + const [currentConversationId, setCurrentConversationId] = useState(null); const scrollRef = useRef(null); const { toast } = useToast(); @@ -35,6 +48,21 @@ export default function TestPrompt() { { id: "3", name: "Agente de Análise" }, ]; + // Carrega conversas do localStorage + useEffect(() => { + const stored = localStorage.getItem("test_conversations"); + if (stored) { + setConversations(JSON.parse(stored)); + } + }, []); + + // Salva conversas no localStorage + useEffect(() => { + if (conversations.length > 0) { + localStorage.setItem("test_conversations", JSON.stringify(conversations)); + } + }, [conversations]); + useEffect(() => { if (scrollRef.current) { scrollRef.current.scrollTop = scrollRef.current.scrollHeight; @@ -43,9 +71,59 @@ export default function TestPrompt() { // Limpa a conversa ao trocar de agente useEffect(() => { - setMessages([]); + handleNewConversation(); }, [selectedAgent]); + const handleNewConversation = () => { + if (!selectedAgent) return; + + const agent = agents.find((a) => a.id === selectedAgent); + const newConversation: Conversation = { + id: Date.now().toString(), + agentId: selectedAgent, + agentName: agent?.name || "Agente", + title: "Nova Conversa", + messages: [], + createdAt: new Date().toISOString(), + updatedAt: new Date().toISOString(), + }; + + setConversations((prev) => [newConversation, ...prev]); + setCurrentConversationId(newConversation.id); + setMessages([]); + }; + + const handleSelectConversation = (conversationId: string) => { + const conversation = conversations.find((c) => c.id === conversationId); + if (conversation) { + setCurrentConversationId(conversationId); + setSelectedAgent(conversation.agentId); + setMessages(conversation.messages); + } + }; + + const updateConversation = (newMessages: Message[]) => { + if (!currentConversationId) return; + + setConversations((prev) => + prev.map((conv) => { + if (conv.id === currentConversationId) { + const title = + newMessages.length > 0 + ? newMessages[0].content.substring(0, 50) + "..." + : "Nova Conversa"; + return { + ...conv, + messages: newMessages, + title, + updatedAt: new Date().toISOString(), + }; + } + return conv; + }) + ); + }; + const handleSend = () => { if (!input.trim() || !selectedAgent) return; @@ -55,7 +133,9 @@ export default function TestPrompt() { content: input, }; - setMessages((prev) => [...prev, userMessage]); + const newMessages = [...messages, userMessage]; + setMessages(newMessages); + updateConversation(newMessages); setInput(""); setIsLoading(true); @@ -66,7 +146,9 @@ export default function TestPrompt() { content: "Esta é uma resposta simulada do agente de IA. Em produção, esta resposta seria gerada pelo modelo de IA configurado no agente selecionado.", }; - setMessages((prev) => [...prev, aiMessage]); + const finalMessages = [...newMessages, aiMessage]; + setMessages(finalMessages); + updateConversation(finalMessages); setIsLoading(false); }, 1500); }; @@ -78,53 +160,116 @@ export default function TestPrompt() { } }; - const handleClearChat = () => { - setMessages([]); + const handleDeleteConversation = (conversationId: string) => { + setConversations((prev) => prev.filter((c) => c.id !== conversationId)); + if (currentConversationId === conversationId) { + setCurrentConversationId(null); + setMessages([]); + } toast({ - title: "Conversa limpa", - description: "O histórico de mensagens foi limpo com sucesso.", + title: "Conversa excluída", + description: "A conversa foi removida com sucesso.", }); }; return ( -
-
-

Testar Agente

-

- Converse com o agente para testar seu comportamento -

-
- - - {/* Selector de Agente */} +
+ {/* Sidebar com conversas anteriores */} +
-
-
- - +
+

Conversas

+ +
+
+ + +
+
+ + +
+ {conversations + .filter((conv) => conv.agentId === selectedAgent) + .map((conversation) => ( + + ))} + {selectedAgent && conversations.filter((c) => c.agentId === selectedAgent).length === 0 && ( +
+ Nenhuma conversa ainda +
+ )} +
+
+ + + {/* Área principal do chat */} + + {/* Header */} +
+
+
+

+ {selectedAgent + ? agents.find((a) => a.id === selectedAgent)?.name + : "Testar Agente"} +

+ {currentConversationId && ( +

+ Conversa atual +

+ )}