Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(computer): keep focus in watch full screen and keep it open when Take control fails
AdityaUmale07b139244933
diff --git a/src/components/ComputerPanel.takeover.test.ts b/src/components/ComputerPanel.takeover.test.tsindex 5326181864..0f86044298 100644--- a/src/components/ComputerPanel.takeover.test.ts+++ b/src/components/ComputerPanel.takeover.test.ts@@ -10,6 +10,8 @@ const fixture = vi.hoisted(() => ({held: false,/** Whether a take with a lease gets it: false plays a hold someone else already has. */owned: true,+ /** True plays a take the server turns down. */+ refuse: false,controls: [] as Array<{ action?: string; controlLeaseId?: string }>,posts: [] as string[],beacons: [] as Array<{ url: string; body: Record<string, unknown>; keepalive: boolean }>,@@ -53,6 +55,7 @@ vi.mock("@/state/store", async (importOriginal) => ({const body = JSON.parse(String(init.body));fixture.controls.push(body);if (body.action === "take") {+ if (fixture.refuse) return { held: false, helpReason: null };const owned = !fixture.held || fixture.owned;fixture.held = true;return { held: true, helpReason: null, ...(body.controlLeaseId ? { owned: owned && fixture.owned, acquired: owned } : {}) };@@ -148,6 +151,7 @@ beforeEach(() => {});fixture.held = false;fixture.owned = true;+ fixture.refuse = false;fixture.controls = [];fixture.posts = [];fixture.beacons = [];@@ -221,6 +225,35 @@ describe("taking control of a Local VM", () => {expect(watch()).toBeNull();});+ it("a refused Take control in full screen keeps watching and says why", async () => {+ fixture.refuse = true;+ await open(makeBot({ computer: "vm" }));+ await press("Full screen");+ const watch = () => document.querySelector<HTMLElement>("[data-watch-full-screen]");+ const take = [...watch()!.querySelectorAll("button")].find((candidate) => candidate.textContent?.trim() === "Take control")!;+ flushSync(() => take.click());+ await settle();+ expect(fixture.controls.map((control) => control.action)).toEqual(["take"]);+ expect(watch(), "a failed take must not drop the person out of full screen").not.toBeNull();+ expect(watch()!.querySelector("[role=alert]")?.textContent).toBeTruthy();+ });++ it("full screen keeps Tab inside and gives focus back to Full screen when it closes", async () => {+ await open(makeBot({ computer: "vm" }));+ const trigger = button("Full screen")!;+ trigger.focus();+ await press("Full screen");+ const watch = document.querySelector<HTMLElement>("[data-watch-full-screen]")!;+ const [exit, take] = [...watch.querySelectorAll("button")];+ expect(document.activeElement).toBe(exit);+ exit!.dispatchEvent(new KeyboardEvent("keydown", { key: "Tab", shiftKey: true, bubbles: true, cancelable: true }));+ expect(document.activeElement, "Shift+Tab from Exit must stay in full screen").toBe(take);+ window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }));+ await settle();+ expect(document.querySelector("[data-watch-full-screen]")).toBeNull();+ expect(document.activeElement).toBe(trigger);+ });+it("closing the panel, leaving its Screen view or closing the page gives back the hold it took", async () => {await open(makeBot({ computer: "vm" }));await press("Take control");diff --git a/src/components/ComputerPanel.tsx b/src/components/ComputerPanel.tsxindex 811209693a..8597a2b181 100644--- a/src/components/ComputerPanel.tsx+++ b/src/components/ComputerPanel.tsx@@ -1613,7 +1613,7 @@ export function ComputerPanel({)}<FullScreenButtonheld={control.held} frame={frameSrc} name={bot.name} onTakeControl={(expand) => void takeControl(expand)}- disabled={control.held && takeBlocked} takeDisabled={controlPending || takeBlocked}+ disabled={control.held && takeBlocked} takeDisabled={controlPending || takeBlocked} error={errorText}/>{cloudPreviewReady && canManageCloud && (cloudBackend === "vps" || boatState !== "archived") && (<buttondiff --git a/src/components/FullScreenButton.tsx b/src/components/FullScreenButton.tsxindex cc402ad5bc..9db4342390 100644--- a/src/components/FullScreenButton.tsx+++ b/src/components/FullScreenButton.tsx@@ -1,14 +1,15 @@-import { useEffect, useState } from "react";+import { useCallback, useEffect, useRef, useState } from "react";import { Hand, Loader2, Maximize2, Minimize2 } from "lucide-react";import { cn } from "@/lib/cn";import { t } from "@/lib/i18n";+import { useModalDialog } from "@/hooks/use-modal-dialog";import { useCaptionChrome, useDesktopCapabilities } from "./DesktopCapabilities";/*** Full screen under the bot's screen. Without control it only watches: the bot keeps working and the picture fills* the window, with Take control as its own button. With control held it goes on into the live desktop, as before.*/-export function FullScreenButton({ held, frame, name, disabled, takeDisabled, onTakeControl }: {+export function FullScreenButton({ held, frame, name, disabled, takeDisabled, error, onTakeControl }: {held: boolean;/** The latest watched frame of the bot's screen. */frame: string | null;@@ -16,28 +17,19 @@ export function FullScreenButton({ held, frame, name, disabled, takeDisabled, ondisabled: boolean;/** The overlay's Take control is off for the same reasons as the panel's own Take control. */takeDisabled: boolean;+ /** Why the last control request failed, shown over the picture so a refused Take control keeps watching. */+ error: string | null;/** Take control; `true` opens the live desktop full screen. */onTakeControl: (expand: boolean) => void;}) {const [watching, setWatching] = useState(false);- const { dragProps, noDragStyle } = useCaptionChrome();- const macInset = useDesktopCapabilities().capabilities.windowChrome === "mac-inset";- const watchingNow = watching && !held;+ const stopWatching = useCallback(() => setWatching(false), []);+ // Once control is held the live desktop takes over; handing it back returns to the panel, not to this view.useEffect(() => {- if (!watchingNow) return;- // Capture, and claim the key, so the same press cannot also close something beneath the overlay.- const onKey = (event: KeyboardEvent) => {- if (event.key !== "Escape" || event.defaultPrevented) return;- event.preventDefault();- setWatching(false);- };- window.addEventListener("keydown", onKey, true);- return () => window.removeEventListener("keydown", onKey, true);- }, [watchingNow]);+ if (held) setWatching(false);+ }, [held]);- const iconButton = "flex items-center gap-1.5 rounded-lg px-2.5 py-1.5 text-[12px] focus-visible:outline-2 focus-visible:outline-focus disabled:opacity-50";- const label = t("computer.screenOf", { name });return (<><button@@ -50,41 +42,82 @@ export function FullScreenButton({ held, frame, name, disabled, takeDisabled, on<Maximize2 size={14} />{t("computer.fullScreen")}</button>- {watchingNow && (- <div data-watch-full-screen role="dialog" aria-modal="true" aria-label={label} className="fixed inset-0 z-[65] flex flex-col bg-app">- <header {...dragProps} className={cn("flex shrink-0 flex-wrap items-center gap-2 border-b border-hairline/40 bg-panel px-3 py-2", macInset && "pl-[86px]")}>- <span className="min-w-0 truncate text-[13px] font-medium text-ink">{label}</span>- <span className="pointer-events-none rounded-full bg-black/70 px-2.5 py-1 text-[11px] font-medium text-white shadow-sm">- {t("computer.watchingBotWorks")}- </span>- <button type="button" style={noDragStyle} autoFocus onClick={() => setWatching(false)} className={cn(iconButton, "ml-auto bg-control text-ink hover:bg-raised-hover")}>- <Minimize2 size={14} aria-hidden="true" />- <span className="max-sm:hidden">{t("desktopViewer.exitFullscreen")}</span>- </button>- <button- type="button"- style={noDragStyle}- onClick={() => { setWatching(false); onTakeControl(true); }}- disabled={takeDisabled}- className={cn(iconButton, "bg-accent font-medium text-accent-ink hover:brightness-110")}- title={t("computer.takeControlTitle")}- >- <Hand size={14} aria-hidden="true" />- {t("computer.takeControl")}- </button>- </header>- <div className="relative flex min-h-0 flex-1 items-center justify-center overflow-hidden bg-inset">- {frame ? (- <img src={frame} alt={label} className="h-full w-full object-contain" />- ) : (- <span role="status" className="flex items-center gap-2 text-[12px] text-ink-secondary">- <Loader2 size={16} className="animate-spin" aria-hidden="true" />- {t("computer.waitingFrame")}- </span>- )}- </div>- </div>+ {watching && !held && (+ <WatchFullScreen+ frame={frame} name={name} takeDisabled={takeDisabled} error={error}+ onClose={stopWatching} onTakeControl={() => onTakeControl(true)}+ />)}</>);}++/** The watch-only view: Tab stays inside it, and focus goes back to Full screen when it closes. */+function WatchFullScreen({ frame, name, takeDisabled, error, onClose, onTakeControl }: {+ frame: string | null;+ name: string;+ takeDisabled: boolean;+ error: string | null;+ onClose: () => void;+ onTakeControl: () => void;+}) {+ const dialogRef = useRef<HTMLDivElement>(null);+ const exitRef = useRef<HTMLButtonElement>(null);+ const { dragProps, noDragStyle } = useCaptionChrome();+ const macInset = useDesktopCapabilities().capabilities.windowChrome === "mac-inset";+ useModalDialog(dialogRef, onClose);+ // After the dialog has noted Full screen as the place to return to, so autoFocus can't take that place.+ useEffect(() => exitRef.current?.focus(), []);++ useEffect(() => {+ // Capture, and claim the key, so the same press cannot also close something beneath the overlay.+ const onKey = (event: KeyboardEvent) => {+ if (event.key !== "Escape" || event.defaultPrevented) return;+ event.preventDefault();+ onClose();+ };+ window.addEventListener("keydown", onKey, true);+ return () => window.removeEventListener("keydown", onKey, true);+ }, [onClose]);++ const iconButton = "flex items-center gap-1.5 rounded-lg px-2.5 py-1.5 text-[12px] focus-visible:outline-2 focus-visible:outline-focus disabled:opacity-50";+ const label = t("computer.screenOf", { name });+ return (+ <div ref={dialogRef} tabIndex={-1} data-watch-full-screen role="dialog" aria-modal="true" aria-label={label} className="fixed inset-0 z-[65] flex flex-col bg-app outline-none">+ <header {...dragProps} className={cn("flex shrink-0 flex-wrap items-center gap-2 border-b border-hairline/40 bg-panel px-3 py-2", macInset && "pl-[86px]")}>+ <span className="min-w-0 truncate text-[13px] font-medium text-ink">{label}</span>+ <span className="pointer-events-none rounded-full bg-black/70 px-2.5 py-1 text-[11px] font-medium text-white shadow-sm">+ {t("computer.watchingBotWorks")}+ </span>+ <button ref={exitRef} type="button" style={noDragStyle} onClick={onClose} className={cn(iconButton, "ml-auto bg-control text-ink hover:bg-raised-hover")}>+ <Minimize2 size={14} aria-hidden="true" />+ <span className="max-sm:hidden">{t("desktopViewer.exitFullscreen")}</span>+ </button>+ <button+ type="button"+ style={noDragStyle}+ onClick={onTakeControl}+ disabled={takeDisabled}+ className={cn(iconButton, "bg-accent font-medium text-accent-ink hover:brightness-110")}+ title={t("computer.takeControlTitle")}+ >+ <Hand size={14} aria-hidden="true" />+ {t("computer.takeControl")}+ </button>+ </header>+ {error && (+ <div role="alert" className="shrink-0 border-b border-danger/30 bg-danger/10 px-3 py-2 text-[12px] text-danger">{error}</div>+ )}+ <div className="relative flex min-h-0 flex-1 items-center justify-center overflow-hidden bg-inset">+ {frame ? (+ <img src={frame} alt={label} className="h-full w-full object-contain" />+ ) : (+ <span role="status" className="flex items-center gap-2 text-[12px] text-ink-secondary">+ <Loader2 size={16} className="animate-spin" aria-hidden="true" />+ {t("computer.waitingFrame")}+ </span>+ )}+ </div>+ </div>+ );+}