diff --git a/public/claude-ide/terminal.js b/public/claude-ide/terminal.js index f2aae7d1..98c40621 100644 --- a/public/claude-ide/terminal.js +++ b/public/claude-ide/terminal.js @@ -1213,8 +1213,39 @@ class TerminalManager { const currentSessionId = window.chatSessionId; if (!currentSessionId) { - showToast('No active Claude Code session. Start a chat first!', 'warning'); - return; + showToast('No active session. Creating a new session...', 'warning'); + + // Create a new session automatically + try { + const res = await fetch('/claude/api/claude/sessions', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ workingDir: process.env.HOME || '/home/uroma' }) + }); + + const data = await res.json(); + + if (!data.success) { + throw new Error(data.error || 'Failed to create session'); + } + + const newSessionId = data.session.id; + + // Update global session ID + window.chatSessionId = newSessionId; + window.attachedSessionId = newSessionId; + + // Update UI + const sessionIdEl = document.getElementById('current-session-id'); + if (sessionIdEl) sessionIdEl.textContent = newSessionId; + + // Now attach to the new session + return await this.attachToSession(terminalId); + } catch (error) { + console.error('[TerminalManager] Error creating session:', error); + showToast('Failed to create session. Please try creating one from the chat panel.', 'error'); + return; + } } try { @@ -1226,7 +1257,19 @@ class TerminalManager { const data = await res.json(); - if (!data.success) { + if (!res.ok || !data.success) { + // Handle "Session not found" error specifically + if (data.error && data.error.includes('not found')) { + showToast(`Session "${currentSessionId.substring(0, 12)}..." no longer exists. Creating a new one...`, 'warning'); + + // Clear the invalid session ID + window.chatSessionId = null; + window.attachedSessionId = null; + + // Recursively call to create a new session + return await this.attachToSession(terminalId); + } + throw new Error(data.error || 'Failed to attach'); }