File size: 8,520 Bytes
f2eba97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70e7c19
 
 
 
 
 
 
 
 
 
f2eba97
 
70e7c19
f2eba97
70e7c19
 
 
 
 
 
 
 
 
 
 
 
 
f2eba97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// --- State ---
let sessionId   = Math.random().toString(36).substring(2, 10);
let messages    = [];
let isWelcome   = true;
// Default is light-theme. isDark = true only if user explicitly chose dark.
let isDarkTheme = localStorage.getItem('theme') === 'dark';

// --- Selectors ---
const body            = document.body;
const themeToggle     = document.getElementById('theme-toggle');
const chatMessages    = document.getElementById('chat-messages');
const userInput       = document.getElementById('user-input');
const sendBtn         = document.getElementById('send-btn');
const welcomeScreen   = document.getElementById('welcome-screen');
const inputWrapper    = document.getElementById('input-container-wrapper');
const newChatBtn      = document.getElementById('new-chat-btn');
const statMessages    = document.getElementById('stat-messages');
const downloadBtn     = document.getElementById('download-chat-btn');
const mobileMenuBtn   = document.getElementById('mobile-menu-btn');
const sidebarOverlay  = document.getElementById('sidebar-overlay');
const sidebar         = document.querySelector('.sidebar');
const desktopToggle   = document.getElementById('desktop-sidebar-toggle');

// --- Init ---
function init() {
    // Apply saved theme preference
    if (isDarkTheme) {
        body.classList.remove('light-theme');
        body.classList.add('dark-theme');
        themeToggle.querySelector('i').setAttribute('data-lucide', 'sun');
    } else {
        body.classList.remove('dark-theme');
        body.classList.add('light-theme');
        themeToggle.querySelector('i').setAttribute('data-lucide', 'moon');
    }
    lucide.createIcons();

    if (window.marked) {
        marked.setOptions({ breaks: true, gfm: true, headerIds: false, mangle: false });
    }
}

// --- Theme toggle ---
themeToggle.addEventListener('click', () => {
    isDarkTheme = !isDarkTheme;
    if (isDarkTheme) {
        body.classList.remove('light-theme');
        body.classList.add('dark-theme');
        themeToggle.querySelector('i').setAttribute('data-lucide', 'sun');
    } else {
        body.classList.remove('dark-theme');
        body.classList.add('light-theme');
        themeToggle.querySelector('i').setAttribute('data-lucide', 'moon');
    }
    localStorage.setItem('theme', isDarkTheme ? 'dark' : 'light');
    lucide.createIcons();
});

// --- Sidebar open/close (same logic for desktop & mobile) ---
function openSidebar() {
    sidebar.classList.remove('collapsed');
    sidebar.classList.add('open');
    sidebarOverlay.classList.add('active');
}
function closeSidebar() {
    sidebar.classList.remove('open');
    sidebar.classList.add('collapsed');
    sidebarOverlay.classList.remove('active');
}

// Desktop hamburger toggle
if (desktopToggle) {
    desktopToggle.addEventListener('click', () => {
        sidebar.classList.contains('open') ? closeSidebar() : openSidebar();
    });
}

// Mobile hamburger
if (mobileMenuBtn) {
    mobileMenuBtn.addEventListener('click', openSidebar);
}

// Click overlay to close
if (sidebarOverlay) {
    sidebarOverlay.addEventListener('click', closeSidebar);
}

// --- Welcome transition ---
function exitWelcome() {
    if (!isWelcome) return;
    isWelcome = false;
    welcomeScreen.style.opacity    = '0';
    welcomeScreen.style.visibility = 'hidden';
    inputWrapper.classList.remove('welcome-mode');
}

// --- Input auto-resize ---
userInput.addEventListener('input', () => {
    userInput.style.height = 'auto';
    userInput.style.height = userInput.scrollHeight + 'px';
    sendBtn.disabled = userInput.value.trim() === '';
});

userInput.addEventListener('keydown', e => {
    if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); sendMessage(); }
});

sendBtn.addEventListener('click', sendMessage);

newChatBtn.addEventListener('click', () => {
    sessionId  = Math.random().toString(36).substring(2, 10);
    messages   = [];
    chatMessages.innerHTML = '';
    isWelcome  = true;
    welcomeScreen.style.opacity    = '1';
    welcomeScreen.style.visibility = 'visible';
    inputWrapper.classList.add('welcome-mode');
    statMessages.textContent = '0';
    downloadBtn.disabled     = true;
});

