privateuserh commited on
Commit
8aa84ea
·
verified ·
1 Parent(s): cd8ef8b

Update scripts/chat.js

Browse files
Files changed (1) hide show
  1. 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 = getElement('user-input');
7
- const chatMessages = getElement('chat-messages');
8
 
9
  if (userInput.value.trim() === '') return;
10
 
11
- // ... (keep the complete sendMessage logic from the previous response here)
12
- // ... (This includes adding user message, creating AI bubble, and the fetch call)
13
- }
14
-
15
- function quickPrompt(prompt) {
16
- getElement('user-input').value = prompt;
17
- sendMessage();
18
- }
19
-
20
- function updateRecommendationsFromAI(aiResponse) {
21
- // This is a placeholder. You can enhance this logic.
22
- let filter = 'all';
23
- if (aiResponse.includes('comedy')) filter = 'comedy';
24
- else if (aiResponse.includes('thriller')) filter = 'thriller';
25
- else if (aiResponse.includes('romance')) filter = 'romance';
26
-
27
- // In a truly modular system, this would emit an event that ui.js listens for.
28
- // For simplicity, we'll call a function defined in the global scope (see app.js).
29
- window.loadRecommendations(filter);
30
- }
31
-
32
-
33
- export function initChat() {
34
- getElement('send-btn').addEventListener('click', sendMessage);
35
- getElement('user-input').addEventListener('keypress', (e) => {
36
- if (e.key === 'Enter') {
37
- sendMessage();
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  }
39
- });
40
 
41
- // Make quickPrompt globally accessible for the inline HTML onclick attributes
42
- window.quickPrompt = quickPrompt;
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  }