File size: 27,569 Bytes
58eaa29
 
 
 
 
 
 
 
4c03e6e
58eaa29
 
4c03e6e
 
 
 
905d68e
4c03e6e
 
 
 
 
905d68e
4c03e6e
 
 
 
58eaa29
905d68e
58eaa29
905d68e
58eaa29
4c03e6e
 
 
 
 
58eaa29
 
 
 
4c03e6e
58eaa29
905d68e
58eaa29
 
905d68e
9f694c9
58eaa29
905d68e
58eaa29
 
9f694c9
58eaa29
4c03e6e
58eaa29
 
905d68e
58eaa29
4c03e6e
 
 
 
 
9f694c9
58eaa29
4c03e6e
58eaa29
4c03e6e
9f694c9
4c03e6e
905d68e
 
4c03e6e
905d68e
 
4c03e6e
 
 
905d68e
58eaa29
4c03e6e
 
 
 
 
 
 
58eaa29
905d68e
58eaa29
9f694c9
4c03e6e
 
58eaa29
905d68e
4c03e6e
58eaa29
 
 
905d68e
58eaa29
9f694c9
58eaa29
905d68e
58eaa29
 
905d68e
4c03e6e
58eaa29
4c03e6e
 
 
905d68e
58eaa29
 
4c03e6e
905d68e
58eaa29
905d68e
9f694c9
 
 
 
 
 
 
 
 
 
 
 
 
905d68e
 
 
 
 
9f694c9
 
905d68e
9f694c9
4c03e6e
 
 
 
 
 
9f694c9
905d68e
 
 
4c03e6e
905d68e
9f694c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905d68e
4c03e6e
58eaa29
 
 
 
 
9f694c9
4c03e6e
58eaa29
905d68e
58eaa29
4c03e6e
 
58eaa29
4c03e6e
 
 
58eaa29
 
 
 
4c03e6e
58eaa29
 
905d68e
4c03e6e
 
905d68e
58eaa29
4c03e6e
 
 
905d68e
 
4c03e6e
 
 
905d68e
 
4c03e6e
 
 
58eaa29
4c03e6e
 
 
58eaa29
905d68e
4c03e6e
905d68e
58eaa29
4c03e6e
 
 
 
 
 
 
58eaa29
4c03e6e
58eaa29
905d68e
4c03e6e
58eaa29
 
 
 
9f694c9
58eaa29
 
905d68e
4c03e6e
 
 
 
 
 
 
58eaa29
4c03e6e
 
58eaa29
 
4c03e6e
 
 
58eaa29
905d68e
4c03e6e
58eaa29
 
9f694c9
 
58eaa29
 
 
4c03e6e
 
9f694c9
58eaa29
4c03e6e
 
905d68e
58eaa29
4c03e6e
 
 
 
905d68e
4c03e6e
 
 
 
58eaa29
 
 
4c03e6e
 
58eaa29
 
 
905d68e
4c03e6e
58eaa29
905d68e
58eaa29
 
 
905d68e
4c03e6e
58eaa29
4c03e6e
 
 
 
 
905d68e
4c03e6e
 
58eaa29
905d68e
4c03e6e
58eaa29
 
905d68e
58eaa29
 
 
 
905d68e
4c03e6e
 
905d68e
4c03e6e
58eaa29
4c03e6e
 
 
 
905d68e
 
4c03e6e
58eaa29
 
4c03e6e
 
 
905d68e
4c03e6e
 
905d68e
4c03e6e
58eaa29
4c03e6e
 
905d68e
 
4c03e6e
 
 
 
58eaa29
905d68e
4c03e6e
58eaa29
 
 
 
 
 
 
 
 
4c03e6e
 
 
905d68e
58eaa29
 
4c03e6e
 
 
 
 
 
 
 
 
 
58eaa29
 
 
4c03e6e
 
 
58eaa29
 
4c03e6e
58eaa29
 
 
905d68e
58eaa29
 
 
 
4c03e6e
 
 
 
905d68e
4c03e6e
58eaa29
4c03e6e
 
 
 
58eaa29
 
 
 
 
 
905d68e
9f694c9
4c03e6e
58eaa29
4c03e6e
9f694c9
58eaa29
 
 
4c03e6e
58eaa29
9f694c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905d68e
 
