File size: 4,831 Bytes
7578bd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c71e464
7578bd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c71e464
 
7578bd6
 
c71e464
7578bd6
 
c71e464
7578bd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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);