Spaces:
Running
Running
Update scripts/chat.js
Browse files- scripts/chat.js +39 -15
scripts/chat.js
CHANGED
@@ -2,17 +2,38 @@
|
|
2 |
|
3 |
export function initChat() {
|
4 |
const sendBtn = document.getElementById('send-btn');
|
5 |
-
|
|
|
6 |
sendBtn.addEventListener('click', sendMessage);
|
|
|
|
|
|
|
7 |
}
|
8 |
}
|
9 |
|
10 |
async function sendMessage() {
|
11 |
const userInput = document.getElementById('user-input');
|
12 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
//
|
15 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
try {
|
18 |
const response = await fetch(workerUrl, {
|
@@ -20,19 +41,22 @@ async function sendMessage() {
|
|
20 |
headers: { 'Content-Type': 'application/json' },
|
21 |
body: JSON.stringify({ prompt: userMessageText }),
|
22 |
});
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
const
|
29 |
-
|
30 |
-
const aiBubble = document.createElement('div');
|
31 |
-
aiBubble.className = 'chat-bubble ai-bubble';
|
32 |
-
aiBubble.innerHTML = `<p>${aiResponseText}</p>`;
|
33 |
-
chatMessages.appendChild(aiBubble);
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
} catch (error) {
|
36 |
-
|
37 |
}
|
38 |
}
|
|
|
2 |
|
3 |
export function initChat() {
|
4 |
const sendBtn = document.getElementById('send-btn');
|
5 |
+
const userInput = document.getElementById('user-input');
|
6 |
+
if (sendBtn && userInput) {
|
7 |
sendBtn.addEventListener('click', sendMessage);
|
8 |
+
userInput.addEventListener('keypress', (e) => {
|
9 |
+
if (e.key === 'Enter') sendMessage();
|
10 |
+
});
|
11 |
}
|
12 |
}
|
13 |
|
14 |
async function sendMessage() {
|
15 |
const userInput = document.getElementById('user-input');
|
16 |
+
const chatMessages = document.getElementById('chat-messages');
|
17 |
+
const userMessageText = userInput.value.trim();
|
18 |
+
if (userMessageText === '') return;
|
19 |
+
|
20 |
+
// 1. Add styled user bubble
|
21 |
+
const userBubble = document.createElement('div');
|
22 |
+
userBubble.className = 'chat-bubble user-bubble p-4 fade-in';
|
23 |
+
userBubble.innerHTML = `<p>${userMessageText}</p>`;
|
24 |
+
chatMessages.appendChild(userBubble);
|
25 |
+
userInput.value = '';
|
26 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
27 |
|
28 |
+
// 2. Add styled AI bubble with a typing indicator
|
29 |
+
const aiBubble = document.createElement('div');
|
30 |
+
aiBubble.className = 'chat-bubble ai-bubble p-4 fade-in';
|
31 |
+
aiBubble.innerHTML = '<div class="typing-indicator"><span></span><span></span><span></span></div>';
|
32 |
+
chatMessages.appendChild(aiBubble);
|
33 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
34 |
+
|
35 |
+
// 3. Fetch the real response
|
36 |
+
const workerUrl = 'https://YOUR-WORKER-NAME.workers.dev'; // <-- Make sure your URL is here
|
37 |
|
38 |
try {
|
39 |
const response = await fetch(workerUrl, {
|
|
|
41 |
headers: { 'Content-Type': 'application/json' },
|
42 |
body: JSON.stringify({ prompt: userMessageText }),
|
43 |
});
|
44 |
+
if (!response.ok) throw new Error(`Network response was not ok. Status: ${response.status}`);
|
45 |
|
46 |
+
// 4. Handle streaming response
|
47 |
+
const reader = response.body.getReader();
|
48 |
+
const decoder = new TextDecoder();
|
49 |
+
aiBubble.innerHTML = ''; // Clear the typing indicator
|
50 |
+
const aiParagraph = document.createElement('p');
|
51 |
+
aiBubble.appendChild(aiParagraph);
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
while (true) {
|
54 |
+
const { done, value } = await reader.read();
|
55 |
+
if (done) break;
|
56 |
+
aiParagraph.textContent += decoder.decode(value, { stream: true });
|
57 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
58 |
+
}
|
59 |
} catch (error) {
|
60 |
+
aiBubble.innerHTML = `<p class="text-red-400">Error: Could not connect to the AI assistant. ${error.message}</p>`;
|
61 |
}
|
62 |
}
|