AnesKAM commited on
Commit
776083f
·
verified ·
1 Parent(s): 4e8cd1e

Update public/script.js

Browse files
Files changed (1) hide show
  1. public/script.js +68 -120
public/script.js CHANGED
@@ -1,113 +1,93 @@
1
- const API_URL = 'http://localhost:7680/api';
2
-
3
  let currentMode = 'flash';
4
 
5
  // عناصر DOM
6
  const chatMessages = document.getElementById('chatMessages');
7
  const userInput = document.getElementById('userInput');
8
  const sendBtn = document.getElementById('sendBtn');
9
- const flashModeBtn = document.getElementById('flashModeBtn');
10
- const proModeBtn = document.getElementById('proModeBtn');
11
- const currentModeDisplay = document.getElementById('currentModeDisplay');
12
- const clearChatBtn = document.getElementById('clearChatBtn');
13
- const statusDiv = document.getElementById('status');
14
 
15
- // إضافة رسالة للشات
16
  function addMessage(text, isUser = false, parsedResponse = null) {
17
  const messageDiv = document.createElement('div');
18
  messageDiv.className = `message ${isUser ? 'user' : 'bot'}`;
19
 
20
  const avatar = document.createElement('div');
21
- avatar.className = 'message-avatar';
22
  avatar.textContent = isUser ? '👤' : '🤖';
23
 
24
- const contentDiv = document.createElement('div');
25
- contentDiv.className = 'message-content';
26
 
27
  if (isUser) {
28
- const textDiv = document.createElement('div');
29
- textDiv.className = 'message-text';
30
- textDiv.textContent = text;
31
- contentDiv.appendChild(textDiv);
32
- } else {
33
- if (parsedResponse && Array.isArray(parsedResponse)) {
34
- parsedResponse.forEach(part => {
35
- if (part.type === 'thought') {
36
- // إضافة حاوية التفكير
37
- const thoughtDiv = document.createElement('div');
38
- thoughtDiv.className = 'thought-container';
39
-
40
- const thoughtHeader = document.createElement('div');
41
- thoughtHeader.className = 'thought-header';
42
- thoughtHeader.innerHTML = `
43
- <span class="thought-icon">🧠</span>
44
  <span>تفكير النموذج (genisi thought)</span>
45
- `;
46
-
47
- const thoughtContent = document.createElement('div');
48
- thoughtContent.className = 'thought-content';
49
- thoughtContent.textContent = part.content;
50
-
51
- thoughtDiv.appendChild(thoughtHeader);
52
- thoughtDiv.appendChild(thoughtContent);
53
- contentDiv.appendChild(thoughtDiv);
54
- } else if (part.type === 'text' && part.content) {
55
- const textDiv = document.createElement('div');
56
- textDiv.className = 'regular-content';
57
- textDiv.textContent = part.content;
58
- contentDiv.appendChild(textDiv);
59
- }
60
- });
61
- } else {
62
- // إذا لم يكن هناك تحليل، عرض النص العادي
63
- const textDiv = document.createElement('div');
64
- textDiv.className = 'regular-content';
65
- textDiv.textContent = text;
66
- contentDiv.appendChild(textDiv);
67
- }
68
  }
69
 
70
  messageDiv.appendChild(avatar);
71
- messageDiv.appendChild(contentDiv);
72
  chatMessages.appendChild(messageDiv);
73
-
74
- // التمرير للأسفل
75
  chatMessages.scrollTop = chatMessages.scrollHeight;
76
  }
77
 
 
 
 
 
 
 
 
78
  // عرض حالة التحميل
79
  function showLoading() {
80
  const loadingDiv = document.createElement('div');
81
  loadingDiv.className = 'message bot';
82
  loadingDiv.id = 'loadingMessage';
83
  loadingDiv.innerHTML = `
84
- <div class="message-avatar">🤖</div>
85
- <div class="message-content">
86
- <div class="message-text">
87
- <span class="loading-dots">جاري التفكير</span>
88
- </div>
89
  </div>
90
  `;
91
  chatMessages.appendChild(loadingDiv);
92
  chatMessages.scrollTop = chatMessages.scrollHeight;
93
  }
94
 
95
- // إخفاء حالة التحميل
96
  function hideLoading() {
97
  const loading = document.getElementById('loadingMessage');
98
- if (loading) {
99
- loading.remove();
100
- }
101
  }
