Skip to content

Commit 57be6a2

Browse files
authored
feat(moatless): add sandbox status controls (pingdotgg#26)
1 parent 6eed58a commit 57be6a2

15 files changed

Lines changed: 380 additions & 38 deletions

File tree

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export const RPC_REQUIRED_SCOPES = {
9797
[WS_METHODS.subscribePreviewEvents]: AuthOrchestrationReadScope,
9898
[WS_METHODS.subscribeDiscoveredLocalServers]: AuthOrchestrationReadScope,
9999
[WS_METHODS.serversList]: AuthOrchestrationReadScope,
100+
[WS_METHODS.sandboxStatus]: AuthOrchestrationReadScope,
101+
[WS_METHODS.sandboxStart]: AuthOrchestrationOperateScope,
100102
[WS_METHODS.subscribeServerStatus]: AuthOrchestrationReadScope,
101103
[WS_METHODS.serversSubscribeLogs]: AuthOrchestrationReadScope,
102104
[WS_METHODS.subscribeServerConfig]: AuthOrchestrationReadScope,

‎apps/server/src/ws.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,14 +1986,26 @@ const makeWsRpcLayer = (
19861986
// Thread servers are containers a hosted environment declares and runs.
19871987
// This server runs threads directly on the machine it is on, so it
19881988
// declares none — the honest answer is an empty list rather than an
1989-
// unsupported method, and both subscriptions have nothing to report.
1990-
// A hosted environment answers the same three methods with real data.
1989+
// unsupported method, and the subscriptions have nothing to report.
1990+
// A hosted environment answers these methods with real data.
19911991
[WS_METHODS.serversList]: (_input) =>
19921992
observeRpcEffect(
19931993
WS_METHODS.serversList,
19941994
Effect.succeed({ servers: [], sandboxStatus: "not_created" as const }),
19951995
{ "rpc.aggregate": "servers" },
19961996
),
1997+
[WS_METHODS.sandboxStatus]: (_input) =>
1998+
observeRpcEffect(
1999+
WS_METHODS.sandboxStatus,
2000+
Effect.succeed({ sandboxStatus: "not_created" as const }),
2001+
{ "rpc.aggregate": "sandbox" },
2002+
),
2003+
[WS_METHODS.sandboxStart]: (_input) =>
2004+
observeRpcEffect(
2005+
WS_METHODS.sandboxStart,
2006+
Effect.succeed({ sandboxStatus: "not_created" as const }),
2007+
{ "rpc.aggregate": "sandbox" },
2008+
),
19972009
[WS_METHODS.subscribeServerStatus]: (_input) =>
19982010
observeRpcStream(WS_METHODS.subscribeServerStatus, Stream.never, {
19992011
"rpc.aggregate": "servers",

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ import {
138138
selectThreadPreviewMiniPlayer,
139139
usePreviewMiniPlayerStore,
140140
} from "../previewMiniPlayerStore";
141-
import { RightPanelTabs } from "./RightPanelTabs";
141+
import { SandboxedRightPanelTabs as RightPanelTabs } from "./SandboxedRightPanelTabs";
142142
import { DiffWorkerPoolProvider } from "./DiffWorkerPoolProvider";
143143
import { BranchToolbar } from "./BranchToolbar";
144144
import { resolveShortcutCommand, shortcutLabelForCommand } from "../keybindings";
@@ -226,7 +226,7 @@ import { DraftHeroHeadline } from "./chat/DraftHeroHeadline";
226226
import { ExpandedImageDialog } from "./chat/ExpandedImageDialog";
227227
import { PullRequestThreadDialog } from "./PullRequestThreadDialog";
228228
import { MessagesTimeline } from "./chat/MessagesTimeline";
229-
import { ChatHeader } from "./chat/ChatHeader";
229+
import { SandboxedChatHeader as ChatHeader } from "./chat/SandboxedChatHeader";
230230
import { PanelLayoutControls, RightPanelMaximizeControl } from "./chat/PanelLayoutControls";
231231
import { type ExpandedImagePreview } from "./chat/ExpandedImagePreview";
232232
import { NoActiveThreadState } from "./NoActiveThreadState";
@@ -6125,6 +6125,7 @@ function ChatViewContent(props: ChatViewProps) {
61256125

61266126
{!shouldUsePlanSidebarSheet && rightPanelOpen && activeThreadRef ? (
61276127
<RightPanelTabs
6128+
threadRef={activeThreadRef}
61286129
mode="inline"
61296130
maximized={rightPanelMaximized}
61306131
surfaces={rightPanelState.surfaces}
@@ -6153,6 +6154,7 @@ function ChatViewContent(props: ChatViewProps) {
61536154
{shouldUsePlanSidebarSheet && rightPanelOpen && activeThreadRef ? (
61546155
<RightPanelSheet open onClose={planSidebarOpen ? closePlanSidebar : closePreviewPanel}>
61556156
<RightPanelTabs
6157+
threadRef={activeThreadRef}
61566158
mode="sheet"
61576159
layoutControls={panelToggleControls}
61586160
surfaces={rightPanelState.surfaces}

‎apps/web/src/components/RightPanelTabs.tsx‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
2424

2525
import { PreviewPanelShell, type PreviewPanelMode } from "./preview/PreviewPanelShell";
2626
import { PierreEntryIcon } from "./chat/PierreEntryIcon";
27+
import { RightPanelDisabledState } from "./sandbox/RightPanelDisabledState";
2728

2829
interface RightPanelTabsProps {
2930
mode: PreviewPanelMode;
@@ -48,6 +49,8 @@ interface RightPanelTabsProps {
4849
browserAvailable: boolean;
4950
diffAvailable: boolean;
5051
filesAvailable: boolean;
52+
surfaceDisabled?: boolean | undefined;
53+
surfaceDisabledReason?: string | undefined;
5154
children: ReactNode;
5255
}
5356

@@ -70,7 +73,7 @@ function DisabledReasonTooltip(props: { reason: string; trigger: ReactElement })
7073

7174
function SurfaceMenuItem(props: {
7275
available: boolean;
73-
disabledReason?: string;
76+
disabledReason?: string | undefined;
7477
onClick: () => void;
7578
children: ReactNode;
7679
}) {
@@ -280,6 +283,11 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
280283
const ownsDesktopTitleBar = isElectron && props.mode === "inline";
281284
const { resolvedTheme } = useTheme();
282285
const tabListRef = useRef<HTMLDivElement>(null);
286+
const surfaceDisabledReason =
287+
props.surfaceDisabledReason ?? "Start the sandbox to use right-panel surfaces.";
288+
const surfaceAvailable = (available: boolean) => available && !props.surfaceDisabled;
289+
const disabledReason = (reason: string) =>
290+
props.surfaceDisabled ? surfaceDisabledReason : reason;
283291

284292
const handleTabContextMenu = useCallback(
285293
async (event: ReactMouseEvent, surface: RightPanelSurface) => {
@@ -456,28 +464,32 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
456464
</MenuTrigger>
457465
<MenuPopup align="start" side="bottom" sideOffset={6} className="min-w-44">
458466
<SurfaceMenuItem
459-
available={props.browserAvailable}
460-
disabledReason={SURFACE_DISABLED_REASONS.browser}
467+
available={surfaceAvailable(props.browserAvailable)}
468+
disabledReason={disabledReason(SURFACE_DISABLED_REASONS.browser)}
461469
onClick={props.onAddBrowser}
462470
>
463471
<Globe2 />
464472
Browser
465473
</SurfaceMenuItem>
466-
<SurfaceMenuItem available onClick={props.onAddTerminal}>
474+
<SurfaceMenuItem
475+
available={!props.surfaceDisabled}
476+
disabledReason={props.surfaceDisabled ? surfaceDisabledReason : undefined}
477+
onClick={props.onAddTerminal}
478+
>
467479
<TerminalSquare />
468480
Terminal
469481
</SurfaceMenuItem>
470482
<SurfaceMenuItem
471-
available={props.filesAvailable}
472-
disabledReason={SURFACE_DISABLED_REASONS.files}
483+
available={surfaceAvailable(props.filesAvailable)}
484+
disabledReason={disabledReason(SURFACE_DISABLED_REASONS.files)}
473485
onClick={props.onAddFiles}
474486
>
475487
<Files />
476488
Files
477489
</SurfaceMenuItem>
478490
<SurfaceMenuItem
479-
available={props.diffAvailable}
480-
disabledReason={SURFACE_DISABLED_REASONS.diff}
491+
available={surfaceAvailable(props.diffAvailable)}
492+
disabledReason={disabledReason(SURFACE_DISABLED_REASONS.diff)}
481493
onClick={props.onAddDiff}
482494
>
483495
<FileDiff />
@@ -491,7 +503,9 @@ export function RightPanelTabs(props: RightPanelTabsProps) {
491503
{props.layoutControls}
492504
</div>
493505
<div className="flex min-h-0 flex-1 flex-col">
494-
{props.activeSurfaceId === null ? (
506+
{props.surfaceDisabled ? (
507+
<RightPanelDisabledState reason={surfaceDisabledReason} />
508+
) : props.activeSurfaceId === null ? (
495509
<RightPanelEmptyState
496510
onAddBrowser={props.onAddBrowser}
497511
onAddTerminal={props.onAddTerminal}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import {
2+
isAtomCommandInterrupted,
3+
squashAtomCommandFailure,
4+
} from "@t3tools/client-runtime/state/runtime";
5+
import type { SandboxStatusResult, ScopedThreadRef } from "@t3tools/contracts";
6+
import { AlertTriangleIcon, LoaderCircleIcon, PlayIcon } from "lucide-react";
7+
import { useCallback, useState } from "react";
8+
9+
import { cn } from "~/lib/utils";
10+
import type { EnvironmentQueryView } from "~/state/query";
11+
import { serversEnvironment } from "~/state/servers";
12+
import { useAtomCommand } from "~/state/use-atom-command";
13+
14+
import { Button } from "./ui/button";
15+
16+
type SandboxStatus = SandboxStatusResult["sandboxStatus"];
17+
18+
const STATUS_COPY: Record<SandboxStatus, { readonly tone: "muted" | "warning" | "error" }> = {
19+
not_created: {
20+
tone: "warning",
21+
},
22+
initializing: {
23+
tone: "muted",
24+
},
25+
ready: {
26+
tone: "muted",
27+
},
28+
stopped: {
29+
tone: "warning",
30+
},
31+
removing: {
32+
tone: "muted",
33+
},
34+
removed: {
35+
tone: "warning",
36+
},
37+
error: {
38+
tone: "error",
39+
},
40+
};
41+
42+
const STARTABLE_STATUSES = new Set<SandboxStatus>(["not_created", "stopped", "removed", "error"]);
43+
44+
interface SandboxStatusControlProps {
45+
readonly threadRef: ScopedThreadRef;
46+
readonly status: EnvironmentQueryView<SandboxStatusResult>;
47+
readonly className?: string | undefined;
48+
}
49+
50+
function failureMessage(error: unknown): string {
51+
return error instanceof Error && error.message.trim().length > 0
52+
? error.message
53+
: "Could not start the sandbox.";
54+
}
55+
56+
export function SandboxStatusControl({ threadRef, status, className }: SandboxStatusControlProps) {
57+
const startSandbox = useAtomCommand(serversEnvironment.startSandbox, { reportFailure: false });
58+
const [isStarting, setIsStarting] = useState(false);
59+
const [startError, setStartError] = useState<string | null>(null);
60+
const { refresh } = status;
61+
const sandboxStatus = status.data?.sandboxStatus ?? null;
62+
63+
const startable =
64+
!status.isPending && sandboxStatus !== null && STARTABLE_STATUSES.has(sandboxStatus);
65+
66+
const handleStart = useCallback(async () => {
67+
setStartError(null);
68+
setIsStarting(true);
69+
const result = await startSandbox({
70+
environmentId: threadRef.environmentId,
71+
input: { threadId: threadRef.threadId },
72+
});
73+
setIsStarting(false);
74+
refresh();
75+
if (result._tag === "Success" || isAtomCommandInterrupted(result)) {
76+
return;
77+
}
78+
setStartError(failureMessage(squashAtomCommandFailure(result)));
79+
}, [refresh, startSandbox, threadRef.environmentId, threadRef.threadId]);
80+
81+
const statusMeta = !status.isPending && sandboxStatus ? STATUS_COPY[sandboxStatus] : null;
82+
const tone =
83+
startError || status.error
84+
? "error"
85+
: status.isPending
86+
? "muted"
87+
: (statusMeta?.tone ?? "warning");
88+
const label =
89+
status.isPending && !startError && !status.error
90+
? "Checking sandbox status"
91+
: sandboxStatus === "ready"
92+
? "Sandbox running"
93+
: "Sandbox unavailable";
94+
const showRetry = Boolean(status.error) && !startable;
95+
const actionLabel = sandboxStatus === "error" || startError ? "Retry" : "Start";
96+
97+
return (
98+
<div
99+
aria-live="polite"
100+
className={cn(
101+
"flex min-w-0 shrink-0 items-center gap-2 rounded-md border border-border/70 bg-background px-2 py-1 shadow-xs",
102+
tone === "error" && "border-destructive/35 bg-destructive/5",
103+
tone === "warning" && "border-warning/35 bg-warning/5",
104+
className,
105+
)}
106+
>
107+
<div className="flex min-w-0 items-center gap-2">
108+
{status.isPending || sandboxStatus === "initializing" || sandboxStatus === "removing" ? (
109+
<LoaderCircleIcon className="size-3.5 shrink-0 animate-spin text-muted-foreground" />
110+
) : tone === "error" ? (
111+
<AlertTriangleIcon className="size-3.5 shrink-0 text-destructive" />
112+
) : (
113+
<span className="size-2 shrink-0 rounded-full bg-warning" />
114+
)}
115+
<div className="min-w-0 truncate text-xs font-medium text-foreground">{label}</div>
116+
</div>
117+
{showRetry ? (
118+
<Button size="xs" variant="outline" onClick={status.refresh}>
119+
Retry
120+
</Button>
121+
) : startable ? (
122+
<Button size="xs" variant="outline" disabled={isStarting} onClick={handleStart}>
123+
{isStarting ? (
124+
<LoaderCircleIcon className="size-3.5 animate-spin" />
125+
) : (
126+
<PlayIcon className="size-3.5" />
127+
)}
128+
<span>{actionLabel}</span>
129+
</Button>
130+
) : null}
131+
</div>
132+
);
133+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { ScopedThreadRef } from "@t3tools/contracts";
2+
import type { ComponentProps } from "react";
3+
4+
import { useSandboxAvailability } from "./sandbox/useSandboxAvailability";
5+
import { RightPanelTabs } from "./RightPanelTabs";
6+
7+
type SandboxedRightPanelTabsProps = ComponentProps<typeof RightPanelTabs> & {
8+
readonly threadRef: ScopedThreadRef;
9+
};
10+
11+
export function SandboxedRightPanelTabs({ threadRef, ...props }: SandboxedRightPanelTabsProps) {
12+
const sandboxAvailability = useSandboxAvailability(threadRef);
13+
14+
return (
15+
<RightPanelTabs
16+
{...props}
17+
surfaceDisabled={sandboxAvailability.surfaceDisabled}
18+
surfaceDisabledReason={sandboxAvailability.surfaceDisabledReason}
19+
/>
20+
);
21+
}

‎apps/web/src/components/chat/ChatHeader.tsx‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import {
66
type ThreadId,
77
} from "@t3tools/contracts";
88
import { scopeThreadRef } from "@t3tools/client-runtime/environment";
9-
import { memo } from "react";
10-
import GitActionsControl from "../GitActionsControl";
9+
import { memo, type ReactNode } from "react";
1110
import { type DraftId } from "~/composerDraftStore";
1211
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
12+
import GitActionsControl from "../GitActionsControl";
1313
import ProjectScriptsControl, {
1414
type NewProjectScriptInput,
1515
type ProjectScriptActionResult,
@@ -35,6 +35,7 @@ interface ChatHeaderProps {
3535
availableEditors: ReadonlyArray<EditorId>;
3636
rightPanelOpen: boolean;
3737
gitCwd: string | null;
38+
actionsOverride?: ReactNode;
3839
onNewThreadInProject: () => void;
3940
onRunProjectScript: (script: ProjectScript) => void;
4041
onAddProjectScript: (input: NewProjectScriptInput) => Promise<ProjectScriptActionResult>;
@@ -71,6 +72,7 @@ export const ChatHeader = memo(function ChatHeader({
7172
availableEditors,
7273
rightPanelOpen,
7374
gitCwd,
75+
actionsOverride,
7476
onNewThreadInProject,
7577
onRunProjectScript,
7678
onAddProjectScript,
@@ -141,7 +143,8 @@ export const ChatHeader = memo(function ChatHeader({
141143
rightPanelOpen ? "pr-0" : "pr-16",
142144
)}
143145
>
144-
{activeProjectScripts && (
146+
{actionsOverride}
147+
{!actionsOverride && activeProjectScripts && (
145148
<ProjectScriptsControl
146149
scripts={activeProjectScripts}
147150
fileScripts={fileScripts}
@@ -153,15 +156,15 @@ export const ChatHeader = memo(function ChatHeader({
153156
onDeleteScript={onDeleteProjectScript}
154157
/>
155158
)}
156-
{showOpenInPicker && FEATURES.workspaceOpenIn && (
159+
{!actionsOverride && showOpenInPicker && FEATURES.workspaceOpenIn && (
157160
<OpenInPicker
158161
environmentId={activeThreadEnvironmentId}
159162
keybindings={keybindings}
160163
availableEditors={availableEditors}
161164
openInCwd={openInCwd}
162165
/>
163166
)}
164-
{activeProjectName && FEATURES.versionControl && (
167+
{!actionsOverride && activeProjectName && FEATURES.versionControl && (
165168
<GitActionsControl
166169
gitCwd={gitCwd}
167170
activeThreadRef={scopeThreadRef(activeThreadEnvironmentId, activeThreadId)}

0 commit comments

Comments
 (0)