File size: 23,714 Bytes
797c3b0
 
 
 
cf7b440
2a51ba7
cf7b440
2a51ba7
ab53fb2
22bcaf5
797c3b0
cf7b440
 
 
2a51ba7
cf7b440
2a51ba7
 
 
 
 
cf7b440
 
 
2a51ba7
cf7b440
22bcaf5
cf7b440
2a51ba7
cf7b440
 
ab53fb2
2a51ba7
cf7b440
 
 
2a51ba7
cf7b440
 
22bcaf5
cf7b440
 
 
 
2a51ba7
 
 
 
 
 
 
 
 
22bcaf5
 
cf7b440
2a51ba7
22bcaf5
 
 
 
2a51ba7
cf7b440
 
22bcaf5
 
2a51ba7
22bcaf5
 
2a51ba7
 
c67c626
22bcaf5
2a51ba7
 
 
 
 
 
ab53fb2
30f0dfb
2a51ba7
 
 
 
 
 
 
 
 
 
 
22bcaf5
2a51ba7
 
 
 
 
 
 
8024816
22bcaf5
cf7b440
2a51ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf7b440
 
2a51ba7
cf7b440
 
 
2a51ba7
cf7b440
 
 
 
ab53fb2
2a51ba7
cf7b440
 
 
2a51ba7
 
 
 
cf7b440
 
2a51ba7
cf7b440
 
2a51ba7
 
22bcaf5
cf7b440
2a51ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf7b440
22bcaf5
 
2a51ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22bcaf5
cf7b440
2a51ba7
 
 
ab53fb2
22bcaf5
2a51ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22bcaf5
cf7b440
2a51ba7
cf7b440
 
 
 
 
2a51ba7
 
cf7b440
 
2a51ba7
 
 
 
 
 
cf7b440
 
 
2a51ba7
 
 
 
 
 
 
 
cf7b440
 
 
2a51ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab53fb2
788582a
2a51ba7
 
 
 
 
 
788582a
2a51ba7
 
 
 
 
 
 
 
797c3b0
 
 
cf7b440
 
2a51ba7
 
 
 
cf7b440
22bcaf5
2a51ba7
 
 
22bcaf5
2a51ba7
 
 
22bcaf5
 
 
2a51ba7
 
 
 
 
cf7b440
 
 
 
 
2a51ba7
cf7b440
2a51ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788582a
ab53fb2
cf7b440
c1b406f
cf7b440
 
2a51ba7
 
ab53fb2
 
797c3b0
cf7b440
797c3b0
8024816
2a51ba7
8024816
 
22bcaf5
 
 
 
 
2a51ba7
22bcaf5
2a51ba7
22bcaf5
2a51ba7
 
 
 
22bcaf5
8024816
22bcaf5
2a51ba7
 
 
 
 
8024816
797c3b0
cf7b440
c1b406f
8024816
c1b406f
797c3b0
30f0dfb
 
cf7b440
 
 
2a51ba7
 
22bcaf5
2a51ba7
22bcaf5
 
 
 
 
8024816
22bcaf5
2a51ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
788582a
2a51ba7
 
fe1cdda
2a51ba7
 
 
 
 
22bcaf5
2a51ba7
8024816
 
 
22bcaf5
2a51ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8024816
22bcaf5
2a51ba7
 
 
 
22bcaf5
2a51ba7
 
22bcaf5
 
 
2a51ba7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22bcaf5
c1b406f
2a51ba7
c1b406f
 
30f0dfb
c1b406f
2a51ba7
30f0dfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a51ba7
30f0dfb
2a51ba7
 
cf7b440
30f0dfb
cf7b440
c1b406f
2a51ba7
 
 
 
30f0dfb
aa3cbc4
c1b406f
797c3b0
cf7b440
2a51ba7
cf7b440
2a51ba7
 
 
788582a
2a51ba7
cf7b440
 
2a51ba7
 
 
 
 
 
30f0dfb
ab53fb2
 