102
 
103
- // تحديث حالة الوضع
104
  function updateStatus(text, isError = false) {
105
- statusDiv.textContent = text;
106
- statusDiv.style.color = isError ? '#dc3545' : '#6c757d';
107
  setTimeout(() => {
108
  if (!isError) {
109
- statusDiv.textContent = 'جاهز للحديث';
110
- statusDiv.style.color = '#6c757d';
111
  }
112
  }, 3000);
113
  }
@@ -117,29 +97,24 @@ async function setMode(mode) {
117
  try {
118
  const response = await fetch(`${API_URL}/mode`, {
119
  method: 'POST',
120
- headers: {
121
- 'Content-Type': 'application/json',
122
- },
123
  body: JSON.stringify({ mode })
124
  });
125
 
126
  if (response.ok) {
127
  currentMode = mode;
128
- currentModeDisplay.textContent = mode === 'flash' ? 'فلاش' : 'برو';
129
-
130
- // تحديث مظهر الأزرار
131
  if (mode === 'flash') {
132
- flashModeBtn.classList.add('active');
133
- proModeBtn.classList.remove('active');
134
  updateStatus('✅ تم التبديل إلى وضع الفلاش - ردود سريعة');
135
  } else {
136
- proModeBtn.classList.add('active');
137
- flashModeBtn.classList.remove('active');
138
- updateStatus('✅ تم التبديل إلى وضع البرو - تحليل عميق');
139
  }
140
  }
141
  } catch (error) {
142
- console.error('خطأ في تغيير الوضع:', error);
143
  updateStatus('❌ فشل تغيير الوضع', true);
144
  }
145
  }
