import type { AgentLogEntry } from "@fusion/core"; import { ProviderIcon } from "./ProviderIcon"; import { useRef, useEffect, useState } from "react"; import ReactMarkdown from "react-markdown"; import remarkGfm from "remark-gfm"; import type { Components } from "react-markdown"; function formatTimestamp(iso: string): string { const date = new Date(iso); const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffMin = Math.floor(diffMs / 60000); const diffHr = Math.floor(diffMin / 60); const diffDay = Math.floor(diffHr / 24); if (diffMin < 1) return "just now"; if (diffMin < 60) return `${diffMin}m ago`; if (diffHr < 24) return `${diffHr}h ago`; if (diffDay < 7) return `${diffDay}d ago`; return date.toLocaleDateString(); } const markdownComponents: Components = { pre: ({ children, ...props }) => (
      {children}
    
), table: ({ children, ...props }) => ( {children}
), }; interface ModelInfo { provider?: string; modelId?: string; } interface AgentLogViewerProps { entries: AgentLogEntry[]; loading: boolean; executorModel?: ModelInfo | null; validatorModel?: ModelInfo | null; planningModel?: ModelInfo | null; } /** * Renders agent log entries in a scrollable, monospace container. * Displays entries in reverse chronological order (newest first). * Auto-scrolls to keep latest entries visible when streaming. * Supports toggling between markdown-formatted and plain-text rendering. */ export function AgentLogViewer({ entries, loading, executorModel, validatorModel, planningModel }: AgentLogViewerProps) { const containerRef = useRef(null); const previousEntryCountRef = useRef(0); const [renderMarkdown, setRenderMarkdown] = useState(true); // Auto-scroll to top when new entries arrive (since newest are first) useEffect(() => { const container = containerRef.current; if (!container) return; const newEntryCount = entries.length; const previousCount = previousEntryCountRef.current; // Only scroll if new entries were added and user is near the top if (newEntryCount > previousCount) { // Check if user is already near the top (within 50px) const isNearTop = container.scrollTop <= 50; if (isNearTop) { container.scrollTop = 0; } } previousEntryCountRef.current = newEntryCount; }, [entries]); if (loading && entries.length === 0) { return (
Loading agent logs…
); } if (entries.length === 0) { return (
No agent output yet.
); } // Reverse entries so newest appear first const reversedEntries = [...entries].reverse(); const hasExecutorOverride = executorModel?.provider && executorModel?.modelId; const hasValidatorOverride = validatorModel?.provider && validatorModel?.modelId; const hasPlanningOverride = planningModel?.provider && planningModel?.modelId; return (
{/* Model info header */}
Executor: {hasExecutorOverride ? ( {executorModel.provider}/{executorModel.modelId} ) : ( Using default )}
Validator: {hasValidatorOverride ? ( {validatorModel.provider}/{validatorModel.modelId} ) : ( Using default )}
Planning/Triage: {hasPlanningOverride ? ( {planningModel.provider}/{planningModel.modelId} ) : ( Using default )}
{/* Markdown render toggle */}
{reversedEntries.map((entry, i) => { // Look at previous entry in reversed array (= next chronologically) for deduplication const prev = reversedEntries[i - 1]; const isBlockLevel = entry.type === "tool" || entry.type === "tool_result" || entry.type === "tool_error"; const showBadge = entry.agent ? isBlockLevel || i === 0 || (prev && (prev.agent !== entry.agent || prev.type !== entry.type)) : false; const agentBadge = showBadge ? ( [{entry.agent}] ) : null; const timestampSpan = ( {formatTimestamp(entry.timestamp)} ); if (entry.type === "tool") { return (
{agentBadge}{timestampSpan}⚡ {entry.text} {entry.detail && ( — {entry.detail} )}
); } if (entry.type === "thinking") { return ( {agentBadge}{timestampSpan} {renderMarkdown ? ( {entry.text} ) : ( entry.text )} ); } if (entry.type === "tool_result") { return (
{agentBadge}{timestampSpan}✓ {entry.text} {entry.detail && ( — {entry.detail} )}
); } if (entry.type === "tool_error") { return (
{agentBadge}{timestampSpan}✗ {entry.text} {entry.detail && ( — {entry.detail} )}
); } // Default: text entries return ( {agentBadge}{timestampSpan} {renderMarkdown ? ( {entry.text} ) : ( entry.text )} ); })}
); }