|
| 1 | +import React, { FC, ReactNode, useEffect, useRef, useState } from "react"; |
| 2 | +import Box from "@mui/material/Box"; |
| 3 | +import IconButton from "@mui/material/IconButton"; |
| 4 | +import Typography from "@mui/material/Typography"; |
| 5 | +import { useTheme } from "@mui/material/styles"; |
| 6 | +import { ChevronDown, ChevronUp } from "lucide-react"; |
| 7 | + |
| 8 | +import StreamingIndicator from "./StreamingIndicator"; |
| 9 | +import { preserveDisclosureExpansion } from "./disclosureScroll"; |
| 10 | + |
| 11 | +export const formatActivityDuration = (durationMs: number) => { |
| 12 | + const totalSeconds = Math.max(0, Math.floor(durationMs / 1000)); |
| 13 | + const minutes = Math.floor(totalSeconds / 60); |
| 14 | + const seconds = totalSeconds % 60; |
| 15 | + |
| 16 | + if (minutes === 0) return `${seconds}s`; |
| 17 | + return `${minutes}m ${seconds}s`; |
| 18 | +}; |
| 19 | + |
| 20 | +interface ActivitySummaryProps { |
| 21 | + children?: ReactNode; |
| 22 | + durationMs?: number; |
| 23 | + hasActivity: boolean; |
| 24 | + isStreaming: boolean; |
| 25 | + startedAt?: number; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Keeps reasoning and tool activity behind one response-level disclosure. |
| 30 | + * The final assistant prose is rendered outside this component and remains |
| 31 | + * visible when the activity is collapsed. |
| 32 | + */ |
| 33 | +const ActivitySummary: FC<ActivitySummaryProps> = ({ |
| 34 | + children, |
| 35 | + durationMs = 0, |
| 36 | + hasActivity, |
| 37 | + isStreaming, |
| 38 | + startedAt, |
| 39 | +}) => { |
| 40 | + const theme = useTheme(); |
| 41 | + const [expanded, setExpanded] = useState(false); |
| 42 | + const [elapsedMs, setElapsedMs] = useState(durationMs); |
| 43 | + const lastElapsedMsRef = useRef(durationMs); |
| 44 | + const textColor = |
| 45 | + theme.palette.mode === "dark" ? "rgba(255,255,255,0.55)" : "text.secondary"; |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + if (!isStreaming) { |
| 49 | + const completedDuration = durationMs || lastElapsedMsRef.current; |
| 50 | + setElapsedMs(completedDuration); |
| 51 | + lastElapsedMsRef.current = completedDuration; |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const started = startedAt || Date.now(); |
| 56 | + const updateElapsed = () => { |
| 57 | + const nextElapsed = Math.max(0, Date.now() - started); |
| 58 | + setElapsedMs(nextElapsed); |
| 59 | + lastElapsedMsRef.current = nextElapsed; |
| 60 | + }; |
| 61 | + updateElapsed(); |
| 62 | + const interval = window.setInterval(updateElapsed, 1000); |
| 63 | + return () => window.clearInterval(interval); |
| 64 | + }, [durationMs, isStreaming, startedAt]); |
| 65 | + |
| 66 | + const toggleExpanded = (event: React.MouseEvent<HTMLElement>) => { |
| 67 | + if (!expanded) preserveDisclosureExpansion(event.currentTarget); |
| 68 | + setExpanded((value) => !value); |
| 69 | + }; |
| 70 | + |
| 71 | + return ( |
| 72 | + <Box sx={{ my: 0.75 }}> |
| 73 | + <Box |
| 74 | + sx={{ |
| 75 | + display: "flex", |
| 76 | + alignItems: "center", |
| 77 | + minHeight: 24, |
| 78 | + color: textColor, |
| 79 | + }} |
| 80 | + > |
| 81 | + {isStreaming && <StreamingIndicator />} |
| 82 | + <Typography |
| 83 | + variant="body2" |
| 84 | + sx={{ |
| 85 | + flex: 1, |
| 86 | + fontSize: "0.76rem", |
| 87 | + color: "inherit", |
| 88 | + fontFamily: "monospace", |
| 89 | + }} |
| 90 | + > |
| 91 | + {isStreaming |
| 92 | + ? `Working… ${formatActivityDuration(elapsedMs)}` |
| 93 | + : `Worked for ${formatActivityDuration(elapsedMs)}`} |
| 94 | + </Typography> |
| 95 | + {hasActivity && ( |
| 96 | + <IconButton |
| 97 | + size="small" |
| 98 | + onClick={toggleExpanded} |
| 99 | + aria-expanded={expanded} |
| 100 | + aria-label={expanded ? "Hide work log" : "Show work log"} |
| 101 | + sx={{ |
| 102 | + p: 0, |
| 103 | + color: "inherit", |
| 104 | + "&:hover": { backgroundColor: "transparent" }, |
| 105 | + }} |
| 106 | + > |
| 107 | + {expanded ? ( |
| 108 | + <ChevronUp size={15} strokeWidth={1.8} /> |
| 109 | + ) : ( |
| 110 | + <ChevronDown size={15} strokeWidth={1.8} /> |
| 111 | + )} |
| 112 | + </IconButton> |
| 113 | + )} |
| 114 | + </Box> |
| 115 | + {expanded ? children : null} |
| 116 | + </Box> |
| 117 | + ); |
| 118 | +}; |
| 119 | + |
| 120 | +export default ActivitySummary; |
0 commit comments