File size: 36,047 Bytes
d38785e
 
 
 
 
092eda1
d38785e
 
 
092eda1
d38785e
 
092eda1
 
 
 
 
 
 
 
 
 
 
 
d38785e
 
 
 
 
 
 
b67d13e
 
092eda1
 
d38785e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b67d13e
092eda1
d38785e
 
 
 
 
 
 
 
 
 
59706cf
 
d38785e
59706cf
d38785e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b67d13e
 
092eda1
b67d13e
 
 
 
 
 
092eda1
b67d13e
092eda1
b67d13e
 
 
092eda1
b67d13e
092eda1
 
b67d13e
092eda1
 
b67d13e
 
092eda1
b67d13e
092eda1
 
b67d13e
 
092eda1
b67d13e
 
 
 
 
 
092eda1
b67d13e
 
092eda1
b67d13e
 
092eda1
b67d13e
 
 
 
 
 
 
 
 
 
 
 
092eda1
b67d13e
092eda1
b67d13e
 
092eda1
b67d13e
 
 
 
 
 
092eda1
b67d13e
092eda1
b67d13e
 
 
 
d38785e
 
 
 
b67d13e
 
 
 
092eda1
59706cf
dc1d631
092eda1
dc1d631
d38785e
 
59706cf
d38785e
 
 
 
 
 
 
 
 
 
 
 
dc1d631
 
d38785e
 
 
092eda1
d38785e
 
 
 
 
 
 
b67d13e
4984ab5
d38785e
 
 
 
 
092eda1
d38785e
 
 
 
59706cf
d38785e
 
 
59706cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d38785e
 
 
 
 
 
 
59706cf
 
 
d38785e
 
092eda1
 
 
b67d13e
 
 
d38785e
 
 
 
 
 
 
 
59706cf
d38785e
59706cf
d38785e
59706cf
264c9dc
c69537b
59706cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d38785e
 
 
 
 
 
 
 
 
 
 
 
 
092eda1
d38785e
59706cf
d38785e
59706cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
092eda1
59706cf
 
 
 
 
 
d38785e
 
 
 
59706cf
 
 
 
 
 
 
 
 
 
 
 
 
 
d38785e
 
 
 
 
59706cf
d38785e
59706cf
 
 
 
 
 
 
 
 
d38785e
 
 
59706cf
 
 
 
 
 
d38785e
 
 
59706cf
d38785e
 
 
 
 
 
 
 
59706cf
d38785e
59706cf
d38785e
 
 
 
59706cf
 
 
 
 
 
d38785e
dab29e6
d38785e
dab29e6
d38785e
59706cf
 
 
dab29e6
 
d38785e
b67d13e
59706cf
 
 
dab29e6
59706cf
b67d13e
59706cf
 
b67d13e
 
 
dab29e6
 
b67d13e
 
 
 
 
 
 
 
 
 
59706cf
b67d13e
59706cf
 
 
 
 
 
 
 
d38785e
 
 
 
59706cf
 
 
 
 
 
 
 
 
 
 
 
 
 
d38785e
59706cf
d38785e
59706cf
 
 
 
 
 
 
 
 
 
 
d38785e
 
 
 
59706cf
 
 
d38785e
 
dab29e6
59706cf
d38785e
 
 
 
 
 
59706cf
 
dab29e6
59706cf
 
d38785e
59706cf
 
 
d38785e
 
 
 
 
59706cf
d38785e
59706cf
 
 
 
 
 
 
 
 
 
 
 
d38785e
59706cf
 
 
 
 
 
d38785e
 
59706cf
 
 
 
 
 
 
 
 
 
dab29e6
59706cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d38785e
59706cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d38785e
dab29e6
 
b67d13e
d38785e
 
 
 
