Stabilize dashboard runtime proxy

This commit is contained in:
Codex
2026-04-24 13:47:10 +00:00
parent 4d15ce4388
commit 05db76d930
39 changed files with 4349 additions and 1472 deletions
+101 -7
View File
@@ -1,14 +1,108 @@
import { NextResponse } from 'next/server'
import { getOpenClawDashboardUrl } from '../../../../lib/openshellHost'
import { getDefaultOpenClawDashboardInstanceId, getOpenClawDashboardUrl, probeOpenClawDashboard } from '../../../../lib/openshellHost'
import { getOpenClawInstanceIdForSandbox, resolveOpenClawInstance, resolveOpenClawInstanceForSandbox } from '../../../../lib/openclawInstances'
import { resolveRuntimeAuthority } from '../../../../lib/runtimeAuthority'
function parseInventoryCount(requestUrl: URL) {
const raw = requestUrl.searchParams.get('inventoryCount')
if (raw == null) return null
const count = Number.parseInt(raw, 10)
return Number.isFinite(count) && count >= 0 ? count : null
}
function buildLaunchUrl(proxiedUrl: string, bootstrapUrl: string | null) {
if (!bootstrapUrl) return proxiedUrl
const bootstrap = new URL(bootstrapUrl)
const browserHash = bootstrap.hash
bootstrap.hash = ''
const params = new URLSearchParams({
path: '/',
bootstrapUrl: bootstrap.toString(),
})
return `${proxiedUrl}?${params.toString()}${browserHash}`
}
export async function GET(request: Request) {
const requestUrl = new URL(request.url)
const sandboxId = requestUrl.searchParams.get('sandboxId')
const requestedInstanceId = requestUrl.searchParams.get('instanceId')
const inventoryCount = parseInventoryCount(requestUrl)
const mappedSandboxInstanceId = getOpenClawInstanceIdForSandbox(sandboxId)
const instance = requestedInstanceId
? resolveOpenClawInstance(requestedInstanceId)
: resolveOpenClawInstanceForSandbox(sandboxId)
const authority = resolveRuntimeAuthority({
sandboxId,
instanceId: requestedInstanceId,
})
const probe = await probeOpenClawDashboard(authority.openclaw.id)
const instanceQualifiedProxyBase = `/api/openshell/instances/${encodeURIComponent(instance.id)}/dashboard/proxy`
const zeroInventoryMappedDashboard = Boolean(
inventoryCount === 0 &&
sandboxId &&
authority.bridgeActive &&
probe.reachable
)
const truthState = zeroInventoryMappedDashboard
? 'degraded'
: probe.bootstrapAuthority === 'tokenized-cli'
? 'tokenized'
: authority.bridgeActive
? 'bridged'
: 'verified'
const proxiedUrl = authority.explicitInstanceOverride ? instanceQualifiedProxyBase : '/api/openshell/dashboard/proxy'
const launchUrl = buildLaunchUrl(proxiedUrl, probe.bootstrapUrl)
export async function GET() {
const dashboardUrl = getOpenClawDashboardUrl()
return NextResponse.json({
ok: true,
dashboardUrl,
proxiedUrl: '/api/openshell/dashboard/proxy',
sandboxId,
sandboxInstanceId: mappedSandboxInstanceId,
instanceId: instance.id,
inventoryCount,
truthState,
degraded: zeroInventoryMappedDashboard,
authority: {
requestedSandboxId: authority.requestedSandboxId,
resolvedSandboxId: authority.resolvedSandboxId,
sandboxAuthority: authority.sandboxAuthority,
authorityMode: authority.authorityMode,
provenance: authority.provenance,
bridgeActive: authority.bridgeActive,
bridgeReason: authority.bridgeReason,
requestedInstanceId: authority.requestedInstanceId,
instanceId: authority.openclaw.id,
sandboxInstanceId: mappedSandboxInstanceId,
explicitInstanceOverride: authority.explicitInstanceOverride,
usedMappedSandboxInstance: authority.usedMappedSandboxInstance,
},
defaultInstanceId: getDefaultOpenClawDashboardInstanceId(),
dashboardUrl: getOpenClawDashboardUrl(authority.openclaw.id),
bootstrapUrl: probe.bootstrapUrl,
bootstrapTokenPresent: probe.bootstrapTokenPresent,
bootstrapSource: probe.bootstrapSource,
bootstrapAuthority: probe.bootstrapAuthority,
proxiedUrl,
launchUrl,
instanceQualifiedProxiedUrl: instanceQualifiedProxyBase,
openInNewTab: true,
loopbackOnly: true,
note: 'OpenClaw Dashboard is loopback-only on the host, so the web UI uses a local proxy route to expose it.'
loopbackOnly: authority.openclaw.loopbackOnly,
reachable: probe.reachable,
upstreamStatus: probe.status,
upstreamStatusText: probe.statusText,
listenerPresent: probe.listenerPresent,
listenerSummary: probe.listenerSummary,
note: zeroInventoryMappedDashboard
? 'Degraded truth: live OpenShell inventory is zero, but a bridged OpenClaw dashboard upstream is still reachable. Treat this dashboard as a fallback operator surface, not proof that the sandbox currently exists.'
: probe.bootstrapAuthority === 'tokenized-cli'
? 'Tokenized bootstrap available: the bridge resolved a dashboard URL from `openclaw dashboard --no-open`, so the proxy can carry the real OpenClaw session contract instead of treating raw loopback fetch as sufficient truth.'
: authority.bridgeActive
? authority.bridgeReason || 'Bridge-active: this dashboard action is using an explicit upstream OpenClaw bridge instead of assuming same-lane loopback ownership.'
: authority.openclaw.loopbackOnly
? 'OpenClaw Dashboard is loopback-only on the host, so the web UI uses a local proxy route to expose it.'
: 'OpenClaw Dashboard is reachable directly for this instance.'
})
}
@@ -0,0 +1,9 @@
import { proxyErrorResponse, proxyOpenClawDashboard } from '../shared'
export async function GET(request: Request) {
try {
return await proxyOpenClawDashboard(request)
} catch (error) {
return proxyErrorResponse(error)
}
}
+3 -29
View File
@@ -1,35 +1,9 @@
import { NextResponse } from 'next/server'
const OPENCLAW_LOCAL_DASHBOARD = 'http://127.0.0.1:18789/'
import { proxyErrorResponse, proxyOpenClawDashboard } from './shared'
export async function GET(request: Request) {
try {
const url = new URL(request.url)
const targetPath = url.searchParams.get('path') || ''
const target = new URL(targetPath, OPENCLAW_LOCAL_DASHBOARD)
const upstream = await fetch(target.toString(), {
method: 'GET',
headers: {
accept: 'text/html,application/json;q=0.9,*/*;q=0.8'
},
cache: 'no-store'
})
const contentType = upstream.headers.get('content-type') || 'text/html; charset=utf-8'
const body = await upstream.text()
return new NextResponse(body, {
status: upstream.status,
headers: {
'content-type': contentType,
'cache-control': 'no-store'
}
})
return await proxyOpenClawDashboard(request)
} catch (error) {
return NextResponse.json({
ok: false,
error: error instanceof Error ? error.message : 'Failed to proxy OpenClaw dashboard'
}, { status: 500 })
return proxyErrorResponse(error)
}
}
+200
View File
@@ -0,0 +1,200 @@
import { NextResponse } from 'next/server'
import { getDefaultOpenClawDashboardInstanceId } from '@/app/lib/openshellHost'
import { resolveRuntimeAuthority } from '@/app/lib/runtimeAuthority'
const LEGACY_PROXY_PREFIX = '/api/openshell/dashboard/proxy'
const INSTANCES_PREFIX = '/api/openshell/instances/'
const DASHBOARD_PROXY_SUFFIX = '/dashboard/proxy'
const HOP_BY_HOP_HEADERS = new Set([
'accept-encoding',
'connection',
'keep-alive',
'proxy-authenticate',
'proxy-authorization',
'te',
'trailer',
'transfer-encoding',
'upgrade',
'content-length',
])
type ProxyTargetResolution = {
instanceId: string | null
proxyPrefix: string
targetBaseUrl: string
authorityMode: 'direct-host' | 'bridged-sandbox-map' | 'bridged-instance-override'
bridgeActive: boolean
}
function normalizeDashboardProxyInstanceId(instanceId: string | null) {
if (!instanceId) return null
return instanceId === getDefaultOpenClawDashboardInstanceId() ? null : instanceId
}
function resolveProxyTarget(requestUrl: URL): ProxyTargetResolution {
const pathname = requestUrl.pathname
if (pathname.startsWith(INSTANCES_PREFIX)) {
const suffixIndex = pathname.indexOf(DASHBOARD_PROXY_SUFFIX, INSTANCES_PREFIX.length)
if (suffixIndex !== -1) {
const rawInstanceId = pathname.slice(INSTANCES_PREFIX.length, suffixIndex)
const decodedInstanceId = decodeURIComponent(rawInstanceId)
const normalizedInstanceId = normalizeDashboardProxyInstanceId(decodedInstanceId)
const authority = resolveRuntimeAuthority({
sandboxId: requestUrl.searchParams.get('sandboxId'),
instanceId: normalizedInstanceId,
})
return {
instanceId: authority.openclaw.id,
proxyPrefix: `/api/openshell/instances/${encodeURIComponent(decodedInstanceId)}/dashboard/proxy`,
targetBaseUrl: authority.openclaw.dashboardUrl,
authorityMode: authority.authorityMode,
bridgeActive: authority.bridgeActive,
}
}
}
const authority = resolveRuntimeAuthority({
sandboxId: requestUrl.searchParams.get('sandboxId'),
})
return {
instanceId: authority.bridgeActive ? authority.openclaw.id : null,
proxyPrefix: LEGACY_PROXY_PREFIX,
targetBaseUrl: authority.openclaw.dashboardUrl,
authorityMode: authority.authorityMode,
bridgeActive: authority.bridgeActive,
}
}
function buildTargetUrl(requestUrl: URL) {
const { proxyPrefix, targetBaseUrl, authorityMode, bridgeActive } = resolveProxyTarget(requestUrl)
const pathParam = requestUrl.searchParams.get('path')
const bootstrapUrl = requestUrl.searchParams.get('bootstrapUrl')
const pathname = requestUrl.pathname.startsWith(proxyPrefix)
? requestUrl.pathname.slice(proxyPrefix.length)
: '/'
const upstreamPath = pathParam ?? pathname ?? '/'
const normalizedPath = upstreamPath.startsWith('/') ? upstreamPath : `/${upstreamPath}`
const target = bootstrapUrl
? new URL(bootstrapUrl)
: new URL(normalizedPath, targetBaseUrl)
if (!bootstrapUrl) {
requestUrl.searchParams.forEach((value, key) => {
if (key !== 'path') target.searchParams.append(key, value)
})
}
return { target, proxyPrefix, authorityMode, bridgeActive }
}
function copyRequestHeaders(request: Request, target: URL) {
const headers = new Headers()
request.headers.forEach((value, key) => {
if (!HOP_BY_HOP_HEADERS.has(key.toLowerCase()) && key.toLowerCase() !== 'host') {
headers.set(key, value)
}
})
headers.set('host', target.host)
return headers
}
function copyResponseHeaders(upstream: Response) {
const headers = new Headers()
upstream.headers.forEach((value, key) => {
if (!HOP_BY_HOP_HEADERS.has(key.toLowerCase())) {
headers.set(key, value)
}
})
headers.set('cache-control', 'no-store')
return headers
}
function proxiedAssetPath(value: string, proxyPrefix: string) {
if (
!value ||
value.startsWith('#') ||
value.startsWith('//') ||
/^[a-z][a-z\d+.-]*:/i.test(value)
) {
return value
}
const normalized = value.startsWith('./')
? value.slice(2)
: value.startsWith('/')
? value.slice(1)
: value
return `${proxyPrefix}/${normalized}`
}
function rewriteHtml(body: string, proxyPrefix: string) {
return body.replace(
/(<(?:script|img|link)\b[^>]*\s(?:src|href)=)(["'])([^"']+)(["'])/gi,
(_match, prefix: string, quote: string, value: string, suffix: string) =>
`${prefix}${quote}${proxiedAssetPath(value, proxyPrefix)}${suffix}`
)
}
export async function proxyOpenClawDashboard(request: Request) {
const requestUrl = new URL(request.url)
const { target, proxyPrefix, authorityMode, bridgeActive } = buildTargetUrl(requestUrl)
const method = request.method.toUpperCase()
const headers = copyRequestHeaders(request, target)
const shouldSendBody = !['GET', 'HEAD'].includes(method)
const upstreamInit: RequestInit & { duplex?: 'half' } = {
method,
headers,
body: shouldSendBody ? request.body : undefined,
redirect: 'manual',
cache: 'no-store',
}
if (shouldSendBody) {
upstreamInit.duplex = 'half'
}
const upstream = await fetch(target.toString(), {
...upstreamInit,
})
const responseHeaders = copyResponseHeaders(upstream)
responseHeaders.set('x-openclaw-authority-mode', authorityMode)
responseHeaders.set('x-openclaw-bridge-active', bridgeActive ? 'true' : 'false')
responseHeaders.set('x-openclaw-dashboard-bootstrap-contract', 'tokenized-openclaw-dashboard')
const location = upstream.headers.get('location')
if (location) {
responseHeaders.set('location', location.startsWith('/') ? `${proxyPrefix}${location}` : location)
}
const contentType = upstream.headers.get('content-type') || 'application/octet-stream'
if (contentType.includes('text/html')) {
const body = rewriteHtml(await upstream.text(), proxyPrefix)
responseHeaders.set('content-type', contentType)
return new NextResponse(body, {
status: upstream.status,
headers: responseHeaders,
})
}
return new NextResponse(upstream.body, {
status: upstream.status,
headers: responseHeaders,
})
}
export function proxyErrorResponse(error: unknown) {
return NextResponse.json(
{
ok: false,
error: error instanceof Error ? error.message : 'Failed to proxy OpenClaw dashboard',
},
{ status: 500 }
)
}
@@ -0,0 +1,9 @@
import { proxyErrorResponse, proxyOpenClawDashboard } from '../../../../../dashboard/proxy/shared'
export async function GET(request: Request) {
try {
return await proxyOpenClawDashboard(request)
} catch (error) {
return proxyErrorResponse(error)
}
}
@@ -0,0 +1,9 @@
import { proxyErrorResponse, proxyOpenClawDashboard } from '../../../../dashboard/proxy/shared'
export async function GET(request: Request) {
try {
return await proxyOpenClawDashboard(request)
} catch (error) {
return proxyErrorResponse(error)
}
}
@@ -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 })
}
}
+136 -5
View File
@@ -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({
+9 -4
View File
@@ -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 })
}
}
+121 -102
View File
@@ -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 })
}
}
-206
View File
@@ -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 })
}
}