"use client" import Link from 'next/link' import { useSearchParams } from 'next/navigation' import { useCallback, useEffect, useMemo, useState } from 'react' interface IntrospectResponse { ok: boolean sandboxId?: string containers?: string[] images?: string[] runningStatuses?: string[] stderr?: string note?: string error?: string } type LoadState = 'idle' | 'loading' | 'ready' | 'error' const SSH_ALIAS = 'ssh openshell-my-assistant' const SSH_FALLBACK = 'ssh -i ~/.ssh/id_ed25519_openclaw_labmac openshell-my-assistant' 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 refreshReadiness = useCallback(async () => { if (!sandboxId) { setState('idle') setData(null) return } setState('loading') setCopyMessage('') try { const response = await fetch(`/api/openshell/terminal/introspect?sandboxId=${encodeURIComponent(sandboxId)}`, { cache: 'no-store', }) const result: IntrospectResponse = 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 so the page can show a truthful operator handoff.', } } if (state === 'ready' && data?.ok) { return { tone: 'border-[var(--status-running)] text-[var(--status-running)]', label: 'READY FOR LOCAL SSH ATTACH', detail: 'This page confirms the sandbox exists. Interactive PTY embedding is not wired up yet, so attach from your local terminal.', } } return { tone: 'border-[var(--status-stopped)] text-[var(--status-stopped)]', label: 'READINESS CHECK FAILED', detail: 'The dashboard could not confirm pod metadata. 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}`) } }, []) return (

Operator path

Operator Terminal

Truthful first render for the current terminal path. This page does not pretend to host an embedded shell; it verifies readiness and tells the operator exactly 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.

Preferred attach command

{SSH_ALIAS}

Fallback if alias or agent config is missing

{SSH_FALLBACK}

If your non-interactive shell misses Homebrew tooling, retry from a login shell or prepend PATH=/opt/homebrew/bin:$PATH before related helper commands.

{copyMessage && (
{copyMessage}
)}

Readiness Evidence

Live pod introspection from the dashboard backend. This is the page’s truth source until persistent PTY transport exists.

Containers

{data?.containers?.length ? data.containers.join(', ') : 'Unavailable'}

Images

{data?.images?.length ? data.images.join(', ') : 'Unavailable'}

Running statuses

{data?.runningStatuses?.length ? data.runningStatuses.join(', ') : 'Unavailable'}

Recovery notes

  • If readiness fails but SSH still works, use the local attach command and treat the dashboard as degraded, not authoritative.
  • If SSH fails, verify the host alias and key path on the Mac first.
  • If both fail, restart the dashboard/backend side and rerun the readiness check.
{(data?.note || data?.error || data?.stderr) && (
{data?.note &&

Note: {data.note}

} {data?.error &&

Error: {data.error}

} {data?.stderr &&

stderr: {data.stderr}

}
)}
) }