File size: 13,693 Bytes
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
/**
 * Agent Graphs Section Component
 *
 * Displays and manages agent graphs for a trace, extracted from TraceKnowledgeGraphView
 */

import React from "react";
import { Button } from "@/components/ui/button";
import { CardTitle } from "@/components/ui/card";
import {
  Tooltip,
  TooltipContent,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { FileText, Activity, AlertCircle, Plus } from "lucide-react";
import { Trace, KnowledgeGraph } from "@/types";
import { KnowledgeGraphTree } from "@/components/features/traces/KnowledgeGraphTree";
// import { useKGDisplayMode } from "@/context/KGDisplayModeContext";
// import { getDisplayModeDescription } from "@/lib/kg-selection";
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";

interface AgentGraphsSectionProps {
  trace: Trace;
  kgData: KnowledgeGraph[];
  loading: boolean;
  error: string | null;
  isGenerating: boolean;
  isPolling: boolean;
  generationProgress: number;
  onGenerateKG: () => void;
  onRefreshKnowledgeGraphs: () => void;
  onOpenContextDocuments: () => void;
}

export function AgentGraphsSection({
  trace,
  kgData,
  loading,
  error,
  isGenerating,
  isPolling,
  generationProgress,
  onGenerateKG,
  onRefreshKnowledgeGraphs,
  onOpenContextDocuments,
}: AgentGraphsSectionProps) {
  // const { mode: kgDisplayMode } = useKGDisplayMode();
  const { openModal } = useModal();
  const { showNotification } = useNotification();
  const { actions: navigationActions } = useNavigation();
  const { actions } = useAgentGraph();

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

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

    console.log("Selected KG:", kg);
    actions.setSelectedKnowledgeGraph(kg);
    actions.setActiveView("kg-visualizer");
  };

  return (
    <div className="h-full">
      {/* Agent Graphs Section */}
      <div className="border border-border/50 rounded-lg bg-background h-full">
        <div className="p-4 pb-3">
          <div className="flex items-center justify-between">
            <div className="flex items-center gap-2 flex-1 min-w-0">
              <Activity className="h-5 w-5 text-primary" />
              <CardTitle className="text-lg">Agent Graphs</CardTitle>
            </div>

            {/* Action buttons in top right */}
            <div className="flex items-center gap-2">
              {/* Context Documents Button */}
              <Tooltip>
                <TooltipTrigger asChild>
                  <Button
                    onClick={onOpenContextDocuments}
                    size="sm"
                    variant="outline"
                    className="gap-1"
                  >
                    <FileText className="h-4 w-4" />
                    <span className="hidden sm:inline">Context</span>
                  </Button>
                </TooltipTrigger>
                <TooltipContent>
                  <div className="max-w-sm">
                    <p className="font-medium mb-1 text-primary-foreground">
                      Context Documents
                    </p>
                    <p className="text-xs text-primary-foreground/80">
                      Manage domain knowledge, schemas, and guidelines to
                      improve extraction quality.
                    </p>
                  </div>
                </TooltipContent>
              </Tooltip>

              {/* Generate Button */}
              <Tooltip>
                <TooltipTrigger asChild>
                  <Button
                    onClick={onGenerateKG}
                    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" />
                        <span className="hidden sm:inline">
                          {generationProgress > 0
                            ? `Generating... ${Math.round(generationProgress)}%`
                            : "Generating..."}
                        </span>
                        <span className="sm:hidden">
                          {generationProgress > 0
                            ? `${Math.round(generationProgress)}%`
                            : "..."}
                        </span>
                      </>
                    ) : (
                      <>
                        <Plus className="h-4 w-4 mr-2" />
                        <span className="hidden sm:inline">Generate</span>
                        <span className="sm:hidden">+</span>
                      </>
                    )}
                  </Button>
                </TooltipTrigger>
                <TooltipContent>
                  <div className="max-w-sm">
                    <p className="font-medium mb-1 text-primary-foreground">
                      Generate New Agent Graph
                    </p>
                    <p className="text-xs text-primary-foreground/80">
                      Create a new agent graph with different chunking
                      strategies. You can generate multiple graphs to compare
                      approaches and insights.
                    </p>
                  </div>
                </TooltipContent>
              </Tooltip>
            </div>
          </div>

          {/* Subtitle */}
          <p className="text-sm text-muted-foreground">
            Generated knowledge graphs from this trace
          </p>
        </div>

        <div className="p-4 space-y-4 flex-1 overflow-hidden">
          {loading ? (
            <div className="space-y-4">
              <div className="h-8 w-full bg-muted animate-pulse rounded" />
              <div className="h-6 w-3/4 bg-muted animate-pulse rounded" />
              <div className="h-6 w-1/2 bg-muted animate-pulse rounded" />
            </div>
          ) : error ? (
            <div className="border border-destructive/50 bg-destructive/10 rounded-lg p-4">
              <div className="flex items-center gap-2 text-destructive">
                <AlertCircle className="h-4 w-4" />
                <span className="text-sm">{error}</span>
                <Button
                  variant="outline"
                  size="sm"
                  onClick={onRefreshKnowledgeGraphs}
                  className="ml-2"
                >
                  Try Again
                </Button>
              </div>
            </div>
          ) : kgData.length > 0 ? (
            <>
              {/* Display mode indicator
              <div className="mb-4 p-3 bg-muted/20 rounded-lg border">
                <div className="flex items-center gap-2 text-sm text-muted-foreground">
                  <Info className="h-4 w-4" />
                  {getDisplayModeDescription(kgDisplayMode)}
                </div>
              </div> */}

              <KnowledgeGraphTree
                knowledgeGraphs={kgData}
                _onViewKg={handleViewKnowledgeGraph}
                onDeleteKg={async (kgId, name) => {
                  const confirmMessage = `Are you sure you want to delete the knowledge graph "${name}"?

This will permanently remove:
- All extracted entities and relationships
- Generated insights and analysis
- Historical comparison data

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

                    // Refresh the knowledge graphs list
                    onRefreshKnowledgeGraphs();
                  } catch (error) {
                    console.error("Error deleting knowledge graph:", error);
                    alert(
                      `Error deleting knowledge graph: ${
                        error instanceof Error ? error.message : String(error)
                      }`
                    );
                  }
                }}
                onReplayKg={async (kgId, traceId, runId) => {
                  try {
                    // Import the temporal API functions
                    const { fetchTemporalGraphData, validateTemporalData } =
                      await import("@/lib/temporal-api");

                    // Validate temporal data availability
                    const isValid = await validateTemporalData(traceId, runId);
                    if (!isValid) {
                      showSystemNotification({
                        type: "error",
                        title: "Insufficient Data",
                        message:
                          "Need at least 2 window knowledge graphs for temporal replay.",
                      });
                      return;
                    }

                    // Fetch temporal data
                    const temporalData = await fetchTemporalGraphData(
                      traceId,
                      runId
                    );

                    // Set temporal data and navigate to visualizer
                    actions.setSelectedTemporalData(temporalData);
                    actions.setActiveView("temporal-visualizer");

                    showSystemNotification({
                      type: "success",
                      title: "Temporal Visualization",
                      message:
                        "Loading temporal knowledge graph visualization...",
                    });
                  } catch (error) {
                    console.error("Error starting temporal replay:", error);
                    showSystemNotification({
                      type: "error",
                      title: "Replay Error",
                      message: "Failed to load temporal data for replay.",
                    });
                  }
                }}
                onViewSegment={async (traceId, start, end, window) => {
                  try {
                    const segmentData = await api.traces.extractSegment(
                      traceId,
                      start,
                      end
                    );

                    openModal(
                      "trace-segment",
                      `Trace Segment - Window ${window}`,
                      {
                        trace: trace,
                        segment: {
                          content: segmentData.content,
                          startChar: start,
                          endChar: end,
                          windowIndex: window,
                        },
                      },
                      {
                        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.",
                    });
                  }
                }}
                currentTraceId={trace.trace_id}
              />
            </>
          ) : (
            <div className="flex-1 flex items-center justify-center py-12">
              <div className="text-center space-y-4">
                <div className="p-4 rounded-full bg-muted/50 w-16 h-16 flex items-center justify-center mx-auto">
                  <Activity className="h-8 w-8 text-muted-foreground" />
                </div>
                <div>
                  <p className="text-sm font-medium text-muted-foreground mb-1">
                    No Agent Graphs Yet
                  </p>
                  <p className="text-xs text-muted-foreground">
                    Generate your first agent graph to start analyzing this
                    trace
                  </p>
                </div>
                <Button onClick={onGenerateKG} className="mt-4">
                  <Plus className="h-4 w-4 mr-2" />
                  Generate First Graph
                </Button>
              </div>
            </div>
          )}
        </div>
      </div>
    </div>
  );
}