async function sendMessage(e) { e.preventDefault(); const input = document.getElementById('user-input'); const chatWindow = document.getElementById('chat-window'); const sendBtn = document.getElementById('send-btn'); const text = input.value.trim(); if (!text) return; if (inputHistory[inputHistory.length - 1] !== text) { inputHistory.push(text); if (inputHistory.length > 50) inputHistory.shift(); localStorage.setItem(STORAGE_KEY, JSON.stringify(inputHistory)); } historyIndex = -1; chatWindow.innerHTML += `
${text}
`; input.value = ''; input.style.height = 'auto'; chatWindow.scrollTop = chatWindow.scrollHeight; sendBtn.disabled = true; sendBtn.classList.add('opacity-50'); const endpoint = IS_GUEST ? '/api/v1/chat/guest' : '/api/v1/chat'; const headers = { 'Content-Type': 'application/json' }; if (!IS_GUEST) { headers['Authorization'] = `Bearer ${API_TOKEN}`; } try { const res = await fetch(endpoint, { method: 'POST', headers: headers, body: JSON.stringify({ session_id: SESSION_ID, message: text }) }); if (res.status === 401 && !IS_GUEST) { logout(); return; } const data = await res.json(); const assistantTitle = IS_GUEST ? "Локальная нейросеть (Гость)" : "ИИ-Ассистент SCUD Orion AI"; chatWindow.innerHTML += `

${assistantTitle}

${data.reply}

`; chatWindow.scrollTop = chatWindow.scrollHeight; if (!IS_GUEST) loadTasks(); } catch (err) { chatWindow.innerHTML += `
Ошибка связи с сервером.
`; } finally { sendBtn.disabled = false; sendBtn.classList.remove('opacity-50'); } }