diff --git a/client/src/components/Chat/Trace/Ledger.tsx b/client/src/components/Chat/Trace/Ledger.tsx index 2631c73b86..e979cc082a 100644 --- a/client/src/components/Chat/Trace/Ledger.tsx +++ b/client/src/components/Chat/Trace/Ledger.tsx @@ -1,18 +1,29 @@ import { memo, useId, useMemo, useState } from 'react'; import { ChevronRight, CircleAlert, CircleDashed } from 'lucide-react'; import type { RefObject, CSSProperties, KeyboardEvent } from 'react'; -import type { TraceModel, TraceNode, TraceRow, TraceTurn, TraceWindow } from './model'; +import type { + TraceRow, + TraceNode, + TraceStep, + TraceTurn, + TraceSpan, + TraceModel, + TraceScale, + TraceWindow, +} from './model'; +import { spanOf, stepSpan, turnSpan, turnKey, boundsOf } from './model'; import { useTraceFormat, recordDurationText } from './format'; import { KIND_APPEARANCE, STATUS_LABEL } from './kinds'; import { formatTokens } from '~/utils/tokens'; import { useRowWindow } from './virtual'; import { useLocalize } from '~/hooks'; -import { turnKey } from './model'; import { cn } from '~/utils'; const ROW_HEIGHT = 32; const HEADER_HEIGHT = 28; const INDENT_PX = 14; +/** Tool names a step header lists before summarizing the rest as a count. */ +const STEP_TOOL_NAMES = 3; /** Shown only while the tree itself has keyboard focus, so a pointer user sees just the selection. */ const ACTIVE_RING = 'group-focus-visible/tree:ring-2 group-focus-visible/tree:ring-inset group-focus-visible/tree:ring-ring-primary'; @@ -21,17 +32,25 @@ const GRID = type Domain = { start: number; span: number }; -const barStyle = (start: number, end: number, domain: Domain): CSSProperties => ({ - left: `${((start - domain.start) / domain.span) * 100}%`, - width: `${((end - start) / domain.span) * 100}%`, +const barStyle = (span: TraceSpan, domain: Domain): CSSProperties => ({ + left: `${((span.start - domain.start) / domain.span) * 100}%`, + width: `${((span.end - span.start) / domain.span) * 100}%`, }); -function RecordBar({ node, domain }: { node: TraceNode; domain: Domain }) { +function RecordBar({ + node, + scale, + domain, +}: { + node: TraceNode; + scale: TraceScale; + domain: Domain; +}) { const { record } = node; const appearance = KIND_APPEARANCE[record.kind]; const solid = record.status === 'error' ? 'bg-status-error' : appearance.bar; - if (node.end == null) { + if (scale === 'time' && node.end == null) { return ( 0 ? (node.firstToken - node.start) / duration : 0; + scale === 'time' && node.firstToken != null && duration > 0 + ? (node.firstToken - node.start) / duration + : 0; return ( {ttftShare > 0 && ( ); } @@ -80,33 +102,37 @@ function recordTokens(node: TraceNode): string { return total > 0 ? formatTokens(total) : ''; } -const toDomain = (start: number, end: number): Domain => ({ - start, - span: Math.max(end - start, 1), +const toDomain = (span: TraceSpan): Domain => ({ + start: span.start, + span: Math.max(span.end - span.start, 1), }); /** - * The hierarchical record list: one row per turn and per record. Each bar is - * scaled to its own response, so a short turn in a long conversation stays - * legible, until an interval is focused and every row shares that scale. Rows - * are windowed, and the tree follows the ARIA tree pattern through + * The hierarchical record list: one row per turn, per step and per record. Each + * bar is scaled to its own response, so a short turn in a long conversation + * stays legible, until an interval is focused and every row shares that scale. + * Rows are windowed, and the tree follows the ARIA tree pattern through * `aria-activedescendant`, which must name a mounted row, so the active row * stays rendered while the window scrolls away from it. */ function Ledger({ rows, model, + scale, view, selectedId, treeRef, + previewFor, onSelect, onToggle, }: { rows: TraceRow[]; model: TraceModel; + scale: TraceScale; view: TraceWindow | null; selectedId: string | null; treeRef: RefObject; + previewFor: (node: TraceNode) => string | undefined; onSelect: (id: string) => void; onToggle: (key: string) => void; }) { @@ -121,15 +147,18 @@ function Ledger({ const indexByKey = useMemo(() => new Map(rows.map((row, index) => [row.key, index])), [rows]); const turnDomains = useMemo( - () => new Map(model.turns.map((turn) => [turn.messageId, toDomain(turn.start, turn.end)])), - [model], + () => new Map(model.turns.map((turn) => [turn.messageId, toDomain(turnSpan(turn, scale))])), + [model, scale], ); - const viewDomain = useMemo(() => (view ? toDomain(view.start, view.end) : null), [view]); + const viewDomain = useMemo(() => (view ? toDomain(view) : null), [view]); + const bounds = boundsOf(model, scale); const domainFor = (messageId: string): Domain => - viewDomain ?? turnDomains.get(messageId) ?? toDomain(model.start, model.end); + viewDomain ?? turnDomains.get(messageId) ?? toDomain(bounds); const activeIndex = activeKey != null ? (indexByKey.get(activeKey) ?? 0) : 0; const activeRow = rows[activeIndex]; const rowId = (key: string) => `${idPrefix}-${key}`; + const position = (value: number) => + scale === 'sequence' ? String(Math.round(value)) : format.duration(value - bounds.start); /** Keeps a keyboard-moved row inside the viewport so its windowed element mounts. */ const scrollIntoView = (index: number) => { @@ -163,11 +192,23 @@ function Ledger({ } }; + const parentKeyOf = (row: TraceRow): string | undefined => { + if (row.type === 'turn') { + return undefined; + } + if (row.type === 'step') { + return turnKey(row.step.messageId); + } + const { node } = row; + const stepParent = model.mode === 'simple' ? node.stepKey : null; + return node.viewParentId ?? stepParent ?? turnKey(node.record.messageId); + }; + const handleKeyDown = (event: KeyboardEvent) => { if (!activeRow) { return; } - const expandable = activeRow.type === 'turn' || activeRow.hasChildren; + const expandable = activeRow.type !== 'record' || activeRow.hasChildren; const pageSize = Math.max(1, Math.floor((scrollRef.current?.clientHeight ?? 0) / ROW_HEIGHT)); const handlers: Record void> = { ArrowDown: () => moveTo(activeIndex + 1), @@ -188,13 +229,10 @@ function Ledger({ onToggle(activeRow.key); return; } - if (activeRow.type === 'record') { - const { node } = activeRow; - const parentKey = node.parentId ?? turnKey(node.record.messageId); - const parentIndex = indexByKey.get(parentKey); - if (parentIndex != null) { - moveTo(parentIndex); - } + const parentKey = parentKeyOf(activeRow); + const parentIndex = parentKey != null ? indexByKey.get(parentKey) : undefined; + if (parentIndex != null) { + moveTo(parentIndex); } }, Enter: () => activate(activeRow), @@ -207,61 +245,142 @@ function Ledger({ } }; - const renderRow = (row: TraceRow, index: number) => { - const style: CSSProperties = { top: HEADER_HEIGHT + index * ROW_HEIGHT, height: ROW_HEIGHT }; - const active = activeRow?.key === row.key; - const common = { - id: rowId(row.key), - role: 'treeitem', - 'aria-posinset': row.position, - 'aria-setsize': row.setSize, - style, - onClick: () => activate(row), - }; + const chevron = (expanded: boolean) => ( +