/** * Decision Path 3D Visualization * * Shows the exact path through the neural network when generating tokens, * highlighting critical layers, attention patterns, and decision factors. * This is the core "Glass Box" visualization for the PhD thesis. * * @component */ "use client"; import { useRef, useState, useEffect, useMemo } from "react"; import { Canvas, useFrame, useThree } from "@react-three/fiber"; import { OrbitControls, Text, Box, Sphere, Line, Billboard } from "@react-three/drei"; import * as THREE from "three"; import { Brain, Zap, Eye, GitBranch, Activity, Sparkles, AlertCircle, TrendingUp, Layers } from "lucide-react"; interface LayerActivation { layer_index: number; attention_weights: number[][]; hidden_state_norm: number; ffn_activation: number; top_attention_heads: number[]; confidence: number; } interface DecisionPath { token: string; token_id: number; probability: number; layer_activations: LayerActivation[]; attention_flow: Array<{ from_layer: number; to_layer: number | string; strength: number; top_heads: number[]; }>; alternatives: Array<{ token: string; token_id: number; probability: number; }>; decision_factors: { attention_focus: number; semantic_alignment: number; syntactic_correctness: number; context_relevance: number; confidence: number; }; critical_layers: number[]; confidence_score: number; timestamp: number; } // Enhanced Transformer Layer with activation visualization function EnhancedTransformerLayer({ position, layerIndex, activation, isCritical, isActive }: { position: [number, number, number]; layerIndex: number; activation?: LayerActivation; isCritical?: boolean; isActive?: boolean; }) { const meshRef = useRef(null); const glowRef = useRef(null); useFrame((state) => { if (meshRef.current && isCritical) { // Pulse critical layers const scale = 1 + Math.sin(state.clock.elapsedTime * 3) * 0.1; meshRef.current.scale.set(scale, scale, scale); } if (glowRef.current && activation) { // Glow based on activation strength glowRef.current.scale.set( 1 + activation.confidence * 0.3, 1 + activation.confidence * 0.3, 1 + activation.confidence * 0.3 ); } }); const baseColor = isCritical ? "#ff6b6b" : isActive ? "#4ecdc4" : "#2d3748"; const emissiveIntensity = activation ? activation.confidence : 0; return ( {/* Glow effect for active layers */} {activation && ( )} {/* Main layer box */} {/* Layer label */} Layer {layerIndex} {/* Confidence indicator */} {activation && ( Confidence: {(activation.confidence * 100).toFixed(0)}% )} {/* Attention head activation visualization */} {activation && activation.top_attention_heads.map((headIdx, i) => ( ))} ); } // Animated decision flow particle function DecisionParticle({ path, onComplete }: { path: THREE.Vector3[]; onComplete?: () => void; }) { const particleRef = useRef(null); const [progress, setProgress] = useState(0); useFrame((state, delta) => { if (particleRef.current && progress < 1) { const newProgress = Math.min(progress + delta * 0.3, 1); setProgress(newProgress); // Interpolate position along path const segmentCount = path.length - 1; const currentSegment = Math.floor(newProgress * segmentCount); const segmentProgress = (newProgress * segmentCount) % 1; if (currentSegment < segmentCount) { const start = path[currentSegment]; const end = path[currentSegment + 1]; particleRef.current.position.lerpVectors(start, end, segmentProgress); } if (newProgress >= 1 && onComplete) { onComplete(); } } }); return ( ); } // Attention flow visualization function AttentionFlowVisualization({ flow, layerSpacing }: { flow: DecisionPath['attention_flow']; layerSpacing: number; }) { return ( <> {flow.map((connection, idx) => { const startY = connection.from_layer * layerSpacing; const endY = connection.to_layer === "output" ? 20 * layerSpacing + 5 : (connection.to_layer as number) * layerSpacing; const points = [ new THREE.Vector3(0, startY, 0), new THREE.Vector3(0, endY, 0) ]; return ( 0.7 ? "#ff6b6b" : "#4ecdc4"} lineWidth={connection.strength * 5} transparent opacity={0.6} /> ); })} ); } // Alternative tokens display function AlternativesDisplay({ alternatives, position }: { alternatives: DecisionPath['alternatives']; position: [number, number, number]; }) { return ( Alternatives Considered: {alternatives.slice(0, 3).map((alt, idx) => ( {alt.token}: {(alt.probability * 100).toFixed(1)}% ))} ); } // Decision factors visualization function DecisionFactorsDisplay({ factors, position }: { factors: DecisionPath['decision_factors']; position: [number, number, number]; }) { const factorList = Object.entries(factors); return ( Decision Factors: {factorList.map(([key, value], idx) => { const barWidth = value * 2; return ( 0.7 ? "#4ecdc4" : value > 0.4 ? "#ffd93d" : "#ff6b6b"} emissive={value > 0.7 ? "#4ecdc4" : "#ffd93d"} emissiveIntensity={0.3} /> {key.replace(/_/g, ' ')}: {(value * 100).toFixed(0)}% ); })} ); } // Main 3D scene with decision path function DecisionPathScene({ decisionPath }: { decisionPath: DecisionPath | null }) { const numLayers = 20; const layerSpacing = 3.5; const [showParticle, setShowParticle] = useState(false); // Create path for particle animation const particlePath = useMemo(() => { if (!decisionPath) return []; const path: THREE.Vector3[] = [ new THREE.Vector3(0, -5, 0), // Start at input ]; // Add critical layers decisionPath.critical_layers.forEach(layerIdx => { path.push(new THREE.Vector3(0, layerIdx * layerSpacing, 0)); }); // End at output path.push(new THREE.Vector3(0, numLayers * layerSpacing + 5, 0)); return path; }, [decisionPath]); useEffect(() => { if (decisionPath) { setShowParticle(true); } }, [decisionPath]); return ( <> {/* Lighting */} {/* Input Layer */} Input Embeddings {/* Transformer Layers */} {Array.from({ length: numLayers }).map((_, i) => { const activation = decisionPath?.layer_activations[i]; const isCritical = decisionPath?.critical_layers.includes(i); return ( ); })} {/* Output Layer */} Output: {decisionPath?.token || "..."} Probability: {decisionPath ? (decisionPath.probability * 100).toFixed(1) : "0"}% {/* Attention Flow Visualization */} {decisionPath && ( )} {/* Decision Particle Animation */} {showParticle && particlePath.length > 0 && ( setShowParticle(false)} /> )} {/* Alternatives Display */} {decisionPath && ( )} {/* Decision Factors Display */} {decisionPath && ( )} {/* Grid */} ); } export default function DecisionPath3D() { const [decisionPath, setDecisionPath] = useState(null); const [isConnected, setIsConnected] = useState(false); const [isAnalyzing, setIsAnalyzing] = useState(false); const [isClient, setIsClient] = useState(false); const wsRef = useRef(null); const [prompt, setPrompt] = useState("def quicksort(arr):"); // Ensure client-side only rendering useEffect(() => { setIsClient(true); }, []); // Connect to decision path service useEffect(() => { if (!isClient) return; const connectToService = () => { try { const ws = new WebSocket('ws://localhost:8769'); ws.onopen = () => { console.log('[DecisionPath3D] Connected to service'); setIsConnected(true); wsRef.current = ws; }; ws.onmessage = (event) => { const data = JSON.parse(event.data); console.log('[DecisionPath3D] Received:', data.type); if (data.type === 'decision_path') { setDecisionPath(data.data); } else if (data.type === 'analysis_complete') { setIsAnalyzing(false); } }; ws.onerror = (error) => { console.log('[DecisionPath3D] Service not available'); setIsConnected(false); }; ws.onclose = () => { console.log('[DecisionPath3D] Disconnected'); setIsConnected(false); wsRef.current = null; }; } catch (error) { console.log('[DecisionPath3D] Connection failed'); setIsConnected(false); } }; connectToService(); return () => { if (wsRef.current) { wsRef.current.close(); } }; }, [isClient]); const startAnalysis = () => { if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) { setIsAnalyzing(true); wsRef.current.send(JSON.stringify({ type: 'analyze', prompt: prompt })); } }; return (
{/* Header */}

Decision Path Visualization

See exactly how the model makes its decisions - the Glass Box view

{isConnected ? 'Connected' : 'Disconnected'}
{/* Controls */}
setPrompt(e.target.value)} className="flex-1 px-3 py-2 bg-gray-900 text-white rounded-lg border border-gray-700 focus:border-blue-500 focus:outline-none font-mono text-sm" placeholder="Enter code to analyze..." />
{/* 3D Canvas */}
{isClient ? ( ) : (
Loading 3D visualization...
)} {/* Legend */}
Decision Path
Critical Layers
Active Layers
Top Attention Heads
Decision Flow
{/* Info Panel */} {decisionPath && (
Current Decision
Token: {decisionPath.token}
Confidence: {(decisionPath.confidence_score * 100).toFixed(0)}%
Critical Layers: {decisionPath.critical_layers.join(", ")}
)}
); }