104 lines
2.9 KiB
JavaScript
104 lines
2.9 KiB
JavaScript
// ── Chat Page ──
|
|
|
|
registerPage('chat', {
|
|
async init(params) {
|
|
const sessionId = params.id;
|
|
if (!sessionId) return navigateTo('dashboard');
|
|
|
|
currentSessionId = sessionId;
|
|
|
|
const session = await apiGetSession(sessionId);
|
|
if (!session) return navigateTo('dashboard');
|
|
|
|
// Kein Agent = kein Chat
|
|
if (!session.agentId) {
|
|
navigateTo('session', { id: sessionId });
|
|
return;
|
|
}
|
|
|
|
const titleEl = document.getElementById('sessionTitle');
|
|
if (titleEl) {
|
|
titleEl.textContent = session.name || 'Unbenannt';
|
|
}
|
|
|
|
// Zurück zur Session-Edit-Seite
|
|
const backBtn = document.getElementById('chatBackBtn');
|
|
if (backBtn) {
|
|
backBtn.addEventListener('click', () => navigateTo('session', { id: sessionId }));
|
|
}
|
|
|
|
// Send
|
|
const sendBtn = document.getElementById('send');
|
|
if (sendBtn) sendBtn.addEventListener('click', sendMessage);
|
|
|
|
// Ctrl+Enter
|
|
const inputEl = document.getElementById('input');
|
|
if (inputEl) {
|
|
inputEl.addEventListener('keydown', (e) => {
|
|
if (e.ctrlKey && e.key === 'Enter') sendMessage();
|
|
});
|
|
inputEl.addEventListener('input', function () {
|
|
this.style.height = 'auto';
|
|
this.style.height = Math.min(this.scrollHeight, 150) + 'px';
|
|
});
|
|
}
|
|
|
|
// File Upload
|
|
const fileBtn = document.getElementById('fileBtn');
|
|
const fileInput = document.getElementById('fileInput');
|
|
if (fileBtn && fileInput) {
|
|
fileBtn.addEventListener('click', () => fileInput.click());
|
|
fileInput.addEventListener('change', (e) => {
|
|
if (e.target.files.length > 0) handleMultipleFiles(e.target.files);
|
|
});
|
|
}
|
|
|
|
// Drag & Drop
|
|
const chatContainer = document.querySelector('.chat-page');
|
|
if (chatContainer) {
|
|
chatContainer.addEventListener('dragover', (e) => { e.preventDefault(); e.stopPropagation(); });
|
|
chatContainer.addEventListener('drop', (e) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
if (e.dataTransfer.files.length > 0) handleMultipleFiles(e.dataTransfer.files);
|
|
});
|
|
}
|
|
|
|
// Paste
|
|
if (inputEl) {
|
|
inputEl.addEventListener('paste', (e) => {
|
|
const items = e.clipboardData?.items;
|
|
if (!items) return;
|
|
for (const item of items) {
|
|
if (item.kind === 'file') {
|
|
e.preventDefault();
|
|
handleFileSelect(item.getAsFile());
|
|
return;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
// Archivieren
|
|
const archiveBtn = document.getElementById('archiveBtn');
|
|
if (archiveBtn) archiveBtn.addEventListener('click', clearChat);
|
|
|
|
// State zurücksetzen und History laden
|
|
messages.length = 0;
|
|
lastKnownMessageId = 0;
|
|
loadedArchiveCount = 0;
|
|
isStreaming = false;
|
|
|
|
await loadHistory();
|
|
},
|
|
|
|
destroy() {
|
|
stopHistoryPolling();
|
|
messages.length = 0;
|
|
lastKnownMessageId = 0;
|
|
loadedArchiveCount = 0;
|
|
isStreaming = false;
|
|
clearPendingFiles();
|
|
}
|
|
});
|