File size: 1,312 Bytes
7fafb5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98a0172
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59


// Front-end JavaSscript - app.js

console.log("🔥 app.js loaded")

async function chat(){
    console.log("🚀 chat triggered")

    const res = await fetch("http://localhost:8000/chat",{
        method:"POST",
        headers:{"Content-Type":"application/json"},
        body:JSON.stringify({
            session_id:"test123",
            message:"hello"
        })
    })

    const data = await res.json()
    console.log("✅ RESPONSE:", data)
}


function removeLastMessage(){
    const thinking = document.getElementById("thinking")
    if(thinking) thinking.remove()
}

// Chat Renderer

function appendMessage(role, text){

const chatBox = document.getElementById("chat")

const msg = document.createElement("div")

msg.className = role === "user" ? "user-msg" : "ai-msg"

msg.innerText = text

chatBox.appendChild(msg)

chatBox.scrollTop = chatBox.scrollHeight
}

// End session

async function endSession(){
    await fetch("http://localhost:8000/chat",{
        method:"POST",
        headers:{"Content-Type":"application/json"},
        body:JSON.stringify({
            session_id: window.session_id,
            message: "end session"
        })
    })
    window.session_id = null
    document.getElementById("chat").innerHTML = ""
}