feat: история диалогов — SQLite + UI

- db.py: SQLite хранилище (messages таблица)
- Все сообщения (user/assistant/system) сохраняются на диск
- GET /api/history — история текущей вкладки
- GET /api/sessions — список всех диалогов с превью
- UI: история загружается при обновлении страницы
- UI: кнопка "История" показывает прошлые диалоги
- "Новый диалог" создаёт новый tabId (старый остаётся в БД)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
anor
2026-03-24 19:39:59 +10:00
co-authored by Claude Opus 4.6
parent 6954c41785
commit dce8ad2f7c
3 changed files with 157 additions and 9 deletions
+38 -2
View File
@@ -68,6 +68,7 @@ body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-
<div id="header">
<h1>Сметный ИИ-ассистент</h1>
<div class="header-actions">
<button onclick="showSessions()">История</button>
<button onclick="resetSession()">Новый диалог</button>
<button onclick="listFiles()">Файлы</button>
<button onclick="logout()">Выйти</button>
@@ -95,7 +96,7 @@ let token = localStorage.getItem('token');
let pendingFiles = [];
let streaming = false;
// Each tab gets its own session — allows multiple windows
const tabId = sessionStorage.getItem('tabId') || crypto.randomUUID();
let tabId = sessionStorage.getItem('tabId') || crypto.randomUUID();
sessionStorage.setItem('tabId', tabId);
// --- Init ---
@@ -109,6 +110,7 @@ function showChat() {
document.getElementById('login-screen').style.display = 'none';
document.getElementById('chat-screen').style.display = 'flex';
document.getElementById('msg-input').focus();
loadHistory();
}
// --- Auth ---
@@ -315,7 +317,9 @@ async function sendMessage() {
// --- Session ---
async function resetSession() {
await fetch(API + '/api/reset', {method: 'POST', headers: {...authHeaders(), 'Content-Type': 'application/json'}, body: '{}'});
// Create new tab = new session, old one stays in DB
tabId = crypto.randomUUID();
sessionStorage.setItem('tabId', tabId);
msgs.innerHTML = '';
addMsg('system', 'Новый диалог начат.');
}
@@ -345,6 +349,38 @@ async function listFiles() {
});
}
// --- History ---
async function loadHistory() {
try {
const r = await fetch(API + '/api/history', {headers: authHeaders()});
if (!r.ok) return;
const history = await r.json();
if (history.length === 0) return;
msgs.innerHTML = '';
for (const m of history) {
if (m.role === 'user') addMsg('user', m.content);
else if (m.role === 'assistant') addMsg('ai', m.content);
else if (m.role === 'system') addMsg('system', m.content);
}
} catch {}
}
async function showSessions() {
try {
const r = await fetch(API + '/api/sessions', {headers: authHeaders()});
if (!r.ok) return;
const sessions = await r.json();
if (sessions.length === 0) { addMsg('system', 'Нет сохранённых диалогов.'); return; }
let text = 'Прошлые диалоги:\n\n';
sessions.forEach((s, i) => {
const date = new Date(s.last_ts * 1000).toLocaleString('ru');
const preview = (s.last_question || '').slice(0, 60);
text += (i + 1) + '. ' + date + ' (' + s.msg_count + ' сообщ.) — ' + preview + '\n';
});
addMsg('system', text);
} catch {}
}
// Auto-resize textarea
document.getElementById('msg-input').addEventListener('input', function() {
this.style.height = 'auto';