58eaa29
 
 
 
4c03e6e
58eaa29
 
 
3c37047
905d68e
3c37047
 
 
58eaa29
 
 
 
 
3c37047
4c03e6e
58eaa29
 
 
4c03e6e
58eaa29
 
 
4c03e6e
58eaa29
 
 
 
 
9f694c9
 
 
58eaa29
 
 
 
 
 
 
9f694c9
905d68e
9f694c9
 
 
 
58eaa29
 
 
9f694c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905d68e
3c37047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905d68e
58eaa29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c37047
 
 
 
 
 
 
 
 
 
905d68e
58eaa29
 
 
 
 
 
 
 
 
 
 
9f694c9
3c37047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58eaa29
 
 
 
 
 
 
 
 
 
3c37047
58eaa29
 
 
 
 
3c37047
58eaa29
 
 
 
 
 
 
 
 
 
 
 
 
 
4c03e6e
58eaa29
 
 
 
 
 
 
 
 
 
 
 
 
 
4c03e6e
58eaa29
 
 
4c03e6e
58eaa29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
905d68e
58eaa29
4c03e6e
58eaa29
 
 
 
 
 
 
 
 
 
4c03e6e
58eaa29
 
 
 
 
905d68e
58eaa29
 
 
 
 
 
 
 
 
 
 
9f694c9
 
905d68e
58eaa29
 
 
 
905d68e
58eaa29
9f694c9
905d68e
58eaa29
 
