Spaces:
Running
Running
Update scripts/chat.js
Browse files- scripts/chat.js +6 -10
scripts/chat.js
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
-
// scripts/chat.js --
|
2 |
|
3 |
-
// This array will store the
|
4 |
let conversationHistory = [
|
5 |
{
|
6 |
role: 'system',
|
7 |
-
content: 'You are a friendly and helpful streaming assistant for an app called StreamAI.
|
8 |
}
|
9 |
];
|
10 |
|
@@ -25,39 +25,36 @@ async function sendMessage() {
|
|
25 |
const userMessageText = userInput.value.trim();
|
26 |
if (userMessageText === '') return;
|
27 |
|
28 |
-
// Add user message to the UI
|
29 |
const userBubble = document.createElement('div');
|
30 |
userBubble.className = 'chat-bubble user-bubble p-4 fade-in';
|
31 |
userBubble.innerHTML = `<p>${userMessageText}</p>`;
|
32 |
chatMessages.appendChild(userBubble);
|
33 |
|
34 |
-
// Add user message to our history array
|
35 |
conversationHistory.push({ role: 'user', content: userMessageText });
|
36 |
|
37 |
userInput.value = '';
|
38 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
39 |
|
40 |
-
// Add a placeholder for the AI's response
|
41 |
const aiBubble = document.createElement('div');
|
42 |
aiBubble.className = 'chat-bubble ai-bubble p-4 fade-in';
|
43 |
aiBubble.innerHTML = '<div class="typing-indicator"><span></span><span></span><span></span></div>';
|
44 |
chatMessages.appendChild(aiBubble);
|
45 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
46 |
|
|
|
47 |
const workerUrl = 'https://streamai-backend-v2.smplushypermedia.workers.dev/api/chat';
|
48 |
|
49 |
try {
|
50 |
const response = await fetch(workerUrl, {
|
51 |
method: 'POST',
|
52 |
headers: { 'Content-Type': 'application/json' },
|
53 |
-
// Send the ENTIRE conversation history to the worker
|
54 |
body: JSON.stringify({ messages: conversationHistory }),
|
55 |
});
|
56 |
if (!response.ok) throw new Error(`Network response was not ok. Status: ${response.status}`);
|
57 |
|
58 |
const reader = response.body.getReader();
|
59 |
const decoder = new TextDecoder();
|
60 |
-
aiBubble.innerHTML = '';
|
61 |
const aiParagraph = document.createElement('p');
|
62 |
aiBubble.appendChild(aiParagraph);
|
63 |
|
@@ -67,11 +64,10 @@ async function sendMessage() {
|
|
67 |
if (done) break;
|
68 |
const chunk = decoder.decode(value, { stream: true });
|
69 |
aiResponseText += chunk;
|
70 |
-
aiParagraph.textContent = aiResponseText;
|
71 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
72 |
}
|
73 |
|
74 |
-
// Add the final, complete AI response to our history
|
75 |
conversationHistory.push({ role: 'model', content: aiResponseText });
|
76 |
|
77 |
} catch (error) {
|
|
|
1 |
+
// scripts/chat.js -- FINAL VERSION
|
2 |
|
3 |
+
// This array will store the conversation history.
|
4 |
let conversationHistory = [
|
5 |
{
|
6 |
role: 'system',
|
7 |
+
content: 'You are a friendly and helpful streaming assistant for an app called StreamAI. Keep your responses concise and focused on recommending movies, TV shows, or streaming content.'
|
8 |
}
|
9 |
];
|
10 |
|
|
|
25 |
const userMessageText = userInput.value.trim();
|
26 |
if (userMessageText === '') return;
|
27 |
|
|
|
28 |
const userBubble = document.createElement('div');
|
29 |
userBubble.className = 'chat-bubble user-bubble p-4 fade-in';
|
30 |
userBubble.innerHTML = `<p>${userMessageText}</p>`;
|
31 |
chatMessages.appendChild(userBubble);
|
32 |
|
|
|
33 |
conversationHistory.push({ role: 'user', content: userMessageText });
|
34 |
|
35 |
userInput.value = '';
|
36 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
37 |
|
|
|
38 |
const aiBubble = document.createElement('div');
|
39 |
aiBubble.className = 'chat-bubble ai-bubble p-4 fade-in';
|
40 |
aiBubble.innerHTML = '<div class="typing-indicator"><span></span><span></span><span></span></div>';
|
41 |
chatMessages.appendChild(aiBubble);
|
42 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
43 |
|
44 |
+
// This points to the correct, working backend URL
|
45 |
const workerUrl = 'https://streamai-backend-v2.smplushypermedia.workers.dev/api/chat';
|
46 |
|
47 |
try {
|
48 |
const response = await fetch(workerUrl, {
|
49 |
method: 'POST',
|
50 |
headers: { 'Content-Type': 'application/json' },
|
|
|
51 |
body: JSON.stringify({ messages: conversationHistory }),
|
52 |
});
|
53 |
if (!response.ok) throw new Error(`Network response was not ok. Status: ${response.status}`);
|
54 |
|
55 |
const reader = response.body.getReader();
|
56 |
const decoder = new TextDecoder();
|
57 |
+
aiBubble.innerHTML = '';
|
58 |
const aiParagraph = document.createElement('p');
|
59 |
aiBubble.appendChild(aiParagraph);
|
60 |
|
|
|
64 |
if (done) break;
|
65 |
const chunk = decoder.decode(value, { stream: true });
|
66 |
aiResponseText += chunk;
|
67 |
+
aiParagraph.textContent = aiResponseText;
|
68 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
69 |
}
|
70 |
|
|
|
71 |
conversationHistory.push({ role: 'model', content: aiResponseText });
|
72 |
|
73 |
} catch (error) {
|