Spaces:
Running
Running
File size: 15,836 Bytes
c2ea5ed cc3c5e9 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 |
import React, { useState, useEffect } from "react";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import {
Database,
Eye,
Download,
GitBranch,
Play,
Calendar,
Hash,
FileText,
Settings,
Network,
Loader,
} from "lucide-react";
import {
KnowledgeGraph,
WindowKnowledgeGraph,
Entity,
Relation,
} from "@/types";
import { formatRelativeTime } from "@/lib/utils";
import { SimpleGraphVisualization } from "../../features/workspace/SimpleGraphVisualization";
import { api } from "@/lib/api";
import { useAgentGraph } from "@/context/AgentGraphContext";
interface KnowledgeGraphModalProps {
data: {
knowledgeGraph: KnowledgeGraph;
windowGraphs?: WindowKnowledgeGraph[];
};
onClose: () => void;
}
interface GraphData {
entities: Entity[];
relations: Relation[];
metadata?: Record<string, any>;
}
export function KnowledgeGraphModal({
data,
onClose,
}: KnowledgeGraphModalProps) {
const { knowledgeGraph, windowGraphs = [] } = data;
const [graphData, setGraphData] = useState<GraphData | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const { actions } = useAgentGraph();
// Removed visualization type toggle - only using simple visualization in modal
// Fetch the actual graph data when component mounts
useEffect(() => {
const fetchGraphData = async () => {
if (!knowledgeGraph.id) return;
setIsLoading(true);
setError(null);
try {
const data = await api.knowledgeGraphs.getData(knowledgeGraph.id);
setGraphData(data);
} catch (err) {
console.error("Failed to fetch knowledge graph data:", err);
setError(
err instanceof Error ? err.message : "Failed to load graph data"
);
} finally {
setIsLoading(false);
}
};
fetchGraphData();
}, [knowledgeGraph.id]);
const getStatusColor = (status: KnowledgeGraph["status"]) => {
switch (status) {
case "created":
return "bg-green-500/10 text-green-700 border-green-200";
case "enriched":
return "bg-blue-500/10 text-blue-700 border-blue-200";
case "perturbed":
return "bg-purple-500/10 text-purple-700 border-purple-200";
case "analyzed":
return "bg-orange-500/10 text-orange-700 border-orange-200";
case "causal":
return "bg-red-500/10 text-red-700 border-red-200";
default:
return "bg-yellow-500/10 text-yellow-700 border-yellow-200";
}
};
const getPipelineStageStatus = (status?: string) => {
switch (status) {
case "completed":
return "bg-green-500/10 text-green-700";
case "running":
return "bg-blue-500/10 text-blue-700";
case "error":
return "bg-red-500/10 text-red-700";
default:
return "bg-gray-500/10 text-gray-700";
}
};
const formatDisplayName = (filename?: string, id?: string) => {
if (!filename) return `Knowledge Graph ${id?.slice(0, 8)}`;
// Strip the _knowledge_graph_{timestamp}_{uuid}.json part if present
if (filename.includes("_knowledge_graph_")) {
return filename.split("_knowledge_graph_")[0];
}
return filename;
};
return (
<Tabs defaultValue="details" className="w-full">
<TabsList className="grid w-full grid-cols-2">
<TabsTrigger value="details">Details</TabsTrigger>
<TabsTrigger value="visualization">
<Network className="h-4 w-4 mr-2" />
Visualization
</TabsTrigger>
</TabsList>
<TabsContent value="details" className="space-y-6 p-6">
<div className="space-y-6">
{/* Header */}
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<Database className="h-6 w-6" />
<div>
<h2 className="text-xl font-semibold">
{formatDisplayName(
knowledgeGraph.filename,
knowledgeGraph.id
)}
</h2>
<p className="text-sm text-muted-foreground">
Created {formatRelativeTime(knowledgeGraph.created_at)}
</p>
</div>
</div>
<div className="flex items-center gap-2">
<Badge
variant="outline"
className={getStatusColor(knowledgeGraph.status)}
>
{knowledgeGraph.status}
</Badge>
{knowledgeGraph.is_final && (
<Badge variant="secondary">Final</Badge>
)}
</div>
</div>
{/* Knowledge Graph Information */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Database className="h-5 w-5" />
Graph Information
</CardTitle>
</CardHeader>
<CardContent>
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
<div className="flex items-center gap-2">
<Hash className="h-4 w-4 text-muted-foreground" />
<div>
<p className="text-sm font-medium">Entities</p>
<p className="text-sm text-muted-foreground">
{knowledgeGraph.entity_count?.toLocaleString() || "N/A"}
</p>
</div>
</div>
<div className="flex items-center gap-2">
<GitBranch className="h-4 w-4 text-muted-foreground" />
<div>
<p className="text-sm font-medium">Relations</p>
<p className="text-sm text-muted-foreground">
{knowledgeGraph.relation_count?.toLocaleString() || "N/A"}
</p>
</div>
</div>
<div className="flex items-center gap-2">
<Calendar className="h-4 w-4 text-muted-foreground" />
<div>
<p className="text-sm font-medium">Created</p>
<p className="text-sm text-muted-foreground">
{new Date(knowledgeGraph.created_at).toLocaleDateString()}
</p>
</div>
</div>
<div className="flex items-center gap-2">
<FileText className="h-4 w-4 text-muted-foreground" />
<div>
<p className="text-sm font-medium">Status</p>
<p className="text-sm text-muted-foreground">
{knowledgeGraph.status}
</p>
</div>
</div>
{knowledgeGraph.window_total && (
<div className="flex items-center gap-2">
<Settings className="h-4 w-4 text-muted-foreground" />
<div>
<p className="text-sm font-medium">Windows</p>
<p className="text-sm text-muted-foreground">
{knowledgeGraph.window_total} total
</p>
</div>
</div>
)}
{knowledgeGraph.processing_run_id && (
<div className="flex items-center gap-2">
<Hash className="h-4 w-4 text-muted-foreground" />
<div>
<p className="text-sm font-medium">Run ID</p>
<p className="text-sm text-muted-foreground font-mono">
{knowledgeGraph.processing_run_id.slice(0, 8)}
</p>
</div>
</div>
)}
</div>
</CardContent>
</Card>
{/* Pipeline Status */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<GitBranch className="h-5 w-5" />
Pipeline Status
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-3">
<div className="flex items-center justify-between p-3 border rounded-lg">
<div className="flex items-center gap-2">
<Settings className="h-4 w-4" />
<span className="text-sm font-medium">
Prompt Reconstruction
</span>
</div>
<Badge
className={getPipelineStageStatus(
knowledgeGraph.is_enriched ? "completed" : "pending"
)}
>
{knowledgeGraph.is_enriched ? "Completed" : "Pending"}
</Badge>
</div>
<div className="flex items-center justify-between p-3 border rounded-lg">
<div className="flex items-center gap-2">
<Settings className="h-4 w-4" />
<span className="text-sm font-medium">
Perturbation Testing
</span>
</div>
<Badge
className={getPipelineStageStatus(
knowledgeGraph.is_perturbed ? "completed" : "pending"
)}
>
{knowledgeGraph.is_perturbed ? "Completed" : "Pending"}
</Badge>
</div>
<div className="flex items-center justify-between p-3 border rounded-lg">
<div className="flex items-center gap-2">
<Settings className="h-4 w-4" />
<span className="text-sm font-medium">Causal Analysis</span>
</div>
<Badge
className={getPipelineStageStatus(
knowledgeGraph.is_analyzed ? "completed" : "pending"
)}
>
{knowledgeGraph.is_analyzed ? "Completed" : "Pending"}
</Badge>
</div>
</div>
</CardContent>
</Card>
{/* Window Agent Graphs */}
{windowGraphs.length > 0 && (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Database className="h-5 w-5" />
Window Agent Graphs ({windowGraphs.length})
</CardTitle>
</CardHeader>
<CardContent>
<div className="space-y-3">
{windowGraphs.map((windowKg, index) => {
const entityCount = windowKg.entity_count || 0;
const relationCount = windowKg.relation_count || 0;
return (
<div
key={windowKg.kg_id}
className="flex items-center justify-between p-3 border rounded-lg"
>
<div className="flex items-center gap-3">
<div className="text-center min-w-[2rem]">
<span className="text-sm font-medium">
#{index + 1}
</span>
</div>
<div>
<p className="text-sm font-medium">
Characters{" "}
{windowKg.window_start_char?.toLocaleString()} -{" "}
{windowKg.window_end_char?.toLocaleString()}
</p>
<div className="flex items-center gap-3 text-xs text-muted-foreground">
<span>{entityCount} entities</span>
<span>{relationCount} relations</span>
<span>Window {index + 1}</span>
</div>
</div>
</div>
<div className="flex items-center gap-2">
<Button
variant="outline"
size="sm"
onClick={() => {
// Navigate to the window knowledge graph visualizer
const kgForVisualizer: KnowledgeGraph = {
...windowKg,
created_at: new Date().toISOString(),
filename:
windowKg.filename ||
`Window ${windowKg.window_index}`,
status:
(windowKg.status as KnowledgeGraph["status"]) ||
"created",
};
onClose();
actions.setSelectedKnowledgeGraph(
kgForVisualizer
);
actions.setActiveView("kg-visualizer");
}}
>
<Eye className="h-3 w-3 mr-1" />
View
</Button>
</div>
</div>
);
})}
</div>
</CardContent>
</Card>
)}
{/* Actions */}
<div className="flex gap-2 pt-4 border-t">
<Button variant="outline">
<Download className="h-4 w-4 mr-2" />
Download
</Button>
{knowledgeGraph.processing_run_id && windowGraphs.length > 1 && (
<Button variant="outline">
<Play className="h-4 w-4 mr-2" />
Replay Creation
</Button>
)}
</div>
</div>
</TabsContent>
<TabsContent value="visualization" className="p-6">
{isLoading ? (
<div className="flex items-center justify-center py-12">
<div className="text-center">
<Loader className="h-8 w-8 animate-spin mx-auto mb-4" />
<p className="text-sm text-muted-foreground">
Loading graph data...
</p>
</div>
</div>
) : error ? (
<div className="text-center py-12">
<div className="text-red-500 mb-4">Error: {error}</div>
<Button variant="outline" onClick={() => window.location.reload()}>
Retry
</Button>
</div>
) : (
<div className="space-y-4">
{/* Simple Visualization */}
<div className="text-center">
<h3 className="text-lg font-semibold">
Agent Graph Visualization
</h3>
<p className="text-sm text-muted-foreground">
For advanced interactive visualization, use the full-page
visualizer
</p>
</div>
<SimpleGraphVisualization
knowledgeGraph={knowledgeGraph}
width={550}
height={400}
graphData={graphData}
/>
</div>
)}
</TabsContent>
</Tabs>
);
}
|