Merge remote-tracking branch 'origin/feature/chatbot-sidebar' into fix/sync-task-assignment

This commit is contained in:
arifal
2025-03-06 11:45:34 +07:00
5 changed files with 198 additions and 150 deletions

View File

@@ -10,6 +10,7 @@ document.addEventListener("DOMContentLoaded", function () {
const textarea = document.getElementById("user-message");
const sendButton = document.getElementById("send");
const conversationArea = document.querySelector(".row.flex-grow");
const chatHistory = [];
// Fungsi untuk mengirim pesan
async function sendMessage() {
@@ -30,7 +31,7 @@ document.addEventListener("DOMContentLoaded", function () {
}
// Panggil API untuk mendapatkan respons dari bot
const botResponse = await getBotResponse(userText);
const botResponse = await getBotResponse(userText, chatHistory);
// Perbarui pesan bot dengan respons yang sebenarnya
if (messageTextContainer) {
@@ -124,12 +125,12 @@ document.addEventListener("DOMContentLoaded", function () {
}
// Fungsi untuk memanggil API
async function getBotResponse(userText) {
async function getBotResponse(userText, historyChat) {
try {
const url = `${GlobalConfig.apiHost}/api/main-generate-text`;
const response = await fetch(url, {
method: "POST",
body: JSON.stringify({prompt: userText }),
body: JSON.stringify({prompt: userText, chatHistory: historyChat}),
headers: {
Authorization: `Bearer ${document
.querySelector('meta[name="api-token"]')
@@ -139,6 +140,12 @@ document.addEventListener("DOMContentLoaded", function () {
});
const data = await response.json();
const rawBotResponse = data.nlpResponse;
// Tambahkan ke chatHistory
chatHistory.push({
user: userText,
rawBotResponse: rawBotResponse,
});
return data.response || "Maaf, saya tidak mengerti.";
} catch (error) {
console.error("Error fetching bot response:", error);

View File

@@ -32,6 +32,32 @@ document.addEventListener("DOMContentLoaded", function () {
setTimeout(() => {
const tab_active = getActiveTabId();
console.log("Active Tab ID:", tab_active);
// Hapus semua chat kecuali pesan default bot
conversationArea.innerHTML = `
<div class="row flex-grow overflow-auto align-items-start">
<!-- Avatar -->
<div class="col-auto alignpe-0">
<img class="rounded-circle" width="45" src="/images/iconchatbot.jpeg" alt="avatar-3">
</div>
<!-- Nama dan Bubble Chat -->
<div class="col-9 w-auto">
<!-- Nama Bot -->
<p class="fw-bolder mb-1">Neng Bedas</p>
<!-- Bubble Chat -->
<div class="bot-response p-2 bg-light rounded mb-2 d-inline-block">
<p class="mb-0">Halo! Ada yang bisa saya bantu?</p>
<!-- Waktu (Tetap di Dalam Bubble Chat) -->
<div class="sending-message-time text-end mt-1">
<p class="text-muted small mb-0">Now</p>
</div>
</div>
</div>
</div>
`;
}, 100); // Timeout untuk memastikan class `active` sudah diperbarui
});
});
@@ -39,6 +65,7 @@ document.addEventListener("DOMContentLoaded", function () {
const textarea = document.getElementById("user-message");
const sendButton = document.getElementById("send");
const conversationArea = document.querySelector(".row.flex-grow");
const chatHistory = [];
// Fungsi untuk mengirim pesan
async function sendMessage() {
@@ -62,7 +89,7 @@ document.addEventListener("DOMContentLoaded", function () {
}
// Panggil API untuk mendapatkan respons dari bot
const botResponse = await getBotResponse(currentTab, userText);
const botResponse = await getBotResponse(currentTab, userText, chatHistory);
// Perbarui pesan bot dengan respons yang sebenarnya
if (messageTextContainer) {
@@ -156,12 +183,12 @@ document.addEventListener("DOMContentLoaded", function () {
}
// Fungsi untuk memanggil API
async function getBotResponse(tab_active, userText) {
async function getBotResponse(tab_active, userText, historyChat) {
try {
const url = `${GlobalConfig.apiHost}/api/generate-text`;
const response = await fetch(url, {
method: "POST",
body: JSON.stringify({tab_active:tab_active, prompt: userText }),
body: JSON.stringify({tab_active:tab_active, prompt: userText, chatHistory: historyChat }),
headers: {
Authorization: `Bearer ${document
.querySelector('meta[name="api-token"]')
@@ -171,6 +198,12 @@ document.addEventListener("DOMContentLoaded", function () {
});
const data = await response.json();
const rawBotResponse = data.nlpResponse;
// Tambahkan ke chatHistory
chatHistory.push({
user: userText,
rawBotResponse: rawBotResponse,
});
return data.response || "Maaf, saya tidak mengerti.";
} catch (error) {
console.error("Error fetching bot response:", error);