"use client"; import { useState, useMemo, useCallback } from "react"; interface GraphNode { id: string; label: string; type: string; x: number; y: number; } interface GraphEdge { source: string; target: string; label: string; } const TYPE_COLORS: Record = { PERSON: "#FF6B6B", ORGANIZATION: "#4ECDC4", LOCATION: "#45B7D1", EVENT: "#FFA07A", DATE: "#98D8C8", CONCEPT: "#AED6F1", WORK: "#F9E79F", QUERY: "#FF6B00", }; const DEMO_NODES: GraphNode[] = [ { id: "q", label: "Query", type: "QUERY", x: 400, y: 60 }, { id: "sd", label: "Scott Derrickson", type: "PERSON", x: 200, y: 180 }, { id: "ew", label: "Ed Wood", type: "PERSON", x: 600, y: 180 }, { id: "us", label: "United States", type: "LOCATION", x: 400, y: 300 }, { id: "denver", label: "Denver, CO", type: "LOCATION", x: 150, y: 320 }, { id: "pough", label: "Poughkeepsie, NY", type: "LOCATION", x: 650, y: 320 }, { id: "sinister", label: "Sinister (2012)", type: "WORK", x: 100, y: 200 }, { id: "planNine", label: "Plan 9 from Outer Space", type: "WORK", x: 700, y: 200 }, { id: "horror", label: "Horror Genre", type: "CONCEPT", x: 400, y: 420 }, ]; const DEMO_EDGES: GraphEdge[] = [ { source: "q", target: "sd", label: "FOUND" }, { source: "q", target: "ew", label: "FOUND" }, { source: "sd", target: "denver", label: "BORN_IN" }, { source: "ew", target: "pough", label: "BORN_IN" }, { source: "denver", target: "us", label: "LOCATED_IN" }, { source: "pough", target: "us", label: "LOCATED_IN" }, { source: "sd", target: "sinister", label: "DIRECTED" }, { source: "ew", target: "planNine", label: "DIRECTED" }, { source: "sinister", target: "horror", label: "GENRE" }, { source: "planNine", target: "horror", label: "GENRE" }, ]; export function GraphExplorer() { const [query, setQuery] = useState("Were Scott Derrickson and Ed Wood of the same nationality?"); const [selectedNode, setSelectedNode] = useState(null); const [hops, setHops] = useState(2); const [nodes] = useState(DEMO_NODES); const [edges] = useState(DEMO_EDGES); const nodeMap = useMemo(() => { const map: Record = {}; nodes.forEach((n) => { map[n.id] = n; }); return map; }, [nodes]); const selectedInfo = selectedNode ? nodeMap[selectedNode] : null; return (
{/* Controls */}
Knowledge Graph Explorer

Visualize how GraphRAG traverses the knowledge graph to find answers. Click on nodes to inspect entity details.

setQuery(e.target.value)} placeholder="Enter a question to explore…" />
{/* Graph Visualization */}
{/* Edges */} {edges.map((edge, i) => { const s = nodeMap[edge.source]; const t = nodeMap[edge.target]; if (!s || !t) return null; const mx = (s.x + t.x) / 2; const my = (s.y + t.y) / 2; return ( {edge.label} ); })} {/* Nodes */} {nodes.map((node) => { const color = TYPE_COLORS[node.type] || "#AED6F1"; const isSelected = selectedNode === node.id; const r = isSelected ? 24 : 18; return ( setSelectedNode(isSelected ? null : node.id)} > {/* Glow on select */} {isSelected && ( )} {node.label} ); })}
{/* Info Panel */}
{/* Node Details */}
Node Details
{selectedInfo ? (
{selectedInfo.label}
{selectedInfo.type}
Connected to {edges.filter( (e) => e.source === selectedInfo.id || e.target === selectedInfo.id ).length} other nodes
Connections:
{edges .filter((e) => e.source === selectedInfo.id || e.target === selectedInfo.id) .map((e, i) => { const other = e.source === selectedInfo.id ? e.target : e.source; return (
πŸ”— {e.label} β†’ {nodeMap[other]?.label || other}
); })}
) : (

Click a node on the graph to see its details.

)}
{/* Graph Stats */}
Graph Statistics
{[ { label: "Nodes", value: nodes.length }, { label: "Edges", value: edges.length }, { label: "Avg Degree", value: (edges.length * 2 / nodes.length).toFixed(1) }, { label: "Entity Types", value: new Set(nodes.map((n) => n.type)).size }, { label: "Hops Traversed", value: hops }, ].map((s, i) => (
{s.label} {s.value}
))}
{/* Legend */}
Entity Types
{Object.entries(TYPE_COLORS).map(([type, color]) => (
{type}
))}
{/* Reasoning Explanation */}
🧠 Graph Reasoning Path

1. Entry Points: The query identified two key entities β€”{" "} Scott Derrickson and Ed Wood.

2. Traversal: Following BORN_IN relationships: Scott Derrickson β†’ Denver, CO β†’ United States; Ed Wood β†’ Poughkeepsie, NY β†’ United States.

3. Evidence: Both paths converge at United States, confirming shared nationality through 2-hop graph traversal.

4. Conclusion: Yes β€” both directors are American. The graph reasoning path provides explicit, traceable evidence for the answer.

); }