092eda1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d38785e
59706cf
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>XORTRON</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        /* Custom Styles */
        body {
            font-family: 'Inter', sans-serif; display: flex; justify-content: center; align-items: center;
            min-height: 100vh; /* background-color: #111827; */ /* Removed for matrix background */
            padding: 1rem;
            overflow: hidden; /* Prevent scrollbars from canvas */
        }
        #matrixCanvas {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1; /* Behind other content */
            display: block;
        }
        :root {
            --neon-cyan: #22d3ee; --neon-cyan-focus: #67e8f9; --neon-cyan-darker: #0e7490;
        }
        .main-container {
            background-color: #1f2937; border: 1px solid var(--neon-cyan);
            box-shadow: 0 0 15px rgba(34, 211, 238, 0.3); display: flex;
            flex-direction: column; height: 90vh; max-height: 800px;
            width: 100%; max-width: 768px;
            position: relative; /* Ensure z-index stacking context */
            z-index: 1; /* Above matrix canvas */
        }
        .dark-input {
            background-color: #374151; border: 1px solid #4b5563; color: #f3f4f6;
        }
        .dark-input::placeholder { color: #9ca3af; }
        .dark-input:focus {
            border-color: var(--neon-cyan); outline: none; box-shadow: 0 0 0 2px rgba(34, 211, 238, 0.4);
        }
        .dark-chatbox {
            background-color: #374151; border: 1px solid #4b5563; flex-grow: 1;
            overflow-y: auto; scroll-behavior: smooth;
        }
        .chat-bubble {
            max-width: 80%; padding: 0.75rem 1rem; border-radius: 1rem;
            margin-bottom: 0.5rem; word-wrap: break-word;
            overflow-wrap: break-word;
            line-height: 1.6;
        }
        .user-bubble {
            background-color: var(--neon-cyan); color: #1f2937; margin-left: auto;
            border-bottom-right-radius: 0.25rem;
        }
        .assistant-bubble {
            background-color: #4b5563; color: #f3f4f6; margin-right: auto;
            border-bottom-left-radius: 0.25rem;
        }
        .assistant-bubble.streaming::after {
            content: '▋'; animation: blink 1s step-end infinite;
            opacity: 0.7; margin-left: 2px; font-size: 0.9em;
        }
        @keyframes blink { 50% { opacity: 0; } }
        #recordButton.listening {
            animation: pulse 1.5s infinite; background-color: #ef4444; border-color: #ef4444;
        }
        #recordButton.listening:hover { background-color: #dc2626; border-color: #dc2626; }
        #recordButton { background-color: #4b5563; border: 1px solid #6b7280; }
        #recordButton:hover:not(.listening) {
             background-color: #374151; border-color: var(--neon-cyan);
             box-shadow: 0 0 8px rgba(34, 211, 238, 0.5);
        }
        #sendButton { background-color: var(--neon-cyan); color: #1f2937; }
        #sendButton:hover { background-color: var(--neon-cyan-focus); }
        #sendButton:disabled { background-color: #6b7280; color: #9ca3af; cursor: not-allowed; }
        @keyframes pulse {
            0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7); }
            70% { box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); }
            100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); }
        }
        #chatbox::-webkit-scrollbar { width: 8px; }
        #chatbox::-webkit-scrollbar-track { background: #374151; border-radius: 10px; }
        #chatbox::-webkit-scrollbar-thumb { background: #6b7280; border-radius: 10px; }
        #chatbox::-webkit-scrollbar-thumb:hover { background: var(--neon-cyan); }

        /* <<< ADDED MARKDOWN STYLES START >>> */
        .chat-bubble code:not(pre code) {
          background-color: #111827;
          padding: 0.2em 0.4em;
          margin: 0 0.1em;
          font-size: 85%;
          border-radius: 6px;
          font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
          word-wrap: break-word;
        }
        .chat-bubble pre {
          background-color: #111827;
          padding: 1em;
          border-radius: 6px;
          overflow-x: auto;
          margin: 0.8em 0;
          white-space: pre;
          color: #f3f4f6;
        }
        .chat-bubble pre code {
          background-color: transparent;
          padding: 0;
          margin: 0;
          font-size: inherit;
          border-radius: 0;
          white-space: inherit;
          color: inherit;
        }
        .chat-bubble ul, .chat-bubble ol {
          padding-left: 1.5em;
          margin-top: 0.5em;
          margin-bottom: 0.5em;
        }
        .chat-bubble li {
          margin-bottom: 0.25em;
        }
         .chat-bubble li > p {
           margin-bottom: 0;
         }
        .chat-bubble p {
            margin-bottom: 0.75em;
        }
         .chat-bubble p:last-child {
           margin-bottom: 0;
         }
        .chat-bubble strong, .chat-bubble b {
         font-weight: bold;
        }
        .chat-bubble em, .chat-bubble i {
         font-style: italic;
        }
        .chat-bubble blockquote {
          border-left: 4px solid var(--neon-cyan);
          padding-left: 1em;
          margin: 0.8em 0;
          color: #d1d5db;
        }
        .chat-bubble blockquote p {
           margin-bottom: 0.5em;
        }
        .chat-bubble a {
            color: var(--neon-cyan-focus);
            text-decoration: underline;
        }
        .chat-bubble a:hover {
            color: var(--neon-cyan);
        }
        .chat-bubble hr {
            border: none;
            border-top: 1px solid #4b5563;
            margin: 1em 0;
        }
        /* <<< ADDED MARKDOWN STYLES END >>> */

    </style>
     <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=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">

    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/3.1.4/purify.min.js" integrity="sha512-WcCfo2F+5U1zKjjKwpPszIOxeh7o3N63FvQubHDjVAQnRBCw44fAnJsFzt7o06kEMt0h8+drQvdY9e+wOHhVKA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    </head>
