File size: 14,560 Bytes
3593cc3
 
67b042f
 
3593cc3
67b042f
 
 
3593cc3
 
 
67b042f
 
 
 
 
 
 
 
 
 
3593cc3
67b042f
02dbe84
67b042f
 
 
02dbe84
 
 
 
 
67b042f
 
 
 
 
09644e5
67b042f
 
 
09644e5
67b042f
 
 
 
 
 
 
 
 
3593cc3
 
67b042f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02dbe84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67b042f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3593cc3
02dbe84
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
397
398
399
400
import { updateStepEvaluation } from '@/services/api';
import { useAgentStore } from '@/stores/agentStore';
import { AgentStep } from '@/types/agent';
import AccessTimeIcon from '@mui/icons-material/AccessTime';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import InputIcon from '@mui/icons-material/Input';
import OutputIcon from '@mui/icons-material/Output';
import ThumbDownIcon from '@mui/icons-material/ThumbDown';
import ThumbUpIcon from '@mui/icons-material/ThumbUp';
import { Accordion, AccordionDetails, AccordionSummary, Box, Card, CardContent, Chip, IconButton, Tooltip, Typography } from '@mui/material';
import React, { useState } from 'react';

interface StepCardProps {
  step: AgentStep;
  index: number;
  isLatest?: boolean;
  isActive?: boolean;
}