// --- Chat ---
async function sendMessage() {
    const text = userInput.value.trim();
    if (!text) return;

    exitWelcome();
    userInput.value      = '';
    userInput.style.height = 'auto';
    sendBtn.disabled     = true;

    addMessage('user', text);

    const typingId = 'typing-' + Date.now();
    addTypingIndicator(typingId);

    try {
        const response = await fetch('/api/chat', {
            method:  'POST',
            headers: { 'Content-Type': 'application/json' },
            body:    JSON.stringify({ message: text, session_id: sessionId }),
        });

        if (!response.ok) throw new Error('Network error');

        removeTypingIndicator(typingId);
        const msgId     = 'msg-' + Date.now();
        const contentEl = initAssistantMessage(msgId);

        const reader  = response.body.getReader();
        const decoder = new TextDecoder();
        let fullMarkdown = '';

        while (true) {
            const { value, done } = await reader.read();
            if (done) break;
            fullMarkdown += decoder.decode(value);
            const processed = processMarkdown(fullMarkdown);
            contentEl.innerHTML = window.marked ? marked.parse(processed) : processed;
            chatMessages.scrollTop = chatMessages.scrollHeight;
        }

        messages.push({ role: 'assistant', content: fullMarkdown });
        statMessages.textContent = Math.ceil(messages.length / 2);
        downloadBtn.disabled     = false;

    } catch (err) {
        console.error('Chat error:', err);
        removeTypingIndicator(typingId);
        addMessage('assistant', 'Sorry, I encountered an error. Please try again.');
    }
}

function processMarkdown(md) {
    // Just return as-is — marked.js handles everything correctly
    return md;
}

function addMessage(role, content) {
    const msgDiv = document.createElement('div');
    msgDiv.className = `message ${role}`;
    const processed = processMarkdown(content);
    msgDiv.innerHTML = `
        <div class="avatar">${role === 'user' ? 'You' : '<img src="/static/ai-avatar.png" alt="Kuldeep AI">'}</div>
        <div class="message-content">
            <div class="bubble">${role === 'user' ? processed : (window.marked ? marked.parse(processed) : processed)}</div>
            <div class="message-meta">${new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}</div>
        </div>`;
    chatMessages.appendChild(msgDiv);
    chatMessages.scrollTop = chatMessages.scrollHeight;
    if (role === 'user') messages.push({ role, content });
}

function addTypingIndicator(id) {
    const div = document.createElement('div');
    div.id = id;
    div.className = 'message assistant';
    div.innerHTML = `
        <div class="avatar"><img src="/static/ai-avatar.png" alt="Kuldeep AI"></div>
        <div class="message-content">
            <div class="typing-bubble">
                <div class="typing-dot"></div>
                <div class="typing-dot"></div>
                <div class="typing-dot"></div>
            </div>
        </div>`;
    chatMessages.appendChild(div);
    chatMessages.scrollTop = chatMessages.scrollHeight;
}

function removeTypingIndicator(id) {
    const el = document.getElementById(id);
    if (el) el.remove();
}

function initAssistantMessage(id) {
    const div = document.createElement('div');
    div.id = id;
    div.className = 'message assistant';
    div.innerHTML = `
        <div class="avatar"><img src="/static/ai-avatar.png" alt="Kuldeep AI"></div>
        <div class="message-content">
            <div class="bubble assistant-bubble"></div>
            <div class="message-meta">${new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}</div>
        </div>`;
    chatMessages.appendChild(div);
    return div.querySelector('.assistant-bubble');
}

// --- Download ---
downloadBtn.addEventListener('click', () => {
    let md = `# Chat Session ${sessionId}\n\n`;
    messages.forEach(m => {
        const role = m.role === 'user' ? 'You' : 'Kuldeep AI';
        md += `### ${role}\n${m.content}\n\n---\n\n`;
    });
    const blob = new Blob([md], { type: 'text/markdown' });
    const url  = URL.createObjectURL(blob);
    const a    = document.createElement('a');
    a.href = url; a.download = `chat_${sessionId}.md`; a.click();
});

init();