document.addEventListener('DOMContentLoaded', () => {
const chatWindow = document.getElementById('chat-window');
const chatInput = document.getElementById('chat-input');
const sendBtn = document.getElementById('send-btn');
const modeItems = document.querySelectorAll('.mode-item');
const currentModeBadge = document.getElementById('current-mode-badge');
const newChatBtn = document.getElementById('new-chat-btn');
// Settings elements
const creativityRange = document.getElementById('creativity-range');
const creativityVal = document.getElementById('creativity-val');
const limitsToggle = document.getElementById('limits-toggle');
const themeSwatches = document.querySelectorAll('.theme-swatch');
const saveSettingsBtn = document.getElementById('save-settings-btn');
let currentMode = 'regular';
let currentChatId = null;
// --- Sidebar & Mode Switching ---
modeItems.forEach(item => {
item.addEventListener('click', () => {
if (item.classList.contains('active')) return;
modeItems.forEach(i => i.classList.remove('active'));
item.classList.add('active');
currentMode = item.dataset.mode;
currentModeBadge.textContent = item.querySelector('span').textContent;
startNewChat();
});
});
function startNewChat() {
currentChatId = null;
chatWindow.innerHTML = `
New ${currentMode} Chat
How can I help you in this mode?
`;
}
newChatBtn.addEventListener('click', startNewChat);
// --- Chat Logic ---
async function sendMessage() {
const message = chatInput.value.trim();
if (!message) return;
// Clear input and disable
chatInput.value = '';
chatInput.style.height = 'auto';
toggleLoading(true);
// Append User Message
appendMessage('user', message);
try {
const response = await fetch('api/chat.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
message: message,
mode: currentMode,
chat_id: currentChatId,
creativity: creativityRange.value,
limits_off: limitsToggle.checked ? 1 : 0
})
});
const data = await response.json();
if (data.success) {
currentChatId = data.chat_id;
appendMessage('assistant', data.message);
// Special handling for game/app mode
if (currentMode === 'game' || currentMode === 'app') {
addLaunchButton(data.message);
}
} else {
appendMessage('assistant', 'Error: ' + (data.error || 'Unknown error'));
}
} catch (error) {
appendMessage('assistant', 'Error: ' + error.message);
} finally {
toggleLoading(false);
}
}
function appendMessage(role, text) {
// Remove empty state if present
const emptyState = chatWindow.querySelector('.my-auto');
if (emptyState) emptyState.remove();
const msgDiv = document.createElement('div');
msgDiv.className = `message message-${role} animate-fade-in`;
// Use marked.js or simple formatting
msgDiv.innerHTML = formatText(text);
chatWindow.appendChild(msgDiv);
chatWindow.scrollTop = chatWindow.scrollHeight;
}
function formatText(text) {
// Handle code blocks with more flexibility
let formatted = text;
// Escape HTML for non-code parts
// This is tricky without a library, let's just do code blocks first
// Code blocks: ```[lang]\n[code]```
formatted = formatted.replace(/```(\w+)?\s*([\s\S]*?)```/g, (match, lang, code) => {
return `