Spaces:
Sleeping
Sleeping
| <html lang="es"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>FarmaBot - Endocaser | Bilingual Dental Pharmacology Assistant</title> | |
| <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet"> | |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> | |
| <style> | |
| /* Estilos para tablas Markdown */ | |
| .message-content table { | |
| border-collapse: collapse; | |
| width: 100%; | |
| margin: 12px 0; | |
| font-size: 13px; | |
| background: #fff; | |
| border-radius: 8px; | |
| overflow-x: auto; | |
| border: 1px solid var(--border); | |
| display: table; | |
| } | |
| .message-content table tbody, .message-content table thead { | |
| display: table-row-group; | |
| width: 100%; | |
| } | |
| .message-content th, .message-content td { | |
| padding: 10px 12px; | |
| text-align: left; | |
| border: 1px solid var(--border); | |
| word-break: break-word; | |
| } | |
| .message-content th { | |
| background-color: var(--primary-surface); | |
| color: var(--primary-dark); | |
| font-weight: 600; | |
| white-space: normal; | |
| } | |
| .message-content tr:nth-child(even) { | |
| background-color: #f9fafb; | |
| } | |
| /* Modo tarjetas para móvil */ | |
| @media (max-width: 480px) { | |
| .message-content table { | |
| display: block; | |
| border: none; | |
| margin: 8px 0; | |
| } | |
| .message-content table thead { | |
| display: none; | |
| } | |
| .message-content table tbody { | |
| display: flex; | |
| flex-direction: column; | |
| gap: 12px; | |
| } | |
| .message-content table tr { | |
| display: flex; | |
| flex-direction: column; | |
| border: 1px solid var(--border); | |
| border-radius: 8px; | |
| padding: 12px; | |
| background: #fff; | |
| margin-bottom: 0; | |
| } | |
| .message-content table tr:nth-child(even) { | |
| background-color: #fff; | |
| } | |
| .message-content table td { | |
| display: flex; | |
| justify-content: space-between; | |
| align-items: flex-start; | |
| border: none; | |
| padding: 8px 0; | |
| border-bottom: 1px solid var(--border); | |
| } | |
| .message-content table td:last-child { | |
| border-bottom: none; | |
| } | |
| .message-content table td::before { | |
| content: attr(data-label); | |
| font-weight: 600; | |
| color: var(--primary-dark); | |
| margin-right: 12px; | |
| flex-shrink: 0; | |
| } | |
| } | |
| .message-content ul, .message-content ol { | |
| padding-left: 20px; | |
| margin: 8px 0; | |
| } | |
| .message-content li { | |
| margin-bottom: 4px; | |
| } | |
| .message-content strong { | |
| color: var(--primary-dark); | |
| } | |
| .message-content p { | |
| margin: 0 0 8px 0; | |
| word-wrap: break-word; | |
| overflow-wrap: break-word; | |
| } | |
| :root { | |
| --primary: #4574E8; | |
| --primary-light: #6B93F2; | |
| --primary-dark: #2B4FAD; | |
| --primary-surface: #EEF2FD; | |
| --bg: #F7F8FC; | |
| --surface: #FFFFFF; | |
| --text-primary: #1A1D26; | |
| --text-secondary: #6B7280; | |
| --border: #E5E7EB; | |
| --hint: #9CA3AF; | |
| --success: #10B981; | |
| --error: #EF4444; | |
| --bot-bg: #F3F4F6; | |
| --user-bg: #EEF2FD; | |
| --sidebar-bg: #1A2332; | |
| } | |
| * { box-sizing: border-box; margin: 0; padding: 0; } | |
| body { | |
| font-family: 'Inter', sans-serif; | |
| background: var(--bg); | |
| color: var(--text-primary); | |
| height: 100vh; | |
| overflow: hidden; | |
| } | |
| .chat-container { | |
| height: 100vh; | |
| display: flex; | |
| flex-direction: column; | |
| } | |
| /* Header */ | |
| .chat-header { | |
| background: linear-gradient(135deg, var(--primary), var(--primary-light)); | |
| color: #fff; | |
| padding: 16px 24px; | |
| display: flex; | |
| align-items: center; | |
| gap: 14px; | |
| flex-shrink: 0; | |
| box-shadow: 0 4px 20px rgba(69, 116, 232, 0.25); | |
| } | |
| .header-logo { | |
| width: 40px; | |
| height: 40px; | |
| background: rgba(255,255,255,0.2); | |
| border-radius: 12px; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| font-size: 20px; | |
| } | |
| .header-info { flex: 1; } | |
| .header-info h2 { | |
| font-size: 18px; | |
| font-weight: 700; | |
| letter-spacing: -0.3px; | |
| } | |
| .header-info span { | |
| font-size: 12px; | |
| opacity: 0.75; | |
| font-weight: 400; | |
| } | |
| .header-buttons { display: flex; gap: 8px; } | |
| .icon-btn { | |
| background: rgba(255,255,255,0.15); | |
| border: none; | |
| color: #fff; | |
| width: 36px; | |
| height: 36px; | |
| border-radius: 10px; | |
| cursor: pointer; | |
| font-size: 14px; | |
| transition: all 0.2s; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| } | |
| .icon-btn:hover { background: rgba(255,255,255,0.25); transform: translateY(-1px); } | |
| /* Messages */ | |
| .chat-messages { | |
| flex: 1; | |
| padding: 24px; | |
| overflow-y: auto; | |
| background: var(--bg); | |
| } | |
| .message { | |
| margin-bottom: 16px; | |
| display: flex; | |
| max-width: 85%; | |
| animation: fadeIn 0.3s ease; | |
| word-wrap: break-word; | |
| overflow-wrap: break-word; | |
| } | |
| @keyframes fadeIn { | |
| from { opacity: 0; transform: translateY(8px); } | |
| to { opacity: 1; transform: translateY(0); } | |
| } | |
| .message.user { margin-left: auto; } | |
| .message.bot { margin-right: auto; } | |
| .bot-avatar { | |
| width: 32px; | |
| height: 32px; | |
| background: var(--primary); | |
| border-radius: 10px; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| color: #fff; | |
| font-size: 14px; | |
| margin-right: 10px; | |
| flex-shrink: 0; | |
| margin-top: 2px; | |
| } | |
| .message-content { | |
| padding: 12px 16px; | |
| border-radius: 16px; | |
| line-height: 1.6; | |
| font-size: 14px; | |
| min-width: 0; | |
| word-wrap: break-word; | |
| overflow-wrap: break-word; | |
| } | |
| .message.user .message-content { | |
| background: var(--primary); | |
| color: #fff; | |
| border-bottom-right-radius: 4px; | |
| box-shadow: 0 2px 8px rgba(69, 116, 232, 0.2); | |
| word-wrap: break-word; | |
| overflow-wrap: break-word; | |
| } | |
| .message.bot .message-content { | |
| background: var(--surface); | |
| border: 1px solid var(--border); | |
| border-bottom-left-radius: 4px; | |
| box-shadow: 0 1px 4px rgba(0,0,0,0.04); | |
| word-wrap: break-word; | |
| overflow-wrap: break-word; | |
| } | |
| .message-content p { margin: 0; } | |
| .message-content p + p { margin-top: 8px; } | |
| /* Typing indicator */ | |
| .typing-indicator { | |
| display: flex; | |
| align-items: center; | |
| gap: 4px; | |
| padding: 4px 0; | |
| } | |
| .typing-indicator span { | |
| width: 7px; | |
| height: 7px; | |
| background: var(--primary); | |
| border-radius: 50%; | |
| animation: bounce 1.3s linear infinite; | |
| } | |
| @keyframes bounce { | |
| 0%, 60%, 100% { transform: translateY(0); } | |
| 30% { transform: translateY(-6px); } | |
| } | |
| /* Welcome message */ | |
| .welcome-card { | |
| background: var(--surface); | |
| border: 1px solid var(--border); | |
| border-radius: 16px; | |
| padding: 24px; | |
| text-align: center; | |
| max-width: 400px; | |
| margin: 40px auto; | |
| box-shadow: 0 2px 12px rgba(0,0,0,0.04); | |
| } | |
| .welcome-card .icon { | |
| width: 56px; | |
| height: 56px; | |
| background: var(--primary-surface); | |
| border-radius: 16px; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| margin: 0 auto 16px; | |
| font-size: 24px; | |
| color: var(--primary); | |
| } | |
| .welcome-card h3 { | |
| font-size: 18px; | |
| font-weight: 700; | |
| margin-bottom: 6px; | |
| color: var(--text-primary); | |
| } | |
| .welcome-card p { | |
| font-size: 13px; | |
| color: var(--text-secondary); | |
| line-height: 1.5; | |
| } | |
| .language-selector { | |
| display: flex; | |
| gap: 8px; | |
| justify-content: center; | |
| margin-top: 16px; | |
| } | |
| .lang-btn { | |
| padding: 8px 16px; | |
| border: 1px solid var(--primary); | |
| background: transparent; | |
| color: var(--primary); | |
| border-radius: 20px; | |
| cursor: pointer; | |
| font-weight: 500; | |
| font-size: 12px; | |
| transition: all 0.2s; | |
| } | |
| .lang-btn.active { | |
| background: var(--primary); | |
| color: #fff; | |
| } | |
| .lang-btn:hover { | |
| background: var(--primary); | |
| color: #fff; | |
| } | |
| .quick-actions { | |
| display: flex; | |
| flex-wrap: wrap; | |
| gap: 8px; | |
| justify-content: center; | |
| margin-top: 16px; | |
| } | |
| .quick-action { | |
| background: var(--primary-surface); | |
| color: var(--primary); | |
| border: 1px solid rgba(69, 116, 232, 0.15); | |
| padding: 8px 14px; | |
| border-radius: 20px; | |
| font-size: 12px; | |
| font-weight: 500; | |
| cursor: pointer; | |
| transition: all 0.2s; | |
| } | |
| .quick-action:hover { | |
| background: var(--primary); | |
| color: #fff; | |
| transform: translateY(-1px); | |
| } | |
| /* Input area */ | |
| .chat-input { | |
| padding: 16px 24px; | |
| background: var(--surface); | |
| border-top: 1px solid var(--border); | |
| display: flex; | |
| align-items: flex-end; | |
| gap: 10px; | |
| flex-shrink: 0; | |
| } | |
| .chat-input textarea { | |
| flex: 1; | |
| padding: 12px 16px; | |
| border: 1px solid var(--border); | |
| border-radius: 24px; | |
| resize: none; | |
| font-size: 14px; | |
| font-family: 'Inter', sans-serif; | |
| background: var(--bg); | |
| color: var(--text-primary); | |
| transition: border-color 0.2s; | |
| max-height: 120px; | |
| min-width: 0; | |
| } | |
| /* Media queries para responsividad */ | |
| @media (max-width: 768px) { | |
| .chat-header { | |
| padding: 12px 16px; | |
| } | |
| .header-info h2 { | |
| font-size: 16px; | |
| } | |
| .header-info span { | |
| font-size: 11px; | |
| } | |
| .chat-messages { | |
| padding: 16px; | |
| } | |
| .message { | |
| max-width: 90%; | |
| margin-bottom: 12px; | |
| } | |
| .message-content { | |
| padding: 10px 14px; | |
| font-size: 13px; | |
| } | |
| .message-content th, .message-content td { | |
| padding: 8px 10px; | |
| font-size: 12px; | |
| } | |
| .chat-input { | |
| padding: 12px 16px; | |
| gap: 8px; | |
| } | |
| .chat-input textarea { | |
| padding: 10px 14px; | |
| font-size: 13px; | |
| } | |
| .send-btn { | |
| width: 40px; | |
| height: 40px; | |
| font-size: 14px; | |
| } | |
| } | |
| @media (max-width: 480px) { | |
| .chat-header { | |
| padding: 10px 12px; | |
| } | |
| .header-logo { | |
| width: 32px; | |
| height: 32px; | |
| font-size: 16px; | |
| } | |
| .header-info h2 { | |
| font-size: 14px; | |
| } | |
| .header-info span { | |
| font-size: 10px; | |
| } | |
| .chat-messages { | |
| padding: 12px; | |
| } | |
| .message { | |
| max-width: 95%; | |
| margin-bottom: 10px; | |
| } | |
| .bot-avatar { | |
| width: 28px; | |
| height: 28px; | |
| font-size: 12px; | |
| margin-right: 8px; | |
| } | |
| .message-content { | |
| padding: 8px 12px; | |
| font-size: 12px; | |
| border-radius: 12px; | |
| } | |
| .message-content ul, .message-content ol { | |
| padding-left: 16px; | |
| margin: 6px 0; | |
| } | |
| .chat-input { | |
| padding: 10px 12px; | |
| gap: 6px; | |
| } | |
| .chat-input textarea { | |
| padding: 8px 12px; | |
| font-size: 12px; | |
| border-radius: 20px; | |
| } | |
| .send-btn { | |
| width: 36px; | |
| height: 36px; | |
| font-size: 12px; | |
| border-radius: 10px; | |
| } | |
| .welcome-card { | |
| max-width: 90%; | |
| padding: 16px; | |
| margin: 20px auto; | |
| } | |
| .welcome-card .icon { | |
| width: 48px; | |
| height: 48px; | |
| font-size: 20px; | |
| } | |
| .welcome-card h3 { | |
| font-size: 16px; | |
| } | |
| .welcome-card p { | |
| font-size: 12px; | |
| } | |
| .quick-action { | |
| padding: 6px 12px; | |
| font-size: 11px; | |
| } | |
| } | |
| .chat-input textarea:focus { | |
| border-color: var(--primary); | |
| outline: none; | |
| box-shadow: 0 0 0 3px rgba(69, 116, 232, 0.1); | |
| } | |
| .chat-input textarea::placeholder { color: var(--hint); } | |
| .send-btn { | |
| background: var(--primary); | |
| color: #fff; | |
| border: none; | |
| width: 44px; | |
| height: 44px; | |
| border-radius: 14px; | |
| cursor: pointer; | |
| font-size: 16px; | |
| transition: all 0.2s; | |
| display: flex; | |
| align-items: center; | |
| justify-content: center; | |
| box-shadow: 0 2px 8px rgba(69, 116, 232, 0.3); | |
| } | |
| .send-btn:hover { background: var(--primary-dark); transform: translateY(-1px); } | |
| .send-btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; } | |
| /* Modal */ | |
| .modal-overlay { | |
| position: fixed; top: 0; left: 0; width: 100%; height: 100%; | |
| background: rgba(0,0,0,0.5); backdrop-filter: blur(4px); | |
| display: flex; justify-content: center; align-items: center; | |
| z-index: 1000; opacity: 0; visibility: hidden; transition: all 0.3s; | |
| } | |
| .modal-overlay.visible { opacity: 1; visibility: visible; } | |
| .modal-content { | |
| background: var(--surface); | |
| padding: 28px; | |
| border-radius: 20px; | |
| width: 90%; max-width: 600px; max-height: 85vh; overflow-y: auto; | |
| box-shadow: 0 20px 60px rgba(0,0,0,0.15); | |
| transform: scale(0.95); transition: transform 0.3s; | |
| } | |
| .modal-overlay.visible .modal-content { transform: scale(1); } | |
| .modal-content h3 { | |
| margin-bottom: 20px; | |
| font-size: 18px; | |
| font-weight: 700; | |
| color: var(--text-primary); | |
| display: flex; | |
| align-items: center; | |
| gap: 10px; | |
| } | |
| .modal-content h3 i { color: var(--primary); } | |
| .modal-close { | |
| position: absolute; top: 16px; right: 16px; | |
| background: var(--bg); border: none; width: 32px; height: 32px; | |
| border-radius: 8px; font-size: 16px; color: var(--text-secondary); | |
| cursor: pointer; display: flex; align-items: center; justify-content: center; | |
| } | |
| .modal-close:hover { background: var(--border); } | |
| .admin-grid { | |
| display: grid; | |
| grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); | |
| gap: 10px; | |
| margin-bottom: 20px; | |
| } | |
| .admin-btn { | |
| background: var(--bg); | |
| border: 1px solid var(--border); | |
| color: var(--text-primary); | |
| padding: 14px; | |
| border-radius: 12px; | |
| cursor: pointer; | |
| font-size: 13px; | |
| font-weight: 500; | |
| font-family: 'Inter', sans-serif; | |
| display: flex; | |
| align-items: center; | |
| gap: 10px; | |
| transition: all 0.2s; | |
| } | |
| .admin-btn:hover { | |
| background: var(--primary); | |
| color: #fff; | |
| border-color: var(--primary); | |
| transform: translateY(-2px); | |
| box-shadow: 0 4px 12px rgba(69, 116, 232, 0.25); | |
| } | |
| .admin-btn i { font-size: 16px; width: 20px; text-align: center; } | |
| .status-box { | |
| background: var(--bg); | |
| padding: 14px; | |
| border-radius: 10px; | |
| font-family: 'JetBrains Mono', monospace; | |
| font-size: 12px; | |
| white-space: pre-wrap; | |
| border: 1px solid var(--border); | |
| color: var(--text-secondary); | |
| } | |
| .form-group { margin-bottom: 14px; } | |
| .form-group label { display: block; margin-bottom: 6px; font-weight: 500; font-size: 13px; } | |
| .form-group input { | |
| width: 100%; padding: 10px 14px; | |
| border: 1px solid var(--border); border-radius: 10px; | |
| background: var(--bg); color: var(--text-primary); | |
| font-family: 'Inter', sans-serif; font-size: 14px; | |
| } | |
| .form-group input:focus { border-color: var(--primary); outline: none; } | |
| .modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; } | |
| .btn { | |
| padding: 10px 20px; border-radius: 10px; border: none; | |
| cursor: pointer; font-weight: 600; font-size: 13px; | |
| font-family: 'Inter', sans-serif; transition: all 0.2s; | |
| } | |
| .btn-primary { background: var(--primary); color: #fff; } | |
| .btn-primary:hover { background: var(--primary-dark); } | |
| .btn-secondary { background: var(--bg); color: var(--text-primary); border: 1px solid var(--border); } | |
| /* Scrollbar */ | |
| ::-webkit-scrollbar { width: 6px; } | |
| ::-webkit-scrollbar-track { background: transparent; } | |
| ::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; } | |
| ::-webkit-scrollbar-thumb:hover { background: var(--hint); } | |
| /* Responsive */ | |
| @media (max-width: 600px) { | |
| .chat-header { padding: 12px 16px; } | |
| .chat-messages { padding: 16px; } | |
| .chat-input { padding: 12px 16px; } | |
| .message { max-width: 90%; } | |
| .welcome-card { margin: 20px 16px; } | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="chat-container"> | |
| <div class="chat-header"> | |
| <div class="header-logo"><i class="fas fa-pills"></i></div> | |
| <div class="header-info"> | |
| <h2>FarmaBot</h2> | |
| <span id="header-subtitle">Asistente de farmacología dental - Endocaser</span> | |
| </div> | |
| <div class="header-buttons"> | |
| </div> | |
| </div> | |
| <div class="chat-messages" id="chat-messages"> | |
| <div class="welcome-card"> | |
| <div class="icon"><i class="fas fa-capsules"></i></div> | |
| <h3>FarmaBot</h3> | |
| <p id="welcome-text">Tu asistente de farmacología dental. Consulta interacciones, dosis, protocolos de medicación y más.</p> | |
| <div class="language-selector"> | |
| <button class="lang-btn active" id="lang-es" onclick="setLanguage('es')">Español</button> | |
| <button class="lang-btn" id="lang-en" onclick="setLanguage('en')">English</button> | |
| </div> | |
| <div class="quick-actions" id="quick-actions-container"> | |
| <button class="quick-action" onclick="sendQuickAction(this)">Interacciones medicamentosas</button> | |
| <button class="quick-action" onclick="sendQuickAction(this)">Dosis de amoxicilina</button> | |
| <button class="quick-action" onclick="sendQuickAction(this)">Analgesia post-endo</button> | |
| <button class="quick-action" onclick="sendQuickAction(this)">Paciente alérgico a penicilina</button> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="chat-input"> | |
| <textarea id="user-input" placeholder="Escribe tu consulta farmacológica... / Write your pharmacological query..." rows="1" disabled></textarea> | |
| <button id="send-button" class="send-btn" disabled><i class="fas fa-paper-plane"></i></button> | |
| </div> | |
| </div> | |
| <div class="modal-overlay" id="credentials-modal"> | |
| <div class="modal-content" style="max-width: 380px; position: relative;"> | |
| <h3 id="credentials-title"><i class="fas fa-lock"></i> Autenticación</h3> | |
| <div class="form-group"> | |
| <label for="username-input">Usuario</label> | |
| <input type="text" id="username-input" autocomplete="username"> | |
| </div> | |
| <div class="form-group"> | |
| <label for="password-input">Contraseña</label> | |
| <input type="password" id="password-input" autocomplete="current-password"> | |
| </div> | |
| <div class="modal-actions"> | |
| <button id="credentials-cancel-btn" class="btn btn-secondary">Cancelar</button> | |
| <button id="credentials-submit-btn" class="btn btn-primary">Acceder</button> | |
| </div> | |
| </div> | |
| </div> | |
| <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script> | |
| <script src="https://unpkg.com/autosize@4.0.2/dist/autosize.min.js"></script> | |
| <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> | |
| <script> | |
| // Configurar marked para saltos de línea automáticos | |
| marked.setOptions({ | |
| breaks: true, | |
| gfm: true | |
| }); | |
| // Traducciónes para soporte bilingüe | |
| const translations = { | |
| es: { | |
| welcomeText: 'Tu asistente de farmacología dental. Consulta interacciones, dosis, protocolos de medicación y más.', | |
| quickActions: [ | |
| 'Interacciones medicamentosas', | |
| 'Dosis de amoxicilina', | |
| 'Analgesia post-endo', | |
| 'Paciente alérgico a penicilina' | |
| ], | |
| headerSubtitle: 'Asistente de farmacología dental - Endocaser', | |
| placeholder: 'Escribe tu consulta farmacológica...' | |
| }, | |
| en: { | |
| welcomeText: 'Your dental pharmacology assistant. Consult drug interactions, dosages, medication protocols and more.', | |
| quickActions: [ | |
| 'Drug interactions', | |
| 'Amoxicillin dosage', | |
| 'Post-endo analgesia', | |
| 'Penicillin-allergic patient' | |
| ], | |
| headerSubtitle: 'Dental Pharmacology Assistant - Endocaser', | |
| placeholder: 'Write your pharmacological query...' | |
| } | |
| }; | |
| let currentLanguage = 'es'; | |
| function setLanguage(lang) { | |
| currentLanguage = lang; | |
| const t = translations[lang]; | |
| // Actualizar UI | |
| document.getElementById('welcome-text').textContent = t.welcomeText; | |
| document.getElementById('header-subtitle').textContent = t.headerSubtitle; | |
| document.getElementById('user-input').placeholder = t.placeholder; | |
| // Actualizar botones de acción rápida | |
| const quickActionButtons = document.querySelectorAll('#quick-actions-container .quick-action'); | |
| quickActionButtons.forEach((btn, index) => { | |
| if (index < t.quickActions.length) { | |
| btn.textContent = t.quickActions[index]; | |
| } | |
| }); | |
| // Actualizar botones de idioma | |
| document.getElementById('lang-es').classList.toggle('active', lang === 'es'); | |
| document.getElementById('lang-en').classList.toggle('active', lang === 'en'); | |
| } | |
| function sendQuickAction(btn) { | |
| const input = document.getElementById('user-input'); | |
| input.value = btn.textContent; | |
| document.getElementById('send-button').click(); | |
| } | |
| document.addEventListener('DOMContentLoaded', () => { | |
| let sessionId = null; | |
| let sessionAuth = { admin: null, report: null }; | |
| const ui = { | |
| sendButton: document.getElementById('send-button'), | |
| userInput: document.getElementById('user-input'), | |
| chatMessages: document.getElementById('chat-messages'), | |
| credsModal: { | |
| overlay: document.getElementById('credentials-modal'), | |
| title: document.getElementById('credentials-title'), | |
| usernameInput: document.getElementById('username-input'), | |
| passwordInput: document.getElementById('password-input'), | |
| cancelBtn: document.getElementById('credentials-cancel-btn'), | |
| submitBtn: document.getElementById('credentials-submit-btn') | |
| } | |
| }; | |
| autosize(ui.userInput); | |
| const toggleModal = (modal, show) => modal.classList.toggle('visible', show); | |
| const initializeChat = async () => { | |
| try { | |
| const response = await axios.post('/create-session'); | |
| sessionId = response.data.session_id; | |
| ui.userInput.disabled = false; | |
| ui.sendButton.disabled = false; | |
| } catch (error) { | |
| console.error('Error creating session:', error); | |
| appendMessage({sender: 'bot', text: 'Error al iniciar la sesión. Recarga la página.'}); | |
| } | |
| }; | |
| const appendMessage = ({ sender, text, isHtml = false }) => { | |
| // Remove welcome card on first message | |
| const welcome = ui.chatMessages.querySelector('.welcome-card'); | |
| if (welcome) welcome.remove(); | |
| const typing = ui.chatMessages.querySelector('.typing-indicator-wrapper'); | |
| if (typing) typing.remove(); | |
| const el = document.createElement('div'); | |
| el.className = `message ${sender}`; | |
| let content = ''; | |
| if (isHtml) { | |
| content = text; | |
| } else if (sender === 'bot') { | |
| // Renderizar Markdown para el bot | |
| content = marked.parse(text); | |
| } else { | |
| // Escapar HTML para el usuario y envolver en párrafo | |
| content = `<p>${text.replace(/</g, "<").replace(/>/g, ">")}</p>`; | |
| } | |
| if (sender === 'bot') { | |
| el.innerHTML = `<div class="bot-avatar"><i class="fas fa-pills"></i></div><div class="message-content">${content}</div>`; | |
| } else { | |
| el.innerHTML = `<div class="message-content">${content}</div>`; | |
| } | |
| ui.chatMessages.appendChild(el); | |
| ui.chatMessages.scrollTop = ui.chatMessages.scrollHeight; | |
| }; | |
| const showTypingIndicator = () => { | |
| if (ui.chatMessages.querySelector('.typing-indicator-wrapper')) return; | |
| const el = document.createElement('div'); | |
| el.className = 'message bot typing-indicator-wrapper'; | |
| el.innerHTML = `<div class="bot-avatar"><i class="fas fa-pills"></i></div><div class="message-content"><div class="typing-indicator"><span></span><span style="animation-delay:0.2s"></span><span style="animation-delay:0.4s"></span></div></div>`; | |
| ui.chatMessages.appendChild(el); | |
| ui.chatMessages.scrollTop = ui.chatMessages.scrollHeight; | |
| }; | |
| const sendMessage = async () => { | |
| if (!sessionId) return; | |
| const message = ui.userInput.value.trim(); | |
| if (!message) return; | |
| appendMessage({sender: 'user', text: message}); | |
| ui.userInput.value = ''; | |
| autosize.update(ui.userInput); | |
| showTypingIndicator(); | |
| try { | |
| const response = await axios.post('/chat-bot', { query: message, session_id: sessionId }); | |
| const raw = response.data?.answer || 'Lo siento, no pude procesar la consulta.'; | |
| const clean = raw.replace(/<think>[\s\S]*?<\/think>/, '').trim(); | |
| appendMessage({sender: 'bot', text: clean || 'Respuesta vacía.'}); | |
| } catch (error) { | |
| appendMessage({sender: 'bot', text: 'Error al procesar. Revisa los logs del servidor.'}); | |
| } | |
| }; | |
| const getCredentials = (type) => { | |
| return new Promise((resolve, reject) => { | |
| if (sessionAuth[type]) return resolve(sessionAuth[type]); | |
| ui.credsModal.usernameInput.value = ''; | |
| ui.credsModal.passwordInput.value = ''; | |
| toggleModal(ui.credsModal.overlay, true); | |
| ui.credsModal.usernameInput.focus(); | |
| const submit = () => { cleanup(); const u = ui.credsModal.usernameInput.value, p = ui.credsModal.passwordInput.value; u && p ? resolve({username:u,password:p}) : reject(new Error('No credentials')); }; | |
| const cancel = () => { cleanup(); reject(new Error('Cancelled')); }; | |
| const cleanup = () => { ui.credsModal.submitBtn.removeEventListener('click', submit); ui.credsModal.cancelBtn.removeEventListener('click', cancel); toggleModal(ui.credsModal.overlay, false); }; | |
| ui.credsModal.submitBtn.addEventListener('click', submit); | |
| ui.credsModal.cancelBtn.addEventListener('click', cancel); | |
| }); | |
| }; | |
| // Events | |
| ui.sendButton.addEventListener('click', sendMessage); | |
| ui.userInput.addEventListener('keypress', e => e.key==='Enter' && !e.shiftKey && (e.preventDefault(), sendMessage())); | |
| initializeChat(); | |
| }); | |
| </script> | |
| </body> | |
| </html> |