File size: 25,192 Bytes
c2ea5ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df14457
c2ea5ed
 
df14457
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
import React, { useState, useEffect, useCallback } from "react";
import { TooltipProvider } from "@/components/ui/tooltip";
import { Trace, KnowledgeGraph } from "@/types";
import { useAgentGraph } from "@/context/AgentGraphContext";
import { useKGDisplayMode } from "@/context/KGDisplayModeContext";
import { filterKnowledgeGraphsByMode } from "@/lib/kg-selection";
import { AgentGraphsSection } from "@/components/features/traces/AgentGraphsSection";
import { TraceOverviewSection } from "@/components/features/traces/TraceOverviewSection";
import { ContextDocumentsModal } from "@/components/shared/modals/ContextDocumentsModal";
import { TraceContentModal } from "@/components/shared/modals/TraceContentModal";
import { api } from "@/lib/api";
import { useModal } from "@/context/ModalContext";
import { useNotification } from "@/context/NotificationContext";
import { useNavigation } from "@/context/NavigationContext";
import {
  SplitterSelectionModal,
  SplitterType,
  // ChunkingConfig,
} from "@/components/shared/modals/SplitterSelectionModal";
import { useTaskPolling } from "@/hooks/useTaskPolling";
import { MetadataCardConfig } from "@/components/shared/CompactMetadataCard";
import {
  MetadataCardSelector,
  AVAILABLE_CARDS,
  DEFAULT_CARDS,
} from "@/components/shared/modals/MetadataCardSelector";

interface TraceKnowledgeGraphViewProps {
  trace: Trace;
  knowledgeGraphs: KnowledgeGraph[];
}

