Spaces:
Running
Running
Update scripts/chat.js
Browse files- scripts/chat.js +9 -6
scripts/chat.js
CHANGED
@@ -11,13 +11,15 @@ export function initChat() {
|
|
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 |
-
//
|
21 |
const userBubble = document.createElement('div');
|
22 |
userBubble.className = 'chat-bubble user-bubble p-4 fade-in';
|
23 |
userBubble.innerHTML = `<p>${userMessageText}</p>`;
|
@@ -25,15 +27,16 @@ async function sendMessage() {
|
|
25 |
userInput.value = '';
|
26 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
27 |
|
28 |
-
//
|
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 |
-
//
|
36 |
-
|
|
|
37 |
|
38 |
try {
|
39 |
const response = await fetch(workerUrl, {
|
@@ -43,7 +46,7 @@ async function sendMessage() {
|
|
43 |
});
|
44 |
if (!response.ok) throw new Error(`Network response was not ok. Status: ${response.status}`);
|
45 |
|
46 |
-
//
|
47 |
const reader = response.body.getReader();
|
48 |
const decoder = new TextDecoder();
|
49 |
aiBubble.innerHTML = ''; // Clear the typing indicator
|
|
|
11 |
}
|
12 |
}
|
13 |
|
14 |
+
// In scripts/chat.js
|
15 |
+
|
16 |
async function sendMessage() {
|
17 |
const userInput = document.getElementById('user-input');
|
18 |
const chatMessages = document.getElementById('chat-messages');
|
19 |
const userMessageText = userInput.value.trim();
|
20 |
if (userMessageText === '') return;
|
21 |
|
22 |
+
// Create and add the user's message bubble
|
23 |
const userBubble = document.createElement('div');
|
24 |
userBubble.className = 'chat-bubble user-bubble p-4 fade-in';
|
25 |
userBubble.innerHTML = `<p>${userMessageText}</p>`;
|
|
|
27 |
userInput.value = '';
|
28 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
29 |
|
30 |
+
// Create the AI's response bubble
|
31 |
const aiBubble = document.createElement('div');
|
32 |
aiBubble.className = 'chat-bubble ai-bubble p-4 fade-in';
|
33 |
aiBubble.innerHTML = '<div class="typing-indicator"><span></span><span></span><span></span></div>';
|
34 |
chatMessages.appendChild(aiBubble);
|
35 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
36 |
+
|
37 |
+
// --- THIS IS THE CRUCIAL UPDATE ---
|
38 |
+
// This now points to your new worker's chat endpoint.
|
39 |
+
const workerUrl = 'https://streamai-backend-v2.smplushypermedia.workers.dev/api/chat';
|
40 |
|
41 |
try {
|
42 |
const response = await fetch(workerUrl, {
|
|
|
46 |
});
|
47 |
if (!response.ok) throw new Error(`Network response was not ok. Status: ${response.status}`);
|
48 |
|
49 |
+
// Handle the streaming response from Gemini
|
50 |
const reader = response.body.getReader();
|
51 |
const decoder = new TextDecoder();
|
52 |
aiBubble.innerHTML = ''; // Clear the typing indicator
|