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 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
---
// TrackioWrapper.astro
import Trackio from "./Trackio.svelte";
---

<!-- Ensure Roboto Mono is loaded for Oblivion theme -->
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
  href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;600;700&display=swap"
  rel="stylesheet"
/>

<div class="trackio-wrapper">
  <div class="trackio-controls">
    <div class="controls-left">
      <div class="theme-selector">
        <label for="theme-select">Theme</label>
        <select id="theme-select" class="theme-select">
          <option value="classic">Classic</option>
          <option value="oblivion">Oblivion</option>
        </select>
      </div>
      <div class="scale-controls">
        <label>
          <input type="checkbox" id="log-scale-x" checked />
          Log Scale X
        </label>
        <label>
          <input type="checkbox" id="smooth-data" checked />
          Smooth
        </label>
      </div>
    </div>
    <div class="controls-right">
      <button class="button button--ghost" type="button" id="randomize-btn">
        Randomize Data
      </button>
      <button
        class="button button--primary"
        type="button"
        id="start-simulation-btn"
      >
        Live Run
      </button>
      <button
        class="button button--danger"
        type="button"
        id="stop-simulation-btn"
        style="display: none;"
      >
        Stop
      </button>
    </div>
  </div>

  <div class="trackio-container">
    <Trackio client:load variant="classic" logScaleX={true} smoothing={true} />
  </div>
</div>

