Skip to content

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

fix(browser): a sign-in card's Open wakes a browser that closed while idle

AdityaUmale3c0b93341a87
diff --git a/src/components/BrowserPanel.test.ts b/src/components/BrowserPanel.test.ts
index b089510de5..3a0e27d834 100644
--- a/src/components/BrowserPanel.test.ts
+++ b/src/components/BrowserPanel.test.ts
@@ -396,6 +396,26 @@ describe("what the person does in the panel", () => {
await flush();
expect(socket.sent.at(-1)).toEqual({ type: "navigate", url: "https://accounts.example.com/login" });
});
+
+ it("wakes a browser that sat unused before taking it for a sign-in", async () => {
+ await render({ signIn: { url: "https://accounts.example.com/login", site: "example.com", nonce: 1 } });
+ const socket = FakeSocket.instances.at(-1)!;
+ await act(async () => { socket.open(); socket.control("unclaimed"); socket.message({ type: "asleep" }); });
+ await flush();
+ expect(socket.sent).toEqual([{ type: "wake" }]);
+ await act(async () => socket.message({ type: "asleep" }));
+ await flush();
+ expect(socket.sent).toEqual([{ type: "wake" }]);
+ await act(async () => {
+ socket.message({ type: "awake" });
+ socket.message({ type: "tabs", tabs: [{ tabId: "t1", title: "", url: "about:blank", active: true }] });
+ });
+ await flush();
+ expect(socket.sent).toEqual([{ type: "wake" }, { type: "takeControl" }]);
+ await act(async () => socket.control("you"));
+ await flush();
+ expect(socket.sent.at(-1)).toEqual({ type: "navigate", url: "https://accounts.example.com/login" });
+ });
});
describe("a published page's Open", () => {
diff --git a/src/components/BrowserPanel.tsx b/src/components/BrowserPanel.tsx
index 4cc907fb55..28861cdf34 100644
--- a/src/components/BrowserPanel.tsx
+++ b/src/components/BrowserPanel.tsx
@@ -77,6 +77,7 @@ export function LiveBrowser({ bot, signIn, page }: { bot: Bot; signIn?: BrowserS
const resume = useRef<LiveResume & { key: string }>({ key: "", viewer: "" });
const keepNow = useRef(false);
const signInHandled = useRef(0);
+ const signInWoken = useRef(0);
const pageHandled = useRef(0);
const signInPage = useRef<string | null>(null);
const pageNow = useRef<string | null>(null);
@@ -200,13 +201,21 @@ export function LiveBrowser({ bot, signIn, page }: { bot: Bot; signIn?: BrowserS
if (!pageNow.current || pageNow.current === "about:blank") interact({ type: "navigate", url });
};
latest.current = { openSignInPage, takeControl };
+ // Woken once per request: a browser that cannot start answers "asleep" again, and Wake stays for the person.
useEffect(() => {
if (!signIn || !connected || signInHandled.current === signIn.nonce) return;
+ if (asleep) {
+ if (asleep === "asleep" && signInWoken.current !== signIn.nonce && clientRef.current?.send({ type: "wake" })) {
+ signInWoken.current = signIn.nonce;
+ setAsleep("waking");
+ }
+ return;
+ }
signInHandled.current = signIn.nonce;
signInPage.current = signIn.url;
latest.current.takeControl();
latest.current.openSignInPage();
- }, [signIn, connected]);
+ }, [signIn, connected, asleep]);
// A new tab, so whatever the bot has open stays as it was; a sleeping browser is woken first.
useEffect(() => {