-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
529 lines (460 loc) · 18.3 KB
/
script.js
File metadata and controls
529 lines (460 loc) · 18.3 KB
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
class ChatInterface {
constructor() {
this.chatMessages = document.getElementById('chatMessages');
this.userInput = document.getElementById('userInput');
this.sendButton = document.getElementById('sendButton');
this.themeToggle = document.getElementById('themeToggle');
this.modelSelector = document.getElementById('modelSelector');
this.addModelBtn = document.getElementById('addModel');
this.apiModal = document.getElementById('apiModal');
this.modelSelect = document.getElementById('modelSelect');
this.apiKeyInput = document.getElementById('apiKey');
this.saveApiKeyBtn = document.getElementById('saveApiKey');
this.cancelApiKeyBtn = document.getElementById('cancelApiKey');
this.apiStatus = document.getElementById('apiStatus');
this.currentModel = null;
this.models = this.loadModels();
this.initializeEventListeners();
this.initializeTheme();
this.initializeModels();
this.adjustTextareaHeight();
}
initializeEventListeners() {
// Chat event listeners
this.sendButton.addEventListener('click', () => this.sendMessage());
this.userInput.addEventListener('input', () => this.adjustTextareaHeight());
this.userInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
if (e.shiftKey) {
setTimeout(() => this.adjustTextareaHeight(), 0);
} else {
e.preventDefault();
this.sendMessage();
}
}
});
// Theme toggle
this.themeToggle.addEventListener('click', () => {
const currentTheme = document.body.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
this.applyTheme(newTheme);
});
// Model management
this.addModelBtn.addEventListener('click', () => this.showApiModal());
this.saveApiKeyBtn.addEventListener('click', () => this.saveApiKey());
this.cancelApiKeyBtn.addEventListener('click', () => this.hideApiModal());
this.modelSelector.addEventListener('click', (e) => {
const modelButton = e.target.closest('.model-button:not(.add-model)');
if (modelButton) {
if (e.target.classList.contains('remove-icon')) {
this.removeModel(modelButton.dataset.model);
} else {
this.setActiveModel(modelButton.dataset.model);
}
}
});
// API key validation on input
this.apiKeyInput.addEventListener('input', () => {
this.apiStatus.className = 'api-status';
this.apiStatus.textContent = '';
});
}
loadModels() {
const savedModels = localStorage.getItem('models');
return savedModels ? JSON.parse(savedModels) : {};
}
saveModels() {
localStorage.setItem('models', JSON.stringify(this.models));
}
initializeModels() {
// Add saved models to the UI
const modelEntries = Object.entries(this.models);
modelEntries.forEach(([model, data]) => {
this.addModelButton(model, data.status === 'active');
});
// Select the last used model if available
if (modelEntries.length > 0) {
const lastModel = modelEntries[modelEntries.length - 1][0];
this.setActiveModel(lastModel);
}
}
showApiModal() {
// Update model select options to only show unselected models
const modelSelect = document.getElementById('modelSelect');
modelSelect.innerHTML = ''; // Clear existing options
const availableModels = {
'gemini': 'Gemini',
'chatgpt': 'ChatGPT',
'claude': 'Claude'
};
// Only show models that haven't been added yet
Object.entries(availableModels).forEach(([value, text]) => {
if (!this.models[value]) {
const option = document.createElement('option');
option.value = value;
option.textContent = text;
modelSelect.appendChild(option);
}
});
// If no models are available, show message and return
if (modelSelect.options.length === 0) {
alert('تم إضافة جميع النماذج المتاحة. يرجى حذف نموذج لإضافة نموذج جديد.');
return;
}
this.apiModal.classList.add('show');
this.apiStatus.className = 'api-status';
this.apiStatus.textContent = '';
this.apiKeyInput.value = '';
}
hideApiModal() {
this.apiModal.classList.remove('show');
this.apiKeyInput.value = '';
this.apiStatus.className = 'api-status';
this.apiStatus.textContent = '';
}
async validateApiKey(model, apiKey) {
this.apiStatus.className = 'api-status loading';
this.apiStatus.innerHTML = `
<div class="loading-spinner"></div>
<div>جاري التحقق من مفتاح API</div>
`;
try {
// Example API validation for different models
let isValid = false;
switch(model) {
case 'chatgpt':
// OpenAI API validation
const response = await fetch('https://api.openai.com/v1/models', {
headers: {
'Authorization': `Bearer ${apiKey}`
}
});
isValid = response.ok;
break;
case 'claude':
// Claude API validation
// Add your Claude API validation logic here
isValid = apiKey.startsWith('claude-');
break;
case 'gemini':
// Gemini API validation
// Add your Gemini API validation logic here
isValid = apiKey.length > 20;
break;
}
if (isValid) {
this.apiStatus.className = 'api-status success';
this.apiStatus.textContent = 'مفتاح API صالح';
} else {
this.apiStatus.className = 'api-status error';
this.apiStatus.textContent = 'مفتاح API غير صالح';
}
return isValid;
} catch (error) {
this.apiStatus.className = 'api-status error';
this.apiStatus.textContent = 'حدث خطأ أثناء التحقق من مفتاح API';
return false;
}
}
async checkApiStatus(model, apiKey) {
try {
const isValid = await this.validateApiKey(model, apiKey);
return isValid ? 'active' : 'disabled';
} catch {
return 'disabled';
}
}
async saveApiKey() {
const model = this.modelSelect.value;
const apiKey = this.apiKeyInput.value.trim();
// Check if model already exists
if (this.models[model]) {
this.apiStatus.className = 'api-status error';
this.apiStatus.textContent = 'هذا النموذج موجود بالفعل';
return;
}
if (!apiKey) {
this.apiStatus.className = 'api-status error';
this.apiStatus.textContent = 'الرجاء إدخال مفتاح API';
return;
}
try {
const isValid = await this.validateApiKey(model, apiKey);
if (isValid) {
const status = await this.checkApiStatus(model, apiKey);
this.models[model] = {
apiKey: apiKey,
status: status
};
this.saveModels();
this.addModelButton(model, status === 'active');
// Set this as the active model
setTimeout(() => {
this.hideApiModal();
this.setActiveModel(model);
}, 1000);
}
} catch (error) {
this.apiStatus.className = 'api-status error';
this.apiStatus.textContent = 'حدث خطأ أثناء حفظ مفتاح API';
}
}
addModelButton(model, isActive = true) {
const modelNames = {
'gemini': 'Gemini',
'chatgpt': 'ChatGPT',
'claude': 'Claude'
};
// Remove existing button if it exists
const existingButton = this.modelSelector.querySelector(`[data-model="${model}"]`);
if (existingButton) {
existingButton.remove();
}
const button = document.createElement('button');
button.className = `model-button ${!isActive ? 'disabled' : ''}`;
button.dataset.model = model;
button.innerHTML = `
${modelNames[model]}
<span class="remove-icon">×</span>
`;
// Insert before the add button
this.modelSelector.insertBefore(button, this.addModelBtn);
}
removeModel(model) {
const button = this.modelSelector.querySelector(`[data-model="${model}"]`);
if (button) {
if (button.classList.contains('active')) {
this.currentModel = null;
}
button.remove();
delete this.models[model];
this.saveModels();
}
}
async setActiveModel(model) {
const modelData = this.models[model];
if (!modelData) {
alert('الرجاء إضافة مفتاح API أولاً');
return;
}
// Check if API is still valid
const status = await this.checkApiStatus(model, modelData.apiKey);
if (status === 'disabled') {
alert('مفتاح API غير صالح أو منتهي الصلاحية');
this.addModelButton(model, false);
return;
}
this.currentModel = model;
this.modelSelector.querySelectorAll('.model-button').forEach(button => {
button.classList.toggle('active', button.dataset.model === model);
});
}
adjustTextareaHeight() {
const textarea = this.userInput;
// Reset height to auto to get the correct scrollHeight
textarea.style.height = 'auto';
// Calculate new height
const newHeight = Math.min(Math.max(textarea.scrollHeight, 50), 200);
// Set new height
textarea.style.height = newHeight + 'px';
// Adjust input container padding if needed
const inputContainer = textarea.closest('.input-container');
if (inputContainer) {
const extraPadding = newHeight > 50 ? 10 : 0;
inputContainer.style.paddingTop = (15 + extraPadding) + 'px';
inputContainer.style.paddingBottom = (15 + extraPadding) + 'px';
}
}
initializeTheme() {
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
const getCurrentTheme = () => {
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
return savedTheme;
}
return prefersDarkScheme.matches ? 'dark' : 'light';
};
this.applyTheme(getCurrentTheme());
prefersDarkScheme.addEventListener('change', (e) => {
if (!localStorage.getItem('theme')) {
this.applyTheme(e.matches ? 'dark' : 'light');
}
});
}
applyTheme(theme) {
document.body.setAttribute('data-theme', theme);
this.themeToggle.textContent = theme === 'dark' ? 'الوضع النهاري' : 'الوضع الليلي';
localStorage.setItem('theme', theme);
}
async sendMessage() {
if (!this.currentModel) {
alert('الرجاء اختيار نموذج للمحادثة');
return;
}
const message = this.userInput.value.trim();
if (!message) return;
// Add user message to chat
this.addMessageToChat(message, 'user');
// Clear input and reset height
this.userInput.value = '';
this.userInput.style.height = '50px';
// Reset input container padding
const inputContainer = this.userInput.closest('.input-container');
if (inputContainer) {
inputContainer.style.paddingTop = '15px';
inputContainer.style.paddingBottom = '15px';
}
// Show loading indicator with typing animation
this.showLoading();
try {
const response = await this.fetchGPTResponse(message);
this.hideLoading();
this.addMessageToChat(response, 'bot');
} catch (error) {
this.hideLoading();
this.addMessageToChat(error.message || 'عذراً، حدث خطأ في الاتصال. يرجى المحاولة مرة أخرى.', 'bot', true);
console.error('Error:', error);
}
}
async fetchGPTResponse(message) {
const modelData = this.models[this.currentModel];
if (!modelData) {
throw new Error('لم يتم العثور على مفتاح API للنموذج');
}
try {
let response;
switch(this.currentModel) {
case 'chatgpt':
response = await this.callOpenAI(message, modelData.apiKey);
break;
case 'claude':
response = await this.callClaude(message, modelData.apiKey);
break;
case 'gemini':
response = await this.callGemini(message, modelData.apiKey);
break;
default:
throw new Error('نموذج غير مدعوم');
}
return response;
} catch (error) {
console.error('Error:', error);
throw new Error(`حدث خطأ أثناء الاتصال بـ ${this.currentModel}: ${error.message}`);
}
}
async callOpenAI(message, apiKey) {
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
},
body: JSON.stringify({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: message
}
],
temperature: 0.7
})
});
if (!response.ok) {
throw new Error('فشل في الاتصال بـ OpenAI');
}
const data = await response.json();
return data.choices[0].message.content;
}
async callClaude(message, apiKey) {
const response = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey,
'anthropic-version': '2023-06-01'
},
body: JSON.stringify({
model: 'claude-2',
max_tokens: 1000,
messages: [
{
role: 'user',
content: message
}
]
})
});
if (!response.ok) {
throw new Error('فشل في الاتصال بـ Claude');
}
const data = await response.json();
return data.content[0].text;
}
async callGemini(message, apiKey) {
const response = await fetch(`https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=${apiKey}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
contents: [{
parts: [{
text: message
}]
}]
})
});
if (!response.ok) {
throw new Error('فشل في الاتصال بـ Gemini');
}
const data = await response.json();
if (data.candidates && data.candidates[0] && data.candidates[0].content) {
return data.candidates[0].content.parts[0].text;
} else {
throw new Error('لم يتم استلام رد صحيح من Gemini');
}
}
addMessageToChat(message, sender, isError = false) {
const messageDiv = document.createElement('div');
messageDiv.className = `chat-message ${sender}-message`;
if (isError) messageDiv.classList.add('error-message');
// Split message into paragraphs
const paragraphs = message.split('\n');
paragraphs.forEach((paragraph, index) => {
if (paragraph.trim()) {
const p = document.createElement('p');
p.textContent = paragraph;
messageDiv.appendChild(p);
// Add spacing between paragraphs
if (index < paragraphs.length - 1) {
messageDiv.appendChild(document.createElement('br'));
}
}
});
this.chatMessages.appendChild(messageDiv);
this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
}
showLoading() {
const typingIndicator = document.createElement('div');
typingIndicator.className = 'typing-indicator';
typingIndicator.id = 'typingIndicator';
typingIndicator.innerHTML = `
<div class="typing-dot"></div>
<div class="typing-dot"></div>
<div class="typing-dot"></div>
`;
this.chatMessages.appendChild(typingIndicator);
this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
}
hideLoading() {
const typingIndicator = document.getElementById('typingIndicator');
if (typingIndicator) {
typingIndicator.remove();
}
}
}
// Initialize the chat interface when the page loads
document.addEventListener('DOMContentLoaded', () => {
new ChatInterface();
});