Spaces:
Running
Running
File size: 14,841 Bytes
67b042f 26f5120 67b042f 26f5120 67b042f 26f5120 67b042f 26f5120 67b042f 09644e5 67b042f 09644e5 67b042f 09644e5 67b042f 26f5120 67b042f 26f5120 67b042f 26f5120 67b042f 26f5120 67b042f 26f5120 67b042f |
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
import React, { useRef, useEffect } from 'react';
import { AgentTrace } from '@/types/agent';
import { Box, Typography, Stack, Paper } from '@mui/material';
import { StepCard } from './StepCard';
import { FinalStepCard } from './FinalStepCard';
import { ThinkingStepCard } from './ThinkingStepCard';
import { ConnectionStepCard } from './ConnectionStepCard';
import ListAltIcon from '@mui/icons-material/ListAlt';
import FormatListNumberedIcon from '@mui/icons-material/FormatListNumbered';
import { useAgentStore, selectSelectedStepIndex, selectFinalStep, selectIsConnectingToE2B, selectIsAgentProcessing } from '@/stores/agentStore';
interface StepsListProps {
trace?: AgentTrace;
}
export const StepsList: React.FC<StepsListProps> = ({ trace }) => {
const containerRef = useRef<HTMLDivElement>(null);
const selectedStepIndex = useAgentStore(selectSelectedStepIndex);
const setSelectedStepIndex = useAgentStore((state) => state.setSelectedStepIndex);
const finalStep = useAgentStore(selectFinalStep);
const isConnectingToE2B = useAgentStore(selectIsConnectingToE2B);
const isAgentProcessing = useAgentStore(selectIsAgentProcessing);
const isScrollingProgrammatically = useRef(false);
const [showThinkingCard, setShowThinkingCard] = React.useState(false);
const thinkingTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const streamStartTimeRef = useRef<number | null>(null);
const [showConnectionCard, setShowConnectionCard] = React.useState(false);
const hasConnectedRef = useRef(false);
// Check if final step is active (when selectedStepIndex is null and finalStep exists and trace is not running)
const isFinalStepActive = selectedStepIndex === null && finalStep && !trace?.isRunning;
// Check if thinking card is active (when in live mode and thinking card is shown)
const isThinkingCardActive = selectedStepIndex === null && showThinkingCard;
// Determine the active step index
// If a specific step is selected, use that
// If the final step is active, no normal step should be active
// If the thinking card is active, no normal step should be active
// Otherwise, show the last step as active
const activeStepIndex = selectedStepIndex !== null
? selectedStepIndex
: isFinalStepActive
? null // When final step is active, no normal step is active
: isThinkingCardActive
? null // When thinking card is active, no normal step is active
: (trace?.steps && trace.steps.length > 0 && trace?.isRunning)
? trace.steps.length - 1
: (trace?.steps && trace.steps.length > 0)
? trace.steps.length - 1
: null;
// Manage ConnectionStepCard display:
// - Shows when isConnectingToE2B = true OR when we had a connection
// - Remains visible even when task is finished (if we have steps or finalStep)
useEffect(() => {
if (isConnectingToE2B || isAgentProcessing || (trace?.steps && trace.steps.length > 0) || finalStep) {
setShowConnectionCard(true);
hasConnectedRef.current = true;
}
}, [isConnectingToE2B, isAgentProcessing, trace?.steps, finalStep]);
// Manage ThinkingCard display:
// - Appears 5 seconds AFTER stream starts (isAgentProcessing = true, NOT during isConnectingToE2B)
// - Remains visible during the entire agent processing
// - Hides only when agent stops OR a finalStep exists
useEffect(() => {
// If stream really starts (isAgentProcessing = true and NOT connecting)
// And no startTime recorded yet
if (isAgentProcessing && !isConnectingToE2B && !streamStartTimeRef.current) {
streamStartTimeRef.current = Date.now();
}
// If agent stops OR we have a finalStep, reset and hide
if (!isAgentProcessing || finalStep) {
streamStartTimeRef.current = null;
setShowThinkingCard(false);
if (thinkingTimeoutRef.current) {
clearTimeout(thinkingTimeoutRef.current);
thinkingTimeoutRef.current = null;
}
return;
}
// If agent is running, not connecting, no finalStep: start 5 second timer
if (isAgentProcessing && !isConnectingToE2B && !finalStep && streamStartTimeRef.current) {
// Clean up any existing timeout
if (thinkingTimeoutRef.current) {
clearTimeout(thinkingTimeoutRef.current);
}
// Calculate elapsed time since stream started
const elapsedTime = Date.now() - streamStartTimeRef.current;
const remainingTime = Math.max(0, 5000 - elapsedTime);
thinkingTimeoutRef.current = setTimeout(() => {
setShowThinkingCard(true);
}, remainingTime);
}
// Cleanup on unmount or when dependencies change
return () => {
if (thinkingTimeoutRef.current) {
clearTimeout(thinkingTimeoutRef.current);
thinkingTimeoutRef.current = null;
}
};
}, [isAgentProcessing, isConnectingToE2B, finalStep]);
// Auto-scroll logic
useEffect(() => {
const container = containerRef.current;
if (!container) return;
isScrollingProgrammatically.current = true;
// Use setTimeout to ensure DOM has updated
setTimeout(() => {
if (!container) return;
// LIVE MODE: Always scroll to the bottom (last visible element)
if (selectedStepIndex === null) {
// Scroll to bottom
container.scrollTo({
top: container.scrollHeight,
behavior: 'smooth',
});
}
// NON-LIVE MODE: Scroll to selected step
else {
const selectedElement = container.querySelector(`[data-step-index="${selectedStepIndex}"]`);
if (selectedElement) {
selectedElement.scrollIntoView({
behavior: 'smooth',
block: 'center',
});
}
}
// Reset flag after scroll animation
setTimeout(() => {
isScrollingProgrammatically.current = false;
}, 500);
}, 100);
}, [selectedStepIndex, trace?.steps?.length, showThinkingCard, finalStep]);
// Detect which step is visible when scrolling (steps → timeline)
useEffect(() => {
const container = containerRef.current;
if (!container || !trace?.steps || trace.steps.length === 0) return;
const handleScroll = () => {
// Don't update if we're scrolling programmatically
if (isScrollingProgrammatically.current) return;
// Don't update if agent is running (stay in live mode)
if (trace?.isRunning) return;
const containerRect = container.getBoundingClientRect();
const containerTop = containerRect.top;
const containerBottom = containerRect.bottom;
const containerCenter = containerRect.top + containerRect.height / 2;
// Check scroll position
const isAtTop = container.scrollTop <= 5; // 5px tolerance
const isAtBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - 5; // 5px tolerance
let targetStepIndex: number | null = -1;
let targetDistance = Infinity;
let isFinalStepTarget = false;
if (isAtTop) {
// At the top: find the highest visible step
let highestVisibleBottom = Infinity;
trace.steps.forEach((_, index) => {
const stepElement = container.querySelector(`[data-step-index="${index}"]`);
if (stepElement) {
const stepRect = stepElement.getBoundingClientRect();
const stepTop = stepRect.top;
const stepBottom = stepRect.bottom;
const isVisible = stepTop < containerBottom && stepBottom > containerTop;
if (isVisible && stepTop < highestVisibleBottom) {
highestVisibleBottom = stepTop;
targetStepIndex = index;
isFinalStepTarget = false;
}
}
});
} else if (isAtBottom) {
// At the bottom: find the lowest visible step
let lowestVisibleTop = -Infinity;
trace.steps.forEach((_, index) => {
const stepElement = container.querySelector(`[data-step-index="${index}"]`);
if (stepElement) {
const stepRect = stepElement.getBoundingClientRect();
const stepTop = stepRect.top;
const stepBottom = stepRect.bottom;
const isVisible = stepTop < containerBottom && stepBottom > containerTop;
if (isVisible && stepTop > lowestVisibleTop) {
lowestVisibleTop = stepTop;
targetStepIndex = index;
isFinalStepTarget = false;
}
}
});
// Check if final step is the lowest visible
if (finalStep) {
const finalStepElement = container.querySelector(`[data-step-index="final"]`);
if (finalStepElement) {
const finalStepRect = finalStepElement.getBoundingClientRect();
const finalStepTop = finalStepRect.top;
const finalStepBottom = finalStepRect.bottom;
const isVisible = finalStepTop < containerBottom && finalStepBottom > containerTop;
if (isVisible && finalStepTop > lowestVisibleTop) {
targetStepIndex = null;
isFinalStepTarget = true;
}
}
}
} else {
// Not at bottom: find the step closest to center
trace.steps.forEach((_, index) => {
const stepElement = container.querySelector(`[data-step-index="${index}"]`);
if (stepElement) {
const stepRect = stepElement.getBoundingClientRect();
const stepCenter = stepRect.top + stepRect.height / 2;
const distance = Math.abs(containerCenter - stepCenter);
if (distance < targetDistance) {
targetDistance = distance;
targetStepIndex = index;
isFinalStepTarget = false;
}
}
});
// Check if final step is closest to center
if (finalStep) {
const finalStepElement = container.querySelector(`[data-step-index="final"]`);
if (finalStepElement) {
const finalStepRect = finalStepElement.getBoundingClientRect();
const finalStepCenter = finalStepRect.top + finalStepRect.height / 2;
const distance = Math.abs(containerCenter - finalStepCenter);
if (distance < targetDistance) {
targetStepIndex = null;
isFinalStepTarget = true;
}
}
}
}
// Update the selected step if changed
if (isFinalStepTarget && selectedStepIndex !== null) {
setSelectedStepIndex(null);
} else if (!isFinalStepTarget && targetStepIndex !== -1 && targetStepIndex !== selectedStepIndex) {
setSelectedStepIndex(targetStepIndex);
}
};
// Throttle scroll events
let scrollTimeout: NodeJS.Timeout;
const throttledScroll = () => {
clearTimeout(scrollTimeout);
scrollTimeout = setTimeout(handleScroll, 150);
};
container.addEventListener('scroll', throttledScroll);
return () => {
container.removeEventListener('scroll', throttledScroll);
clearTimeout(scrollTimeout);
};
}, [trace?.steps, selectedStepIndex, setSelectedStepIndex, finalStep]);
return (
<Paper
elevation={0}
sx={{
width: { xs: '100%', md: 320 },
flexShrink: 0,
display: 'flex',
flexDirection: 'column',
ml: { xs: 0, md: 1.5 },
mt: { xs: 3, md: 0 },
overflow: 'hidden',
}}
>
<Box sx={{ px: 2, py: 1.5, borderBottom: '1px solid', borderColor: 'divider', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
<Typography variant="h6" sx={{ fontSize: '0.9rem', fontWeight: 700, color: 'text.primary' }}>
Steps
</Typography>
{trace?.traceMetadata && trace.traceMetadata.numberOfSteps > 0 && (
<Box sx={{ display: 'flex', alignItems: 'center', gap: 0 }}>
<Typography
variant="caption"
sx={{
fontSize: '0.75rem',
fontWeight: 700,
color: 'text.primary',
}}
>
{trace.traceMetadata.numberOfSteps}
</Typography>
<Typography
variant="caption"
sx={{
fontSize: '0.75rem',
fontWeight: 700,
color: 'text.disabled',
}}
>
/{trace.traceMetadata.maxSteps}
</Typography>
</Box>
)}
</Box>
<Box
ref={containerRef}
sx={{
flex: 1,
overflowY: 'auto',
minHeight: 0,
p: 2,
}}
>
{(trace?.steps && trace.steps.length > 0) || finalStep || showThinkingCard || showConnectionCard ? (
<Stack spacing={2.5}>
{/* Show connection step card (first item) */}
{showConnectionCard && (
<Box data-step-index="connection">
<ConnectionStepCard isConnecting={isConnectingToE2B} />
</Box>
)}
{/* Show all steps */}
{trace?.steps && trace.steps.map((step, index) => (
<Box key={step.stepId} data-step-index={index}>
<StepCard
step={step}
index={index}
isLatest={index === trace.steps!.length - 1}
isActive={index === activeStepIndex}
/>
</Box>
))}
{/* Show thinking indicator after steps (appears 5 seconds after stream start) */}
{showThinkingCard && (
<Box data-step-index="thinking">
<ThinkingStepCard isActive={isThinkingCardActive} />
</Box>
)}
{/* Show final step card if exists */}
{finalStep && (
<Box data-step-index="final">
<FinalStepCard
finalStep={finalStep}
isActive={isFinalStepActive}
/>
</Box>
)}
</Stack>
) : (
<Box
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
height: '100%',
color: 'text.secondary',
p: 3,
textAlign: 'center',
}}
>
<ListAltIcon sx={{ fontSize: 48, mb: 2, opacity: 0.5 }} />
<Typography variant="body1" sx={{ fontWeight: 600, mb: 0.5 }}>
No steps yet
</Typography>
<Typography variant="caption" sx={{ fontSize: '0.75rem' }}>
Steps will appear as the agent progresses
</Typography>
</Box>
)}
</Box>
</Paper>
);
};
|