fix: wait for WebSocket connection before sending command
Make connectTerminal() return a Promise that resolves when the WebSocket connection is established, ensuring commands are sent only after the terminal is ready. Root cause: - connectTerminal() created WebSocket but returned immediately - 500ms delay wasn't reliable - WebSocket might not be open yet - Command was sent before connection was ready, causing it to fail silently Fix: - Wrap WebSocket creation in Promise - Resolve Promise in ws.onopen callback - Reject on ws.onerror - Remove unnecessary 500ms delay Now when "Claude Code CLI" terminal type is selected: 1. WebSocket connection is established 2. We wait for connection to be ready 3. Command is sent immediately 4. claude --dangerously-skip-permissions launches reliably Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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,11 +767,13 @@ class TerminalManager {
|
||||
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const wsUrl = `${protocol}//${window.location.host}/claude/api/terminals/${terminalId}/ws`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
const ws = new WebSocket(wsUrl);
|
||||
|
||||
ws.onopen = () => {
|
||||
console.log(`[TerminalManager] Connected to terminal ${terminalId}`);
|
||||
resolve();
|
||||
};
|
||||
|
||||
ws.onmessage = (event) => {
|
||||
@@ -782,6 +783,7 @@ class TerminalManager {
|
||||
|
||||
ws.onerror = (error) => {
|
||||
console.error(`[TerminalManager] WebSocket error for terminal ${terminalId}:`, error);
|
||||
reject(error);
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
@@ -795,8 +797,9 @@ class TerminalManager {
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[TerminalManager] Error connecting WebSocket:', error);
|
||||
throw error;
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user