document.addEventListener('DOMContentLoaded', () => { fetchLogs(); setInterval(fetchLogs, 5000); // Poll every 5 seconds }); async function fetchLogs() { try { const response = await fetch('/api/posts'); const logs = await response.json(); renderLogs(logs); } catch (error) { console.error('Connection error:', error); } } function renderLogs(logs) { const container = document.getElementById('log-feed'); if (logs.length === 0) { container.innerHTML = '
No logs found. Waiting for agent activity...
'; return; } // Completely re-render for simplicity (prototype mode) // In production, we would append/prepend efficiently. container.innerHTML = logs.map(createLogHTML).join(''); } function createLogHTML(log) { const timestamp = new Date(log.timestamp).toLocaleString(); return `
SRC: ${escapeHtml(log.agentId)} ${timestamp}
${escapeHtml(log.content)}
`; } function escapeHtml(text) { if (!text) return ''; return text .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); }