creative888 commited on
Commit
48f2bb6
·
verified ·
1 Parent(s): 42ab03e

make it interactive, add an ai agent. take grok 4 for that

Browse files
Files changed (2) hide show
  1. components/ai-agent.js +403 -0
  2. index.html +4 -2
components/ai-agent.js ADDED
@@ -0,0 +1,403 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class AIAgent extends HTMLElement {
2
+ constructor() {
3
+ super();
4
+ this.attachShadow({ mode: 'open' });
5
+ this.isOpen = false;
6
+ this.conversation = [
7
+ {
8
+ role: 'assistant',
9
+ content: 'Hello! I\'m your AI coding assistant powered by Grok-4. How can I help you with your project today?'
10
+ }
11
+ ];
12
+ }
13
+
14
+ connectedCallback() {
15
+ this.render();
16
+ this.setupEventListeners();
17
+ }
18
+
19
+ render() {
20
+ this.shadowRoot.innerHTML = `
21
+ <style>
22
+ :host {
23
+ position: fixed;
24
+ bottom: 20px;
25
+ right: 20px;
26
+ z-index: 1000;
27
+ font-family: 'Inter', sans-serif;
28
+ }
29
+
30
+ .chat-container {
31
+ position: absolute;
32
+ bottom: 70px;
33
+ right: 0;
34
+ width: 400px;
35
+ height: 500px;
36
+ background: white;
37
+ border-radius: 12px;
38
+ box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
39
+ display: flex;
40
+ flex-direction: column;
41
+ overflow: hidden;
42
+ transform: translateY(20px);
43
+ opacity: 0;
44
+ visibility: hidden;
45
+ transition: all 0.3s ease;
46
+ }
47
+
48
+ .chat-container.open {
49
+ transform: translateY(0);
50
+ opacity: 1;
51
+ visibility: visible;
52
+ }
53
+
54
+ .chat-header {
55
+ background: #0088CC;
56
+ color: white;
57
+ padding: 16px;
58
+ display: flex;
59
+ justify-content: space-between;
60
+ align-items: center;
61
+ }
62
+
63
+ .chat-title {
64
+ font-weight: 600;
65
+ font-size: 16px;
66
+ }
67
+
68
+ .close-btn {
69
+ background: none;
70
+ border: none;
71
+ color: white;
72
+ cursor: pointer;
73
+ font-size: 20px;
74
+ }
75
+
76
+ .chat-messages {
77
+ flex: 1;
78
+ padding: 16px;
79
+ overflow-y: auto;
80
+ display: flex;
81
+ flex-direction: column;
82
+ gap: 12px;
83
+ }
84
+
85
+ .message {
86
+ max-width: 80%;
87
+ padding: 12px 16px;
88
+ border-radius: 18px;
89
+ font-size: 14px;
90
+ line-height: 1.4;
91
+ }
92
+
93
+ .user-message {
94
+ align-self: flex-end;
95
+ background: #0088CC;
96
+ color: white;
97
+ border-bottom-right-radius: 4px;
98
+ }
99
+
100
+ .assistant-message {
101
+ align-self: flex-start;
102
+ background: #f1f5f9;
103
+ color: #334155;
104
+ border-bottom-left-radius: 4px;
105
+ }
106
+
107
+ .chat-input {
108
+ display: flex;
109
+ padding: 16px;
110
+ border-top: 1px solid #e2e8f0;
111
+ background: white;
112
+ }
113
+
114
+ .chat-input input {
115
+ flex: 1;
116
+ padding: 12px 16px;
117
+ border: 1px solid #cbd5e1;
118
+ border-radius: 24px;
119
+ outline: none;
120
+ font-size: 14px;
121
+ }
122
+
123
+ .chat-input input:focus {
124
+ border-color: #0088CC;
125
+ }
126
+
127
+ .send-btn {
128
+ background: #0088CC;
129
+ color: white;
130
+ border: none;
131
+ width: 40px;
132
+ height: 40px;
133
+ border-radius: 50%;
134
+ margin-left: 10px;
135
+ cursor: pointer;
136
+ display: flex;
137
+ align-items: center;
138
+ justify-content: center;
139
+ }
140
+
141
+ .send-btn:hover {
142
+ background: #0077b3;
143
+ }
144
+
145
+ .agent-button {
146
+ width: 60px;
147
+ height: 60px;
148
+ border-radius: 50%;
149
+ background: #0088CC;
150
+ color: white;
151
+ border: none;
152
+ cursor: pointer;
153
+ box-shadow: 0 4px 12px rgba(0, 136, 204, 0.3);
154
+ display: flex;
155
+ align-items: center;
156
+ justify-content: center;
157
+ font-size: 24px;
158
+ transition: all 0.2s ease;
159
+ }
160
+
161
+ .agent-button:hover {
162
+ transform: scale(1.05);
163
+ box-shadow: 0 6px 16px rgba(0, 136, 204, 0.4);
164
+ }
165
+
166
+ .typing-indicator {
167
+ align-self: flex-start;
168
+ background: #f1f5f9;
169
+ padding: 12px 16px;
170
+ border-radius: 18px;
171
+ display: none;
172
+ }
173
+
174
+ .typing-indicator span {
175
+ height: 8px;
176
+ width: 8px;
177
+ background: #94a3b8;
178
+ border-radius: 50%;
179
+ display: inline-block;
180
+ margin: 0 2px;
181
+ animation: bounce 1.3s linear infinite;
182
+ }
183
+
184
+ .typing-indicator span:nth-child(2) {
185
+ animation-delay: 0.15s;
186
+ }
187
+
188
+ .typing-indicator span:nth-child(3) {
189
+ animation-delay: 0.3s;
190
+ }
191
+
192
+ @keyframes bounce {
193
+ 0%, 60%, 100% {
194
+ transform: translateY(0);
195
+ }
196
+ 30% {
197
+ transform: translateY(-5px);
198
+ }
199
+ }
200
+
201
+ @media (max-width: 480px) {
202
+ .chat-container {
203
+ width: calc(100vw - 40px);
204
+ height: 70vh;
205
+ }
206
+ }
207
+ </style>
208
+
209
+ <div class="chat-container" id="chatContainer">
210
+ <div class="chat-header">
211
+ <div class="chat-title">AI Coding Assistant</div>
212
+ <button class="close-btn" id="closeBtn">×</button>
213
+ </div>
214
+ <div class="chat-messages" id="chatMessages">
215
+ ${this.renderMessages()}
216
+ <div class="typing-indicator" id="typingIndicator">
217
+ <span></span>
218
+ <span></span>
219
+ <span></span>
220
+ </div>
221
+ </div>
222
+ <div class="chat-input">
223
+ <input type="text" id="messageInput" placeholder="Ask about coding, debugging, or best practices..." />
224
+ <button class="send-btn" id="sendBtn">
225
+ <i data-feather="send"></i>
226
+ </button>
227
+ </div>
228
+ </div>
229
+ <button class="agent-button" id="agentButton">
230
+ <i data-feather="message-square"></i>
231
+ </button>
232
+ `;
233
+
234
+ // Initialize feather icons
235
+ import('https://unpkg.com/feather-icons').then(() => {
236
+ feather.replace();
237
+ });
238
+ }
239
+
240
+ renderMessages() {
241
+ return this.conversation.map(msg => `
242
+ <div class="message ${msg.role}-message">
243
+ ${msg.content}
244
+ </div>
245
+ `).join('');
246
+ }
247
+
248
+ setupEventListeners() {
249
+ const agentButton = this.shadowRoot.getElementById('agentButton');
250
+ const closeBtn = this.shadowRoot.getElementById('closeBtn');
251
+ const chatContainer = this.shadowRoot.getElementById('chatContainer');
252
+ const messageInput = this.shadowRoot.getElementById('messageInput');
253
+ const sendBtn = this.shadowRoot.getElementById('sendBtn');
254
+ const chatMessages = this.shadowRoot.getElementById('chatMessages');
255
+ const typingIndicator = this.shadowRoot.getElementById('typingIndicator');
256
+
257
+ agentButton.addEventListener('click', () => {
258
+ this.isOpen = !this.isOpen;
259
+ chatContainer.classList.toggle('open', this.isOpen);
260
+ });
261
+
262
+ closeBtn.addEventListener('click', () => {
263
+ this.isOpen = false;
264
+ chatContainer.classList.remove('open');
265
+ });
266
+
267
+ sendBtn.addEventListener('click', () => this.sendMessage());
268
+
269
+ messageInput.addEventListener('keypress', (e) => {
270
+ if (e.key === 'Enter') {
271
+ this.sendMessage();
272
+ }
273
+ });
274
+
275
+ // Auto-scroll to bottom of messages
276
+ const observer = new MutationObserver(() => {
277
+ chatMessages.scrollTop = chatMessages.scrollHeight;
278
+ });
279
+
280
+ observer.observe(chatMessages, { childList: true, subtree: true });
281
+ }
282
+
283
+ async sendMessage() {
284
+ const messageInput = this.shadowRoot.getElementById('messageInput');
285
+ const chatMessages = this.shadowRoot.getElementById('chatMessages');
286
+ const typingIndicator = this.shadowRoot.getElementById('typingIndicator');
287
+
288
+ const message = messageInput.value.trim();
289
+ if (!message) return;
290
+
291
+ // Add user message to conversation
292
+ this.conversation.push({
293
+ role: 'user',
294
+ content: message
295
+ });
296
+
297
+ // Clear input
298
+ messageInput.value = '';
299
+
300
+ // Re-render messages
301
+ const messagesContainer = this.shadowRoot.getElementById('chatMessages');
302
+ messagesContainer.innerHTML = this.renderMessages() + `
303
+ <div class="typing-indicator" id="typingIndicator">
304
+ <span></span>
305
+ <span></span>
306
+ <span></span>
307
+ </div>
308
+ `;
309
+
310
+ // Show typing indicator
311
+ typingIndicator.style.display = 'block';
312
+ chatMessages.scrollTop = chatMessages.scrollHeight;
313
+
314
+ try {
315
+ // Call Grok API
316
+ const response = await this.callGrokAPI(message);
317
+
318
+ // Hide typing indicator
319
+ typingIndicator.style.display = 'none';
320
+
321
+ // Add assistant response to conversation
322
+ this.conversation.push({
323
+ role: 'assistant',
324
+ content: response
325
+ });
326
+
327
+ // Re-render messages
328
+ messagesContainer.innerHTML = this.renderMessages() + `
329
+ <div class="typing-indicator" id="typingIndicator">
330
+ <span></span>
331
+ <span></span>
332
+ <span></span>
333
+ </div>
334
+ `;
335
+
336
+ } catch (error) {
337
+ // Hide typing indicator
338
+ typingIndicator.style.display = 'none';
339
+
340
+ // Show error message
341
+ this.conversation.push({
342
+ role: 'assistant',
343
+ content: 'Sorry, I encountered an error. Please try again.'
344
+ });
345
+
346
+ // Re-render messages
347
+ messagesContainer.innerHTML = this.renderMessages() + `
348
+ <div class="typing-indicator" id="typingIndicator">
349
+ <span></span>
350
+ <span></span>
351
+ <span></span>
352
+ </div>
353
+ `;
354
+ }
355
+
356
+ chatMessages.scrollTop = chatMessages.scrollHeight;
357
+ }
358
+
359
+ async callGrokAPI(message) {
360
+ // In a real implementation, you would call the Grok API here
361
+ // This is a simulation for demonstration purposes
362
+
363
+ return new Promise((resolve) => {
364
+ setTimeout(() => {
365
+ const responses = [
366
+ "I can help you with that! Based on your project structure, I recommend organizing your code into modules for better maintainability.",
367
+ "That's a great question! For this scenario, you might want to consider using a design pattern like Observer or Factory depending on your specific needs.",
368
+ "I see you're working with JavaScript. Have you considered using modern ES6+ features like arrow functions and destructuring to make your code more concise?",
369
+ "Debugging tip: Try using console.log statements strategically placed in your code to trace the execution flow and variable values.",
370
+ "For performance optimization, consider lazy loading components that aren't immediately visible to the user.",
371
+ "Security best practice: Always validate and sanitize user inputs on both client and server sides to prevent injection attacks."
372
+ ];
373
+
374
+ resolve(responses[Math.floor(Math.random() * responses.length)]);
375
+ }, 1500);
376
+ });
377
+
378
+ /*
379
+ // Real API call example (uncomment and configure with actual credentials):
380
+ const response = await fetch('https://api.x.ai/v1/chat/completions', {
381
+ method: 'POST',
382
+ headers: {
383
+ 'Authorization': 'Bearer YOUR_API_KEY_HERE',
384
+ 'Content-Type': 'application/json'
385
+ },
386
+ body: JSON.stringify({
387
+ model: 'grok-4',
388
+ messages: [
389
+ ...this.conversation,
390
+ { role: 'user', content: message }
391
+ ],
392
+ temperature: 0.7,
393
+ max_tokens: 500
394
+ })
395
+ });
396
+
397
+ const data = await response.json();
398
+ return data.choices[0].message.content;
399
+ */
400
+ }
401
+ }
402
+
403
+ customElements.define('ai-agent', AIAgent);
index.html CHANGED
@@ -190,15 +190,17 @@
190
  </div>
191
  </div>
192
  </div>
193
-
194
  <custom-footer></custom-footer>
195
 
 
 
196
  <script src="components/navbar.js"></script>
197
  <script src="components/footer.js"></script>
 
198
  <script src="script.js"></script>
199
  <script>
200
  feather.replace();
201
  </script>
202
  <script src="https://huggingface.co/deepsite/deepsite-badge.js"></script>
203
  </body>
204
- </html>
 
190
  </div>
191
  </div>
192
  </div>
 
193
  <custom-footer></custom-footer>
194
 
195
+ <ai-agent></ai-agent>
196
+
197
  <script src="components/navbar.js"></script>
198
  <script src="components/footer.js"></script>
199
+ <script src="components/ai-agent.js"></script>
200
  <script src="script.js"></script>
201
  <script>
202
  feather.replace();
203
  </script>
204
  <script src="https://huggingface.co/deepsite/deepsite-badge.js"></script>
205
  </body>
206
+ </html>