Stabilize dashboard runtime proxy
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { dockerExecInOpenShell, OPENSHELL_NAMESPACE } from '../../../../lib/openshellHost'
|
||||
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const sandboxId = searchParams.get('sandboxId')
|
||||
|
||||
if (!sandboxId) {
|
||||
return NextResponse.json({ error: 'sandboxId is required' }, { status: 400 })
|
||||
}
|
||||
|
||||
const result = await dockerExecInOpenShell(
|
||||
`kubectl -n ${OPENSHELL_NAMESPACE} get pod ${sandboxId} -o json`
|
||||
)
|
||||
|
||||
const pod = JSON.parse(result.stdout)
|
||||
const containerNames = Array.isArray(pod?.spec?.containers) ? pod.spec.containers.map((c: any) => c.name) : []
|
||||
const containerImages = Array.isArray(pod?.spec?.containers) ? pod.spec.containers.map((c: any) => c.image) : []
|
||||
const statusNames = Array.isArray(pod?.status?.containerStatuses) ? pod.status.containerStatuses.map((c: any) => c.name) : []
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
sandboxId,
|
||||
containers: containerNames,
|
||||
images: containerImages,
|
||||
runningStatuses: statusNames,
|
||||
raw: result.stdout.trim(),
|
||||
stderr: result.stderr.trim(),
|
||||
note: 'Pod/container introspection succeeded. Use this to select the right exec target for terminal attach.'
|
||||
})
|
||||
} catch (error) {
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
error: error instanceof Error ? error.message : 'Failed to introspect sandbox pod'
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,131 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { inspectSandbox } from '@/app/lib/openshellHost'
|
||||
import { resolveRuntimeAuthority } from '@/app/lib/runtimeAuthority'
|
||||
|
||||
const TERMINAL_SERVER_URL = process.env.TERMINAL_SERVER_URL || 'http://127.0.0.1:3011'
|
||||
const TERMINAL_WS_PROXY_PATH = '/api/openshell/terminal/live/ws'
|
||||
const PUBLIC_BROWSER_HOST = process.env.PUBLIC_BROWSER_HOST || process.env.PUBLIC_WS_HOST || null
|
||||
const PUBLIC_BASE_URL = process.env.PUBLIC_BASE_URL || null
|
||||
|
||||
function isNonRoutableHost(candidate: string, protocol = 'http:') {
|
||||
const normalized = candidate.trim()
|
||||
if (!normalized) return true
|
||||
|
||||
try {
|
||||
const parsed = new URL(`${protocol}//${normalized}`)
|
||||
return (
|
||||
parsed.hostname === '0.0.0.0' ||
|
||||
parsed.hostname === '::' ||
|
||||
parsed.hostname === '127.0.0.1' ||
|
||||
parsed.hostname === 'localhost' ||
|
||||
parsed.hostname === '[::1]' ||
|
||||
parsed.hostname === '::1'
|
||||
)
|
||||
} catch {
|
||||
return (
|
||||
normalized.startsWith('0.0.0.0') ||
|
||||
normalized.startsWith('[::]') ||
|
||||
normalized.startsWith('127.0.0.1') ||
|
||||
normalized.startsWith('localhost') ||
|
||||
normalized.startsWith('[::1]') ||
|
||||
normalized.startsWith('::1')
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function resolveBrowserHost(requestUrl: URL, forwardedHost: string | null) {
|
||||
const forwardedCandidate = (forwardedHost && forwardedHost.trim()) || ''
|
||||
if (forwardedCandidate && !isNonRoutableHost(forwardedCandidate, requestUrl.protocol)) {
|
||||
return forwardedCandidate
|
||||
}
|
||||
|
||||
if (PUBLIC_BROWSER_HOST && !isNonRoutableHost(PUBLIC_BROWSER_HOST, requestUrl.protocol)) {
|
||||
return PUBLIC_BROWSER_HOST.trim()
|
||||
}
|
||||
|
||||
if (PUBLIC_BASE_URL) {
|
||||
try {
|
||||
const parsed = new URL(PUBLIC_BASE_URL)
|
||||
if (!isNonRoutableHost(parsed.host, parsed.protocol)) {
|
||||
return parsed.host
|
||||
}
|
||||
} catch {
|
||||
// ignore malformed PUBLIC_BASE_URL
|
||||
}
|
||||
}
|
||||
|
||||
const requestHostHeader = requestUrl.host
|
||||
if (!isNonRoutableHost(requestHostHeader, requestUrl.protocol)) {
|
||||
return requestHostHeader
|
||||
}
|
||||
|
||||
const hostHeader = requestUrl.hostname
|
||||
const requestPort = requestUrl.port
|
||||
if (hostHeader && !isNonRoutableHost(hostHeader, requestUrl.protocol)) {
|
||||
return requestPort ? `${hostHeader}:${requestPort}` : hostHeader
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function getBrowserWebSocketUrl(request: Request, params: {
|
||||
sessionId: string
|
||||
sandboxId: string
|
||||
dashboardSessionId: string
|
||||
}) {
|
||||
const requestUrl = new URL(request.url)
|
||||
const forwardedProto = request.headers.get('x-forwarded-proto')
|
||||
const forwardedHost = request.headers.get('x-forwarded-host')
|
||||
const host = resolveBrowserHost(requestUrl, forwardedHost)
|
||||
if (!host) {
|
||||
throw new Error('No routable browser host available for terminal websocket URL. Set PUBLIC_BROWSER_HOST or PUBLIC_BASE_URL.')
|
||||
}
|
||||
const baseProtocol = (forwardedProto && forwardedProto.trim()) || requestUrl.protocol.replace(/:$/, '')
|
||||
const protocol = baseProtocol === 'https' ? 'wss:' : 'ws:'
|
||||
const websocketUrl = new URL(TERMINAL_WS_PROXY_PATH, `${protocol}//${host}`)
|
||||
websocketUrl.searchParams.set('sessionId', params.sessionId)
|
||||
websocketUrl.searchParams.set('sandboxId', params.sandboxId)
|
||||
websocketUrl.searchParams.set('dashboardSessionId', params.dashboardSessionId)
|
||||
return websocketUrl.toString()
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const response = await fetch(`${TERMINAL_SERVER_URL}/session`, {
|
||||
const sandboxId = typeof body?.sandboxId === 'string' ? body.sandboxId.trim() : ''
|
||||
const sessionId = typeof body?.sessionId === 'string' ? body.sessionId.trim() : ''
|
||||
const dashboardSessionId = typeof body?.dashboardSessionId === 'string' && body.dashboardSessionId.trim()
|
||||
? body.dashboardSessionId.trim()
|
||||
: 'dashboard-host'
|
||||
|
||||
const requestedSandboxId = sandboxId || 'host'
|
||||
|
||||
let inspection = null
|
||||
if (sandboxId) {
|
||||
try {
|
||||
inspection = await inspectSandbox(sandboxId)
|
||||
} catch {
|
||||
inspection = null
|
||||
}
|
||||
}
|
||||
|
||||
const authority = resolveRuntimeAuthority({
|
||||
sandboxId,
|
||||
resolvedSandboxId: inspection?.name || null,
|
||||
instanceId: typeof body?.instanceId === 'string' ? body.instanceId.trim() : '',
|
||||
})
|
||||
const terminalServerUrl = authority.terminalServerUrl || TERMINAL_SERVER_URL
|
||||
const resolvedSandboxId = authority.resolvedSandboxId || requestedSandboxId
|
||||
|
||||
const response = await fetch(`${terminalServerUrl}/session`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
sandboxId: body?.sandboxId,
|
||||
sessionId: body?.sessionId,
|
||||
sandboxId: resolvedSandboxId,
|
||||
sessionId,
|
||||
dashboardSessionId,
|
||||
}),
|
||||
cache: 'no-store',
|
||||
})
|
||||
@@ -22,13 +135,31 @@ export async function POST(request: Request) {
|
||||
return NextResponse.json({ ok: false, error: result.error || 'Failed to initialize live terminal session.' }, { status: 502 })
|
||||
}
|
||||
|
||||
const wsBase = TERMINAL_SERVER_URL.replace(/^http/, 'ws')
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
sessionId: result.sessionId,
|
||||
sandboxId: result.sandboxId,
|
||||
dashboardSessionId: result.dashboardSessionId || dashboardSessionId,
|
||||
replay: result.replay,
|
||||
websocketUrl: `${wsBase}/ws?sessionId=${encodeURIComponent(result.sessionId)}&sandboxId=${encodeURIComponent(result.sandboxId)}`,
|
||||
transport: result.transport,
|
||||
instanceId: authority.openclaw.id,
|
||||
authority: {
|
||||
requestedSandboxId: authority.requestedSandboxId,
|
||||
resolvedSandboxId: authority.resolvedSandboxId,
|
||||
sandboxAuthority: authority.sandboxAuthority,
|
||||
requestedInstanceId: authority.requestedInstanceId,
|
||||
instanceId: authority.openclaw.id,
|
||||
sandboxInstanceId: authority.mappedSandboxInstanceId,
|
||||
explicitInstanceOverride: authority.explicitInstanceOverride,
|
||||
usedMappedSandboxInstance: authority.usedMappedSandboxInstance,
|
||||
},
|
||||
sshHostAlias: inspection?.sshHostAlias || null,
|
||||
websocketUrl: getBrowserWebSocketUrl(request, {
|
||||
sessionId: result.sessionId,
|
||||
sandboxId: result.sandboxId,
|
||||
dashboardSessionId: result.dashboardSessionId || dashboardSessionId,
|
||||
}),
|
||||
degraded: !inspection,
|
||||
})
|
||||
} catch (error) {
|
||||
return NextResponse.json({
|
||||
|
||||
@@ -16,14 +16,19 @@ export async function GET(request: Request) {
|
||||
ok: true,
|
||||
sandboxId,
|
||||
attached: true,
|
||||
output: result.stdout.trim(),
|
||||
stderr: result.stderr.trim(),
|
||||
note: 'Backend-local terminal probe succeeded. Next step is persistent interactive terminal transport.'
|
||||
name: result.name,
|
||||
resolvedId: result.id,
|
||||
namespace: result.namespace,
|
||||
phase: result.phase,
|
||||
sshHostAlias: result.sshHostAlias,
|
||||
sshConfig: result.sshConfig,
|
||||
output: result.rawDetails,
|
||||
note: 'Sandbox inspection succeeded. Next step is persistent interactive terminal transport.',
|
||||
})
|
||||
} catch (error) {
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
error: error instanceof Error ? error.message : 'Failed to probe terminal attach'
|
||||
error: error instanceof Error ? error.message : 'Failed to probe terminal attach',
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,131 +1,150 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { dockerExecInOpenShell, OPENSHELL_NAMESPACE, probeSandboxShell, validateSandboxId } from '../../../../lib/openshellHost'
|
||||
import { inspectSandbox, isOpenShellTransportError } from '@/app/lib/openshellHost'
|
||||
import { resolveRuntimeAuthority } from '@/app/lib/runtimeAuthority'
|
||||
|
||||
interface PodSummary {
|
||||
containers: string[]
|
||||
images: string[]
|
||||
runningStatuses: string[]
|
||||
phase?: string
|
||||
readyCondition?: string
|
||||
function normalizeSandboxId(value: string | null) {
|
||||
return (value || '').trim()
|
||||
}
|
||||
|
||||
const EMPTY_POD_SUMMARY: PodSummary = {
|
||||
containers: [],
|
||||
images: [],
|
||||
runningStatuses: [],
|
||||
}
|
||||
function buildAttach(alias: string, sandboxId: string) {
|
||||
const loginShell = process.platform === 'darwin' ? 'zsh' : 'bash'
|
||||
const extraPath = process.platform === 'darwin' ? '/opt/homebrew/bin' : `${process.env.HOME || '~'}/.local/bin`
|
||||
|
||||
async function inspectPod(sandboxId: string): Promise<PodSummary> {
|
||||
const result = await dockerExecInOpenShell(
|
||||
`kubectl -n ${OPENSHELL_NAMESPACE} get pod ${sandboxId} -o json`
|
||||
)
|
||||
|
||||
if (result.exitCode !== 0) {
|
||||
const failureText = `${result.stderr}\n${result.stdout}`.toLowerCase()
|
||||
if (failureText.includes('(notfound)') || failureText.includes('not found')) {
|
||||
return EMPTY_POD_SUMMARY
|
||||
}
|
||||
throw new Error(result.stderr.trim() || result.stdout.trim() || `kubectl get pod failed with exit code ${result.exitCode}`)
|
||||
}
|
||||
|
||||
if (!result.stdout.trim()) {
|
||||
throw new Error('kubectl get pod returned empty stdout')
|
||||
}
|
||||
|
||||
const pod = JSON.parse(result.stdout)
|
||||
return {
|
||||
containers: Array.isArray(pod?.spec?.containers) ? pod.spec.containers.map((c: any) => c.name) : [],
|
||||
images: Array.isArray(pod?.spec?.containers) ? pod.spec.containers.map((c: any) => c.image) : [],
|
||||
runningStatuses: Array.isArray(pod?.status?.containerStatuses) ? pod.status.containerStatuses.map((c: any) => c.name) : [],
|
||||
phase: pod?.status?.phase,
|
||||
readyCondition: Array.isArray(pod?.status?.conditions)
|
||||
? pod.status.conditions.find((c: any) => c.type === 'Ready')?.status
|
||||
: undefined,
|
||||
}
|
||||
}
|
||||
|
||||
function buildAttachContract(sandboxId: string) {
|
||||
const alias = `openshell-${sandboxId}`
|
||||
return {
|
||||
alias,
|
||||
aliasCommand: `ssh ${alias}`,
|
||||
fallbackCommand: `ssh -i ~/.ssh/id_ed25519_openclaw_labmac ${alias}`,
|
||||
loginShellCommand: `env PATH=/opt/homebrew/bin:$PATH zsh -l -c 'ssh ${alias}'`,
|
||||
shellHint: 'If a non-interactive shell misses Homebrew tooling, retry from a login shell or prepend PATH=/opt/homebrew/bin:$PATH before ssh.',
|
||||
fallbackCommand: `ssh ${alias}`,
|
||||
loginShellCommand: `env PATH=${extraPath}:$PATH ${loginShell} -l -c 'ssh ${alias}'`,
|
||||
shellHint: process.platform === 'darwin'
|
||||
? 'If your non-interactive shell misses Homebrew tooling, retry from a login shell or prepend PATH=/opt/homebrew/bin:$PATH before ssh.'
|
||||
: 'If your non-interactive shell misses local tooling, retry from a login shell or prepend PATH=$HOME/.local/bin:$PATH before ssh.',
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const url = new URL(request.url)
|
||||
let sandboxId = 'unknown'
|
||||
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const sandboxId = validateSandboxId(searchParams.get('sandboxId') || '')
|
||||
sandboxId = normalizeSandboxId(url.searchParams.get('sandboxId'))
|
||||
|
||||
const attach = buildAttachContract(sandboxId)
|
||||
const pod = await inspectPod(sandboxId)
|
||||
|
||||
let shellProbe: {
|
||||
ok: boolean
|
||||
output?: string
|
||||
stderr?: string
|
||||
error?: string
|
||||
} = { ok: false }
|
||||
|
||||
const podExists = Boolean(pod.containers.length || pod.images.length || pod.phase)
|
||||
|
||||
if (podExists) {
|
||||
try {
|
||||
const probe = await probeSandboxShell(sandboxId)
|
||||
shellProbe = {
|
||||
if (!sandboxId) {
|
||||
const authority = resolveRuntimeAuthority({ sandboxId: null })
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
sandboxId: 'host',
|
||||
authority: {
|
||||
requestedSandboxId: authority.requestedSandboxId,
|
||||
resolvedSandboxId: authority.resolvedSandboxId,
|
||||
sandboxAuthority: authority.sandboxAuthority,
|
||||
requestedInstanceId: authority.requestedInstanceId,
|
||||
instanceId: authority.openclaw.id,
|
||||
sandboxInstanceId: authority.mappedSandboxInstanceId,
|
||||
explicitInstanceOverride: authority.explicitInstanceOverride,
|
||||
usedMappedSandboxInstance: authority.usedMappedSandboxInstance,
|
||||
terminalFallback: true,
|
||||
},
|
||||
podExists: false,
|
||||
podReady: true,
|
||||
sshReachable: true,
|
||||
degraded: true,
|
||||
attach: null,
|
||||
pod: {
|
||||
phase: 'Host',
|
||||
readyCondition: 'Host',
|
||||
containers: [],
|
||||
images: [],
|
||||
runningStatuses: [],
|
||||
},
|
||||
shellProbe: {
|
||||
ok: true,
|
||||
output: probe.stdout.trim(),
|
||||
stderr: probe.stderr.trim(),
|
||||
}
|
||||
} catch (error) {
|
||||
shellProbe = {
|
||||
ok: false,
|
||||
error: error instanceof Error ? error.message : 'Shell probe failed',
|
||||
}
|
||||
}
|
||||
} else {
|
||||
shellProbe = {
|
||||
ok: false,
|
||||
error: `Sandbox pod ${sandboxId} was not found in ${OPENSHELL_NAMESPACE}.`,
|
||||
}
|
||||
output: 'Host terminal mode active. No sandbox selected.',
|
||||
stderr: '',
|
||||
},
|
||||
note: 'Host operator terminal is ready. Sandbox context is optional.',
|
||||
})
|
||||
}
|
||||
|
||||
const podReady = pod.readyCondition === 'True' || pod.phase === 'Running'
|
||||
const sshReachable = shellProbe.ok
|
||||
const sandbox = await inspectSandbox(sandboxId)
|
||||
const authority = resolveRuntimeAuthority({
|
||||
sandboxId,
|
||||
resolvedSandboxId: sandbox.name,
|
||||
})
|
||||
const podReady = sandbox.phase === 'Running'
|
||||
const sshReachable = sandbox.phase === 'Running'
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
sandboxId,
|
||||
podExists,
|
||||
sandboxId: sandbox.name,
|
||||
authority: {
|
||||
requestedSandboxId: authority.requestedSandboxId,
|
||||
resolvedSandboxId: authority.resolvedSandboxId,
|
||||
sandboxAuthority: authority.sandboxAuthority,
|
||||
requestedInstanceId: authority.requestedInstanceId,
|
||||
instanceId: authority.openclaw.id,
|
||||
sandboxInstanceId: authority.mappedSandboxInstanceId,
|
||||
explicitInstanceOverride: authority.explicitInstanceOverride,
|
||||
usedMappedSandboxInstance: authority.usedMappedSandboxInstance,
|
||||
terminalFallback: false,
|
||||
},
|
||||
podExists: true,
|
||||
podReady,
|
||||
sshReachable,
|
||||
degraded: podExists && !sshReachable,
|
||||
attach,
|
||||
pod,
|
||||
shellProbe,
|
||||
note: !podExists
|
||||
? `Sandbox pod ${sandboxId} is not present in ${OPENSHELL_NAMESPACE}.`
|
||||
: sshReachable
|
||||
? 'Pod introspection and bounded shell probe both succeeded.'
|
||||
: 'Pod introspection succeeded, but bounded shell probe did not. Treat this as degraded dashboard reachability until direct SSH is confirmed.',
|
||||
degraded: !sshReachable,
|
||||
attach: buildAttach(sandbox.sshHostAlias, sandbox.name),
|
||||
pod: {
|
||||
phase: sandbox.phase,
|
||||
readyCondition: podReady ? 'True' : sandbox.rawPhase || 'Unknown',
|
||||
containers: [],
|
||||
images: [],
|
||||
runningStatuses: [],
|
||||
},
|
||||
shellProbe: {
|
||||
ok: sshReachable,
|
||||
output: sandbox.rawDetails,
|
||||
stderr: '',
|
||||
},
|
||||
note: sshReachable
|
||||
? 'Sandbox inspection succeeded and the normalized phase is Running.'
|
||||
: `Sandbox inspection succeeded, but authoritative phase is ${sandbox.rawPhase || 'Unknown'} (normalized ${sandbox.phase || 'Unknown'}).`,
|
||||
})
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to build readiness contract'
|
||||
const status = message === 'sandboxId is required'
|
||||
|| message.includes('sandboxId must be a valid lowercase DNS-style identifier')
|
||||
? 400
|
||||
: 500
|
||||
|
||||
const message = error instanceof Error ? error.message : 'Failed to inspect terminal readiness.'
|
||||
const transportError = isOpenShellTransportError(error)
|
||||
const authority = resolveRuntimeAuthority({ sandboxId })
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
ok: true,
|
||||
sandboxId: sandboxId || 'host',
|
||||
authority: {
|
||||
requestedSandboxId: authority.requestedSandboxId,
|
||||
resolvedSandboxId: authority.resolvedSandboxId,
|
||||
sandboxAuthority: authority.sandboxAuthority,
|
||||
requestedInstanceId: authority.requestedInstanceId,
|
||||
instanceId: authority.openclaw.id,
|
||||
sandboxInstanceId: authority.mappedSandboxInstanceId,
|
||||
explicitInstanceOverride: authority.explicitInstanceOverride,
|
||||
usedMappedSandboxInstance: authority.usedMappedSandboxInstance,
|
||||
terminalFallback: true,
|
||||
},
|
||||
podExists: false,
|
||||
podReady: false,
|
||||
sshReachable: false,
|
||||
degraded: false,
|
||||
podReady: true,
|
||||
sshReachable: true,
|
||||
degraded: true,
|
||||
error: message,
|
||||
}, { status })
|
||||
attach: null,
|
||||
pod: {
|
||||
phase: 'Host',
|
||||
readyCondition: 'Host',
|
||||
containers: [],
|
||||
images: [],
|
||||
runningStatuses: [],
|
||||
},
|
||||
shellProbe: {
|
||||
ok: true,
|
||||
output: 'Host terminal mode active. Sandbox inspection failed, but host shell remains available.',
|
||||
stderr: message,
|
||||
},
|
||||
note: transportError ? 'OpenShell transport is unavailable for sandbox inspection; host operator terminal remains available.' : 'Sandbox inspection failed; host operator terminal remains available.',
|
||||
}, { status: 200 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,206 +0,0 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import { DEFAULT_EXEC_TIMEOUT_MS, MAX_COMMAND_LENGTH, runSandboxCommand, validateSandboxId, validateTerminalCommand } from '../../../lib/openshellHost'
|
||||
|
||||
type TranscriptEntry = {
|
||||
id: string
|
||||
command: string
|
||||
stdout: string
|
||||
stderr: string
|
||||
exitCode: number
|
||||
executionMode?: string
|
||||
ranAt: string
|
||||
}
|
||||
|
||||
type TerminalSession = {
|
||||
sessionId: string
|
||||
sandboxId: string
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
cwd?: string
|
||||
transcript: TranscriptEntry[]
|
||||
}
|
||||
|
||||
const sessions = new Map<string, TerminalSession>()
|
||||
const MAX_TRANSCRIPT = 100
|
||||
|
||||
function ensureSession(sessionId: string, sandboxId: string) {
|
||||
const existing = sessions.get(sessionId)
|
||||
if (existing) {
|
||||
if (existing.sandboxId !== sandboxId) {
|
||||
throw new Error('sessionId does not match sandboxId')
|
||||
}
|
||||
return existing
|
||||
}
|
||||
|
||||
const now = new Date().toISOString()
|
||||
const created: TerminalSession = {
|
||||
sessionId,
|
||||
sandboxId,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
cwd: undefined,
|
||||
transcript: [],
|
||||
}
|
||||
sessions.set(sessionId, created)
|
||||
return created
|
||||
}
|
||||
|
||||
function sanitizePromptPath(cwd?: string) {
|
||||
if (!cwd) return '~'
|
||||
return cwd
|
||||
}
|
||||
|
||||
function buildWrappedCommand(command: string, cwd?: string) {
|
||||
const trimmed = validateTerminalCommand(command)
|
||||
const cdPrefix = cwd ? `cd ${quoteForSingleQuotedShell(cwd)} >/dev/null 2>&1 || exit 98; ` : ''
|
||||
const shellBody = `${cdPrefix}${trimmed}; __cmd_exit=$?; printf '\n__OPENCLAW_PWD__=%s\n' "$PWD"; exit $__cmd_exit`
|
||||
return shellBody
|
||||
}
|
||||
|
||||
function quoteForSingleQuotedShell(value: string) {
|
||||
return `'${value.replace(/'/g, `'"'"'`)}'`
|
||||
}
|
||||
|
||||
function extractPwd(stdout: string) {
|
||||
const match = stdout.match(/\n__OPENCLAW_PWD__=(.*)\n?$/)
|
||||
if (!match) {
|
||||
return { cleanedStdout: stdout.trim(), cwd: undefined }
|
||||
}
|
||||
|
||||
const cwd = match[1]?.trim() || undefined
|
||||
const cleanedStdout = stdout.slice(0, match.index).trimEnd()
|
||||
return { cleanedStdout, cwd }
|
||||
}
|
||||
|
||||
function serializeSession(session: TerminalSession) {
|
||||
return {
|
||||
ok: true,
|
||||
interactive: false,
|
||||
terminalLike: true,
|
||||
mode: 'session-backed-command-terminal',
|
||||
session: {
|
||||
sessionId: session.sessionId,
|
||||
sandboxId: session.sandboxId,
|
||||
cwd: session.cwd,
|
||||
createdAt: session.createdAt,
|
||||
updatedAt: session.updatedAt,
|
||||
prompt: `${session.sandboxId}:${sanitizePromptPath(session.cwd)}$`,
|
||||
transcript: session.transcript,
|
||||
},
|
||||
limits: {
|
||||
timeoutMs: DEFAULT_EXEC_TIMEOUT_MS,
|
||||
maxCommandLength: MAX_COMMAND_LENGTH,
|
||||
},
|
||||
note: 'Dashboard terminal is session-backed and command-scoped. It preserves transcript and working directory across commands, but it is not a raw PTY stream yet.',
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const sandboxId = searchParams.get('sandboxId') || ''
|
||||
const sessionId = searchParams.get('sessionId') || ''
|
||||
|
||||
if (!sandboxId) {
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
interactive: false,
|
||||
mode: 'session-backed-command-terminal',
|
||||
error: 'sandboxId is required',
|
||||
}, { status: 400 })
|
||||
}
|
||||
|
||||
try {
|
||||
const validatedSandboxId = validateSandboxId(sandboxId)
|
||||
|
||||
if (!sessionId) {
|
||||
const createdSession = ensureSession(randomUUID(), validatedSandboxId)
|
||||
return NextResponse.json(serializeSession(createdSession))
|
||||
}
|
||||
|
||||
const session = sessions.get(sessionId)
|
||||
if (!session || session.sandboxId !== validatedSandboxId) {
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
interactive: false,
|
||||
mode: 'session-backed-command-terminal',
|
||||
error: 'session not found',
|
||||
}, { status: 404 })
|
||||
}
|
||||
|
||||
return NextResponse.json(serializeSession(session))
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to inspect terminal session'
|
||||
const status = message.includes('sandboxId') ? 400 : 500
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
interactive: false,
|
||||
mode: 'session-backed-command-terminal',
|
||||
error: message,
|
||||
}, { status })
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const sandboxInput = typeof body?.sandboxId === 'string' ? body.sandboxId : ''
|
||||
const sessionInput = typeof body?.sessionId === 'string' ? body.sessionId : ''
|
||||
const commandInput = typeof body?.command === 'string' ? body.command : ''
|
||||
|
||||
const sandboxId = validateSandboxId(sandboxInput)
|
||||
const command = validateTerminalCommand(commandInput)
|
||||
const sessionId = sessionInput || randomUUID()
|
||||
const session = ensureSession(sessionId, sandboxId)
|
||||
|
||||
const wrappedCommand = buildWrappedCommand(command, session.cwd)
|
||||
const result = await runSandboxCommand(sandboxId, wrappedCommand)
|
||||
const { cleanedStdout, cwd } = extractPwd(result.stdout)
|
||||
|
||||
session.cwd = cwd || session.cwd
|
||||
session.updatedAt = new Date().toISOString()
|
||||
session.transcript.push({
|
||||
id: randomUUID(),
|
||||
command,
|
||||
stdout: cleanedStdout,
|
||||
stderr: result.stderr.trim(),
|
||||
exitCode: result.exitCode,
|
||||
executionMode: result.mode,
|
||||
ranAt: session.updatedAt,
|
||||
})
|
||||
if (session.transcript.length > MAX_TRANSCRIPT) {
|
||||
session.transcript.splice(0, session.transcript.length - MAX_TRANSCRIPT)
|
||||
}
|
||||
|
||||
const ok = result.exitCode === 0
|
||||
|
||||
return NextResponse.json({
|
||||
...serializeSession(session),
|
||||
ok,
|
||||
command,
|
||||
exitCode: result.exitCode,
|
||||
executionMode: result.mode,
|
||||
stdout: cleanedStdout,
|
||||
stderr: result.stderr.trim(),
|
||||
note: ok
|
||||
? 'Command completed and terminal session state was updated.'
|
||||
: `Command exited non-zero (${result.exitCode}); transcript preserved for inspection.`,
|
||||
}, { status: ok ? 200 : 502 })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to run sandbox command'
|
||||
const status = message === 'sandboxId is required'
|
||||
|| message === 'command is required'
|
||||
|| message.includes('sandboxId must be a valid lowercase DNS-style identifier')
|
||||
|| message.includes('command exceeds')
|
||||
|| message.includes('sessionId does not match sandboxId')
|
||||
? 400
|
||||
: 500
|
||||
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
interactive: false,
|
||||
mode: 'session-backed-command-terminal',
|
||||
error: message,
|
||||
}, { status })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user