Stabilize dashboard runtime proxy
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { execFile, spawn } from "node:child_process"
|
||||
import { promisify } from "node:util"
|
||||
import { inspectSandbox } from "@/app/lib/openshellHost"
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
const HOME = process.env.HOME || ""
|
||||
const NEMOCLAW_SETUP = process.env.NEMOCLAW_SETUP || `${HOME}/NemoClaw/scripts/setup.sh`
|
||||
const OPENSHELL_BIN = process.env.OPENSHELL_BIN || `${HOME}/.local/bin/openshell`
|
||||
const HOST_PATH = [
|
||||
`${HOME}/.local/bin`,
|
||||
`${HOME}/.nvm/versions/node/v22.22.2/bin`,
|
||||
`${HOME}/.nvm/versions/node/v22.22.1/bin`,
|
||||
"/opt/homebrew/bin",
|
||||
"/usr/local/bin",
|
||||
"/usr/bin",
|
||||
"/bin",
|
||||
"/usr/sbin",
|
||||
"/sbin",
|
||||
process.env.PATH || "",
|
||||
].filter(Boolean).join(":")
|
||||
|
||||
const GATEWAY_REUSE_POLICY = [
|
||||
"Gateway reuse policy:",
|
||||
"- reuse the existing OpenShell gateway when `openshell status` reports `Connected`",
|
||||
"- only start a fresh gateway when no healthy gateway is already connected",
|
||||
`- force non-interactive PATH to include ${HOME}/.local/bin so openshell resolves during setup`,
|
||||
].join("\n")
|
||||
|
||||
function validateSandboxName(name: string) {
|
||||
if (!name || typeof name !== "string") throw new Error("sandbox name is required")
|
||||
if (name.length > 63) throw new Error("sandbox name too long (max 63 chars)")
|
||||
if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(name)) {
|
||||
throw new Error("sandbox name must be lowercase alphanumeric with optional internal hyphens")
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
type SandboxVerification = {
|
||||
verified: boolean
|
||||
summary: string
|
||||
details?: {
|
||||
name: string
|
||||
id: string | null
|
||||
phase: string | null
|
||||
namespace: string | null
|
||||
sshHostAlias: string
|
||||
rawPhase: string | null
|
||||
rawDetails: string
|
||||
}
|
||||
error?: string
|
||||
}
|
||||
|
||||
const AUTHORITATIVE_SUCCESS_PHASE = "Ready"
|
||||
|
||||
function elapsedMs(start: number) {
|
||||
return Date.now() - start
|
||||
}
|
||||
|
||||
async function verifySandboxCreation(sandboxName: string): Promise<SandboxVerification> {
|
||||
const startedAt = Date.now()
|
||||
console.log(`[sandbox/create] verify:start sandbox=${sandboxName}`)
|
||||
try {
|
||||
const sandbox = await inspectSandbox(sandboxName)
|
||||
const phase = sandbox.phase ?? "Unknown"
|
||||
const rawPhase = sandbox.rawPhase ?? "Unknown"
|
||||
const verified = rawPhase === AUTHORITATIVE_SUCCESS_PHASE
|
||||
console.log(
|
||||
`[sandbox/create] verify:done sandbox=${sandboxName} rawPhase=${rawPhase} normalizedPhase=${phase} verified=${verified} elapsedMs=${elapsedMs(startedAt)}`,
|
||||
)
|
||||
|
||||
return {
|
||||
verified,
|
||||
summary: verified
|
||||
? `Verified via openshell sandbox get with authoritative phase (${rawPhase}).`
|
||||
: `Sandbox lookup succeeded, but authoritative phase was not ready (${rawPhase}); normalized phase=${phase}.`,
|
||||
details: {
|
||||
name: sandbox.name,
|
||||
id: sandbox.id,
|
||||
phase: sandbox.phase,
|
||||
namespace: sandbox.namespace,
|
||||
sshHostAlias: sandbox.sshHostAlias,
|
||||
rawPhase: sandbox.rawPhase,
|
||||
rawDetails: sandbox.rawDetails,
|
||||
},
|
||||
...(verified ? {} : { error: `Sandbox authoritative phase ${rawPhase} is not the required success state ${AUTHORITATIVE_SUCCESS_PHASE}.` }),
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error ?? "Sandbox verification failed")
|
||||
console.log(`[sandbox/create] verify:error sandbox=${sandboxName} elapsedMs=${elapsedMs(startedAt)} message=${message}`)
|
||||
return {
|
||||
verified: false,
|
||||
summary: "Create command returned success, but post-create authoritative verification failed.",
|
||||
error: message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function runCommand(file: string, args: string[], env: NodeJS.ProcessEnv) {
|
||||
const startedAt = Date.now()
|
||||
console.log(`[sandbox/create] command:start file=${file} args=${JSON.stringify(args)}`)
|
||||
console.log(`[sandbox/create] gateway-policy ${GATEWAY_REUSE_POLICY.replace(/\n/g, " | ")}`)
|
||||
try {
|
||||
const { stdout, stderr } = await execFileAsync(file, args, {
|
||||
env,
|
||||
maxBuffer: 20 * 1024 * 1024,
|
||||
})
|
||||
console.log(`[sandbox/create] command:done file=${file} elapsedMs=${elapsedMs(startedAt)} stdoutBytes=${String(stdout).length} stderrBytes=${String(stderr).length}`)
|
||||
return { ok: true as const, stdout: String(stdout).trim(), stderr: String(stderr).trim() }
|
||||
} catch (error: any) {
|
||||
const message = error instanceof Error ? error.message : String(error ?? "Command failed")
|
||||
console.log(
|
||||
`[sandbox/create] command:error file=${file} elapsedMs=${elapsedMs(startedAt)} message=${message} stdoutBytes=${String(error?.stdout || "").length} stderrBytes=${String(error?.stderr || "").length}`,
|
||||
)
|
||||
return {
|
||||
ok: false as const,
|
||||
stdout: String(error?.stdout || "").trim(),
|
||||
stderr: String(error?.stderr || "").trim(),
|
||||
error: message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function runCreateCommandBounded(file: string, args: string[], env: NodeJS.ProcessEnv, timeoutMs: number) {
|
||||
const startedAt = Date.now()
|
||||
console.log(`[sandbox/create] bounded-command:start file=${file} args=${JSON.stringify(args)} timeoutMs=${timeoutMs}`)
|
||||
|
||||
return await new Promise<{
|
||||
completed: boolean
|
||||
timedOut: boolean
|
||||
exitCode: number | null
|
||||
signal: NodeJS.Signals | null
|
||||
stdout: string
|
||||
stderr: string
|
||||
error?: string
|
||||
}>((resolve) => {
|
||||
const child = spawn(file, args, {
|
||||
env,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
})
|
||||
|
||||
let stdout = ""
|
||||
let stderr = ""
|
||||
let settled = false
|
||||
|
||||
const finish = (result: {
|
||||
completed: boolean
|
||||
timedOut: boolean
|
||||
exitCode: number | null
|
||||
signal: NodeJS.Signals | null
|
||||
stdout: string
|
||||
stderr: string
|
||||
error?: string
|
||||
}) => {
|
||||
if (settled) return
|
||||
settled = true
|
||||
clearTimeout(timer)
|
||||
resolve(result)
|
||||
}
|
||||
|
||||
child.stdout?.on("data", (chunk) => {
|
||||
stdout += String(chunk)
|
||||
})
|
||||
|
||||
child.stderr?.on("data", (chunk) => {
|
||||
stderr += String(chunk)
|
||||
})
|
||||
|
||||
child.on("error", (error) => {
|
||||
const message = error instanceof Error ? error.message : String(error ?? "Command failed")
|
||||
console.log(`[sandbox/create] bounded-command:error file=${file} elapsedMs=${elapsedMs(startedAt)} message=${message}`)
|
||||
finish({
|
||||
completed: false,
|
||||
timedOut: false,
|
||||
exitCode: null,
|
||||
signal: null,
|
||||
stdout: stdout.trim(),
|
||||
stderr: stderr.trim(),
|
||||
error: message,
|
||||
})
|
||||
})
|
||||
|
||||
child.on("close", (code, signal) => {
|
||||
console.log(`[sandbox/create] bounded-command:close file=${file} elapsedMs=${elapsedMs(startedAt)} code=${code} signal=${signal} stdoutBytes=${stdout.length} stderrBytes=${stderr.length}`)
|
||||
finish({
|
||||
completed: true,
|
||||
timedOut: false,
|
||||
exitCode: code,
|
||||
signal,
|
||||
stdout: stdout.trim(),
|
||||
stderr: stderr.trim(),
|
||||
...(code === 0 ? {} : { error: `Command exited with code ${code}${signal ? ` signal ${signal}` : ""}` }),
|
||||
})
|
||||
})
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
console.log(`[sandbox/create] bounded-command:timeout file=${file} elapsedMs=${elapsedMs(startedAt)} sending=SIGTERM`)
|
||||
child.kill("SIGTERM")
|
||||
setTimeout(() => {
|
||||
if (!settled) {
|
||||
console.log(`[sandbox/create] bounded-command:timeout-escalate file=${file} elapsedMs=${elapsedMs(startedAt)} sending=SIGKILL`)
|
||||
child.kill("SIGKILL")
|
||||
}
|
||||
}, 2000)
|
||||
finish({
|
||||
completed: false,
|
||||
timedOut: true,
|
||||
exitCode: null,
|
||||
signal: "SIGTERM",
|
||||
stdout: stdout.trim(),
|
||||
stderr: stderr.trim(),
|
||||
error: `Command exceeded timeout of ${timeoutMs}ms`,
|
||||
})
|
||||
}, timeoutMs)
|
||||
})
|
||||
}
|
||||
|
||||
async function waitForSandboxReady(sandboxName: string, timeoutMs: number, intervalMs: number) {
|
||||
const startedAt = Date.now()
|
||||
let lastVerification: SandboxVerification | null = null
|
||||
let attempts = 0
|
||||
|
||||
while (elapsedMs(startedAt) < timeoutMs) {
|
||||
attempts += 1
|
||||
lastVerification = await verifySandboxCreation(sandboxName)
|
||||
console.log(`[sandbox/create] readiness:attempt sandbox=${sandboxName} attempt=${attempts} verified=${lastVerification.verified} elapsedMs=${elapsedMs(startedAt)}`)
|
||||
if (lastVerification.verified) {
|
||||
return {
|
||||
verified: true as const,
|
||||
verification: lastVerification,
|
||||
attempts,
|
||||
elapsedMs: elapsedMs(startedAt),
|
||||
}
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, intervalMs))
|
||||
}
|
||||
|
||||
return {
|
||||
verified: false as const,
|
||||
verification: lastVerification,
|
||||
attempts,
|
||||
elapsedMs: elapsedMs(startedAt),
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
blueprints: [
|
||||
{
|
||||
id: "nemoclaw-blueprint",
|
||||
label: "New NemoClaw Sandbox",
|
||||
description: "Bootstraps a full NemoClaw sandbox using the nemoclaw-blueprint workflow.",
|
||||
type: "blueprint",
|
||||
source: "~/NemoClaw/nemoclaw-blueprint/blueprint.yaml",
|
||||
supportsTailscale: true,
|
||||
},
|
||||
{
|
||||
id: "custom-sandbox",
|
||||
label: "New Custom Sandbox",
|
||||
description: "Create a generic OpenShell sandbox with a custom policy path.",
|
||||
type: "custom",
|
||||
source: "dashboard-custom",
|
||||
supportsTailscale: false,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const requestStartedAt = Date.now()
|
||||
console.log(`[sandbox/create] request:start elapsedMs=0`)
|
||||
try {
|
||||
const body = await request.json()
|
||||
const blueprint = typeof body?.blueprint === "string" ? body.blueprint : ""
|
||||
const sandboxName = validateSandboxName(typeof body?.sandboxName === "string" ? body.sandboxName.trim() : "")
|
||||
const policy = body?.policy && typeof body.policy === "object" ? body.policy : null
|
||||
const enableTailscale = Boolean(body?.enableTailscale)
|
||||
console.log(`[sandbox/create] request:parsed sandbox=${sandboxName} blueprint=${blueprint} enableTailscale=${enableTailscale} elapsedMs=${elapsedMs(requestStartedAt)}`)
|
||||
|
||||
if (!blueprint) {
|
||||
return NextResponse.json({ ok: false, error: "blueprint is required" }, { status: 400 })
|
||||
}
|
||||
|
||||
if (blueprint === "nemoclaw-blueprint") {
|
||||
const env: NodeJS.ProcessEnv = {
|
||||
...process.env,
|
||||
PATH: HOST_PATH,
|
||||
NEMOCLAW_SANDBOX_NAME: sandboxName,
|
||||
}
|
||||
|
||||
if (!enableTailscale) {
|
||||
env.NVIDIA_API_KEY = env.NVIDIA_API_KEY || "optional-local-mode"
|
||||
}
|
||||
|
||||
const result = await runCommand("/bin/bash", [NEMOCLAW_SETUP, sandboxName], env)
|
||||
|
||||
if (!result.ok) {
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
error: result.error,
|
||||
stdout: result.stdout,
|
||||
stderr: result.stderr,
|
||||
blueprint,
|
||||
sandboxName,
|
||||
enableTailscale,
|
||||
}, { status: 500 })
|
||||
}
|
||||
|
||||
const verification = await verifySandboxCreation(sandboxName)
|
||||
const created = verification.verified
|
||||
console.log(`[sandbox/create] request:complete sandbox=${sandboxName} created=${created} elapsedMs=${elapsedMs(requestStartedAt)}`)
|
||||
|
||||
return NextResponse.json({
|
||||
ok: created,
|
||||
blueprint,
|
||||
sandboxName,
|
||||
created,
|
||||
verified: verification.verified,
|
||||
verification,
|
||||
mode: "nemoclaw-blueprint",
|
||||
enableTailscale,
|
||||
gatewayReusePolicy: GATEWAY_REUSE_POLICY,
|
||||
setupPath: NEMOCLAW_SETUP,
|
||||
hostPath: HOST_PATH,
|
||||
stdout: result.stdout,
|
||||
stderr: result.stderr,
|
||||
note: created
|
||||
? (enableTailscale
|
||||
? "NemoClaw blueprint workflow completed with Tailscale-enabled prerequisites. Existing healthy OpenShell gateways are reused before any new gateway start is attempted."
|
||||
: "NemoClaw blueprint workflow completed in local/default mode. Existing healthy OpenShell gateways are reused before any new gateway start is attempted.")
|
||||
: "Blueprint command reported success, but the sandbox never reached a ready verification state afterward.",
|
||||
}, { status: created ? 200 : 502 })
|
||||
}
|
||||
|
||||
if (blueprint === "custom-sandbox") {
|
||||
const env: NodeJS.ProcessEnv = {
|
||||
...process.env,
|
||||
PATH: HOST_PATH,
|
||||
NO_COLOR: "1",
|
||||
CLICOLOR: "0",
|
||||
CLICOLOR_FORCE: "0",
|
||||
}
|
||||
const createAttempt = await runCreateCommandBounded(OPENSHELL_BIN, ["sandbox", "create", "--name", sandboxName], env, 15000)
|
||||
|
||||
if (createAttempt.completed && createAttempt.exitCode !== 0) {
|
||||
return NextResponse.json({
|
||||
ok: false,
|
||||
error: createAttempt.error ?? `create command failed with exit code ${createAttempt.exitCode}`,
|
||||
stdout: createAttempt.stdout,
|
||||
stderr: createAttempt.stderr,
|
||||
blueprint,
|
||||
sandboxName,
|
||||
}, { status: 500 })
|
||||
}
|
||||
|
||||
const readiness = await waitForSandboxReady(sandboxName, 90000, 2000)
|
||||
const verification = readiness.verification ?? {
|
||||
verified: false,
|
||||
summary: "Sandbox readiness polling produced no verification result.",
|
||||
error: "Sandbox readiness polling produced no verification result.",
|
||||
}
|
||||
const created = readiness.verified
|
||||
const policyPrepared = Boolean(policy)
|
||||
console.log(
|
||||
`[sandbox/create] request:complete sandbox=${sandboxName} created=${created} createTimedOut=${createAttempt.timedOut} readinessAttempts=${readiness.attempts} elapsedMs=${elapsedMs(requestStartedAt)}`,
|
||||
)
|
||||
return NextResponse.json({
|
||||
ok: created,
|
||||
blueprint,
|
||||
sandboxName,
|
||||
created,
|
||||
verified: verification.verified,
|
||||
verification,
|
||||
mode: "custom-sandbox",
|
||||
stdout: createAttempt.stdout,
|
||||
stderr: createAttempt.stderr,
|
||||
createCommand: {
|
||||
completed: createAttempt.completed,
|
||||
timedOut: createAttempt.timedOut,
|
||||
exitCode: createAttempt.exitCode,
|
||||
signal: createAttempt.signal,
|
||||
error: createAttempt.error,
|
||||
},
|
||||
readiness: {
|
||||
attempts: readiness.attempts,
|
||||
elapsedMs: readiness.elapsedMs,
|
||||
},
|
||||
policyPrepared,
|
||||
note: created
|
||||
? (createAttempt.timedOut
|
||||
? "Custom sandbox reached Ready even though the create CLI did not exit promptly."
|
||||
: (policyPrepared
|
||||
? "Custom sandbox created. Policy draft is prepared, but applying it live should be a follow-up action."
|
||||
: "Custom sandbox created."))
|
||||
: "Create command started, but the sandbox never reached a ready verification state afterward.",
|
||||
}, { status: created ? 200 : 502 })
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: false, error: `unknown blueprint: ${blueprint}` }, { status: 400 })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Sandbox creation failed"
|
||||
const status = /required|must be|too long|unknown blueprint/.test(message) ? 400 : 500
|
||||
console.log(`[sandbox/create] request:error elapsedMs=${elapsedMs(requestStartedAt)} message=${message}`)
|
||||
return NextResponse.json({ ok: false, error: message }, { status })
|
||||
}
|
||||
}
|
||||
+388
-82
@@ -1,94 +1,400 @@
|
||||
import { NextResponse } from "next/server"
|
||||
import { execFile } from "node:child_process"
|
||||
import { existsSync } from "node:fs"
|
||||
import { hostname, networkInterfaces } from "node:os"
|
||||
import { promisify } from "node:util"
|
||||
import { resolveRuntimeAuthority } from "@/app/lib/runtimeAuthority"
|
||||
|
||||
const execFileAsync = promisify(execFile)
|
||||
|
||||
const HOME = process.env.HOME || ""
|
||||
|
||||
const NODE_BIN_CANDIDATES = [
|
||||
process.env.NODE_BIN,
|
||||
HOME ? `${HOME}/.nvm/versions/node/v22.22.2/bin/node` : undefined,
|
||||
HOME ? `${HOME}/.nvm/versions/node/v22.22.1/bin/node` : undefined,
|
||||
"/Users/markmckeen/.nvm/versions/node/v22.22.2/bin/node",
|
||||
"/Users/markmckeen/.nvm/versions/node/v22.22.1/bin/node",
|
||||
"/opt/homebrew/bin/node",
|
||||
"/usr/local/bin/node",
|
||||
"/usr/bin/node",
|
||||
].filter((value): value is string => Boolean(value))
|
||||
const NODE_BIN = NODE_BIN_CANDIDATES.find((candidate) => existsSync(candidate)) ?? NODE_BIN_CANDIDATES[0]
|
||||
const NEMOCLAW_BIN_CANDIDATES = [
|
||||
process.env.NEMOCLAW_BIN,
|
||||
HOME ? `${HOME}/.local/bin/nemoclaw` : undefined,
|
||||
HOME ? `${HOME}/.nemoclaw/source/bin/nemoclaw.js` : undefined,
|
||||
HOME ? `${HOME}/NemoClaw/bin/nemoclaw.js` : undefined,
|
||||
HOME ? `${HOME}/.nvm/versions/node/v22.22.2/bin/nemoclaw` : undefined,
|
||||
"/Users/markmckeen/.local/bin/nemoclaw",
|
||||
"/Users/markmckeen/.nemoclaw/source/bin/nemoclaw.js",
|
||||
"/Users/markmckeen/NemoClaw/bin/nemoclaw.js",
|
||||
"/Users/markmckeen/NemoClaw-mmckeen/bin/nemoclaw.js",
|
||||
"/Users/markmckeen/.nvm/versions/node/v22.22.2/bin/nemoclaw",
|
||||
"/usr/local/bin/nemoclaw",
|
||||
"/opt/homebrew/bin/nemoclaw",
|
||||
].filter((value): value is string => Boolean(value))
|
||||
const NEMOCLAW_BIN =
|
||||
NEMOCLAW_BIN_CANDIDATES.find((candidate) => existsSync(candidate)) ?? NEMOCLAW_BIN_CANDIDATES[0]
|
||||
|
||||
const OPENSHELL_BIN_CANDIDATES = [
|
||||
process.env.OPENSHELL_BIN,
|
||||
HOME ? `${HOME}/.local/bin/openshell` : undefined,
|
||||
"/Users/markmckeen/.local/bin/openshell",
|
||||
"/Users/markmckeen/OpenShell/target/release/openshell",
|
||||
"/Users/markmckeen/openshell/target/release/openshell",
|
||||
"/Users/markmckeen/openshell/scripts/bin/openshell",
|
||||
"/usr/local/bin/openshell",
|
||||
"/opt/homebrew/bin/openshell",
|
||||
].filter((value): value is string => Boolean(value))
|
||||
const OPENSHELL_BIN =
|
||||
OPENSHELL_BIN_CANDIDATES.find((candidate) => existsSync(candidate)) ?? OPENSHELL_BIN_CANDIDATES[0]
|
||||
|
||||
type SandboxSummary = {
|
||||
id: string
|
||||
name: string
|
||||
namespace: string
|
||||
status: string
|
||||
sshHostAlias: string
|
||||
hasSshConfig: boolean
|
||||
source: "openshell"
|
||||
isDefault: boolean
|
||||
}
|
||||
|
||||
type NemoClawSummary = {
|
||||
available: boolean
|
||||
defaultSandboxNames: string[]
|
||||
serviceLines: string[]
|
||||
summaryLines: string[]
|
||||
source: "nemoclaw-cli" | "none"
|
||||
}
|
||||
|
||||
type SandboxItem = {
|
||||
metadata: {
|
||||
name: string
|
||||
namespace: string
|
||||
labels: Record<string, string>
|
||||
}
|
||||
spec: {
|
||||
template: {
|
||||
image: string | null
|
||||
}
|
||||
}
|
||||
status: {
|
||||
phase: string
|
||||
podIP: string | null
|
||||
conditions: Array<{
|
||||
type: string
|
||||
status: string
|
||||
}>
|
||||
}
|
||||
}
|
||||
|
||||
function stripAnsi(value: string) {
|
||||
return value.replace(/\u001b\[[0-9;]*m/g, "")
|
||||
}
|
||||
|
||||
function parseField(output: string, label: string) {
|
||||
const normalizedLabel = label.toLowerCase()
|
||||
const line = output
|
||||
.split(/\r?\n/)
|
||||
.map((entry) => stripAnsi(entry).trim())
|
||||
.find((entry) => entry.toLowerCase().startsWith(`${normalizedLabel}:`))
|
||||
|
||||
return line ? line.slice(label.length + 1).trim() : null
|
||||
}
|
||||
|
||||
function parseSshHostAlias(sshConfig: string, fallbackName: string) {
|
||||
const hostLine = sshConfig
|
||||
.split(/\r?\n/)
|
||||
.map((entry) => entry.trim())
|
||||
.find((entry) => entry.toLowerCase().startsWith("host "))
|
||||
|
||||
const alias = hostLine
|
||||
?.split(/\s+/)
|
||||
.slice(1)
|
||||
.find((entry) => entry !== "*")
|
||||
|
||||
return alias || `openshell-${fallbackName}`
|
||||
}
|
||||
|
||||
function normalizePhase(phase: string | null) {
|
||||
const value = (phase ?? "Unknown").toLowerCase()
|
||||
|
||||
switch (value) {
|
||||
case "ready":
|
||||
return "Running"
|
||||
case "provisioning":
|
||||
return "Pending"
|
||||
case "deleting":
|
||||
return "Stopping"
|
||||
case "error":
|
||||
return "Error"
|
||||
default:
|
||||
return phase ?? "Unknown"
|
||||
}
|
||||
}
|
||||
|
||||
function parseOpenShellSandboxNames(output: string) {
|
||||
return output
|
||||
.split(/\r?\n/)
|
||||
.map((entry) => stripAnsi(entry).trim())
|
||||
.filter(
|
||||
(entry) =>
|
||||
entry &&
|
||||
!/^name\s+/i.test(entry) &&
|
||||
!/^[\s\-=]+$/.test(entry) &&
|
||||
!/^no sandboxes found\.?$/i.test(entry)
|
||||
)
|
||||
.map((entry) => entry.split(/\s{2,}/)[0]?.trim())
|
||||
.filter((entry): entry is string => Boolean(entry))
|
||||
}
|
||||
|
||||
function parseDefaultSandboxNames(output: string) {
|
||||
return new Set(
|
||||
output
|
||||
.split(/\r?\n/)
|
||||
.map((entry) => stripAnsi(entry))
|
||||
.filter((entry) => /^\s{2,}[\w.-]+(?:\s+\*)?\s*$/.test(entry) && entry.includes("*"))
|
||||
.map((entry) => entry.replace("*", "").trim())
|
||||
.filter(Boolean)
|
||||
)
|
||||
}
|
||||
|
||||
function buildNemoClawSummary(output: string | null, defaultSandboxNames: Set<string>): NemoClawSummary {
|
||||
if (!output) {
|
||||
return {
|
||||
available: false,
|
||||
defaultSandboxNames: [],
|
||||
serviceLines: [],
|
||||
summaryLines: [],
|
||||
source: "none",
|
||||
}
|
||||
}
|
||||
|
||||
const lines = output
|
||||
.split(/\r?\n/)
|
||||
.map((entry) => stripAnsi(entry).trim())
|
||||
.filter(Boolean)
|
||||
|
||||
const serviceLines = lines.filter((entry) => /\((running|stopped|failed|error|unknown)\)/i.test(entry))
|
||||
|
||||
return {
|
||||
available: true,
|
||||
defaultSandboxNames: Array.from(defaultSandboxNames),
|
||||
serviceLines,
|
||||
summaryLines: lines.slice(0, 20),
|
||||
source: "nemoclaw-cli",
|
||||
}
|
||||
}
|
||||
|
||||
async function execNemoclaw(args: string[]) {
|
||||
const env = {
|
||||
...process.env,
|
||||
PATH: `${process.env.PATH ?? ""}:${HOME ? `${HOME}/.local/bin:${HOME}/.nvm/versions/node/v22.22.2/bin:${HOME}/.nvm/versions/node/v22.22.1/bin:` : ""}/opt/homebrew/bin:/usr/local/bin:/usr/bin`,
|
||||
NO_COLOR: "1",
|
||||
CLICOLOR: "0",
|
||||
CLICOLOR_FORCE: "0",
|
||||
}
|
||||
|
||||
const command = /\.(?:c?m?js|ts)$/i.test(NEMOCLAW_BIN)
|
||||
? execFileAsync(NODE_BIN, [NEMOCLAW_BIN, ...args], { env })
|
||||
: execFileAsync(NEMOCLAW_BIN, args, { env })
|
||||
|
||||
const { stdout, stderr } = await command
|
||||
return { stdout, stderr }
|
||||
}
|
||||
|
||||
async function execOpenShell(args: string[]) {
|
||||
const { stdout, stderr } = await execFileAsync(OPENSHELL_BIN, args, {
|
||||
env: {
|
||||
...process.env,
|
||||
NO_COLOR: "1",
|
||||
CLICOLOR: "0",
|
||||
CLICOLOR_FORCE: "0",
|
||||
},
|
||||
})
|
||||
|
||||
return { stdout, stderr }
|
||||
}
|
||||
|
||||
|
||||
function readHostIdentity() {
|
||||
const interfaces = networkInterfaces()
|
||||
const preferred = ["en0", "en1", "bridge0", "lo0"]
|
||||
|
||||
for (const name of preferred) {
|
||||
const entries = interfaces[name] ?? []
|
||||
const match = entries.find((entry) => entry.family === "IPv4" && !entry.internal)
|
||||
if (match?.address) {
|
||||
return { hostname: hostname(), address: match.address, interface: name }
|
||||
}
|
||||
}
|
||||
|
||||
for (const [name, entries] of Object.entries(interfaces)) {
|
||||
const match = (entries ?? []).find((entry) => entry.family === "IPv4" && !entry.internal)
|
||||
if (match?.address) {
|
||||
return { hostname: hostname(), address: match.address, interface: name }
|
||||
}
|
||||
}
|
||||
|
||||
return { hostname: hostname(), address: "127.0.0.1", interface: "lo0" }
|
||||
}
|
||||
|
||||
async function readSandbox(name: string, defaultSandboxNames: Set<string>): Promise<{ summary: SandboxSummary; pod: SandboxItem }> {
|
||||
try {
|
||||
const [{ stdout: detailsStdout }, { stdout: sshStdout }] = await Promise.all([
|
||||
execOpenShell(["sandbox", "get", name]),
|
||||
execOpenShell(["sandbox", "ssh-config", name]),
|
||||
])
|
||||
|
||||
const sandboxName = parseField(detailsStdout, "Name") ?? name
|
||||
const namespace = parseField(detailsStdout, "Namespace") ?? "openshell"
|
||||
const phase = normalizePhase(parseField(detailsStdout, "Phase"))
|
||||
const sandboxId = parseField(detailsStdout, "Id") ?? sandboxName
|
||||
const sshConfig = sshStdout.trim()
|
||||
const sshHostAlias = parseSshHostAlias(sshConfig, sandboxName)
|
||||
const isDefault = defaultSandboxNames.has(sandboxName)
|
||||
|
||||
return {
|
||||
summary: {
|
||||
id: sandboxId,
|
||||
name: sandboxName,
|
||||
namespace,
|
||||
status: phase,
|
||||
sshHostAlias,
|
||||
hasSshConfig: Boolean(sshConfig),
|
||||
source: "openshell",
|
||||
isDefault,
|
||||
},
|
||||
pod: {
|
||||
metadata: {
|
||||
name: sandboxName,
|
||||
namespace,
|
||||
labels: {
|
||||
"nemoclaw.ai/sandbox-name": sandboxName,
|
||||
...(sandboxId ? { "nemoclaw.ai/sandbox-id": sandboxId } : {}),
|
||||
...(isDefault ? { "nemoclaw.ai/default": "true" } : {}),
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
template: {
|
||||
image: null,
|
||||
},
|
||||
},
|
||||
status: {
|
||||
phase,
|
||||
podIP: sshHostAlias,
|
||||
conditions: [
|
||||
{
|
||||
type: "Ready",
|
||||
status: phase === "Running" ? "True" : "False",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
}
|
||||
} catch (error) {
|
||||
const sandboxName = name
|
||||
const isDefault = defaultSandboxNames.has(sandboxName)
|
||||
return {
|
||||
summary: {
|
||||
id: sandboxName,
|
||||
name: sandboxName,
|
||||
namespace: "openshell",
|
||||
status: "Unknown",
|
||||
sshHostAlias: `openshell-${sandboxName}`,
|
||||
hasSshConfig: false,
|
||||
source: "openshell",
|
||||
isDefault,
|
||||
},
|
||||
pod: {
|
||||
metadata: {
|
||||
name: sandboxName,
|
||||
namespace: "openshell",
|
||||
labels: {
|
||||
"nemoclaw.ai/sandbox-name": sandboxName,
|
||||
"nemoclaw.ai/sandbox-id": sandboxName,
|
||||
...(isDefault ? { "nemoclaw.ai/default": "true" } : {}),
|
||||
},
|
||||
},
|
||||
spec: {
|
||||
template: {
|
||||
image: null,
|
||||
},
|
||||
},
|
||||
status: {
|
||||
phase: "Unknown",
|
||||
podIP: `openshell-${sandboxName}`,
|
||||
conditions: [
|
||||
{
|
||||
type: "Ready",
|
||||
status: "False",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Use docker exec with a timeout and direct kubectl inside container
|
||||
const { exec } = await import('child_process')
|
||||
const util = await import('util')
|
||||
const execPromise = util.promisify(exec)
|
||||
const [{ stdout: sandboxListStdout }, nemoclawListResult, nemoclawStatusResult] = await Promise.all([
|
||||
execOpenShell(["sandbox", "list"]),
|
||||
execNemoclaw(["list"]).catch(() => null),
|
||||
execNemoclaw(["status"]).catch(() => null),
|
||||
])
|
||||
|
||||
const dockerPath = '/Applications/Docker.app/Contents/Resources/bin/docker'
|
||||
|
||||
// Run kubectl inside container with explicit kubeconfig
|
||||
const command = `${dockerPath} exec openshell-cluster-openshell /bin/sh -c 'KUBECONFIG=/etc/rancher/k3s/k3s.yaml kubectl get pods -o json -n agent-sandbox-system'`
|
||||
|
||||
const execWithTimeout = (cmd: string, timeoutMs: number): Promise<{ stdout: string; stderr: string }> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = exec(cmd, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
reject(error)
|
||||
} else {
|
||||
resolve({ stdout, stderr })
|
||||
}
|
||||
})
|
||||
|
||||
setTimeout(() => {
|
||||
child.kill('SIGKILL')
|
||||
reject(new Error('Command timed out'))
|
||||
}, timeoutMs)
|
||||
})
|
||||
}
|
||||
|
||||
const { stdout, stderr } = await execWithTimeout(command, 10000)
|
||||
const defaultSandboxNames = parseDefaultSandboxNames(nemoclawListResult?.stdout ?? "")
|
||||
const names = parseOpenShellSandboxNames(sandboxListStdout)
|
||||
const results = await Promise.all(names.map((name) => readSandbox(name, defaultSandboxNames)))
|
||||
const sandboxes = results.map((result) => result.summary)
|
||||
const items = results.map((result) => result.pod)
|
||||
const nemoclaw = buildNemoClawSummary(nemoclawStatusResult?.stdout ?? null, defaultSandboxNames)
|
||||
|
||||
if (stderr && !stderr.includes('Waiting')) {
|
||||
console.error('kubectl stderr:', stderr)
|
||||
}
|
||||
const authorities = sandboxes.map((sandbox) => ({
|
||||
sandboxId: sandbox.id,
|
||||
...resolveRuntimeAuthority({ sandboxId: sandbox.id }),
|
||||
}))
|
||||
|
||||
if (!stdout.trim()) {
|
||||
throw new Error('No output from kubectl')
|
||||
}
|
||||
const inventoryCount = sandboxes.length
|
||||
const hasMappedFallbackWithoutInventory = inventoryCount === 0
|
||||
|
||||
const pods = JSON.parse(stdout)
|
||||
|
||||
// Filter to only sandbox-related pods
|
||||
const sandboxPods = pods.items
|
||||
.filter((pod: any) =>
|
||||
pod.metadata.namespace === 'agent-sandbox-system' ||
|
||||
pod.metadata.name.includes('sandbox')
|
||||
)
|
||||
.map((pod: any) => ({
|
||||
metadata: {
|
||||
name: pod.metadata.name,
|
||||
namespace: pod.metadata.namespace
|
||||
},
|
||||
status: {
|
||||
phase: pod.status.phase,
|
||||
podIP: pod.status.podIP,
|
||||
conditions: pod.status.conditions || []
|
||||
}
|
||||
}))
|
||||
|
||||
return NextResponse.json({
|
||||
pods: { items: sandboxPods },
|
||||
message: 'Real telemetry fetched from container'
|
||||
})
|
||||
} catch (error: any) {
|
||||
console.error('Error fetching real telemetry:', error.message)
|
||||
|
||||
// Fallback to mock data
|
||||
return NextResponse.json({
|
||||
pods: {
|
||||
items: [
|
||||
{
|
||||
metadata: { name: 'agent-sandbox-1', namespace: 'agent-sandbox-system' },
|
||||
status: {
|
||||
phase: 'Running',
|
||||
podIP: '10.42.0.2',
|
||||
conditions: [{ type: 'Ready', status: 'True' }]
|
||||
}
|
||||
},
|
||||
{
|
||||
metadata: { name: 'agent-sandbox-2', namespace: 'agent-sandbox-system' },
|
||||
status: {
|
||||
phase: 'Pending',
|
||||
podIP: null,
|
||||
conditions: [{ type: 'Ready', status: 'False' }]
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
message: `Using mock data (${error.message})`
|
||||
sandboxes,
|
||||
pods: { items },
|
||||
nemoclaw,
|
||||
host: readHostIdentity(),
|
||||
source: "openshell-cli",
|
||||
authoritySource: "runtimeAuthority",
|
||||
truthState: hasMappedFallbackWithoutInventory ? "unverified" : "verified",
|
||||
degraded: hasMappedFallbackWithoutInventory,
|
||||
inventoryCount,
|
||||
authorities: authorities.map((authority) => ({
|
||||
sandboxId: authority.sandboxId,
|
||||
requestedSandboxId: authority.requestedSandboxId,
|
||||
resolvedSandboxId: authority.resolvedSandboxId,
|
||||
sandboxAuthority: authority.sandboxAuthority,
|
||||
instanceId: authority.openclaw.id,
|
||||
sandboxInstanceId: authority.mappedSandboxInstanceId,
|
||||
explicitInstanceOverride: authority.explicitInstanceOverride,
|
||||
usedMappedSandboxInstance: authority.usedMappedSandboxInstance,
|
||||
})),
|
||||
defaultSource: defaultSandboxNames.size > 0 ? "nemoclaw-cli" : "none",
|
||||
count: inventoryCount,
|
||||
message: hasMappedFallbackWithoutInventory
|
||||
? "Fetched live OpenShell inventory: zero sandboxes reported, so any mapped NemoClaw dashboard should be treated as fallback-only."
|
||||
: "Fetched live sandbox inventory from the clean OpenShell runtime",
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Error fetching real telemetry:", error)
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: error instanceof Error ? error.message : "Failed to fetch live OpenShell sandbox inventory",
|
||||
},
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user