Spaces:
Running
Running
| // scripts/chat.js | |
| export function initChat() { | |
| const sendBtn = document.getElementById('send-btn'); | |
| if (sendBtn) { | |
| sendBtn.addEventListener('click', sendMessage); | |
| } | |
| } | |
| async function sendMessage() { | |
| const userInput = document.getElementById('user-input'); | |
| const userMessageText = userInput.value; | |
| // IMPORTANT: Replace this with your actual Cloudflare Worker URL | |
| const workerUrl = 'https://stream-ai-backend.smplushypermedia.workers.dev/'; | |
| 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}`); | |
| } | |
| const aiResponseText = await response.text(); | |
| const chatMessages = document.getElementById('chat-messages'); | |
| const aiBubble = document.createElement('div'); | |
| aiBubble.className = 'chat-bubble ai-bubble'; | |
| aiBubble.innerHTML = `<p>${aiResponseText}</p>`; | |
| chatMessages.appendChild(aiBubble); | |
| } catch (error) { | |
| alert(`AI FETCH FAILED: ${error.message}`); | |
| } | |
| } |