SergioI1991 commited on
Commit
b5bef06
verified
1 Parent(s): b9410f0

Update templates/chat-bot.html

Browse files
Files changed (1) hide show
  1. templates/chat-bot.html +80 -193
templates/chat-bot.html CHANGED
@@ -1,201 +1,88 @@
1
- <script>
2
- document.addEventListener('DOMContentLoaded', () => {
3
- // --- STATE MANAGEMENT (Sin cambios) ---
4
- let sessionId = null;
5
- let sessionAuth = { admin: null, report: null };
6
-
7
- // --- ELEMENT SELECTORS (COMPLETO) ---
8
- // Ahora incluimos TODOS los selectores de tu JS original
9
- const ui = {
10
- sendButton: document.getElementById('send-button'),
11
- userInput: document.getElementById('user-input'),
12
- chatMessages: document.getElementById('chat-messages'),
13
- themeToggle: document.getElementById('theme-toggle'),
14
- adminPanelButton: document.getElementById('admin-panel-button'),
15
- adminModal: {
16
- overlay: document.getElementById('admin-modal-overlay'),
17
- closeBtn: document.getElementById('modal-close-btn'),
18
- rebuildIndexBtn: document.getElementById('rebuild-index-button'),
19
- clearHistoryBtn: document.getElementById('clear-history-button'),
20
- downloadDbBtn: document.getElementById('download-db-button'),
21
- downloadLogBtn: document.getElementById('download-log-button'),
22
- refreshStatusBtn: document.getElementById('refresh-status-button'),
23
- changeCredsBtn: document.getElementById('change-credentials-button'),
24
- statusDisplay: document.getElementById('rag-status-display')
25
- },
26
- credsModal: {
27
- overlay: document.getElementById('credentials-modal'),
28
- title: document.getElementById('credentials-title'),
29
- usernameInput: document.getElementById('username-input'),
30
- passwordInput: document.getElementById('password-input'),
31
- cancelBtn: document.getElementById('credentials-cancel-btn'),
32
- submitBtn: document.getElementById('credentials-submit-btn')
33
- }
34
- };
35
- autosize(ui.userInput);
36
-
37
- // --- THEME MANAGEMENT (Sin cambios) ---
38
- const applyTheme = (theme) => {
39
- document.documentElement.setAttribute('data-theme', theme);
40
- localStorage.setItem('chatTheme', theme);
41
- // Peque帽o ajuste para que funcione con la estructura del bot贸n
42
- const icon = ui.themeToggle.querySelector('i');
43
- if(icon) {
44
- icon.className = `fas ${theme === 'dark' ? 'fa-sun' : 'fa-moon'}`;
45
- }
46
- };
47
- const toggleTheme = () => applyTheme(document.documentElement.getAttribute('data-theme') === 'dark' ? 'light' : 'dark');
48
-
49
- // --- MODAL MANAGEMENT (Sin cambios) ---
50
- const toggleModal = (modalOverlay, show) => {
51
- if (modalOverlay) {
52
- modalOverlay.classList.toggle('visible', show);
53
- }
54
- };
55
-
56
- // --- CHAT MESSAGING (Funcional) ---
57
- const initializeChat = async () => {
58
- applyTheme(localStorage.getItem('chatTheme') || 'light');
59
- try {
60
- // Descomenta estas l铆neas cuando tu backend est茅 listo
61
- // const response = await axios.post('/create-session');
62
- // sessionId = response.data.session_id;
63
- ui.userInput.disabled = false;
64
- ui.sendButton.disabled = false;
65
- } catch (error) {
66
- console.error('Error creating session:', error);
67
- appendMessage({sender: 'bot', text: 'Problema al inicial la sesi贸n. Por favor refresca la pagina.'});
68
- }
69
- };
70
-
71
- const appendMessage = ({ sender, text }) => {
72
- const typingIndicator = document.getElementById('typing-indicator');
73
- if (typingIndicator) typingIndicator.remove();
74
-
75
- const templateId = sender === 'bot' ? 'assistant-message-template' : 'user-message-template';
76
- const template = document.getElementById(templateId);
77
- if (!template) return; // Guarda de seguridad
78
-
79
- const messageClone = template.content.cloneNode(true);
80
- const messageTextElement = messageClone.querySelector('.message-text-placeholder');
81
- if(messageTextElement) messageTextElement.textContent = text;
82
-
83
- ui.chatMessages.appendChild(messageClone);
84
- ui.chatMessages.scrollTop = ui.chatMessages.scrollHeight;
85
- };
86
-
87
- const showTypingIndicator = () => {
88
- if (document.getElementById('typing-indicator')) return;
89
- const indicatorHTML = `
90
- <div id="typing-indicator" class="flex items-start gap-3">
91
- <div class="w-10 h-10 bg-blue-500 text-white rounded-full flex items-center justify-center flex-shrink-0"><i class="fas fa-tooth"></i></div>
92
- <div class="bg-white p-4 rounded-xl rounded-tl-none shadow-sm"><div class="flex items-center space-x-1"><span class="h-2 w-2 bg-slate-300 rounded-full animate-bounce [animation-delay:-0.3s]"></span><span class="h-2 w-2 bg-slate-300 rounded-full animate-bounce [animation-delay:-0.15s]"></span><span class="h-2 w-2 bg-slate-300 rounded-full animate-bounce"></span></div></div>
93
- </div>`;
94
- ui.chatMessages.insertAdjacentHTML('beforeend', indicatorHTML);
95
- ui.chatMessages.scrollTop = ui.chatMessages.scrollHeight;
96
- };
97
-
98
- const sendMessage = async () => {
99
- const message = ui.userInput.value.trim();
100
- if (message === '') return;
101
-
102
- appendMessage({sender: 'user', text: message});
103
- ui.userInput.value = '';
104
- autosize.update(ui.userInput);
105
- showTypingIndicator();
106
-
107
- try {
108
- const response = await axios.post('/chat-bot', { query: message, session_id: sessionId });
109
- const cleanResponse = response.data?.answer || 'Perdona, no puedo procesar eso.';
110
- appendMessage({sender: 'bot', text: cleanResponse});
111
- } catch (error) {
112
- console.error('Error sending message:', error);
113
- appendMessage({sender: 'bot', text: 'Ocurri贸 un error cr铆tico. Por favor, revisa los logs del servidor.'});
114
- }
115
- };
116
 
117
- // --- CREDENTIALS & ADMIN FUNCTIONS (Toda tu l贸gica original) ---
118
- const getCredentials = (type) => {
119
- return new Promise((resolve, reject) => {
120
- if (sessionAuth[type]) {
121
- return resolve(sessionAuth[type]);
122
- }
123
- if (!ui.credsModal.overlay) {
124
- return reject(new Error('Modal de credenciales no encontrado'));
125
- }
126
- ui.credsModal.title.textContent = `${type.charAt(0).toUpperCase() + type.slice(1)} Authentication`;
127
- ui.credsModal.usernameInput.value = '';
128
- ui.credsModal.passwordInput.value = '';
129
- toggleModal(ui.credsModal.overlay, true);
130
- ui.credsModal.usernameInput.focus();
131
-
132
- const handleSubmit = () => {
133
- const username = ui.credsModal.usernameInput.value;
134
- const password = ui.credsModal.passwordInput.value;
135
- cleanup();
136
- if (username && password) {
137
- resolve({ username, password });
138
- } else {
139
- reject(new Error('Credentials not provided.'));
140
- }
141
- };
142
-
143
- const handleCancel = () => {
144
- cleanup();
145
- reject(new Error('Authentication cancelled.'));
146
- };
147
-
148
- const cleanup = () => {
149
- ui.credsModal.submitBtn.removeEventListener('click', handleSubmit);
150
- ui.credsModal.cancelBtn.removeEventListener('click', handleCancel);
151
- toggleModal(ui.credsModal.overlay, false);
152
- };
153
-
154
- ui.credsModal.submitBtn.addEventListener('click', handleSubmit);
155
- ui.credsModal.cancelBtn.addEventListener('click', handleCancel);
156
- });
157
- };
158
- // ... Aqu铆 ir铆an tus otras funciones de admin como forceRebuildIndex, downloadFile, etc...
159
 
160
- // --- EVENT LISTENERS (AHORA COMPLETOS) ---
161
- const chatForm = document.getElementById('chat-form');
162
- if (chatForm) {
163
- chatForm.addEventListener('submit', (e) => {
164
- e.preventDefault();
165
- sendMessage();
166
- });
167
- }
168
 
169
- if(ui.userInput) {
170
- ui.userInput.addEventListener('keypress', (e) => e.key === 'Enter' && !e.shiftKey && (e.preventDefault(), sendMessage()));
171
- }
 
 
 
172
 
173
- // 隆AQU脥 EST脕 LA MAGIA! Conectamos los botones.
174
- if(ui.themeToggle) {
175
- ui.themeToggle.addEventListener('click', toggleTheme);
176
- }
177
 
178
- if(ui.adminPanelButton) {
179
- ui.adminPanelButton.addEventListener('click', () => toggleModal(ui.adminModal.overlay, true));
180
- }
 
 
 
 
 
 
 
 
 
 
 
181
 
182
- if(ui.adminModal.closeBtn) {
183
- ui.adminModal.closeBtn.addEventListener('click', () => toggleModal(ui.adminModal.overlay, false));
184
- }
 
 
 
 
 
 
 
 
185
 
186
- if(ui.adminModal.overlay) {
187
- ui.adminModal.overlay.addEventListener('click', (e) => {
188
- if (e.target === ui.adminModal.overlay) {
189
- toggleModal(ui.adminModal.overlay, false);
190
- }
191
- });
192
- }
 
 
 
 
193
 
194
- // ... Aqu铆 ir铆an el resto de tus listeners para los botones del modal de admin ...
195
- // ui.adminModal.rebuildIndexBtn.addEventListener(...)
196
- // etc.
197
-
198
- // --- STARTUP ---
199
- initializeChat();
200
- });
201
- </script>
 
