"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; const AGENT_CONFIG: Record = { horus: { name: "Horus", avatar: "👁️", color: "#ff7bc0", role: "Master Orchestrator", description: "Command center, delegates tasks, manages all agents" }, thoth: { name: "Thoth", avatar: "🧠", color: "#8b5cf6", role: "Strategy & Research", description: "SiteMente planning, research, analysis" }, "thoth-trading": { name: "Thoth Trading", avatar: "📈", color: "#f59e0b", role: "Market Research", description: "Crypto market analysis and research" }, ptah: { name: "Ptah", avatar: "🏗️", color: "#10b981", role: "Dev & Ops", description: "Development, deployment, technical implementation" }, seshat: { name: "Seshat", avatar: "📜", color: "#ec4899", role: "Content & SEO", description: "Content strategy, SEO, marketing copy" }, anubis: { name: "Anubis", avatar: "🐺", color: "#6366f1", role: "Outreach & Growth", description: "Lead generation, client acquisition" }, sekhmet: { name: "Sekhmet", avatar: "⚔️", color: "#ef4444", role: "Risk Management", description: "Trade execution and risk" }, }; export default function AgentCenterPage({ params }: { params: { id: string } }) { const router = useRouter(); const agentId = params.id; const agent = AGENT_CONFIG[agentId] || { name: agentId, avatar: "🤖", color: "#666", role: "Agent", description: "" }; const [tasks, setTasks] = useState([]); const [templates, setTemplates] = useState([]); const [logs, setLogs] = useState([]); const [changelog, setChangelog] = useState([]); const [brainownNotes, setBrainownNotes] = useState([]); const [loading, setLoading] = useState(true); const [agentStatus, setAgentStatus] = useState<"idle" | "active" | "error">("idle"); const [tokens, setTokens] = useState("0"); const [lastRun, setLastRun] = useState(null); const [expanded, setExpanded] = useState>({ tasks: true, recurring: true, logs: true, changelog: true, brainown: true, }); useEffect(() => { fetchAllData(); }, [agentId]); const fetchAllData = async () => { setLoading(true); // Tasks try { const tRes = await fetch("/api/tasks"); if (tRes.ok) { const allTasks = await tRes.json(); const agentTasks = allTasks.filter((t: any) => t.assignee === agentId || t.project === getAgentProject(agentId) ); setTasks(agentTasks); } } catch {} // Templates try { const rRes = await fetch("/api/recurring"); if (rRes.ok) { const allTemplates = await rRes.json(); const agentTemplates = allTemplates.filter((t: any) => t.agent === agentId); setTemplates(agentTemplates); if (agentTemplates.length > 0) { const last = agentTemplates.reduce((prev: any, curr: any) => (!curr.lastRun || new Date(curr.lastRun) > new Date(prev.lastRun || 0) ? curr : prev), {}); if (last.lastRun) setLastRun(last.lastRun); } } } catch {} // Logs try { const lRes = await fetch(`/api/execution-logs?agent=${agentId}&limit=20`); if (lRes.ok) { const agentLogs = await lRes.json(); setLogs(agentLogs); if (agentLogs.length > 0) { setLastRun(agentLogs[0].startedAt); const running = agentLogs.find((l: any) => l.status === "running"); setAgentStatus(running ? "active" : "idle"); } } } catch {} // Changelog try { const cRes = await fetch(`/api/changelog?agent=${agentId}&limit=20`); if (cRes.ok) { setChangelog(await cRes.json()); } } catch {} // Brainown try { const bRes = await fetch(`/api/agent-outputs?agent=${agentId}`); if (bRes.ok) { const dates = await bRes.json(); const notes: any[] = []; for (const date of dates.slice(0, 10)) { const noteRes = await fetch(`/api/agent-outputs?agent=${agentId}&date=${date}`); if (noteRes.ok) { const note = await noteRes.json(); if (note.content) notes.push({ date, content: note.content }); } } setBrainownNotes(notes); } } catch {} // Simulate tokens setTokens((Math.random() * 2).toFixed(1)); setLoading(false); }; const getAgentProject = (id: string): string => { const map: Record = { thoth: "sitemente", "thoth-trading": "trading", ptah: "infrastructure", seshat: "sitemente", anubis: "sitemente", sekhmet: "trading", }; return map[id] || "sitemente"; }; const toggleSection = (section: string) => { setExpanded(prev => ({ ...prev, [section]: !prev[section] })); }; const toggleTemplate = async (templateId: string, enabled: boolean) => { await fetch("/api/recurring", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: "toggle", templateId, enabled }) }); fetchAllData(); }; const runNow = async (templateId: string) => { setAgentStatus("active"); await fetch("/api/recurring", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: "run-now", templateId }) }); setTimeout(fetchAllData, 2000); }; const todoTasks = tasks.filter((t: any) => t.status === "todo"); const inProgressTasks = tasks.filter((t: any) => t.status === "in_progress"); const doneTasks = tasks.filter((t: any) => t.status === "done"); const getStatusBadge = (status: string) => { switch (status) { case "active": return ● Active; case "idle": return ● Idle; case "error": return ● Error; default: return null; } }; if (loading) { return
Loading {agent.name}...
; } return (
{/* Back Button */} {/* Header Card */}
{agent.avatar}

{agent.name}

{agent.role}

{getStatusBadge(agentStatus)}

Tokens: {tokens}k

{lastRun && (

Last: {new Date(lastRun).toLocaleTimeString()}

)}
{/* 📋 TASKS - 3 Column Kanban */}
{expanded.tasks && (
{/* To Do */}

To Do

{todoTasks.length === 0 ? (

No tasks

) : ( todoTasks.map((task: any) => (
[{task.priority}]

{task.title}

)) )}
{/* In Progress */}

In Progress

{inProgressTasks.length === 0 ? (

No tasks

) : ( inProgressTasks.map((task: any) => (
[{task.priority}]

{task.title}

)) )}
{/* Done */}

Done

{doneTasks.length === 0 ? (

No tasks

) : ( doneTasks.slice(0, 5).map((task: any) => (

{task.title}

)) )}
)}
{/* 🔄 RECURRING SCHEDULES */}
{expanded.recurring && (
{templates.length === 0 ? (

No recurring tasks

) : ( templates.map((template: any) => (

{template.taskTemplate.title}

{template.schedule.type === "hourly" ? "Hourly" : template.schedule.time ? `${template.schedule.time} CET` : template.schedule.type}

)) )}
)}
{/* 📊 EXECUTION LOGS */}
{expanded.logs && (
{logs.length === 0 ? (

No execution logs

) : ( logs.map((log: any) => (

{log.taskTitle}

{new Date(log.startedAt).toLocaleString()} {log.completedAt && ` • ${Math.round((new Date(log.completedAt).getTime() - new Date(log.startedAt).getTime()) / 1000)}s`}

{log.status === "completed" ? "✓ Success" : log.status === "failed" ? "✕ Error" : log.status}
)) )}
)}
{/* 📝 CHANGE LOG */}
{expanded.changelog && (
{changelog.length === 0 ? (

No changes recorded

) : ( changelog.map((entry: any) => (
v{entry.version || "1.0"} {new Date(entry.date).toLocaleDateString()}

{entry.description}

)) )}
)}
{/* 🧠 BRAINOWN OUTPUTS */}
{expanded.brainown && (
{brainownNotes.length === 0 ? (

No outputs yet

) : ( brainownNotes.map((note: any) => (
📄 {note.date}
                    {note.content.substring(0, 300)}...
                  
)) )}
)}
{/* Back Button Bottom */}
); }