e9ee60c
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>PhoneLLM + Pipecat XKCD Voice Agent</title>
  <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=Comic+Neue:ital,wght@0,400;0,700;1,700&family=Space+Grotesk:wght@600;700&family=JetBrains+Mono:wght@600;700&display=swap" rel="stylesheet">
  <style>
    :root {
      /* Comic / Newsprint Pastel Palette (Zero Purple) */
      --bg-paper: #fbf7ee;
      --bg-dots: rgba(0, 0, 0, 0.04);
      --ink: #1e1e24;
      
      --comic-yellow: #fef08a;
      --comic-cyan: #bae6fd;
      --comic-mint: #bbf7d0;
      --comic-coral: #fed7aa;
      --comic-pink: #fecdd3;

      --card-paper: #ffffff;
      --shadow-hard: 5px 5px 0px var(--ink);
      --shadow-hard-sm: 3px 3px 0px var(--ink);
      --shadow-hard-btn: 4px 4px 0px var(--ink);
    }

    * { box-sizing: border-box; margin: 0; padding: 0; }
    
    body {
      background-color: var(--bg-paper);
      background-image: radial-gradient(var(--bg-dots) 1.5px, transparent 1.5px);
      background-size: 16px 16px;
      color: var(--ink);
      font-family: 'Comic Neue', cursive, sans-serif;
      min-height: 100vh;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 24px 16px 48px;
    }

    header {
      text-align: center;
      max-width: 860px;
      margin-bottom: 20px;
    }

    .badge-row {
      display: flex;
      gap: 10px;
      justify-content: center;
      margin-bottom: 12px;
      flex-wrap: wrap;
    }

    .badge {
      background: var(--comic-yellow);
      border: 2px solid var(--ink);
      color: var(--ink);
      padding: 4px 14px;
      border-radius: 8px;
      font-size: 0.82rem;
      font-family: 'JetBrains Mono', monospace;
      font-weight: 700;
      text-decoration: none;
      box-shadow: var(--shadow-hard-sm);
      transition: transform 0.15s ease;
      display: inline-block;
    }
    .badge:hover {
      transform: translate(-1px, -1px);
    }

    .badge.cyan { background: var(--comic-cyan); }
    .badge.coral { background: var(--comic-coral); }
    .badge.mint { background: var(--comic-mint); }

    h1 {
      font-size: clamp(2.2rem, 5vw, 3.2rem);
      font-weight: 700;
      letter-spacing: -0.02em;
      color: var(--ink);
      text-transform: uppercase;
      text-shadow: 2px 2px 0px var(--comic-yellow), 4px 4px 0px var(--ink);
      margin-bottom: 6px;
    }

    p.sub {
      font-size: 1.1rem;
      font-weight: 700;
      color: #4b5563;
    }

    /* Comic Layout Grid */
    .main-grid {
      display: grid;
      grid-template-columns: 1fr;
      gap: 24px;
      width: 100%;
      max-width: 1140px;
    }
    @media (min-width: 880px) {
      .main-grid { grid-template-columns: 1.15fr 1fr; }
    }

    /* Comic Book Panels */
    .panel {
      background: var(--card-paper);
      border: 3px solid var(--ink);
      border-radius: 16px;
      padding: 24px;
      display: flex;
      flex-direction: column;
      box-shadow: var(--shadow-hard);
      position: relative;
    }

    /* TTS Controls Banner */
    .tts-controls-box {
      background: #fefce8;
      border: 2px solid var(--ink);
      border-radius: 12px;
      padding: 12px 14px;
      margin-bottom: 16px;
      box-shadow: var(--shadow-hard-sm);
      display: flex;
      flex-direction: column;
      gap: 8px;
    }
    .tts-row {
      display: flex;
      justify-content: space-between;
      align-items: center;
      flex-wrap: wrap;
      gap: 8px;
      font-size: 0.95rem;
      font-weight: 700;
    }
    .tts-row select {
      background: var(--comic-cyan);
      border: 2px solid var(--ink);
      color: var(--ink);
      font-family: 'Comic Neue', cursive;
      font-size: 0.95rem;
      font-weight: 700;
      padding: 4px 10px;
      border-radius: 8px;
      outline: none;
      cursor: pointer;
      box-shadow: 2px 2px 0px var(--ink);
    }
    .model-loader-bar {
      height: 8px;
      background: #e2e8f0;
      border: 1.5px solid var(--ink);
      border-radius: 9999px;
      overflow: hidden;
      display: none;
    }
    .model-loader-fill {
      height: 100%;
      background: #10b981;
      width: 0%;
      transition: width 0.2s ease;
    }
    .tts-status-label {
      font-size: 0.8rem;
      font-family: 'JetBrains Mono', monospace;
      color: #065f46;
      font-weight: 700;
      display: none;
    }

    /* Comic Mic Orb */
    .orb-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      padding: 10px 0;
      gap: 12px;
    }

    .voice-orb {
      width: 100px;
      height: 100px;
      border-radius: 50%;
      background: var(--comic-yellow);
      border: 3px solid var(--ink);
      box-shadow: var(--shadow-hard);
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
      position: relative;
    }
    .voice-orb:hover {
      transform: translate(-2px, -2px) scale(1.04);
      box-shadow: 7px 7px 0px var(--ink);
    }
    .voice-orb.listening {
      background: var(--comic-mint);
      transform: scale(1.1);
      animation: comicPulse 1s infinite alternate ease-in-out;
    }
    .voice-orb.speaking {
      background: var(--comic-coral);
      transform: scale(1.08);
      animation: comicTalk 0.8s infinite alternate ease-in-out;
    }

    @keyframes comicPulse {
      0% { transform: scale(1.05) rotate(-2deg); }
      100% { transform: scale(1.14) rotate(2deg); }
    }
    @keyframes comicTalk {
      0% { transform: scale(1.04) translateY(-2px); }
      100% { transform: scale(1.1) translateY(2px); }
    }

    .mic-icon { width: 44px; height: 44px; fill: var(--ink); pointer-events: none; }
    
    .status-badge {
      font-size: 0.95rem;
      color: var(--ink);
      font-family: 'Comic Neue', cursive;
      font-weight: 700;
      background: var(--bg-paper);
      border: 2px solid var(--ink);
      padding: 4px 14px;
      border-radius: 9999px;
      box-shadow: 2px 2px 0px var(--ink);
    }

    /* Comic Prompt Chips */
    .chips-row {
      display: flex;
      gap: 8px;
      flex-wrap: wrap;
      margin: 10px 0 12px;
    }
    .chip {
      background: #ffffff;
      border: 2px solid var(--ink);
      color: var(--ink);
      font-size: 0.9rem;
      font-family: 'Comic Neue', cursive;
      font-weight: 700;
      padding: 6px 12px;
      border-radius: 10px;
      cursor: pointer;
      box-shadow: var(--shadow-hard-sm);
      transition: all 0.15s ease;
    }
    .chip:hover {
      background: var(--comic-yellow);
      transform: translate(-1px, -1px);
      box-shadow: 4px 4px 0px var(--ink);
    }

    /* Speech Bubble Chat */
    .chat-box {
      flex: 1;
      min-height: 230px;
      max-height: 310px;
      overflow-y: auto;
      display: flex;
      flex-direction: column;
      gap: 14px;
      padding: 10px 8px 10px 2px;
      margin: 6px 0 14px;
    }
    .chat-box::-webkit-scrollbar { width: 6px; }
    .chat-box::-webkit-scrollbar-thumb { background: var(--ink); border-radius: 4px; }

    .msg {
      padding: 12px 18px;
      border: 2px solid var(--ink);
      font-size: 1.05rem;
      line-height: 1.4;
      max-width: 88%;
      position: relative;
      border-radius: 14px;
      box-shadow: var(--shadow-hard-sm);
      font-weight: 700;
    }
    .msg.user {
      align-self: flex-end;
      background: var(--comic-yellow);
      border-bottom-right-radius: 2px;
    }
    .msg.agent {
      align-self: flex-start;
      background: #ffffff;
      border-bottom-left-radius: 2px;
    }

    .tool-call-pill {
      display: inline-flex;
      align-items: center;
      gap: 6px;
      font-size: 0.8rem;
      font-family: 'JetBrains Mono', monospace;
      font-weight: 700;
      color: #065f46;
      background: var(--comic-mint);
      border: 1.5px solid var(--ink);
      padding: 2px 8px;
      border-radius: 6px;
      margin-bottom: 6px;
      box-shadow: 2px 2px 0px var(--ink);
    }

    /* Input Strip */
    .input-row {
      display: flex;
      gap: 10px;
      margin-top: auto;
    }
    input[type="text"] {
      flex: 1;
      background: #ffffff;
      border: 2px solid var(--ink);
      color: var(--ink);
      padding: 12px 16px;
      border-radius: 12px;
      outline: none;
      font-family: 'Comic Neue', cursive;
      font-size: 1.05rem;
      font-weight: 700;
      box-shadow: var(--shadow-hard-sm);
    }
    input[type="text"]:focus {
      background: #fefce8;
    }
    button.btn-send {
      background: var(--comic-coral);
      border: 2px solid var(--ink);
      color: var(--ink);
      padding: 0 22px;
      border-radius: 12px;
      font-family: 'Comic Neue', cursive;
      font-weight: 700;
      font-size: 1.1rem;
      cursor: pointer;
      box-shadow: var(--shadow-hard-btn);
      transition: all 0.15s ease;
    }
    button.btn-send:hover {
      transform: translate(-1px, -1px);
      box-shadow: 5px 5px 0px var(--ink);
      background: #f97316;
      color: white;
    }

    /* Right Comic Display Panel */
    .comic-card {
      display: flex;
      flex-direction: column;
      align-items: center;
      text-align: center;
      height: 100%;
      justify-content: center;
    }
    .comic-placeholder {
      color: #6b7280;
      font-size: 1.15rem;
      font-weight: 700;
      padding: 48px 20px;
    }
    .comic-title {
      font-size: 1.45rem;
      font-weight: 700;
      color: var(--ink);
      margin-bottom: 12px;
      background: var(--comic-yellow);
      border: 2px solid var(--ink);
      padding: 6px 16px;
      border-radius: 8px;
      box-shadow: var(--shadow-hard-sm);
      display: inline-block;
    }
    .comic-img-wrap {
      background: #ffffff;
      padding: 14px;
      border: 3px solid var(--ink);
      border-radius: 12px;
      margin: 12px 0;
      max-width: 100%;
      box-shadow: var(--shadow-hard);
    }
    .comic-img {
      max-width: 100%;
      max-height: 290px;
      object-fit: contain;
      display: block;
    }
    .comic-alt {
      font-size: 0.95rem;
      color: var(--ink);
      background: #fef3c7;
      border: 2px solid var(--ink);
      padding: 12px 16px;
      border-radius: 10px;
      text-align: left;
      margin-top: 12px;
      line-height: 1.45;
      font-weight: 700;
      box-shadow: var(--shadow-hard-sm);
    }
  </style>
