import React, { useMemo, useState } from "react"; import { ChevronRight } from "lucide-react"; import type { ThinkingTimelineEntry } from "../../../types/chatStream"; import { cn } from "../../../utils/classNames"; interface AssistantThinkingSummaryProps { label: string; timeline?: ThinkingTimelineEntry[]; isActive?: boolean; defaultExpanded?: boolean; } const visibleEntries = (timeline: ThinkingTimelineEntry[]) => ( timeline.filter((entry) => { if (entry.kind === 'tool') return entry.label.trim(); if (entry.kind === 'work_step') return entry.label.trim(); return false; }) ); const entryLabel = (entry: ThinkingTimelineEntry) => entry.label; const entryState = (entry: ThinkingTimelineEntry) => { if (entry.kind === 'tool' || entry.kind === 'work_step') return entry.state; return 'done'; }; const isReasoningEntry = (entry: ThinkingTimelineEntry) => ( entry.kind === 'work_step' && entry.source === 'reasoning_summary' ); const compactLabel = (label: string) => { const normalized = label.replace(/\s+/g, ' ').trim(); if (normalized.length <= 150) return normalized; const boundary = normalized.lastIndexOf(' ', 146); return `${normalized.slice(0, boundary > 48 ? boundary : 147).trim()}...`; }; // Marker shown on the timeline rail for a tool / work step. const StepMarker = ({ state }: { state: 'running' | 'done' | 'error' }) => { if (state === 'running') { return ( ); } if (state === 'error') { return ( ); } return ( ); }; const AssistantThinkingSummary: React.FC = ({ label, timeline = [], isActive = false, defaultExpanded = false, }) => { const [isExpandedState, setIsExpanded] = useState(defaultExpanded); const entries = useMemo(() => visibleEntries(timeline), [timeline]); const latestReasoningIndex = useMemo(() => { for (let index = entries.length - 1; index >= 0; index -= 1) { if (isReasoningEntry(entries[index])) return index; } return -1; }, [entries]); const isCollapsible = !isActive && entries.length > 0; const isExpanded = isActive || isExpandedState; return (
{isExpanded && entries.length > 0 && (
)}
); }; export default React.memo(AssistantThinkingSummary);