diff --git a/public/claude-ide/terminal.js b/public/claude-ide/terminal.js index 66041a0b..123470e0 100644 --- a/public/claude-ide/terminal.js +++ b/public/claude-ide/terminal.js @@ -213,7 +213,11 @@ class TerminalManager { // Create terminal UI await this.createTerminalUI(terminalId, selectedDir, mode); - // Connect WebSocket + // Initialize xterm.js FIRST (before connecting WebSocket) + // This ensures this.terminals map has the entry ready + await this.initializeXTerm(terminalId); + + // NOW connect WebSocket (terminal entry exists in map) await this.connectTerminal(terminalId); // Switch to new terminal @@ -773,6 +777,19 @@ class TerminalManager { ws.onopen = () => { console.log(`[TerminalManager] Connected to terminal ${terminalId}`); + + // Store WebSocket in terminal entry + // NOTE: This assumes initializeXTerm() has already been called + // and this.terminals has the entry + const terminal = this.terminals.get(terminalId); + if (terminal) { + terminal.ws = ws; + } else { + console.error(`[TerminalManager] CRITICAL: Terminal ${terminalId} not found in map! WebSocket connection will be lost.`); + reject(new Error(`Terminal ${terminalId} not initialized`)); + return; + } + resolve(); }; @@ -789,12 +806,6 @@ class TerminalManager { ws.onclose = () => { console.log(`[TerminalManager] WebSocket closed for terminal ${terminalId}`); }; - - // Store WebSocket - const terminal = this.terminals.get(terminalId); - if (terminal) { - terminal.ws = ws; - } } catch (error) { console.error('[TerminalManager] Error connecting WebSocket:', error); reject(error); @@ -908,11 +919,12 @@ class TerminalManager { this.sendTerminalResize(terminalId, cols, rows); }); - // Store terminal instance + // Store terminal instance in map + // This MUST happen before connectTerminal() is called this.terminals.set(terminalId, { terminal, fitAddon, - ws: null, + ws: null, // Will be set by connectTerminal() container, mode: 'mixed' });