"use client" import Link from 'next/link' import { useSearchParams } from 'next/navigation' import { useCallback, useEffect, useMemo, useState } from 'react' interface ReadinessPodSummary { containers?: string[] images?: string[] runningStatuses?: string[] phase?: string readyCondition?: string } interface ReadinessResponse { ok: boolean sandboxId?: string podExists?: boolean podReady?: boolean sshReachable?: boolean degraded?: boolean attach?: { alias?: string aliasCommand?: string fallbackCommand?: string loginShellCommand?: string shellHint?: string } pod?: ReadinessPodSummary shellProbe?: { ok?: boolean output?: string stderr?: string error?: string } note?: string error?: string } interface CommandRunResponse { ok: boolean command?: string executionMode?: string exitCode?: number stdout?: string stderr?: string error?: string note?: string } type LoadState = 'idle' | 'loading' | 'ready' | 'error' const defaultAlias = (sandboxId: string | null) => `ssh openshell-${sandboxId || 'my-assistant'}` const defaultFallback = (sandboxId: string | null) => `ssh -i ~/.ssh/id_ed25519_openclaw_labmac openshell-${sandboxId || 'my-assistant'}` const defaultLoginShell = (sandboxId: string | null) => `env PATH=/opt/homebrew/bin:$PATH zsh -l -c 'ssh openshell-${sandboxId || 'my-assistant'}'` const DEFAULT_SHELL_HINT = 'If your non-interactive shell misses Homebrew tooling, retry from a login shell or prepend PATH=/opt/homebrew/bin:$PATH before ssh.' export default function OperatorTerminalPage() { const searchParams = useSearchParams() const sandboxId = searchParams.get('sandboxId') const [state, setState] = useState(sandboxId ? 'loading' : 'idle') const [data, setData] = useState(null) const [lastCheckedAt, setLastCheckedAt] = useState(null) const [copyMessage, setCopyMessage] = useState('') const [commandInput, setCommandInput] = useState('pwd && whoami') const [commandState, setCommandState] = useState<'idle' | 'running' | 'done' | 'error'>('idle') const [commandResult, setCommandResult] = useState(null) const refreshReadiness = useCallback(async () => { if (!sandboxId) { setState('idle') setData(null) return } setState('loading') setCopyMessage('') try { const response = await fetch(`/api/openshell/terminal/readiness?sandboxId=${encodeURIComponent(sandboxId)}`, { cache: 'no-store', }) const result: ReadinessResponse = await response.json() setData(result) setState(response.ok && result.ok ? 'ready' : 'error') setLastCheckedAt(new Date().toLocaleTimeString()) } catch (error) { setData({ ok: false, error: error instanceof Error ? error.message : 'Failed to inspect sandbox readiness.', }) setState('error') setLastCheckedAt(new Date().toLocaleTimeString()) } }, [sandboxId]) useEffect(() => { refreshReadiness() }, [refreshReadiness]) const readiness = useMemo(() => { if (!sandboxId) { return { tone: 'border-[var(--status-pending)] text-[var(--status-pending)]', label: 'NO SANDBOX SELECTED', detail: 'Return to the dashboard and choose a sandbox before opening Operator Terminal.', } } if (state === 'loading') { return { tone: 'border-[var(--status-pending)] text-[var(--status-pending)]', label: 'CHECKING READINESS', detail: 'Verifying pod metadata and bounded shell reachability so the page can report terminal truth in one pass.', } } if (state === 'ready' && data?.ok && data?.podReady && data?.sshReachable) { return { tone: 'border-[var(--status-running)] text-[var(--status-running)]', label: 'TERMINAL PATH VERIFIED', detail: 'The dashboard confirmed the pod is ready and a bounded backend shell exec succeeded. Interactive PTY embedding is still not wired up, but this path now proves command execution reachability without requiring a second manual probe.', } } if (state === 'ready' && data?.ok && data?.podExists && data?.degraded) { return { tone: 'border-[var(--status-pending)] text-[var(--status-pending)]', label: 'DEGRADED: POD UP, SHELL UNCONFIRMED', detail: 'The pod exists, but the bounded shell probe did not succeed from the dashboard backend. Treat the page as degraded and try the local SSH attach path.', } } if (state === 'ready' && data?.ok && data?.podExists) { return { tone: 'border-[var(--status-running)] text-[var(--status-running)]', label: 'READY FOR LOCAL SSH ATTACH', detail: 'The dashboard confirmed pod metadata. Use the attach commands below while persistent PTY transport is still pending.', } } return { tone: 'border-[var(--status-stopped)] text-[var(--status-stopped)]', label: 'READINESS CHECK FAILED', detail: 'The dashboard could not confirm pod metadata or shell reachability. Use the recovery commands below, then retry.', } }, [sandboxId, state, data]) const copyCommand = useCallback(async (command: string) => { try { await navigator.clipboard.writeText(command) setCopyMessage(`Copied: ${command}`) } catch { setCopyMessage(`Copy failed. Use: ${command}`) } }, []) const runCommand = useCallback(async () => { if (!sandboxId) { setCommandState('error') setCommandResult({ ok: false, error: 'Select a sandbox before running a command.' }) return } setCommandState('running') setCommandResult(null) try { const response = await fetch('/api/openshell/terminal', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ sandboxId, command: commandInput, }), }) const result: CommandRunResponse = await response.json() setCommandResult(result) setCommandState(response.ok && result.ok ? 'done' : 'error') } catch (error) { setCommandResult({ ok: false, error: error instanceof Error ? error.message : 'Failed to run dashboard command.', }) setCommandState('error') } }, [commandInput, sandboxId]) const aliasCommand = data?.attach?.aliasCommand || defaultAlias(sandboxId) const fallbackCommand = data?.attach?.fallbackCommand || defaultFallback(sandboxId) const loginShellCommand = data?.attach?.loginShellCommand || defaultLoginShell(sandboxId) const shellHint = data?.attach?.shellHint || DEFAULT_SHELL_HINT return (

Operator path

Operator Terminal

Truthful operator render for the current terminal path. This page still does not pretend to host an embedded PTY, but it now checks pod readiness and bounded shell reachability together before telling you how to attach.

Back to Dashboard

Sandbox

{sandboxId || 'UNSELECTED'}

Status

{readiness.label}

{lastCheckedAt &&

Last checked {lastCheckedAt}

}

{readiness.detail}

Attach Instructions

Preferred path is the SSH alias. If your shell environment is missing the alias or key wiring, use the explicit fallback. The readiness check below is now the page's single preflight source.

Preferred attach command

{aliasCommand}

Fallback if alias or agent config is missing

{fallbackCommand}

{shellHint}

Login-shell retry for Homebrew/path issues

{loginShellCommand}

Bounded shell reachability

This is now captured as part of the readiness contract, so the page can show command execution reachability immediately on load.

{data?.shellProbe?.ok ? 'Probe passed' : 'Probe not confirmed'}

Probe stdout

{data?.shellProbe?.output || 'No backend shell output captured yet.'}

Probe stderr / status

{data?.shellProbe?.error || data?.shellProbe?.stderr || data?.note || 'No stderr.'}

Bounded dashboard command runner

Minimal in-dashboard execution while PTY transport is still pending. Runs a single bounded command through the dashboard backend against the selected sandbox.

{commandState === 'running' ? 'Running…' : commandState === 'done' ? 'Last run succeeded' : commandState === 'error' ? 'Last run failed' : 'Idle'}