Spaces:
Running
Running
Update scripts/chat.js
Browse files- scripts/chat.js +58 -34
scripts/chat.js
CHANGED
@@ -1,43 +1,67 @@
|
|
1 |
// scripts/chat.js
|
2 |
|
3 |
-
const getElement = (id) => document.getElementById(id);
|
4 |
-
|
5 |
async function sendMessage() {
|
6 |
-
const userInput =
|
7 |
-
const chatMessages =
|
8 |
|
9 |
if (userInput.value.trim() === '') return;
|
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 |
}
|
|
|
1 |
// scripts/chat.js
|
2 |
|
|
|
|
|
3 |
async function sendMessage() {
|
4 |
+
const userInput = document.getElementById('user-input');
|
5 |
+
const chatMessages = document.getElementById('chat-messages');
|
6 |
|
7 |
if (userInput.value.trim() === '') return;
|
8 |
|
9 |
+
// ... (rest of the code to add user message to chat) ...
|
10 |
+
const userMessage = document.createElement('div');
|
11 |
+
userMessage.className = 'chat-bubble user-bubble p-4 w-3/4 ml-auto mb-4 fade-in';
|
12 |
+
userMessage.innerHTML = `<p>${userInput.value}</p>`;
|
13 |
+
chatMessages.appendChild(userMessage);
|
14 |
+
|
15 |
+
const userMessageText = userInput.value;
|
16 |
+
userInput.value = '';
|
17 |
+
|
18 |
+
// --- Start Debugging ---
|
19 |
+
window.logToScreen('sendMessage triggered. Preparing to fetch...');
|
20 |
+
|
21 |
+
try {
|
22 |
+
const workerUrl = 'https://YOUR-WORKER-NAME.workers.dev'; // <-- Make sure this URL is correct!
|
23 |
+
window.logToScreen(`Fetching from: ${workerUrl}`);
|
24 |
+
|
25 |
+
const response = await fetch(workerUrl, {
|
26 |
+
method: 'POST',
|
27 |
+
headers: { 'Content-Type': 'application/json' },
|
28 |
+
body: JSON.stringify({ prompt: userMessageText }),
|
29 |
+
});
|
30 |
+
|
31 |
+
window.logToScreen(`Fetch response received. Status: ${response.status} ${response.statusText}`);
|
32 |
+
|
33 |
+
if (!response.ok) {
|
34 |
+
window.logToScreen('Response was not OK. See status above.');
|
35 |
+
const errorText = await response.text();
|
36 |
+
window.logToScreen(`Error response body: ${errorText}`);
|
37 |
+
throw new Error(`Network response was not ok: ${response.statusText}`);
|
38 |
+
}
|
39 |
+
|
40 |
+
// ... (rest of the code to handle the streaming response) ...
|
41 |
+
const reader = response.body.getReader();
|
42 |
+
const decoder = new TextDecoder();
|
43 |
+
let aiResponseText = '';
|
44 |
+
while (true) {
|
45 |
+
const { done, value } = await reader.read();
|
46 |
+
if (done) break;
|
47 |
+
const chunk = decoder.decode(value, { stream: true });
|
48 |
+
aiResponseText += chunk;
|
49 |
}
|
50 |
+
window.logToScreen('Stream finished.');
|
51 |
|
52 |
+
// Add final AI response to chat
|
53 |
+
const aiMessage = document.createElement('div');
|
54 |
+
aiMessage.className = 'chat-bubble ai-bubble p-4 w-3/4 fade-in';
|
55 |
+
aiMessage.innerHTML = `<p>${aiResponseText}</p>`;
|
56 |
+
chatMessages.appendChild(aiMessage);
|
57 |
+
|
58 |
+
} catch (error) {
|
59 |
+
window.logToScreen(`FETCH FAILED: ${error.message}`);
|
60 |
+
// Display a user-friendly error in the chat
|
61 |
+
const errorMessage = document.createElement('div');
|
62 |
+
errorMessage.className = 'chat-bubble ai-bubble p-4 w-3/4 fade-in';
|
63 |
+
errorMessage.innerHTML = `<p class="text-red-500">Sorry, an error occurred. Check the debug console for details.</p>`;
|
64 |
+
chatMessages.appendChild(errorMessage);
|
65 |
+
}
|
66 |
+
// --- End Debugging ---
|
67 |
}
|