1
+ <!DOCTYPE html>
2
+ <html lang="es" data-theme="light">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>EndoBot</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script src="https://kit.fontawesome.com/a2e0e6a680.js" crossorigin="anonymous"></script>
9
+ <style>
10
+ :root[data-theme='dark'] {
11
+ --tw-bg-opacity: 1;
12
+ background-color: rgba(17, 24, 39, var(--tw-bg-opacity));
13
+ color: white;
14
+ }
15
+ .visible { display: flex !important; }
16
+ </style>
17
+ </head>
18
+ <body class="bg-gray-50 dark:bg-gray-900 min-h-screen flex flex-col items-center justify-center p-4 transition-colors">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ <!-- Header -->
21
+ <div class="flex justify-between w-full max-w-2xl mb-4">
22
+ <h1 class="text-2xl font-bold text-gray-800 dark:text-gray-100">馃挰 EndoBot</h1>
23
+ <button id="theme-toggle" class="text-xl"><i class="fas fa-moon text-gray-700 dark:text-yellow-300"></i></button>
24
+ </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ <!-- Chat Container -->
27
+ <div class="w-full max-w-2xl bg-white dark:bg-gray-800 rounded-lg shadow p-4 flex flex-col gap-3">
28
+ <div id="chat-messages" class="h-96 overflow-y-auto space-y-3 px-1 scroll-smooth">
29
+ <!-- Aqu铆 se inyectan los mensajes -->
30
+ </div>
 
 
 
