feat: add HostConsole component for interactive terminal access and integrate into EditorLayout

This commit is contained in:
SaelixCode
2026-02-26 19:52:43 -05:00
parent 4932d90e4a
commit fcf0c9f983
8 changed files with 309 additions and 15 deletions
+26
View File
@@ -13,6 +13,8 @@ import si from 'systeminformation';
import http from 'http';
import { spawn, exec } from 'child_process';
import { promisify } from 'util';
import path from 'path';
import { HostTerminalService } from './services/HostTerminalService';
const execAsync = promisify(exec);
@@ -220,6 +222,7 @@ server.on('upgrade', async (req, socket, head) => {
// Check if this is a stack logs WebSocket request
const url = req.url || '';
const logsMatch = url.match(/^\/api\/stacks\/([^/]+)\/logs$/);
const hostConsoleMatch = url.match(/^\/api\/system\/host-console/);
if (logsMatch) {
// Dedicated stack logs WebSocket - uses Supervisor loop for persistent logs
@@ -235,6 +238,29 @@ server.on('upgrade', async (req, socket, head) => {
}
}
});
} else if (hostConsoleMatch) {
const hostConsoleWss = new WebSocket.Server({ noServer: true });
hostConsoleWss.handleUpgrade(req, socket, head, (ws) => {
let targetDirectory = fileSystemService.getBaseDir();
try {
const reqUrl = new URL(req.url || '', `http://${req.headers.host || 'localhost'}`);
const stackParam = reqUrl.searchParams.get('stack');
if (stackParam) {
targetDirectory = path.join(targetDirectory, stackParam);
}
} catch (e) {
// ignore parsing error, fallback to base dir
}
try {
HostTerminalService.spawnTerminal(ws, targetDirectory);
} catch (error) {
console.error('Failed to spawn host terminal:', error);
if (ws.readyState === WebSocket.OPEN) {
ws.send(`Error spawning terminal: ${(error as Error).message}\r\n`);
ws.close();
}
}
});
} else {
// Generic terminal WebSocket
wss.handleUpgrade(req, socket, head, (ws) => {
@@ -0,0 +1,59 @@
import * as os from 'os';
import * as pty from 'node-pty';
import { WebSocket } from 'ws';
import { execSync } from 'child_process';
function getUnixShell() {
try {
execSync('which bash', { stdio: 'ignore' });
return 'bash';
} catch {
return 'sh';
}
}
export class HostTerminalService {
static spawnTerminal(ws: WebSocket, targetDirectory: string) {
const shell = os.platform() === 'win32' ? 'powershell.exe' : getUnixShell();
const ptyProcess = pty.spawn(shell, [], {
name: 'xterm-color',
cols: 80,
rows: 30,
cwd: targetDirectory,
env: process.env as Record<string, string>,
});
ptyProcess.onData((data) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(data); // Raw-Down protocol
}
});
ws.on('message', (message: string) => {
try {
const parsed = JSON.parse(message); // JSON-Up protocol
if (parsed.type === 'input') {
ptyProcess.write(parsed.payload);
} else if (parsed.type === 'resize') {
ptyProcess.resize(parsed.cols, parsed.rows);
}
} catch (e) {
console.error('Failed to parse Host terminal message:', e);
}
});
ws.on('close', () => {
console.log('Host terminal WebSocket closed, cleaning up PTY process');
ptyProcess.kill();
});
// Handle PTY process exit
ptyProcess.onExit(({ exitCode, signal }) => {
console.log(`Host terminal PTY process exited with code ${exitCode} and signal ${signal}`);
if (ws.readyState === WebSocket.OPEN) {
ws.close();
}
});
}
}