import { useState, useEffect } from 'react'; import { useParams, Link, useNavigate } from 'react-router-dom'; function AgentStatus() { const { name } = useParams(); const navigate = useNavigate(); const [statusData, setStatusData] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [eventSource, setEventSource] = useState(null); const [liveUpdates, setLiveUpdates] = useState([]); // Update document title useEffect(() => { if (name) { document.title = `Agent Status: ${name} - LocalAGI`; } return () => { document.title = 'LocalAGI'; // Reset title when component unmounts }; }, [name]); // Fetch initial status data useEffect(() => { const fetchStatusData = async () => { try { const response = await fetch(`/api/agent/${name}/status`); if (!response.ok) { throw new Error(`Server responded with status: ${response.status}`); } const data = await response.json(); setStatusData(data); } catch (err) { console.error('Error fetching agent status:', err); setError(`Failed to load status for agent "${name}": ${err.message}`); } finally { setLoading(false); } }; fetchStatusData(); // Setup SSE connection for live updates const sse = new EventSource(`/sse/${name}`); setEventSource(sse); sse.addEventListener('status', (event) => { try { const data = JSON.parse(event.data); setLiveUpdates(prev => [data, ...prev.slice(0, 19)]); // Keep last 20 updates } catch (err) { console.error('Error parsing SSE data:', err); } }); sse.onerror = (err) => { console.error('SSE connection error:', err); }; // Cleanup on unmount return () => { if (sse) { sse.close(); } }; }, [name]); // Helper function to safely convert any value to a displayable string const formatValue = (value) => { if (value === null || value === undefined) { return 'N/A'; } if (typeof value === 'object') { try { return JSON.stringify(value, null, 2); } catch (err) { return '[Complex Object]'; } } return String(value); }; if (loading) { return (

Loading agent status...

); } if (error) { return (

Error

{error}

Back to Agents
); } // Combine live updates with history const allUpdates = [...liveUpdates, ...(statusData?.History || [])]; return (

Agent Status: {name}

{/* Chat Messages */}
{allUpdates.length > 0 ? ( allUpdates.map((item, index) => (

Agent Action:

Result: {formatValue(item.Result)}
Action: {formatValue(item.Action)}
Parameters: {formatValue(item.Params)}
{item.Reasoning && (
Reasoning: {formatValue(item.Reasoning)}
)}
)) ) : (

No status data available for this agent.

)}
); } export default AgentStatus;