// ── 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'; } // Custom Command Buttons const cmdBar = document.getElementById('customCmdBar'); if (cmdBar) { cmdBar.innerHTML = ''; let cmds = []; try { cmds = JSON.parse(session.customCommands || '[]') || []; } catch(e) {} cmds.forEach(cmd => { if (!cmd.name || !cmd.command) return; const btn = document.createElement('button'); btn.className = 'custom-cmd-btn'; btn.textContent = cmd.name; btn.title = cmd.command; btn.addEventListener('click', async () => { btn.disabled = true; btn.classList.add('running'); try { await apiExecCommand(sessionId, cmd.command); } catch(e) { console.error('Exec failed:', e); } setTimeout(() => { btn.disabled = false; btn.classList.remove('running'); }, 1000); }); cmdBar.appendChild(btn); }); } // 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); const inputEl = document.getElementById('input'); if (inputEl) { inputEl.addEventListener('keydown', (e) => { if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) { e.preventDefault(); sendMessage(); } }); inputEl.addEventListener('input', function () { this.style.height = 'auto'; this.style.height = Math.min(this.scrollHeight, 150) + 'px'; }); inputEl.dispatchEvent(new Event('input')) } // Workspace Explorer const explorerBtn = document.getElementById('explorerBtn'); if (explorerBtn) { explorerBtn.addEventListener('click', () => { openFileExplorer({ sessionId, onSelect(ref) { addPendingRef(ref); document.getElementById('input')?.focus(); } }); }); } // 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; } } }); } // Header-Menü (3-Dot) const chatMenuBtn = document.getElementById('chatMenuBtn'); const chatMenuDropdown = document.getElementById('chatMenuDropdown'); if (chatMenuBtn && chatMenuDropdown) { chatMenuBtn.addEventListener('click', (e) => { e.stopPropagation(); chatMenuDropdown.hidden = !chatMenuDropdown.hidden; }); document.addEventListener('click', this._closeMenu = () => { chatMenuDropdown.hidden = true; }); } // Archivieren const archiveBtn = document.getElementById('archiveBtn'); if (archiveBtn) archiveBtn.addEventListener('click', () => { if (chatMenuDropdown) chatMenuDropdown.hidden = true; clearChat(); }); // State zurücksetzen und History laden messages.length = 0; lastKnownMessageId = 0; loadedArchiveCount = 0; isStreaming = false; await loadHistory(); }, destroy() { if (this._closeMenu) { document.removeEventListener('click', this._closeMenu); this._closeMenu = null; } stopHistoryPolling(); messages.length = 0; lastKnownMessageId = 0; loadedArchiveCount = 0; isStreaming = false; clearPendingFiles(); clearPendingRefs(); } });