diff --git a/public/claude-ide/terminal.js b/public/claude-ide/terminal.js index 5fe45750..66041a0b 100644 --- a/public/claude-ide/terminal.js +++ b/public/claude-ide/terminal.js @@ -221,10 +221,9 @@ class TerminalManager { // Handle terminal type specific initialization if (selectedTerminalType === 'claude-cli') { - // Wait a moment for terminal to be ready - await new Promise(resolve => setTimeout(resolve, 500)); // Launch Claude CLI with skip permissions flag // Note: Keep mode as 'mixed' since we're not attaching to a session + // WebSocket is already connected (await connectTerminal), so no delay needed await this.launchCommand(terminalId, 'claude --dangerously-skip-permissions\n'); if (!silent) { @@ -768,35 +767,39 @@ class TerminalManager { const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; const wsUrl = `${protocol}//${window.location.host}/claude/api/terminals/${terminalId}/ws`; - try { - const ws = new WebSocket(wsUrl); + return new Promise((resolve, reject) => { + try { + const ws = new WebSocket(wsUrl); - ws.onopen = () => { - console.log(`[TerminalManager] Connected to terminal ${terminalId}`); - }; + ws.onopen = () => { + console.log(`[TerminalManager] Connected to terminal ${terminalId}`); + resolve(); + }; - ws.onmessage = (event) => { - const message = JSON.parse(event.data); - this.handleTerminalMessage(terminalId, message); - }; + ws.onmessage = (event) => { + const message = JSON.parse(event.data); + this.handleTerminalMessage(terminalId, message); + }; - ws.onerror = (error) => { - console.error(`[TerminalManager] WebSocket error for terminal ${terminalId}:`, error); - }; + ws.onerror = (error) => { + console.error(`[TerminalManager] WebSocket error for terminal ${terminalId}:`, error); + reject(error); + }; - ws.onclose = () => { - console.log(`[TerminalManager] WebSocket closed for terminal ${terminalId}`); - }; + ws.onclose = () => { + console.log(`[TerminalManager] WebSocket closed for terminal ${terminalId}`); + }; - // Store WebSocket - const terminal = this.terminals.get(terminalId); - if (terminal) { - terminal.ws = ws; + // Store WebSocket + const terminal = this.terminals.get(terminalId); + if (terminal) { + terminal.ws = ws; + } + } catch (error) { + console.error('[TerminalManager] Error connecting WebSocket:', error); + reject(error); } - } catch (error) { - console.error('[TerminalManager] Error connecting WebSocket:', error); - throw error; - } + }); } /**