export const StepCard: React.FC<StepCardProps> = ({ step, index, isLatest = false, isActive = false }) => {
  const setSelectedStepIndex = useAgentStore((state) => state.setSelectedStepIndex);
  const updateStepEvaluationInStore = useAgentStore((state) => state.updateStepEvaluation);
  const [thoughtExpanded, setThoughtExpanded] = useState(false);
  const [actionsExpanded, setActionsExpanded] = useState(false);
  const [evaluation, setEvaluation] = useState<'like' | 'dislike' | 'neutral'>(step.step_evaluation || 'neutral');
  const [isVoting, setIsVoting] = useState(false);

  const hasMultipleActions = step.actions && step.actions.length > 1;
  const displayedActions = hasMultipleActions && !actionsExpanded
    ? step.actions.slice(0, 1)
    : step.actions;

  const handleClick = () => {
    setSelectedStepIndex(index);
  };

  const handleAccordionClick = (event: React.MouseEvent) => {
    event.stopPropagation(); // Prevent propagation to avoid selecting the step
  };

  const handleVote = async (event: React.MouseEvent, vote: 'like' | 'dislike') => {
    event.stopPropagation(); // Prevent propagation to avoid selecting the step

    if (isVoting) return;

    const newEvaluation = evaluation === vote ? 'neutral' : vote;
    setIsVoting(true);

    try {
      await updateStepEvaluation(step.traceId, step.stepId, newEvaluation);
      setEvaluation(newEvaluation);
      // Update the store so the evaluation is reflected in JSON export
      updateStepEvaluationInStore(step.stepId, newEvaluation);
    } catch (error) {
      console.error('Failed to update step evaluation:', error);
    } finally {
      setIsVoting(false);
    }
  };

  return (
    <Card
      elevation={0}
      onClick={handleClick}
      sx={{
        backgroundColor: 'background.paper',
        border: '1px solid',
        borderColor: (theme) => `${isActive ? theme.palette.primary.main : theme.palette.divider} !important`,
        borderRadius: 1.5,
        transition: 'all 0.2s ease',
        cursor: 'pointer',
        boxShadow: isActive ? (theme) => `0 2px 8px ${theme.palette.mode === 'dark' ? 'rgba(79, 134, 198, 0.3)' : 'rgba(79, 134, 198, 0.2)'}` : 'none',
        '&:hover': {
          borderColor: (theme) => `${theme.palette.primary.main} !important`,
          boxShadow: (theme) => `0 2px 8px ${theme.palette.mode === 'dark' ? 'rgba(79, 134, 198, 0.2)' : 'rgba(79, 134, 198, 0.1)'}`,
        },
      }}
    >
      <CardContent sx={{ p: 1.5, '&:last-child': { pb: 1.5 } }}>
        {/* Step header */}
        <Box sx={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', mb: 1.5 }}>
          <Typography
            sx={{
              fontSize: '1.5rem',
              fontWeight: 800,
              color: isActive ? 'primary.main' : 'text.primary',
              lineHeight: 1,
            }}
          >
            {index + 1}
          </Typography>
          <Box sx={{ display: 'flex', gap: 0.5, alignItems: 'center' }}>
            <Chip
              icon={<AccessTimeIcon sx={{ fontSize: '0.7rem !important' }} />}
              label={`${step.duration.toFixed(1)}s`}
              size="small"
              sx={{
                height: 'auto',
                py: 0.25,
                fontSize: '0.65rem',
                fontWeight: 600,
                backgroundColor: 'action.hover',
                color: 'text.primary',
                '& .MuiChip-icon': { marginLeft: 0.5, color: 'text.secondary' },
              }}
            />
            <Chip
              icon={<InputIcon sx={{ fontSize: '0.7rem !important' }} />}
              label={step.inputTokensUsed.toLocaleString()}
              size="small"
              sx={{
                height: 'auto',
                py: 0.25,
                fontSize: '0.65rem',
                fontWeight: 600,
                backgroundColor: 'action.hover',
                color: 'text.primary',
                '& .MuiChip-icon': { marginLeft: 0.5, color: 'text.secondary' },
              }}
            />
            <Chip
              icon={<OutputIcon sx={{ fontSize: '0.7rem !important' }} />}
              label={step.outputTokensUsed.toLocaleString()}
              size="small"
              sx={{
                height: 'auto',
                py: 0.25,
                fontSize: '0.65rem',
                fontWeight: 600,
                backgroundColor: 'action.hover',
                color: 'text.primary',
                '& .MuiChip-icon': { marginLeft: 0.5, color: 'text.secondary' },
              }}
            />
          </Box>
        </Box>

        {/* Step image */}
        {step.image && (
          <Box
            sx={{
              mb: 1.5,
              borderRadius: 1,
              overflow: 'hidden',
              border: '1px solid',
              borderColor: (theme) => isActive ? theme.palette.primary.main : theme.palette.divider,
              backgroundColor: 'action.hover',
              transition: 'border-color 0.2s ease',
            }}
          >
            <img
              src={step.image}
              alt={`Step ${index + 1}`}
              style={{ width: '100%', height: 'auto', display: 'block' }}
            />
          </Box>
        )}

        {/* Action */}
        {step.actions && step.actions.length > 0 && (
          <Box sx={{ mb: 1.5 }}>
            <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5, mb: 0.75, justifyContent: 'space-between' }}>
              <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
                <Typography
                  variant="caption"
                  sx={{
                    fontWeight: 700,
                    color: 'text.secondary',
                    fontSize: '0.65rem',
                    textTransform: 'uppercase',
                    letterSpacing: '0.5px',
                  }}
                >
                  Action
                </Typography>
                {hasMultipleActions && (
                  <Tooltip title={actionsExpanded ? 'Show less' : `Show all ${step.actions.length} actions`}>
                    <IconButton
                      size="small"
                      onClick={(e) => {
                        e.stopPropagation();
                        setActionsExpanded(!actionsExpanded);
                      }}
                      sx={{
                        padding: '2px',
                        color: 'text.secondary',
                        '&:hover': {
                          color: 'text.primary',
                          backgroundColor: 'action.hover',
                        },
                      }}
                    >
                      <ExpandMoreIcon
                        sx={{
                          fontSize: 16,
                          transform: actionsExpanded ? 'rotate(180deg)' : 'rotate(0deg)',
                          transition: 'transform 0.2s',
                        }}
                      />
                    </IconButton>
                  </Tooltip>
                )}
              </Box>

              {/* Vote buttons */}
              <Box sx={{ display: 'flex', gap: 0.5 }}>
                <Tooltip title={evaluation === 'like' ? 'Remove like' : 'Like this step'}>
                  <IconButton
                    size="small"
                    onClick={(e) => handleVote(e, 'like')}
                    disabled={isVoting}
                    sx={{
                      padding: '2px',
                      color: evaluation === 'like' ? 'success.main' : 'action.disabled',
                      '&:hover': {
                        color: 'success.main',
                        backgroundColor: (theme) => theme.palette.mode === 'dark' ? 'rgba(102, 187, 106, 0.1)' : 'rgba(102, 187, 106, 0.08)',
                      },
                    }}
                  >
                    <ThumbUpIcon sx={{ fontSize: 14 }} />
                  </IconButton>
                </Tooltip>
                <Tooltip title={evaluation === 'dislike' ? 'Remove dislike' : 'Dislike this step'}>
                  <IconButton
                    size="small"
                    onClick={(e) => handleVote(e, 'dislike')}
                    disabled={isVoting}
                    sx={{
                      padding: '2px',
                      color: evaluation === 'dislike' ? 'error.main' : 'action.disabled',
                      '&:hover': {
                        color: 'error.main',
                        backgroundColor: (theme) => theme.palette.mode === 'dark' ? 'rgba(244, 67, 54, 0.1)' : 'rgba(244, 67, 54, 0.08)',
                      },
                    }}
                  >
                    <ThumbDownIcon sx={{ fontSize: 14 }} />
                  </IconButton>
                </Tooltip>
              </Box>
            </Box>
            <Box component="ul" sx={{ listStyle: 'none', p: 0, m: 0 }}>
              {displayedActions?.map((action, actionIndex) => (
                <Box
                  key={actionIndex}
                  component="li"
                  sx={{
                    display: 'flex',
                    alignItems: 'flex-start',
                    fontSize: '0.75rem',
                    color: 'text.primary',
                    lineHeight: 1.4,
                    mb: 0.5,
                    '&:last-child': { mb: 0 },
                  }}
                >
                  {/* <Typography
                    component="span"
                    sx={{
                      mr: 0.5,
                      color: 'text.secondary',
                      fontWeight: 700,
                      flexShrink: 0,
                      fontSize: '0.75rem',
                    }}
                  >

                  </Typography> */}
                  <Typography
                    component="span"
                    sx={{
                      fontSize: '0.75rem',
                      fontWeight: 900,
                      wordBreak: 'break-word',
                    }}
                  >
                    {action.description}
                  </Typography>
                </Box>
              ))}
            </Box>
          </Box>
        )}

        {/* Thought - Accordion */}
        {step.thought && (
          <Accordion
            expanded={thoughtExpanded}
            onChange={(e, expanded) => setThoughtExpanded(expanded)}
            onClick={handleAccordionClick}
            elevation={0}
            disableGutters
            sx={{
              mb: 0.5,
              backgroundColor: 'transparent',
              border: 'none',
              boxShadow: 'none',
              '&:before': { display: 'none' },
              '&.MuiAccordion-root': {
                backgroundColor: 'transparent',
                boxShadow: 'none',
                '&:before': {
                  display: 'none',
                },
              },
              '& .MuiAccordionSummary-root': {
                minHeight: 'auto',
                p: 0,
                backgroundColor: 'transparent',
                '&:hover': {
                  backgroundColor: 'transparent',
                },
                '&.Mui-expanded': {
                  minHeight: 'auto',
                },
              },
              '& .MuiAccordionSummary-content': {
                margin: '0 !important',
              },
              '& .MuiAccordionDetails-root': {
                p: 0,
                pt: 0.5,
                pb: 0,
                backgroundColor: 'transparent',
              },
            }}
          >
            <AccordionSummary
              expandIcon={<ExpandMoreIcon sx={{ fontSize: 16, color: 'text.secondary' }} />}
              sx={{
                flexDirection: 'row',
                border: 'none',
                '& .MuiAccordionSummary-expandIconWrapper': {
                  transform: 'rotate(-90deg)',
                  transition: 'transform 0.2s',
                  '&.Mui-expanded': {
                    transform: 'rotate(0deg)',
                  },
                },
              }}
            >
              <Box sx={{ display: 'flex', alignItems: 'center', gap: 0.5 }}>
                <Typography
                  variant="caption"
                  sx={{
                    fontWeight: 700,
                    color: 'text.secondary',
                    fontSize: '0.65rem',
                    textTransform: 'uppercase',
                    letterSpacing: '0.5px',
                  }}
                >
                  Thought
                </Typography>
              </Box>
            </AccordionSummary>
            <AccordionDetails>
              <Typography
                variant="body2"
                sx={{
                  fontSize: '0.75rem',
                  color: 'text.primary',
                  lineHeight: 1.4,
                  pl: 2.5,
                }}
              >
                {step.thought}
              </Typography>
            </AccordionDetails>
          </Accordion>
        )}

        {/* Error */}
        {step.error && (
          <Box sx={{
            mt: 1.5,
            p: 1,
            borderRadius: 1,
            backgroundColor: (theme) => theme.palette.mode === 'dark' ? 'rgba(244, 67, 54, 0.1)' : 'rgba(244, 67, 54, 0.08)',
            border: '1px solid',
            borderColor: 'error.main'
          }}>
            <Typography
              variant="caption"
              sx={{
                fontSize: '0.7rem',
                color: 'error.main',
                fontWeight: 600,
              }}
            >
              Error: {step.error}
            </Typography>
          </Box>
        )}
      </CardContent>
    </Card>
  );
};