export function TraceKnowledgeGraphView({
  trace,
  knowledgeGraphs: _knowledgeGraphs,
}: TraceKnowledgeGraphViewProps) {
  const { actions } = useAgentGraph();
  const {
    setActiveView, // Used in TraceContentModal onEditTrace
  } = actions;
  const { mode: kgDisplayMode } = useKGDisplayMode();
  const [kgData, setKgData] = useState<KnowledgeGraph[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  useModal(); // Keep for potential future use
  const { showNotification } = useNotification();
  const { actions: navigationActions } = useNavigation();

  // Add state for trace content
  const [traceContent, setTraceContent] = useState<string>("");
  const [traceContentLoading, setTraceContentLoading] = useState(false);
  const [traceContentError, setTraceContentError] = useState<string | null>(
    null
  );

  // State for metadata regeneration
  const [metadataOutdated, setMetadataOutdated] = useState(false);
  const [updatedMetadata, setUpdatedMetadata] = useState<any>(null);
  const [updatedTraceData, setUpdatedTraceData] = useState<any>(null);

  // State for enhanced statistics
  const [enhancedStats, setEnhancedStats] = useState<any>(null);
  const [enhancedStatsLoading, setEnhancedStatsLoading] = useState(false);

  // Get current metadata (use updated if available, otherwise original)
  const currentMetadata = updatedMetadata || trace.metadata;

  // 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]
  );

  // Function to check if metadata is outdated
  const checkMetadataFreshness = useCallback(
    (trace: Trace, content: string, metadata: any, updatedTraceData?: any) => {
      if (!metadata?.schema_analytics || !content) {
        return false; // No metadata to check
      }

      // Check 1: JSON validity - if current content is invalid JSON, metadata is definitely outdated
      try {
        JSON.parse(content);
      } catch (e) {
        return true; // Invalid JSON means metadata is outdated
      }

      // Check 2: Content length difference (>5% threshold)
      const currentLength = content.length;
      // Use updated trace data if available, otherwise use original trace
      const metadataLength =
        updatedTraceData?.character_count || trace.character_count || 0;
      if (metadataLength > 0) {
        const lengthDifference =
          Math.abs(currentLength - metadataLength) / metadataLength;
        if (lengthDifference > 0.05) {
          return true;
        }
      }

      // Check 3: Character count mismatch (exact)
      if (currentLength !== metadataLength) {
        return true;
      }

      return false;
    },
    []
  );

  // Check metadata freshness when trace content is loaded
  useEffect(() => {
    if (traceContent && trace) {
      const outdated = checkMetadataFreshness(
        trace,
        traceContent,
        currentMetadata,
        updatedTraceData
      );
      setMetadataOutdated(outdated);
    }
  }, [
    traceContent,
    trace,
    currentMetadata,
    updatedTraceData,
    checkMetadataFreshness,
  ]);

  // Auto-refresh metadata when outdated
  useEffect(() => {
    const autoRefreshMetadata = async () => {
      if (!metadataOutdated || !trace.trace_id) return;

      try {
        // Call the universal parser endpoint to regenerate metadata
        const response = await fetch(
          `/api/traces/${trace.trace_id}/regenerate-metadata`,
          {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
          }
        );

        if (!response.ok) {
          throw new Error("Failed to regenerate metadata");
        }

        // Fetch updated trace data
        const traceResponse = await fetch(`/api/traces/${trace.trace_id}`);
        if (traceResponse.ok) {
          const response = await traceResponse.json();
          const updatedTrace = response.trace; // Extract trace from response wrapper
          // Store the updated metadata and trace data locally
          setUpdatedMetadata(updatedTrace.metadata);
          setUpdatedTraceData(updatedTrace);

          // Reset metadata outdated flag
          setMetadataOutdated(false);
        }
      } catch (error) {
        console.error("Auto-refresh metadata failed:", error);
      }
    };

    // Auto-refresh metadata every 90 seconds if outdated (reduced frequency for HF Spaces)
    const interval = setInterval(() => {
      autoRefreshMetadata();
    }, 90000); // 90 seconds (increased from 30s to avoid 429 errors)

    return () => clearInterval(interval);
  }, [metadataOutdated, trace.trace_id]);

  const [isSplitterModalOpen, setIsSplitterModalOpen] = useState(false);
  const [isContextDocumentsModalOpen, setIsContextDocumentsModalOpen] =
    useState(false);
  const [isTraceContentModalOpen, setIsTraceContentModalOpen] = useState(false);
  const [isGenerating, setIsGenerating] = useState(false);
  const [generationProgress, setGenerationProgress] = useState(0);

  // Load trace content (clean content for overview display)
  const fetchTraceContent = useCallback(async () => {
    if (!trace.trace_id) return;

    setTraceContentLoading(true);
    setTraceContentError(null);

    try {
      // Use clean content for overview display, not numbered content
      const content = await api.traces.getContent(trace.trace_id);
      setTraceContent(content);
    } catch (err) {
      setTraceContentError(
        err instanceof Error ? err.message : "Failed to load trace content"
      );
    } finally {
      setTraceContentLoading(false);
    }
  }, [trace.trace_id]);

  const fetchKnowledgeGraphs = useCallback(async () => {
    if (!trace.trace_id) return;

    setLoading(true);
    setError(null);

    try {
      // Also check if trace already has knowledge graphs
      console.log("Trace object:", trace);
      console.log("Trace knowledge_graphs:", trace.knowledge_graphs);
      const response = await fetch(
        `/api/traces/${trace.trace_id}/knowledge-graphs`
      );

      if (!response.ok) {
        throw new Error(
          `Failed to fetch knowledge graphs: ${response.statusText}`
        );
      }

      const data = await response.json();

      console.log("Raw API response:", data);

      if (data?.knowledge_graphs?.length) {
        console.log(
          "Total knowledge graphs received:",
          data.knowledge_graphs.length
        );

        // Log each KG to see their structure
        data.knowledge_graphs.forEach((kg: any, index: number) => {
          console.log(`KG ${index}:`, {
            id: kg.id,
            kg_id: kg.kg_id,
            is_final: kg.is_final,
            window_index: kg.window_index,
            window_total: kg.window_total,
            filename: kg.filename,
          });
        });

        // Filter for final graphs using the exact logic from stage_processor.js
        const finalKGs = data.knowledge_graphs.filter((kg: any) => {
          const hasValidId = Boolean(kg.kg_id || kg.id);
          const isFinal =
            kg.is_final === true ||
            (kg.window_index === null && kg.window_total !== null);

          if (!hasValidId) {
            console.warn(
              "Found knowledge graph with missing ID - skipping:",
              kg
            );
            return false;
          }

          if (!isFinal) {
            console.log(
              "Skipping non-final KG:",
              kg.kg_id,
              "window_index:",
              kg.window_index,
              "is_final:",
              kg.is_final
            );
          } else {
            console.log(
              "Including final KG:",
              kg.kg_id,
              "is_final:",
              kg.is_final,
              "window_index:",
              kg.window_index,
              "window_total:",
              kg.window_total
            );
          }

          return isFinal;
        });

        console.log("Filtered final KGs:", finalKGs.length, finalKGs);

        // Apply display mode filtering
        const filteredByMode = filterKnowledgeGraphsByMode(
          finalKGs,
          kgDisplayMode
        );
        console.log(
          `Applied ${kgDisplayMode} mode filtering:`,
          filteredByMode.length,
          "KGs"
        );

        setKgData(filteredByMode);
      } else {
        console.log(
          "No knowledge graphs in API response, checking trace object"
        );
        // Fallback to trace.knowledge_graphs if API returns empty
        if (trace.knowledge_graphs && trace.knowledge_graphs.length > 0) {
          console.log("Using trace.knowledge_graphs:", trace.knowledge_graphs);
          setKgData(trace.knowledge_graphs);
        } else {
          setKgData([]);
        }
      }
    } catch (err) {
      console.log("API call failed, trying trace.knowledge_graphs fallback");
      setError(
        err instanceof Error ? err.message : "Failed to fetch knowledge graphs"
      );

      // Fallback to trace data if API fails
      if (trace.knowledge_graphs && trace.knowledge_graphs.length > 0) {
        console.log(
          "Using trace.knowledge_graphs as fallback:",
          trace.knowledge_graphs
        );
        setKgData(trace.knowledge_graphs);
        setError(null); // Clear error since we found data
      } else {
        setKgData([]);
      }
    } finally {
      setLoading(false);
    }
  }, [trace, kgDisplayMode]);

  // Fetch enhanced statistics
  const fetchEnhancedStatistics = useCallback(async () => {
    if (!trace.trace_id) return;

    setEnhancedStatsLoading(true);
    try {
      const response = await api.traces.getEnhancedStatistics(trace.trace_id);
      setEnhancedStats(response.enhanced_statistics);
    } catch (err) {
      console.error("Failed to fetch enhanced statistics:", err);
      setEnhancedStats(null);
    } finally {
      setEnhancedStatsLoading(false);
    }
  }, [trace.trace_id]);

  useEffect(() => {
    fetchKnowledgeGraphs();
    fetchTraceContent();
    fetchEnhancedStatistics();
  }, [fetchKnowledgeGraphs, fetchTraceContent, fetchEnhancedStatistics]);

  // Auto-refresh knowledge graphs every 10 seconds
  useEffect(() => {
    const interval = setInterval(() => {
      if (!loading && !isGenerating) {
        fetchKnowledgeGraphs();
      }
    }, 10000); // 10 seconds

    return () => clearInterval(interval);
  }, [fetchKnowledgeGraphs, loading, isGenerating]);

  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 knowledge graphs list
      fetchKnowledgeGraphs();
      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,
    model?: string,
    chunkingConfig?: { min_chunk_size?: number; max_chunk_size?: number }
  ) => {
    const finalMethodName = methodName || "production";
    const finalModel = model || "gpt-5-mini";
    console.log("TraceKnowledgeGraphView: Using method name:", finalMethodName);
    console.log("TraceKnowledgeGraphView: Using model:", finalModel);
    console.log(
      "TraceKnowledgeGraphView: 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, // force_regenerate = true to allow generating new graphs even if existing ones exist
        finalMethodName,
        finalModel,
        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
        await fetchKnowledgeGraphs();
        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);
      showNotification({
        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 (matching stage_processor.js)
  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;
    }
  };

  // Metadata cards state management
  const [selectedMetadataCards, setSelectedMetadataCards] = useState<string[]>(
    () => {
      const saved = localStorage.getItem("metadata-cards-selection");
      return saved ? JSON.parse(saved) : DEFAULT_CARDS;
    }
  );
  const [isMetadataCardSelectorOpen, setIsMetadataCardSelectorOpen] =
    useState(false);

  // Save metadata card selection to localStorage
  const handleSaveMetadataCards = (cards: string[]) => {
    setSelectedMetadataCards(cards);
    localStorage.setItem("metadata-cards-selection", JSON.stringify(cards));
  };

  // Generate metadata card configs
  const generateMetadataCards = (): MetadataCardConfig[] => {
    const formatDate = (timestamp?: string) => {
      if (!timestamp) return "N/A";
      return new Date(timestamp).toLocaleDateString();
    };

    const formatCurrency = (amount: number | string): string => {
      const num = typeof amount === "string" ? parseFloat(amount) : amount;
      if (isNaN(num) || num === 0) return "$0.00";
      if (num < 0.01) return `$${num.toFixed(6)}`;
      return `$${num.toFixed(4)}`;
    };

    const getCardValue = (cardId: string): string | number => {
      // Get schema analytics from trace metadata
      const schemaAnalytics = currentMetadata?.schema_analytics;

      // Use enhanced stats if available and not loading
      const stats =
        !enhancedStatsLoading && enhancedStats ? enhancedStats : null;

      switch (cardId) {
        case "size":
          return trace.character_count || 0;
        case "type":
          return trace.trace_type || "Unknown";
        case "uploaded":
          return formatDate(trace.upload_timestamp);
        case "graphs":
          return kgData.length;
        case "modified":
          return formatDate(trace.upload_timestamp);

        case "source":
          return "Manual Upload";
        case "method":
          return "Production";

        // Schema analytics cards
        case "depth":
          return (
            stats?.components?.max_depth ||
            schemaAnalytics?.numerical_overview?.component_stats?.max_depth ||
            "N/A"
          );
        case "execution_time": {
          const totalTimeMs =
            stats?.performance?.total_execution_time_ms ||
            schemaAnalytics?.numerical_overview?.timing_analytics
              ?.total_execution_time_ms;
          if (totalTimeMs && totalTimeMs > 0) {
            if (totalTimeMs >= 1000) {
              return `${(totalTimeMs / 1000).toFixed(1)}s`;
            } else {
              return `${totalTimeMs}ms`;
            }
          }
          return "N/A";
        }
        case "total_tokens":
          return (
            stats?.tokens?.total_tokens ||
            schemaAnalytics?.numerical_overview?.token_analytics
              ?.total_tokens ||
            "N/A"
          );
        case "prompt_calls":
          return (
            stats?.prompt_calls?.total_calls ||
            schemaAnalytics?.prompt_analytics?.prompt_calls_detected ||
            "N/A"
          );
        case "components":
          return (
            stats?.components?.total_components ||
            schemaAnalytics?.numerical_overview?.component_stats
              ?.total_components ||
            "N/A"
          );
        case "success_rate": {
          const successRate =
            stats?.components?.success_rate ||
            schemaAnalytics?.numerical_overview?.component_stats?.success_rate;
          if (successRate !== undefined && successRate !== null) {
            return `${successRate.toFixed(1)}%`;
          }
          return "N/A";
        }

        // Cost cards
        case "total_cost":
          return enhancedStatsLoading
            ? "Loading..."
            : stats?.cost?.total_cost_usd
            ? formatCurrency(stats.cost.total_cost_usd)
            : "N/A";
        case "cost_per_call":
          return enhancedStatsLoading
            ? "Loading..."
            : stats?.cost?.avg_cost_per_call_usd
            ? formatCurrency(stats.cost.avg_cost_per_call_usd)
            : "N/A";
        case "input_cost":
          return enhancedStatsLoading
            ? "Loading..."
            : stats?.cost?.input_cost_usd
            ? formatCurrency(stats.cost.input_cost_usd)
            : "N/A";
        case "output_cost":
          return enhancedStatsLoading
            ? "Loading..."
            : stats?.cost?.output_cost_usd
            ? formatCurrency(stats.cost.output_cost_usd)
            : "N/A";

        // Token analytics cards
        case "avg_input_tokens":
          return enhancedStatsLoading
            ? "Loading..."
            : stats?.prompt_calls?.avg_prompt_tokens_per_call
            ? Math.round(stats.prompt_calls.avg_prompt_tokens_per_call)
            : "N/A";
        case "avg_output_tokens":
          return enhancedStatsLoading
            ? "Loading..."
            : stats?.prompt_calls?.avg_completion_tokens_per_call
            ? Math.round(stats.prompt_calls.avg_completion_tokens_per_call)
            : "N/A";

        default:
          return "N/A";
      }
    };

    return selectedMetadataCards
      .map((cardId) => {
        const cardConfig = AVAILABLE_CARDS.find((card) => card.id === cardId);
        if (!cardConfig) return null;

        return {
          ...cardConfig,
          value: getCardValue(cardId),
        };
      })
      .filter((card): card is MetadataCardConfig => card !== null);
  };

  return (
    <TooltipProvider delayDuration={300} skipDelayDuration={100}>
      <div className="flex-1 flex flex-col p-4 space-y-4 min-h-0">
        {/* Main Content */}
        <div className="flex-1 space-y-4">
          {/* Main Side-by-Side Section - Agent Graphs (Left) and Trace Overview (Right) */}
          <div className="flex-1 flex bg-background border border-border/30 rounded-lg min-h-0 gap-4 p-4">
            {/* Left Column - Agent Graphs */}
            <div className="flex-1 flex flex-col min-h-0">
              <AgentGraphsSection
                trace={trace}
                kgData={kgData}
                loading={loading}
                error={error}
                isGenerating={isGenerating}
                isPolling={isPolling}
                generationProgress={generationProgress}
                onGenerateKG={handleGenerateKG}
                onRefreshKnowledgeGraphs={fetchKnowledgeGraphs}
                onOpenContextDocuments={() =>
                  setIsContextDocumentsModalOpen(true)
                }
              />
            </div>

            {/* Right Column - Trace Overview */}
            <div className="flex-1 flex flex-col min-h-0">
              <TraceOverviewSection
                trace={trace}
                metadataOutdated={metadataOutdated}
                currentMetadata={currentMetadata}
                enhancedStats={enhancedStats}
                enhancedStatsLoading={enhancedStatsLoading}
                onOpenTraceContent={() => setIsTraceContentModalOpen(true)}
                metadataCards={generateMetadataCards()}
                onOpenMetadataCardSelector={() =>
                  setIsMetadataCardSelectorOpen(true)
                }
              />
            </div>
          </div>
        </div>

        {/* Splitter Selection Modal */}
        <SplitterSelectionModal
          open={isSplitterModalOpen}
          onOpenChange={setIsSplitterModalOpen}
          onConfirm={handleSplitterConfirm}
          isLoading={isGenerating}
        />

        {/* Metadata Card Selector Modal */}
        <MetadataCardSelector
          open={isMetadataCardSelectorOpen}
          onOpenChange={setIsMetadataCardSelectorOpen}
          selectedCards={selectedMetadataCards}
          onSave={handleSaveMetadataCards}
          trace={trace}
          graphCount={kgData.length}
        />

        {/* Context Documents Modal */}
        <ContextDocumentsModal
          isOpen={isContextDocumentsModalOpen}
          onClose={() => setIsContextDocumentsModalOpen(false)}
          traceId={trace.trace_id}
        />

        {/* Trace Content Modal */}
        <TraceContentModal
          isOpen={isTraceContentModalOpen}
          onClose={() => setIsTraceContentModalOpen(false)}
          trace={trace}
          traceContent={traceContent}
          traceContentLoading={traceContentLoading}
          traceContentError={traceContentError}
          onEditTrace={() => setActiveView("trace-editor")}
        />
      </div>
    </TooltipProvider>
  );
}