File size: 1,258 Bytes
3f7a9ed
 
 
 
 
 
c564bcb
 
703af34
 
8aa84ea
3f7a9ed
8aa84ea
3f7a9ed
 
8aa84ea
 
 
 
3f7a9ed
8aa84ea
 
 
 
3f7a9ed
8aa84ea
 
3f7a9ed
 
 
 
 
 
c564bcb
8aa84ea
3f7a9ed
8aa84ea
703af34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 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}`);
    }
}