AnesKAM commited on
Commit
e1e62ea
·
verified ·
1 Parent(s): 8c28e1b

Create public/script.js

Browse files
Files changed (1) hide show
  1. public/script.js +243 -0
public/script.js ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const API_URL = 'http://localhost:3000/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
+ }
114
+
115
+ // تغيير الوضع
116
+ 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
+ }
146
+
147
+ // إرسال رسالة
148
+ 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();
173
+
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 {
191
+ throw new Error('خطأ في الاتصال');
192
+ }
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();
218
+ sendMessage();
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 للإرسال');