fix: terminal command execution via HTTP POST workaround
The WebSocket send mechanism fails with close code 1006 when client tries to send data to server. Server never receives the message, indicating a network/proxy layer issue that couldn't be fixed through code changes or nginx configuration. Solution: Bypass WebSocket send entirely by using HTTP POST to send commands directly to the PTY. Changes: - Added sendTerminalInput() method to terminal-service.js that writes directly to PTY, bypassing WebSocket - Added POST endpoint /claude/api/terminals/:id/input to server.js - Modified launchCommand() in terminal.js to use fetch() with HTTP POST instead of WebSocket.send() The WebSocket receive direction still works (server→client for output display), only send direction (client→server for commands) is bypassed. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,6 +12,7 @@ class TerminalService {
|
||||
this.terminals = new Map(); // terminalId -> { pty, ws, sessionId, workingDir, mode, createdAt }
|
||||
this.wsServer = null;
|
||||
this.logFile = path.join(process.env.HOME, 'obsidian-vault', '.claude-ide', 'terminal-logs.jsonl');
|
||||
this.pingInterval = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -29,6 +30,7 @@ class TerminalService {
|
||||
|
||||
if (terminalMatch) {
|
||||
const terminalId = terminalMatch[1];
|
||||
console.log(`[TerminalService] Handling WebSocket upgrade for terminal ${terminalId}`);
|
||||
this.wsServer.handleUpgrade(request, socket, head, (ws) => {
|
||||
this.wsServer.emit('connection', ws, request, terminalId);
|
||||
});
|
||||
@@ -37,12 +39,34 @@ class TerminalService {
|
||||
|
||||
// Handle WebSocket connections
|
||||
this.wsServer.on('connection', (ws, request, terminalId) => {
|
||||
console.log(`[TerminalService] WebSocket connection event received for terminal ${terminalId}`);
|
||||
this.handleConnection(terminalId, ws);
|
||||
});
|
||||
|
||||
// Setup ping interval to keep connections alive
|
||||
this.setupPingInterval();
|
||||
|
||||
console.log('[TerminalService] WebSocket server initialized');
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup ping interval to keep WebSocket connections alive
|
||||
*/
|
||||
setupPingInterval() {
|
||||
// Send ping to all clients every 30 seconds
|
||||
this.pingInterval = setInterval(() => {
|
||||
if (this.wsServer) {
|
||||
this.wsServer.clients.forEach((ws) => {
|
||||
if (ws.readyState === ws.OPEN) {
|
||||
ws.ping();
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
console.log('[TerminalService] Ping interval configured (30s)');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new terminal PTY
|
||||
*/
|
||||
@@ -145,6 +169,18 @@ class TerminalService {
|
||||
}
|
||||
});
|
||||
|
||||
// Handle WebSocket ping (respond with pong)
|
||||
ws.on('ping', () => {
|
||||
console.log(`[TerminalService] Ping received from ${terminalId}`);
|
||||
ws.pong();
|
||||
});
|
||||
|
||||
// Handle WebSocket pong (response to our ping)
|
||||
ws.on('pong', () => {
|
||||
console.log(`[TerminalService] Pong received from ${terminalId}`);
|
||||
terminal.lastActivity = new Date().toISOString();
|
||||
});
|
||||
|
||||
// Handle PTY output - send to client
|
||||
terminal.pty.onData((data) => {
|
||||
console.log(`[TerminalService] PTY data from ${terminalId}: ${data.replace(/\n/g, '\\n').replace(/\r/g, '\\r')}`);
|
||||
@@ -196,8 +232,21 @@ class TerminalService {
|
||||
mode: terminal.mode
|
||||
});
|
||||
console.log(`[TerminalService] Sending ready message to ${terminalId}: ${readyMessage}`);
|
||||
ws.send(readyMessage);
|
||||
console.log(`[TerminalService] Ready message sent successfully`);
|
||||
|
||||
try {
|
||||
ws.send(readyMessage);
|
||||
console.log(`[TerminalService] Ready message sent successfully`);
|
||||
|
||||
// Send a ping immediately after ready to ensure connection stays alive
|
||||
setTimeout(() => {
|
||||
if (ws.readyState === ws.OPEN) {
|
||||
ws.ping();
|
||||
console.log(`[TerminalService] Sent ping after ready message`);
|
||||
}
|
||||
}, 100);
|
||||
} catch (error) {
|
||||
console.error(`[TerminalService] Error sending ready message:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -267,6 +316,34 @@ class TerminalService {
|
||||
return { success: true, mode };
|
||||
}
|
||||
|
||||
/**
|
||||
* Send input to terminal via HTTP (WebSocket workaround)
|
||||
*/
|
||||
sendTerminalInput(terminalId, data) {
|
||||
const terminal = this.terminals.get(terminalId);
|
||||
|
||||
if (!terminal) {
|
||||
return { success: false, error: 'Terminal not found' };
|
||||
}
|
||||
|
||||
if (!terminal.pty) {
|
||||
return { success: false, error: 'PTY not found' };
|
||||
}
|
||||
|
||||
try {
|
||||
// Write directly to PTY
|
||||
terminal.pty.write(data);
|
||||
terminal.lastActivity = new Date().toISOString();
|
||||
|
||||
console.log(`[TerminalService] Wrote to PTY ${terminalId}: ${data.replace(/\n/g, '\\n')}`);
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error(`[TerminalService] Error writing to PTY ${terminalId}:`, error);
|
||||
return { success: false, error: error.message };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close terminal and kill PTY process
|
||||
*/
|
||||
@@ -373,6 +450,12 @@ class TerminalService {
|
||||
async cleanup() {
|
||||
console.log('[TerminalService] Cleaning up all terminals...');
|
||||
|
||||
// Clear ping interval
|
||||
if (this.pingInterval) {
|
||||
clearInterval(this.pingInterval);
|
||||
this.pingInterval = null;
|
||||
}
|
||||
|
||||
for (const [id, terminal] of this.terminals.entries()) {
|
||||
try {
|
||||
if (terminal.pty) {
|
||||
|
||||
Reference in New Issue
Block a user