File size: 7,300 Bytes
776083f
e1e62ea
4afaf7c
e1e62ea
 
 
 
 
776083f
 
4afaf7c
776083f
e1e62ea
4afaf7c
 
 
 
 
 
 
e1e62ea
 
 
 
776083f
e1e62ea
 
776083f
 
e1e62ea
 
776083f
 
4afaf7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1e62ea
 
 
776083f
e1e62ea
 
 
 
 
 
 
 
 
776083f
 
 
4afaf7c
e1e62ea
 
 
 
 
 
 
 
776083f
e1e62ea
 
 
776083f
4afaf7c
e1e62ea
 
4afaf7c
 
 
e1e62ea
 
 
 
 
 
 
 
776083f
e1e62ea
 
 
 
 
 
776083f
 
4afaf7c
e1e62ea
776083f
 
4afaf7c
e1e62ea
 
 
4afaf7c
e1e62ea
 
 
 
 
 
 
 
 
 
 
4afaf7c
e1e62ea
 
 
 
776083f
4afaf7c
 
 
 
 
e1e62ea
 
 
 
 
 
 
4afaf7c
 
 
e1e62ea
776083f
e1e62ea
 
 
4afaf7c
e1e62ea
 
 
4afaf7c
776083f
 
e1e62ea
 
 
 
 
776083f
 
4afaf7c
 
 
 
 
e1e62ea
 
 
 
 
 
 
 
 
 
 
 
 
 
776083f
e1e62ea
776083f
e1e62ea
 
 
4afaf7c
e1e62ea
 
 
 
776083f
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
const API_URL = '/api';
let currentMode = 'flash';
let enableSearch = false;

// عناصر DOM
const chatMessages = document.getElementById('chatMessages');
const userInput = document.getElementById('userInput');
const sendBtn = document.getElementById('sendBtn');
const flashBtn = document.getElementById('flashBtn');
const proBtn = document.getElementById('proBtn');
const searchToggle = document.getElementById('searchToggle');
const statusBar = document.getElementById('status');

function escapeHtml(text) {
    const div = document.createElement('div');
    div.textContent = text;
    return div.innerHTML;
}

function addMessage(text, isUser = false, parsedResponse = null, searchUsed = false) {
    const messageDiv = document.createElement('div');
    messageDiv.className = `message ${isUser ? 'user' : 'bot'}`;
    
    const avatar = document.createElement('div');
    avatar.className = 'avatar';
    avatar.textContent = isUser ? '👤' : '🤖';
    
    const content = document.createElement('div');
    content.className = 'content';
    
    if (isUser) {
        content.textContent = text;
    } else {
        if (searchUsed) {
            const searchBadge = document.createElement('div');
            searchBadge.className = 'search-indicator';
            searchBadge.innerHTML = '🔍 تم البحث في الإنترنت';
            content.appendChild(searchBadge);
        }
        
        if (parsedResponse && Array.isArray(parsedResponse)) {
            parsedResponse.forEach(part => {
                if (part.type === 'thought') {
                    const thoughtDiv = document.createElement('div');
                    thoughtDiv.className = 'thought-container';
                    thoughtDiv.innerHTML = `
                        <div class="thought-header">
                            <span>🧠</span>
                            <span>تفكير النموذج (genisi thought)</span>
                        </div>
                        <div>${escapeHtml(part.content)}</div>
                    `;
                    content.appendChild(thoughtDiv);
                } else if (part.type === 'text' && part.content) {
                    const textDiv = document.createElement('div');
                    textDiv.textContent = part.content;
                    content.appendChild(textDiv);
                }
            });
        } else {
            content.textContent = text;
        }
    }
    
    messageDiv.appendChild(avatar);
    messageDiv.appendChild(content);
    chatMessages.appendChild(messageDiv);
    chatMessages.scrollTop = chatMessages.scrollHeight;
}