<body class="bg-gray-900"> <canvas id="matrixCanvas"></canvas>
    <div class="main-container p-6 md:p-8 rounded-lg shadow-xl w-full">
        <div class="text-2xl md:text-3xl font-bold mb-4 text-center text-gray-100 flex-shrink-0">
            <h1>XORTRON</h1>
        </div>

        <div id="chatbox" class="dark-chatbox rounded-md p-4 mb-4 flex flex-col space-y-2">
            </div>
        <div id="status" class="text-center text-sm text-gray-400 mb-2 h-5 flex-shrink-0"></div>
        <div class="flex items-center space-x-2 mb-4 flex-shrink-0">
            <input type="text" id="textInput" placeholder="Type your message..." class="dark-input w-full px-3 py-2 rounded-md shadow-sm text-sm flex-grow" disabled>
            <button id="sendButton" class="px-4 py-2 rounded-md font-semibold shadow-sm transition duration-150 ease-in-out focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-800 focus:ring-[var(--neon-cyan)]" disabled>
                <i class="fas fa-paper-plane"></i> Send
            </button>
        </div>
        <div class="text-center flex-shrink-0">
            <button id="recordButton" title="Start/Stop Listening" class="text-white font-bold py-3 px-5 rounded-full shadow-md transition duration-150 ease-in-out focus:outline-none">
                <i class="fas fa-microphone text-xl"></i>
            </button>
        </div>
         <div class="text-center mt-4 flex-shrink-0"> <a href='https://ko-fi.com/Z8Z51E5TIG' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi6.png?v=6' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
        </div>
    </div>

    <script>
        // Original Chat App JavaScript
        // DOM Elements
        const recordButton = document.getElementById('recordButton');
        const statusDiv = document.getElementById('status');
        const chatbox = document.getElementById('chatbox');
        const textInput = document.getElementById('textInput');
        const sendButton = document.getElementById('sendButton');

        // --- API Endpoint ---
        const API_ENDPOINT_URL = "https://vulture-awake-probably.ngrok-free.app/v1/chat/completions";

        // --- State Variables ---
        let recognition;
        let isListening = false;
        let isApiProcessing = false;
        let conversationHistory = []; 
        let restartTimer;
        let currentAssistantMessageElement = null;
        let sentenceBuffer = "";
        let spokenTextPointer = 0;
        let recognitionWasRunning = false;

        // --- Speech Recognition Setup ---
        const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
        if (!SpeechRecognition) {
            statusDiv.textContent = 'Voice input not supported.';
            recordButton.disabled = true;
            recordButton.title = 'Speech Recognition not supported in this browser.';
            recordButton.classList.add('opacity-50', 'cursor-not-allowed');
        } else {
            recognition = new SpeechRecognition();
            recognition.continuous = true;
            recognition.interimResults = false;
            recognition.lang = 'en-US';
            recognition.maxAlternatives = 1;

            recognition.onstart = () => {
                console.log('Recognition started.');
                if (isListening) statusDiv.textContent = 'Listening...';
            };

            recognition.onresult = (event) => {
                let finalTranscript = '';
                for (let i = event.resultIndex; i < event.results.length; ++i) {
                    if (event.results[i].isFinal) {
                        finalTranscript += event.results[i][0].transcript;
                    }
                }
                finalTranscript = finalTranscript.trim();
                console.log('Transcript:', finalTranscript);
                if (finalTranscript && !isApiProcessing && isListening) {
                    handleUserInput(finalTranscript);
                } else if (!finalTranscript) {
                    console.log('Empty transcript received.');
                }
            };

            recognition.onerror = (event) => {
                console.error('Speech recognition error:', event.error);
                if (event.error === 'no-speech') {
                    console.warn('Recognition error: No speech detected. Restarting if still listening.');
                } else if (event.error === 'audio-capture') {
                    console.warn('Recognition error: Audio capture issue.');
                    statusDiv.textContent = 'Mic Issue';
                } else if (event.error === 'not-allowed') {
                    statusDiv.textContent = 'Microphone access denied.';
                    addMessageToChatbox('assistant', 'Error: Microphone access denied.');
                    if (isListening) stopListening(true);
                } else {
                    statusDiv.textContent = `Voice Error: ${event.error}`;
                    if (isListening) stopListening(true);
                }
            };

            recognition.onend = () => {
                console.log('Recognition ended.');
                if (isListening && !isApiProcessing) {
                    clearTimeout(restartTimer);
                    restartTimer = setTimeout(() => {
                        if (isListening) {
                            console.log('Attempting to restart recognition...');
                            try {
                                recognition.start();
                            } catch (e) {
                                if (e.name !== 'InvalidStateError') {
                                    console.error("Error restarting recognition:", e);
                                    statusDiv.textContent = "Error restarting listening.";
                                    stopListening(true);
                                }
                            }
                        }
                    }, 250);
                } else if (!isListening) {
                    updateButtonUI(false);
                    if (!isApiProcessing && !isSpeaking && ttsQueue.length === 0) {
                         statusDiv.textContent = '';
                    }
                }
            };
        }

        // --- Text-to-Speech Setup ---
        const synth = window.speechSynthesis;
        let ttsQueue = [];
        let isSpeaking = false;

        if (!synth) {
            console.warn("Speech Synthesis not supported in this browser.");
        }

        function speakText(text) {
             let textToSpeak = text.replace(/```[\s\S]*?```/g, 'Code block.') 
                                  .replace(/`([^`]+)`/g, '$1') 
                                  .replace(/[*_~]+/g, ''); 
             if (!synth || !textToSpeak) return;
             ttsQueue.push(textToSpeak);
             processTTSQueue();
        }

        function processTTSQueue() {
            if (isSpeaking || ttsQueue.length === 0 || !synth) {
                return;
            }
            isSpeaking = true;
            const textToSpeak = ttsQueue.shift();

            setTimeout(() => {
                synth.cancel();
                const utterance = new SpeechSynthesisUtterance(textToSpeak);
                utterance.lang = 'en-US';
                utterance.rate = 1.2;
                utterance.pitch = 1;
                utterance.volume = 1;

                utterance.onstart = () => {
                    console.log("Speech started for:", textToSpeak.substring(0, 30) + "...");
                    statusDiv.textContent = 'Speaking...';
                };

                utterance.onend = () => {
                    console.log("Speech finished for:", textToSpeak.substring(0, 30) + "...");
                    isSpeaking = false;
                    if (ttsQueue.length === 0 && !isApiProcessing) {
                         enableInputs();
                         statusDiv.textContent = isListening ? 'Listening...' : '';
                         restartRecognitionIfNeeded(recognitionWasRunning);
                     }
                     processTTSQueue();
                };

                utterance.onerror = (event) => {
                    console.error('SpeechSynthesis Utterance Error:', event.error, "for text:", textToSpeak);
                    statusDiv.textContent = 'Error speaking response.';
                    isSpeaking = false;
                    if (ttsQueue.length === 0 && !isApiProcessing) {
                        enableInputs();
                        statusDiv.textContent = isListening ? 'Listening...' : '';
                        restartRecognitionIfNeeded(recognitionWasRunning);
                     }
                    processTTSQueue();
                };

                console.log("Attempting to speak:", textToSpeak.substring(0, 50) + "...");
                synth.speak(utterance);
            }, 50);
        }

        function handleUserInput(text) {
            if (!text || isApiProcessing) return;
            isApiProcessing = true;
            statusDiv.textContent = 'Processing...';
            disableInputs();
            addMessageToChatbox('user', text);
            sendToApi(text);
        }

        async function sendToApi(userText) {
            const apiEndpoint = API_ENDPOINT_URL; 
            conversationHistory.push({ role: "user", content: userText });

            statusDiv.textContent = 'Thinking...';
            currentAssistantMessageElement = null;
            sentenceBuffer = "";
            spokenTextPointer = 0;
            ttsQueue = [];
            recognitionWasRunning = false;

            if (isListening && recognition) {
                 try {
                     recognition.stop();
                     recognitionWasRunning = true;
                     console.log("Stopped recognition temporarily for API call.");
                 } catch(e) { console.warn("Could not stop recognition before API call:", e); }
            }
             if (synth && synth.speaking) {
                synth.cancel();
                isSpeaking = false;
             }

            const requestBody = {
                messages: conversationHistory,
                max_tokens: 750, 
                stream: true
            };
            const requestHeaders = {
                'Content-Type': 'application/json',
                'Accept': 'text/event-stream'
            };

            try {
                console.log("Sending request to:", apiEndpoint);
                const response = await fetch(apiEndpoint, { method: 'POST', headers: requestHeaders, body: JSON.stringify(requestBody) });

                if (!response.ok) {
                    const errorText = await response.text();
                    let detail = errorText;
                    try {
                        const errorJson = JSON.parse(errorText);
                        detail = errorJson.detail || errorJson.error?.message || errorJson.message || JSON.stringify(errorJson);
                    } catch (parseError) {}
                    throw new Error(`API Error: ${response.status} ${response.statusText} - ${detail}`);
                 }
                if (!response.body) {
                    throw new Error("Response body is null, cannot process stream.");
                }

                const reader = response.body.getReader();
                const decoder = new TextDecoder("utf-8");
                let partialChunk = "";
                let isDoneProcessingStream = false;

                while (!isDoneProcessingStream) {
                    const { done, value } = await reader.read();

                    if (done) {
                        console.log("Stream finished (reader signaled done).");
                        isDoneProcessingStream = true;
                         if (partialChunk.trim()) {
                           console.warn("Stream ended by reader 'done' with unprocessed partial chunk:", partialChunk);
                        }
                        break;
                    }

                    const chunkText = partialChunk + decoder.decode(value, { stream: true });
                    const eventStrings = chunkText.split("\n\n");

                    if (!chunkText.endsWith("\n\n") && eventStrings.length > 0) {
                        partialChunk = eventStrings.pop();
                    } else {
                        partialChunk = "";
                    }

                    for (const eventString of eventStrings) {
                        if (!eventString.trim()) continue;

                        let content = "";
                        let isDoneSignalFound = false;

                        const lines = eventString.split("\n");
                        for (const line of lines) {
                            if (line.startsWith("data:")) {
                                const dataJson = line.substring(5).trim();
                                if (dataJson === "[DONE]") {
                                    console.log("Received [DONE] signal in stream.");
                                    isDoneSignalFound = true;
                                    isDoneProcessingStream = true;
                                    break;
                                }
                                try {
                                    const data = JSON.parse(dataJson);
                                    if (data.choices && data.choices[0]?.delta?.content) {
                                        content += data.choices[0].delta.content;
                                    }
                                } catch (e) {
                                    console.error("Error parsing stream data JSON:", e, "Data:", dataJson);
                                }
                            }
                        } 

                        if (isDoneSignalFound) break; 

                        if (content) {
                            processStreamContent(content);
                        }
                    } 
                } 


                if (sentenceBuffer.length > spokenTextPointer) {
                    const remainingText = sentenceBuffer.substring(spokenTextPointer);
                    console.log("Speaking remaining text after stream:", remainingText);
                    speakText(remainingText); 
                }

                if (currentAssistantMessageElement) {
                    currentAssistantMessageElement.classList.remove('streaming');
                    if (sentenceBuffer) {
                         try {
                            marked.setOptions({
                              breaks: true, 
                              gfm: true      
                            });
                            const unsafeHtml = marked.parse(sentenceBuffer);
                            const safeHtml = DOMPurify.sanitize(unsafeHtml);
                            currentAssistantMessageElement.innerHTML = safeHtml;
                            console.log("Rendered final sanitized HTML for assistant message.");
                         } catch (e) {
                             console.error("Error processing final Markdown/HTML:", e);
                             currentAssistantMessageElement.textContent = sentenceBuffer;
                         }
                    }
                }

                if (sentenceBuffer) {
                    conversationHistory.push({ role: "assistant", content: sentenceBuffer });
                } else {
                     console.log("API call successful but no content received. Removing last user message from history.");
                      if (conversationHistory.length > 0 && conversationHistory[conversationHistory.length - 1].role === 'user') {
                         conversationHistory.pop();
                     }
                }

            } catch (error) {
                console.error('Error during API call or streaming:', error);
                if (currentAssistantMessageElement) { currentAssistantMessageElement.classList.remove('streaming'); }

                 let userFriendlyError = `Sorry, I encountered an error: ${error.message}`;
                 if (error instanceof TypeError && error.message.toLowerCase().includes('fetch')) {
                     userFriendlyError = `Connection Error: Could not connect to the API at ${apiEndpoint}. Please check the URL and network connection.`;
                     statusDiv.textContent = 'Connection Error';
                 } else {
                     statusDiv.textContent = `API Error: ${error.message.substring(0, 100)}...`;
                 }
                 addMessageToChatbox('assistant', userFriendlyError);

                 if (conversationHistory.length > 0 && conversationHistory[conversationHistory.length - 1].role === 'user') {
                    conversationHistory.pop();
                }

            } finally {
                console.log("API processing finished or errored. Entering finally block.");
                isApiProcessing = false;

                setTimeout(() => {
                    if (ttsQueue.length === 0 && !isSpeaking) {
                        console.log("Finally: TTS idle. Enabling inputs and checking recognition restart.");
                        enableInputs();
                        statusDiv.textContent = isListening ? 'Listening...' : '';
                        restartRecognitionIfNeeded(recognitionWasRunning);
                    } else {
                        console.log("Finally: TTS queue active or speaking. Inputs remain disabled. TTS onend will handle enabling/restart.");
                    }
                }, 100);
            }
        }

        function processStreamContent(content) {
            if (!currentAssistantMessageElement) {
                currentAssistantMessageElement = addMessageToChatbox('assistant', '', true);
            }
            sentenceBuffer += content;
            currentAssistantMessageElement.textContent = sentenceBuffer;
            chatbox.scrollTop = chatbox.scrollHeight; 

            let searchStart = spokenTextPointer;
            while (searchStart < sentenceBuffer.length) {
                 const sentenceEndMatch = sentenceBuffer.substring(searchStart).match(/([.?!])(?:\s|\n|$)/);
                 if (sentenceEndMatch) {
                     const sentenceEndIndex = searchStart + sentenceEndMatch.index + sentenceEndMatch[1].length;
                     const textToSpeak = sentenceBuffer.substring(spokenTextPointer, sentenceEndIndex).trim();
                     if (textToSpeak) {
                         console.log("Found sentence for TTS:", textToSpeak);
                         speakText(textToSpeak); 
                         spokenTextPointer = sentenceEndIndex;
                     }
                      searchStart = spokenTextPointer;
                 } else {
                     break;
                 }
            }
        }

        function restartRecognitionIfNeeded(wasRunning) {
            if (wasRunning && isListening && recognition && !isApiProcessing && !isSpeaking && ttsQueue.length === 0) {
                console.log("Conditions met: Restarting recognition.");
                clearTimeout(restartTimer);
                try {
                    statusDiv.textContent = 'Listening...';
                    recognition.start();
                } catch (e) {
                    if (e.name !== 'InvalidStateError') {
                        console.error("Error restarting recognition post-API/TTS:", e);
                        statusDiv.textContent = "Error restarting listening.";
                        stopListening(true);
                    } else {
                         console.log("Recognition likely already restarting or started (InvalidStateError).");
                         if(isListening) statusDiv.textContent = 'Listening...';
                    }
                }
            } else if (!isListening && !isApiProcessing && !isSpeaking && ttsQueue.length === 0) {
                 statusDiv.textContent = '';
            }
            else {
                 console.log(`Conditions not met for restarting recognition (wasRunning: ${wasRunning}, isListening: ${isListening}, isApiProcessing: ${isApiProcessing}, isSpeaking: ${isSpeaking}, ttsQueue: ${ttsQueue.length})`);
            }
        }

        function addMessageToChatbox(role, text, isStreaming = false) {
             const messageDiv = document.createElement('div');
             messageDiv.classList.add('chat-bubble');
             messageDiv.textContent = text;
             messageDiv.classList.add(role === 'user' ? 'user-bubble' : 'assistant-bubble');
             if (role === 'assistant' && isStreaming) {
                 messageDiv.classList.add('streaming');
             }
             chatbox.appendChild(messageDiv);
             chatbox.scrollTo({ top: chatbox.scrollHeight, behavior: 'smooth' });
             return messageDiv; 
         }

        function updateButtonUI(listening) {
            if (!recognition) return;
            if (listening) {
                recordButton.classList.add('listening');
                recordButton.innerHTML = '<i class="fas fa-stop text-xl"></i>';
                recordButton.title = "Stop Listening";
            } else {
                recordButton.classList.remove('listening');
                recordButton.innerHTML = '<i class="fas fa-microphone text-xl"></i>';
                recordButton.title = "Start Listening";
            }
        }

        function disableInputs() {
             console.log("Disabling inputs.");
             textInput.disabled = true;
             sendButton.disabled = true;
             if (recognition) {
                 recordButton.disabled = true;
                 recordButton.classList.add('opacity-50');
             }
         }

        function enableInputs() {
             console.log("Enabling inputs.");
             textInput.disabled = false;
             sendButton.disabled = textInput.value.trim() === '' || isApiProcessing;
             if (recognition) {
                 recordButton.disabled = false;
                 recordButton.classList.remove('opacity-50');
             }
         }

        function stopListening(forceStop = false) {
             if (!recognition) return;
             const wasListening = isListening;
             isListening = false;
             if (wasListening) {
                 console.log("Stopping listening session.");
                 clearTimeout(restartTimer);
                 updateButtonUI(false);
                 if (!isApiProcessing && !isSpeaking && ttsQueue.length === 0) {
                    statusDiv.textContent = 'Stopping...';
                    setTimeout(() => {
                        if (statusDiv.textContent === 'Stopping...') { statusDiv.textContent = ''; }
                    }, 500);
                 }
                 try {
                     recognition.abort();
                     console.log("Recognition aborted.");
                 } catch (e) {
                     console.warn("Error aborting recognition (might have already stopped):", e);
                 }
             }
             if (synth) {
                console.log("Cancelling any TTS on stopListening.");
                synth.cancel();
                ttsQueue = [];
                isSpeaking = false;
             }
             if (!isApiProcessing) {
                enableInputs();
                if (!isSpeaking && ttsQueue.length === 0) {
                    statusDiv.textContent = '';
                }
             }
        }

        function startListening() {
             if (!recognition || isListening) return;
             navigator.mediaDevices.getUserMedia({ audio: true })
                 .then(stream => {
                     stream.getTracks().forEach(track => track.stop());
                     console.log("Microphone permission granted or already available.");
                     isListening = true;
                     updateButtonUI(true);
                     statusDiv.textContent = 'Starting...';
                     try {
                         recognition.start();
                     } catch (e) {
                         console.error("Error starting recognition:", e);
                         statusDiv.textContent = "Error starting listening.";
                         isListening = false;
                         updateButtonUI(false);
                     }
                 })
                 .catch(err => {
                     console.error("Microphone access error:", err);
                     if (err.name === 'NotAllowedError' || err.name === 'PermissionDeniedError') {
                        statusDiv.textContent = 'Microphone access denied.';
                        addMessageToChatbox('assistant', 'Error: Microphone access is required for voice input.');
                     } else {
                        statusDiv.textContent = `Mic Error: ${err.name}`;
                        addMessageToChatbox('assistant', `Error accessing microphone: ${err.message}`);
                     }
                     isListening = false;
                     updateButtonUI(false);
                 });
         }

        recordButton.addEventListener('click', () => {
            if (!recognition) return;
            if (!isListening) {
                startListening();
            } else {
                stopListening();
            }
        });

        sendButton.addEventListener('click', () => {
            const text = textInput.value.trim();
            if (text && !isApiProcessing) {
                handleUserInput(text);
                textInput.value = '';
                sendButton.disabled = true;
            }
        });

        textInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                const text = textInput.value.trim();
                if (text && !sendButton.disabled) {
                    handleUserInput(text);
                    textInput.value = '';
                    sendButton.disabled = true;
                }
            }
        });

        textInput.addEventListener('input', () => {
             sendButton.disabled = textInput.value.trim() === '' || isApiProcessing;
         });

        chatbox.innerHTML = ''; 
        addMessageToChatbox('assistant', 'Hello! Use the microphone or type a message below.'); 
        console.log("Voice/Text Chat App Initialized (Markdown Enabled)");
        updateButtonUI(false);
        enableInputs();

    </script>

    <script>
        // Matrix Rain Effect
        const matrixCanvas = document.getElementById('matrixCanvas');
        const matrixCtx = matrixCanvas.getContext('2d');

        matrixCanvas.width = window.innerWidth;
        matrixCanvas.height = window.innerHeight;

        const katakana = 'アァカサタナハマヤャラワガザダバパイィキシチニヒミリヰギジヂビピウゥクスツヌフムユュルグズブヅプエェケセテネヘメレヱゲゼデベペオォコソトノホモヨョロヲゴゾドボポヴッン';
        const latin = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        const nums = '0123456789';
        const matrixCharacters = katakana + latin + nums;

        const matrixFontSize = 16;
        let matrixColumns = Math.floor(matrixCanvas.width / matrixFontSize);
        let matrixDrops = [];

        function initializeMatrixDrops() {
            matrixColumns = Math.floor(matrixCanvas.width / matrixFontSize);
            matrixDrops = [];
            for (let x = 0; x < matrixColumns; x++) {
                matrixDrops[x] = 1 + Math.floor(Math.random() * (matrixCanvas.height / matrixFontSize));
            }
        }
        initializeMatrixDrops();

        function drawMatrix() {
            matrixCtx.fillStyle = 'rgba(0, 0, 0, 0.04)'; // Slower fade for more pronounced trails
            matrixCtx.fillRect(0, 0, matrixCanvas.width, matrixCanvas.height);

            matrixCtx.fillStyle = '#0F0'; // Green text (classic matrix)
            matrixCtx.font = matrixFontSize + 'px monospace';

            for (let i = 0; i < matrixDrops.length; i++) {
                const text = matrixCharacters.charAt(Math.floor(Math.random() * matrixCharacters.length));
                matrixCtx.fillText(text, i * matrixFontSize, matrixDrops[i] * matrixFontSize);

                if (matrixDrops[i] * matrixFontSize > matrixCanvas.height && Math.random() > 0.975) {
                    matrixDrops[i] = 0;
                }
                matrixDrops[i]++;
            }
        }

        let matrixInterval = setInterval(drawMatrix, 40);

        window.addEventListener('resize', () => {
            const oldWidth = matrixCanvas.width;
            const oldHeight = matrixCanvas.height;
            
            matrixCanvas.width = window.innerWidth;
            matrixCanvas.height = window.innerHeight;

            if (matrixCanvas.width !== oldWidth || matrixCanvas.height !== oldHeight) {
                initializeMatrixDrops();
            }
        });
    </script>
</body>
</html>