File size: 11,475 Bytes
0cfd364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState } from 'react';
import {
  Activity,
  MousePointer,
  Navigation,
  Type,
  Scroll,
  Clock,
  Camera,
  Terminal,
  Users,
  StopCircle,
  ChevronDown,
  ChevronUp,
  Filter,
} from 'lucide-react';
import { Card, CardHeader, CardContent } from '@/components/ui/Card';
import { Badge } from '@/components/ui/Badge';
import { Button } from '@/components/ui/Button';
import { useEpisodeActions, useCurrentEpisode } from '@/hooks/useEpisode';
import { formatTimestamp, truncateText } from '@/utils/helpers';
import type { Action, ActionType } from '@/types';

interface ActionPanelProps {
  className?: string;
}

const ACTION_ICONS: Record<ActionType, React.ReactNode> = {
  navigate: <Navigation className="w-4 h-4" />,
  click: <MousePointer className="w-4 h-4" />,
  extract: <Terminal className="w-4 h-4" />,
  scroll: <Scroll className="w-4 h-4" />,
  input: <Type className="w-4 h-4" />,
  wait: <Clock className="w-4 h-4" />,
  screenshot: <Camera className="w-4 h-4" />,
  execute_tool: <Terminal className="w-4 h-4" />,
  delegate: <Users className="w-4 h-4" />,
  terminate: <StopCircle className="w-4 h-4" />,
};

const ACTION_COLORS: Record<ActionType, string> = {
  navigate: 'text-blue-400 bg-blue-400/10',
  click: 'text-green-400 bg-green-400/10',
  extract: 'text-purple-400 bg-purple-400/10',
  scroll: 'text-yellow-400 bg-yellow-400/10',
  input: 'text-cyan-400 bg-cyan-400/10',
  wait: 'text-gray-400 bg-gray-400/10',
  screenshot: 'text-pink-400 bg-pink-400/10',
  execute_tool: 'text-orange-400 bg-orange-400/10',
  delegate: 'text-indigo-400 bg-indigo-400/10',
  terminate: 'text-red-400 bg-red-400/10',
};

interface ActionItemProps {
  action: Action;
  index: number;
  isLast: boolean;
}

const ActionItem: React.FC<ActionItemProps> = ({ action, index, isLast }) => {
  const [isExpanded, setIsExpanded] = useState(false);

  const iconClass = ACTION_COLORS[action.type] ?? 'text-dark-400 bg-dark-700';

  return (
    <div className="relative">
      {/* Timeline connector */}
      {!isLast && (
        <div className="absolute left-[15px] top-8 bottom-0 w-0.5 bg-dark-700" />
      )}

      <div
        className="flex gap-3 cursor-pointer hover:bg-dark-700/30 rounded-lg p-2 transition-colors"
        onClick={() => setIsExpanded(!isExpanded)}
      >
        {/* Icon */}
        <div
          className={`flex-shrink-0 w-8 h-8 rounded-lg flex items-center justify-center ${iconClass}`}
        >
          {ACTION_ICONS[action.type]}
        </div>

        {/* Content */}
        <div className="flex-1 min-w-0">
          <div className="flex items-center justify-between gap-2">
            <div className="flex items-center gap-2">
              <Badge variant="neutral" size="sm" className={iconClass}>
                {action.type}
              </Badge>
              <span className="text-xs text-dark-500">#{index + 1}</span>
            </div>
            <div className="flex items-center gap-2">
              <span className="text-xs text-dark-500">
                {formatTimestamp(action.timestamp)}
              </span>
              {isExpanded ? (
                <ChevronUp className="w-3 h-3 text-dark-500" />
              ) : (
                <ChevronDown className="w-3 h-3 text-dark-500" />
              )}
            </div>
          </div>

          {/* Quick info */}
          <div className="mt-1 text-sm text-dark-300">
            {action.target?.selector && (
              <span className="font-mono text-xs text-dark-400">
                {truncateText(action.target.selector, 40)}
              </span>
            )}
            {action.value && (
              <span className="text-dark-400">
                {' → '}
                {truncateText(action.value, 30)}
              </span>
            )}
            {action.reasoning && !action.target?.selector && !action.value && (
              <span className="text-dark-400 italic">
                {truncateText(action.reasoning, 50)}
              </span>
            )}
          </div>

          {/* Confidence bar */}
          <div className="mt-1.5 flex items-center gap-2">
            <div className="h-1 flex-1 max-w-[100px] bg-dark-700 rounded-full overflow-hidden">
              <div
                className={`h-full rounded-full ${
                  action.confidence > 0.8
                    ? 'bg-green-500'
                    : action.confidence > 0.5
                    ? 'bg-yellow-500'
                    : 'bg-red-500'
                }`}
                style={{ width: `${action.confidence * 100}%` }}
              />
            </div>
            <span className="text-xs text-dark-500">
              {(action.confidence * 100).toFixed(0)}%
            </span>
          </div>
        </div>
      </div>

      {/* Expanded details */}
      {isExpanded && (
        <div className="ml-11 mt-2 p-3 bg-dark-900/50 rounded-lg space-y-2">
          {action.reasoning && (
            <div>
              <div className="text-xs text-dark-400 mb-1">Reasoning</div>
              <div className="text-sm text-dark-300">{action.reasoning}</div>
            </div>
          )}

          {action.target && (
            <div>
              <div className="text-xs text-dark-400 mb-1">Target</div>
              <div className="code-block text-xs">
                {JSON.stringify(action.target, null, 2)}
              </div>
            </div>
          )}

          {action.parameters && Object.keys(action.parameters).length > 0 && (
            <div>
              <div className="text-xs text-dark-400 mb-1">Parameters</div>
              <div className="code-block text-xs">
                {JSON.stringify(action.parameters, null, 2)}
              </div>
            </div>
          )}

          <div className="flex items-center gap-4 text-xs text-dark-500">
            <span>Agent: {action.agentId}</span>
            <span>Confidence: {(action.confidence * 100).toFixed(1)}%</span>
          </div>
        </div>
      )}
    </div>
  );
};

