diff --git a/database.sqlite-shm b/database.sqlite-shm index e6cdcd3f..6b32c0ee 100644 Binary files a/database.sqlite-shm and b/database.sqlite-shm differ diff --git a/database.sqlite-wal b/database.sqlite-wal index bc92952e..23c9ec53 100644 Binary files a/database.sqlite-wal and b/database.sqlite-wal differ diff --git a/public/claude-ide/terminal.js b/public/claude-ide/terminal.js index 192ac7ac..47404962 100644 --- a/public/claude-ide/terminal.js +++ b/public/claude-ide/terminal.js @@ -699,28 +699,55 @@ class TerminalManager { return; } - this.debugLog('CMD', `Terminal ${terminalId} is ready! Sending command.`); + this.debugLog('CMD', `Terminal ${terminalId} is ready!`); + + // Small delay to ensure WebSocket is stable after switching terminals + this.debugLog('CMD', `Waiting 100ms for WebSocket to stabilize...`); + await new Promise(resolve => setTimeout(resolve, 100)); const terminal = this.terminals.get(terminalId); - if (!terminal || !terminal.ws || terminal.ws.readyState !== WebSocket.OPEN) { - this.debugLog('ERROR', `Cannot send - WebSocket not ready`, { - hasTerminal: !!terminal, - hasWs: !!terminal?.ws, - wsState: terminal?.ws?.readyState, - stateName: terminal?.ws?.readyState === WebSocket.OPEN ? 'OPEN' : 'NOT OPEN' + if (!terminal) { + this.debugLog('ERROR', `Terminal ${terminalId} not found in map`); + return; + } + + if (!terminal.ws) { + this.debugLog('ERROR', `WebSocket not set for terminal ${terminalId}`); + return; + } + + // Check WebSocket state + const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED']; + this.debugLog('CMD', `WebSocket state: ${readyStates[terminal.ws.readyState]} (${terminal.ws.readyState})`); + + if (terminal.ws.readyState !== WebSocket.OPEN) { + this.debugLog('ERROR', `Cannot send - WebSocket not open`, { + wsState: terminal.ws.readyState, + stateName: readyStates[terminal.ws.readyState] }); return; } + // Check if WebSocket has any buffered amount (indicating pending sends) + if (terminal.ws.bufferedAmount > 0) { + this.debugLog('CMD', `WebSocket has ${terminal.ws.bufferedAmount} bytes buffered, waiting...`); + await new Promise(resolve => setTimeout(resolve, 200)); + } + // Send command to terminal const message = JSON.stringify({ type: 'input', data: command }); this.debugLog('CMD', `Sending to WebSocket: ${message}`); - terminal.ws.send(message); - this.debugLog('CMD', `Command sent to terminal ${terminalId}: ${command.trim()}`); + try { + terminal.ws.send(message); + this.debugLog('CMD', `Command sent to terminal ${terminalId}: ${command.trim()}`); + } catch (error) { + this.debugLog('ERROR', `Failed to send command`, { error: error.message, name: error.name }); + showToast(`Failed to send command: ${error.message}`, 'error'); + } } /**