@@ -149,24 +124,16 @@ async function sendMessage() {
149
  const message = userInput.value.trim();
150
  if (!message) return;
151
 
152
- // إضافة رسالة المستخدم
153
  addMessage(message, true);
154
  userInput.value = '';
155
-
156
- // عرض حالة التحميل
157
  showLoading();
158
- updateStatus('🤔 جاري معالجة طلبك...');
159
 
160
  try {
161
  const response = await fetch(`${API_URL}/chat`, {
162
  method: 'POST',
163
- headers: {
164
- 'Content-Type': 'application/json',
165
- },
166
- body: JSON.stringify({
167
- message: message,
168
- mode: currentMode
169
- })
170
  });
171
 
172
  hideLoading();
@@ -174,17 +141,10 @@ async function sendMessage() {
174
  if (response.ok) {
175
  const data = await response.json();
176
  if (data.success) {
177
- // عرض الرد مع تحليل التفكير
178
  addMessage('', false, data.response);
179
- updateStatus(`✅ تم الرد بنجاح (وضع ${data.mode === 'flash' ? 'الفلاش' : 'البرو'})`);
180
-
181
- // إذا كان هناك تفكير، إظهار إشعار
182
- const hasThought = data.response.some(part => part.type === 'thought');
183
- if (hasThought && currentMode === 'pro') {
184
- updateStatus('🧠 تم عرض تفكير النموذج في الإطار المخصص');
185
- }
186
  } else {
187
- addMessage('عذراً، حدث خطأ في معالجة طلبك. حاول مرة أخرى.', false);
188
  updateStatus('❌ خطأ في الاستجابة', true);
189
  }
190
  } else {
@@ -193,25 +153,15 @@ async function sendMessage() {
193
  } catch (error) {
194
  hideLoading();
195
  console.error('خطأ:', error);
196
- addMessage('عذراً، لا يمكن الاتصال بالخادم. تأكد من تشغيل الخادم.', false);
197
- updateStatus('❌ فشل الاتصال بالخادم', true);
198
  }
199
  }
200
 
201
- // مسح الشات
202
- function clearChat() {
203
- chatMessages.innerHTML = '';
204
- addMessage('مرحباً! أنا MiniMax M2.5. كيف يمكنني مساعدتك؟', false);
205
- updateStatus('🗑️ تم مسح المحادثة');
206
- }
207
-
208
  // أحداث
209
  sendBtn.addEventListener('click', sendMessage);
210
- flashModeBtn.addEventListener('click', () => setMode('flash'));
211
- proModeBtn.addEventListener('click', () => setMode('pro'));
212
- clearChatBtn.addEventListener('click', clearChat);
213
-
214
- // إرسال بالضغط على Enter (Ctrl+Enter للإرسال)
215
  userInput.addEventListener('keypress', (e) => {
216
  if (e.key === 'Enter' && !e.shiftKey) {
217
  e.preventDefault();
@@ -219,25 +169,23 @@ userInput.addEventListener('keypress', (e) => {
219
  }
220
  });
221
 
222
- // تحميل الوضع الحالي عند بدء التشغيل
223
  async function loadCurrentMode() {
224
  try {
225
  const response = await fetch(`${API_URL}/mode`);
226
  if (response.ok) {
227
  const data = await response.json();
228
  currentMode = data.mode;
229
- currentModeDisplay.textContent = currentMode === 'flash' ? 'فلاش' : 'برو';
230
  if (currentMode === 'flash') {
231
- flashModeBtn.classList.add('active');
232
  } else {
233
- proModeBtn.classList.add('active');
234
  }
235
  }
236
  } catch (error) {
237
- console.error('خطأ في تحميل الوضع:', error);
238
  }
239
  }
240
 
241
- // بدء التشغيل
242
  loadCurrentMode();
243
- updateStatus('✅ جاهز للحديث! استخدم Ctrl+Enter للإرسال');
 
1
+ const API_URL = '/api';
 
2
  let currentMode = 'flash';
3
 
4
  // عناصر DOM
5
  const chatMessages = document.getElementById('chatMessages');
6
  const userInput = document.getElementById('userInput');
7
  const sendBtn = document.getElementById('sendBtn');
8
+ const flashBtn = document.getElementById('flashBtn');
9
+ const proBtn = document.getElementById('proBtn');
10
+ const statusBar = document.getElementById('status');
 
 
11
 
12
+ // إضافة رسالة
13
  function addMessage(text, isUser = false, parsedResponse = null) {
14
  const messageDiv = document.createElement('div');
15
  messageDiv.className = `message ${isUser ? 'user' : 'bot'}`;
16
 
17
  const avatar = document.createElement('div');
18
+ avatar.className = 'avatar';
19
  avatar.textContent = isUser ? '👤' : '🤖';
20
 
21
+ const content = document.createElement('div');
22
+ content.className = 'content';
23
 
24
  if (isUser) {
25
+ content.textContent = text;
26
+ } else if (parsedResponse && Array.isArray(parsedResponse)) {
27
+ parsedResponse.forEach(part => {
28
+ if (part.type === 'thought') {
29
+ const thoughtDiv = document.createElement('div');
30
+ thoughtDiv.className = 'thought-container';
31
+ thoughtDiv.innerHTML = `
32
+ <div class="thought-header">
33
+ <span>🧠</span>
 
 
 
 
 
 
 
34
  <span>تفكير النموذج (genisi thought)</span>
35
+ </div>
36
+ <div>${escapeHtml(part.content)}</div>
37
+ `;
38
+ content.appendChild(thoughtDiv);
39
+ } else if (part.type === 'text' && part.content) {
40
+ const textDiv = document.createElement('div');
41
+ textDiv.textContent = part.content;
42
+ content.appendChild(textDiv);
43
+ }
44
+ });
45
+ } else {
46
+ content.textContent = text;
 
 
 
 
 
 
 
 
 
 
 
47
  }
48
 
49
  messageDiv.appendChild(avatar);
50
+ messageDiv.appendChild(content);
51
  chatMessages.appendChild(messageDiv);
 
 
52
  chatMessages.scrollTop = chatMessages.scrollHeight;
53
  }
54
 
55
+ // دالة لتشفير HTML
56
+ function escapeHtml(text) {
57
+ const div = document.createElement('div');
58
+ div.textContent = text;
59
+ return div.innerHTML;
60
+ }
61
+
62
  // عرض حالة التحميل
63
  function showLoading() {
64
  const loadingDiv = document.createElement('div');
65
  loadingDiv.className = 'message bot';
66
  loadingDiv.id = 'loadingMessage';
67
  loadingDiv.innerHTML = `
68
+ <div class="avatar">🤖</div>
69
+ <div class="content">
70
+ <div class="loading"></div>
71
+ <div style="margin-top: 5px;">جاري التفكير...</div>
 
72
  </div>
73
  `;
74
  chatMessages.appendChild(loadingDiv);
75
  chatMessages.scrollTop = chatMessages.scrollHeight;
76
  }
77
 
 
78
  function hideLoading() {
79
  const loading = document.getElementById('loadingMessage');
80
+ if (loading) loading.remove();
 
 
81
  }
82
 
83
+ // تحديث الحالة
84
  function updateStatus(text, isError = false) {
85
+ statusBar.textContent = text;
86
+ statusBar.style.color = isError ? '#dc3545' : '#6c757d';
87
  setTimeout(() => {
88
  if (!isError) {
89
+ statusBar.textContent = `✅ جاهز | الوضع: ${currentMode === 'flash' ? 'فلاش' : 'برو'}`;
90
+ statusBar.style.color = '#6c757d';
91
  }
92
  }, 3000);
93
  }
 
97
  try {
98
  const response = await fetch(`${API_URL}/mode`, {
99
  method: 'POST',
100
+ headers: { 'Content-Type': 'application/json' },
 
 
101
  body: JSON.stringify({ mode })
102
  });
103
 
104
  if (response.ok) {
105
  currentMode = mode;
 
 
 
106
  if (mode === 'flash') {
107
+ flashBtn.classList.add('active');
108
+ proBtn.classList.remove('active');
109
  updateStatus('✅ تم التبديل إلى وضع الفلاش - ردود سريعة');
110
  } else {
111
+ proBtn.classList.add('active');
112
+ flashBtn.classList.remove('active');
113
+ updateStatus('✅ تم التبديل إلى وضع البرو - تفكير عميق');
114
  }
115
  }
116
  } catch (error) {
117
+ console.error('خطأ:', error);
118
  updateStatus('❌ فشل تغيير الوضع', true);
119
  }
120
  }
 
124
  const message = userInput.value.trim();
125
  if (!message) return;
126
 
 
127
  addMessage(message, true);
128
  userInput.value = '';
 
 
129
  showLoading();
130
+ updateStatus('🤔 جاري معالجة طلبك عبر MiniMax...');
131
 
132
  try {
133
  const response = await fetch(`${API_URL}/chat`, {
134
  method: 'POST',
135
+ headers: { 'Content-Type': 'application/json' },
136
+ body: JSON.stringify({ message, mode: currentMode })
 
 
 
 
 
137
  });
138
 
139
  hideLoading();
 
141
  if (response.ok) {
142
  const data = await response.json();
143
  if (data.success) {
 
144
  addMessage('', false, data.response);
145
+ updateStatus(`✅ تم الرد (وضع ${data.mode === 'flash' ? 'الفلاش' : 'البرو'})`);
 
 
 
 
 
 
146
  } else {
147
+ addMessage('عذراً، حدث خطأ. حاول مرة أخرى.', false);
148
  updateStatus('❌ خطأ في الاستجابة', true);
149
  }
150
  } else {
 
153
  } catch (error) {
154
  hideLoading();
155
  console.error('خطأ:', error);
156
+ addMessage('عذراً، لا يمكن الاتصال بالخادم.', false);
157
+ updateStatus('❌ فشل الاتصال', true);
158
  }
159
  }
160
 
 
 
 
 
 
 
 
161
  // أحداث
162
  sendBtn.addEventListener('click', sendMessage);
163
+ flashBtn.addEventListener('click', () => setMode('flash'));
164
+ proBtn.addEventListener('click', () => setMode('pro'));
 
 
 
165
  userInput.addEventListener('keypress', (e) => {
166
  if (e.key === 'Enter' && !e.shiftKey) {
167
  e.preventDefault();
 
169
  }
170
  });
171
 
172
+ // تحميل الوضع الحالي
173
  async function loadCurrentMode() {
174
  try {
175
  const response = await fetch(`${API_URL}/mode`);
176
  if (response.ok) {
177
  const data = await response.json();
178
  currentMode = data.mode;
 
179
  if (currentMode === 'flash') {
180
+ flashBtn.classList.add('active');
181
  } else {
182
+ proBtn.classList.add('active');
183
  }
184
  }
185
  } catch (error) {
186
+ console.error('خطأ:', error);
187
  }
188
  }
189
 
 
190
  loadCurrentMode();
191
+ updateStatus('✅ جاهز للحديث!');