/** * Decision Path 3D Visualization - Fixed Version * * 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 } from "@react-three/fiber"; import { OrbitControls } from "@react-three/drei"; import * as THREE from "three"; import { GitBranch, Activity, Sparkles, Zap, Brain } 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 Layer Component with proper FFN visualization interface LayerProps { position: [number, number, number]; layerIndex: number; isCritical: boolean; isActive: boolean; activation?: LayerActivation; } function Layer({ position, layerIndex, isCritical, isActive, activation }: LayerProps) { const meshRef = useRef(null); const ffnRef = useRef(null); useFrame((state) => { if (meshRef.current && isCritical) { const scale = 1 + Math.sin(state.clock.elapsedTime * 3) * 0.1; meshRef.current.scale.set(scale, scale, scale); } if (ffnRef.current && activation) { // Pulse FFN based on activation strength const ffnScale = 1 + (activation.ffn_activation * 0.2); ffnRef.current.scale.set(1, ffnScale, 1); } }); const baseColor = isCritical ? "#ff6b6b" : isActive ? "#4ecdc4" : "#2d3748"; const ffnColor = isCritical ? "#e91e63" : isActive ? "#9c27b0" : "#6b46c1"; return ( {/* Main attention layer */} {/* FFN Component - positioned behind */} {/* Attention heads visualization - small cubes */} {isActive && ( {Array.from({ length: 16 }).map((_, i) => ( ))} )} ); } // Simple scene function DecisionPathScene({ decisionPath }: { decisionPath: DecisionPath | null }) { const numLayers = 20; const layerSpacing = 3.5; return ( <> {/* Input Layer */} {/* Transformer Layers */} {Array.from({ length: numLayers }).map((_, i) => { const isCritical = decisionPath?.critical_layers?.includes(i) || false; const activation = decisionPath?.layer_activations?.[i]; return ( ); })} {/* Output Layer */} {/* Connection lines - simplified for now */} {decisionPath && decisionPath.critical_layers && decisionPath.critical_layers.map((layerIdx, idx) => { const startY = layerIdx * layerSpacing; const endY = idx < decisionPath.critical_layers.length - 1 ? decisionPath.critical_layers[idx + 1] * layerSpacing : numLayers * layerSpacing + 5; const points = []; points.push(new THREE.Vector3(0, startY, 0)); points.push(new THREE.Vector3(0, endY, 0)); const geometry = new THREE.BufferGeometry().setFromPoints(points); return ( ); })} ); } export default function DecisionPath3DFixed() { const [decisionPath, setDecisionPath] = useState(null); const [isConnected, setIsConnected] = useState(false); const [isAnalyzing, setIsAnalyzing] = useState(false); const [mounted, setMounted] = useState(false); const [modelLoading, setModelLoading] = useState(true); const [loadingProgress, setLoadingProgress] = useState(0); const [loadingMessage, setLoadingMessage] = useState("Initializing..."); const wsRef = useRef(null); const [prompt, setPrompt] = useState("def quicksort(arr):"); useEffect(() => { setMounted(true); }, []); useEffect(() => { if (!mounted) return; const connectToService = () => { try { const ws = new WebSocket('ws://localhost:8769'); ws.onopen = () => { console.log('[DecisionPath3D] Connected to service'); setIsConnected(true); wsRef.current = ws; // Don't immediately set as ready - wait for model_ready or loading_progress messages }; ws.onmessage = (event) => { console.log('[DecisionPath3D] Raw message received:', event.data); const data = JSON.parse(event.data); console.log('[DecisionPath3D] Parsed message type:', data.type); console.log('[DecisionPath3D] Message data:', data); if (data.type === 'decision_path') { console.log('[DecisionPath3D] Setting decision path with critical layers:', data.data?.critical_layers); setDecisionPath(data.data); } else if (data.type === 'analysis_complete') { console.log('[DecisionPath3D] Analysis complete'); setIsAnalyzing(false); } else if (data.type === 'loading_progress') { setLoadingProgress(data.progress); setLoadingMessage(data.message); if (data.progress === 100) { setModelLoading(false); } } else if (data.type === 'model_ready') { setModelLoading(false); setLoadingProgress(100); setLoadingMessage("Model ready!"); } else if (data.type === 'loading_error') { setModelLoading(false); setLoadingMessage(`Error: ${data.message}`); } }; 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(); } }; }, [mounted]); const startAnalysis = () => { console.log('[DecisionPath3D] Start analysis clicked'); console.log('[DecisionPath3D] WebSocket state:', wsRef.current?.readyState); console.log('[DecisionPath3D] Is connected:', isConnected); if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) { console.log('[DecisionPath3D] Sending analyze request with prompt:', prompt); setIsAnalyzing(true); wsRef.current.send(JSON.stringify({ type: 'analyze', prompt: prompt })); } else { console.log('[DecisionPath3D] WebSocket not ready, state:', wsRef.current?.readyState); } }; if (!mounted) { return (
Loading 3D visualization...
); } 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 */}
{modelLoading ? (
Loading Model
{loadingMessage}
{loadingProgress}%
356M parameters • 20 layers • 16 attention heads
) : ( )} {/* 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(", ")}
)}
); }