File size: 26,234 Bytes
2cfe5ab
 
 
 
 
 
c5db608
2cfe5ab
c5db608
fcc36da
 
c5db608
 
 
 
2cfe5ab
 
c5db608
2cfe5ab
 
 
 
c5db608
2cfe5ab
 
 
c5db608
2cfe5ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5db608
2cfe5ab
c5db608
2cfe5ab
 
825b15d
c5db608
 
 
 
 
 
 
 
 
 
d68db78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5db608
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cfe5ab
 
 
 
 
c5db608
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cfe5ab
 
 
 
 
c5db608
2cfe5ab
 
 
 
 
 
c5db608
 
2cfe5ab
fcc36da
 
2cfe5ab
 
 
c5db608
 
 
 
 
 
 
 
 
 
 
2cfe5ab
c5db608
 
 
 
 
 
 
 
 
 
 
2cfe5ab
 
 
c5db608
2cfe5ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5db608
2cfe5ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5db608
 
 
 
 
 
2cfe5ab
 
 
 
 
 
 
 
 
 
d68db78
2cfe5ab
 
 
 
 
 
 
 
 
 
d68db78
 
 
 
 
 
 
 
 
c5db608
2cfe5ab
 
 
 
fcc36da
2cfe5ab
 
 
 
 
 
 
 
 
 
 
 
 
 
fcc36da
2cfe5ab
c5db608
 
 
 
2cfe5ab
 
 
c5db608
 
2cfe5ab
 
 
 
c5db608
 
 
 
 
 
2cfe5ab
 
 
 
 
 
 
fcc36da
 
c5db608
 
 
 
 
 
 
 
 
fcc36da
c5db608
fcc36da
 
2cfe5ab
 
d68db78
 
 
 
c5db608
2cfe5ab
fcc36da
2cfe5ab
 
 
 
c5db608
 
 
 
 
fcc36da
c5db608
2cfe5ab
 
 
c5db608
 
 
 
 
2cfe5ab
 
 
c5db608
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cfe5ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d68db78
 
 
 
 
2cfe5ab
 
458c90a
c72ff5d
 
 
2cfe5ab
 
c5db608
2cfe5ab
 
 
 
 
 
 
c5db608
 
 
2cfe5ab
 
c5db608
2cfe5ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5db608
2cfe5ab
c5db608
2cfe5ab
 
c5db608
 
2cfe5ab
 
c5db608
2cfe5ab
 
 
 
 
 
 
 
 
 
c5db608
2cfe5ab
 
 
 
 
 
 
 
 
 
 
c5db608
 
 
 
 
 
 
 
 
 
 
2cfe5ab
c5db608
 
 
 
 
 
2cfe5ab
c5db608
 
 
 
 
 
