Attach live terminal PTY to sandbox SSH target

This commit is contained in:
Mark McKeen
2026-04-08 15:14:39 -06:00
parent d8232f1ad9
commit 35675f6dc5
+24 -2
View File
@@ -20,18 +20,37 @@ function shellForPlatform() {
return process.env.SHELL || '/bin/bash' return process.env.SHELL || '/bin/bash'
} }
function buildAttachCommand(sandboxId) {
const alias = `openshell-${sandboxId}`
return process.env.OPENSHELL_TERMINAL_ATTACH_TEMPLATE || `ssh ${alias}`
}
function buildBootstrapShell(sandboxId) {
const attachCommand = buildAttachCommand(sandboxId)
return [
`export OPENSHELL_SANDBOX_ID=${shellEscape(sandboxId)}`,
`export PS1='[${sandboxId}] \\u@\\h:\\w\\$ '`,
`echo '[openclaw] attaching to ${sandboxId} using: ${attachCommand}'`,
`exec ${attachCommand}`,
].join('; ')
}
function shellEscape(value) {
return `'${String(value).replace(/'/g, `'"'"'`)}'`
}
function createSession(sandboxId) { function createSession(sandboxId) {
trimSessions() trimSessions()
const id = randomUUID() const id = randomUUID()
const shell = shellForPlatform() const shell = shellForPlatform()
const proc = pty.spawn(shell, ['-l'], { const bootstrap = buildBootstrapShell(sandboxId)
const proc = pty.spawn(shell, ['-lc', bootstrap], {
name: 'xterm-256color', name: 'xterm-256color',
cols: 120, cols: 120,
rows: 34, rows: 34,
cwd: process.env.HOME, cwd: process.env.HOME,
env: { env: {
...process.env, ...process.env,
PS1: `[${sandboxId}] \\u@\\h:\\w\\$ `,
OPENSHELL_SANDBOX_ID: sandboxId, OPENSHELL_SANDBOX_ID: sandboxId,
}, },
}) })
@@ -39,6 +58,7 @@ function createSession(sandboxId) {
const session = { const session = {
id, id,
sandboxId, sandboxId,
attachCommand: buildAttachCommand(sandboxId),
pty: proc, pty: proc,
createdAt: Date.now(), createdAt: Date.now(),
updatedAt: Date.now(), updatedAt: Date.now(),
@@ -112,6 +132,7 @@ const server = http.createServer((req, res) => {
ok: true, ok: true,
sessionId: session.id, sessionId: session.id,
sandboxId: session.sandboxId, sandboxId: session.sandboxId,
attachCommand: session.attachCommand,
replay: session.buffer, replay: session.buffer,
})) }))
} catch (error) { } catch (error) {
@@ -140,6 +161,7 @@ wss.on('connection', (socket, req) => {
type: 'ready', type: 'ready',
sessionId: session.id, sessionId: session.id,
sandboxId: session.sandboxId, sandboxId: session.sandboxId,
attachCommand: session.attachCommand,
replay: session.buffer, replay: session.buffer,
})) }))