-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaiChat.js
More file actions
142 lines (123 loc) · 5.12 KB
/
Copy pathaiChat.js
File metadata and controls
142 lines (123 loc) · 5.12 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
class ChatInterface {
constructor() {
try {
this.input = document.querySelector('#chat-input');
this.sendButton = document.querySelector('#send-button');
this.chatMessages = document.querySelector('#chat-messages');
if (!this.input || !this.sendButton || !this.chatMessages) {
throw new Error('Required elements not found');
}
console.log('ChatInterface initialized successfully');
this.initializeEventListeners();
} catch (error) {
console.error('Error initializing ChatInterface:', error);
}
}
initializeEventListeners = () => {
try {
this.sendButton.addEventListener('click', this.handleSend);
this.input.addEventListener('keypress', this.handleKeyPress);
console.log('Event listeners initialized');
} catch (error) {
console.error('Error setting up event listeners:', error);
}
}
handleKeyPress = (e) => {
if (e.key === 'Enter') {
this.handleSend();
}
}
addMessage = (message, isUser = false) => {
const messageTemplate = `
<div class="flex items-start message-animation ${isUser ? 'justify-end space-x-2' : 'space-x-2'}">
${!isUser ? this.getAIAvatarTemplate() : ''}
<div class="${isUser ? 'bg-blue-500 text-white' : 'bg-gray-100 text-gray-800'}
rounded-lg p-3 max-w-[80%] message-bubble">
<p>${message}</p>
</div>
${isUser ? this.getUserAvatarTemplate() : ''}
</div>
`;
this.chatMessages?.insertAdjacentHTML('beforeend', messageTemplate);
this.scrollToBottom();
}
getAIAvatarTemplate = () => `
<div class="w-8 h-8 rounded-full bg-blue-500 flex items-center justify-center flex-shrink-0 ai-avatar">
<svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<circle cx="12" cy="12" r="10" stroke-width="2" class="opacity-50"/>
<path d="M12 8 L15 12 L9 16" stroke-width="2"/>
</svg>
</div>
`
getUserAvatarTemplate = () => `
<div class="w-8 h-8 rounded-full bg-gray-300 flex items-center justify-center flex-shrink-0">
<svg class="w-5 h-5 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
</svg>
</div>
`
scrollToBottom = () => {
if (this.chatMessages) {
this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
}
}
showTypingIndicator = () => {
const typingTemplate = `
<div class="flex items-start space-x-2 typing-indicator">
${this.getAIAvatarTemplate()}
<div class="bg-gray-100 rounded-lg p-3 max-w-[80%]">
<p class="text-gray-500">
<span>•</span>
<span>•</span>
<span>•</span>
</p>
</div>
</div>
`;
this.chatMessages?.insertAdjacentHTML('beforeend', typingTemplate);
this.scrollToBottom();
}
removeTypingIndicator = () => {
const typingIndicator = document.querySelector('.typing-indicator');
typingIndicator?.remove();
}
simulateAIResponse = async (message) => {
this.showTypingIndicator();
// Simulate AI thinking time
await new Promise(resolve => setTimeout(resolve, 1500));
this.removeTypingIndicator();
this.addMessage(this.getAIResponse(message));
}
getAIResponse = (userMessage) => {
// Simple response logic - can be expanded
const responses = [
"I understand. Let me help you with that.",
"That's interesting. Could you tell me more?",
"I'm processing your request. Here's what I think...",
"Based on what you've said, I suggest..."
];
return responses[Math.floor(Math.random() * responses.length)];
}
handleSend = () => {
try {
const message = this.input?.value.trim();
if (message) {
this.sendButton.disabled = true;
this.sendButton.classList.add('opacity-50');
this.addMessage(message, true);
this.input.value = '';
this.simulateAIResponse(message)
.finally(() => {
this.sendButton.disabled = false;
this.sendButton.classList.remove('opacity-50');
});
}
} catch (error) {
console.error('Error sending message:', error);
this.sendButton.disabled = false;
this.sendButton.classList.remove('opacity-50');
}
}
}
export default ChatInterface;