';
+ }
+ }
+
+ async function deleteChat(chatId) {
+ if (!confirm('Are you sure you want to delete this chat?')) return;
+
+ try {
+ const resp = await fetch(`api/history.php?action=delete&chat_id=${chatId}`);
+ const data = await resp.json();
+ if (data.success) {
+ if (currentChatId == chatId) startNewChat();
+ loadHistory();
+ }
+ } catch (e) {
+ console.error(e);
+ }
+ }
+
// --- Chat Logic ---
async function sendMessage() {
const message = chatInput.value.trim();
if (!message) return;
+ const isNewChat = !currentChatId;
+
// Clear input and disable
chatInput.value = '';
chatInput.style.height = 'auto';
@@ -78,6 +196,11 @@ document.addEventListener('DOMContentLoaded', () => {
if (currentMode === 'game' || currentMode === 'app') {
addLaunchButton(data.message);
}
+
+ // If it was a new chat, refresh history to show the title
+ if (isNewChat) {
+ loadHistory();
+ }
} else {
appendMessage('assistant', 'Error: ' + (data.error || 'Unknown error'));
}
@@ -88,15 +211,16 @@ document.addEventListener('DOMContentLoaded', () => {
}
}
- function appendMessage(role, text) {
+ function appendMessage(role, text, animate = true) {
+ if (role === 'system') return;
+
// 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`;
+ msgDiv.className = `message message-${role} ${animate ? 'animate-fade-in' : ''}`;
- // Use marked.js or simple formatting
msgDiv.innerHTML = formatText(text);
chatWindow.appendChild(msgDiv);
@@ -104,24 +228,19 @@ document.addEventListener('DOMContentLoaded', () => {
}
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) => {
+ const safeCode = code.trim().replace(/`/g, '\`');
return `
${lang || 'code'}
- Copy
+ Copy
${escapeHtml(code.trim())}
`;
});
// Simple line breaks for non-code parts
- // (Only for parts outside of the generated HTML above)
- // This is a bit naive but works for simple chat
if (!formatted.includes('
');
}
@@ -136,11 +255,9 @@ document.addEventListener('DOMContentLoaded', () => {
}
function addLaunchButton(content) {
- // Find code block content
const match = content.match(/```(?:html|xml)?\s*([\s\S]*?)```/i);
let codeToLaunch = match ? match[1] : content;
- // Only add button if it looks like a full HTML document or contains significant HTML tags
const hasHtmlTags = / {
btn.className = 'btn btn-sm btn-success mt-2 d-inline-flex align-items-center gap-2 shadow-sm';
btn.innerHTML = ' Launch Application in New Tab';
btn.onclick = () => {
- // If it's not a full HTML, wrap it
if (!codeToLaunch.toLowerCase().includes('AI Generated App${codeToLaunch}`;
}
@@ -157,7 +273,6 @@ document.addEventListener('DOMContentLoaded', () => {
window.open(url, '_blank');
};
- // Append to the last message div
chatWindow.lastElementChild.appendChild(btn);
}
}
@@ -181,7 +296,6 @@ document.addEventListener('DOMContentLoaded', () => {
}
});
- // Auto-resize textarea
chatInput.addEventListener('input', () => {
chatInput.style.height = 'auto';
chatInput.style.height = (chatInput.scrollHeight) + 'px';
@@ -222,7 +336,6 @@ document.addEventListener('DOMContentLoaded', () => {
if (data.success) {
const modal = bootstrap.Modal.getInstance(document.getElementById('settingsModal'));
if (modal) modal.hide();
-
showToast('Settings saved successfully!');
}
} catch (e) {
@@ -251,4 +364,7 @@ document.addEventListener('DOMContentLoaded', () => {
const currentTheme = document.documentElement.getAttribute('data-theme');
const activeSwatch = document.querySelector(`.theme-swatch[data-theme="${currentTheme}"]`);
if (activeSwatch) activeSwatch.classList.add('active');
-});
+
+ // Initial load
+ loadHistory();
+});
\ No newline at end of file
diff --git a/index.php b/index.php
index 7cb89f2..afe75f8 100644
--- a/index.php
+++ b/index.php
@@ -188,26 +188,6 @@ $limitsOff = $settings['limits_off'] ?? '0';
.modal-footer {
border-top: 1px solid var(--border-color);
}
-
- /* Themes Grid */
- .theme-grid {
- display: grid;
- grid-template-columns: repeat(2, 1fr);
- gap: 10px;
- }
- .theme-swatch {
- height: 40px;
- border-radius: 6px;
- cursor: pointer;
- border: 2px solid transparent;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 0.8rem;
- }
- .theme-swatch.active {
- border-color: var(--accent-color);
- }
@@ -240,14 +220,13 @@ $limitsOff = $settings['limits_off'] ?? '0';