Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(computer): keep another view's Local VM viewer open when this join is refused

AdityaUmale8bcc047eca5a
diff --git a/src/components/remote-desktop-panel.open.test.ts b/src/components/remote-desktop-panel.open.test.ts
index 26b03810d3..edc0e72201 100644
--- a/src/components/remote-desktop-panel.open.test.ts
+++ b/src/components/remote-desktop-panel.open.test.ts
@@ -6,7 +6,7 @@ import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import type { AppState, Bot, InstanceInfo } from "@/state/store";
-const fixture = vi.hoisted(() => ({ owned: true, controls: [] as Array<{ action: string; controlLeaseId?: string }>, joins: [] as string[] }));
+const fixture = vi.hoisted(() => ({ owned: true, controls: [] as Array<{ action: string; controlLeaseId?: string }>, joins: [] as string[], viewerCloses: 0 }));
vi.mock("./DesktopCapabilities", async (importOriginal) => ({
...await importOriginal<typeof import("./DesktopCapabilities")>(),
useCaptionChrome: () => ({ padClass: undefined }),
@@ -23,6 +23,7 @@ vi.mock("@/state/store", async (importOriginal) => ({
return body.action === "take" ? { held: true, helpReason: null, owned: fixture.owned } : { held: false, helpReason: null };
}
if (path === "/api/bots/scout/computer/join") return { joinUrl: "https://desktop.invalid/scout" };
+ if (path === "/api/bots/scout/computer/viewer-close") fixture.viewerCloses += 1;
if (path.startsWith("/api/bots/scout/local-computer/join?")) {
fixture.joins.push(path);
return { joinUrl: "/vps-viewer/relay-1" };
@@ -52,6 +53,7 @@ let root: Root | null = null;
beforeEach(() => {
fixture.controls = [];
fixture.joins = [];
+ fixture.viewerCloses = 0;
viewerClosedRelease("scout");
});
afterEach(() => {
@@ -127,4 +129,6 @@ it("a client whose host's Local VM is already held in another view is told so, a
await vi.waitFor(() => expect(document.querySelector("[role=alert]")?.textContent).toContain("already controlling this desktop in another view"));
expect(fixture.joins).toEqual([]);
expect(open).not.toHaveBeenCalled();
+ // Nothing opened here, so the other view's viewer on this session stays open.
+ expect(fixture.viewerCloses).toBe(0);
});
diff --git a/src/components/remote-desktop-panel.tsx b/src/components/remote-desktop-panel.tsx
index f31c03f088..6b961b54c4 100644
--- a/src/components/remote-desktop-panel.tsx
+++ b/src/components/remote-desktop-panel.tsx
@@ -179,6 +179,8 @@ export function RemoteDesktopPanel({ bot }: { bot: Bot }) {
const open = async () => {
setPending(true);
setError(null);
+ // Set when another view holds the Local VM: this attempt opened nothing, so it must not close that view's viewer.
+ let heldElsewhere = false;
try {
if (!window.ogb?.desktopViewer) throw new Error(t("remoteDesktop.viewerUnavailable"));
// A take with no target holds the desktop the host runs this bot on, its Local VM included, as a phone's does.
@@ -192,17 +194,22 @@ export function RemoteDesktopPanel({ bot }: { bot: Bot }) {
// The host binds a Local VM viewer to the lease holding it; a hold another view owns can't be joined from here.
let path = "computer/join";
if (localVm) {
- if (!controlLeaseId) throw new LocalizedPanelError("vm.workspace.alreadyControlled");
+ if (!controlLeaseId) {
+ heldElsewhere = true;
+ throw new LocalizedPanelError("vm.workspace.alreadyControlled");
+ }
path = `local-computer/join?${new URLSearchParams({ controlLeaseId })}`;
}
return { joinUrl: viewerAddress((await api(`/api/bots/${bot.id}/${path}`, { method: "POST", body: "{}", signal })).joinUrl) };
},
});
} catch (cause) {
- await api(`/api/bots/${bot.id}/computer/viewer-close`, {
- method: "POST",
- body: "{}",
- }).catch(() => {});
+ if (!heldElsewhere) {
+ await api(`/api/bots/${bot.id}/computer/viewer-close`, {
+ method: "POST",
+ body: "{}",
+ }).catch(() => {});
+ }
setError(panelErrorText(cause instanceof Error ? cause : String(cause)));
} finally {
setPending(false);