Spaces:
Paused
Paused
Update scripts/chat.js
Browse files12346 unmodified lines111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364// scripts/chat.jsexport function initChat() {const sendBtn = document.getElementById('send-btn');6 unmodified lines}}// In scripts/chat.jsasync function sendMessage() {const userInput = document.getElementById('user-input');const chatMessages = document.getElementById('chat-messages');const userMessageText = userInput.value.trim();if (userMessageText === '') return;// Create and add the user's message bubbleconst userBubble = document.createElement('div');userBubble.className = 'chat-bubble user-bubble p-4 fade-in';userBubble.innerHTML = `<p>${userMessageText}</p>`;chatMessages.appendChild(userBubble);userInput.value = '';chatMessages.scrollTop = chatMessages.scrollHeight;// Create the AI's response bubbleconst aiBubble = document.createElement('div');aiBubble.className = 'chat-bubble ai-bubble p-4 fade-in';aiBubble.innerHTML = '<div class="typing-indicator"><span></span><span></span><span></span></div>';chatMessages.appendChild(aiBubble);chatMessages.scrollTop = chatMessages.scrollHeight;// --- THIS IS THE CRUCIAL UPDATE ---// This now points to your new worker's chat endpoint.const workerUrl = 'https://streamai-backend-v2.smplushypermedia.workers.dev/api/chat';try {const response = await fetch(workerUrl, {method: 'POST',headers: { 'Content-Type': 'application/json' },body: JSON.stringify({ prompt: userMessageText }),});if (!response.ok) throw new Error(`Network response was not ok. Status: ${response.status}`);// Handle the streaming response from Geminiconst reader = response.body.getReader();const decoder = new TextDecoder();aiBubble.innerHTML = ''; // Clear the typing indicatorconst aiParagraph = document.createElement('p');aiBubble.appendChild(aiParagraph);while (true) {const { done, value } = await reader.read();if (done) break;aiParagraph.textContent += decoder.decode(value, { stream: true });chatMessages.scrollTop = chatMessages.scrollHeight;}} catch (error) {aiBubble.innerHTML = `<p class="text-red-400">Error: Could not connect to the AI assistant. ${error.message}</p>`;}1234567891011126 unmodified lines19202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879// scripts/chat.js -- Final version with conversation history// This array will store the entire conversation history.let conversationHistory = [{role: 'system',content: 'You are a friendly and helpful streaming assistant for an app called StreamAI. A user is asking a question. Keep your responses concise and focused on recommending movies, TV shows, or streaming content.'}];export function initChat() {const sendBtn = document.getElementById('send-btn');6 unmodified lines}}async function sendMessage() {const userInput = document.getElementById('user-input');const chatMessages = document.getElementById('chat-messages');const userMessageText = userInput.value.trim();if (userMessageText === '') return;// Add user message to the UIconst userBubble = document.createElement('div');userBubble.className = 'chat-bubble user-bubble p-4 fade-in';userBubble.innerHTML = `<p>${userMessageText}</p>`;chatMessages.appendChild(userBubble);// Add user message to our history arrayconversationHistory.push({ role: 'user', content: userMessageText });userInput.value = '';chatMessages.scrollTop = chatMessages.scrollHeight;// Add a placeholder for the AI's responseconst aiBubble = document.createElement('div');aiBubble.className = 'chat-bubble ai-bubble p-4 fade-in';aiBubble.innerHTML = '<div class="typing-indicator"><span></span><span></span><span></span></div>';chatMessages.appendChild(aiBubble);chatMessages.scrollTop = chatMessages.scrollHeight;const workerUrl = 'https://streamai-backend-v2.smplushypermedia.workers.dev/api/chat';try {const response = await fetch(workerUrl, {method: 'POST',headers: { 'Content-Type': 'application/json' },// Send the ENTIRE conversation history to the workerbody: JSON.stringify({ messages: conversationHistory }),});if (!response.ok) throw new Error(`Network response was not ok. Status: ${response.status}`);const reader = response.body.getReader();const decoder = new TextDecoder();aiBubble.innerHTML = ''; // Clear the typing indicatorconst aiParagraph = document.createElement('p');aiBubble.appendChild(aiParagraph);let aiResponseText = "";while (true) {const { done, value } = await reader.read();if (done) break;const chunk = decoder.decode(value, { stream: true });aiResponseText += chunk;aiParagraph.textContent = aiResponseText; // Update the UI as the stream comes inchatMessages.scrollTop = chatMessages.scrollHeight;}// Add the final, complete AI response to our historyconversationHistory.push({ role: 'model', content: aiResponseText });} catch (error) {aiBubble.innerHTML = `<p class="text-red-400">Error: Could not connect to the AI assistant. ${error.message}</p>`;}
scripts/chat.js+26-11