File size: 9,002 Bytes
453517a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Global variables
let currentChatId = null;
let darkMode = localStorage.getItem('darkMode') === 'true';

// Apply theme on load
document.addEventListener('DOMContentLoaded', () => {
    if (darkMode) {
        document.body.classList.add('dark-mode');
        document.getElementById('theme-toggle').innerHTML = '<i class="fas fa-sun"></i>';
    }
    
    // Add enter key listener for chat input
    const userInput = document.getElementById('user-input');
    if (userInput) {
        userInput.addEventListener('keydown', (e) => {
            if (e.key === 'Enter' && !e.shiftKey) {
                e.preventDefault();
                sendMessage();
            }
        });
    }
    
    // Smooth scroll for navigation
    document.querySelectorAll('a[href^="#"]').forEach(anchor => {
        anchor.addEventListener('click', function (e) {
            e.preventDefault();
            document.querySelector(this.getAttribute('href')).scrollIntoView({
                behavior: 'smooth'
            });
        });
    });
});

// Toggle between light and dark mode
function toggleTheme() {
    darkMode = !darkMode;
    localStorage.setItem('darkMode', darkMode);
    document.body.classList.toggle('dark-mode');
    document.getElementById('theme-toggle').innerHTML = darkMode ? 
        '<i class="fas fa-sun"></i>' : '<i class="fas fa-moon"></i>';
}

// Function to send a chat message to the backend
async function sendMessage() {
    const userInput = document.getElementById('user-input');
    const messageText = userInput.value.trim();
    const modelSelect = document.getElementById('model-select');
    const selectedModel = modelSelect ? modelSelect.value : null;
    
    if (!messageText) return;
    
    // Clear input field
    userInput.value = '';
    
    // Add user message to chat
    addMessageToChat('user', messageText);
    
    // Show typing indicator
    addTypingIndicator();
    
    try {
        // Send request to backend
        const response = await fetch('/api/chat', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                message: messageText,
                chat_id: currentChatId,
                model: selectedModel
            }),
        });
        
        // Remove typing indicator
        removeTypingIndicator();
        
        if (response.ok) {
            const data = await response.json();
            currentChatId = data.chat_id;
            
            // Add AI response to chat
            addMessageToChat('assistant', data.response);
            
            // Scroll to bottom of chat
            scrollToBottom();
        } else {
            const errorData = await response.json();
            addMessageToChat('system', `Error: ${errorData.detail || 'Something went wrong'}`);
        }
    } catch (error) {
        // Remove typing indicator
        removeTypingIndicator();
        addMessageToChat('system', `Error: ${error.message || 'Failed to communicate with the server'}`);
    }
    
    // Scroll to bottom of chat
    scrollToBottom();
}

// Add a message to the chat UI
function addMessageToChat(role, content) {
    const chatMessages = document.getElementById('chat-messages');
    
    if (!chatMessages) return;
    
    const messageDiv = document.createElement('div');
    messageDiv.className = `message ${role}-message`;
    
    const contentDiv = document.createElement('div');
    contentDiv.className = 'message-content';
    
    // Simple markdown-like formatting
    let formattedContent = content
        .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')  // Bold
        .replace(/\*(.*?)\*/g, '<em>$1</em>')              // Italic
        .replace(/```([\s\S]*?)```/g, '<pre><code>$1</code></pre>')  // Code blocks
        .replace(/`(.*?)`/g, '<code>$1</code>')            // Inline code
        .replace(/\n/g, '<br>');                          // Line breaks
    
    contentDiv.innerHTML = formattedContent;
    messageDiv.appendChild(contentDiv);
    chatMessages.appendChild(messageDiv);
}

// Add typing indicator
function addTypingIndicator() {
    const chatMessages = document.getElementById('chat-messages');
    if (!chatMessages) return;
    
    const indicator = document.createElement('div');
    indicator.className = 'message assistant-message typing-indicator';
    indicator.id = 'typing-indicator';
    
    const dots = document.createElement('div');
    dots.className = 'typing-dots';
    for (let i = 0; i < 3; i++) {
        const dot = document.createElement('span');
        dots.appendChild(dot);
    }
    
    indicator.appendChild(dots);
    chatMessages.appendChild(indicator);
    scrollToBottom();
}

// Remove typing indicator
function removeTypingIndicator() {
    const indicator = document.getElementById('typing-indicator');
    if (indicator) {
        indicator.remove();
    }
}

// Scroll to the bottom of the chat
function scrollToBottom() {
    const chatMessages = document.getElementById('chat-messages');
    if (chatMessages) {
        chatMessages.scrollTop = chatMessages.scrollHeight;
    }
}

// Admin Panel Functions
function updateModel() {
    const modelSelector = document.getElementById('model-selector');
    const model = modelSelector.value;
    
    fetch('/api/model', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({ model: model }),
    })
    .then(response => response.json())
    .then(data => {
        if (data.status === 'success') {
            document.getElementById('current-model').textContent = data.model;
            alert('Model updated successfully!');
        }
    })
    .catch(error => alert('Error updating model: ' + error));
}

function addRule() {
    const newRuleInput = document.getElementById('new-rule');
    const rule = newRuleInput.value.trim();
    
    if (!rule) return;
    
    fetch('/api/rules', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({ rule: rule }),
    })
    .then(response => response.json())
    .then(data => {
        if (data.status === 'success') {
            newRuleInput.value = '';
            refreshRulesList(data.rules);
        }
    })
    .catch(error => alert('Error adding rule: ' + error));
}

function deleteRule(ruleId) {
    fetch(`/api/rules/${ruleId}`, {
        method: 'DELETE',
    })
    .then(response => response.json())
    .then(data => {
        if (data.status === 'success') {
            refreshRulesList(data.rules);
        }
    })
    .catch(error => alert('Error deleting rule: ' + error));
}

function refreshRulesList(rules) {
    const rulesList = document.getElementById('rules-list');
    rulesList.innerHTML = '';
    
    rules.forEach((rule, index) => {
        const li = document.createElement('li');
        li.innerHTML = `${rule} <button onclick="deleteRule(${index})">Delete</button>`;
        rulesList.appendChild(li);
    });
}

function viewChat(chatId) {
    fetch(`/api/chats/${chatId}`)
    .then(response => response.json())
    .then(data => {
        const chatViewer = document.getElementById('chat-viewer');
        chatViewer.innerHTML = '<h3>Chat History</h3>';
        
        const chatContainer = document.createElement('div');
        chatContainer.className = 'admin-chat-history';
        
        data.history.forEach(message => {
            const messageDiv = document.createElement('div');
            messageDiv.className = `admin-message ${message.role}-message`;
            messageDiv.textContent = `${message.role.toUpperCase()}: ${message.content}`;
            chatContainer.appendChild(messageDiv);
        });
        
        chatViewer.appendChild(chatContainer);
    })
    .catch(error => alert('Error viewing chat: ' + error));
}

// Load chats for the admin panel
function loadChats() {
    if (document.getElementById('active-chats')) {
        fetch('/api/chats')
        .then(response => response.json())
        .then(data => {
            const chatsList = document.getElementById('active-chats');
            chatsList.innerHTML = '';
            
            data.chats.forEach(chatId => {
                const li = document.createElement('li');
                li.innerHTML = `<a href="#" onclick="viewChat('${chatId}')">${chatId}</a>`;
                chatsList.appendChild(li);
            });
        })
        .catch(error => console.error('Error loading chats:', error));
    }
}

// Check if we're on the admin page and load chats
if (window.location.pathname === '/admin') {
    loadChats();
}