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 Escape and focus in the watch view after Take control
AdityaUmale274a895f57c7
diff --git a/src/components/ComputerPanel.takeover.test.ts b/src/components/ComputerPanel.takeover.test.tsindex ef5d41ee1f..9dd81603ba 100644--- a/src/components/ComputerPanel.takeover.test.ts+++ b/src/components/ComputerPanel.takeover.test.ts@@ -179,7 +179,6 @@ afterEach(() => {vi.restoreAllMocks();});-/** Escape where a person presses it: on whatever has focus. */function pressEscape() {(document.activeElement ?? document.body).dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }));}@@ -215,9 +214,21 @@ describe("taking control of a Local VM", () => {expect(fixture.controls, "watching must not pause the bot").toEqual([]);expect(liveDesktop()).toBeNull();- pressEscape();- await settle();+ const exit = watch()!.querySelector<HTMLButtonElement>("header button")!;+ expect(exit.getAttribute("aria-label"), "Exit keeps its name when its text is hidden on narrow windows").toBe("Exit fullscreen");++ // Something beneath the view that also listens for Escape (a turns call ends on it) must not get the same press.+ const beneath: string[] = [];+ const listen = (event: KeyboardEvent) => beneath.push(event.key);+ window.addEventListener("keydown", listen);+ try {+ pressEscape();+ await settle();+ } finally {+ window.removeEventListener("keydown", listen);+ }expect(watch(), "Escape leaves full screen").toBeNull();+ expect(beneath, "the Escape that closes full screen is not also handled beneath it").toEqual([]);expect(fixture.controls).toEqual([]);await press("Full screen");@@ -240,10 +251,15 @@ describe("taking control of a Local VM", () => {expect(watch()!.querySelector("[role=alert]"), "an earlier panel error is not news in full screen").toBeNull();const over = document.createElement("button");+ const overKeys = vi.fn();+ over.addEventListener("keydown", overKeys);document.body.append(over);over.focus();- over.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }));+ const escape = new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true });+ over.dispatchEvent(escape);await settle();+ expect(overKeys, "the dialog over full screen gets its Escape").toHaveBeenCalledOnce();+ expect(escape.defaultPrevented).toBe(false);expect(watch(), "Escape in a dialog over full screen belongs to that dialog").not.toBeNull();over.remove();});@@ -254,11 +270,17 @@ describe("taking control of a Local 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")!;+ take.focus();flushSync(() => take.click());+ expect(take.disabled, "Take control waits while the request is out").toBe(true);+ expect(document.activeElement, "focus stays in full screen, not on a button that went disabled").toBe(watch());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();+ pressEscape();+ await settle();+ expect(watch(), "Escape still leaves full screen after a refused take").toBeNull();});it("full screen keeps Tab inside and gives focus back to Full screen when it closes", async () => {diff --git a/src/components/FullScreenButton.tsx b/src/components/FullScreenButton.tsxindex 2cfd96c88d..3876c48c60 100644--- a/src/components/FullScreenButton.tsx+++ b/src/components/FullScreenButton.tsx@@ -65,25 +65,30 @@ function WatchFullScreen({ frame, name, takeDisabled, error, onClose, onTakeContconst exitRef = useRef<HTMLButtonElement>(null);const { dragProps, noDragStyle } = useCaptionChrome();const macInset = useDesktopCapabilities().capabilities.windowChrome === "mac-inset";- // An error from before this view opened belongs to the panel; only a Take control tried here shows its outcome.+ const takeRef = useRef<HTMLButtonElement>(null);const [tried, setTried] = useState(false);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. A press from- // a dialog opened over this view (such as keyboard shortcuts) is that dialog's own.const onKey = (event: KeyboardEvent) => {if (event.key !== "Escape" || event.defaultPrevented) return;if (!(event.target instanceof Node) || !dialogRef.current?.contains(event.target)) return;event.preventDefault();+ event.stopPropagation();onClose();};window.addEventListener("keydown", onKey, true);return () => window.removeEventListener("keydown", onKey, true);}, [onClose]);+ // A disabled button loses focus to the page, where Escape would no longer reach this view.+ useEffect(() => {+ const active = document.activeElement;+ if (takeDisabled && (active === takeRef.current || active === document.body)) dialogRef.current?.focus();+ }, [takeDisabled]);+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 (@@ -93,11 +98,12 @@ function WatchFullScreen({ frame, name, takeDisabled, error, onClose, onTakeCont<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")}>+ <button ref={exitRef} type="button" style={noDragStyle} onClick={onClose} aria-label={t("desktopViewer.exitFullscreen")} 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+ ref={takeRef}type="button"style={noDragStyle}onClick={() => { setTried(true); onTakeControl(); }}