fix: auto-create session when attaching terminal
The attach button was failing with "Invalid session" error when: - No active session exists (chatSessionId is null) - The stored session ID no longer exists in Claude service Changes: - Auto-create new session when no active session exists - Detect "Session not found" error and create new session - Recursively retry attach after creating new session - Update global session IDs and UI elements - Better error messages and user feedback The attach button now works seamlessly without requiring users to manually create a session first. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user