cf7b440
8024816
cf7b440
797c3b0
aa3cbc4
797c3b0
 
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
<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kimi Reasoning Interface</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
    
    <style>
        :root {
            --bg: #0d1117;
            --sidebar: #161b22;
            --surface: #21262d;
            --border: #30363d;
            --text: #e6edf3;
            --text-secondary: #8b949e;
            --accent-think: #a371f7;  /* بنفسجي للتفكير */
            --accent-fast: #238636;   /* أخضر للسريع */
            --reasoning-bg: #1f242c; /* خلفية صندوق التفكير */
        }

        * { box-sizing: border-box; margin: 0; padding: 0; }
        
        body {
            background: var(--bg);
            color: var(--text);
            font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
            display: flex;
            height: 100vh;
            overflow: hidden;
            line-height: 1.6;
        }

        .sidebar {
            width: 300px;
            background: var(--sidebar);
            border-left: 1px solid var(--border);
            padding: 24px;
            display: flex;
            flex-direction: column;
        }

        .brand { 
            font-size: 20px; 
            font-weight: 700; 
            margin-bottom: 32px;
            display: flex;
            align-items: center;
            gap: 12px;
            color: white;
        }

        .mode-selector {
            display: flex;
            gap: 8px;
            background: var(--bg);
            padding: 4px;
            border-radius: 8px;
            border: 1px solid var(--border);
            margin-bottom: 24px;
        }

        .mode-btn {
            flex: 1;
            padding: 10px 14px;
            border: none;
            border-radius: 6px;
            background: transparent;
            color: var(--text-secondary);
            font-size: 13px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.2s;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 6px;
        }

        .mode-btn.active.think { 
            background: rgba(163, 113, 247, 0.15); 
            color: var(--accent-think);
            box-shadow: 0 0 0 1px rgba(163, 113, 247, 0.3);
        }
        
        .mode-btn.active.fast { 
            background: rgba(35, 134, 54, 0.15); 
            color: var(--accent-fast);
            box-shadow: 0 0 0 1px rgba(35, 134, 54, 0.3);
        }

        .info-card {
            background: rgba(163, 113, 247, 0.08);
            border: 1px solid rgba(163, 113, 247, 0.2);
            border-radius: 12px;
            padding: 16px;
            font-size: 13px;
            color: var(--text-secondary);
            line-height: 1.6;
        }

        .info-card h4 {
            color: var(--accent-think);
            margin-bottom: 8px;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .chat-container { 
            flex: 1; 
            display: flex; 
            flex-direction: column; 
            position: relative;
            background: var(--bg);
        }

        #chat-flow {
            flex: 1;
            padding: 40px 10% 140px;
            overflow-y: auto;
            display: flex;
            flex-direction: column;
            gap: 32px;
        }

        .message {
            display: flex;
            gap: 16px;
            max-width: 90%;
            animation: fadeIn 0.3s ease;
        }

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

        .message.user { align-self: flex-end; flex-direction: row-reverse; }
        .message.ai { align-self: flex-start; width: 100%; }

        .avatar {
            width: 36px; height: 36px; 
            border-radius: 8px;
            display: flex; align-items: center; justify-content: center;
            flex-shrink: 0;
            font-size: 16px;
        }
        
        .user .avatar { background: #1f6feb; color: white; }
        .ai.think .avatar { 
            background: var(--accent-think); 
            color: white;
            box-shadow: 0 0 0 2px rgba(163, 113, 247, 0.3);
        }
        .ai.fast .avatar { background: var(--accent-fast); color: white; }

        /* ✅ صندوق التفكير (Reasoning Box) */
        .reasoning-container {
            background: var(--reasoning-bg);
            border: 1px solid var(--border);
            border-radius: 12px;
            margin-bottom: 12px;
            overflow: hidden;
            font-family: 'JetBrains Mono', monospace;
            font-size: 13px;
        }

        .reasoning-header {
            padding: 12px 16px;
            background: rgba(163, 113, 247, 0.1);
            border-bottom: 1px solid var(--border);
            display: flex;
            align-items: center;
            justify-content: space-between;
            cursor: pointer;
            user-select: none;
            transition: background 0.2s;
        }

        .reasoning-header:hover {
            background: rgba(163, 113, 247, 0.15);
        }

        .reasoning-title {
            display: flex;
            align-items: center;
            gap: 10px;
            color: var(--accent-think);
            font-weight: 600;
            font-family: 'Inter', sans-serif;
        }

        .reasoning-icon {
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.5; }
        }

        .reasoning-toggle {
            color: var(--text-secondary);
            transition: transform 0.3s;
            font-size: 12px;
        }

        .reasoning-container.expanded .reasoning-toggle {
            transform: rotate(180deg);
        }

        .reasoning-content {
            padding: 16px;
            color: var(--text-secondary);
            line-height: 1.7;
            max-height: 0;
            overflow: hidden;
            transition: max-height 0.3s ease, padding 0.3s;
            white-space: pre-wrap;
            overflow-y: auto;
        }

        .reasoning-container.expanded .reasoning-content {
            max-height: 400px;
            padding: 16px;
        }

        .reasoning-content.streaming {
            max-height: 400px;
            padding: 16px;
        }

        /* ✅ فقاعة الجواب النهائي */
        .answer-bubble {
            background: var(--surface);
            border: 1px solid var(--border);
            border-radius: 12px;
            padding: 20px;
            line-height: 1.8;
            font-size: 15px;
            position: relative;
        }

        .answer-bubble::before {
            content: "الإجابة النهائية";
            position: absolute;
            top: -10px;
            right: 16px;
            background: var(--bg);
            padding: 0 8px;
            font-size: 11px;
            color: var(--accent-think);
            font-weight: 600;
            border: 1px solid var(--border);
            border-radius: 4px;
        }

        .bubble.user {
            background: var(--surface);
            border: 1px solid var(--border);
            padding: 14px 18px;
            border-radius: 12px;
        }

        /* Input */
        .input-wrapper {
            position: absolute;
            bottom: 30px;
            left: 50%;
            transform: translateX(-50%);
            width: 80%;
            max-width: 900px;
            background: var(--sidebar);
            border: 1px solid var(--border);
            border-radius: 16px;
            padding: 14px;
            display: flex;
            align-items: center;
            gap: 12px;
            box-shadow: 0 8px 32px rgba(0,0,0,0.4);
        }

        #userInput {
            flex: 1;
            background: transparent;
            border: none;
            color: white;
            padding: 10px;
            font-size: 16px;
            outline: none;
            font-family: inherit;
        }

        .send-btn {
            width: 36px; height: 36px;
            border-radius: 8px;
            border: none;
            cursor: pointer;
            color: white;
            transition: all 0.2s;
            display: flex; align-items: center; justify-content: center;
        }
        
        .send-btn.think { background: var(--accent-think); }
        .send-btn.fast { background: var(--accent-fast); }
        .send-btn:disabled { 
            background: #30363d !important; 
            cursor: not-allowed;
            opacity: 0.6;
        }

        .typing-cursor {
            display: inline-block;
            width: 2px;
            height: 18px;
            background: var(--accent-think);
            animation: blink 1s infinite;
            vertical-align: middle;
            margin-right: 4px;
        }

        @keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0; } }

        /* Markdown Styles */
        .answer-bubble pre {
            background: #161b22;
            padding: 16px;
            border-radius: 8px;
            overflow-x: auto;
            margin: 12px 0;
            border: 1px solid var(--border);
            font-family: 'JetBrains Mono', monospace;
            font-size: 13px;
        }
        
        .answer-bubble code {
            font-family: 'JetBrains Mono', monospace;
            background: rgba(255,255,255,0.1);
            padding: 2px 6px;
            border-radius: 4px;
            font-size: 13px;
        }
        
        .answer-bubble p { margin-bottom: 12px; }
        .answer-bubble p:last-child { margin-bottom: 0; }

        ::-webkit-scrollbar { width: 8px; }
        ::-webkit-scrollbar-track { background: var(--bg); }
        ::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
        ::-webkit-scrollbar-thumb:hover { background: #484f58; }
    </style>
</head>
<body>

    <aside class="sidebar">
        <div class="brand">
            <i class="fas fa-brain"></i>
            <span>Kimi Reasoning</span>
        </div>
        
        <div class="mode-selector">
            <button class="mode-btn think active" onclick="setMode('think')">
                <i class="fas fa-brain"></i>
                <span>وضيف التفكير</span>
            </button>
            <button class="mode-btn fast" onclick="setMode('fast')">
                <i class="fas fa-bolt"></i>
                <span>سريع</span>
            </button>
        </div>

        <div class="info-card">
            <h4><i class="fas fa-lightbulb"></i> مميزات وضيف التفكير</h4>
            • يعرض خطوات التفكير المنطقي<br>
            • يحل المسائل المعقدة خطوة بخطوة<br>
            • يُظهر صندوق "🧠 عرض التفكير" قابل للطي
        </div>
    </aside>

    <main class="chat-container">
        <div id="chat-flow">
            <div class="message ai think">
                <div class="avatar"><i class="fas fa-robot"></i></div>
                <div style="flex: 1;">
                    <!-- ✅ مثال على صندوق التفكير -->
                    <div class="reasoning-container expanded">
                        <div class="reasoning-header" onclick="toggleReasoning(this)">
                            <div class="reasoning-title">
                                <i class="fas fa-circle-notch reasoning-icon fa-spin"></i>
                                <span>جاري التفكير...</span>
                            </div>
                            <i class="fas fa-chevron-down reasoning-toggle"></i>
                        </div>
                        <div class="reasoning-content streaming">
                            انتظر رسالتك لأبدأ التفكير فيها خطوة بخطوة...
                        </div>
                    </div>
                    <div class="answer-bubble">
                        مرحباً! أنا Kimi K2.5 في وضيف التفكير. سأُظهر لك كيف أفكر قبل أن أُعطيك الإجابة النهائية.
                    </div>
                </div>
            </div>
        </div>

        <div class="input-wrapper">
            <input type="text" id="userInput" placeholder="اكتب سؤالاً يحتاج تفكيراً عميقاً..." autocomplete="off">
            <button class="send-btn think" id="sendBtn" onclick="handleSend()">
                <i class="fas fa-paper-plane"></i>
            </button>
        </div>
    </main>

    <script>
        let currentMode = 'think';

        function setMode(mode) {
            currentMode = mode;
            document.querySelectorAll('.mode-btn').forEach(btn => btn.classList.remove('active'));
            document.querySelector(`.mode-btn.${mode}`).classList.add('active');
            
            const btn = document.getElementById('sendBtn');
            const input = document.getElementById('userInput');
            
            if (mode === 'think') {
                btn.className = 'send-btn think';
                input.placeholder = "اكتب سؤالاً يحتاج تفكيراً عميقاً...";
            } else {
                btn.className = 'send-btn fast';
                input.placeholder = "اكتب رسالتك السريعة...";
            }
        }

        function toggleReasoning(header) {
            const container = header.parentElement;
            container.classList.toggle('expanded');
        }

        async function handleSend() {
            const input = document.getElementById('userInput');
            const btn = document.getElementById('sendBtn');
            const text = input.value.trim();
            
            if (!text) return;

            addMessage(text, 'user');
            input.value = '';
            input.disabled = true;
            btn.disabled = true;

            if (currentMode === 'think') {
                await sendWithReasoning(text);
            } else {
                await sendFast(text);
            }

            input.disabled = false;
            btn.disabled = false;
            input.focus();
        }

        // ✅ الوضيف المتقدم: عرض التفكير + الإجابة
        async function sendWithReasoning(text) {
            const msgId = 'msg-' + Date.now();
            const html = `
                <div class="message ai think" id="${msgId}">
                    <div class="avatar"><i class="fas fa-robot"></i></div>
                    <div style="flex: 1;">
                        <div class="reasoning-container expanded" id="reasoning-${msgId}">
                            <div class="reasoning-header" onclick="toggleReasoning(this)">
                                <div class="reasoning-title">
                                    <i class="fas fa-circle-notch reasoning-icon fa-spin" id="icon-${msgId}"></i>
                                    <span id="title-${msgId}">جاري التفكير...</span>
                                </div>
                                <i class="fas fa-chevron-down reasoning-toggle"></i>
                            </div>
                            <div class="reasoning-content streaming" id="reasoning-content-${msgId}">
                                <span class="typing-cursor"></span>
                            </div>
                        </div>
                        <div class="answer-bubble" id="answer-${msgId}" style="display: none;">
                            <span class="typing-cursor"></span>
                        </div>
                    </div>
                </div>
            `;
            
            document.getElementById('chat-flow').insertAdjacentHTML('beforeend', html);
            scrollToBottom();

            const reasoningContent = document.getElementById(`reasoning-content-${msgId}`);
            const answerBubble = document.getElementById(`answer-${msgId}`);
            const titleText = document.getElementById(`title-${msgId}`);
            const icon = document.getElementById(`icon-${msgId}`);
            
            try {
                const res = await fetch('/api/think', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ message: text })
                });

                const reader = res.body.getReader();
                const decoder = new TextDecoder();
                let reasoningText = "";
                let answerText = "";
                let isAnswering = false;

                while (true) {
                    const { done, value } = await reader.read();
                    if (done) break;

                    const chunk = decoder.decode(value);
                    const lines = chunk.split('\n');

                    for (const line of lines) {
                        if (!line.startsWith('data: ')) continue;
                        const data = line.replace('data: ', '').trim();
                        if (data === '[DONE]') break;
                        
                        try {
                            const json = JSON.parse(data);
                            
                            if (json.type === 'reasoning') {
                                // ✅ عرض نص التفكير
                                reasoningText += json.content;
                                reasoningContent.innerHTML = reasoningText + '<span class="typing-cursor"></span>';
                                scrollToBottom();
                            } 
                            else if (json.type === 'content') {
                                // ✅ الانتقال للإجابة النهائية
                                if (!isAnswering) {
                                    isAnswering = true;
                                    titleText.textContent = "اكتمل التفكير";
                                    icon.className = "fas fa-check reasoning-icon";
                                    icon.style.color = "var(--accent-think)";
                                    
                                    // إخفاء صندوق التفكير تلقائياً بعد ثانية (اختياري)
                                    setTimeout(() => {
                                        document.getElementById(`reasoning-${msgId}`).classList.remove('expanded');
                                    }, 1000);
                                    
                                    answerBubble.style.display = 'block';
                                }
                                
                                answerText += json.content;
                                answerBubble.innerHTML = marked.parse(answerText) + '<span class="typing-cursor"></span>';
                                scrollToBottom();
                            }
                            else if (json.type === 'error') {
                                answerBubble.style.display = 'block';
                                answerBubble.innerHTML = `<span style="color:#f85149">${json.error}</span>`;
                            }
                        } catch (e) {}
                    }
                }
                
                // إزالة المؤشر النهائي
                const cursor = answerBubble.querySelector('.typing-cursor');
                if (cursor) cursor.remove();
                
            } catch (err) {
                answerBubble.style.display = 'block';
                answerBubble.innerHTML = `<span style="color:#f85149">فشل الاتصال: ${err.message}</span>`;
            }
        }

        // ⚡ الوضع السريع (بدون تفكير مرئي)
        async function sendFast(text) {
            const msgId = 'msg-' + Date.now();
            const html = `
                <div class="message ai fast" id="${msgId}">
                    <div class="avatar"><i class="fas fa-robot"></i></div>
                    <div style="flex: 1;">
                        <div class="answer-bubble" id="answer-${msgId}">
                            <span class="typing-cursor"></span>
                        </div>
                    </div>
                </div>
            `;
            
            document.getElementById('chat-flow').insertAdjacentHTML('beforeend', html);
            const bubble = document.getElementById(`answer-${msgId}`);
            
            try {
                const res = await fetch('/api/fast', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ message: text })
                });
                
                const reader = res.body.getReader();
                const decoder = new TextDecoder();
                let fullText = "";

                while (true) {
                    const { done, value } = await reader.read();
                    if (done) break;

                    const chunk = decoder.decode(value);
                    const lines = chunk.split('\n');

                    for (const line of lines) {
                        if (!line.startsWith('data: ')) continue;
                        const data = line.replace('data: ', '').trim();
                        if (data === '[DONE]') break;
                        
                        try {
                            const json = JSON.parse(data);
                            if (json.type === 'content') {
                                fullText += json.content;
                                bubble.innerHTML = marked.parse(fullText) + '<span class="typing-cursor"></span>';
                                scrollToBottom();
                            }
                        } catch (e) {}
                    }
                }
                
                const cursor = bubble.querySelector('.typing-cursor');
                if (cursor) cursor.remove();
                
            } catch (err) {
                bubble.innerHTML = `<span style="color:#f85149">خطأ: ${err.message}</span>`;
            }
        }

        function addMessage(text, role) {
            const html = `
                <div class="message ${role}">
                    <div class="avatar">
                        <i class="fas ${role === 'user' ? 'fa-user' : 'fa-robot'}"></i>
                    </div>
                    <div class="bubble ${role}">${text}</div>
                </div>
            `;
            document.getElementById('chat-flow').insertAdjacentHTML('beforeend', html);
            scrollToBottom();
        }

        function scrollToBottom() {
            const flow = document.getElementById('chat-flow');
            flow.scrollTop = flow.scrollHeight;
        }

        document.getElementById('userInput').addEventListener('keypress', (e) => {
            if (e.key === 'Enter') handleSend();
        });
    </script>

</body>
</html>