All files / src/components/chat/task-header TaskTimeline.tsx

0% Statements 0/163
0% Branches 0/1
0% Functions 0/1
0% Lines 0/163

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213                                                                                                                                                                                                                                                                                                                                                                                                                                         
import { combineApiRequests } from "@shared/combineApiRequests"
import { combineCommandSequences } from "@shared/combineCommandSequences"
import { AiHydroMessage } from "@shared/ExtensionMessage"
import React, { useCallback, useEffect, useMemo, useRef } from "react"
import { Virtuoso } from "react-virtuoso"
import { COLOR_GRAY } from "../colors"
import TaskTimelineTooltip from "./TaskTimelineTooltip"
import { getColor } from "./util"
 
// Timeline dimensions and spacing
const TIMELINE_HEIGHT = "12px"
const BLOCK_WIDTH = "11px"
const BLOCK_GAP = "4px"
const _TOOLTIP_MARGIN = 32 // 32px margin on each side
 
interface TaskTimelineProps {
	messages: AiHydroMessage[]
	onBlockClick?: (messageIndex: number) => void
}
 
const TaskTimeline: React.FC<TaskTimelineProps> = ({ messages, onBlockClick }) => {
	const containerRef = useRef<HTMLDivElement>(null)
	const scrollableRef = useRef<HTMLDivElement>(null)
 
	const { taskTimelinePropsMessages, messageIndexMap } = useMemo(() => {
		if (messages.length <= 1) {
			return { taskTimelinePropsMessages: [], messageIndexMap: [] }
		}
 
		const processed = combineApiRequests(combineCommandSequences(messages.slice(1)))
		const indexMap: number[] = []
 
		const filtered = processed.filter((msg, _processedIndex) => {
			const originalIndex = messages.findIndex((originalMsg, idx) => idx > 0 && originalMsg.ts === msg.ts)
 
			// Filter out standard "say" events we don't want to show
			if (
				msg.type === "say" &&
				(msg.say === "api_req_started" ||
					msg.say === "api_req_finished" ||
					msg.say === "api_req_retried" ||
					msg.say === "deleted_api_reqs" ||
					msg.say === "checkpoint_created" ||
					msg.say === "task_progress" ||
					msg.say === "text" ||
					msg.say === "reasoning")
			) {
				return false
			}
 
			// Filter out "ask" events we don't want to show, including the duplicate completion_result
			if (
				msg.type === "ask" &&
				(msg.ask === "resume_task" || msg.ask === "resume_completed_task" || msg.ask === "completion_result") // Filter out the duplicate completion_result "ask" message
			) {
				return false
			}
			if (originalIndex !== -1) {
				indexMap.push(originalIndex)
			}
 
			return true
		})
		return { taskTimelinePropsMessages: filtered, messageIndexMap: indexMap }
	}, [messages])
 
	useEffect(() => {
		if (scrollableRef.current && taskTimelinePropsMessages.length > 0) {
			scrollableRef.current.scrollLeft = scrollableRef.current.scrollWidth
		}
	}, [taskTimelinePropsMessages])
 
	// Calculate the item size (width of block + gap)
	const itemWidth = parseInt(BLOCK_WIDTH.replace("px", "")) + parseInt(BLOCK_GAP.replace("px", ""))
 
	// Virtuoso requires a reference to scroll to the end
	const virtuosoRef = useRef<any>(null)
 
	// Render a timeline block
	const TimelineBlock = useCallback(
		(index: number) => {
			// Show placeholder block when no items exist
			if (taskTimelinePropsMessages.length === 0 || index >= taskTimelinePropsMessages.length) {
				return (
					<div
						style={{
							width: BLOCK_WIDTH,
							height: BLOCK_WIDTH,
							backgroundColor: "#e5e5e5", // Light gray placeholder
							flexShrink: 0,
							marginRight: BLOCK_GAP,
							opacity: 0.5,
							borderRadius: "50%",
						}}
					/>
				)
			}
 
			const message = taskTimelinePropsMessages[index]
			const originalMessageIndex = messageIndexMap[index]
 
			const handleClick = () => {
				if (onBlockClick && originalMessageIndex !== undefined) {
					onBlockClick(originalMessageIndex)
				}
			}
 
			return (
				<TaskTimelineTooltip message={message}>
					<div
						className="hover:brightness-120"
						onClick={handleClick}
						style={{
							width: BLOCK_WIDTH,
							height: BLOCK_WIDTH,
							backgroundColor: getColor(message),
							flexShrink: 0,
							cursor: "pointer",
							marginRight: BLOCK_GAP,
							borderRadius: 1.5,
						}}
					/>
				</TaskTimelineTooltip>
			)
		},
		[taskTimelinePropsMessages, messageIndexMap, onBlockClick],
	)
 
	// Scroll to the end when messages change
	useEffect(() => {
		if (virtuosoRef.current && taskTimelinePropsMessages.length > 0) {
			virtuosoRef.current.scrollToIndex({
				index: taskTimelinePropsMessages.length - 1,
				align: "end",
			})
		}
	}, [taskTimelinePropsMessages])
 
	if (taskTimelinePropsMessages.length === 0) {
		return (
			<div
				ref={containerRef}
				style={{
					position: "relative",
					width: "100%",
					marginBottom: "4px",
					overflow: "hidden",
				}}>
				<div
					style={{
						height: TIMELINE_HEIGHT,
						width: "100%",
						display: "flex",
						alignItems: "center",
					}}>
					<div
						style={{
							width: BLOCK_WIDTH,
							height: BLOCK_WIDTH,
							backgroundColor: COLOR_GRAY,
							borderRadius: "50%",
							opacity: 0.5,
							flexShrink: 0,
							marginRight: BLOCK_GAP,
						}}
					/>
				</div>
			</div>
		)
	}
 
	return (
		<div
			ref={containerRef}
			style={{
				position: "relative",
				width: "100%",
				height: TIMELINE_HEIGHT,
				marginBottom: "4px",
			}}>
			<style>
				{`
					/* Hide scrollbar for Chrome, Safari and Opera */
					.timeline-virtuoso::-webkit-scrollbar {
						display: none;
					}
					.timeline-virtuoso {
						scrollbar-width: none;
						-ms-overflow-style: none;
					}
				`}
			</style>
 
			<Virtuoso
				className="timeline-virtuoso"
				fixedItemHeight={itemWidth}
				horizontalDirection={true}
				increaseViewportBy={12}
				itemContent={TimelineBlock}
				ref={virtuosoRef}
				style={{
					height: TIMELINE_HEIGHT,
					width: "100%",
					//overflowY: "hidden",
				}}
				totalCount={Math.max(1, taskTimelinePropsMessages.length)}
			/>
		</div>
	)
}
 
export default TaskTimeline