Spaces:
Running
Running
Update scripts/chat.js
Browse files- scripts/chat.js +43 -0
scripts/chat.js
CHANGED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
}
|