<script>
  // @ts-nocheck
  document.addEventListener("DOMContentLoaded", async () => {
    const themeSelect = document.getElementById("theme-select");
    const randomizeBtn = document.getElementById("randomize-btn");
    const startSimulationBtn = document.getElementById("start-simulation-btn");
    const stopSimulationBtn = document.getElementById("stop-simulation-btn");
    const logScaleXCheckbox = document.getElementById("log-scale-x");
    const smoothDataCheckbox = document.getElementById("smooth-data");
    const trackioContainer = document.querySelector(".trackio-container");

    if (
      !themeSelect ||
      !randomizeBtn ||
      !startSimulationBtn ||
      !stopSimulationBtn ||
      !logScaleXCheckbox ||
      !smoothDataCheckbox ||
      !trackioContainer
    )
      return;

    // Variables for simulation
    let simulationInterval = null;
    let currentSimulationRun = null;
    let currentStep = 0;

    // Import the store function
    const { triggerJitter } = await import("./core/store.js");

    // Theme change handler
    themeSelect.addEventListener("change", (e) => {
      const target = e.target;
      if (!target || !("value" in target)) return;

      const newVariant = target.value;
      console.log(`Theme changed to: ${newVariant}`); // Debug log

      // Find the trackio element and call setTheme on the Svelte instance
      const trackioEl = debugTrackioState();
      if (trackioEl && trackioEl.__trackioInstance) {
        console.log("βœ… Calling setTheme on Trackio instance");
        trackioEl.__trackioInstance.setTheme(newVariant);
      } else {
        console.warn("❌ No Trackio instance found for theme change");
      }
    });

    // Log scale X change handler
    logScaleXCheckbox.addEventListener("change", (e) => {
      const target = e.target;
      if (!target || !("checked" in target)) return;

      const isLogScale = target.checked;
      console.log(`Log scale X changed to: ${isLogScale}`); // Debug log

      // Find the trackio element and call setLogScaleX on the Svelte instance
      const trackioEl = debugTrackioState();
      if (trackioEl && trackioEl.__trackioInstance) {
        console.log("βœ… Calling setLogScaleX on Trackio instance");
        trackioEl.__trackioInstance.setLogScaleX(isLogScale);
      } else {
        console.warn("❌ Trackio instance not found for log scale change");
      }
    });

    // Smooth data change handler
    smoothDataCheckbox.addEventListener("change", (e) => {
      const target = e.target;
      if (!target || !("checked" in target)) return;

      const isSmooth = target.checked;
      console.log(`Smooth data changed to: ${isSmooth}`); // Debug log

      // Find the trackio element and call setSmoothing on the Svelte instance
      const trackioEl = debugTrackioState();
      if (trackioEl && trackioEl.__trackioInstance) {
        console.log("βœ… Calling setSmoothing on Trackio instance");
        trackioEl.__trackioInstance.setSmoothing(isSmooth);
      } else {
        console.warn("❌ Trackio instance not found for smooth change");
      }
    });

    // Debug function to check trackio state
    function debugTrackioState() {
      const trackioEl = trackioContainer.querySelector(".trackio");
      console.log("πŸ” Debug Trackio state:", {
        container: !!trackioContainer,
        trackioEl: !!trackioEl,
        hasInstance: !!(trackioEl && trackioEl.__trackioInstance),
        availableMethods:
          trackioEl && trackioEl.__trackioInstance
            ? Object.keys(trackioEl.__trackioInstance)
            : "none",
        windowInstance: !!window.trackioInstance,
      });
      return trackioEl;
    }

    // Initialize with default checked states - increased delay and retry logic
    function initializeTrackio(attempt = 1) {
      console.log(`πŸš€ Initializing Trackio (attempt ${attempt})`);

      const trackioEl = debugTrackioState();

      if (trackioEl && trackioEl.__trackioInstance) {
        console.log("βœ… Trackio instance found, applying initial settings");

        if (logScaleXCheckbox.checked) {
          console.log("Initializing with log scale X enabled");
          trackioEl.__trackioInstance.setLogScaleX(true);
        }

        if (smoothDataCheckbox.checked) {
          console.log("Initializing with smoothing enabled");
          trackioEl.__trackioInstance.setSmoothing(true);
        }
      } else {
        console.log("❌ Trackio instance not ready yet");
        if (attempt < 10) {
          setTimeout(() => initializeTrackio(attempt + 1), 200 * attempt);
        } else {
          console.error("Failed to initialize Trackio after 10 attempts");
        }
      }
    }

    // Start initialization
    setTimeout(() => initializeTrackio(), 100);

    // Function to generate a new simulated metric value
    function generateSimulatedValue(step, metric) {
      const baseProgress = Math.min(1, step / 100); // Normalise sur 100 steps

      if (metric === "loss") {
        // Loss that decreases with noise
        const baseLoss = 2.0 * Math.exp(-0.05 * step);
        const noise = (Math.random() - 0.5) * 0.2;
        return Math.max(0.01, baseLoss + noise);
      } else if (metric === "accuracy") {
        // Accuracy that increases with noise
        const baseAcc = 0.1 + 0.8 * (1 - Math.exp(-0.04 * step));
        const noise = (Math.random() - 0.5) * 0.05;
        return Math.max(0, Math.min(1, baseAcc + noise));
      }
      return Math.random();
    }

    // Handler to start simulation
    function startSimulation() {
      if (simulationInterval) {
        clearInterval(simulationInterval);
      }

      // Generate a new run name
      const adjectives = [
        "live",
        "real-time",
        "streaming",
        "dynamic",
        "active",
        "running",
      ];
      const nouns = [
        "experiment",
        "trial",
        "session",
        "training",
        "run",
        "test",
      ];
      const randomAdj =
        adjectives[Math.floor(Math.random() * adjectives.length)];
      const randomNoun = nouns[Math.floor(Math.random() * nouns.length)];
      currentSimulationRun = `${randomAdj}-${randomNoun}-${Date.now().toString().slice(-4)}`;
      currentStep = 1; // Start at step 1

      console.log(`Starting simulation for run: ${currentSimulationRun}`);

      // Interface UI
      startSimulationBtn.style.display = "none";
      stopSimulationBtn.style.display = "inline-flex";
      startSimulationBtn.disabled = true;

      // Ajouter le premier point
      addSimulationStep();

      // Continuer chaque seconde
      simulationInterval = setInterval(() => {
        currentStep++;
        addSimulationStep();

        // Stop after 200 steps to avoid infinity
        if (currentStep > 200) {
          stopSimulation();
        }
      }, 1000); // Chaque seconde
    }

    // Function to add a new data point
    function addSimulationStep() {
      const trackioEl = trackioContainer.querySelector(".trackio");
      if (trackioEl && trackioEl.__trackioInstance) {
        const newDataPoint = {
          step: currentStep,
          loss: generateSimulatedValue(currentStep, "loss"),
          accuracy: generateSimulatedValue(currentStep, "accuracy"),
        };

        console.log(
          `Adding simulation step ${currentStep} for run ${currentSimulationRun}:`,
          newDataPoint,
        );

        // Ajouter le point via l'instance Trackio
        if (
          typeof trackioEl.__trackioInstance.addLiveDataPoint === "function"
        ) {
          trackioEl.__trackioInstance.addLiveDataPoint(
            currentSimulationRun,
            newDataPoint,
          );
        } else {
          console.warn("addLiveDataPoint method not found on Trackio instance");
        }
      }
    }

    // Handler to stop simulation
    function stopSimulation() {
      if (simulationInterval) {
        clearInterval(simulationInterval);
        simulationInterval = null;
      }

      console.log(`Stopping simulation for run: ${currentSimulationRun}`);

      // Interface UI
      startSimulationBtn.style.display = "inline-flex";
      stopSimulationBtn.style.display = "none";
      startSimulationBtn.disabled = false;

      currentSimulationRun = null;
      currentStep = 0;
    }

    // Event listeners for simulation buttons
    startSimulationBtn.addEventListener("click", startSimulation);
    stopSimulationBtn.addEventListener("click", stopSimulation);

    // Stop the simulation if the user leaves the page
    window.addEventListener("beforeunload", stopSimulation);

    // Randomize data handler - now uses the store
    randomizeBtn.addEventListener("click", () => {
      console.log("Randomize button clicked - triggering jitter via store"); // Debug log

      // Stop the current simulation if it's running
      if (simulationInterval) {
        stopSimulation();
      }

      // Add vibration animation
      randomizeBtn.classList.add("vibrating");
      setTimeout(() => {
        randomizeBtn.classList.remove("vibrating");
      }, 600);

      // Test direct window approach as well
      if (
        window.trackioInstance &&
        typeof window.trackioInstance.jitterData === "function"
      ) {
        console.log(
          "Found window.trackioInstance, calling jitterData directly",
        ); // Debug log
        window.trackioInstance.jitterData();
      } else {
        console.log("No window.trackioInstance found, using store trigger"); // Debug log
        triggerJitter();
      }
    });
  });
