"use client"; import { useState, memo } from "react"; import { IconAlertTriangle, IconInfoCircle, IconHandStop, IconChevronDown, IconChevronRight, } from "@tabler/icons-react"; import { cn } from "@/lib/utils"; import type { Message } from "@/lib/types/http"; import type { StatusMetadata } from "@/components/task/chat/types"; interface ErrorMetadata extends StatusMetadata { error?: string; text?: string; error_data?: Record; stderr?: string[]; provider?: string; provider_agent?: string; } function getStatusStyle(isError: boolean, isWarning: boolean) { if (isError) { return { Icon: IconAlertTriangle, iconClass: "text-red-500", textClass: "text-red-600 dark:text-red-400", }; } if (isWarning) { return { Icon: IconAlertTriangle, iconClass: "text-amber-500", textClass: "text-amber-600 dark:text-amber-400", }; } return { Icon: IconInfoCircle, iconClass: "text-muted-foreground", textClass: "text-muted-foreground", }; } function formatErrorDetails( metadata: ErrorMetadata | undefined, ): { label: string; value: string }[] { const details: { label: string; value: string }[] = []; if (!metadata) return details; if (metadata.stderr && metadata.stderr.length > 0) { details.push({ label: "Agent Output", value: metadata.stderr.join("\n") }); } if (metadata.error) { details.push({ label: "Error", value: metadata.error }); } if (metadata.text) { details.push({ label: "Details", value: metadata.text }); } if (metadata.error_data) { const filteredData = { ...metadata.error_data }; delete filteredData.stderr; if (Object.keys(filteredData).length > 0) { details.push({ label: "Error Data", value: JSON.stringify(filteredData, null, 2) }); } } return details; } function StatusExpandIcon({ hasExpandableContent, isExpanded, iconClass, Icon, }: { hasExpandableContent: boolean; isExpanded: boolean; iconClass: string; Icon: React.ElementType; }) { return (
{hasExpandableContent && (isExpanded ? ( ) : ( ))}
); } function StatusProgressBar({ progress, statusLine, }: { progress: number; statusLine: string | undefined; }) { return (
{statusLine ?? "Progress"} {Math.round(progress)}%
); } function parseStatusMetadata(comment: Message) { const metadata = comment.metadata as ErrorMetadata | undefined; const progress = typeof metadata?.progress === "number" ? Math.min(Math.max(metadata.progress, 0), 100) : null; const statusLine = metadata?.stage || metadata?.status; const message = metadata?.message || comment.content || statusLine || "Status update"; const isError = comment.type === "error" || metadata?.variant === "error"; const isWarning = metadata?.variant === "warning" || metadata?.cancelled === true; return { metadata, progress, statusLine, message, isError, isWarning }; } function computeExpandableContent(isError: boolean, metadata: ErrorMetadata | undefined) { if (!isError) return { hasExpandableContent: false, errorDetails: [] as { label: string; value: string }[] }; const hasErrorDetails = metadata?.error_data || metadata?.error || metadata?.text || metadata?.stderr; if (!hasErrorDetails) return { hasExpandableContent: false, errorDetails: [] as { label: string; value: string }[] }; const errorDetails = formatErrorDetails(metadata); return { hasExpandableContent: errorDetails.length > 0, errorDetails }; } function SimpleStatusMessage({ message }: { message: string }) { return (
{message}
); } function CancelledStatusMessage({ message }: { message: string }) { return (
{message}
); } function ExpandableErrorDetails({ errorDetails, }: { errorDetails: { label: string; value: string }[]; }) { return (
{errorDetails.map((detail) => (
{detail.label}
            {detail.value}
          
))}
); } type StatusMessageBodyProps = { message: string; textClass: string; isExpanded: boolean; errorDetails: { label: string; value: string }[]; hasExpandableContent: boolean; progress: number | null; statusLine: string | undefined; }; function StatusMessageBody({ message, textClass, isExpanded, errorDetails, hasExpandableContent, progress, statusLine, }: StatusMessageBodyProps) { return (
{message || "An error occurred"}
{isExpanded && hasExpandableContent && } {progress !== null && }
); } export const StatusMessage = memo(function StatusMessage({ comment }: { comment: Message }) { const [isExpanded, setIsExpanded] = useState(false); const { metadata, progress, statusLine, message, isError, isWarning } = parseStatusMetadata(comment); const { hasExpandableContent, errorDetails } = computeExpandableContent(isError, metadata); const isSimpleStatus = !isError && !isWarning && progress === null && !statusLine && !metadata?.message; if (isSimpleStatus) return ; if (metadata?.cancelled) return ; const { Icon, iconClass, textClass } = getStatusStyle(isError, isWarning); return (
setIsExpanded((prev) => !prev) : undefined} >
); });