diff --git a/modules/web_api/static/css/styles.css b/modules/web_api/static/css/styles.css index 78f3e4d..d4e3056 100644 --- a/modules/web_api/static/css/styles.css +++ b/modules/web_api/static/css/styles.css @@ -18,3 +18,43 @@ body { min-height: 100vh; min-height: -webkit-fill-available; } + +/* Стилизация Markdown-контента ответов ассистента в стиле Gemini */ +.chat-markdown-content h1, +.chat-markdown-content h2, +.chat-markdown-content h3, +.chat-markdown-content h4 { + font-weight: 700; + color: #0f172a; + margin-top: 0.85rem; + margin-bottom: 0.35rem; + font-size: 0.95rem; +} +.chat-markdown-content p { + margin-bottom: 0.45rem; +} +.chat-markdown-content p:last-child { + margin-bottom: 0; +} +.chat-markdown-content ul { + list-style-type: disc; + margin-left: 1.25rem; + margin-bottom: 0.5rem; +} +.chat-markdown-content ol { + list-style-type: decimal; + margin-left: 1.25rem; + margin-bottom: 0.5rem; +} +.chat-markdown-content li { + margin-bottom: 0.2rem; +} +.chat-markdown-content code:not([id^="code-block-"]) { + padding: 0.15rem 0.35rem; + border-radius: 0.375rem; + background-color: #f1f5f9; + color: #4338ca; + font-size: 0.825rem; + border: 1px solid #e2e8f0; + font-family: monospace; +} diff --git a/modules/web_api/static/index.html b/modules/web_api/static/index.html index 2299a9b..8a6ca0f 100644 --- a/modules/web_api/static/index.html +++ b/modules/web_api/static/index.html @@ -248,6 +248,7 @@ + diff --git a/modules/web_api/static/js/chat/messages.js b/modules/web_api/static/js/chat/messages.js index 279197b..3389e9a 100644 --- a/modules/web_api/static/js/chat/messages.js +++ b/modules/web_api/static/js/chat/messages.js @@ -1,10 +1,116 @@ /** * =============================================================================== * FILE: modules/web_api/static/js/chat/messages.js - * ROLE: Рендеринг пузырей сообщений пользователя, ассистента и индикатора загрузки. + * ROLE: Рендеринг сообщений на базе marked.js с поддержкой marked v4-v14+. * =============================================================================== */ +function safeEscapeCode(str) { + if (!str) return ''; + return String(str) + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + .replace(/'/g, '''); +} + +function renderMarkdownGemini(rawText) { + if (!rawText || typeof rawText !== 'string') return ''; + + if (typeof marked === 'undefined') { + return safeEscapeCode(rawText).replace(/\n/g, '
'); + } + + const renderer = new marked.Renderer(); + + // Переопределяем ТОЛЬКО блоки кода (совместимо со всеми версиями marked) + renderer.code = function(arg1, arg2) { + // Поддержка и старых версий (code, lang), и новых (token) + let code = ''; + let lang = 'code'; + + if (typeof arg1 === 'object' && arg1 !== null) { + code = arg1.text || ''; + lang = arg1.lang || 'code'; + } else { + code = arg1 || ''; + lang = arg2 || 'code'; + } + + const language = (lang || 'code').trim(); + const blockId = 'code-block-' + Math.random().toString(36).substr(2, 9); + + return ` +
+
+ ${safeEscapeCode(language)} + +
+
${safeEscapeCode(code)}
+
+ `; + }; + + marked.setOptions({ + renderer: renderer, + gfm: true, + breaks: true + }); + + return marked.parse(rawText); +} + +window.copyCodeBlock = function(blockId, btnElement) { + const codeEl = document.getElementById(blockId); + if (!codeEl) return; + const textToCopy = codeEl.innerText; + + function showSuccessFeedback() { + const originalHtml = btnElement.innerHTML; + btnElement.innerHTML = `Скопировано!`; + btnElement.classList.add('bg-slate-800'); + setTimeout(() => { + btnElement.innerHTML = originalHtml; + btnElement.classList.remove('bg-slate-800'); + }, 2000); + } + + if (navigator.clipboard && window.isSecureContext) { + navigator.clipboard.writeText(textToCopy) + .then(showSuccessFeedback) + .catch(() => fallbackCopy(textToCopy)); + } else { + fallbackCopy(textToCopy); + } + + function fallbackCopy(text) { + try { + const textArea = document.createElement("textarea"); + textArea.value = text; + textArea.style.position = "fixed"; + textArea.style.left = "-999999px"; + textArea.style.top = "-999999px"; + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + const successful = document.execCommand('copy'); + document.body.removeChild(textArea); + if (successful) { + showSuccessFeedback(); + } else { + console.warn("Fallback execCommand copy failed"); + } + } catch (err) { + console.error("Не удалось скопировать код в буфер:", err); + } + } +}; + window.appendUserMessage = function(text, filename = null) { const container = document.getElementById("chat-messages-container"); if (!container) return; @@ -15,7 +121,7 @@ window.appendUserMessage = function(text, filename = null) { fileBadge = `
- ${escapeHtml(filename)} + ${safeEscapeCode(filename)}
`; } @@ -24,7 +130,7 @@ window.appendUserMessage = function(text, filename = null) {
${fileBadge} -
${escapeHtml(text)}
+
${safeEscapeCode(text)}
@@ -73,9 +179,9 @@ window.appendAssistantMessage = function(text, buttons = [], actionPayload = nul buttonsHtml = `
${allButtons.map(b => ` - `).join('')}
@@ -83,7 +189,7 @@ window.appendAssistantMessage = function(text, buttons = [], actionPayload = nul } } - const bodyContent = isHtmlBody ? text : escapeHtml(text); + const bodyContent = isHtmlBody ? text : renderMarkdownGemini(text); const html = `
@@ -92,7 +198,7 @@ window.appendAssistantMessage = function(text, buttons = [], actionPayload = nul
ИИ-ассистент SCUD Orion AI
-
${bodyContent}
+
${bodyContent}
${payloadHtml} ${buttonsHtml}