import { useState, useRef, useEffect } from "react"; import { Card } from "@/components/ui/card"; 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, MessageSquare, Plus } from "lucide-react"; import { useToast } from "@/hooks/use-toast"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Separator } from "@/components/ui/separator"; type Message = { id: string; role: "user" | "assistant"; 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(); // Mock data de agentes const agents = [ { id: "1", name: "Agente de Atendimento" }, { id: "2", name: "Agente de Vendas" }, { 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; } }, [messages]); // Limpa a conversa ao trocar de agente useEffect(() => { 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; const userMessage: Message = { id: Date.now().toString(), role: "user", content: input, }; const newMessages = [...messages, userMessage]; setMessages(newMessages); updateConversation(newMessages); setInput(""); setIsLoading(true); setTimeout(() => { const aiMessage: Message = { id: (Date.now() + 1).toString(), role: "assistant", content: "Esta é uma resposta simulada do agente de IA. Em produção, esta resposta seria gerada pelo modelo de IA configurado no agente selecionado.", }; const finalMessages = [...newMessages, aiMessage]; setMessages(finalMessages); updateConversation(finalMessages); setIsLoading(false); }, 1500); }; const handleKeyPress = (e: React.KeyboardEvent) => { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); handleSend(); } }; const handleDeleteConversation = (conversationId: string) => { setConversations((prev) => prev.filter((c) => c.id !== conversationId)); if (currentConversationId === conversationId) { setCurrentConversationId(null); setMessages([]); } toast({ title: "Conversa excluída", description: "A conversa foi removida com sucesso.", }); }; return (
{/* 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

)}
{/* Chat Area */}
{messages.length === 0 ? (

Selecione um agente para começar a conversar

) : ( messages.map((message) => (
{message.role === "assistant" && (
)}

{message.content}

{message.role === "user" && (
)}
)) )} {isLoading && (
)}
{/* Input Area */}
setInput(e.target.value)} onKeyPress={handleKeyPress} disabled={isLoading || !selectedAgent} className="flex-1" />
); }