tfrere HF Staff commited on
Commit
9ec3e0f
·
1 Parent(s): 683a975

update post-citation

Browse files
app/plugins/rehype/post-citation.mjs CHANGED
@@ -138,12 +138,36 @@ export default function rehypeReferencesAndFootnotes() {
138
  return out;
139
  };
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  const removeFootnoteBackrefAnchors = (el) => {
142
  if (!isElement(el)) return;
 
 
 
143
  const kids = getChildren(el);
144
  for (let i = kids.length - 1; i >= 0; i--) {
145
  const child = kids[i];
146
  if (isElement(child)) {
 
 
 
147
  if (
148
  child.tagName === 'a' && (
149
  getAttr(child, 'data-footnote-backref') != null ||
@@ -159,11 +183,14 @@ export default function rehypeReferencesAndFootnotes() {
159
  el.children.splice(i, 1);
160
  continue;
161
  }
162
- // Recurse into element
163
  removeFootnoteBackrefAnchors(child);
164
  // If a wrapper like <sup> or <span> became empty, remove it
 
165
  const becameKids = getChildren(child);
166
- if ((child.tagName === 'sup' || child.tagName === 'span') && (!becameKids || becameKids.length === 0)) {
 
 
167
  el.children.splice(i, 1);
168
  }
169
  }
@@ -381,6 +408,28 @@ export default function rehypeReferencesAndFootnotes() {
381
  }
382
  }
383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
384
  // Footnotes cleanup + backrefs harmonized with references
385
  const cleanupFootnotes = () => {
386
  let root = null;
@@ -398,7 +447,9 @@ export default function rehypeReferencesAndFootnotes() {
398
  const items = getChildren(root).filter((n) => isElement(n) && (n.tagName === 'li' || hasClass(n, 'footnote') || n.tagName === 'p' || n.tagName === 'div'));
399
  if (items.length) {
400
  for (const it of items) {
401
- const li = { type: 'element', tagName: 'li', properties: {}, children: getChildren(it) };
 
 
402
  // Promote nested id if present (e.g., <p id="fn-1">)
403
  const nestedWithId = getChildren(it).find((n) => isElement(n) && getAttr(n, 'id'));
404
  if (nestedWithId) setAttr(li, 'id', getAttr(nestedWithId, 'id'));
@@ -415,6 +466,7 @@ export default function rehypeReferencesAndFootnotes() {
415
  if (nestedWithId) setAttr(li, 'id', getAttr(nestedWithId, 'id'));
416
  }
417
  // Remove default footnote backrefs anywhere inside (to avoid duplication)
 
418
  removeFootnoteBackrefAnchors(li);
419
  }
420
  setAttr(root, 'data-built-footnotes', '1');
 
138
  return out;
139
  };
140
 
141
+ // Check if an element is part of KaTeX structure
142
+ const isKaTeXElement = (el) => {
143
+ if (!isElement(el)) return false;
144
+ const className = ensureArray(getAttr(el, 'className') || []).map(String);
145
+ // Check for KaTeX classes
146
+ if (className.some(c => c.includes('katex') || c.includes('math'))) return true;
147
+ // Check parent chain for KaTeX
148
+ let current = el;
149
+ for (let depth = 0; depth < 10; depth++) {
150
+ // We need to walk up, but we don't have parent references in rehype AST
151
+ // So check by tagName and common KaTeX patterns
152
+ const tag = String(current.tagName || '').toLowerCase();
153
+ if (tag === 'math' || className.some(c => c.includes('katex'))) return true;
154
+ break; // Can't walk up in AST, just check current element
155
+ }
156
+ return false;
157
+ };
158
+
159
  const removeFootnoteBackrefAnchors = (el) => {
160
  if (!isElement(el)) return;
161
+ // Never modify KaTeX elements or their contents
162
+ if (isKaTeXElement(el)) return;
163
+
164
  const kids = getChildren(el);
165
  for (let i = kids.length - 1; i >= 0; i--) {
166
  const child = kids[i];
167
  if (isElement(child)) {
168
+ // Never touch KaTeX elements
169
+ if (isKaTeXElement(child)) continue;
170
+
171
  if (
172
  child.tagName === 'a' && (
173
  getAttr(child, 'data-footnote-backref') != null ||
 
183
  el.children.splice(i, 1);
184
  continue;
185
  }
186
+ // Recurse into element (but not if it's KaTeX)
187
  removeFootnoteBackrefAnchors(child);
188
  // If a wrapper like <sup> or <span> became empty, remove it
189
+ // BUT only if it's not part of KaTeX
190
  const becameKids = getChildren(child);
191
+ if ((child.tagName === 'sup' || child.tagName === 'span') &&
192
+ (!becameKids || becameKids.length === 0) &&
193
+ !isKaTeXElement(child)) {
194
  el.children.splice(i, 1);
195
  }
196
  }
 
408
  }
409
  }
410
 
411
+ // Deep clone a node and all its children (preserve KaTeX structure)
412
+ const deepCloneNode = (node) => {
413
+ if (!node || typeof node !== 'object') return node;
414
+ if (node.type === 'text') {
415
+ return { type: 'text', value: node.value };
416
+ }
417
+ if (node.type === 'element') {
418
+ const cloned = {
419
+ type: 'element',
420
+ tagName: node.tagName,
421
+ properties: node.properties ? JSON.parse(JSON.stringify(node.properties)) : {},
422
+ children: []
423
+ };
424
+ const kids = getChildren(node);
425
+ for (const child of kids) {
426
+ cloned.children.push(deepCloneNode(child));
427
+ }
428
+ return cloned;
429
+ }
430
+ return node;
431
+ };
432
+
433
  // Footnotes cleanup + backrefs harmonized with references
434
  const cleanupFootnotes = () => {
435
  let root = null;
 
447
  const items = getChildren(root).filter((n) => isElement(n) && (n.tagName === 'li' || hasClass(n, 'footnote') || n.tagName === 'p' || n.tagName === 'div'));
448
  if (items.length) {
449
  for (const it of items) {
450
+ // Deep clone to preserve all properties including KaTeX structure
451
+ const clonedChildren = getChildren(it).map(deepCloneNode);
452
+ const li = { type: 'element', tagName: 'li', properties: {}, children: clonedChildren };
453
  // Promote nested id if present (e.g., <p id="fn-1">)
454
  const nestedWithId = getChildren(it).find((n) => isElement(n) && getAttr(n, 'id'));
455
  if (nestedWithId) setAttr(li, 'id', getAttr(nestedWithId, 'id'));
 
466
  if (nestedWithId) setAttr(li, 'id', getAttr(nestedWithId, 'id'));
467
  }
468
  // Remove default footnote backrefs anywhere inside (to avoid duplication)
469
+ // But preserve KaTeX elements
470
  removeFootnoteBackrefAnchors(li);
471
  }
472
  setAttr(root, 'data-built-footnotes', '1');
app/src/content/embeds/banner-gold-distillation.html ADDED
@@ -0,0 +1,575 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div class="distillation-visualization"
2
+ style="width:100%;margin:10px 0;aspect-ratio:3/1;min-height:260px;position:relative;overflow:hidden;background:var(--surface-bg);border-radius:12px;border:1px solid var(--border-color);box-shadow:0 2px 8px rgba(0, 0, 0, 0.08);"></div>
3
+ <script>
4
+ (() => {
5
+ const ensureAnime = (cb) => {
6
+ if (window.anime && typeof window.anime === 'function') return cb();
7
+ let s = document.getElementById('anime-cdn-script');
8
+ if (!s) {
9
+ s = document.createElement('script');
10
+ s.id = 'anime-cdn-script';
11
+ s.src = 'https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js';
12
+ document.head.appendChild(s);
13
+ }
14
+ const onReady = () => { if (window.anime && typeof window.anime === 'function') cb(); };
15
+ s.addEventListener('load', onReady, { once: true });
16
+ if (window.anime) onReady();
17
+ };
18
+
19
+ const bootstrap = () => {
20
+ const mount = document.currentScript ? document.currentScript.previousElementSibling : null;
21
+ const container = (mount && mount.querySelector && mount.querySelector('.distillation-visualization')) || document.querySelector('.distillation-visualization');
22
+ if (!container) return;
23
+ if (container.dataset) {
24
+ if (container.dataset.mounted === 'true') return;
25
+ container.dataset.mounted = 'true';
26
+ }
27
+
28
+ // Create canvas
29
+ const canvas = document.createElement('canvas');
30
+ canvas.style.display = 'block';
31
+ canvas.style.width = '100%';
32
+ canvas.style.height = '100%';
33
+ container.appendChild(canvas);
34
+ const ctx = canvas.getContext('2d');
35
+
36
+ // Theme colors
37
+ const getColors = () => {
38
+ const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
39
+ return {
40
+ teacher: isDark ? 'rgba(138, 100, 220, 0.9)' : 'rgba(138, 100, 220, 0.85)',
41
+ student: isDark ? 'rgba(78, 165, 183, 0.9)' : 'rgba(50, 130, 160, 0.85)',
42
+ teacherNode: isDark ? 'rgba(206, 192, 250, 0.8)' : 'rgba(138, 100, 220, 0.7)',
43
+ studentNode: isDark ? 'rgba(125, 211, 252, 0.8)' : 'rgba(78, 165, 183, 0.7)',
44
+ connectionTeacher: isDark ? 'rgba(138, 100, 220, 0.15)' : 'rgba(138, 100, 220, 0.2)',
45
+ connectionStudent: isDark ? 'rgba(78, 165, 183, 0.15)' : 'rgba(78, 165, 183, 0.2)',
46
+ knowledgeFlow: isDark ? 'rgba(232, 137, 171, 0.9)' : 'rgba(220, 80, 130, 0.85)',
47
+ };
48
+ };
49
+
50
+ let colors = getColors();
51
+
52
+ // Watch for theme changes
53
+ const observer = new MutationObserver(() => {
54
+ colors = getColors();
55
+ });
56
+ observer.observe(document.documentElement, {
57
+ attributes: true,
58
+ attributeFilter: ['data-theme']
59
+ });
60
+
61
+ // Teacher and Student network structures
62
+ const teacherLayers = [
63
+ { nodes: 8, name: 'input' },
64
+ { nodes: 14, name: 'hidden1' },
65
+ { nodes: 12, name: 'hidden2' },
66
+ { nodes: 8, name: 'hidden3' },
67
+ { nodes: 6, name: 'output' }
68
+ ];
69
+
70
+ const studentLayers = [
71
+ { nodes: 6, name: 'input' },
72
+ { nodes: 8, name: 'hidden1' },
73
+ { nodes: 6, name: 'hidden2' },
74
+ { nodes: 4, name: 'output' }
75
+ ];
76
+
77
+ let teacherNodes = [];
78
+ let teacherConnections = [];
79
+ let studentNodes = [];
80
+ let studentConnections = [];
81
+ let knowledgeParticles = [];
82
+ let width, height;
83
+ let teacherCenterX = 0;
84
+ let studentCenterX = 0;
85
+ let margin = 0;
86
+
87
+ const resize = () => {
88
+ width = container.clientWidth || 800;
89
+ height = Math.max(260, Math.round(width / 3));
90
+ canvas.width = width;
91
+ canvas.height = height;
92
+ initNetworks();
93
+ };
94
+
95
+ const initNetworks = () => {
96
+ teacherNodes = [];
97
+ teacherConnections = [];
98
+ studentNodes = [];
99
+ studentConnections = [];
100
+ knowledgeParticles = [];
101
+
102
+ // Center networks with spacing
103
+ const teacherX = width * 0.25; // Left side, centered
104
+ const studentX = width * 0.75; // Right side, centered
105
+ teacherCenterX = teacherX;
106
+ studentCenterX = studentX;
107
+ margin = height * 0.2; // More margin to fit labels above
108
+
109
+ // Initialize Teacher Network
110
+ // Use same available height for both networks
111
+ const networkHeight = height - 2 * margin;
112
+ const teacherLayerSpacing = networkHeight / (teacherLayers.length + 1);
113
+ let teacherNodeIndex = 0;
114
+ const teacherLayerStartIndices = [];
115
+ const teacherNetworkWidth = width * 0.15;
116
+
117
+ teacherLayers.forEach((layer, layerIdx) => {
118
+ teacherLayerStartIndices.push(teacherNodeIndex);
119
+ const y = margin + teacherLayerSpacing * (layerIdx + 1);
120
+ const nodeSpacing = teacherNetworkWidth / (layer.nodes + 1);
121
+
122
+ for (let i = 0; i < layer.nodes; i++) {
123
+ const x = teacherX - teacherNetworkWidth / 2 + nodeSpacing * (i + 1);
124
+ const node = {
125
+ x,
126
+ y,
127
+ layer: layerIdx,
128
+ index: i,
129
+ radius: 0,
130
+ targetRadius: 3 + Math.random() * 1,
131
+ pulse: Math.random() * Math.PI * 2,
132
+ activation: 0,
133
+ baseActivity: Math.random() * 0.1
134
+ };
135
+ teacherNodes.push(node);
136
+ teacherNodeIndex++;
137
+ }
138
+ });
139
+
140
+ // Teacher connections
141
+ teacherLayers.forEach((layer, layerIdx) => {
142
+ if (layerIdx < teacherLayers.length - 1) {
143
+ const currentLayerStart = teacherLayerStartIndices[layerIdx];
144
+ const nextLayerStart = teacherLayerStartIndices[layerIdx + 1];
145
+ const nextLayerNodes = teacherLayers[layerIdx + 1].nodes;
146
+
147
+ for (let i = 0; i < layer.nodes; i++) {
148
+ for (let j = 0; j < nextLayerNodes; j++) {
149
+ teacherConnections.push({
150
+ from: currentLayerStart + i,
151
+ to: nextLayerStart + j,
152
+ weight: Math.random(),
153
+ opacity: 0,
154
+ activation: 0
155
+ });
156
+ }
157
+ }
158
+ }
159
+ });
160
+
161
+ // Initialize Student Network
162
+ // Use same available height as teacher network
163
+ const studentLayerSpacing = networkHeight / (studentLayers.length + 1);
164
+ let studentNodeIndex = 0;
165
+ const studentLayerStartIndices = [];
166
+ const studentNetworkWidth = width * 0.15;
167
+
168
+ studentLayers.forEach((layer, layerIdx) => {
169
+ studentLayerStartIndices.push(studentNodeIndex);
170
+ const y = margin + studentLayerSpacing * (layerIdx + 1);
171
+ const nodeSpacing = studentNetworkWidth / (layer.nodes + 1);
172
+
173
+ for (let i = 0; i < layer.nodes; i++) {
174
+ const x = studentX - studentNetworkWidth / 2 + nodeSpacing * (i + 1);
175
+ const node = {
176
+ x,
177
+ y,
178
+ layer: layerIdx,
179
+ index: i,
180
+ radius: 0,
181
+ targetRadius: 2.5 + Math.random() * 0.8,
182
+ pulse: Math.random() * Math.PI * 2,
183
+ activation: 0,
184
+ baseActivity: Math.random() * 0.1
185
+ };
186
+ studentNodes.push(node);
187
+ studentNodeIndex++;
188
+ }
189
+ });
190
+
191
+ // Student connections
192
+ studentLayers.forEach((layer, layerIdx) => {
193
+ if (layerIdx < studentLayers.length - 1) {
194
+ const currentLayerStart = studentLayerStartIndices[layerIdx];
195
+ const nextLayerStart = studentLayerStartIndices[layerIdx + 1];
196
+ const nextLayerNodes = studentLayers[layerIdx + 1].nodes;
197
+
198
+ for (let i = 0; i < layer.nodes; i++) {
199
+ for (let j = 0; j < nextLayerNodes; j++) {
200
+ studentConnections.push({
201
+ from: currentLayerStart + i,
202
+ to: nextLayerStart + j,
203
+ weight: Math.random(),
204
+ opacity: 0,
205
+ activation: 0
206
+ });
207
+ }
208
+ }
209
+ }
210
+ });
211
+
212
+ // Animate nodes appearing
213
+ teacherNodes.forEach((node, i) => {
214
+ anime({
215
+ targets: node,
216
+ radius: node.targetRadius,
217
+ duration: 600,
218
+ delay: i * 5,
219
+ easing: 'easeOutElastic(1, .6)'
220
+ });
221
+ });
222
+
223
+ studentNodes.forEach((node, i) => {
224
+ anime({
225
+ targets: node,
226
+ radius: node.targetRadius,
227
+ duration: 600,
228
+ delay: 300 + i * 5,
229
+ easing: 'easeOutElastic(1, .6)'
230
+ });
231
+ });
232
+
233
+ // Connections fade in
234
+ teacherConnections.forEach((conn, i) => {
235
+ anime({
236
+ targets: conn,
237
+ opacity: 1,
238
+ duration: 300,
239
+ delay: 500 + i * 0.5,
240
+ easing: 'easeOutQuad'
241
+ });
242
+ });
243
+
244
+ studentConnections.forEach((conn, i) => {
245
+ anime({
246
+ targets: conn,
247
+ opacity: 1,
248
+ duration: 300,
249
+ delay: 800 + i * 0.5,
250
+ easing: 'easeOutQuad'
251
+ });
252
+ });
253
+
254
+ // Start animation cycles
255
+ setTimeout(() => {
256
+ startDistillation();
257
+ setInterval(startDistillation, 3000);
258
+ }, 1500);
259
+ };
260
+
261
+ const startDistillation = () => {
262
+ // Activate teacher output nodes
263
+ const teacherOutputNodes = teacherNodes.filter(n => n.layer === teacherLayers.length - 1);
264
+ const teacherOutputs = teacherOutputNodes.slice(0, 3 + Math.floor(Math.random() * 2));
265
+
266
+ teacherOutputs.forEach((node, idx) => {
267
+ anime({
268
+ targets: node,
269
+ activation: 0.7 + Math.random() * 0.2,
270
+ duration: 300,
271
+ delay: idx * 100,
272
+ easing: 'easeOutQuad',
273
+ complete: () => {
274
+ // Create knowledge particles flowing from teacher to student
275
+ createKnowledgeFlow(node, studentNodes);
276
+ anime({
277
+ targets: node,
278
+ activation: node.baseActivity,
279
+ duration: 400,
280
+ easing: 'easeInQuad'
281
+ });
282
+ }
283
+ });
284
+ });
285
+ };
286
+
287
+ const createKnowledgeFlow = (fromNode, studentNodes) => {
288
+ // Select random student input nodes to receive knowledge
289
+ const studentInputNodes = studentNodes.filter(n => n.layer === 0);
290
+ const targets = studentInputNodes.slice(0, 2 + Math.floor(Math.random() * 2));
291
+
292
+ targets.forEach((targetNode, idx) => {
293
+ const particle = {
294
+ fromX: fromNode.x,
295
+ fromY: fromNode.y,
296
+ toX: targetNode.x,
297
+ toY: targetNode.y,
298
+ progress: 0,
299
+ size: 2 + Math.random() * 1.5,
300
+ trail: [],
301
+ startDelay: idx * 150
302
+ };
303
+
304
+ knowledgeParticles.push(particle);
305
+
306
+ setTimeout(() => {
307
+ anime({
308
+ targets: particle,
309
+ progress: 1,
310
+ duration: 800,
311
+ easing: 'easeInOutCubic',
312
+ complete: () => {
313
+ // Activate student node when knowledge arrives
314
+ anime({
315
+ targets: targetNode,
316
+ activation: 0.6 + Math.random() * 0.2,
317
+ duration: 200,
318
+ easing: 'easeOutQuad',
319
+ complete: () => {
320
+ anime({
321
+ targets: targetNode,
322
+ activation: targetNode.baseActivity,
323
+ duration: 500,
324
+ easing: 'easeInQuad'
325
+ });
326
+ }
327
+ });
328
+
329
+ // Propagate through student network
330
+ setTimeout(() => {
331
+ propagateStudentNetwork();
332
+ }, 200);
333
+
334
+ // Remove particle
335
+ const idx = knowledgeParticles.indexOf(particle);
336
+ if (idx > -1) knowledgeParticles.splice(idx, 1);
337
+ }
338
+ });
339
+ }, particle.startDelay);
340
+ });
341
+ };
342
+
343
+ const propagateStudentNetwork = () => {
344
+ for (let layerIdx = 0; layerIdx < studentLayers.length - 1; layerIdx++) {
345
+ setTimeout(() => {
346
+ const fromNodes = studentNodes.filter(n => n.layer === layerIdx && n.activation > 0.3);
347
+ const toNodes = studentNodes.filter(n => n.layer === layerIdx + 1);
348
+
349
+ const layerConnections = studentConnections.filter(c => {
350
+ const fromNode = studentNodes[c.from];
351
+ const toNode = studentNodes[c.to];
352
+ return fromNode.layer === layerIdx && toNode.layer === layerIdx + 1;
353
+ });
354
+
355
+ layerConnections.forEach((conn) => {
356
+ const fromNode = studentNodes[conn.from];
357
+ if (fromNode.activation > 0.2) {
358
+ anime({
359
+ targets: conn,
360
+ activation: fromNode.activation * conn.weight,
361
+ duration: 250,
362
+ easing: 'easeOutQuad',
363
+ complete: () => {
364
+ anime({
365
+ targets: conn,
366
+ activation: 0,
367
+ duration: 300,
368
+ easing: 'easeInQuad'
369
+ });
370
+ }
371
+ });
372
+ }
373
+ });
374
+
375
+ toNodes.forEach(toNode => {
376
+ const toNodeIdx = studentNodes.indexOf(toNode);
377
+ const incomingConns = layerConnections.filter(c => c.to === toNodeIdx);
378
+ let sum = 0;
379
+ incomingConns.forEach(conn => {
380
+ const fromNode = studentNodes[conn.from];
381
+ sum += fromNode.activation * conn.weight;
382
+ });
383
+ const activation = Math.min(1, sum / incomingConns.length * 1.3);
384
+ if (activation > 0.25) {
385
+ anime({
386
+ targets: toNode,
387
+ activation: activation,
388
+ duration: 200,
389
+ easing: 'easeOutQuad',
390
+ complete: () => {
391
+ anime({
392
+ targets: toNode,
393
+ activation: toNode.baseActivity,
394
+ duration: 400,
395
+ easing: 'easeInQuad'
396
+ });
397
+ }
398
+ });
399
+ }
400
+ });
401
+ }, layerIdx * 200);
402
+ }
403
+ };
404
+
405
+ const draw = () => {
406
+ ctx.clearRect(0, 0, width, height);
407
+
408
+ // Draw teacher network connections
409
+ teacherConnections.forEach(conn => {
410
+ if (conn.opacity < 0.01) return;
411
+ const fromNode = teacherNodes[conn.from];
412
+ const toNode = teacherNodes[conn.to];
413
+ if (!fromNode || !toNode) return;
414
+
415
+ const baseOpacity = conn.opacity * conn.weight * 0.4;
416
+ const activeOpacity = conn.activation;
417
+ const totalOpacity = Math.max(baseOpacity, activeOpacity);
418
+
419
+ if (totalOpacity < 0.01) return;
420
+
421
+ ctx.beginPath();
422
+ ctx.moveTo(fromNode.x, fromNode.y);
423
+ ctx.lineTo(toNode.x, toNode.y);
424
+ const rgb = colors.connectionTeacher.match(/[\d.]+/g);
425
+ ctx.strokeStyle = `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${totalOpacity})`;
426
+ ctx.lineWidth = conn.activation > 0.1 ? 1.2 : 0.7;
427
+ ctx.stroke();
428
+ });
429
+
430
+ // Draw student network connections
431
+ studentConnections.forEach(conn => {
432
+ if (conn.opacity < 0.01) return;
433
+ const fromNode = studentNodes[conn.from];
434
+ const toNode = studentNodes[conn.to];
435
+ if (!fromNode || !toNode) return;
436
+
437
+ const baseOpacity = conn.opacity * conn.weight * 0.4;
438
+ const activeOpacity = conn.activation;
439
+ const totalOpacity = Math.max(baseOpacity, activeOpacity);
440
+
441
+ if (totalOpacity < 0.01) return;
442
+
443
+ ctx.beginPath();
444
+ ctx.moveTo(fromNode.x, fromNode.y);
445
+ ctx.lineTo(toNode.x, toNode.y);
446
+ const rgb = colors.connectionStudent.match(/[\d.]+/g);
447
+ ctx.strokeStyle = `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${totalOpacity})`;
448
+ ctx.lineWidth = conn.activation > 0.1 ? 1.2 : 0.7;
449
+ ctx.stroke();
450
+ });
451
+
452
+ // Draw knowledge flow particles
453
+ knowledgeParticles.forEach(particle => {
454
+ const x = particle.fromX + (particle.toX - particle.fromX) * particle.progress;
455
+ const y = particle.fromY + (particle.toY - particle.fromY) * particle.progress;
456
+
457
+ particle.trail.push({ x, y });
458
+ if (particle.trail.length > 8) particle.trail.shift();
459
+
460
+ particle.trail.forEach((point, i) => {
461
+ const alpha = (i / particle.trail.length) * 0.7;
462
+ const size = particle.size * alpha * 0.8;
463
+
464
+ ctx.beginPath();
465
+ ctx.arc(point.x, point.y, size, 0, Math.PI * 2);
466
+ const rgb = colors.knowledgeFlow.match(/[\d.]+/g);
467
+ ctx.fillStyle = `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${alpha * 0.6})`;
468
+ ctx.fill();
469
+ });
470
+
471
+ // Main particle
472
+ ctx.beginPath();
473
+ ctx.arc(x, y, particle.size, 0, Math.PI * 2);
474
+ ctx.fillStyle = colors.knowledgeFlow;
475
+ ctx.shadowBlur = 10;
476
+ ctx.shadowColor = colors.knowledgeFlow;
477
+ ctx.fill();
478
+ ctx.shadowBlur = 0;
479
+ });
480
+
481
+ // Draw teacher nodes
482
+ teacherNodes.forEach(node => {
483
+ if (node.radius < 0.1) return;
484
+
485
+ node.pulse += 0.015;
486
+ const pulseSize = 1 + Math.sin(node.pulse) * 0.08;
487
+ const activationBoost = node.activation * 1.5;
488
+ const finalRadius = node.radius * pulseSize + activationBoost;
489
+
490
+ if (node.activation > 0.15) {
491
+ const glowRadius = finalRadius * 3;
492
+ const gradient = ctx.createRadialGradient(node.x, node.y, 0, node.x, node.y, glowRadius);
493
+ const glowAlpha = node.activation * 0.4;
494
+ const rgb = colors.teacher.match(/[\d.]+/g);
495
+ gradient.addColorStop(0, `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${glowAlpha})`);
496
+ gradient.addColorStop(1, `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, 0)`);
497
+ ctx.beginPath();
498
+ ctx.arc(node.x, node.y, glowRadius, 0, Math.PI * 2);
499
+ ctx.fillStyle = gradient;
500
+ ctx.fill();
501
+ }
502
+
503
+ ctx.beginPath();
504
+ ctx.arc(node.x, node.y, finalRadius, 0, Math.PI * 2);
505
+ const rgb = colors.teacherNode.match(/[\d.]+/g);
506
+ const alpha = Math.max(0.5, parseFloat(rgb[3]) + node.activation * 0.3);
507
+ ctx.fillStyle = `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${alpha})`;
508
+ ctx.fill();
509
+ });
510
+
511
+ // Draw student nodes
512
+ studentNodes.forEach(node => {
513
+ if (node.radius < 0.1) return;
514
+
515
+ node.pulse += 0.015;
516
+ const pulseSize = 1 + Math.sin(node.pulse) * 0.08;
517
+ const activationBoost = node.activation * 1.5;
518
+ const finalRadius = node.radius * pulseSize + activationBoost;
519
+
520
+ if (node.activation > 0.15) {
521
+ const glowRadius = finalRadius * 3;
522
+ const gradient = ctx.createRadialGradient(node.x, node.y, 0, node.x, node.y, glowRadius);
523
+ const glowAlpha = node.activation * 0.4;
524
+ const rgb = colors.student.match(/[\d.]+/g);
525
+ gradient.addColorStop(0, `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${glowAlpha})`);
526
+ gradient.addColorStop(1, `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, 0)`);
527
+ ctx.beginPath();
528
+ ctx.arc(node.x, node.y, glowRadius, 0, Math.PI * 2);
529
+ ctx.fillStyle = gradient;
530
+ ctx.fill();
531
+ }
532
+
533
+ ctx.beginPath();
534
+ ctx.arc(node.x, node.y, finalRadius, 0, Math.PI * 2);
535
+ const rgb = colors.studentNode.match(/[\d.]+/g);
536
+ const alpha = Math.max(0.5, parseFloat(rgb[3]) + node.activation * 0.3);
537
+ ctx.fillStyle = `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, ${alpha})`;
538
+ ctx.fill();
539
+ });
540
+
541
+ // Draw labels below networks
542
+ ctx.font = '11px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif';
543
+ ctx.textAlign = 'center';
544
+ ctx.textBaseline = 'middle';
545
+ const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
546
+ const labelColor = isDark
547
+ ? 'rgba(255, 255, 255, 0.4)'
548
+ : 'rgba(0, 0, 0, 0.4)';
549
+ ctx.fillStyle = labelColor;
550
+ // Center labels below their respective networks, closer to the networks
551
+ ctx.fillText('Teacher', teacherCenterX, height - margin + 12);
552
+ ctx.fillText('Student', studentCenterX, height - margin + 12);
553
+
554
+ requestAnimationFrame(draw);
555
+ };
556
+
557
+ // Start
558
+ if (window.ResizeObserver) {
559
+ const ro = new ResizeObserver(resize);
560
+ ro.observe(container);
561
+ } else {
562
+ window.addEventListener('resize', resize);
563
+ }
564
+
565
+ resize();
566
+ draw();
567
+ };
568
+
569
+ if (document.readyState === 'loading') {
570
+ document.addEventListener('DOMContentLoaded', () => ensureAnime(bootstrap), { once: true });
571
+ } else {
572
+ ensureAnime(bootstrap);
573
+ }
574
+ })();
575
+ </script>
app/src/content/embeds/banner-sequence-alignment-svg.html ADDED
@@ -0,0 +1,374 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div class="sequence-alignment-visualization"
2
+ style="width:100%;margin:10px 0;aspect-ratio:3/1;min-height:260px;position:relative;overflow:hidden;background:var(--surface-bg);border-radius:12px;border:1px solid var(--border-color);box-shadow:0 2px 8px rgba(0, 0, 0, 0.08);display:flex;">
3
+ <div class="section-container" style="flex:1;position:relative;border-right:1px dashed var(--border-color);padding:20px;">
4
+ <div style="position:absolute;top:10px;left:20px;font-weight:600;font-size:12px;color:var(--text-color);">
5
+ 1. Determine the token merges
6
+ </div>
7
+ <div id="svg-section1" style="width:100%;height:calc(100% - 30px);margin-top:30px;"></div>
8
+ </div>
9
+ <div class="section-container" style="flex:1;position:relative;padding:20px;">
10
+ <div style="position:absolute;top:10px;left:20px;font-weight:600;font-size:12px;color:var(--text-color);">
11
+ 2. Add logprob tensors in the merged positions
12
+ </div>
13
+ <div id="svg-section2" style="width:100%;height:calc(100% - 30px);margin-top:30px;"></div>
14
+ </div>
15
+ </div>
16
+
17
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/svg.js/3.2.5/svg.min.js"></script>
18
+ <script>
19
+ (function () {
20
+ const getColors = () => {
21
+ const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
22
+ return {
23
+ originalToken: isDark ? 'rgba(134, 239, 172, 0.4)' : 'rgba(187, 247, 208, 0.7)',
24
+ subToken: isDark ? 'rgba(251, 191, 36, 0.7)' : 'rgba(253, 224, 71, 0.8)',
25
+ mergedToken: isDark ? 'rgba(147, 197, 253, 0.5)' : 'rgba(191, 219, 254, 0.7)',
26
+ text: isDark ? 'rgba(255, 255, 255, 0.95)' : 'rgba(0, 0, 0, 0.9)',
27
+ line: isDark ? 'rgba(255, 255, 255, 0.3)' : 'rgba(0, 0, 0, 0.35)',
28
+ plus: isDark ? 'rgba(255, 255, 255, 0.7)' : 'rgba(0, 0, 0, 0.7)',
29
+ };
30
+ };
31
+
32
+ function drawSection1() {
33
+ const container = document.getElementById('svg-section1');
34
+ if (!container) return;
35
+
36
+ container.innerHTML = '';
37
+ const colors = getColors();
38
+
39
+ const draw = SVG().addTo(container).size('100%', '100%');
40
+ const width = container.clientWidth || 400;
41
+ const height = container.clientHeight || 220;
42
+
43
+ // Calculate scale based on available width
44
+ const baseTotalWidth = 180 + 110 + 40 + 90 + 180 + (15 * 4);
45
+ const scale = Math.min(1, (width - 40) / baseTotalWidth);
46
+
47
+ const padding = 15 * scale;
48
+ const tokenHeight = 35 * scale;
49
+ const spacing = 15 * scale;
50
+ const subTokenSmallSize = 24 * scale;
51
+ const subTokenLargeWidth = 70 * scale;
52
+ const subTokenLargeHeight = 24 * scale;
53
+
54
+ const originalWords = [
55
+ { text: '<think>', subTokens: [
56
+ { id: 0, type: 'small' },
57
+ { id: 1, type: 'small' },
58
+ { id: 2, type: 'small' }
59
+ ], width: 180 * scale },
60
+ { text: 'Hugging Face', subTokens: [
61
+ { id: 3, type: 'large' }
62
+ ], width: 110 * scale },
63
+ { text: 'is', subTokens: [
64
+ { id: 4, type: 'small' }
65
+ ], width: 40 * scale },
66
+ { text: 'awesome!', subTokens: [
67
+ { id: 5, type: 'large' }
68
+ ], width: 90 * scale },
69
+ { text: '</think>', subTokens: [
70
+ { id: 6, type: 'small' },
71
+ { id: 7, type: 'small' },
72
+ { id: 8, type: 'small' },
73
+ { id: 9, type: 'small' }
74
+ ], width: 180 * scale }
75
+ ];
76
+
77
+ // Store word centers for alignment
78
+ const wordCenters = [];
79
+ let currentX = padding;
80
+ const originalY = height * 0.18;
81
+ const subTokenY = height * 0.45;
82
+ const mergedY = height * 0.72;
83
+
84
+ // Draw original words and sub-tokens
85
+ originalWords.forEach((word, wordIdx) => {
86
+ const wordCenterX = currentX + word.width / 2;
87
+ wordCenters.push(wordCenterX);
88
+
89
+ // Original word (green)
90
+ draw.rect(word.width, tokenHeight)
91
+ .move(currentX, originalY - tokenHeight / 2)
92
+ .radius(10 * scale)
93
+ .fill(colors.originalToken)
94
+ .stroke({ color: colors.line, width: 1.5 * scale });
95
+
96
+ draw.text(word.text)
97
+ .move(currentX + word.width / 2, originalY)
98
+ .font({
99
+ family: '-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',
100
+ size: 11 * scale,
101
+ anchor: 'middle',
102
+ weight: '500'
103
+ })
104
+ .fill(colors.text);
105
+
106
+ // Sub-tokens (yellow) below - small squares or large rectangles
107
+ const subTokenGap = 4 * scale;
108
+
109
+ // Calculate total width for centering
110
+ let subTokensTotalWidth = 0;
111
+ word.subTokens.forEach((st, idx) => {
112
+ subTokensTotalWidth += st.type === 'small' ? subTokenSmallSize : subTokenLargeWidth;
113
+ if (idx < word.subTokens.length - 1) subTokensTotalWidth += subTokenGap;
114
+ });
115
+
116
+ let subTokenX = currentX + (word.width - subTokensTotalWidth) / 2;
117
+
118
+ word.subTokens.forEach((st) => {
119
+ const stWidth = st.type === 'small' ? subTokenSmallSize : subTokenLargeWidth;
120
+ const stHeight = st.type === 'small' ? subTokenSmallSize : subTokenLargeHeight;
121
+
122
+ draw.rect(stWidth, stHeight)
123
+ .move(subTokenX, subTokenY - stHeight / 2)
124
+ .radius(6 * scale)
125
+ .fill(colors.subToken)
126
+ .stroke({ color: colors.line, width: 1.5 * scale });
127
+
128
+ draw.text(st.id.toString())
129
+ .move(subTokenX + stWidth / 2, subTokenY)
130
+ .font({
131
+ family: '-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',
132
+ size: 10 * scale,
133
+ anchor: 'middle',
134
+ weight: '600'
135
+ })
136
+ .fill(colors.text);
137
+
138
+ subTokenX += stWidth + subTokenGap;
139
+ });
140
+
141
+ // Vertical dotted line from word center
142
+ draw.line(wordCenterX, originalY + tokenHeight / 2 + (3 * scale), wordCenterX, mergedY - tokenHeight / 2 - (3 * scale))
143
+ .stroke({ color: colors.line, width: 1.5 * scale, dasharray: '4,4' });
144
+
145
+ currentX += word.width + spacing;
146
+ });
147
+
148
+ // Merged tokens (blue rectangles) at bottom
149
+ // Groups of tokens: some have multiple rectangles side-by-side
150
+ const mergedTokenGroups = [
151
+ { ids: [0], alignToWord: 0 }, // Single token "0" under <think>
152
+ { ids: [1, 2], alignToWord: null, x: wordCenters[0] + (wordCenters[1] - wordCenters[0]) * 0.55 }, // Two tokens "1", "2" between words
153
+ { ids: [3], alignToWord: 1 }, // Single token "3" under Hugging Face
154
+ { ids: [4], alignToWord: 2 }, // Single token "4" under is
155
+ { ids: [5], alignToWord: 3 } // Single token "5" under awesome!
156
+ ];
157
+
158
+ const mergedTokenWidth = 55 * scale;
159
+ const mergedTokenGap = 2 * scale;
160
+
161
+ mergedTokenGroups.forEach((group) => {
162
+ const groupTotalWidth = group.ids.length * mergedTokenWidth + (group.ids.length - 1) * mergedTokenGap;
163
+
164
+ // Determine X position: either aligned to word or custom position
165
+ let groupX;
166
+ if (group.alignToWord !== null) {
167
+ groupX = wordCenters[group.alignToWord] - groupTotalWidth / 2;
168
+ } else {
169
+ groupX = group.x - groupTotalWidth / 2;
170
+ }
171
+
172
+ // Draw each token in the group
173
+ group.ids.forEach((id, idIdx) => {
174
+ const tokenX = groupX + idIdx * (mergedTokenWidth + mergedTokenGap);
175
+
176
+ draw.rect(mergedTokenWidth, tokenHeight)
177
+ .move(tokenX, mergedY - tokenHeight / 2)
178
+ .radius(8 * scale)
179
+ .fill(colors.mergedToken)
180
+ .stroke({ color: colors.line, width: 1.5 * scale });
181
+
182
+ draw.text(id.toString())
183
+ .move(tokenX + mergedTokenWidth / 2, mergedY)
184
+ .font({
185
+ family: '-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',
186
+ size: 11 * scale,
187
+ anchor: 'middle',
188
+ weight: '600'
189
+ })
190
+ .fill(colors.text);
191
+ });
192
+ });
193
+ }
194
+
195
+ function drawSection2() {
196
+ const container = document.getElementById('svg-section2');
197
+ if (!container) return;
198
+
199
+ container.innerHTML = '';
200
+ const colors = getColors();
201
+
202
+ const draw = SVG().addTo(container).size('100%', '100%');
203
+ const width = container.clientWidth || 400;
204
+ const height = container.clientHeight || 220;
205
+
206
+ // Calculate scale based on available width
207
+ const baseTotalWidth = 180 + 110 + 35 + 80 + 160 + (15 * 4);
208
+ const scale = Math.min(1, (width - 40) / baseTotalWidth);
209
+
210
+ const padding = 15 * scale;
211
+ const logprobStackHeight = 55 * scale;
212
+ const logprobRectSize = 14 * scale;
213
+ const logprobSpacing = 4 * scale;
214
+ const rectsPerStack = 4; // Input stacks have 4 rectangles
215
+ const stackWidth = (logprobRectSize + logprobSpacing) * rectsPerStack - logprobSpacing;
216
+ const spacing = 15 * scale;
217
+
218
+ const words = [
219
+ { text: '<think>', stacks: 3, width: 180 * scale },
220
+ { text: 'Hugging Face', stacks: 1, width: 110 * scale },
221
+ { text: 'is', stacks: 1, width: 35 * scale },
222
+ { text: 'awesome!', stacks: 1, width: 80 * scale },
223
+ { text: '</think>', stacks: 4, width: 160 * scale }
224
+ ];
225
+
226
+ const wordCenters = [];
227
+ let currentX = padding;
228
+ const inputY = height * 0.18;
229
+ const outputY = height * 0.72;
230
+
231
+ // Draw input logprob tensors (yellow stacks)
232
+ words.forEach((word, wordIdx) => {
233
+ const wordCenterX = currentX + word.width / 2;
234
+ wordCenters.push(wordCenterX);
235
+
236
+ // Draw multiple stacks for words that merge
237
+ const stackGap = 12 * scale;
238
+ const totalStacksWidth = word.stacks * stackWidth + (word.stacks - 1) * stackGap;
239
+ const stacksStartX = wordCenterX - totalStacksWidth / 2;
240
+
241
+ for (let s = 0; s < word.stacks; s++) {
242
+ const stackX = stacksStartX + s * (stackWidth + stackGap);
243
+
244
+ // Draw stack of rectangles (4 rectangles per stack)
245
+ for (let i = 0; i < rectsPerStack; i++) {
246
+ const rectY = inputY - logprobStackHeight / 2 + i * (logprobRectSize + logprobSpacing);
247
+
248
+ draw.rect(logprobRectSize, logprobRectSize)
249
+ .move(stackX, rectY)
250
+ .radius(4 * scale)
251
+ .fill(colors.subToken)
252
+ .stroke({ color: colors.line, width: 1 * scale });
253
+ }
254
+
255
+ // Draw plus signs between stacks (except last)
256
+ if (s < word.stacks - 1) {
257
+ const plusX = stackX + stackWidth + stackGap / 2;
258
+ draw.text('+')
259
+ .move(plusX, inputY)
260
+ .font({
261
+ family: '-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',
262
+ size: 18 * scale,
263
+ anchor: 'middle',
264
+ weight: 'bold'
265
+ })
266
+ .fill(colors.plus);
267
+ }
268
+ }
269
+
270
+ // Word label below input stacks
271
+ const labelHeight = 20 * scale;
272
+ const labelY = inputY + logprobStackHeight / 2 + (18 * scale);
273
+
274
+ draw.rect(word.width, labelHeight)
275
+ .move(currentX, labelY - labelHeight / 2)
276
+ .radius(5 * scale)
277
+ .fill(colors.originalToken)
278
+ .stroke({ color: colors.line, width: 1 * scale });
279
+
280
+ draw.text(word.text)
281
+ .move(currentX + word.width / 2, labelY)
282
+ .font({
283
+ family: '-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',
284
+ size: 10 * scale,
285
+ anchor: 'middle',
286
+ weight: '500'
287
+ })
288
+ .fill(colors.text);
289
+
290
+ // Vertical dotted line from label to output
291
+ draw.line(wordCenterX, labelY + labelHeight / 2 + (5 * scale), wordCenterX, outputY - logprobStackHeight / 2 - (5 * scale))
292
+ .stroke({ color: colors.line, width: 1.5 * scale, dasharray: '4,4' });
293
+
294
+ currentX += word.width + spacing;
295
+ });
296
+
297
+ // Draw output logprob tensors (blue stacks) - 5 rectangles per stack
298
+ words.forEach((word, wordIdx) => {
299
+ const wordCenterX = wordCenters[wordIdx];
300
+
301
+ // Single output stack (merged) - 5 rectangles!
302
+ const outputRectsPerStack = 5;
303
+ const outputStackWidth = (logprobRectSize + logprobSpacing) * outputRectsPerStack - logprobSpacing;
304
+ const adjustedStackX = wordCenterX - outputStackWidth / 2;
305
+
306
+ for (let i = 0; i < outputRectsPerStack; i++) {
307
+ const rectY = outputY - logprobStackHeight / 2 + i * (logprobRectSize + logprobSpacing);
308
+ const rectX = adjustedStackX + i * (logprobRectSize + logprobSpacing);
309
+
310
+ draw.rect(logprobRectSize, logprobRectSize)
311
+ .move(rectX, rectY)
312
+ .radius(4 * scale)
313
+ .fill(colors.mergedToken)
314
+ .stroke({ color: colors.line, width: 1 * scale });
315
+ }
316
+
317
+ // Word label below output stacks
318
+ const labelHeight = 20 * scale;
319
+ const labelY = outputY + logprobStackHeight / 2 + (18 * scale);
320
+
321
+ draw.rect(word.width, labelHeight)
322
+ .move(wordCenters[wordIdx] - word.width / 2, labelY - labelHeight / 2)
323
+ .radius(5 * scale)
324
+ .fill(colors.mergedToken)
325
+ .stroke({ color: colors.line, width: 1 * scale });
326
+
327
+ draw.text(word.text)
328
+ .move(wordCenters[wordIdx], labelY)
329
+ .font({
330
+ family: '-apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',
331
+ size: 10 * scale,
332
+ anchor: 'middle',
333
+ weight: '500'
334
+ })
335
+ .fill(colors.text);
336
+ });
337
+ }
338
+
339
+ function resize() {
340
+ drawSection1();
341
+ drawSection2();
342
+ }
343
+
344
+ // Watch for theme changes
345
+ const observer = new MutationObserver(() => {
346
+ resize();
347
+ });
348
+ observer.observe(document.documentElement, {
349
+ attributes: true,
350
+ attributeFilter: ['data-theme']
351
+ });
352
+
353
+ // Initialize
354
+ const container = document.querySelector('.sequence-alignment-visualization');
355
+ if (container) {
356
+ if (window.ResizeObserver) {
357
+ const ro = new ResizeObserver(resize);
358
+ ro.observe(container);
359
+ } else {
360
+ window.addEventListener('resize', resize);
361
+ }
362
+
363
+ // Wait for SVG.js to load
364
+ const checkSVG = () => {
365
+ if (typeof SVG !== 'undefined') {
366
+ resize();
367
+ } else {
368
+ setTimeout(checkSVG, 50);
369
+ }
370
+ };
371
+ checkSVG();
372
+ }
373
+ })();
374
+ </script>
app/src/content/embeds/banner-sequence-alignment.html ADDED
@@ -0,0 +1,317 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <div class="sequence-alignment-visualization"
2
+ style="width:100%;margin:10px 0;aspect-ratio:3/1;min-height:260px;position:relative;overflow:hidden;background:var(--surface-bg);border-radius:12px;border:1px solid var(--border-color);box-shadow:0 2px 8px rgba(0, 0, 0, 0.08);display:flex;">
3
+ <div class="section-container" style="flex:1;position:relative;border-right:1px dashed var(--border-color);padding:20px;">
4
+ <div style="position:absolute;top:10px;left:20px;font-weight:600;font-size:12px;color:var(--text-color);">
5
+ 1. Determine the token merges
6
+ </div>
7
+ <canvas id="canvas-section1" style="width:100%;height:100%;display:block;"></canvas>
8
+ </div>
9
+ <div class="section-container" style="flex:1;position:relative;padding:20px;">
10
+ <div style="position:absolute;top:10px;left:20px;font-weight:600;font-size:12px;color:var(--text-color);">
11
+ 2. Add logprob tensors in the merged positions
12
+ </div>
13
+ <canvas id="canvas-section2" style="width:100%;height:100%;display:block;"></canvas>
14
+ </div>
15
+ </div>
16
+ <script>
17
+ (() => {
18
+ const getColors = () => {
19
+ const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
20
+ return {
21
+ originalToken: isDark ? 'rgba(134, 239, 172, 0.3)' : 'rgba(187, 247, 208, 0.6)',
22
+ subToken: isDark ? 'rgba(251, 191, 36, 0.6)' : 'rgba(253, 224, 71, 0.7)',
23
+ mergedToken: isDark ? 'rgba(147, 197, 253, 0.4)' : 'rgba(191, 219, 254, 0.6)',
24
+ text: isDark ? 'rgba(255, 255, 255, 0.9)' : 'rgba(0, 0, 0, 0.85)',
25
+ line: isDark ? 'rgba(255, 255, 255, 0.25)' : 'rgba(0, 0, 0, 0.3)',
26
+ plus: isDark ? 'rgba(255, 255, 255, 0.6)' : 'rgba(0, 0, 0, 0.6)',
27
+ };
28
+ };
29
+
30
+ // Helper: rounded rectangle
31
+ if (!CanvasRenderingContext2D.prototype.roundRect) {
32
+ CanvasRenderingContext2D.prototype.roundRect = function(x, y, w, h, r) {
33
+ if (w < 2 * r) r = w / 2;
34
+ if (h < 2 * r) r = h / 2;
35
+ this.moveTo(x + r, y);
36
+ this.lineTo(x + w - r, y);
37
+ this.quadraticCurveTo(x + w, y, x + w, y + r);
38
+ this.lineTo(x + w, y + h - r);
39
+ this.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
40
+ this.lineTo(x + r, y + h);
41
+ this.quadraticCurveTo(x, y + h, x, y + h - r);
42
+ this.lineTo(x, y + r);
43
+ this.quadraticCurveTo(x, y, x + r, y);
44
+ };
45
+ }
46
+
47
+ const drawSection1 = (canvas, colors) => {
48
+ const ctx = canvas.getContext('2d');
49
+ const width = canvas.width;
50
+ const height = canvas.height;
51
+
52
+ ctx.clearRect(0, 0, width, height);
53
+
54
+ // Calculate scale based on available width
55
+ const totalContentWidth = 180 + 120 + 50 + 100 + 200 + (15 * 4); // widths + spacings
56
+ const scale = Math.min(1, (width - 40) / totalContentWidth);
57
+
58
+ const padding = 20 * scale;
59
+ const tokenHeight = 32 * scale;
60
+ const spacing = 15 * scale;
61
+ const subTokenSize = 18 * scale;
62
+
63
+ const originalWords = [
64
+ { text: '<think>', subTokens: [0, 1, 2], width: 180 * scale },
65
+ { text: 'Hugging Face', subTokens: [3], width: 120 * scale },
66
+ { text: 'is', subTokens: [4], width: 50 * scale },
67
+ { text: 'awesome!', subTokens: [5], width: 100 * scale },
68
+ { text: '</think>', subTokens: [6, 7, 8, 9], width: 200 * scale }
69
+ ];
70
+
71
+ let currentX = padding;
72
+ const originalY = height * 0.2;
73
+ const subTokenY = height * 0.45;
74
+ const mergedY = height * 0.7;
75
+
76
+ // Draw original words and sub-tokens
77
+ originalWords.forEach((word) => {
78
+ // Original word (green)
79
+ ctx.fillStyle = colors.originalToken;
80
+ ctx.beginPath();
81
+ ctx.roundRect(currentX, originalY - tokenHeight / 2, word.width, tokenHeight, 8);
82
+ ctx.fill();
83
+
84
+ ctx.fillStyle = colors.text;
85
+ ctx.font = `${10 * scale}px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif`;
86
+ ctx.textAlign = 'center';
87
+ ctx.textBaseline = 'middle';
88
+ ctx.fillText(word.text, currentX + word.width / 2, originalY);
89
+
90
+ // Sub-tokens (yellow) below
91
+ const subTokenSpacing = word.width / (word.subTokens.length + 1);
92
+ word.subTokens.forEach((stId, stIdx) => {
93
+ const stX = currentX + subTokenSpacing * (stIdx + 1) - subTokenSize / 2;
94
+ ctx.fillStyle = colors.subToken;
95
+ ctx.beginPath();
96
+ ctx.roundRect(stX, subTokenY - subTokenSize / 2, subTokenSize, subTokenSize, 5);
97
+ ctx.fill();
98
+
99
+ ctx.fillStyle = colors.text;
100
+ ctx.font = `${9 * scale}px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif`;
101
+ ctx.fillText(stId.toString(), stX + subTokenSize / 2, subTokenY);
102
+ });
103
+
104
+ // Vertical dotted line
105
+ ctx.strokeStyle = colors.line;
106
+ ctx.lineWidth = 1;
107
+ ctx.setLineDash([3, 3]);
108
+ ctx.beginPath();
109
+ ctx.moveTo(currentX + word.width / 2, originalY + tokenHeight / 2);
110
+ ctx.lineTo(currentX + word.width / 2, mergedY - tokenHeight / 2);
111
+ ctx.stroke();
112
+ ctx.setLineDash([]);
113
+
114
+ currentX += word.width + spacing;
115
+ });
116
+
117
+ // Merged tokens (blue rectangles) at bottom - showing IDs (0, 1, 2, 3, 4)
118
+ const mergedTokens = [
119
+ { id: 0, fromSubTokens: [0, 1, 2], width: 60 * scale },
120
+ { id: 1, fromSubTokens: [3], width: 60 * scale },
121
+ { id: 2, fromSubTokens: [4], width: 60 * scale },
122
+ { id: 3, fromSubTokens: [5], width: 60 * scale },
123
+ { id: 4, fromSubTokens: [6, 7, 8, 9], width: 60 * scale }
124
+ ];
125
+
126
+ currentX = padding + (originalWords.reduce((sum, w) => sum + w.width + spacing, -spacing) - mergedTokens.reduce((sum, t) => sum + t.width + spacing, -spacing)) / 2;
127
+ mergedTokens.forEach((token) => {
128
+ ctx.fillStyle = colors.mergedToken;
129
+ ctx.beginPath();
130
+ ctx.roundRect(currentX, mergedY - tokenHeight / 2, token.width, tokenHeight, 8);
131
+ ctx.fill();
132
+
133
+ ctx.fillStyle = colors.text;
134
+ ctx.font = `${10 * scale}px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif`;
135
+ ctx.textAlign = 'center';
136
+ ctx.textBaseline = 'middle';
137
+ ctx.fillText(token.id.toString(), currentX + token.width / 2, mergedY);
138
+
139
+ currentX += token.width + spacing;
140
+ });
141
+ };
142
+
143
+ const drawSection2 = (canvas, colors) => {
144
+ const ctx = canvas.getContext('2d');
145
+ const width = canvas.width;
146
+ const height = canvas.height;
147
+
148
+ ctx.clearRect(0, 0, width, height);
149
+
150
+ // Calculate scale based on available width
151
+ const totalContentWidth = 180 + 120 + 50 + 100 + 200 + (20 * 4); // widths + spacings
152
+ const scale = Math.min(1, (width - 40) / totalContentWidth);
153
+
154
+ const padding = 20 * scale;
155
+ const logprobStackHeight = 60 * scale;
156
+ const logprobRectSize = 12 * scale;
157
+ const logprobSpacing = 3 * scale;
158
+ const rectsPerStack = 4; // Input stacks have 4 rectangles
159
+ const stackWidth = (logprobRectSize + logprobSpacing) * rectsPerStack - logprobSpacing;
160
+ const spacing = 20 * scale;
161
+
162
+ const words = [
163
+ { text: '<think>', stacks: 3, width: 180 * scale },
164
+ { text: 'Hugging Face', stacks: 1, width: 120 * scale },
165
+ { text: 'is', stacks: 1, width: 50 * scale },
166
+ { text: 'awesome!', stacks: 1, width: 100 * scale },
167
+ { text: '</think>', stacks: 4, width: 200 * scale }
168
+ ];
169
+
170
+ let currentX = padding;
171
+ const inputY = height * 0.2;
172
+ const outputY = height * 0.7;
173
+
174
+ // Draw input logprob tensors (yellow stacks)
175
+ words.forEach((word) => {
176
+ const wordCenterX = currentX + word.width / 2;
177
+
178
+ // Draw multiple stacks for words that merge
179
+ for (let s = 0; s < word.stacks; s++) {
180
+ const stackX = wordCenterX - (word.stacks - 1) * (stackWidth + 8) / 2 + s * (stackWidth + 8);
181
+
182
+ // Draw stack of rectangles
183
+ for (let i = 0; i < rectsPerStack; i++) {
184
+ const rectY = inputY - logprobStackHeight / 2 + i * (logprobRectSize + logprobSpacing);
185
+ ctx.fillStyle = colors.subToken;
186
+ ctx.beginPath();
187
+ ctx.roundRect(stackX, rectY, logprobRectSize, logprobRectSize, 3);
188
+ ctx.fill();
189
+ }
190
+
191
+ // Draw plus signs between stacks (except last)
192
+ if (s < word.stacks - 1) {
193
+ ctx.fillStyle = colors.plus;
194
+ ctx.font = `bold ${14 * scale}px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif`;
195
+ ctx.textAlign = 'center';
196
+ ctx.fillText('+', stackX + stackWidth / 2 + (4 * scale), inputY);
197
+ }
198
+ }
199
+
200
+ // Word label below stacks
201
+ const labelY = inputY + logprobStackHeight / 2 + (12 * scale);
202
+ ctx.fillStyle = colors.originalToken;
203
+ ctx.beginPath();
204
+ ctx.roundRect(currentX, labelY - (8 * scale), word.width, 16 * scale, 4 * scale);
205
+ ctx.fill();
206
+
207
+ ctx.fillStyle = colors.text;
208
+ ctx.font = `${9 * scale}px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif`;
209
+ ctx.textAlign = 'center';
210
+ ctx.textBaseline = 'middle';
211
+ ctx.fillText(word.text, currentX + word.width / 2, labelY);
212
+
213
+ // Vertical dotted line
214
+ ctx.strokeStyle = colors.line;
215
+ ctx.lineWidth = 1;
216
+ ctx.setLineDash([3, 3]);
217
+ ctx.beginPath();
218
+ ctx.moveTo(wordCenterX, labelY + (8 * scale));
219
+ ctx.lineTo(wordCenterX, outputY - logprobStackHeight / 2);
220
+ ctx.stroke();
221
+ ctx.setLineDash([]);
222
+
223
+ currentX += word.width + spacing;
224
+ });
225
+
226
+ // Draw output logprob tensors (blue stacks) - 5 rectangles per stack
227
+ currentX = padding;
228
+ words.forEach((word) => {
229
+ const wordCenterX = currentX + word.width / 2;
230
+
231
+ // Single output stack (merged) - 5 rectangles!
232
+ const outputRectsPerStack = 5;
233
+ const outputStackWidth = (logprobRectSize + logprobSpacing) * outputRectsPerStack - logprobSpacing;
234
+ const adjustedStackX = wordCenterX - outputStackWidth / 2;
235
+
236
+ for (let i = 0; i < outputRectsPerStack; i++) {
237
+ const rectY = outputY - logprobStackHeight / 2 + i * (logprobRectSize + logprobSpacing);
238
+ const rectX = adjustedStackX + i * (logprobRectSize + logprobSpacing);
239
+ ctx.fillStyle = colors.mergedToken;
240
+ ctx.beginPath();
241
+ ctx.roundRect(rectX, rectY, logprobRectSize, logprobRectSize, 3);
242
+ ctx.fill();
243
+ }
244
+
245
+ // Word label below output stacks
246
+ const labelY = outputY + logprobStackHeight / 2 + (12 * scale);
247
+ ctx.fillStyle = colors.mergedToken;
248
+ ctx.beginPath();
249
+ ctx.roundRect(currentX, labelY - (8 * scale), word.width, 16 * scale, 4 * scale);
250
+ ctx.fill();
251
+
252
+ ctx.fillStyle = colors.text;
253
+ ctx.font = `${9 * scale}px -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif`;
254
+ ctx.textAlign = 'center';
255
+ ctx.textBaseline = 'middle';
256
+ ctx.fillText(word.text, currentX + word.width / 2, labelY);
257
+
258
+ currentX += word.width + spacing;
259
+ });
260
+ };
261
+
262
+ const setupCanvas = (canvasId, drawFn) => {
263
+ const container = document.querySelector('.sequence-alignment-visualization');
264
+ if (!container) return;
265
+
266
+ const canvas = document.getElementById(canvasId);
267
+ if (!canvas) return;
268
+
269
+ const resize = () => {
270
+ const sectionContainer = canvas.closest('.section-container');
271
+ if (!sectionContainer) return;
272
+
273
+ const containerRect = container.getBoundingClientRect();
274
+ const sectionRect = sectionContainer.getBoundingClientRect();
275
+
276
+ // Account for padding (20px each side) and label space (30px at top)
277
+ const width = Math.max(100, sectionRect.width - 40);
278
+ const height = Math.max(150, containerRect.height - 50);
279
+
280
+ canvas.width = width;
281
+ canvas.height = height;
282
+
283
+ const colors = getColors();
284
+ drawFn(canvas, colors);
285
+ };
286
+
287
+ // Watch for theme changes
288
+ const observer = new MutationObserver(() => {
289
+ resize();
290
+ });
291
+ observer.observe(document.documentElement, {
292
+ attributes: true,
293
+ attributeFilter: ['data-theme']
294
+ });
295
+
296
+ if (window.ResizeObserver) {
297
+ const ro = new ResizeObserver(resize);
298
+ ro.observe(container);
299
+ } else {
300
+ window.addEventListener('resize', resize);
301
+ }
302
+
303
+ resize();
304
+ };
305
+
306
+ const bootstrap = () => {
307
+ setupCanvas('canvas-section1', drawSection1);
308
+ setupCanvas('canvas-section2', drawSection2);
309
+ };
310
+
311
+ if (document.readyState === 'loading') {
312
+ document.addEventListener('DOMContentLoaded', bootstrap, { once: true });
313
+ } else {
314
+ bootstrap();
315
+ }
316
+ })();
317
+ </script>