// src/components/TokenRow.jsx /** * Displays tokens as colored chips. * Heat-maps each token by its average attention *to* the selected token * (column of the attention matrix for the selected source token). */ export default function TokenRow({ tokens, matrix, selectedToken, onSelectToken }) { if (!tokens || tokens.length === 0) return null; // Compute max for normalisation let maxVal = 0; if (matrix && selectedToken !== null) { const row = matrix[selectedToken] ?? []; maxVal = Math.max(...row, 1e-9); } const getColor = (idx) => { if (!matrix || selectedToken === null) { return { bg: 'var(--bg-input)', color: 'var(--text-secondary)' }; } const val = (matrix[selectedToken]?.[idx] ?? 0) / maxVal; // Gradient: dark-blue → indigo → gold const r = Math.round(99 + (245 - 99) * val); const g = Math.round(102 + (158 - 102) * val); const b = Math.round(241 + (11 - 241) * val); const alpha = 0.15 + val * 0.55; return { bg: `rgba(${r},${g},${b},${alpha})`, color: val > 0.5 ? '#fff' : 'var(--text-secondary)', borderColor: val > 0.3 ? `rgba(${r},${g},${b},0.7)` : 'var(--border)', }; }; return (