109 lines
2.8 KiB
JavaScript
109 lines
2.8 KiB
JavaScript
// ── Session State ──
|
|
|
|
let currentSessionId = null;
|
|
|
|
function getCurrentSessionId() {
|
|
return currentSessionId;
|
|
}
|
|
|
|
async function initSession() {
|
|
// SessionId aus localStorage oder neue erstellen
|
|
const stored = localStorage.getItem('sessionId');
|
|
if (stored) {
|
|
currentSessionId = stored;
|
|
} else {
|
|
const session = await apiCreateSession();
|
|
currentSessionId = session.sessionId;
|
|
localStorage.setItem('sessionId', currentSessionId);
|
|
}
|
|
|
|
await refreshSessionList();
|
|
}
|
|
|
|
async function createNewSession() {
|
|
const session = await apiCreateSession();
|
|
currentSessionId = session.sessionId;
|
|
localStorage.setItem('sessionId', currentSessionId);
|
|
|
|
// Chat leeren und neu laden
|
|
messages.length = 0;
|
|
chatEl().innerHTML = '';
|
|
lastKnownMessageId = 0;
|
|
loadedArchiveCount = 0;
|
|
setMeta('');
|
|
|
|
await refreshSessionList();
|
|
await loadHistory();
|
|
}
|
|
|
|
async function switchSession(sessionId) {
|
|
if (sessionId === currentSessionId) return;
|
|
|
|
currentSessionId = sessionId;
|
|
localStorage.setItem('sessionId', currentSessionId);
|
|
|
|
// Chat leeren und Session laden
|
|
messages.length = 0;
|
|
chatEl().innerHTML = '';
|
|
lastKnownMessageId = 0;
|
|
loadedArchiveCount = 0;
|
|
setMeta('');
|
|
|
|
await refreshSessionList();
|
|
await loadHistory();
|
|
}
|
|
|
|
async function refreshSessionList() {
|
|
const sessions = await apiGetSessions();
|
|
const list = $('#sessionList');
|
|
list.innerHTML = '';
|
|
|
|
for (const s of sessions) {
|
|
const div = document.createElement('div');
|
|
div.className = 'session-item' + (s.sessionId === currentSessionId ? ' active' : '');
|
|
div.dataset.sessionId = s.sessionId;
|
|
|
|
const name = document.createElement('span');
|
|
name.className = 'session-name';
|
|
name.textContent = s.name || 'Unbenannt';
|
|
name.addEventListener('dblclick', () => renameSession(s.sessionId, name));
|
|
|
|
const date = document.createElement('span');
|
|
date.className = 'session-date';
|
|
date.textContent = new Date(s.updatedAt).toLocaleString('de-AT', {
|
|
day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit'
|
|
});
|
|
|
|
div.appendChild(name);
|
|
div.appendChild(date);
|
|
div.addEventListener('click', () => switchSession(s.sessionId));
|
|
|
|
list.appendChild(div);
|
|
}
|
|
}
|
|
|
|
async function renameSession(sessionId, nameEl) {
|
|
const input = document.createElement('input');
|
|
input.type = 'text';
|
|
input.className = 'session-rename';
|
|
input.value = nameEl.textContent;
|
|
|
|
nameEl.replaceWith(input);
|
|
input.focus();
|
|
input.select();
|
|
|
|
const save = async () => {
|
|
const newName = input.value.trim();
|
|
if (newName) {
|
|
await apiUpdateSession(sessionId, { name: newName });
|
|
}
|
|
await refreshSessionList();
|
|
};
|
|
|
|
input.addEventListener('blur', save);
|
|
input.addEventListener('keydown', (e) => {
|
|
if (e.key === 'Enter') input.blur();
|
|
if (e.key === 'Escape') { input.value = nameEl.textContent; input.blur(); }
|
|
});
|
|
}
|