Update static/script.js
Browse files- static/script.js +131 -182
static/script.js
CHANGED
|
@@ -1,236 +1,185 @@
|
|
| 1 |
-
|
| 2 |
-
let currentModel = 'gpt5';
|
| 3 |
-
let messages = [];
|
| 4 |
-
let isLoading = false;
|
| 5 |
|
| 6 |
// عناصر DOM
|
| 7 |
-
const
|
| 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 |
-
const modelDiv = document.createElement('div');
|
| 33 |
-
modelDiv.className = `model-card ${model.id === currentModel ? 'active' : ''}`;
|
| 34 |
-
modelDiv.innerHTML = `
|
| 35 |
-
<div>
|
| 36 |
-
<span class="model-icon">${model.icon}</span>
|
| 37 |
-
<span class="model-name">${model.name}</span>
|
| 38 |
-
</div>
|
| 39 |
-
<div class="model-desc">نموذج ${model.name === 'GPT-5 Mini' ? 'متقدم' : 'سريع'}</div>
|
| 40 |
-
`;
|
| 41 |
-
modelDiv.onclick = () => switchModel(model.id, model.icon, model.name);
|
| 42 |
-
modelsList.appendChild(modelDiv);
|
| 43 |
-
});
|
| 44 |
-
} catch (error) {
|
| 45 |
-
console.error('خطأ في تحميل النماذج:', error);
|
| 46 |
-
}
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
// تبديل النموذج
|
| 50 |
-
function switchModel(modelId, icon, name) {
|
| 51 |
-
currentModel = modelId;
|
| 52 |
-
|
| 53 |
-
// تحديث الواجهة
|
| 54 |
-
document.querySelectorAll('.model-card').forEach(card => {
|
| 55 |
-
card.classList.remove('active');
|
| 56 |
-
});
|
| 57 |
-
event.target.closest('.model-card').classList.add('active');
|
| 58 |
-
|
| 59 |
-
// تحديث شريط الحالة
|
| 60 |
-
elements.modelStatus.innerHTML = `
|
| 61 |
-
<span class="model-icon">${icon}</span>
|
| 62 |
-
<span class="model-name">${name}</span>
|
| 63 |
-
`;
|
| 64 |
-
|
| 65 |
-
// إشعار قصير
|
| 66 |
-
showNotification(`تم التبديل إلى ${name}`);
|
| 67 |
-
}
|
| 68 |
|
| 69 |
// إرسال رسالة
|
| 70 |
async function sendMessage() {
|
| 71 |
-
const message =
|
| 72 |
-
if (!message
|
| 73 |
-
|
| 74 |
-
// إخفاء رسالة الترحيب
|
| 75 |
-
if (elements.welcomeMessage) {
|
| 76 |
-
elements.welcomeMessage.style.display = 'none';
|
| 77 |
-
}
|
| 78 |
|
| 79 |
// إضافة رسالة المستخدم
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
// مسح حقل الإدخال
|
| 84 |
-
elements.messageInput.value = '';
|
| 85 |
-
updateCharCount();
|
| 86 |
-
autoResizeTextarea();
|
| 87 |
|
| 88 |
-
// إظهار مؤشر الت
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
try {
|
| 93 |
const response = await fetch('/api/chat', {
|
| 94 |
method: 'POST',
|
| 95 |
-
headers: {
|
| 96 |
-
'Content-Type': 'application/json'
|
| 97 |
-
},
|
| 98 |
body: JSON.stringify({
|
| 99 |
-
|
| 100 |
-
model:
|
|
|
|
|
|
|
|
|
|
| 101 |
})
|
| 102 |
});
|
| 103 |
|
| 104 |
-
if (!response.ok) {
|
| 105 |
-
throw new Error(`HTTP ${response.status}`);
|
| 106 |
-
}
|
| 107 |
-
|
| 108 |
const data = await response.json();
|
| 109 |
|
| 110 |
-
// إ
|
| 111 |
-
|
| 112 |
-
messages.push({ role: 'assistant', content: data.message });
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
} catch (error) {
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
} finally {
|
| 118 |
-
isLoading = false;
|
| 119 |
-
elements.loadingOverlay.classList.remove('show');
|
| 120 |
}
|
|
|
|
|
|
|
| 121 |
}
|
| 122 |
|
| 123 |
-
// إضافة رسالة
|
| 124 |
-
function
|
| 125 |
const messageDiv = document.createElement('div');
|
| 126 |
-
messageDiv.className = `message ${
|
| 127 |
-
|
| 128 |
-
const avatar = sender === 'user' ? '👤' :
|
| 129 |
-
currentModel === 'gpt5' ? '🤖' : '⚡';
|
| 130 |
|
| 131 |
-
const
|
|
|
|
| 132 |
|
| 133 |
messageDiv.innerHTML = `
|
| 134 |
-
<div class="message-avatar">${
|
| 135 |
<div class="message-content">
|
| 136 |
-
${escapeHtml(content)}
|
| 137 |
-
<
|
| 138 |
</div>
|
| 139 |
`;
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
// مسح المحادثة
|
| 146 |
-
function clearChat() {
|
| 147 |
-
if (messages.length > 0 && confirm('هل تريد مسح كل الرسائل؟')) {
|
| 148 |
-
messages = [];
|
| 149 |
-
elements.messagesArea.innerHTML = '';
|
| 150 |
-
elements.welcomeMessage.style.display = 'flex';
|
| 151 |
-
showNotification('تم مسح المحادثة');
|
| 152 |
-
}
|
| 153 |
-
}
|
| 154 |
-
|
| 155 |
-
// محادثة جديدة
|
| 156 |
-
function newChat() {
|
| 157 |
-
if (messages.length > 0 && confirm('بدء محادثة جديدة؟ سيتم مسح الرسائل الحالية')) {
|
| 158 |
-
messages = [];
|
| 159 |
-
elements.messagesArea.innerHTML = '';
|
| 160 |
-
elements.welcomeMessage.style.display = 'flex';
|
| 161 |
-
showNotification('محادثة جديدة');
|
| 162 |
}
|
|
|
|
|
|
|
|
|
|
| 163 |
}
|
| 164 |
|
| 165 |
-
//
|
| 166 |
function escapeHtml(text) {
|
| 167 |
const div = document.createElement('div');
|
| 168 |
div.textContent = text;
|
| 169 |
return div.innerHTML;
|
| 170 |
}
|
| 171 |
|
|
|
|
| 172 |
function scrollToBottom() {
|
| 173 |
-
|
| 174 |
-
container.scrollTop = container.scrollHeight;
|
| 175 |
-
}
|
| 176 |
-
|
| 177 |
-
function autoResizeTextarea() {
|
| 178 |
-
const textarea = elements.messageInput;
|
| 179 |
-
textarea.style.height = 'auto';
|
| 180 |
-
textarea.style.height = Math.min(textarea.scrollHeight, 120) + 'px';
|
| 181 |
}
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
}
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
}
|
| 192 |
|
| 193 |
-
|
|
|
|
| 194 |
if (e.key === 'Enter' && !e.shiftKey) {
|
| 195 |
e.preventDefault();
|
| 196 |
sendMessage();
|
| 197 |
}
|
| 198 |
-
}
|
| 199 |
-
|
| 200 |
-
// تب
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
-
//
|
| 216 |
-
function
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
}
|
| 229 |
-
elements.messageInput.addEventListener('keypress', handleKeyPress);
|
| 230 |
-
|
| 231 |
-
setupSuggestions();
|
| 232 |
-
updateCharCount();
|
| 233 |
}
|
| 234 |
|
| 235 |
-
|
| 236 |
-
|
|
|
|
| 1 |
+
let currentConversationId = 'default';
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
// عناصر DOM
|
| 4 |
+
const messagesContainer = document.getElementById('messages');
|
| 5 |
+
const messageInput = document.getElementById('messageInput');
|
| 6 |
+
const sendBtn = document.getElementById('sendBtn');
|
| 7 |
+
const clearBtn = document.getElementById('clearChat');
|
| 8 |
+
const newChatBtn = document.getElementById('newChat');
|
| 9 |
+
const modelSelect = document.getElementById('modelSelect');
|
| 10 |
+
const temperature = document.getElementById('temperature');
|
| 11 |
+
const maxTokens = document.getElementById('maxTokens');
|
| 12 |
+
const tempValue = document.getElementById('tempValue');
|
| 13 |
+
const tokensValue = document.getElementById('tokensValue');
|
| 14 |
+
const typingIndicator = document.getElementById('typingIndicator');
|
| 15 |
+
const modelBadge = document.getElementById('modelBadge');
|
| 16 |
+
|
| 17 |
+
// تحديث القيم المعروضة
|
| 18 |
+
temperature.addEventListener('input', () => {
|
| 19 |
+
tempValue.textContent = temperature.value;
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
maxTokens.addEventListener('input', () => {
|
| 23 |
+
tokensValue.textContent = maxTokens.value;
|
| 24 |
+
});
|
| 25 |
+
|
| 26 |
+
modelSelect.addEventListener('change', () => {
|
| 27 |
+
modelBadge.textContent = modelSelect.options[modelSelect.selectedIndex].text;
|
| 28 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
// إرسال رسالة
|
| 31 |
async function sendMessage() {
|
| 32 |
+
const message = messageInput.value.trim();
|
| 33 |
+
if (!message) return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
// إضافة رسالة المستخدم
|
| 36 |
+
addMessageToUI('user', message);
|
| 37 |
+
messageInput.value = '';
|
| 38 |
+
messageInput.style.height = 'auto';
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
// إظهار مؤشر الكتابة
|
| 41 |
+
typingIndicator.style.display = 'flex';
|
| 42 |
+
scrollToBottom();
|
| 43 |
|
| 44 |
try {
|
| 45 |
const response = await fetch('/api/chat', {
|
| 46 |
method: 'POST',
|
| 47 |
+
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
|
|
| 48 |
body: JSON.stringify({
|
| 49 |
+
message: message,
|
| 50 |
+
model: modelSelect.value,
|
| 51 |
+
conversation_id: currentConversationId,
|
| 52 |
+
temperature: parseFloat(temperature.value),
|
| 53 |
+
max_tokens: parseInt(maxTokens.value)
|
| 54 |
})
|
| 55 |
});
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
const data = await response.json();
|
| 58 |
|
| 59 |
+
// إخفاء مؤشر الكتابة
|
| 60 |
+
typingIndicator.style.display = 'none';
|
|
|
|
| 61 |
|
| 62 |
+
if (response.ok) {
|
| 63 |
+
addMessageToUI('assistant', data.reply);
|
| 64 |
+
} else {
|
| 65 |
+
addMessageToUI('assistant', `❌ خطأ: ${data.error}`);
|
| 66 |
+
}
|
| 67 |
} catch (error) {
|
| 68 |
+
typingIndicator.style.display = 'none';
|
| 69 |
+
addMessageToUI('assistant', `❌ خطأ في الاتصال: ${error.message}`);
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
+
|
| 72 |
+
scrollToBottom();
|
| 73 |
}
|
| 74 |
|
| 75 |
+
// إضافة رسالة للواجهة
|
| 76 |
+
function addMessageToUI(role, content) {
|
| 77 |
const messageDiv = document.createElement('div');
|
| 78 |
+
messageDiv.className = `message ${role}`;
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
const now = new Date();
|
| 81 |
+
const timestamp = now.toLocaleTimeString('ar-EG', { hour: '2-digit', minute: '2-digit' });
|
| 82 |
|
| 83 |
messageDiv.innerHTML = `
|
| 84 |
+
<div class="message-avatar">${role === 'user' ? '👤' : '🤖'}</div>
|
| 85 |
<div class="message-content">
|
| 86 |
+
${escapeHtml(content).replace(/\n/g, '<br>')}
|
| 87 |
+
<span class="message-timestamp">${timestamp}</span>
|
| 88 |
</div>
|
| 89 |
`;
|
| 90 |
|
| 91 |
+
// إزالة رسالة الترحيب إذا وجدت
|
| 92 |
+
const welcomeMsg = messagesContainer.querySelector('.welcome-message');
|
| 93 |
+
if (welcomeMsg && messagesContainer.children.length === 1) {
|
| 94 |
+
messagesContainer.innerHTML = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
}
|
| 96 |
+
|
| 97 |
+
messagesContainer.appendChild(messageDiv);
|
| 98 |
+
scrollToBottom();
|
| 99 |
}
|
| 100 |
|
| 101 |
+
// منع XSS
|
| 102 |
function escapeHtml(text) {
|
| 103 |
const div = document.createElement('div');
|
| 104 |
div.textContent = text;
|
| 105 |
return div.innerHTML;
|
| 106 |
}
|
| 107 |
|
| 108 |
+
// التمرير للأسفل
|
| 109 |
function scrollToBottom() {
|
| 110 |
+
messagesContainer.scrollTop = messagesContainer.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
}
|
| 112 |
|
| 113 |
+
// مسح المحادثة
|
| 114 |
+
async function clearConversation() {
|
| 115 |
+
if (confirm('هل أنت متأكد من مسح المحادثة؟')) {
|
| 116 |
+
await fetch(`/api/conversations/${currentConversationId}`, {
|
| 117 |
+
method: 'DELETE'
|
| 118 |
+
});
|
| 119 |
+
messagesContainer.innerHTML = `
|
| 120 |
+
<div class="welcome-message">
|
| 121 |
+
<div class="welcome-icon">✨</div>
|
| 122 |
+
<h3>تم مسح المحادثة!</h3>
|
| 123 |
+
<p>يمكنك البدء من جديد</p>
|
| 124 |
+
</div>
|
| 125 |
+
`;
|
| 126 |
+
}
|
| 127 |
}
|
| 128 |
|
| 129 |
+
// محادثة جديدة
|
| 130 |
+
function newConversation() {
|
| 131 |
+
currentConversationId = `conv_${Date.now()}`;
|
| 132 |
+
messagesContainer.innerHTML = `
|
| 133 |
+
<div class="welcome-message">
|
| 134 |
+
<div class="welcome-icon">✨</div>
|
| 135 |
+
<h3>محادثة جديدة!</h3>
|
| 136 |
+
<p>كيف يمكنني مساعدتك اليوم؟</p>
|
| 137 |
+
</div>
|
| 138 |
+
`;
|
| 139 |
}
|
| 140 |
|
| 141 |
+
// إرسال بالضغط على Enter (Shift+Enter لسطر جديد)
|
| 142 |
+
messageInput.addEventListener('keypress', (e) => {
|
| 143 |
if (e.key === 'Enter' && !e.shiftKey) {
|
| 144 |
e.preventDefault();
|
| 145 |
sendMessage();
|
| 146 |
}
|
| 147 |
+
});
|
| 148 |
+
|
| 149 |
+
// تكبير تلقائي لحقل النص
|
| 150 |
+
messageInput.addEventListener('input', function() {
|
| 151 |
+
this.style.height = 'auto';
|
| 152 |
+
this.style.height = Math.min(this.scrollHeight, 120) + 'px';
|
| 153 |
+
});
|
| 154 |
+
|
| 155 |
+
// أحداث الأزرار
|
| 156 |
+
sendBtn.addEventListener('click', sendMessage);
|
| 157 |
+
clearBtn.addEventListener('click', clearConversation);
|
| 158 |
+
newChatBtn.addEventListener('click', newConversation);
|
| 159 |
+
|
| 160 |
+
// الاقتراحات
|
| 161 |
+
document.addEventListener('click', (e) => {
|
| 162 |
+
if (e.target.classList.contains('suggestion')) {
|
| 163 |
+
messageInput.value = e.target.getAttribute('data-msg');
|
| 164 |
+
sendMessage();
|
| 165 |
+
}
|
| 166 |
+
});
|
| 167 |
|
| 168 |
+
// فحص صحة الخدمة
|
| 169 |
+
async function checkHealth() {
|
| 170 |
+
try {
|
| 171 |
+
const response = await fetch('/api/health');
|
| 172 |
+
const data = await response.json();
|
| 173 |
+
const statusDot = document.querySelector('.status-dot');
|
| 174 |
+
if (data.client_ready) {
|
| 175 |
+
statusDot.style.background = '#10b981';
|
| 176 |
+
} else {
|
| 177 |
+
statusDot.style.background = '#ef4444';
|
| 178 |
+
}
|
| 179 |
+
} catch (error) {
|
| 180 |
+
console.error('خطأ في فحص الصحة:', error);
|
| 181 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
}
|
| 183 |
|
| 184 |
+
setInterval(checkHealth, 30000);
|
| 185 |
+
checkHealth();
|