// ── Session Edit Page ── let _sessionImageData = null; let _sessionPasteHandler = null; registerPage('session', { async init(params) { const sessionId = params.id; const isNew = !sessionId; let session = null; if (!isNew) { session = await apiGetSession(sessionId); if (!session) return navigateTo('dashboard'); } const titleEl = document.getElementById('sessionPageTitle'); const nameEl = document.getElementById('sessionName'); const descEl = document.getElementById('sessionDescription'); const contextEl = document.getElementById('sessionContext'); const workspaceEl = document.getElementById('sessionWorkspace'); const agentSelect = document.getElementById('agentSelect'); const imagePreview = document.getElementById('sessionImagePreview'); const imageInput = document.getElementById('sessionImageInput'); const imageBtn = document.getElementById('sessionImageBtn'); const imageClearBtn = document.getElementById('sessionImageClearBtn'); const saveBtn = document.getElementById('sessionSaveBtn'); const cancelBtn = document.getElementById('sessionCancelBtn'); const deleteBtn = document.getElementById('sessionDeleteBtn'); const commandsList = document.getElementById('customCommandsList'); const addCommandBtn = document.getElementById('addCommandBtn'); _sessionImageData = null; if (session) { titleEl.textContent = session.name || 'Unbenannt'; nameEl.value = session.name || ''; descEl.value = session.description || ''; contextEl.value = session.context || ''; workspaceEl.value = session.workspace || ''; _sessionImageData = session.image || null; deleteBtn.hidden = false; if (session.image) { imagePreview.innerHTML = ``; } // Custom Commands laden loadCommandsIntoUI(commandsList, session.customCommands); } else { titleEl.textContent = 'Neue Session'; deleteBtn.hidden = true; } // Befehl hinzufügen addCommandBtn.addEventListener('click', () => createCommandRow(commandsList)); // Agents laden const agents = await apiGetAgents(); agentSelect.innerHTML = ''; for (const a of agents) { const opt = document.createElement('option'); opt.value = a.agentId; opt.textContent = `${a.name} (${a.model})`; agentSelect.appendChild(opt); } if (session && session.agentId) agentSelect.value = session.agentId; // Bild Upload imageBtn.addEventListener('click', () => imageInput.click()); imageInput.addEventListener('change', () => { const file = imageInput.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = () => { _sessionImageData = reader.result; imagePreview.innerHTML = ``; }; reader.readAsDataURL(file); }); imageClearBtn.addEventListener('click', () => { _sessionImageData = null; imagePreview.innerHTML = '?'; imageInput.value = ''; }); // Bild Paste (Ctrl+V) document.addEventListener('paste', _sessionPasteHandler = (e) => { const items = e.clipboardData?.items; if (!items) return; for (const item of items) { if (item.type.startsWith('image/')) { e.preventDefault(); const file = item.getAsFile(); if (!file) return; const reader = new FileReader(); reader.onload = () => { _sessionImageData = reader.result; imagePreview.innerHTML = ``; }; reader.readAsDataURL(file); return; } } }); // Workspace Browse const workspaceBrowse = document.getElementById('sessionWorkspaceBrowse'); if (workspaceBrowse) { workspaceBrowse.addEventListener('click', () => { openFolderBrowser({ startPath: workspaceEl.value.trim() || '/', onSelect(path) { workspaceEl.value = path; } }); }); } // Speichern saveBtn.addEventListener('click', async () => { const commands = getCommandsFromUI(commandsList); const data = { name: nameEl.value.trim() || 'Unbenannt', description: descEl.value.trim(), image: _sessionImageData, agentId: agentSelect.value || null, context: contextEl.value || null, workspace: workspaceEl.value.trim() || null, customCommands: commands }; if (isNew) { await apiCreateSession(data); } else { await apiUpdateSession(sessionId, data); } navigateTo('dashboard'); }); // Abbrechen cancelBtn.addEventListener('click', () => navigateTo('dashboard')); // Löschen deleteBtn.addEventListener('click', async () => { if (!confirm('Session wirklich löschen?')) return; await apiDeleteSession(sessionId); navigateTo('dashboard'); }); }, destroy() { if (_sessionPasteHandler) { document.removeEventListener('paste', _sessionPasteHandler); _sessionPasteHandler = null; } _sessionImageData = null; } });