function showLoading() {
    const loadingDiv = document.createElement('div');
    loadingDiv.className = 'message bot';
    loadingDiv.id = 'loadingMessage';
    loadingDiv.innerHTML = `
        <div class="avatar">🤖</div>
        <div class="content">
            <div class="loading"></div>
            <div style="margin-top: 5px;">جاري المعالجة...</div>
        </div>
    `;
    chatMessages.appendChild(loadingDiv);
    chatMessages.scrollTop = chatMessages.scrollHeight;
}

function hideLoading() {
    const loading = document.getElementById('loadingMessage');
    if (loading) loading.remove();
}

function updateStatus(text, isError = false) {
    statusBar.textContent = text;
    statusBar.style.color = isError ? '#dc3545' : '#666';
    setTimeout(() => {
        if (!isError) {
            const searchText = enableSearch ? 'مفعل' : 'غير مفعل';
            statusBar.textContent = `✅ جاهز | الوضع: ${currentMode === 'flash' ? 'فلاش' : 'برو'} | البحث: ${searchText}`;
            statusBar.style.color = '#666';
        }
    }, 3000);
}

async function setMode(mode) {
    try {
        const response = await fetch(`${API_URL}/mode`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ mode })
        });
        
        if (response.ok) {
            currentMode = mode;
            if (mode === 'flash') {
                flashBtn.classList.add('active');
                proBtn.classList.remove('active');
                updateStatus('✅ تم التبديل إلى وضع الفلاش - ردود سريعة بدون تفكير');
            } else {
                proBtn.classList.add('active');
                flashBtn.classList.remove('active');
                updateStatus('✅ تم التبديل إلى وضع البرو - تفكير عميق مع genisi thought');
            }
        }
    } catch (error) {
        console.error('Error:', error);
        updateStatus('❌ فشل تغيير الوضع', true);
    }
}

async function sendMessage() {
    const message = userInput.value.trim();
    if (!message) return;
    
    addMessage(message, true);
    userInput.value = '';
    showLoading();
    updateStatus('🤔 جاري المعالجة...');
    
    try {
        const response = await fetch(`${API_URL}/chat`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ 
                message, 
                mode: currentMode,
                enableSearch: enableSearch
            })
        });
        
        hideLoading();
        
        if (response.ok) {
            const data = await response.json();
            if (data.success) {
                addMessage('', false, data.response, data.searchUsed);
                const searchMsg = data.searchUsed ? ` (تم البحث - ${data.searchResultsCount} نتيجة)` : '';
                updateStatus(`✅ تم الرد${searchMsg}`);
            } else {
                addMessage('عذراً، حدث خطأ. حاول مرة أخرى.', false);
                updateStatus('❌ خطأ في الاستجابة', true);
            }
        } else {
            throw new Error('Connection error');
        }
    } catch (error) {
        hideLoading();
        console.error('Error:', error);
        addMessage('عذراً، لا يمكن الاتصال بالخادم.', false);
        updateStatus('❌ فشل الاتصال', true);
    }
}

// أحداث
sendBtn.addEventListener('click', sendMessage);
flashBtn.addEventListener('click', () => setMode('flash'));
proBtn.addEventListener('click', () => setMode('pro'));
searchToggle.addEventListener('change', (e) => {
    enableSearch = e.target.checked;
    updateStatus(enableSearch ? '🔍 تم تفعيل البحث في الإنترنت' : '🔍 تم إيقاف البحث في الإنترنت');
});

userInput.addEventListener('keypress', (e) => {
    if (e.key === 'Enter' && !e.shiftKey) {
        e.preventDefault();
        sendMessage();
    }
});

async function loadCurrentMode() {
    try {
        const response = await fetch(`${API_URL}/mode`);
        if (response.ok) {
            const data = await response.json();
            currentMode = data.mode;
            if (currentMode === 'flash') {
                flashBtn.classList.add('active');
            } else {
                proBtn.classList.add('active');
            }
        }
    } catch (error) {
        console.error('Error:', error);
    }
}

loadCurrentMode();
updateStatus('✅ جاهز للحديث!');