Spaces:
Sleeping

doluong6 commited on
Commit
3caecef
·
verified ·
1 Parent(s): e304ed6

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +70 -0
static/script.js CHANGED
@@ -179,6 +179,10 @@ function chonMuc(muc) {
179
  document.getElementById("taikemtheo").style.display = "block";
180
  taiDanhSachThuMuc();
181
  }
 
 
 
 
182
  }
183
 
184
 
@@ -1438,3 +1442,69 @@ document.querySelectorAll(".submenu a").forEach(a => {
1438
  });
1439
  });
1440
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  document.getElementById("taikemtheo").style.display = "block";
180
  taiDanhSachThuMuc();
181
  }
182
+ if (muc === "chatbot") {
183
+ const section = document.getElementById("chatbot");
184
+ if (section) section.style.display = "block";
185
+ }
186
  }
187
 
188
 
 
1442
  });
1443
  });
1444
 
1445
+ // ===== Chatbot client (GENERAL / RAG / ACTION) =====
1446
+ const CHAT_HISTORY = [];
1447
+ let sendingChat = false;
1448
+
1449
+ function appendBubble(text, role = "user"){
1450
+ const wrap = document.getElementById("chatWindow");
1451
+ const bubble = document.createElement("div");
1452
+ bubble.className = `chat-bubble ${role}`;
1453
+ bubble.textContent = text;
1454
+ wrap.appendChild(bubble);
1455
+ wrap.scrollTop = wrap.scrollHeight;
1456
+ return bubble;
1457
+ }
1458
+ function appendSources(sources){
1459
+ if (!sources || !sources.length) return;
1460
+ const wrap = document.getElementById("chatWindow");
1461
+ const meta = document.createElement("div");
1462
+ meta.className = "chat-meta";
1463
+ meta.textContent = "Nguồn:\n" + sources.map(s => "• " + s).join("\n");
1464
+ wrap.appendChild(meta);
1465
+ wrap.scrollTop = wrap.scrollHeight;
1466
+ }
1467
+
1468
+ async function sendChatSmart(){
1469
+ if (sendingChat) return;
1470
+ const ip = document.getElementById("chatInput");
1471
+ const btn = document.getElementById("chatSend");
1472
+ const text = (ip.value || "").trim();
1473
+ if (!text) return;
1474
+
1475
+ sendingChat = true; btn.disabled = true;
1476
+
1477
+ appendBubble(text, "user");
1478
+ CHAT_HISTORY.push({ role: "user", content: text });
1479
+ ip.value = "";
1480
+
1481
+ const thinking = appendBubble("...", "bot");
1482
+
1483
+ try{
1484
+ const r = await fetch(`${BACKEND_URL}/chat_smart`, {
1485
+ method: "POST",
1486
+ headers: { "Content-Type":"application/json" },
1487
+ body: JSON.stringify({ message: text, history: CHAT_HISTORY })
1488
+ });
1489
+ const data = await r.json();
1490
+ const intent = data.intent || "general";
1491
+ const reply = data.reply || "Xin lỗi, không có phản hồi.";
1492
+
1493
+ thinking.textContent = `[${intent}] ${reply}`;
1494
+ appendSources(data.sources);
1495
+ CHAT_HISTORY.push({ role: "assistant", content: reply });
1496
+
1497
+ }catch(err){
1498
+ console.error("Chatbot error:", err);
1499
+ thinking.textContent = "Lỗi kết nối backend.";
1500
+ }finally{
1501
+ sendingChat = false; btn.disabled = false;
1502
+ }
1503
+ }
1504
+
1505
+ document.addEventListener("DOMContentLoaded", () => {
1506
+ document.getElementById("chatSend")?.addEventListener("click", sendChatSmart);
1507
+ document.getElementById("chatInput")?.addEventListener("keydown", (e)=>{
1508
+ if (e.key === "Enter") sendChatSmart();
1509
+ });
1510
+ });