2cfe5ab
 
 
c5db608
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cfe5ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5db608
 
 
825b15d
c5db608
 
 
 
 
2cfe5ab
 
 
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
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Speedy Chat</title>
    <!-- Tailwind CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
    <!-- Prism.js for code highlighting -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/themes/prism-tomorrow.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/plugins/line-numbers/prism-line-numbers.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/prism.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.24.1/plugins/line-numbers/prism-line-numbers.min.js"></script>
    <!-- TinyMCE for rich text editing -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/5.10.7/tinymce.min.js"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Sans+Arabic:wght@100;200;300;400;500;600;700&display=swap');
        @import url('https://fonts.googleapis.com/css2?family=Noto+Kufi+Arabic:wght@100;200;300;400;500;600;700;800;900&display=swap');
        
        body {
            font-family: 'IBM Plex Sans Arabic', sans-serif;
            line-height: 1.6;
            letter-spacing: -0.01em;
            transition: background-color 0.3s, color 0.3s;
        }
        
        /* Light/Dark Mode Styles */
        body.light-mode {
            background-color: #fafafa;
            color: #374151;
        }
        
        body.dark-mode {
            background-color: #1a1a1a;
            color: #e5e5e5;
        }
        
        .dark-mode header {
            background-color: #2d2d2d;
            border-color: #404040;
        }
        
        .dark-mode footer {
            background-color: #2d2d2d;
            border-color: #404040;
        }
        
        .dark-mode .message-input {
            background-color: #363636;
            border-color: #404040;
            color: #e5e5e5;
        }
        
        .dark-mode .style-select {
            background-color: #363636;
            border-color: #404040;
            color: #e5e5e5;
        }
        
        .dark-mode .bot-message {
            background-color: #363636;
            color: #e5e5e5;
        }

        .dark-mode .action-button {
            color: #e5e5e5;
            background-color: #404040;
        }

        /* Typing Indicator Styles */
        .typing-indicator {
            display: flex;
            align-items: center;
            padding: 0.5rem 1rem;
        }

        .typing-indicator span {
            height: 8px;
            width: 8px;
            margin: 0 1px;
            background-color: #6366f1;
            display: inline-block;
            border-radius: 50%;
            opacity: 0.4;
            animation: typing 1s infinite ease-in-out;
        }

        .typing-indicator span:nth-child(1) {
            animation-delay: 200ms;
        }

        .typing-indicator span:nth-child(2) {
            animation-delay: 300ms;
        }

        .typing-indicator span:nth-child(3) {
            animation-delay: 400ms;
        }

        @keyframes typing {
            0% { transform: translateY(0px); opacity: 0.4; }
            50% { transform: translateY(-5px); opacity: 0.8; }
            100% { transform: translateY(0px); opacity: 0.4; }
        }

        /* Code Block Styles */
        pre[class*="language-"] {
            direction: ltr;
            text-align: left;
            border-radius: 0.5rem;
            margin: 1rem 0;
            padding: 1rem;
            font-size: 0.9rem;
            line-height: 1.5;
        }

        .code-block-wrapper {
            position: relative;
            margin: 1rem 0;
            border-radius: 0.5rem;
            overflow: hidden;
        }

        .code-block-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            background: #2d2d2d;
            color: white;
            padding: 0.5rem 1rem;
        }

        /* Long Text Styles */
        .long-text-wrapper {
            max-height: 300px;
            overflow-y: auto;
            padding: 1rem;
            background: #f8f9fa;
            border-radius: 0.5rem;
            margin: 1rem 0;
        }

        /* Rich Text Editor Styles */
        .editor-wrapper {
            display: none;
            background: white;
            border-radius: 0.5rem;
            margin: 1rem 0;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .editor-toolbar {
            display: flex;
            gap: 0.5rem;
            padding: 0.5rem;
            border-bottom: 1px solid #e5e7eb;
        }

        /* Message Input Styles */
        .message-input {
            resize: none;
            min-height: 50px;
            max-height: 200px;
        }

        .message-input:focus {
            outline: none;
            box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
        }

        /* Welcome Text Style */
        .welcome-text {
            font-family: 'Noto Kufi Arabic', sans-serif;
            font-size: 2rem;
            font-weight: 700;
            text-align: center;
            margin: 1.5rem 0;
            letter-spacing: -0.02em;
        }

        /* Message Animation */
        .message {
            opacity: 0;
            transform: translateY(20px);
            animation: fadeIn 0.3s ease forwards;
        }

        @keyframes fadeIn {
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* Utility Classes */
        .bot-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            object-fit: cover;
        }

        .action-button {
            padding: 0.5rem;
            border-radius: 0.375rem;
            transition: all 0.2s;
            display: inline-flex;
            align-items: center;
            gap: 0.5rem;
            font-size: 0.875rem;
            color: #6B7280;
            background-color: #F3F4F6;
        }

        .action-button:hover {
            background-color: #E5E7EB;
        }

        .style-select {
            appearance: none;
            background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
            background-repeat: no-repeat;
            background-position: left 0.5rem center;
            background-size: 1em;
        }
    </style>
</head>
<body class="text-lg light-mode">
    <header class="fixed top-0 left-0 right-0 bg-white border-b border-gray-100 z-50 shadow-sm">
        <div class="flex items-center px-4 py-2">
            <div class="flex items-center flex-1">
                <span class="text-2xl font-bold text-indigo-600">Speedy</span>
            </div>
            <button id="darkModeToggle" class="text-gray-500 hover:bg-gray-50 p-2 rounded-full transition-colors mr-2">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.354 15.354A9 9 0 018.646 3.646 9.003 9.003 0 0012 21a9.003 9.003 0 008.354-5.646z"></path>
                </svg>
            </button>
            <button id="clearChat" class="text-gray-500 hover:bg-gray-50 p-2 rounded-full transition-colors">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
                </svg>
            </button>
        </div>
    </header>

    <main class="pt-24 pb-24">
        <div class="max-w-3xl mx-auto px-4">
            <div class="welcome-text">سبيدي هنا لمساعدتك</div>
            <div id="messagesContainer" class="space-y-4"></div>
        </div>
    </main>

    <footer class="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-100 shadow-sm">
        <div class="max-w-3xl mx-auto px-4 py-4">
            <div class="relative">
                <textarea 
                    id="messageInput"
                    class="message-input w-full border border-gray-200 rounded-lg px-4 py-3 pl-32 pr-4 resize-none text-lg"
                    rows="1"
                    placeholder="اكتب رسالتك هنا..."
                ></textarea>
                <div class="absolute bottom-3 left-2 flex items-center space-x-2 rtl:space-x-reverse">
                    <button id="sendMessage" class="bg-indigo-500 hover:bg-indigo-600 text-white rounded-full p-2 transition-colors">
                        <svg class="w-6 h-6 transform rotate-90" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18"></path>
                        </svg>
                    </button>
                    <a href="https://joermd-test11.static.hf.space" target="_blank" id="micButton" class="bg-green-500 hover:bg-green-600 text-white rounded-full p-2 transition-colors">
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11a7 7 0 01-7 7m0 0a7 7 0 01-7-7m7 7v4m0 0H8m4 0h4m-4-8a3 3 0 01-3-3V5a3 3 0 116 0v6a3 3 0 01-3 3z"></path>
                        </svg>
                    </a>
                    <select id="styleSelect" class="style-select bg-gray-50 border border-gray-200 text-gray-600 text-sm rounded-lg pl-8 pr-2 py-2 focus:outline-none focus:ring-2 focus:ring-indigo-200">
                        <option value="short">⚡ ردود قصيرة</option>
                        <option value="normal" selected>◯ عادي</option>
                        <option value="long">↔ ردود مفصلة</option>
                    </select>
                </div>
            </div>
        </div>
    </footer>

    <script>
        const API_URL = 'https://nvgsxt9jqx730v-7777.proxy.runpod.net/proxy/8000/chat';
        const messagesContainer = document.getElementById('messagesContainer');
        const messageInput = document.getElementById('messageInput');
        const sendButton = document.getElementById('sendMessage');
        const clearButton = document.getElementById('clearChat');
        const styleSelect = document.getElementById('styleSelect');
        const darkModeToggle = document.getElementById('darkModeToggle');
        let chatHistory = [];
        let currentStyle = 'normal';
        let currentController = null;

        function createTypingIndicator() {
            return `
                <div class="typing-indicator">
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
            `;
        }

        function createUserMessage(text) {
            return `
                <div class="message flex justify-end mb-4">
                    <div class="max-w-[80%]">
                        <div class="bg-indigo-500 text-white rounded-lg p-4 shadow-sm">
                            <p class="text-lg">${text}</p>
                        </div>
                    </div>
                </div>
            `;
        }

        function createBotMessage(text, messageId) {
            return `
                <div class="message flex justify-start mb-4">
                    <div class="flex-shrink-0 mt-1">
                        <img src="https://ufastpro.com/wp-content/uploads/2024/12/3.png" alt="Bot Avatar" class="bot-avatar">
                    </div>
                    <div class="max-w-[80%] mr-3">
                        <div class="bot-message bg-white rounded-lg p-4 shadow-sm">
                            <p id="${messageId}" class="text-gray-700"></p>
                            <div class="message-actions">
                                <button class="action-button copy-button" data-message-id="${messageId}">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2m0 0h2a2 2 0 012 2v3m2 4H10m0 0l3-3m-3 3l3 3"></path>
                                    </svg>
                                    نسخ
                                </button>
                                <button class="action-button regenerate-button" data-message-id="${messageId}">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>
                                    </svg>
                                    إعادة التوليد
                                </button>
                                <button class="action-button edit-text-btn">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"></path>
                                    </svg>
                                    تحرير كمستند
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            `;
        }

        function formatCodeBlock(code, language) {
            return `
                <div class="code-block-wrapper">
                    <div class="code-block-header">
                        <span>${language}</span>
                        <button class="copy-code-btn">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2"></path>
                            </svg>
                        </button>
                    </div>
                    <pre class="line-numbers"><code class="language-${language}">${code}</code></pre>
                </div>`;
        }

        async function typeText(elementId, text) {
            const element = document.getElementById(elementId);
            const typingIndicator = element.querySelector('.typing-indicator');
            if (typingIndicator) {
                typingIndicator.remove();
            }
            element.innerHTML = '';

            const codeBlockRegex = /```(\w+)\n([\s\S]+?)```/g;
            let lastIndex = 0;
            let match;

            while ((match = codeBlockRegex.exec(text)) !== null) {
                const beforeText = text.slice(lastIndex, match.index);
                if (beforeText) {
                    await addTextWithEditor(element, beforeText);
                }

                const [, language, code] = match;
                element.innerHTML += formatCodeBlock(code.trim(), language);
                lastIndex = match.index + match[0].length;
            }

            const remainingText = text.slice(lastIndex);
            if (remainingText) {
                await addTextWithEditor(element, remainingText);
            }

            Prism.highlightAllUnder(element);
        }

        async function addTextWithEditor(element, text) {
            const textWrapper = document.createElement('div');
            textWrapper.className = 'long-text-wrapper';
            textWrapper.innerHTML = text;
            
            const editorWrapper = document.createElement('div');
            editorWrapper.className = 'editor-wrapper';
            editorWrapper.innerHTML = `
                <div class="editor-toolbar">
                    <button class="save-btn action-button">حفظ</button>
                    <button class="cancel-btn action-button">إلغاء</button>
                </div>
                <textarea class="tinymce-editor"></textarea>
            `;
            
            element.appendChild(textWrapper);
            element.appendChild(editorWrapper);

            tinymce.init({
                selector: '.tinymce-editor:last',
                directionality: 'rtl',
                height: 400,
                menubar: false,
                plugins: 'lists link table',
                toolbar: 'undo redo | formatselect | bold italic | alignleft aligncenter alignright | bullist numlist | link table',
                content_style: 'body { font-family: "IBM Plex Sans Arabic", sans-serif; }',
                setup: function(editor) {
                    editor.on('init', function() {
                        editor.setContent(text);
                    });
                }
            });
        }

        async function sendMessage() {
            const message = messageInput.value.trim();
            if (!message) return;

            if (currentController) {
                currentController.abort();
                currentController = null;
                toggleSendButton(false);
                return;
            }

            messageInput.value = '';
            adjustTextareaHeight();

            const messageId = 'msg-' + Date.now();
            let actualMessage = message;
            
            if (currentStyle === 'short') {
                actualMessage = 'اعطني رد باختصار وسرعة: ' + message;
            } else if (currentStyle === 'long') {
                actualMessage = 'اعطني رد مفصل وموسع: ' + message;
            }

            messagesContainer.insertAdjacentHTML('beforeend', createUserMessage(message));
            messagesContainer.insertAdjacentHTML('beforeend', createBotMessage('', messageId));
            
            // Add typing indicator
            const messageElement = document.getElementById(messageId);
            messageElement.innerHTML = createTypingIndicator();
            
            scrollToBottom();

            try {
                currentController = new AbortController();
                toggleSendButton(true);

                const response = await fetch(API_URL, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({
                        message: actualMessage,
                        history: chatHistory
                    }),
                    signal: currentController.signal
                });

                const data = await response.json();
                await typeText(messageId, data.response);
                
                chatHistory.push({
                    human: actualMessage,
                    assistant: data.response
                });
            } catch (error) {
                if (error.name === 'AbortError') {
                    document.getElementById(messageId).textContent = 'تم إيقاف التوليد.';
                } else {
                    document.getElementById(messageId).textContent = 'عذراً، حدث خطأ في المعالجة.';
                }
            } finally {
                currentController = null;
                toggleSendButton(false);
            }
            scrollToBottom();
        }

        function toggleSendButton(isGenerating) {
            const sendButton = document.getElementById('sendMessage');
            if (isGenerating) {
                sendButton.classList.add('stop-generation');
                sendButton.innerHTML = `
                    <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
                    </svg>
                `;
            } else {
                sendButton.classList.remove('stop-generation');
                sendButton.innerHTML = `
                    <svg class="w-6 h-6 transform rotate-90" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 10l7-7m0 0l7 7m-7-7v18"></path>
                    </svg>
                `;
            }
        }

        function adjustTextareaHeight() {
            messageInput.style.height = 'auto';
            messageInput.style.height = messageInput.scrollHeight + 'px';
        }

        function scrollToBottom() {
            window.scrollTo({
                top: document.documentElement.scrollHeight,
                behavior: 'smooth'
            });
        }

        // Event Listeners
        document.addEventListener('click', async (e) => {
            if (e.target.closest('.copy-code-btn')) {
                const codeBlock = e.target.closest('.code-block-wrapper').querySelector('code');
                await navigator.clipboard.writeText(codeBlock.textContent);
                const btn = e.target.closest('.copy-code-btn');
                btn.innerHTML = 'تم النسخ!';
                setTimeout(() => {
                    btn.innerHTML = `<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2"></path>
                    </svg>`;
                }, 2000);
            }

            if (e.target.closest('.copy-button')) {
                const messageId = e.target.closest('.copy-button').dataset.messageId;
                const text = document.getElementById(messageId).textContent;
                await navigator.clipboard.writeText(text);
                const btn = e.target.closest('.copy-button');
                btn.textContent = 'تم النسخ!';
                setTimeout(() => {
                    btn.innerHTML = `
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 5H6a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2v-1M8 5a2 2 0 002 2h2a2 2 0 002-2M8 5a2 2 0 012-2h2a2 2 0 012 2"></path>
                        </svg>
                        نسخ
                    `;
                }, 2000);
            }

            if (e.target.closest('.edit-text-btn')) {
                const messageWrapper = e.target.closest('.bot-message');
                const textWrapper = messageWrapper.querySelector('.long-text-wrapper');
                const editorWrapper = messageWrapper.querySelector('.editor-wrapper');
                
                if (textWrapper && editorWrapper) {
                    textWrapper.style.display = 'none';
                    editorWrapper.style.display = 'block';
                }
            }

            if (e.target.closest('.save-btn')) {
                const messageWrapper = e.target.closest('.bot-message');
                const textWrapper = messageWrapper.querySelector('.long-text-wrapper');
                const editorWrapper = messageWrapper.querySelector('.editor-wrapper');
                const editor = tinymce.get(editorWrapper.querySelector('.tinymce-editor').id);
                
                if (textWrapper && editor) {
                    textWrapper.innerHTML = editor.getContent();
                    textWrapper.style.display = 'block';
                    editorWrapper.style.display = 'none';
                }
            }

            if (e.target.closest('.cancel-btn')) {
                const messageWrapper = e.target.closest('.bot-message');
                const textWrapper = messageWrapper.querySelector('.long-text-wrapper');
                const editorWrapper = messageWrapper.querySelector('.editor-wrapper');
                
                if (textWrapper && editorWrapper) {
                    textWrapper.style.display = 'block';
                    editorWrapper.style.display = 'none';
                }
            }

            if (e.target.closest('.regenerate-button')) {
                if (chatHistory.length > 0) {
                    const lastMessage = chatHistory[chatHistory.length - 1].human;
                    messageInput.value = lastMessage;
                    sendMessage();
                }
            }
        });

        sendButton.addEventListener('click', sendMessage);
        messageInput.addEventListener('keypress', (e) => {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                sendMessage();
            }
        });
        messageInput.addEventListener('input', adjustTextareaHeight);
        clearButton.addEventListener('click', () => {
            messagesContainer.innerHTML = '';
            chatHistory = [];
        });

        // Initial welcome message
        const initialMessageId = 'msg-initial';
        messagesContainer.insertAdjacentHTML('beforeend', createBotMessage('', initialMessageId));
        typeText(initialMessageId, 'مرحباً! كيف يمكنني مساعدتك اليوم؟');
    </script>
</body>
</html>