</head>
<body>
  <header>
    <div class="badge-row">
      <a class="badge" href="https://huggingface.co/pipecat-ai/phonellm-alpha-1" target="_blank">🎙️ PhoneLLM Alpha 1</a>
      <span class="badge mint" id="device-cap-badge">⚡ Device Check: WASM / WebGPU</span>
      <a class="badge cyan" href="https://xkcd.hemanth.deno.net/" target="_blank">🌐 xkcd.hemanth.deno.net</a>
    </div>
    <h1>XKCD Comic Voice Agent</h1>
    <p class="sub">Comic interface with lightweight on-device neural TTS (Kokoro-82M) & live XKCD tools.</p>
  </header>

  <main class="main-grid">
    <!-- Left Voice & Speech Bubble Panel -->
    <div class="panel">
      <!-- TTS Engine & Voice Config -->
      <div class="tts-controls-box">
        <div class="tts-row">
          <span>🧠 <strong>TTS Engine:</strong></span>
          <select id="tts-engine-select">
            <option value="kokoro">Kokoro-82M Neural Model (On-Device)</option>
            <option value="system">Native System Speech (Instant)</option>
          </select>
        </div>
        <div class="tts-row" id="voice-row">
          <span>🎭 <strong>Voice Profile:</strong></span>
          <select id="voice-select">
            <option value="af_heart">Heart (Female US - Warm)</option>
            <option value="af_bella">Bella (Female US - Energetic)</option>
            <option value="am_michael">Michael (Male US - Narrator)</option>
            <option value="bf_emma">Emma (Female UK)</option>
            <option value="bm_george">George (Male UK)</option>
          </select>
        </div>
        <div class="model-loader-bar" id="loader-bar">
          <div class="model-loader-fill" id="loader-fill"></div>
        </div>
        <span class="tts-status-label" id="loader-label">⚡ Loading Kokoro 82M Neural Weights...</span>
      </div>

      <div class="orb-container">
        <div id="voice-orb" class="voice-orb" title="Click to Speak / Stop">
          <svg class="mic-icon" viewBox="0 0 24 24"><path d="M12 14c1.66 0 3-1.34 3-3V5c0-1.66-1.34-3-3-3S9 3.34 9 5v6c0 1.66 1.34 3 3 3z"/><path d="M17 11c0 2.76-2.24 5-5 5s-5-2.24-5-5H5c0 3.53 2.61 6.43 6 6.92V21h2v-3.08c3.39-.49 6-3.39 6-6.92h-2z"/></svg>
        </div>
        <span id="status-badge" class="status-badge">Tap mic orb or click a prompt!</span>
      </div>

      <div class="chips-row">
        <button class="chip" onclick="sendQuery('Show me a comic on Python programming language')">🐍 Python Comic</button>
        <button class="chip" onclick="sendQuery('What is XKCD comic 327 (Little Bobby Tables)?')">💻 Little Bobby Tables (#327)</button>
        <button class="chip" onclick="sendQuery('Show me a comic about Git version control')">🌿 Git Comic</button>
        <button class="chip" onclick="sendQuery('Show me the latest XKCD comic')">✨ Latest Comic</button>
        <button class="chip" onclick="sendQuery('Give me a random XKCD comic')">🎲 Random Comic</button>
      </div>

      <div id="chat-box" class="chat-box"></div>

      <div class="input-row">
        <input id="user-input" type="text" placeholder="Speak or type (e.g. 'Show me a comic on Python')..." />
        <button id="send-btn" class="btn-send">SEND 💥</button>
      </div>
    </div>

    <!-- Right Live Comic Frame -->
    <div class="panel">
      <div id="comic-card" class="comic-card">
        <div class="comic-placeholder">
          <p>🗯️ Ask for an XKCD comic to see the panel drawn here!</p>
        </div>
      </div>
    </div>
  </main>

  <script type="module">
    import { KokoroTTS } from "https://cdn.jsdelivr.net/npm/kokoro-js@1.2.1";

    const XKCD_API_BASE = "https://xkcd.hemanth.deno.net";
    const orb = document.getElementById('voice-orb');
    const statusBadge = document.getElementById('status-badge');
    const chatBox = document.getElementById('chat-box');
    const userInput = document.getElementById('user-input');
    const sendBtn = document.getElementById('send-btn');
    const comicCard = document.getElementById('comic-card');
    const ttsEngineSelect = document.getElementById('tts-engine-select');
    const voiceSelect = document.getElementById('voice-select');
    const loaderBar = document.getElementById('loader-bar');
    const loaderFill = document.getElementById('loader-fill');
    const loaderLabel = document.getElementById('loader-label');
    const deviceCapBadge = document.getElementById('device-cap-badge');

    let isListening = false;
    let recognition = null;
    let kokoroModel = null;
    let isLoadingKokoro = false;
    let currentAudio = null;

    // Detect Device Hardware Acceleration Capability
    async function checkDeviceCapability() {
      const hasWebGPU = !!navigator.gpu;
      const deviceLabel = hasWebGPU ? "🚀 Hardware: WebGPU Accelerated" : "⚡ Hardware: WASM SIMD Mode";
      deviceCapBadge.textContent = deviceLabel;
      console.log("Device Capability:", deviceLabel);
    }
    checkDeviceCapability();

    // Initialize Kokoro-82M Lightweight On-Device Neural Model
    async function initKokoroModel() {
      if (kokoroModel || isLoadingKokoro) return;
      isLoadingKokoro = true;
      loaderBar.style.display = "block";
      loaderLabel.style.display = "block";
      loaderLabel.textContent = "⚡ Initializing Kokoro-82M ONNX in-browser...";

      try {
        const device = navigator.gpu ? "webgpu" : "wasm";
        kokoroModel = await KokoroTTS.from_pretrained("onnx-community/Kokoro-82M-v1.0-ONNX", {
          dtype: "q8", // 8-bit quantized: fast, small footprint, cached in browser!
          device: device,
          progress_callback: (progress) => {
            if (progress.status === "progress" && progress.total) {
              const pct = Math.round((progress.loaded / progress.total) * 100);
              loaderFill.style.width = pct + "%";
              loaderLabel.textContent = `⚡ Loading Kokoro weights: ${pct}% (${(progress.loaded / 1024 / 1024).toFixed(1)}MB)`;
            }
          }
        });

        loaderFill.style.width = "100%";
        loaderLabel.textContent = "✅ Kokoro-82M Neural Model Ready On-Device!";
        setTimeout(() => {
          loaderBar.style.display = "none";
          loaderLabel.style.display = "none";
        }, 2500);
      } catch (err) {
        console.warn("Kokoro on-device load error, fallback to system TTS:", err);
        loaderLabel.textContent = "⚠️ On-device model fallback to System TTS";
        ttsEngineSelect.value = "system";
      } finally {
        isLoadingKokoro = false;
      }
    }

    // Pre-warm on-device model if device is capable
    initKokoroModel();

    // High-Fidelity Audio Synthesizer
    async function speak(text) {
      if (currentAudio) {
        currentAudio.pause();
        currentAudio = null;
      }
      if ('speechSynthesis' in window) {
        window.speechSynthesis.cancel();
      }

      orb.classList.remove('listening');
      orb.classList.add('speaking');
      statusBadge.textContent = "🔊 Speaking response...";

      const engine = ttsEngineSelect.value;
      const chosenVoice = voiceSelect.value || "af_heart";

      // 1. Try On-Device Kokoro-82M Neural TTS if loaded or loading
      if (engine === "kokoro" && kokoroModel) {
        try {
          statusBadge.textContent = "🧠 Neural synthesizing on-device...";
          const audioOutput = await kokoroModel.generate(text, {
            voice: chosenVoice,
            speed: 1.05
          });

          const blob = audioOutput.toBlob();
          const audioUrl = URL.createObjectURL(blob);
          currentAudio = new Audio(audioUrl);

          currentAudio.onended = () => {
            orb.classList.remove('speaking');
            statusBadge.textContent = "Ready!";
          };
          currentAudio.onerror = () => {
            orb.classList.remove('speaking');
            statusBadge.textContent = "Ready!";
          };

          await currentAudio.play();
          return;
        } catch (e) {
          console.warn("Kokoro synthesis error, falling back to System Speech:", e);
        }
      }

      // 2. System Speech Fallback
      if ('speechSynthesis' in window) {
        const utterance = new SpeechSynthesisUtterance(text);
        const voices = window.speechSynthesis.getVoices();
        const preferredVoice = voices.find(v => v.lang.startsWith('en') && (v.name.includes('Natural') || v.name.includes('Samantha') || v.name.includes('Google') || v.name.includes('Siri')));
        if (preferredVoice) utterance.voice = preferredVoice;
        utterance.rate = 1.05;

        utterance.onend = () => {
          orb.classList.remove('speaking');
          statusBadge.textContent = "Ready!";
        };
        utterance.onerror = () => {
          orb.classList.remove('speaking');
          statusBadge.textContent = "Ready!";
        };
        window.speechSynthesis.speak(utterance);
      }
    }

    // Topic Knowledge Index for iconic XKCD comics
    const TOPIC_INDEX = {
      "python": 353,
      "antigravity": 353,
      "sql": 327,
      "database": 327,
      "injection": 327,
      "bobby tables": 327,
      "little bobby tables": 327,
      "git": 1597,
      "version control": 1597,
      "tar": 1168,
      "zip": 1168,
      "archive": 1168,
      "regex": 208,
      "regular expression": 208,
      "password": 936,
      "correct horse": 936,
      "machine learning": 1838,
      "ai": 1838,
      "artificial intelligence": 1838,
      "compiling": 303,
      "sword fight": 303,
      "standards": 927,
      "facebook": 300,
      "social media": 300,
      "wisdom of the ancients": 979,
      "sudo": 149,
      "sandwich": 149,
      "lisp": 224,
      "c++": 435,
      "rust": 2248,
      "javascript": 2248,
      "linux": 272,
      "kernel": 272
    };

    // Live XKCD API Tool Functions
    async function toolGetLatestXkcd() {
      const res = await fetch(`${XKCD_API_BASE}/`);
      const json = await res.json();
      return json.data || json;
    }

    async function toolGetRandomXkcd() {
      const res = await fetch(`${XKCD_API_BASE}/random`);
      const json = await res.json();
      return json.data || json;
    }

    async function toolGetXkcdByNumber(num) {
      const res = await fetch(`${XKCD_API_BASE}/${num}`);
      const json = await res.json();
      return json.data || json;
    }

    async function toolSearchXkcdByTopic(topic) {
      const cleanTopic = topic.toLowerCase().trim();
      for (const [key, num] of Object.entries(TOPIC_INDEX)) {
        if (cleanTopic.includes(key) || key.includes(cleanTopic)) {
          return { comic: await toolGetXkcdByNumber(num), topic: key };
        }
      }
      return { comic: await toolGetRandomXkcd(), topic: cleanTopic };
    }

    // PhoneLLM Conversational & Tool Calling Engine
    async function processPhoneLLMTurn(userText) {
      statusBadge.textContent = "⚡ PhoneLLM Reasoning...";
      addMessage(userText, "user");

      const lower = userText.toLowerCase();
      let comic = null;
      let toolName = null;
      let replyText = "";

      const numMatch = lower.match(/\b(?:comic|number|#)?\s*(\d+)\b/);

      // Check for topic queries first
      let matchedTopic = null;
      for (const key of Object.keys(TOPIC_INDEX)) {
        if (lower.includes(key)) {
          matchedTopic = key;
          break;
        }
      }

      if (matchedTopic && (lower.includes("about") || lower.includes("on") || lower.includes("show") || lower.includes("comic") || lower.includes("tell") || lower.includes("find") || lower.includes("give") || lower.includes("python") || lower.includes("git") || lower.includes("sql"))) {
        toolName = `search_xkcd_by_topic(topic="${matchedTopic}")`;
        statusBadge.textContent = `⚙️ Executing search_xkcd_by_topic("${matchedTopic}")...`;
        const res = await toolSearchXkcdByTopic(matchedTopic);
        comic = res.comic;
        replyText = `Here is the iconic XKCD comic on ${matchedTopic.toUpperCase()}: #${comic.num} titled "${comic.title}". The punchline alt text says: "${comic.alt}"`;
      } else if (lower.includes("random") || lower.includes("any comic")) {
        toolName = "get_random_xkcd()";
        statusBadge.textContent = "⚙️ Executing get_random_xkcd...";
        comic = await toolGetRandomXkcd();
        replyText = `Here is a random XKCD comic: #${comic.num} titled "${comic.title}". The alt text says: "${comic.alt}"`;
      } else if (numMatch && (lower.includes("comic") || lower.includes("what is") || lower.includes("show") || lower.includes("#"))) {
        const comicNum = parseInt(numMatch[1], 10);
        toolName = `get_xkcd_by_number(comic_num=${comicNum})`;
        statusBadge.textContent = `⚙️ Executing get_xkcd_by_number(${comicNum})...`;
        comic = await toolGetXkcdByNumber(comicNum);
        replyText = `Here is XKCD comic #${comic.num}: "${comic.title}". The punchline alt text says: "${comic.alt}"`;
      } else if (lower.includes("latest") || lower.includes("newest") || lower.includes("today") || lower.includes("recent")) {
        toolName = "get_latest_xkcd()";
        statusBadge.textContent = "⚙️ Executing get_latest_xkcd...";
        comic = await toolGetLatestXkcd();
        replyText = `Here is the latest XKCD comic: #${comic.num} titled "${comic.title}". The alt text says: "${comic.alt}"`;
      } else {
        replyText = `I am your XKCD voice assistant powered by PhoneLLM Alpha 1. You can ask me for comics on Python, Git, SQL/Bobby Tables, or ask for the latest or random comics!`;
      }

      addMessage(replyText, "agent", toolName);
      if (comic) renderComic(comic);
      speak(replyText);
    }

    function addMessage(text, sender, toolName = null) {
      const msg = document.createElement('div');
      msg.className = `msg ${sender}`;
      
      if (toolName) {
        const toolPill = document.createElement('div');
        toolPill.className = 'tool-call-pill';
        toolPill.innerHTML = `<span>⚡ TOOL:</span> <span>${toolName}</span>`;
        msg.appendChild(toolPill);
      }

      const p = document.createElement('p');
      p.textContent = text;
      msg.appendChild(p);

      chatBox.appendChild(msg);
      chatBox.scrollTop = chatBox.scrollHeight;
    }

    function renderComic(comic) {
      if (!comic || comic.error) return;
      comicCard.innerHTML = `
        <div class="comic-title">#${comic.num}: ${comic.title} (${comic.year || ''})</div>
        <div class="comic-img-wrap">
          <img class="comic-img" src="${comic.img}" alt="${comic.alt || ''}" />
        </div>
        <div class="comic-alt"><strong>🗯️ PUNCHLINE:</strong> ${comic.alt || 'No alt text'}</div>
      `;
    }

    window.sendQuery = function(text) {
      if (!text) return;
      processPhoneLLMTurn(text);
      userInput.value = "";
    };

    sendBtn.addEventListener('click', () => sendQuery(userInput.value.trim()));
    userInput.addEventListener('keypress', (e) => {
      if (e.key === 'Enter') sendQuery(userInput.value.trim());
    });

    // Voice recognition
    if ('webkitSpeechRecognition' in window || 'SpeechRecognition' in window) {
      const SpeechRec = window.SpeechRecognition || window.webkitSpeechRecognition;
      recognition = new SpeechRec();
      recognition.continuous = false;
      recognition.interimResults = false;
      recognition.lang = 'en-US';

      recognition.onstart = () => {
        isListening = true;
        orb.classList.remove('speaking');
        orb.classList.add('listening');
        statusBadge.textContent = "🎙️ Listening to you...";
      };

      recognition.onresult = (event) => {
        const transcript = event.results[0][0].transcript;
        sendQuery(transcript);
      };

      recognition.onend = () => {
        isListening = false;
        orb.classList.remove('listening');
        statusBadge.textContent = "⚡ Processing...";
      };

      recognition.onerror = () => {
        isListening = false;
        orb.classList.remove('listening');
        statusBadge.textContent = "Tap mic orb to speak";
      };
    }

    orb.addEventListener('click', () => {
      if (!recognition) {
        alert("Speech Recognition not supported in this browser. You can type queries in the box below.");
        return;
      }
      if (isListening) {
        recognition.stop();
      } else {
        if (currentAudio) currentAudio.pause();
        if ('speechSynthesis' in window) window.speechSynthesis.cancel();
        orb.classList.remove('speaking');
        recognition.start();
      }
    });

    // Initial greeting
    setTimeout(() => {
      addMessage("🗯️ Hey! I am your XKCD voice bot powered by PhoneLLM Alpha 1 with on-device Kokoro TTS. Ask me for comics on Python, Git, SQL, or say 'Random'!", "agent");
    }, 400);
  </script>
</body>
</html>