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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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import React, { useRef, useEffect } from 'react';
import { Box, Typography, CircularProgress, Button } from '@mui/material';
import CheckIcon from '@mui/icons-material/Check';
import CloseIcon from '@mui/icons-material/Close';
import StopCircleIcon from '@mui/icons-material/StopCircle';
import HourglassEmptyIcon from '@mui/icons-material/HourglassEmpty';
import AccessTimeIcon from '@mui/icons-material/AccessTime';
import CableIcon from '@mui/icons-material/Cable';
import { AgentTraceMetadata } from '@/types/agent';
import { useAgentStore, selectSelectedStepIndex, selectFinalStep, selectIsConnectingToE2B, selectIsAgentProcessing } from '@/stores/agentStore';

interface TimelineProps {
  metadata: AgentTraceMetadata;
  isRunning: boolean;
}

export const Timeline: React.FC<TimelineProps> = ({ metadata, isRunning }) => {
  const timelineRef = 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);

  // Show connection indicator if connecting or if we have started processing
  const showConnectionIndicator = isConnectingToE2B || isAgentProcessing || (metadata.numberOfSteps > 0) || finalStep;

  // Generate array of steps with their status
  // Only show completed steps + current step if running
  const totalStepsToShow = isRunning && !isConnectingToE2B
    ? metadata.numberOfSteps + 1  // Show completed steps + current step
    : metadata.numberOfSteps;     // Show only completed steps when not running

  // Calculate total width for the line (including finalStep if present)
  const lineWidth = finalStep
    ? `calc(${totalStepsToShow} * (40px + 12px) + 52px)` // Add space for finalStep (40px + 12px gap)
    : `calc(${totalStepsToShow} * (40px + 12px))`;

  const steps = Array.from({ length: totalStepsToShow }, (_, index) => ({
    stepNumber: index + 1,
    stepIndex: index,
    isCompleted: index < metadata.numberOfSteps,
    // Step is current if: we're at the right index AND running AND not connecting to E2B
    isCurrent: (index === metadata.numberOfSteps && isRunning && !isConnectingToE2B) ||
               (index === 0 && metadata.numberOfSteps === 0 && isRunning && !isConnectingToE2B),
    isSelected: selectedStepIndex === index,
  }));

  // Handle step click
  const handleStepClick = (stepIndex: number, isCompleted: boolean, isCurrent: boolean) => {
    if (isCompleted) {
      setSelectedStepIndex(stepIndex);
    } else if (isCurrent) {
      // Clicking on the current step (with animation) goes back to live mode
      setSelectedStepIndex(null);
    }
  };

  // Handle final step click (goes to live mode showing the final status)
  const handleFinalStepClick = () => {
    setSelectedStepIndex(null);
  };

  // Auto-scroll to current step while running
  useEffect(() => {
    if (timelineRef.current && isRunning) {
      // Only auto-scroll while running, not when finished
      const currentStepElement = timelineRef.current.querySelector(`[data-step="${metadata.numberOfSteps}"]`);
      if (currentStepElement) {
        currentStepElement.scrollIntoView({ behavior: 'smooth', inline: 'center', block: 'nearest' });
      }
    }
  }, [metadata.numberOfSteps, isRunning]);

  return (
    <Box

      sx={{

        p: 2,

        border: '1px solid',

        borderColor: 'divider',

        borderRadius: '12px',

        backgroundColor: 'background.paper',

        flexShrink: 0,

      }}

    >

      <Box sx={{ display: 'flex', flexDirection: 'column', gap: 1.5 }}>

        {/* Header with step count */}

        <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>

          <Typography variant="h6" sx={{ fontSize: '0.9rem', fontWeight: 700, color: 'text.primary' }}>

            Timeline

            {selectedStepIndex !== null && (

              <Typography component="span" sx={{ ml: 1, color: 'text.secondary', fontWeight: 500, fontSize: '0.65rem' }}>

                - Viewing step {selectedStepIndex + 1}

              </Typography>

            )}

          </Typography>

          {selectedStepIndex !== null && (

            <Button

              size="small"

              variant="outlined"

              onClick={handleFinalStepClick}

              sx={{

                textTransform: 'none',

                fontSize: '0.7rem',

                fontWeight: 600,

                px: 1.5,

                py: 0.25,

                minWidth: 'auto',

                color: 'text.secondary',

                borderColor: 'divider',

                '&:hover': {

                  backgroundColor: (theme) => theme.palette.mode === 'dark' ? 'rgba(255,255,255,0.05)' : 'rgba(0,0,0,0.03)',

                  borderColor: 'text.secondary',

                },

              }}

            >

              Back to latest step

            </Button>

          )}

        </Box>



        {/* Horizontal scrollable step indicators */}

        <Box

          ref={timelineRef}

          sx={{

            display: 'flex',

            alignItems: 'center',

            overflowX: 'auto',

            overflowY: 'hidden',

            gap: 1.5,

            py: 1.5,

            height: 60,

            position: 'relative',

            // Hide scrollbar completely

            scrollbarWidth: 'none', // Firefox

            '&::-webkit-scrollbar': {

              display: 'none', // Chrome, Safari, Edge

            },

            // Horizontal line crossing through circles

            '&::before': {

              content: '""',

              position: 'absolute',

              left: "25px",

              // Calculate width to cover visible steps + finalStep if present

              width: lineWidth,

              top: '19.5px',

              transform: 'translateY(-50%)',

              transition: 'width 0.6s cubic-bezier(0.4, 0, 0.2, 1)',

              height: '2px',

              backgroundColor: (theme) => theme.palette.mode === 'dark' ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.3)',

              zIndex: 0,

              pointerEvents: 'none',

            },

          }}

        >

          {/* Connection indicator (step 0) */}

          {showConnectionIndicator && (

            <Box

              data-step="connection"

              sx={{

                display: 'flex',

                flexDirection: 'column',

                alignItems: 'center',

                gap: 0.75,

                minWidth: 40,

                flexShrink: 0,

                position: 'relative',

                zIndex: 1,

              }}

            >

              {/* White circle background to hide the line */}

              <Box

                sx={{

                  position: 'relative',

                  display: 'flex',

                  alignItems: 'center',

                  justifyContent: 'center',

                  height: 28,

                  width: 28,

                }}

              >

                {/* White background to hide the line */}

                <Box

                  sx={{

                    position: 'absolute',

                    width: 28,

                    height: 28,

                    borderRadius: '50%',

                    backgroundColor: 'background.paper',

                    zIndex: 0,

                  }}

                />



                {/* Connection icon */}

                {isConnectingToE2B ? (

                  <CircularProgress

                    size={20}

                    thickness={5}

                    sx={{

                      color: 'primary.main',

                      position: 'relative',

                      zIndex: 1,

                    }}

                  />

                ) : (

                  <CableIcon

                    sx={{

                      fontSize: 20,

                      color: 'success.main',

                      position: 'relative',

                      zIndex: 1,

                    }}

                  />

                )}

              </Box>



              {/* Connection label */}

              <Typography

                variant="caption"

                sx={{

                  fontSize: '0.7rem',

                  fontWeight: 700,

                  color: isConnectingToE2B ? 'primary.main' : 'success.main',

                  whiteSpace: 'nowrap',

                }}

              >

                {isConnectingToE2B ? 'Connecting' : 'Connected'}

              </Typography>

            </Box>

          )}



          {/* Render steps and insert final step at the right position */}

          {steps.map((step, index) => (

            <React.Fragment key={step.stepNumber}>

              <Box

                data-step={step.stepNumber}

                onClick={() => handleStepClick(step.stepIndex, step.isCompleted, step.isCurrent)}

                sx={{

                  display: 'flex',

                  flexDirection: 'column',

                  alignItems: 'center',

                  gap: 0.75,

                  minWidth: 40,

                  flexShrink: 0,

                  position: 'relative',

                  zIndex: 1,

                  cursor: (step.isCompleted || step.isCurrent) ? 'pointer' : 'default',

                  '&:hover': (step.isCompleted || step.isCurrent) ? {

                    '& .step-dot': {

                      transform: 'scale(1.15)',

                    },

                  } : {},

                }}

              >

                {/* White circle background to hide the line */}

                <Box

                  sx={{

                    position: 'relative',

                    display: 'flex',

                    alignItems: 'center',

                    justifyContent: 'center',

                    height: 28,

                    width: 28,

                  }}

                >

                  {/* White background to hide the line */}

                  <Box

                    sx={{

                      position: 'absolute',

                      width: 28,

                      height: 28,

                      borderRadius: '50%',

                      backgroundColor: 'background.paper',

                      zIndex: 0,

                    }}

                  />



                  {/* Step dot */}

                  {step.isCurrent ? (

                    <Box

                      sx={{

                        position: 'relative',

                        display: 'flex',

                        alignItems: 'center',

                        justifyContent: 'center',

                        zIndex: 1,

                      }}

                    >

                      <CircularProgress

                        size={20}

                        thickness={5}

                        sx={{

                          color: 'primary.main',

                          position: 'absolute',

                        }}

                      />

                      <Box

                        sx={{

                          width: 8,

                          height: 8,

                          borderRadius: '50%',

                          backgroundColor: 'white',

                          position: 'absolute',

                          pointerEvents: 'none',

                          boxShadow: '0 0 4px rgba(0,0,0,0.2)',

                        }}

                      />

                    </Box>

                  ) : (

                    <Box

                      sx={{

                        position: 'relative',

                        display: 'flex',

                        alignItems: 'center',

                        justifyContent: 'center',

                        zIndex: 1,

                      }}

                    >

                      <Box

                        className="step-dot"

                        sx={{

                          width: step.isSelected ? 20 : step.isCompleted ? 14 : 12,

                          height: step.isSelected ? 20 : step.isCompleted ? 14 : 12,

                          borderRadius: '50%',

                          // Always keep steps in primary color (blue)

                          backgroundColor: step.isCompleted

                            ? 'primary.main' // Blue for completed steps

                            : (theme) => theme.palette.mode === 'dark' ? 'grey.800' : 'grey.300', // Light grey for future steps

                          transition: 'all 0.2s ease',

                          boxShadow: step.isCompleted || step.isSelected

                            ? step.isSelected

                              ? '0 0 8px rgba(255, 167, 38, 0.5)'

                              : '0 2px 4px rgba(0,0,0,0.1)'

                            : 'none',

                        }}

                      />

                      {/* White dot for selected step */}

                      {step.isSelected && (

                        <Box

                          sx={{

                            width: 8,

                            height: 8,

                            borderRadius: '50%',

                            backgroundColor: 'white',

                            position: 'absolute',

                          }}

                        />

                      )}

                    </Box>

                  )}

                </Box>



                {/* Step number - show for all steps */}

                <Typography

                  variant="caption"

                  sx={{

                    fontSize: '0.7rem',

                    fontWeight: step.isSelected || step.isCurrent ? 900 : 400,

                    color: step.isCurrent

                      ? 'primary.main'

                      : (step.isCompleted || step.isSelected

                        ? 'text.primary'

                        : (theme) => theme.palette.mode === 'dark' ? 'grey.700' : 'grey.400'),

                    whiteSpace: 'nowrap',

                    lineHeight: 1,

                  }}

                >

                  {step.stepNumber}

                </Typography>

              </Box>



              {/* Insert final step indicator right after the last completed step */}

              {finalStep && step.stepNumber === metadata.numberOfSteps && (

            <Box

              data-step="final"

              onClick={handleFinalStepClick}

              sx={{

                display: 'flex',

                flexDirection: 'column',

                alignItems: 'center',

                gap: 0.75,

                minWidth: 40,

                flexShrink: 0,

                position: 'relative',

                zIndex: 1,

                cursor: 'pointer',

                '&:hover': {

                  '& .final-step-icon': {

                    transform: 'scale(1.15)',

                  },

                },

              }}

            >

              {/* White circle background to hide the line */}

              <Box

                sx={{

                  position: 'relative',

                  display: 'flex',

                  alignItems: 'center',

                  justifyContent: 'center',

                  height: 28,

                  width: 28,

                }}

              >

                {/* White background to hide the line */}

                <Box

                  sx={{

                    position: 'absolute',

                    width: 28,

                    height: 28,

                    borderRadius: '50%',

                    backgroundColor: 'background.paper',

                    zIndex: 0,

                  }}

                />



                {/* Final step icon */}

                <Box

                  className="final-step-icon"

                  sx={{

                    width: selectedStepIndex === null ? 20 : 18,

                    height: selectedStepIndex === null ? 20 : 18,

                    borderRadius: '50%',

                    backgroundColor:

                      finalStep.type === 'success' ? 'success.main' :

                      finalStep.type === 'stopped' || finalStep.type === 'max_steps_reached' ? 'warning.main' :

                      'error.main',

                    display: 'flex',

                    alignItems: 'center',

                    justifyContent: 'center',

                    transition: 'all 0.2s ease',

                    boxShadow: selectedStepIndex === null

                      ? finalStep.type === 'success'

                        ? '0 2px 8px rgba(102, 187, 106, 0.4)'

                        : finalStep.type === 'stopped' || finalStep.type === 'max_steps_reached'

                          ? '0 2px 8px rgba(255, 152, 0, 0.4)'

                          : '0 2px 8px rgba(244, 67, 54, 0.4)'

                      : '0 2px 4px rgba(0,0,0,0.1)',

                    position: 'relative',

                    zIndex: 1,

                  }}

                >

                  {finalStep.type === 'success' ? (

                    <CheckIcon sx={{ fontSize: 14, color: 'white' }} />

                  ) : finalStep.type === 'stopped' ? (

                    <StopCircleIcon sx={{ fontSize: 14, color: 'white' }} />

                  ) : finalStep.type === 'max_steps_reached' ? (

                    <HourglassEmptyIcon sx={{ fontSize: 14, color: 'white' }} />

                  ) : finalStep.type === 'sandbox_timeout' ? (

                    <AccessTimeIcon sx={{ fontSize: 14, color: 'white' }} />

                  ) : (

                    <CloseIcon sx={{ fontSize: 14, color: 'white' }} />

                  )}

                </Box>

              </Box>



              {/* Final step label */}

              <Typography

                variant="caption"

                sx={{

                  fontSize: '0.7rem',

                  fontWeight: selectedStepIndex === null ? 700 : 500,

                  color:

                    finalStep.type === 'success'

                      ? (selectedStepIndex === null ? 'text.primary' : 'text.secondary')

                      : finalStep.type === 'stopped' || finalStep.type === 'max_steps_reached'

                        ? 'warning.main'

                        : 'error.main',

                  whiteSpace: 'nowrap',

                }}

              >

                {finalStep.type === 'success' ? 'End' :

                 finalStep.type === 'stopped' ? 'Stopped' :

                 finalStep.type === 'max_steps_reached' ? 'Max Steps' :

                 finalStep.type === 'sandbox_timeout' ? 'Timeout' :

                 'Failed'}

              </Typography>

            </Box>

              )}

            </React.Fragment>

          ))}

        </Box>

      </Box>

    </Box>
  );
};