File size: 24,370 Bytes
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bc750c
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
import React, { useState, useCallback } from "react";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import {
  FileText,
  Database,
  Download,
  GitBranch,
  HardDrive,
  Hash,
  Activity,
  Trash2,
  AlertCircle,
  Plus,
} from "lucide-react";
import { Trace, KnowledgeGraph } from "@/types";
import { formatFileSize, formatRelativeTime } from "@/lib/utils";
import { getTraceStatusColor } from "@/lib/status-utils";
import { api } from "@/lib/api";
import { useModal } from "@/context/ModalContext";
import { useNotification } from "@/context/NotificationContext";
import { useNavigation } from "@/context/NavigationContext";
import { useAgentGraph } from "@/context/AgentGraphContext";
import { KnowledgeGraphTree } from "@/components/features/traces/KnowledgeGraphTree";
import { SplitterSelectionModal, SplitterType } from "./SplitterSelectionModal";
import { useTaskPolling } from "@/hooks/useTaskPolling";
import { ContextDocumentsSection } from "@/components/features/context/ContextDocumentsSection";

interface TraceDetailsModalProps {
  data: {
    trace: Trace;
    knowledgeGraphs?: KnowledgeGraph[];
  };
  onClose: () => void;
}

export function TraceDetailsModal({
  data,
  onClose: _onClose,
}: TraceDetailsModalProps) {
  const { trace, knowledgeGraphs = [] } = data;
  const { openModal } = useModal();
  const { showNotification } = useNotification();
  const { actions: navigationActions } = useNavigation();

  // Helper to show both toast and persistent notifications
  const showSystemNotification = useCallback(
    (notification: {
      type: "success" | "error" | "warning" | "info";
      title: string;
      message: string;
    }) => {
      // Show toast notification
      showNotification(notification);

      // Add to persistent notifications for all types
      navigationActions.addNotification({
        type: notification.type,
        title: notification.title,
        message: notification.message,
      });
    },
    [showNotification, navigationActions]
  );

  const { actions } = useAgentGraph();
  const [isSplitterModalOpen, setIsSplitterModalOpen] = useState(false);
  const [isGenerating, setIsGenerating] = useState(false);
  const [generationProgress, setGenerationProgress] = useState(0);

  const formatTraceType = (type?: string) => {
    if (!type) return "Unknown";
    return type.replace(/[-_]/g, " ").replace(/\b\w/g, (l) => l.toUpperCase());
  };

  const handleViewKnowledgeGraph = (kgId: string) => {
    const kg = knowledgeGraphs.find((k) => k.kg_id === kgId || k.id === kgId);
    if (!kg) return;

    openModal(
      "knowledge-graph",
      `Knowledge Graph - ${
        kg.filename || `Agent Graph ${kg.kg_id?.slice(0, 8) || "unknown"}`
      }`,
      {
        knowledgeGraph: kg,
        windowGraphs: kg.window_knowledge_graphs || [],
      },
      {
        size: "xl",
        closable: true,
      }
    );
  };

  const handleViewSegment = async (
    traceId: string,
    startChar: number,
    endChar: number,
    windowIndex: number
  ) => {
    try {
      const segmentData = await api.traces.extractSegment(
        traceId,
        startChar,
        endChar
      );

      openModal(
        "trace-segment",
        `Trace Segment - Window ${windowIndex}`,
        {
          trace: trace,
          segment: {
            content: segmentData.content,
            startChar,
            endChar,
            windowIndex,
          },
        },
        {
          size: "xl",
          closable: true,
        }
      );
    } catch (error) {
      console.error("Error loading trace segment:", error);
      showSystemNotification({
        type: "error",
        title: "Failed to Load Segment",
        message: "Could not load the trace segment. Please try again.",
      });
    }
  };

  const handleDeleteKnowledgeGraph = async (kgId: string, name: string) => {
    const confirmMessage = `Are you sure you want to delete the knowledge graph "${name}"?

This will permanently remove:
• The knowledge graph and all entities/relationships
• All prompt reconstruction data
• All perturbation test results
• All causal analysis results

This action cannot be undone.`;

    if (!confirm(confirmMessage)) {
      return;
    }

    try {
      const response = await fetch(`/api/knowledge-graphs/${kgId}`, {
        method: "DELETE",
        headers: {
          "Content-Type": "application/json",
        },
      });

      if (!response.ok) {
        throw new Error(`API error: ${response.status} ${response.statusText}`);
      }

      const data = await response.json();
      console.log("Knowledge graph deleted:", data);

      // TODO: Add success notification and refresh knowledge graphs list
      alert("Knowledge graph deleted successfully");
    } catch (error) {
      console.error("Error deleting knowledge graph:", error);
      alert(
        `Error deleting knowledge graph: ${
          error instanceof Error ? error.message : String(error)
        }`
      );
    }
  };

  const handleReplayKg = (
    kgId: string,
    traceId: string,
    processingRunId?: string
  ) => {
    // Handle replay functionality
    console.log("Replay KG:", { kgId, traceId, processingRunId });
    // Implement actual replay logic
  };

  const handleGenerateKG = () => {
    setIsSplitterModalOpen(true);
  };

  // Task polling setup for knowledge graph generation
  const { pollTaskStatus, isPolling } = useTaskPolling({
    onSuccess: (taskId) => {
      console.log("Knowledge graph generation completed:", taskId);
      showSystemNotification({
        type: "success",
        title: "Agent Graph Generation Complete",
        message: "Your agent graph has been generated successfully!",
      });

      // Refresh the trace data to get the updated knowledge graphs
      actions.setTraces([]);
      actions.setKnowledgeGraphs([]);
      setIsGenerating(false);
      setGenerationProgress(0);
    },
    onError: (error, taskId) => {
      console.error("Knowledge graph generation failed:", error, taskId);

      // Check if this is a timeout vs a real failure
      const isTimeout =
        error.includes("timeout") || error.includes("timed out");

      showSystemNotification({
        type: isTimeout ? "warning" : "error",
        title: isTimeout
          ? "Generation Taking Longer Than Expected"
          : "Agent Graph Generation Failed",
        message: isTimeout
          ? `${error} You can refresh the page to check if it completed.`
          : error,
      });

      // Only reset state if it's not a timeout (user might want to keep waiting)
      if (!isTimeout) {
        setIsGenerating(false);
        setGenerationProgress(0);
      }
    },
    onProgress: (progress, message) => {
      setGenerationProgress(progress);
      if (message) {
        console.log("Generation progress:", progress, message);
      }
    },
    maxAttempts: 180, // 15 minutes with exponential backoff
    interval: 5000, // Start with 5 seconds
    enableExponentialBackoff: true,
  });

  const handleSplitterConfirm = async (
    splitterType: SplitterType,
    methodName?: string,
    modelName?: string,
    chunkingConfig?: { min_chunk_size?: number; max_chunk_size?: number }
  ) => {
    const finalMethodName = methodName || "production";
    console.log("TraceDetailsModal: Using method name:", finalMethodName);
    console.log("TraceDetailsModal: Using chunking config:", chunkingConfig);

    setIsGenerating(true);
    setIsSplitterModalOpen(false); // Safety fallback - modal should already be closed
    setGenerationProgress(0);

    try {
      const response = await api.traces.generateKnowledgeGraph(
        trace.trace_id,
        splitterType,
        true,
        finalMethodName,
        modelName || "gpt-5-mini",
        chunkingConfig
      );

      showSystemNotification({
        type: "info",
        title: "Agent Graph Generation Started",
        message: `Using ${getSplitterDisplayName(
          splitterType
        )} splitter with ${finalMethodName} method. This may take several minutes.`,
      });

      // Start polling for task status if we got a task_id
      if (response.task_id) {
        pollTaskStatus(response.task_id);
      } else {
        // If no task_id, just refresh immediately and mark as complete
        const updatedTraces = await api.traces.list();
        actions.setTraces(updatedTraces);
        const updatedKGs = await api.knowledgeGraphs.list();
        actions.setKnowledgeGraphs(updatedKGs);
        setIsGenerating(false);
        showSystemNotification({
          type: "success",
          title: "Agent Graph Generation Complete",
          message: "Your agent graph has been generated successfully!",
        });
      }
    } catch (error) {
      console.error("Failed to generate knowledge graph:", error);
      showSystemNotification({
        type: "error",
        title: "Agent Graph Generation Failed",
        message: "Failed to start agent graph generation. Please try again.",
      });
      setIsGenerating(false);
      setGenerationProgress(0);
    }
  };

  // Helper function to get display name for splitter type
  const getSplitterDisplayName = (splitterType: SplitterType) => {
    switch (splitterType) {
      case "agent_semantic":
        return "Agent Semantic";
      case "json":
        return "JSON";
      case "prompt_interaction":
        return "Prompt Interaction";
      default:
        return splitterType;
    }
  };

  const handleViewContent = async () => {
    try {
      // Get numbered content for ContentReference compatibility
      const numberedContent = await api.traces.getNumberedContent(
        trace.trace_id
      );
      openModal(
        "trace-content",
        `Trace Content - ${trace.filename}`,
        {
          trace: trace,
          content: { content: numberedContent, type: "numbered" },
        },
        {
          size: "xl",
          closable: true,
        }
      );
    } catch (error) {
      console.error("Failed to load numbered content:", error);
      // Fallback to existing content if numbered content fails
      openModal(
        "trace-content",
        `Trace Content - ${trace.filename}`,
        {
          trace: trace,
          content: { content: trace.content || "", type: "raw" },
        },
        {
          size: "xl",
          closable: true,
        }
      );
    }
  };

  const handleDownloadTrace = async () => {
    try {
      // Get the full trace content instead of just metadata
      const traceContent = await api.traces.getContent(trace.trace_id);

      // Create blob with the actual trace content
      const blob = new Blob([traceContent], {
        type: "application/json",
      });
      const url = URL.createObjectURL(blob);

      const exportFileDefaultName = `${trace.filename || "trace"}.json`;

      const linkElement = document.createElement("a");
      linkElement.setAttribute("href", url);
      linkElement.setAttribute("download", exportFileDefaultName);
      linkElement.click();

      // Clean up the object URL
      URL.revokeObjectURL(url);

      console.log("Trace downloaded successfully:", trace.trace_id);
    } catch (error) {
      console.error("Failed to download trace:", error);
      alert("Failed to download trace. Please try again.");
    }
  };

  const handleDeleteTrace = async () => {
    const kgCount = knowledgeGraphs.length;
    const displayFilename =
      trace.filename.length > 50
        ? `${trace.filename.substring(0, 47)}...`
        : trace.filename;
    const confirmMessage = `Are you sure you want to delete the trace "${displayFilename}"?

This will permanently remove:
• The trace and all its content
• All associated knowledge graphs (${kgCount} found)
• All entities and relationships
• All prompt reconstruction data
• All perturbation test results
• All causal analysis results

This action cannot be undone.`;

    if (!confirm(confirmMessage)) {
      return;
    }

    try {
      await api.traces.delete(trace.trace_id);

      // Close modal and refresh page
      window.location.reload();
    } catch (error) {
      console.error("Error deleting trace:", error);
      alert(
        `Error deleting trace: ${
          error instanceof Error ? error.message : String(error)
        }`
      );
    }
  };

  // Calculate summary statistics - use consistent counting
  const finalKGsArray = knowledgeGraphs.filter(
    (kg) =>
      kg.is_final === true ||
      (kg.window_index === null && kg.window_total !== null)
  );
  const finalKGs = finalKGsArray.length; // Count of final KGs
  const totalKGs = finalKGs; // Use final KG count for consistency
  const completedPipelines = knowledgeGraphs.filter(
    (kg) => kg.is_analyzed
  ).length;

  return (
    <div className="space-y-6 p-6 max-h-[90vh] overflow-y-auto">
      {/* Trace Header */}
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-3">
          <div className="p-2 rounded-lg bg-primary/10">
            <FileText className="h-6 w-6 text-primary" />
          </div>
          <div>
            <h2 className="text-xl font-semibold">{trace.filename}</h2>
            <p className="text-sm text-muted-foreground">
              Uploaded{" "}
              {formatRelativeTime(
                trace.upload_timestamp || trace.timestamp || ""
              )}
            </p>
          </div>
        </div>
        <Badge variant="outline" className={getTraceStatusColor(trace.status)}>
          {trace.status}
        </Badge>
      </div>

      {/* Quick Stats */}
      <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
        <Card className="p-4 quick-stats-card">
          <div className="flex items-center gap-2">
            <HardDrive className="h-4 w-4 text-muted-foreground" />
            <div>
              <p className="text-2xl font-bold">
                {formatFileSize(trace.character_count || trace.size || 0)}
              </p>
              <p className="text-xs text-muted-foreground">File Size</p>
            </div>
          </div>
        </Card>

        <Card className="p-4 quick-stats-card">
          <div className="flex items-center gap-2">
            <GitBranch className="h-4 w-4 text-muted-foreground" />
            <div>
              <p className="text-2xl font-bold">{trace.turn_count || 0}</p>
              <p className="text-xs text-muted-foreground">Turns</p>
            </div>
          </div>
        </Card>

        <Card className="p-4 quick-stats-card">
          <div className="flex items-center gap-2">
            <Activity className="h-4 w-4 text-muted-foreground" />
            <div>
              <p className="text-2xl font-bold">{finalKGs}</p>
              <p className="text-xs text-muted-foreground">
                Final Agent Graphs
              </p>
            </div>
          </div>
        </Card>

        <Card className="p-4 quick-stats-card">
          <div className="flex items-center gap-2">
            <Database className="h-4 w-4 text-muted-foreground" />
            <div>
              <p className="text-2xl font-bold">{completedPipelines}</p>
              <p className="text-xs text-muted-foreground">Completed</p>
            </div>
          </div>
        </Card>
      </div>

      {/* Trace Information */}
      <Card>
        <CardHeader>
          <CardTitle className="flex items-center gap-2">
            <Database className="h-5 w-5" />
            Trace Information
          </CardTitle>
        </CardHeader>
        <CardContent className="space-y-6">
          {/* Core Information Grid */}
          <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
            {/* Characters */}
            <Card className="p-4 bg-muted/30">
              <div className="flex items-center gap-3">
                <div className="p-2 rounded-lg bg-primary/10">
                  <Hash className="h-4 w-4 text-primary" />
                </div>
                <div className="flex-1">
                  <p className="text-xs text-muted-foreground uppercase tracking-wide font-medium">
                    Content Size
                  </p>
                  <p className="text-lg font-semibold">
                    {trace.character_count?.toLocaleString() || "N/A"} chars
                  </p>
                  {trace.turn_count && (
                    <p className="text-sm text-muted-foreground">
                      {trace.turn_count} turns
                    </p>
                  )}
                </div>
              </div>
            </Card>

            {/* Type & Upload Info */}
            <Card className="p-4 bg-muted/30">
              <div className="flex items-center gap-3">
                <div className="p-2 rounded-lg bg-primary/10">
                  <FileText className="h-4 w-4 text-primary" />
                </div>
                <div className="flex-1">
                  <p className="text-xs text-muted-foreground uppercase tracking-wide font-medium">
                    Type & Date
                  </p>
                  <p className="text-lg font-semibold">
                    {formatTraceType(trace.trace_type)}
                  </p>
                  <p className="text-sm text-muted-foreground">
                    {new Date(
                      trace.upload_timestamp || trace.timestamp || ""
                    ).toLocaleDateString()}
                  </p>
                </div>
              </div>
            </Card>
          </div>

          {/* Description */}
          {trace.description && (
            <div className="space-y-2">
              <p className="text-sm font-medium flex items-center gap-2">
                <FileText className="h-4 w-4 text-muted-foreground" />
                Description
              </p>
              <div className="p-4 rounded-lg bg-muted/30 border-l-4 border-primary">
                <p className="text-sm text-muted-foreground leading-relaxed">
                  {trace.description}
                </p>
              </div>
            </div>
          )}

          {/* Error Message */}
          {trace.error_message && (
            <div className="space-y-2">
              <p className="text-sm font-medium text-destructive flex items-center gap-2">
                <AlertCircle className="h-4 w-4" />
                Error Message
              </p>
              <div className="p-4 rounded-lg bg-destructive/10 border border-destructive/20">
                <p className="text-sm text-destructive font-mono">
                  {trace.error_message}
                </p>
              </div>
            </div>
          )}

          {/* Tags */}
          {trace.tags && trace.tags.length > 0 && (
            <div className="space-y-3">
              <p className="text-sm font-medium flex items-center gap-2">
                <Hash className="h-4 w-4 text-muted-foreground" />
                Tags
              </p>
              <div className="flex flex-wrap gap-2">
                {trace.tags.map((tag) => (
                  <Badge
                    key={tag}
                    variant="secondary"
                    className="text-xs px-3 py-1"
                  >
                    {tag}
                  </Badge>
                ))}
              </div>
            </div>
          )}
        </CardContent>
      </Card>

      {/* Context Documents Section */}
      <ContextDocumentsSection traceId={trace.trace_id} />

      {/* Knowledge Graphs using KnowledgeGraphTree */}
      {knowledgeGraphs.length > 0 ? (
        <Card>
          <CardHeader>
            <CardTitle className="flex items-center justify-between">
              <div className="flex items-center gap-2">
                <Activity className="h-5 w-5" />
                Agent Graphs ({totalKGs})
              </div>
              <div className="flex items-center gap-2">
                <Button
                  onClick={handleGenerateKG}
                  disabled={isGenerating || isPolling}
                  size="sm"
                  className="bg-primary hover:bg-primary/90"
                >
                  {isGenerating || isPolling ? (
                    <>
                      <div className="w-3 h-3 border-2 border-white border-t-transparent rounded-full animate-spin mr-2" />
                      {generationProgress > 0
                        ? `${Math.round(generationProgress)}%`
                        : "Generating..."}
                    </>
                  ) : (
                    <>
                      <Plus className="h-3 w-3 mr-2" />
                      Generate
                    </>
                  )}
                </Button>
                <div className="flex items-center gap-2 text-sm text-muted-foreground">
                  <span>{finalKGs} Final</span>
                  <span></span>
                  <span>{completedPipelines} Completed</span>
                </div>
              </div>
            </CardTitle>
          </CardHeader>
          <CardContent className="pt-0">
            <KnowledgeGraphTree
              knowledgeGraphs={knowledgeGraphs}
              _onViewKg={handleViewKnowledgeGraph}
              onDeleteKg={handleDeleteKnowledgeGraph}
              onReplayKg={handleReplayKg}
              onViewSegment={handleViewSegment}
              currentTraceId={trace.trace_id}
            />
          </CardContent>
        </Card>
      ) : (
        <Card>
          <CardContent className="p-6 text-center empty-state">
            <div className="text-muted-foreground">
              <Activity className="h-8 w-8 mx-auto mb-3 empty-state-icon" />
              <p className="font-medium mb-2">Ready to analyze this trace</p>
              <p className="text-sm text-muted-foreground mb-1">
                Transform your trace data into an interactive agent graph.
              </p>
              <p className="text-xs text-muted-foreground mb-4">
                Choose from different chunking strategies for optimal analysis.
              </p>
            </div>
            <Button
              onClick={handleGenerateKG}
              className="mt-2 bg-primary hover:bg-primary/90"
              size="sm"
            >
              <Plus className="h-4 w-4 mr-2" />
              Generate Agent Graph
            </Button>
          </CardContent>
        </Card>
      )}

      {/* Metadata */}
      {trace.metadata && Object.keys(trace.metadata).length > 0 && (
        <Card>
          <CardHeader>
            <CardTitle>Metadata</CardTitle>
          </CardHeader>
          <CardContent>
            <div className="space-y-2">
              {Object.entries(trace.metadata).map(([key, value]) => (
                <div key={key} className="flex justify-between">
                  <span className="text-sm font-medium">{key}:</span>
                  <span className="text-sm text-muted-foreground">
                    {typeof value === "object"
                      ? JSON.stringify(value, null, 2)
                      : String(value)}
                  </span>
                </div>
              ))}
            </div>
          </CardContent>
        </Card>
      )}

      {/* Actions */}
      <div className="flex gap-2 pt-4 border-t action-button-group">
        <Button onClick={handleViewContent}>
          <FileText className="h-4 w-4 mr-2" />
          View Content
        </Button>
        <Button variant="outline" onClick={handleGenerateKG}>
          <Activity className="h-4 w-4 mr-2" />
          Generate Knowledge Graph
        </Button>
        <Button variant="outline" onClick={handleDownloadTrace}>
          <Download className="h-4 w-4 mr-2" />
          Download Trace
        </Button>
        <Button variant="destructive" onClick={handleDeleteTrace}>
          <Trash2 className="h-4 w-4 mr-2" />
          Delete Trace
        </Button>
      </div>

      <SplitterSelectionModal
        open={isSplitterModalOpen}
        onOpenChange={setIsSplitterModalOpen}
        onConfirm={handleSplitterConfirm}
        isLoading={isGenerating}
      />
    </div>
  );
}