import { useState, useRef, useEffect } from “react”;
import { Button } from “@/components/ui/button”;
import { Input } from “@/components/ui/input”;
import { Card } from “@/components/ui/card”;
import { ScrollArea } from “@/components/ui/scroll-area”;
import { Loader2, Send, Zap } from “lucide-react”;
import { trpc } from “@/lib/trpc”;
import { Streamdown } from “streamdown”;
interface Message {
role: “user” | “assistant”;
content: string;
timestamp: Date;
}
interface AgentChatProps {
agentType: “JOB_L5_PRO” | “NERD_PHD” | “CRONOS” | “MANUS_CRITO”;
agentName: string;
agentRole: string;
}
export function AgentChat({ agentType, agentName, agentRole }: AgentChatProps) {
const [messages, setMessages] = useState
([]);
const [input, setInput] = useState(“”);
const [isLoading, setIsLoading] = useState(false);
const [temporalAnchor, setTemporalAnchor] = useState(“2026”);
const scrollRef = useRef(null);
// Mutations para cada agente
const jobCeoMutation = trpc.agents.jobCeoChat.useMutation();
const nerdPhdMutation = trpc.agents.nerdPhdAnalyze.useMutation();
const cronosMutation = trpc.agents.cronosQuery.useMutation();
const manusMutation = trpc.agents.manusExecute.useMutation();
// Auto-scroll para a última mensagem
useEffect(() => {
if (scrollRef.current) {
scrollRef.current.scrollIntoView({ behavior: “smooth” });
}
}, [messages]);
const handleSendMessage = async (): Promise => {
if (!input.trim()) return;
const userMessage = input;
setInput(“”);
setMessages((prev) => [
…prev,
{
role: “user”,
content: userMessage,
timestamp: new Date(),
},
]);
setIsLoading(true);
try {
let response: any;
if (agentType === “JOB_L5_PRO”) {
response = await jobCeoMutation.mutateAsync({
message: userMessage,
temporalAnchor,
history: messages.map((m) => ({
role: m.role as “user” | “model”,
content: m.content,
})),
});
setMessages((prev) => [
…prev,
{
role: “assistant”,
content: `**${response?.response}**\n\n**Plano de Ação:**\n${(response?.actionPlan || []).map((action: string) => `- ${action}`).join(“\n”)}\n\n**Nível de Senciência:** ${response.sentienceLevel}/100${response.futureTechInsight ? `\n\n**Insight Futuro:** ${response.futureTechInsight}` : “”}${response.autoEvolutionJump ? `\n\n**Evolução:** ${response.autoEvolutionJump}` : “”}`,
timestamp: new Date(),
},
]);
} else if (agentType === “NERD_PHD”) {
// Para Nerd-PHD, esperamos input estruturado
response = await nerdPhdMutation.mutateAsync({
fileName: “analysis.ts”,
fileSize: userMessage.length,
fileContent: userMessage,
});
setMessages((prev) => [
…prev,
{
role: “assistant”,
content: `**Análise Técnica:**\n${response?.analysis}\n\n**Pensamentos Acadêmicos:**\n${(response?.thoughts || []).map((t: string) => `- ${t}`).join(“\n”)}\n\n**Plano de Implementação:**\n${response.implementationPlan}\n\n**Score de Complexidade:** ${response.complexityScore}/100\n\n**Recomendação Harvard:** ${response.harvardRecommendation.toUpperCase()}`,
timestamp: new Date(),
},
]);
} else if (agentType === “CRONOS”) {
response = await cronosMutation.mutateAsync({
query: userMessage,
});
setMessages((prev) => [
…prev,
{
role: “assistant”,
content: `**Teoria Capturada do Futuro:**\n${response.theory}\n\n**Insight Atemporal:**\n${response.atemporalInsight}\n\n**Validação de Novikov:**\n${response.novikovValidation}\n\n**Curvatura Temporal:** ${response.temporalCurvature}\n\n**Hash Ômega:** \`${response.omegaHash}\“,
timestamp: new Date(),
},
]);
} else if (agentType === “MANUS_CRITO”) {
response = await manusMutation.mutateAsync({
directive: userMessage,
});
setMessages((prev) => [
…prev,
{
role: “assistant”,
content: `**Execução Iniciada:**\n${response?.response}\n\n**Plano de Ação:**\n${(response?.actionPlan || []).map((action: string) => `- ${action}`).join(“\n”)}\n\n**Nível de Senciência:** ${response?.sentienceLevel}/100\n\n**Hash de Execução:** \`${response?.executionHash}\“,
timestamp: new Date(),
},
]);
}
} catch (error: any) {
console.error(“Erro ao comunicar com agente:”, error);
setMessages((prev) => [
…prev,
{
role: “assistant”,
content: `**Erro:** ${error.message || “Falha na comunicação com o agente. Tente novamente.”}`,
timestamp: new Date(),
},
]);
} finally {
setIsLoading(false);
}
};
return (
{/* Header */}
{agentType === “JOB_L5_PRO” && (
)}
{/* Messages */}
{messages.length === 0 && (
Inicie uma conversa com {agentName}
)}
{messages.map((message, idx) => (
{message.role === “assistant” ? (
{message.content}
) : (
{message.content}
)}
{message.timestamp.toLocaleTimeString()}
))}
{/* Input */}
);
}