31
 
32
+ <!-- Input -->
33
+ <form id="chat-form" class="flex gap-2 mt-2">
34
+ <textarea id="user-input" class="flex-1 rounded-lg border px-3 py-2 resize-none dark:bg-gray-700 dark:text-white" rows="1" placeholder="Escribe tu pregunta..." disabled></textarea>
35
+ <button id="send-button" class="bg-blue-600 text-white px-4 py-2 rounded-lg hover:bg-blue-700 transition" disabled>Enviar</button>
36
+ </form>
37
+ </div>
38
 
39
+ <!-- Admin Button -->
40
+ <button id="admin-panel-button" class="mt-4 text-sm text-gray-500 hover:underline">Panel de administraci贸n</button>
 
 
41
 
42
+ <!-- Modales (Admin y Credenciales) -->
43
+ <div id="admin-modal-overlay" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
44
+ <div class="bg-white dark:bg-gray-700 p-6 rounded-lg w-96 space-y-3 relative">
45
+ <button id="modal-close-btn" class="absolute top-2 right-3 text-gray-500"><i class="fas fa-times"></i></button>
46
+ <h2 class="text-lg font-bold text-gray-800 dark:text-white">Panel de administraci贸n</h2>
47
+ <button id="rebuild-index-button" class="w-full bg-indigo-600 text-white py-2 rounded">Reconstruir 铆ndice</button>
48
+ <button id="clear-history-button" class="w-full bg-red-500 text-white py-2 rounded">Borrar historial</button>
49
+ <button id="download-db-button" class="w-full bg-green-500 text-white py-2 rounded">Descargar DB</button>
50
+ <button id="download-log-button" class="w-full bg-yellow-500 text-white py-2 rounded">Descargar logs</button>
51
+ <button id="refresh-status-button" class="w-full bg-blue-500 text-white py-2 rounded">Estado del sistema</button>
52
+ <button id="change-credentials-button" class="w-full bg-gray-500 text-white py-2 rounded">Cambiar credenciales</button>
53
+ <div id="rag-status-display" class="mt-2 text-sm text-gray-700 dark:text-gray-300">...</div>
54
+ </div>
55
+ </div>
56
 