export const ActionPanel: React.FC<ActionPanelProps> = ({ className }) => {
  const { data: episode } = useCurrentEpisode();
  const { data: actions, isLoading } = useEpisodeActions(episode?.id);
  const [typeFilter, setTypeFilter] = useState<ActionType | null>(null);
  const [showFilters, setShowFilters] = useState(false);

  const actionTypes = React.useMemo(() => {
    if (!actions) return [];
    return [...new Set(actions.map((a) => a.type))];
  }, [actions]);

  const filteredActions = React.useMemo(() => {
    if (!actions) return [];
    if (!typeFilter) return actions;
    return actions.filter((a) => a.type === typeFilter);
  }, [actions, typeFilter]);

  const actionCounts = React.useMemo(() => {
    if (!actions) return {};
    return actions.reduce(
      (acc, a) => {
        acc[a.type] = (acc[a.type] || 0) + 1;
        return acc;
      },
      {} as Record<string, number>
    );
  }, [actions]);

  return (
    <Card className={className}>
      <CardHeader
        title="Actions"
        subtitle={`${actions?.length ?? 0} total`}
        icon={<Activity className="w-4 h-4" />}
        action={
          <Button
            variant="ghost"
            size="sm"
            onClick={() => setShowFilters(!showFilters)}
          >
            <Filter className="w-4 h-4" />
          </Button>
        }
      />
      <CardContent>
        {/* Filter Bar */}
        {showFilters && (
          <div className="mb-4 p-3 bg-dark-900/50 rounded-lg">
            <div className="text-xs text-dark-400 mb-2">Filter by type</div>
            <div className="flex flex-wrap gap-2">
              <button
                onClick={() => setTypeFilter(null)}
                className={`px-2 py-1 text-xs rounded transition-colors ${
                  !typeFilter
                    ? 'bg-accent-primary text-white'
                    : 'bg-dark-700 text-dark-300 hover:bg-dark-600'
                }`}
              >
                All ({actions?.length ?? 0})
              </button>
              {actionTypes.map((type) => (
                <button
                  key={type}
                  onClick={() =>
                    setTypeFilter(typeFilter === type ? null : type)
                  }
                  className={`px-2 py-1 text-xs rounded transition-colors flex items-center gap-1 ${
                    typeFilter === type
                      ? ACTION_COLORS[type]
                      : 'bg-dark-700 text-dark-300 hover:bg-dark-600'
                  }`}
                >
                  {ACTION_ICONS[type]}
                  {type} ({actionCounts[type] ?? 0})
                </button>
              ))}
            </div>
          </div>
        )}

        {/* Action Stats */}
        {actions && actions.length > 0 && (
          <div className="flex flex-wrap gap-2 mb-4">
            {Object.entries(actionCounts)
              .sort((a, b) => b[1] - a[1])
              .slice(0, 5)
              .map(([type, count]) => (
                <Badge
                  key={type}
                  variant="neutral"
                  size="sm"
                  className={ACTION_COLORS[type as ActionType]}
                >
                  {type}: {count}
                </Badge>
              ))}
          </div>
        )}

        {/* Action Timeline */}
        <div className="max-h-[400px] overflow-y-auto">
          {isLoading ? (
            <div className="flex items-center justify-center py-8">
              <Activity className="w-6 h-6 text-dark-500 animate-pulse" />
            </div>
          ) : !filteredActions || filteredActions.length === 0 ? (
            <div className="text-center py-8 text-dark-500">
              <Activity className="w-8 h-8 mx-auto mb-2 opacity-50" />
              <p>No actions recorded</p>
            </div>
          ) : (
            <div className="space-y-1">
              {filteredActions
                .slice()
                .reverse()
                .map((action, i, arr) => (
                  <ActionItem
                    key={`${action.timestamp}-${i}`}
                    action={action}
                    index={arr.length - 1 - i}
                    isLast={i === arr.length - 1}
                  />
                ))}
            </div>
          )}
        </div>

        {/* Current Action Highlight */}
        {actions && actions.length > 0 && (
          <div className="mt-4 pt-4 border-t border-dark-700">
            <div className="text-xs text-dark-400 mb-2">Latest Action</div>
            <div className="bg-dark-900/50 rounded-lg p-3">
              <div className="flex items-center gap-2 mb-1">
                <div
                  className={`p-1.5 rounded ${
                    ACTION_COLORS[actions[actions.length - 1]!.type]
                  }`}
                >
                  {ACTION_ICONS[actions[actions.length - 1]!.type]}
                </div>
                <span className="font-medium text-dark-200">
                  {actions[actions.length - 1]!.type}
                </span>
              </div>
              {actions[actions.length - 1]!.reasoning && (
                <p className="text-sm text-dark-400 mt-1">
                  {actions[actions.length - 1]!.reasoning}
                </p>
              )}
            </div>
          </div>
        )}
      </CardContent>
    </Card>
  );
};

export default ActionPanel;