</script>

<style>
  .trackio-wrapper {
    width: 100%;
    margin: 0px 0 20px 0;
  }

  .trackio-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    padding: 12px 0px;
    /* border-bottom: 1px solid var(--border-color); */
    gap: 16px;
    flex-wrap: nowrap;
  }

  .controls-left {
    display: flex;
    align-items: center;
    gap: 24px;
    flex-wrap: wrap;
  }

  .controls-right {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
  }

  .btn-randomize {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    background: var(--accent-color, #007acc);
    color: white;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
  }

  .btn-randomize:hover {
    background: var(--accent-hover, #005a9e);
    transform: translateY(-1px);
  }

  .btn-randomize:active {
    transform: translateY(0);
  }

  .theme-selector {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    flex-shrink: 0;
    white-space: nowrap;
  }

  .theme-selector label {
    font-weight: 500;
    color: var(--text-color);
  }

  .theme-select {
    padding: 6px 12px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background: var(--input-bg, var(--surface-bg));
    color: var(--text-color);
    font-size: 14px;
    cursor: pointer;
    transition: border-color 0.15s ease;
  }

  .theme-select:focus {
    outline: none;
    border-color: var(--accent-color, #007acc);
  }

  .scale-controls {
    display: flex;
    align-items: center;
    gap: 16px;
    flex-shrink: 0;
    white-space: nowrap;
  }

  /* Vibration animation for button */
  @keyframes vibrate {
    0% {
      transform: translateX(0);
    }
    10% {
      transform: translateX(-2px) rotate(-1deg);
    }
    20% {
      transform: translateX(2px) rotate(1deg);
    }
    30% {
      transform: translateX(-2px) rotate(-1deg);
    }
    40% {
      transform: translateX(2px) rotate(1deg);
    }
    50% {
      transform: translateX(-1px) rotate(-0.5deg);
    }
    60% {
      transform: translateX(1px) rotate(0.5deg);
    }
    70% {
      transform: translateX(-1px) rotate(-0.5deg);
    }
    80% {
      transform: translateX(1px) rotate(0.5deg);
    }
    90% {
      transform: translateX(-0.5px) rotate(-0.25deg);
    }
    100% {
      transform: translateX(0) rotate(0);
    }
  }

  .button.vibrating {
    animation: vibrate 0.6s ease-in-out;
  }

  .trackio-container {
    width: 100%;
    margin-top: 10px;
    border: 1px solid var(--border-color);
    padding: 24px 12px;
  }

  @media (max-width: 768px) {
    .trackio-controls {
      flex-direction: column;
      align-items: stretch;
      gap: 12px;
    }

    .controls-left {
      flex-direction: column;
      align-items: stretch;
      gap: 12px;
    }

    .theme-selector {
      justify-content: space-between;
    }

    .scale-controls {
      justify-content: space-between;
    }
  }
</style>