| <!DOCTYPE html> |
| <html lang="fr"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <title>Mariam AI</title> |
| |
| <script src="https://cdn.tailwindcss.com?plugins=forms,typography"></script> |
| |
| <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> |
| |
| <link rel="preconnect" href="https://fonts.googleapis.com"> |
| <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> |
| <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap" rel="stylesheet"> |
| |
| <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>✨</text></svg>"> |
| <script> |
| tailwind.config = { |
| darkMode: 'class', |
| theme: { |
| extend: { |
| fontFamily: { |
| sans: ['Inter', 'system-ui', 'sans-serif'], |
| mono: ['"JetBrains Mono"', 'monospace'] |
| }, |
| colors: { |
| primary: { 50: '#f0f9ff', 100: '#e0f2fe', 200: '#bae6fd', 300: '#7dd3fc', 400: '#38bdf8', 500: '#0ea5e9', 600: '#0284c7', 700: '#0369a1', 800: '#075985', 900: '#0c4a6e' }, |
| secondary: { 50: '#f8fafc', 100: '#f1f5f9', 200: '#e2e8f0', 300: '#cbd5e1', 400: '#94a3b8', 500: '#64748b', 600: '#475569', 700: '#334155', 800: '#1e293b', 900: '#0f172a' }, |
| accent: { 50: '#fdf4ff', 100: '#fae8ff', 200: '#f5d0fe', 300: '#f0abfc', 400: '#e879f9', 500: '#d946ef', 600: '#c026d3', 700: '#a21caf', 800: '#86198f', 900: '#701a75' } |
| }, |
| animation: { 'bounce-slow': 'bounce 2s infinite', 'pulse-slow': 'pulse 3s infinite', 'typing': 'typing 1.2s steps(3) infinite' }, |
| keyframes: { typing: { '0%': { width: '0.15em' }, '50%': { width: '0.7em' }, '100%': { width: '0.15em' } } } |
| } |
| } |
| } |
| </script> |
| <style> |
| html { |
| scroll-behavior: smooth; |
| overflow-x: hidden; |
| } |
| body { |
| font-family: 'Inter', sans-serif; |
| transition: background-color 0.3s ease, color 0.3s ease; |
| overflow-x: hidden; |
| } |
| .chat-layout { |
| min-height: calc(100vh - 64px); |
| display: grid; |
| grid-template-rows: 1fr auto; |
| } |
| ::-webkit-scrollbar { |
| width: 5px; |
| height: 5px; |
| } |
| ::-webkit-scrollbar-track { |
| background: transparent; |
| } |
| ::-webkit-scrollbar-thumb { |
| background: #cbd5e1; |
| border-radius: 5px; |
| } |
| .dark ::-webkit-scrollbar-thumb { |
| background: #475569; |
| } |
| ::-webkit-scrollbar-thumb:hover { |
| background: #94a3b8; |
| } |
| .dark ::-webkit-scrollbar-thumb:hover { |
| background: #64748b; |
| } |
| .message-bubble { |
| position: relative; |
| max-width: 85%; |
| border-radius: 1rem; |
| padding: 0.875rem 1rem; |
| line-height: 1.5; |
| animation: message-fade-in 0.3s ease-out; |
| box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05); |
| transition: transform 0.2s ease, box-shadow 0.2s ease; |
| } |
| .message-bubble:hover { |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05), 0 1px 3px rgba(0, 0, 0, 0.1); |
| } |
| .dark .message-bubble { |
| box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2); |
| } |
| .dark .message-bubble:hover { |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2), 0 1px 3px rgba(0, 0, 0, 0.3); |
| } |
| .user-message { |
| border-bottom-right-radius: 0.125rem; |
| align-self: flex-end; |
| background: linear-gradient(to bottom right, #3b82f6, #2563eb); |
| color: white; |
| } |
| .assistant-message { |
| border-bottom-left-radius: 0.125rem; |
| align-self: flex-start; |
| } |
| .dark .assistant-message { |
| background-color: #1e293b; |
| color: #e2e8f0; |
| border-color: #334155; |
| } |
| @keyframes message-fade-in { |
| from { opacity: 0; transform: translateY(10px); } |
| to { opacity: 1; transform: translateY(0); } |
| } |
| @keyframes pulse-fade { |
| 0%, 100% { opacity: 0.5; } |
| 50% { opacity: 1; } |
| } |
| .typing-indicator { |
| display: inline-flex; |
| align-items: center; |
| margin-left: 0.5rem; |
| } |
| .typing-dot { |
| width: 0.5rem; |
| height: 0.5rem; |
| border-radius: 50%; |
| background-color: currentColor; |
| opacity: 0.7; |
| margin: 0 0.1rem; |
| } |
| .typing-dot:nth-child(1) { animation: pulse-fade 1.2s 0s infinite; } |
| .typing-dot:nth-child(2) { animation: pulse-fade 1.2s 0.2s infinite; } |
| .typing-dot:nth-child(3) { animation: pulse-fade 1.2s 0.4s infinite; } |
| .dark body { |
| background-color: #0f172a; |
| color: #e2e8f0; |
| } |
| .tooltip { |
| position: relative; |
| } |
| .tooltip .tooltip-text { |
| visibility: hidden; |
| width: max-content; |
| max-width: 200px; |
| background-color: #1e293b; |
| color: #f8fafc; |
| text-align: center; |
| border-radius: 6px; |
| padding: 0.375rem 0.625rem; |
| position: absolute; |
| z-index: 1; |
| bottom: 125%; |
| left: 50%; |
| transform: translateX(-50%); |
| opacity: 0; |
| transition: opacity 0.3s, visibility 0.3s; |
| font-size: 0.75rem; |
| box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
| pointer-events: none; |
| } |
| .dark .tooltip .tooltip-text { |
| background-color: #475569; |
| color: #f1f5f9; |
| } |
| .tooltip .tooltip-text::after { |
| content: ""; |
| position: absolute; |
| top: 100%; |
| left: 50%; |
| margin-left: -5px; |
| border-width: 5px; |
| border-style: solid; |
| border-color: #1e293b transparent transparent transparent; |
| } |
| .dark .tooltip .tooltip-text::after { |
| border-color: #475569 transparent transparent transparent; |
| } |
| .tooltip:hover .tooltip-text { |
| visibility: visible; |
| opacity: 1; |
| } |
| .copy-btn { |
| position: absolute; |
| top: 0.5rem; |
| right: 0.5rem; |
| opacity: 0; |
| transition: opacity 0.2s ease, background-color 0.2s ease; |
| z-index: 2; |
| } |
| .message-bubble:hover .copy-btn { |
| opacity: 1; |
| } |
| .file-preview { |
| max-width: 300px; |
| margin: 0.5rem auto; |
| position: relative; |
| overflow: hidden; |
| border-radius: 0.5rem; |
| transition: transform 0.2s ease; |
| } |
| .file-preview:hover { |
| transform: scale(1.02); |
| } |
| .file-preview img { |
| width: 100%; |
| height: auto; |
| display: block; |
| object-fit: cover; |
| } |
| .chip { |
| display: inline-flex; |
| align-items: center; |
| background: #e0f2fe; |
| border-radius: 9999px; |
| padding: 0.25rem 0.75rem; |
| font-size: 0.75rem; |
| font-weight: 500; |
| color: #0369a1; |
| transition: all 0.2s ease; |
| } |
| .chip .chip-icon { margin-right: 0.25rem; } |
| .chip .chip-close { |
| margin-left: 0.25rem; |
| cursor: pointer; |
| opacity: 0.7; |
| transition: opacity 0.2s ease; |
| } |
| .chip .chip-close:hover { opacity: 1; } |
| .dark .chip { |
| background: #0c4a6e; |
| color: #7dd3fc; |
| } |
| .toggle-switch { |
| position: relative; |
| display: inline-block; |
| width: 2.5rem; |
| height: 1.25rem; |
| } |
| .toggle-switch input { |
| opacity: 0; |
| width: 0; |
| height: 0; |
| } |
| .toggle-slider { |
| position: absolute; |
| cursor: pointer; |
| top: 0; |
| left: 0; |
| right: 0; |
| bottom: 0; |
| background-color: #cbd5e1; |
| transition: .4s; |
| border-radius: 1.25rem; |
| } |
| .toggle-slider:before { |
| position: absolute; |
| content: ""; |
| height: 0.875rem; |
| width: 0.875rem; |
| left: 0.25rem; |
| bottom: 0.1875rem; |
| background-color: white; |
| transition: .4s; |
| border-radius: 50%; |
| } |
| input:checked + .toggle-slider { |
| background-color: #0ea5e9; |
| } |
| input:focus + .toggle-slider { |
| box-shadow: 0 0 1px #0ea5e9; |
| } |
| input:checked + .toggle-slider:before { |
| transform: translateX(1.125rem); |
| } |
| .dark .toggle-slider { |
| background-color: #475569; |
| } |
| .dark input:checked + .toggle-slider { |
| background-color: #38bdf8; |
| } |
| .chat-input { |
| transition: all 0.3s ease; |
| border-color: #e2e8f0; |
| } |
| .chat-input:focus { |
| border-color: #38bdf8; |
| box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.2); |
| } |
| .dark .chat-input { |
| background-color: #1e293b; |
| color: #f1f5f9; |
| border-color: #334155; |
| } |
| .dark .chat-input:focus { |
| border-color: #38bdf8; |
| box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.2); |
| } |
| pre { |
| position: relative; |
| background-color: #f8fafc; |
| border-radius: 0.5rem; |
| margin: 1rem 0; |
| padding: 1.25rem 1rem; |
| overflow-x: auto; |
| } |
| .dark pre { |
| background-color: #1e293b; |
| color: #e2e8f0; |
| } |
| code { |
| font-family: 'JetBrains Mono', monospace; |
| font-size: 0.875rem; |
| } |
| .code-copy-btn { |
| position: absolute; |
| top: 0.5rem; |
| right: 0.5rem; |
| opacity: 0; |
| transition: opacity 0.2s ease; |
| } |
| pre:hover .code-copy-btn { |
| opacity: 0.7; |
| } |
| |
| .table-wrapper { |
| width: 100%; |
| overflow-x: auto; |
| margin: 1rem 0; |
| border: 1px solid #e2e8f0; |
| border-radius: 0.5rem; |
| } |
| .dark .table-wrapper { |
| border-color: #334155; |
| } |
| .prose table { |
| width: 100%; |
| min-width: 100%; |
| border-collapse: collapse; |
| margin: 0; |
| } |
| .prose table th, |
| .prose table td { |
| border: 1px solid #e2e8f0; |
| padding: 0.5rem 0.75rem; |
| text-align: left; |
| border-top: none; |
| border-left: none; |
| border-right: none; |
| } |
| .prose table th { |
| border-bottom-width: 2px; |
| } |
| .dark .prose table th, |
| .dark .prose table td { |
| border-color: #334155; |
| } |
| .prose table thead { |
| background-color: #f8fafc; |
| font-weight: 600; |
| } |
| .dark .prose table thead { |
| background-color: #1e293b; |
| } |
| .prose table tbody tr:nth-child(even) { |
| background-color: #f8fafc; |
| } |
| .dark .prose table tbody tr:nth-child(even) { |
| background-color: #1e293b; |
| } |
| |
| .chat-textarea { |
| resize: none; |
| min-height: 44px; |
| max-height: 200px; |
| overflow-y: auto; |
| line-height: 1.5; |
| width: 100%; |
| border-radius: 9999px; |
| padding-top: 0.625rem; |
| padding-bottom: 0.625rem; |
| padding-left: 1rem; |
| padding-right: 3rem; |
| box-sizing: border-box; |
| } |
| |
| @media (max-width: 640px) { |
| .message-bubble { |
| max-width: 90%; |
| padding: 0.75rem 0.875rem; |
| } |
| .chat-textarea { |
| max-height: 120px; |
| font-size: 0.95rem; |
| min-height: 40px; |
| padding-top: 0.5rem; |
| padding-bottom: 0.5rem; |
| } |
| .prose table { |
| min-width: 0; |
| font-size: 0.85rem; |
| } |
| .prose table th, |
| .prose table td { |
| padding: 0.375rem 0.5rem; |
| } |
| .send-button-wrapper { |
| padding-right: 0.25rem; |
| } |
| .chat-textarea { |
| padding-right: 2.75rem; |
| } |
| #send-button { |
| width: 32px; |
| height: 32px; |
| padding: 0; |
| display: inline-flex; |
| align-items: center; |
| justify-content: center; |
| } |
| #send-button i { |
| font-size: 0.9rem; |
| } |
| } |
| |
| .input-wrapper { |
| position: relative; |
| display: flex; |
| align-items: flex-end; |
| } |
| .send-button-wrapper { |
| position: absolute; |
| right: 0.5rem; |
| bottom: 0.5rem; |
| } |
| </style> |
| </head> |
| <body class="bg-gray-50 text-gray-900 antialiased"> |
| |
| <header class="bg-gradient-to-r from-primary-600 to-primary-800 text-white py-3 px-4 shadow-md sticky top-0 z-10"> |
| <div class="max-w-4xl mx-auto flex justify-between items-center"> |
| |
| <div class="flex items-center space-x-2"> |
| <img src="https://mariam-241.vercel.app/static/image/logoboma.png" alt="Logo Mariam AI" class="h-8 sm:h-10 object-contain"> |
| <h1 class="text-xl font-bold">Mariam AI</h1> |
| </div> |
| |
| <div class="flex items-center space-x-2 sm:space-x-4"> |
| <button id="theme-toggle" class="p-2 rounded-full hover:bg-primary-700/50 transition-colors duration-200 tooltip" aria-label="Changer de thème"> |
| <i class="fa-solid fa-moon dark:hidden"></i> |
| <i class="fa-solid fa-sun hidden dark:inline"></i> |
| <span class="tooltip-text">Mode clair/sombre</span> |
| </button> |
| <form action="/clear" method="POST" id="clear-form"> |
| <button type="submit" class="flex items-center bg-red-500 hover:bg-red-600 text-white text-xs font-semibold py-1.5 px-3 rounded-full transition duration-200 focus:outline-none focus:ring-2 focus:ring-red-400 focus:ring-opacity-75 tooltip"> |
| <i class="fa-solid fa-trash-can mr-1.5"></i> |
| <span class="hidden sm:inline">Effacer</span> |
| <span class="tooltip-text">Effacer la conversation</span> |
| </button> |
| </form> |
| </div> |
| </div> |
| </header> |
| |
| <main class="max-w-4xl mx-auto chat-layout"> |
| |
| <section id="chat-messages" class="flex flex-col space-y-6 p-4 overflow-y-auto"> |
| |
| <div id="history-loading" class="text-center py-10"> |
| <div class="inline-flex items-center px-4 py-2 bg-primary-50 text-primary-700 rounded-lg dark:bg-primary-900/30 dark:text-primary-300"> |
| <svg class="animate-spin h-5 w-5 mr-3" viewBox="0 0 24 24"> |
| <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle> |
| <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path> |
| </svg> |
| <span>Chargement de la conversation...</span> |
| </div> |
| </div> |
| |
| <div id="loading-indicator" class="flex items-start space-x-2 hidden"> |
| <div class="w-8 h-8 rounded-full bg-primary-100 flex items-center justify-center flex-shrink-0 dark:bg-primary-900/50"> |
| <span class="text-lg">✨</span> |
| </div> |
| <div class="message-bubble assistant-message bg-secondary-50 text-secondary-900 border border-secondary-200 flex items-center"> |
| <span>Mariam réfléchit</span> |
| <div class="typing-indicator"> |
| <span class="typing-dot"></span> |
| <span class="typing-dot"></span> |
| <span class="typing-dot"></span> |
| </div> |
| </div> |
| </div> |
| </section> |
| |
| <div class="border-t border-gray-200 dark:border-gray-700"> |
| |
| <div id="error-message" class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 dark:bg-red-900/30 dark:text-red-300 dark:border-red-600 hidden" role="alert"> |
| <div class="flex"> |
| <div class="flex-shrink-0"><i class="fa-solid fa-circle-exclamation"></i></div> |
| <div class="ml-3"><p class="text-sm font-medium" id="error-text">Le message d'erreur détaillé ira ici.</p></div> |
| <button class="ml-auto" id="dismiss-error"><i class="fa-solid fa-xmark"></i></button> |
| </div> |
| </div> |
| |
| <div id="preview-area" class="px-4 py-2 bg-gray-50 dark:bg-gray-800/50 hidden"> |
| <div id="file-preview" class="hidden"></div> |
| </div> |
| |
| <div class="flex items-center justify-between flex-wrap gap-y-2 px-4 py-2 bg-gray-100 dark:bg-gray-800/80 text-sm text-gray-600 dark:text-gray-300"> |
| <div class="flex items-center space-x-4 flex-wrap gap-y-2"> |
| <label class="flex items-center cursor-pointer tooltip"> |
| <span class="mr-2 text-xs sm:text-sm font-medium"><i class="fa-solid fa-globe mr-1.5"></i><span class="hidden sm:inline">Recherche Web</span></span> |
| <span class="toggle-switch"> |
| <input type="checkbox" id="web_search_toggle" name="web_search" value="true"> |
| <span class="toggle-slider"></span> |
| </span> |
| <span class="tooltip-text">Activer la recherche web</span> |
| </label> |
| <label class="flex items-center cursor-pointer tooltip"> |
| <span class="mr-2 text-xs sm:text-sm font-medium text-accent-700 dark:text-accent-300"> |
| <i class="fa-solid fa-brain mr-1.5"></i> |
| <span class="hidden sm:inline">Avancé</span> |
| <span id="advanced-cooldown-timer" class="text-xs ml-1 hidden"></span> |
| </span> |
| <span class="toggle-switch"> |
| <input type="checkbox" id="advanced_reasoning_toggle" name="advanced_reasoning" value="true"> |
| <span class="toggle-slider"></span> |
| </span> |
| <span class="tooltip-text">Raisonnement avancé (1 fois/min)</span> |
| </label> |
| </div> |
| <div class="flex items-center space-x-2"> |
| <label for="file_upload" class="cursor-pointer flex items-center text-primary-600 hover:text-primary-700 dark:text-primary-400 dark:hover:text-primary-300 tooltip"> |
| <i class="fa-solid fa-paperclip"></i> |
| <span class="ml-1.5 hidden sm:inline">Fichier</span> |
| <input type="file" id="file_upload" name="file" class="hidden" accept=".txt,.pdf,.png,.jpg,.jpeg"> |
| <span class="tooltip-text">Joindre (txt, pdf, image)</span> |
| </label> |
| <div id="file-chip" class="chip hidden"> |
| <i class="fa-solid fa-file chip-icon"></i> |
| <span id="file-name" class="truncate max-w-[100px] sm:max-w-[120px]"></span> |
| <i id="clear-file" class="fa-solid fa-xmark chip-close"></i> |
| </div> |
| </div> |
| </div> |
| |
| <form id="chat-form" class="p-3 sm:p-4 bg-white dark:bg-gray-900"> |
| <div class="input-wrapper"> |
| <textarea |
| id="prompt" |
| name="prompt" |
| class="chat-input chat-textarea w-full border focus:outline-none text-sm sm:text-base" |
| placeholder="Posez votre question à Mariam..." |
| autocomplete="off" |
| rows="1"></textarea> |
| <div class="send-button-wrapper"> |
| <button |
| type="submit" |
| id="send-button" |
| class="bg-primary-500 hover:bg-primary-600 disabled:bg-primary-300 text-white rounded-full p-2 transition focus:outline-none focus:ring-2 focus:ring-primary-400 flex items-center justify-center" |
| title="Envoyer le message"> |
| <i class="fa-solid fa-paper-plane text-sm"></i> |
| </button> |
| </div> |
| </div> |
| <div class="text-xs text-center mt-2 text-gray-400 dark:text-gray-500"> |
| Appuyez sur <kbd class="px-1.5 py-0.5 bg-gray-100 dark:bg-gray-800 rounded border border-gray-300 dark:border-gray-700">Entrée</kbd> |
| <span class="hidden sm:inline"> pour envoyer</span> |
| <span class="sm:hidden"> pour une nouvelle ligne</span> |
| • |
| <kbd class="px-1.5 py-0.5 bg-gray-100 dark:bg-gray-800 rounded border border-gray-300 dark:border-gray-700">Shift+Entrée</kbd> |
| <span class="hidden sm:inline"> pour une nouvelle ligne</span> |
| <span class="sm:hidden"> pour envoyer</span> |
| </div> |
| </form> |
| </div> |
| </main> |
| |
| <script> |
| document.addEventListener('DOMContentLoaded', () => { |
| |
| const chatForm = document.getElementById('chat-form'); |
| const promptInput = document.getElementById('prompt'); |
| const chatMessages = document.getElementById('chat-messages'); |
| const loadingIndicator = document.getElementById('loading-indicator'); |
| const historyLoadingIndicator = document.getElementById('history-loading'); |
| const errorMessageDiv = document.getElementById('error-message'); |
| const errorTextP = document.getElementById('error-text'); |
| const dismissErrorBtn = document.getElementById('dismiss-error'); |
| const webSearchToggle = document.getElementById('web_search_toggle'); |
| const fileUpload = document.getElementById('file_upload'); |
| const fileChip = document.getElementById('file-chip'); |
| const fileNameSpan = document.getElementById('file-name'); |
| const clearFileButton = document.getElementById('clear-file'); |
| const filePreview = document.getElementById('file-preview'); |
| const previewArea = document.getElementById('preview-area'); |
| const sendButton = document.getElementById('send-button'); |
| const clearForm = document.getElementById('clear-form'); |
| const advancedToggle = document.getElementById('advanced_reasoning_toggle'); |
| const advancedCooldownTimerSpan = document.getElementById('advanced-cooldown-timer'); |
| const themeToggleBtn = document.getElementById('theme-toggle'); |
| const API_CHAT_ENDPOINT = '/api/chat'; |
| const API_HISTORY_ENDPOINT = '/api/history'; |
| const CLEAR_ENDPOINT = '/clear'; |
| const COOLDOWN_DURATION = 60 * 1000; |
| const MOBILE_BREAKPOINT = 640; |
| let advancedToggleCooldownEndTime = 0; |
| let isComposing = false; |
| |
| |
| function initializeTheme() { |
| if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { |
| document.documentElement.classList.add('dark'); |
| } else { |
| document.documentElement.classList.remove('dark'); |
| } |
| } |
| function toggleTheme() { |
| if (document.documentElement.classList.contains('dark')) { |
| document.documentElement.classList.remove('dark'); |
| localStorage.theme = 'light'; |
| } else { |
| document.documentElement.classList.add('dark'); |
| localStorage.theme = 'dark'; |
| } |
| } |
| themeToggleBtn.addEventListener('click', toggleTheme); |
| initializeTheme(); |
| |
| |
| function adjustTextareaHeight(textarea) { |
| textarea.style.height = 'auto'; |
| const style = window.getComputedStyle(textarea); |
| const maxHeight = parseInt(style.maxHeight, 10); |
| const newHeight = Math.min(maxHeight || 200, textarea.scrollHeight); |
| textarea.style.height = newHeight + 'px'; |
| } |
| |
| |
| function scrollToBottom(smooth = true) { |
| setTimeout(() => { |
| chatMessages.scrollTo({ top: chatMessages.scrollHeight, behavior: smooth ? 'smooth' : 'auto' }); |
| }, 50); |
| } |
| |
| |
| function showLoading(show) { |
| if (show) { |
| loadingIndicator.classList.remove('hidden'); |
| chatMessages.appendChild(loadingIndicator); |
| scrollToBottom(); |
| } else { |
| loadingIndicator.classList.add('hidden'); |
| } |
| sendButton.disabled = show; |
| promptInput.disabled = show; |
| fileUpload.disabled = show; |
| if (show) { |
| clearFileButton.style.pointerEvents = 'none'; |
| clearFileButton.style.opacity = '0.5'; |
| } else { |
| clearFileButton.style.pointerEvents = 'auto'; |
| clearFileButton.style.opacity = '0.7'; |
| } |
| } |
| |
| |
| function displayError(message) { |
| errorTextP.textContent = message || "Une erreur inconnue est survenue."; |
| errorMessageDiv.classList.remove('hidden'); |
| } |
| dismissErrorBtn.addEventListener('click', () => { errorMessageDiv.classList.add('hidden'); }); |
| |
| |
| function addMessageToChat(role, content, isHtml = false) { |
| errorMessageDiv.classList.add('hidden'); |
| const messageWrapper = document.createElement('div'); |
| messageWrapper.classList.add('flex', 'w-full', role === 'user' ? 'justify-end' : 'justify-start'); |
| |
| let messageContentHtml = ''; |
| if (isHtml) { |
| messageContentHtml = content; |
| } else { |
| messageContentHtml = `<p class="text-sm sm:text-base whitespace-pre-wrap">${escapeHtml(content)}</p>`; |
| } |
| |
| let bubbleHtml = ''; |
| if (role === 'user') { |
| bubbleHtml = ` |
| <div class="message-bubble user-message"> |
| ${messageContentHtml} |
| </div> |
| `; |
| } else { |
| bubbleHtml = ` |
| <div class="flex items-start space-x-2 max-w-[85%]"> |
| <div class="w-8 h-8 rounded-full bg-primary-100 flex items-center justify-center flex-shrink-0 dark:bg-primary-900/50"> |
| <span class="text-lg">✨</span> |
| </div> |
| <div class="message-bubble assistant-message bg-secondary-50 text-secondary-900 border border-secondary-200 relative"> |
| <div class="prose prose-sm sm:prose-base max-w-none dark:prose-invert"> |
| ${messageContentHtml} |
| </div> |
| <button class="copy-btn text-xs bg-white/90 dark:bg-gray-800/90 hover:bg-gray-100 dark:hover:bg-gray-700 py-1 px-2 rounded text-gray-600 dark:text-gray-300 flex items-center"> |
| <i class="fa-regular fa-copy mr-1"></i> Copier |
| </button> |
| </div> |
| </div> |
| `; |
| } |
| messageWrapper.innerHTML = bubbleHtml; |
| |
| |
| const copyBtns = messageWrapper.querySelectorAll('.copy-btn'); |
| copyBtns.forEach(btn => { |
| btn.addEventListener('click', function() { |
| const textToCopy = this.closest('.message-bubble').querySelector('.prose').innerText || this.closest('.message-bubble').innerText; |
| navigator.clipboard.writeText(textToCopy).then(() => { |
| const originalText = this.innerHTML; |
| this.innerHTML = '<i class="fa-solid fa-check mr-1"></i> Copié'; |
| setTimeout(() => { this.innerHTML = originalText; }, 2000); |
| }).catch(err => console.error('Copy failed:', err)); |
| }); |
| }); |
| |
| |
| const codeBlocks = messageWrapper.querySelectorAll('pre'); |
| codeBlocks.forEach(pre => { |
| const code = pre.querySelector('code'); |
| if (!code) return; |
| if (pre.querySelector('.code-copy-btn')) return; |
| const copyButton = document.createElement('button'); |
| copyButton.innerHTML = '<i class="fa-regular fa-copy"></i>'; |
| copyButton.className = 'code-copy-btn p-1.5 bg-gray-200/50 dark:bg-gray-700/50 rounded text-gray-600 dark:text-gray-300 hover:bg-gray-300/70 dark:hover:bg-gray-600/70 tooltip'; |
| copyButton.setAttribute('aria-label', 'Copier le code'); |
| const tooltipText = document.createElement('span'); |
| tooltipText.className = 'tooltip-text !text-xs'; |
| tooltipText.textContent = 'Copier le code'; |
| copyButton.appendChild(tooltipText); |
| copyButton.addEventListener('click', () => { |
| navigator.clipboard.writeText(code.innerText).then(() => { |
| tooltipText.textContent = 'Copié!'; |
| copyButton.innerHTML = '<i class="fa-solid fa-check"></i>'; |
| setTimeout(() => { |
| copyButton.innerHTML = '<i class="fa-regular fa-copy"></i>'; |
| copyButton.appendChild(tooltipText); |
| tooltipText.textContent = 'Copier le code'; |
| }, 2000); |
| }).catch(err => { |
| tooltipText.textContent = 'Erreur copie'; |
| console.error('Failed to copy code: ', err); |
| setTimeout(() => { tooltipText.textContent = 'Copier le code'; }, 2000); |
| }); |
| }); |
| pre.appendChild(copyButton); |
| }); |
| |
| chatMessages.insertBefore(messageWrapper, loadingIndicator); |
| scrollToBottom(); |
| } |
| function escapeHtml(unsafe) { |
| return unsafe |
| .replace(/&/g, "&") |
| .replace(/</g, "<") |
| .replace(/>/g, ">") |
| .replace(/"/g, """) |
| .replace(/'/g, "'"); |
| } |
| |
| |
| function startAdvancedCooldownTimer() { |
| advancedToggle.disabled = true; |
| advancedToggleCooldownEndTime = Date.now() + COOLDOWN_DURATION; |
| const intervalId = setInterval(() => { |
| const now = Date.now(); |
| if (now >= advancedToggleCooldownEndTime) { |
| clearInterval(intervalId); |
| advancedCooldownTimerSpan.classList.add('hidden'); |
| advancedToggle.disabled = false; |
| advancedToggleCooldownEndTime = 0; |
| } else { |
| const remainingSeconds = Math.ceil((advancedToggleCooldownEndTime - now) / 1000); |
| advancedCooldownTimerSpan.textContent = `(${remainingSeconds}s)`; |
| advancedCooldownTimerSpan.classList.remove('hidden'); |
| } |
| }, 1000); |
| const remainingSeconds = Math.ceil((advancedToggleCooldownEndTime - Date.now()) / 1000); |
| advancedCooldownTimerSpan.textContent = `(${remainingSeconds}s)`; |
| advancedCooldownTimerSpan.classList.remove('hidden'); |
| } |
| |
| |
| async function loadChatHistory() { |
| historyLoadingIndicator.style.display = 'flex'; |
| try { |
| const response = await fetch(API_HISTORY_ENDPOINT); |
| if (!response.ok) { |
| let errorMsg = `Erreur serveur (${response.status})`; |
| try { |
| const errData = await response.json(); |
| errorMsg = errData.error || errorMsg; |
| } catch (e) {} |
| throw new Error(errorMsg); |
| } |
| const data = await response.json(); |
| const messagesToRemove = chatMessages.querySelectorAll(':scope > *:not(#loading-indicator)'); |
| messagesToRemove.forEach(el => el.remove()); |
| loadingIndicator.classList.add('hidden'); |
| if (data.success && Array.isArray(data.history)) { |
| if (data.history.length === 0) { |
| addMessageToChat('assistant', "Bonjour ! Je suis Mariam, votre assistant IA. Comment puis-je vous aider aujourd'hui ?", true); |
| } else { |
| data.history.forEach(message => { |
| const isAssistantHtml = message.role === 'assistant'; |
| addMessageToChat(message.role, message.text, isAssistantHtml); |
| }); |
| } |
| scrollToBottom(false); |
| } else { |
| throw new Error(data.error || "Format de réponse invalide pour l'historique."); |
| } |
| } catch (error) { |
| displayError(`Impossible de charger l'historique: ${error.message}`); |
| if (chatMessages.querySelectorAll(':scope > *:not(#loading-indicator):not(#history-loading)').length === 0) { |
| addMessageToChat('assistant', "Bonjour ! Je suis Mariam. Je n'ai pas pu charger notre conversation précédente. Comment puis-je vous aider ?", true); |
| } |
| } finally { |
| historyLoadingIndicator.remove(); |
| promptInput.focus(); |
| adjustTextareaHeight(promptInput); |
| } |
| } |
| |
| |
| function clearFileInput() { |
| fileUpload.value = ''; |
| fileChip.classList.add('hidden'); |
| fileNameSpan.textContent = ''; |
| fileNameSpan.title = ''; |
| filePreview.innerHTML = ''; |
| filePreview.classList.add('hidden'); |
| previewArea.classList.add('hidden'); |
| } |
| fileUpload.addEventListener('change', () => { |
| if (fileUpload.files.length > 0) { |
| const file = fileUpload.files[0]; |
| const name = file.name; |
| fileNameSpan.textContent = name; |
| fileNameSpan.title = name; |
| fileChip.classList.remove('hidden'); |
| filePreview.innerHTML = ''; |
| if (file.type.startsWith('image/')) { |
| const reader = new FileReader(); |
| reader.onload = (e) => { |
| filePreview.innerHTML = ` |
| <div class="file-preview"> |
| <img src="${e.target.result}" alt="Prévisualisation: ${escapeHtml(name)}"> |
| </div>`; |
| filePreview.classList.remove('hidden'); |
| previewArea.classList.remove('hidden'); |
| }; |
| reader.onerror = () => { |
| filePreview.innerHTML = `<p class="text-red-500 text-xs text-center p-2">Erreur lecture image</p>`; |
| filePreview.classList.remove('hidden'); |
| previewArea.classList.remove('hidden'); |
| }; |
| reader.readAsDataURL(file); |
| } else { |
| filePreview.innerHTML = ` |
| <div class="flex items-center justify-center p-3"> |
| <div class="bg-gray-100 dark:bg-gray-800 p-3 rounded-lg text-center"> |
| <i class="fa-solid ${getFileIcon(file.type)} text-3xl text-gray-500 dark:text-gray-400 mb-2"></i> |
| <p class="text-xs text-gray-500 dark:text-gray-400">${formatFileSize(file.size)}</p> |
| </div> |
| </div>`; |
| filePreview.classList.remove('hidden'); |
| previewArea.classList.remove('hidden'); |
| } |
| } else { |
| clearFileInput(); |
| } |
| }); |
| function getFileIcon(fileType) { |
| if (!fileType) return 'fa-file'; |
| if (fileType.includes('pdf')) return 'fa-file-pdf'; |
| if (fileType.includes('text')) return 'fa-file-lines'; |
| return 'fa-file'; |
| } |
| function formatFileSize(bytes) { |
| if (bytes === 0) return '0 octets'; |
| const k = 1024; |
| const sizes = ['octets', 'Ko', 'Mo', 'Go', 'To']; |
| const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]; |
| } |
| clearFileButton.addEventListener('click', clearFileInput); |
| |
| |
| promptInput.addEventListener('input', () => { |
| adjustTextareaHeight(promptInput); |
| }); |
| promptInput.addEventListener('keydown', (e) => { |
| if (isComposing) return; |
| const isMobile = window.innerWidth < MOBILE_BREAKPOINT; |
| if (e.key === 'Enter') { |
| if (isMobile && !e.shiftKey) { |
| setTimeout(() => adjustTextareaHeight(promptInput), 0); |
| } else if (!isMobile && !e.shiftKey) { |
| e.preventDefault(); |
| if (!sendButton.disabled && (promptInput.value.trim() || fileUpload.files.length > 0)) { |
| chatForm.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })); |
| } |
| } else if (e.shiftKey) { |
| setTimeout(() => adjustTextareaHeight(promptInput), 0); |
| if (isMobile) { |
| e.preventDefault(); |
| if (!sendButton.disabled && (promptInput.value.trim() || fileUpload.files.length > 0)) { |
| chatForm.dispatchEvent(new Event('submit', { bubbles: true, cancelable: true })); |
| } |
| } |
| } |
| } |
| }); |
| promptInput.addEventListener('compositionstart', () => { isComposing = true; }); |
| promptInput.addEventListener('compositionend', () => { |
| isComposing = false; |
| setTimeout(() => adjustTextareaHeight(promptInput), 0); |
| }); |
| adjustTextareaHeight(promptInput); |
| |
| |
| chatForm.addEventListener('submit', async (e) => { |
| e.preventDefault(); |
| const prompt = promptInput.value.trim(); |
| const file = fileUpload.files[0]; |
| const useWebSearch = webSearchToggle.checked; |
| const useAdvanced = advancedToggle.checked; |
| if (sendButton.disabled) return; |
| if (!prompt && !file) { |
| promptInput.focus(); |
| return; |
| } |
| errorMessageDiv.classList.add('hidden'); |
| if (useAdvanced) { |
| const now = Date.now(); |
| if (advancedToggleCooldownEndTime > 0 && now < advancedToggleCooldownEndTime) { |
| const remainingSeconds = Math.ceil((advancedToggleCooldownEndTime - now) / 1000); |
| displayError(`Le raisonnement avancé est disponible dans ${remainingSeconds} seconde(s).`); |
| return; |
| } |
| } |
| let userMessageText = prompt; |
| if (file && file.name) { |
| userMessageText = prompt ? `${prompt}\n[Fichier: ${file.name}]` : `[Fichier joint: ${file.name}]`; |
| } |
| addMessageToChat('user', userMessageText); |
| const formData = new FormData(); |
| formData.append('prompt', prompt); |
| formData.append('web_search', useWebSearch); |
| formData.append('advanced_reasoning', useAdvanced); |
| if (file) { |
| formData.append('file', file); |
| } |
| promptInput.value = ''; |
| adjustTextareaHeight(promptInput); |
| clearFileInput(); |
| if (useAdvanced) { |
| startAdvancedCooldownTimer(); |
| advancedToggle.checked = false; |
| } |
| webSearchToggle.checked = false; |
| showLoading(true); |
| try { |
| const response = await fetch(API_CHAT_ENDPOINT, { method: 'POST', body: formData }); |
| const data = await response.json(); |
| if (!response.ok) { |
| throw new Error(data.error || `Erreur serveur: ${response.status} ${response.statusText}`); |
| } |
| if (data.success && data.message) { |
| addMessageToChat('assistant', data.message, true); |
| } else { |
| throw new Error(data.error || "Réponse invalide ou vide du serveur."); |
| } |
| } catch (error) { |
| console.error("Chat Error:", error); |
| addMessageToChat('assistant', `<p class="text-red-600 dark:text-red-400">Désolé, une erreur est survenue :<br>${escapeHtml(error.message)}</p>`, true); |
| } finally { |
| showLoading(false); |
| promptInput.focus(); |
| } |
| }); |
| |
| |
| clearForm.addEventListener('submit', async (e) => { |
| e.preventDefault(); |
| const confirmDialog = document.createElement('div'); |
| confirmDialog.className = 'fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4'; |
| confirmDialog.innerHTML = ` |
| <div class="bg-white dark:bg-gray-800 p-6 rounded-lg shadow-xl max-w-sm w-full animate-[message-fade-in_0.2s_ease-out]"> |
| <h3 class="text-lg font-semibold mb-3 text-gray-900 dark:text-gray-100">Confirmer l'effacement</h3> |
| <p class="text-sm text-gray-600 dark:text-gray-300 mb-5">Êtes-vous sûr de vouloir effacer toute la conversation ? Cette action est irréversible.</p> |
| <div class="flex justify-end space-x-3"> |
| <button type="button" id="cancel-clear" class="px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-700 rounded hover:bg-gray-200 dark:hover:bg-gray-600 transition-colors focus:outline-none focus:ring-2 focus:ring-gray-400"> |
| Annuler |
| </button> |
| <button type="button" id="confirm-clear" class="px-4 py-2 text-sm font-medium text-white bg-red-600 rounded hover:bg-red-700 transition-colors focus:outline-none focus:ring-2 focus:ring-red-500"> |
| Effacer |
| </button> |
| </div> |
| </div> |
| `; |
| const closeModal = () => { |
| if (document.body.contains(confirmDialog)) { |
| document.body.removeChild(confirmDialog); |
| document.body.style.overflow = ''; |
| } |
| }; |
| confirmDialog.querySelector('#cancel-clear').addEventListener('click', closeModal); |
| confirmDialog.querySelector('#confirm-clear').addEventListener('click', async () => { |
| closeModal(); |
| const clearButton = e.target.querySelector('button[type="submit"]'); |
| const originalButtonContent = clearButton.innerHTML; |
| clearButton.disabled = true; |
| clearButton.innerHTML = '<i class="fa-solid fa-spinner fa-spin mr-1.5"></i><span class="hidden sm:inline">Effacement...</span>'; |
| try { |
| const response = await fetch(CLEAR_ENDPOINT, { |
| method: 'POST', |
| headers: { |
| 'X-Requested-With': 'XMLHttpRequest' |
| } |
| }); |
| const data = await response.json(); |
| if (response.ok && data.success) { |
| const messagesToRemove = chatMessages.querySelectorAll(':scope > *:not(#loading-indicator)'); |
| messagesToRemove.forEach(el => el.remove()); |
| addMessageToChat('assistant', "Conversation effacée. Comment puis-je vous aider maintenant ?", true); |
| errorMessageDiv.classList.add('hidden'); |
| } else { |
| throw new Error(data.error || "Impossible d'effacer la conversation."); |
| } |
| } catch (error) { |
| console.error("Clear Error:", error); |
| displayError(`Erreur lors de l'effacement: ${error.message}`); |
| } finally { |
| clearButton.innerHTML = originalButtonContent; |
| clearButton.disabled = false; |
| promptInput.focus(); |
| } |
| }); |
| document.body.appendChild(confirmDialog); |
| document.body.style.overflow = 'hidden'; |
| confirmDialog.querySelector('#cancel-clear').focus(); |
| }); |
| |
| |
| loadChatHistory(); |
| |
| |
| const observeMarkdownTables = () => { |
| const observer = new MutationObserver(mutations => { |
| mutations.forEach(mutation => { |
| if (mutation.addedNodes.length) { |
| mutation.addedNodes.forEach(node => { |
| if (node.nodeType === 1) { |
| const tables = node.matches('.prose table') ? [node] : node.querySelectorAll('.prose table'); |
| tables.forEach(table => { |
| if (!table.closest('.table-wrapper')) { |
| const wrapper = document.createElement('div'); |
| wrapper.className = 'table-wrapper'; |
| table.parentNode.insertBefore(wrapper, table); |
| wrapper.appendChild(table); |
| } |
| }); |
| } |
| }); |
| } |
| }); |
| }); |
| observer.observe(chatMessages, { childList: true, subtree: true }); |
| }; |
| observeMarkdownTables(); |
| |
| |
| const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)'); |
| const handleSchemeChange = (e) => { |
| if (!localStorage.getItem('theme')) { |
| if (e.matches) { |
| document.documentElement.classList.add('dark'); |
| } else { |
| document.documentElement.classList.remove('dark'); |
| } |
| } |
| }; |
| prefersDarkScheme.addEventListener('change', handleSchemeChange); |
| }); |
| </script> |
| </body> |
| </html> |
|
|