import { useLocaleContext } from "@arcblock/ux/lib/Locale/context"; import AccessTimeIcon from "@mui/icons-material/AccessTime"; import ChevronRightIcon from "@mui/icons-material/ChevronRight"; import ErrorIcon from "@mui/icons-material/Error"; import ExpandMoreIcon from "@mui/icons-material/ExpandMore"; import UnfoldLessIcon from "@mui/icons-material/UnfoldLess"; import UnfoldMoreIcon from "@mui/icons-material/UnfoldMore"; import { Box, Button, IconButton, Typography } from "@mui/material"; import { type ReactElement, useState } from "react"; import Status from "../../components/status.tsx"; import { parseDurationMs, parseDurationTime } from "../../utils/latency.ts"; import { AgentTag } from "./agent-tag.tsx"; import type { TraceData } from "./types.ts"; type TraceItemProps = { name: string; taskTitle?: string; duration: number; selected?: boolean; depth?: number; onSelect?: () => void; agentTag?: string; model?: string; hasChildren?: boolean; isExpanded?: boolean; onToggleExpand?: () => void; status?: { code: number; message: string; }; totalDuration: number; start: number; }; function TraceItem({ name, taskTitle, duration, selected, depth = 0, onSelect, agentTag, model, hasChildren, isExpanded, onToggleExpand, status, totalDuration, start = 0, }: TraceItemProps) { const hasError = status?.code === 2; const isPending = status?.code === 0; const widthPercent = totalDuration ? Math.min(Math.max((duration / totalDuration) * 100 || 0, 0.5), 100) : 0; let marginLeftPercent = totalDuration ? Math.min((start / totalDuration) * 100, 100) : 0; if (marginLeftPercent >= 100) { marginLeftPercent -= widthPercent; } return ( 0 ? { content: '""', position: "absolute", left: `${marginLeftPercent}%`, top: 0, width: `${widthPercent}%`, height: "100%", backgroundColor: "primary.main", opacity: 0.04, borderRadius: 0.5, zIndex: 0, } : {}, }} onClick={() => onSelect?.()} > {hasChildren ? ( { e.stopPropagation(); onToggleExpand?.(); }} sx={{ p: 0 }} > {isExpanded ? ( ) : ( )} ) : ( )} {name} {taskTitle && ( {`(${taskTitle})`} )} {hasError && ( )} {isPending && } {parseDurationTime(duration * 1000)} ); } type TraceStep = { name: string; duration: number; selected?: boolean; children?: TraceStep[]; start?: number; run?: TraceData; agentTag?: string; totalDuration?: number; status?: { code: number; message: string; }; }; export function formatTraceStepsAndTotalDuration({ steps, start = 0, selectedTrace, parallel = false, }: { steps: TraceData[]; start: number; selectedTrace?: TraceData | null; parallel?: boolean; }): TraceStep[] { let current = start; return steps.map((step, index) => { const isSameStartTimeWithNextStep = steps[index + 1] && steps[index + 1].startTime && step.startTime && Math.abs(step.startTime - (steps[index + 1].startTime ?? 0)) <= 1; let children: TraceStep[] | undefined; let childrenTotal = 0; if (step.children?.length) { children = formatTraceStepsAndTotalDuration({ steps: step.children, start: current, selectedTrace, }); childrenTotal = children.reduce((sum, c) => sum + (c.totalDuration ?? 0), 0); } const duration = parseDurationMs(step.startTime, step.endTime); const isParallel = Boolean(children?.length ? childrenTotal > duration : false); const maxDuration = Math.max( ...(children || []).map((c) => c.totalDuration ?? c.duration), duration, ); if (isParallel) { children = formatTraceStepsAndTotalDuration({ steps: step.children ?? [], start: current, selectedTrace, parallel: isParallel, }); } const totalDuration = duration; const annotated = { maxDuration, ...step, selected: step.id === selectedTrace?.id, start: current, duration, children, run: step, agentTag: step.attributes?.agentTag, totalDuration, status: step.status, }; if (!isSameStartTimeWithNextStep && !parallel) { current += annotated.duration; } return annotated; }); } export function renderTraceItems({ traceId, items, totalDuration, depth = 0, onSelect, expandedItems, onToggleExpand, parentPath = "", }: { traceId: string; items: TraceStep[]; totalDuration: number; depth?: number; onSelect?: (step?: TraceData) => void; expandedItems: Set; onToggleExpand: (itemKey: string) => void; parentPath?: string; }): ReactElement[] { return items.flatMap((item, index) => { const itemPath = parentPath ? `${parentPath}-${index}` : `${index}`; const itemKey = item.run?.id || `${traceId}-${itemPath}`; const isExpanded = expandedItems.has(itemKey); const hasChildren = !!item.children && item.children.length > 0; return [ onSelect?.(item.run)} hasChildren={hasChildren} isExpanded={isExpanded} onToggleExpand={() => onToggleExpand(itemKey)} status={item.status} totalDuration={totalDuration} start={item.start ?? 0} />, ...(hasChildren && isExpanded && item.children ? renderTraceItems({ traceId, items: item.children, totalDuration, depth: depth + 1, onSelect, expandedItems, onToggleExpand, parentPath: itemPath, }) : []), ]; }); } export default function TraceItemList({ traceId, steps, onSelect, selectedTrace, }: { traceId: string; steps: TraceData[]; onSelect?: (step?: TraceData) => void; selectedTrace?: TraceData | null; }) { const { t } = useLocaleContext(); const traceSteps = formatTraceStepsAndTotalDuration({ steps, start: 0, selectedTrace }); const collectAllExpandableKeys = (items: TraceStep[], parentPath = ""): Set => { const allKeys = new Set(); const collectKeys = (items: TraceStep[], parentPath: string) => { items.forEach((item, index) => { if (item.children && item.children.length > 0) { const itemPath = parentPath ? `${parentPath}-${index}` : `${index}`; const itemKey = item.run?.id || `${traceId}-${itemPath}`; allKeys.add(itemKey); collectKeys(item.children, itemPath); } }); }; collectKeys(items, parentPath); return allKeys; }; const [expandedItems, setExpandedItems] = useState>(() => collectAllExpandableKeys(traceSteps), ); const toggleExpand = (itemKey: string) => { setExpandedItems((prev) => { const next = new Set(prev); if (next.has(itemKey)) { next.delete(itemKey); } else { next.add(itemKey); } return next; }); }; const expandAll = () => { setExpandedItems(collectAllExpandableKeys(traceSteps)); }; const collapseAll = () => { setExpandedItems(new Set()); }; return ( {t("traceTimeline")} {renderTraceItems({ traceId, items: traceSteps, totalDuration: traceSteps[0]?.totalDuration ?? 0, depth: 0, onSelect, expandedItems, onToggleExpand: toggleExpand, })} ); }