57
+ <div id="credentials-modal" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50">
58
+ <div class="bg-white dark:bg-gray-700 p-6 rounded-lg w-80 space-y-4">
59
+ <h2 id="credentials-title" class="text-lg font-bold text-gray-800 dark:text-white">Credenciales</h2>
60
+ <input id="username-input" type="text" class="w-full px-3 py-2 border rounded dark:bg-gray-600 dark:text-white" placeholder="Usuario">
61
+ <input id="password-input" type="password" class="w-full px-3 py-2 border rounded dark:bg-gray-600 dark:text-white" placeholder="Contrase帽a">
62
+ <div class="flex justify-end gap-2">
63
+ <button id="credentials-cancel-btn" class="px-4 py-2 text-gray-600">Cancelar</button>
64
+ <button id="credentials-submit-btn" class="bg-blue-600 text-white px-4 py-2 rounded">Aceptar</button>
65
+ </div>
66
+ </div>
67
+ </div>
68
 
69
+ <!-- Templates para mensajes -->
70
+ <template id="user-message-template">
71
+ <div class="flex justify-end">
72
+ <div class="bg-blue-600 text-white p-3 rounded-xl max-w-xs w-fit message-text-placeholder"></div>
73
+ </div>
74
+ </template>
75
+ <template id="assistant-message-template">
76
+ <div class="flex justify-start">
77
+ <div class="bg-gray-200 dark:bg-gray-600 text-black dark:text-white p-3 rounded-xl max-w-xs w-fit message-text-placeholder"></div>
78
+ </div>
79
+ </template>
80
 
81
+ <!-- Tu JS original -->
82
+ <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
83
+ <script src="https://cdn.jsdelivr.net/npm/autosize@5.0.1/dist/autosize.min.js"></script>
84
+ <script>
85
+ // Aqu铆 va tu script JS completo (el que me pasaste)
86
+ </script>
87
+ </body>
88
+ </html>