Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
371 lines (325 loc) · 13.2 KB
/
Copy pathscript.js
File metadata and controls
371 lines (325 loc) · 13.2 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
// Chatbot functionality with Gemini API integration
class FrezeerChatbot {
constructor() {
this.chatMessages = document.getElementById('chatMessages');
this.messageInput = document.getElementById('messageInput');
this.sendButton = document.getElementById('sendButton');
this.isTyping = false;
// Initialize event listeners
this.initializeEventListeners();
// Mock responses for demo (replace with actual Gemini API calls)
this.responses = {
greeting: [
"Hello! I'm Frezeer AI, your advanced smart assistant. How can I help you today?",
"Greetings! I'm here to assist you with any questions or tasks you might have.",
"Welcome! I'm Frezeer AI, ready to help you with information, analysis, or any other assistance you need."
],
weather: [
"I can help you with weather information! Just let me know which city you're interested in.",
"Weather data is one of my specialties. What location would you like to check?"
],
technology: [
"Technology is my passion! I can help you with programming, AI, machine learning, and much more.",
"I love discussing technology! What specific tech topic would you like to explore?"
],
general: [
"That's an interesting question! Let me help you with that.",
"I'd be happy to assist you with that. Could you provide more details?",
"Great question! I can definitely help you with that."
]
};
}
initializeEventListeners() {
// Send button click
this.sendButton.addEventListener('click', () => {
this.sendMessage();
});
// Enter key press
this.messageInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
this.sendMessage();
}
});
// Input focus effects
this.messageInput.addEventListener('focus', () => {
this.messageInput.parentElement.style.transform = 'scale(1.02)';
});
this.messageInput.addEventListener('blur', () => {
this.messageInput.parentElement.style.transform = 'scale(1)';
});
// Action buttons
document.querySelectorAll('.action-btn').forEach(btn => {
btn.addEventListener('click', (e) => {
this.handleActionButton(e.target.closest('.action-btn'));
});
});
}
async sendMessage() {
const message = this.messageInput.value.trim();
if (!message || this.isTyping) return;
// Add user message
this.addMessage(message, 'user');
this.messageInput.value = '';
// Show typing indicator
this.showTypingIndicator();
// Simulate API call delay
await this.simulateResponseDelay();
// Hide typing indicator
this.hideTypingIndicator();
// Generate and add bot response
const response = await this.generateResponse(message);
this.addMessage(response, 'bot');
}
addMessage(content, sender) {
const messageDiv = document.createElement('div');
messageDiv.className = `message ${sender}-message`;
const currentTime = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
if (sender === 'user') {
messageDiv.innerHTML = `
<div class="message-avatar">
<div style="background: linear-gradient(45deg, #00ffff, #0080ff); width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; border-radius: 50%;">U</div>
</div>
<div class="message-content">
<div class="message-bubble">
<p>${this.escapeHtml(content)}</p>
</div>
<div class="message-time">${currentTime}</div>
</div>
`;
} else {
messageDiv.innerHTML = `
<div class="message-avatar">
<img src="frezeer_logo.jpg" alt="Frezeer AI" class="avatar-img">
</div>
<div class="message-content">
<div class="message-bubble">
<p>${this.escapeHtml(content)}</p>
</div>
<div class="message-time">${currentTime}</div>
</div>
`;
}
this.chatMessages.appendChild(messageDiv);
this.scrollToBottom();
}
showTypingIndicator() {
this.isTyping = true;
const typingDiv = document.createElement('div');
typingDiv.className = 'message bot-message typing-indicator-message';
typingDiv.id = 'typing-indicator';
typingDiv.innerHTML = `
<div class="message-avatar">
<img src="frezeer_logo.jpg" alt="Frezeer AI" class="avatar-img">
</div>
<div class="message-content">
<div class="message-bubble typing-indicator">
<div class="typing-dot"></div>
<div class="typing-dot"></div>
<div class="typing-dot"></div>
</div>
</div>
`;
this.chatMessages.appendChild(typingDiv);
this.scrollToBottom();
}
hideTypingIndicator() {
this.isTyping = false;
const typingIndicator = document.getElementById('typing-indicator');
if (typingIndicator) {
typingIndicator.remove();
}
}
async generateResponse(userMessage) {
try {
const openRouterAPI = new OpenRouterAPI('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');
const response = await openRouterAPI.generateResponse(userMessage);
return response;
} catch (error) {
console.error('API Error:', error);
return `حدث خطأ أثناء الاتصال بـ OpenRouter: ${error.message || error}`;
}
}
generateContextualResponse(message) {
const responses = [
`I understand you're asking about "${message}". That's a great question! I can help you with that. Could you provide more specific details so I can give you the most accurate and helpful response?`,
`Interesting! You mentioned "${message}". I'd be happy to assist you with this. What specific aspect would you like me to focus on?`,
`Thanks for your question about "${message}"! I'm here to help. Could you elaborate a bit more so I can provide you with the most relevant information?`,
`I see you're interested in "${message}". That's definitely something I can help you with! What specific information are you looking for?`
];
return responses[Math.floor(Math.random() * responses.length)];
}
getRandomResponse(category) {
const responses = this.responses[category];
return responses[Math.floor(Math.random() * responses.length)];
}
async simulateResponseDelay() {
// Simulate API call delay (1-3 seconds)
const delay = Math.random() * 2000 + 1000;
return new Promise(resolve => setTimeout(resolve, delay));
}
scrollToBottom() {
this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
}
escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
handleActionButton(button) {
const action = button.getAttribute('title');
switch(action) {
case 'Voice Input':
this.showNotification('Voice input feature coming soon!');
break;
case 'Upload File':
this.showNotification('File upload feature coming soon!');
break;
case 'Settings':
this.showNotification('Settings panel coming soon!');
break;
}
}
showNotification(message) {
// Create notification element
const notification = document.createElement('div');
notification.className = 'notification';
notification.textContent = message;
notification.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
background: rgba(0, 255, 255, 0.9);
color: #000;
padding: 15px 20px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
z-index: 1000;
animation: slideIn 0.3s ease-out;
`;
document.body.appendChild(notification);
// Remove notification after 3 seconds
setTimeout(() => {
notification.style.animation = 'slideOut 0.3s ease-out';
setTimeout(() => {
if (notification.parentNode) {
notification.parentNode.removeChild(notification);
}
}, 300);
}, 3000);
}
}
// OpenRouter API Integration
class OpenRouterAPI {
constructor(apiKey) {
this.apiKey = apiKey;
this.baseURL = 'https://openrouter.ai/api/v1/chat/completions';
}
async generateResponse(prompt) {
try {
const response = await fetch(this.baseURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.apiKey}`
},
body: JSON.stringify({
model: "openai/gpt-3.5-turbo",
messages: [{ role: "user", content: prompt }]
})
});
const data = await response.json();
if (data.choices && data.choices[0] && data.choices[0].message) {
return data.choices[0].message.content;
} else {
throw new Error('Invalid response format');
}
} catch (error) {
console.error('OpenRouter API Error:', error);
return `حدث خطأ أثناء الاتصال بـ OpenRouter: ${error.message || error}`;
}
}
}
// Initialize the chatbot when the page loads
document.addEventListener('DOMContentLoaded', () => {
const chatbot = new FrezeerChatbot();
// Add some CSS for notifications
const style = document.createElement('style');
style.textContent = `
@keyframes slideIn {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
@keyframes slideOut {
from {
transform: translateX(0);
opacity: 1;
}
to {
transform: translateX(100%);
opacity: 0;
}
}
`;
document.head.appendChild(style);
// Add welcome message after a short delay
setTimeout(() => {
chatbot.addMessage("Welcome to Frezeer AI! I'm here to help you with any questions or tasks. Feel free to ask me anything!", 'bot');
}, 1000);
});
// Add some interactive effects
document.addEventListener('DOMContentLoaded', () => {
// Add hover effects to the logo
const logo = document.querySelector('.logo');
if (logo) {
logo.addEventListener('mouseenter', () => {
logo.style.transform = 'scale(1.1) rotate(5deg)';
});
logo.addEventListener('mouseleave', () => {
logo.style.transform = 'scale(1) rotate(0deg)';
});
}
// Add particle effect on click
document.addEventListener('click', (e) => {
const particle = document.createElement('div');
particle.style.cssText = `
position: fixed;
left: ${e.clientX}px;
top: ${e.clientY}px;
width: 4px;
height: 4px;
background: #00ffff;
border-radius: 50%;
pointer-events: none;
z-index: 1000;
animation: particleFade 1s ease-out forwards;
`;
document.body.appendChild(particle);
setTimeout(() => {
if (particle.parentNode) {
particle.parentNode.removeChild(particle);
}
}, 1000);
});
});
// Add particle animation CSS
const particleStyle = document.createElement('style');
particleStyle.textContent = `
@keyframes particleFade {
0% {
transform: scale(1);
opacity: 1;
}
100% {
transform: scale(0) translateY(-20px);
opacity: 0;
}
}
`;
document.head.appendChild(particleStyle);