ChatLLM / script.js
VikaskumarDas's picture
Update script.js
c71e464 verified
Raw
History Blame Contribute Delete
4.83 kB
let conversations = JSON.parse(localStorage.getItem('gemini_history')) || {};
let currentChatId = null;
function toggleSidebar() {
const sidebar = document.getElementById('sidebar');
sidebar.classList.toggle('closed');
}
function renderHistory() {
const list = document.getElementById('historyList');
list.innerHTML = '';
Object.keys(conversations).sort((a,b) => b-a).forEach(id => {
const item = document.createElement('div');
item.className = 'history-item';
if(id === currentChatId) item.style.backgroundColor = "#282a2c";
item.innerHTML = `
<span onclick="loadChat('${id}')" style="flex:1; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">${conversations[id].title}</span>
<button class="delete-chat-btn" onclick="deleteChat('${id}', event)">✕</button>
`;
list.appendChild(item);
});
}
function startNewChat() {
currentChatId = 'chat_' + Date.now();
conversations[currentChatId] = { title: "New Chat", messages: [] };
saveToLocalStorage();
renderHistory();
document.getElementById('welcomeScreen').style.display = 'block';
document.getElementById('chatMessages').style.display = 'none';
document.getElementById('chatMessages').innerHTML = '';
if(window.innerWidth <= 768) toggleSidebar();
}
function loadChat(id) {
currentChatId = id;
renderHistory();
document.getElementById('welcomeScreen').style.display = 'none';
const msgDiv = document.getElementById('chatMessages');
msgDiv.style.display = 'flex';
msgDiv.innerHTML = '';
conversations[id].messages.forEach(msg => {
appendBubble(msg.role, msg.text);
});
if(window.innerWidth <= 768) toggleSidebar();
}
function deleteChat(id, event) {
event.stopPropagation();
delete conversations[id];
saveToLocalStorage();
renderHistory();
if (currentChatId === id) {
currentChatId = null;
document.getElementById('welcomeScreen').style.display = 'block';
document.getElementById('chatMessages').style.display = 'none';
}
}
function appendBubble(role, text) {
const container = document.getElementById('chatMessages');
const bubble = document.createElement('div');
bubble.className = `chat-bubble ${role}`;
const avatar = role === 'user' ? '👤' : '✨';
bubble.innerHTML = `
<div class="bubble-avatar">${avatar}</div>
<div class="bubble-text">${text}</div>
`;
container.appendChild(bubble);
const chatContainer = document.getElementById('chatContainer');
chatContainer.scrollTop = chatContainer.scrollHeight;
}
function useSuggestion(text) {
document.getElementById('userInput').value = text;
sendMessage();
}
// এই ফাংশনটি FastAPI-র সাথে কানেক্ট করার জন্য আপডেট করা হলো
async function sendMessage() {
const input = document.getElementById('userInput');
const text = input.value.trim();
if (!text) return;
if (!currentChatId) {
currentChatId = 'chat_' + Date.now();
conversations[currentChatId] = { title: text.substring(0, 24), messages: [] };
}
document.getElementById('welcomeScreen').style.display = 'none';
document.getElementById('chatMessages').style.display = 'flex';
appendBubble('user', text);
conversations[currentChatId].messages.push({ role: 'user', text: text });
if(conversations[currentChatId].title === "New Chat") {
conversations[currentChatId].title = text.substring(0, 24);
}
input.value = '';
saveToLocalStorage();
renderHistory();
appendBubble('bot', 'Thinking...');
const lastBubble = document.getElementById('chatMessages').lastChild.querySelector('.bubble-text');
try {
// নতুন FastAPI এন্ডপয়েন্ট কল করা হচ্ছে
const response = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: text })
});
const resJson = await response.json();
const reply = resJson.reply;
lastBubble.innerText = reply;
conversations[currentChatId].messages.push({ role: 'bot', text: reply });
saveToLocalStorage();
} catch (e) {
lastBubble.innerText = "Error: Could not connect to Gemini backend.";
}
}
function handleKeyPress(e) {
if (e.key === 'Enter') sendMessage();
}
function saveToLocalStorage() {
localStorage.setItem('gemini_history', JSON.stringify(conversations));
}
setTimeout(() => {
renderHistory();
if(window.innerWidth <= 768) document.getElementById('sidebar').classList.add('closed');
}, 300);