Skip to content

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

fix: offer actionable live connection recovery without reloading

bycommute7acd720c6a0b
diff --git a/src/components/LiveConnectionStrip.test.ts b/src/components/LiveConnectionStrip.test.ts
index 7144e1f9a4..6db752cfe4 100644
--- a/src/components/LiveConnectionStrip.test.ts
+++ b/src/components/LiveConnectionStrip.test.ts
@@ -2,8 +2,9 @@
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
-import { LiveConnectionStrip, RECONNECTING_AFTER_MS } from "./LiveConnectionStrip";
+import { LiveConnectionStrip, RECONNECTING_AFTER_MS, CONNECTION_HELP_AFTER_MS } from "./LiveConnectionStrip";
import { t } from "@/lib/i18n";
+import { markCloudAsleep, resetCloudPowerForTests } from "@/lib/cloud-power";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
@@ -16,6 +17,8 @@ beforeEach(() => {
});
afterEach(() => {
act(() => root.unmount());
+ resetCloudPowerForTests();
+ vi.restoreAllMocks();
vi.useRealTimers();
});
const draw = (connected: boolean, hydrated = true) => act(() => root.render(createElement(LiveConnectionStrip, { connected, hydrated })));
@@ -36,3 +39,39 @@ it("says the device is offline at once", () => {
act(() => { window.dispatchEvent(new Event("online")); });
expect(container.textContent).toBe("");
});
+
+it.each([true, false])("offers help and a retry after a prolonged connection failure (hydrated: %s)", async (hydrated) => {
+ draw(false, hydrated);
+ await act(async () => { await vi.advanceTimersByTimeAsync(CONNECTION_HELP_AFTER_MS - 1); });
+ expect(container.querySelector("button")).toBeNull();
+ await act(async () => { await vi.advanceTimersByTimeAsync(1); });
+ expect(container.textContent).toContain(t("liveConnection.help"));
+ const retry = container.querySelector("button")!;
+ expect(retry.textContent).toBe(t("liveConnection.retry"));
+ const sent = vi.spyOn(window, "dispatchEvent");
+ act(() => retry.click());
+ expect(sent).toHaveBeenCalledWith(expect.objectContaining({ type: "omb:retry-live-events" }));
+ draw(true, hydrated);
+ expect(container.textContent).toBe("");
+ draw(false, hydrated);
+ expect(container.querySelector("button")).toBeNull();
+});
+
+it("does not offer a network retry while offline or while a Cloud is asleep", async () => {
+ draw(false);
+ await act(async () => { await vi.advanceTimersByTimeAsync(CONNECTION_HELP_AFTER_MS); });
+ act(() => { window.dispatchEvent(new Event("offline")); });
+ expect(container.textContent).toBe(t("liveConnection.offline"));
+ expect(container.querySelector("button")).toBeNull();
+ act(() => { window.dispatchEvent(new Event("online")); markCloudAsleep(); });
+ expect(container.textContent).toBe("");
+});
+
+it("cancels delayed help when an initial connection recovers", async () => {
+ draw(false, false);
+ await act(async () => { await vi.advanceTimersByTimeAsync(RECONNECTING_AFTER_MS); });
+ expect(container.textContent).toBe("");
+ draw(true);
+ await act(async () => { await vi.advanceTimersByTimeAsync(CONNECTION_HELP_AFTER_MS); });
+ expect(container.textContent).toBe("");
+});
diff --git a/src/components/LiveConnectionStrip.tsx b/src/components/LiveConnectionStrip.tsx
index 038634a30d..2bb5e15dba 100644
--- a/src/components/LiveConnectionStrip.tsx
+++ b/src/components/LiveConnectionStrip.tsx
@@ -2,13 +2,16 @@ import { useEffect, useState } from "react";
import { CloudOff, Loader2 } from "lucide-react";
import { cloudAsleep, onCloudPower } from "@/lib/cloud-power";
import { t } from "@/lib/i18n";
+import { retryLiveEvents } from "@/lib/live-events";
export const RECONNECTING_AFTER_MS = 4_000;
+export const CONNECTION_HELP_AFTER_MS = 20_000;
/** The live stream is down: replies and other people's messages wait until it is back. A blip shorter than a few
* seconds says nothing; a Cloud asleep has its own strip. */
export function LiveConnectionStrip({ connected, hydrated }: { connected: boolean; hydrated: boolean }) {
const [late, setLate] = useState(false);
+ const [help, setHelp] = useState(false);
const [online, setOnline] = useState(() => typeof navigator === "undefined" || navigator.onLine !== false);
const [asleep, setAsleep] = useState(cloudAsleep);
useEffect(() => onCloudPower(setAsleep), []);
@@ -23,15 +26,20 @@ export function LiveConnectionStrip({ connected, hydrated }: { connected: boolea
}, []);
useEffect(() => {
setLate(false);
+ setHelp(false);
if (connected) return;
const timer = setTimeout(() => setLate(true), RECONNECTING_AFTER_MS);
- return () => clearTimeout(timer);
+ const helpTimer = setTimeout(() => setHelp(true), CONNECTION_HELP_AFTER_MS);
+ return () => { clearTimeout(timer); clearTimeout(helpTimer); };
}, [connected]);
- if (asleep || !hydrated) return null;
- const notice = !online ? "offline" : !connected && late ? "reconnecting" : null;
+ if (asleep) return null;
+ const notice = !online ? "offline" : !connected && late && (hydrated || help) ? "reconnecting" : null;
if (!notice) return null;
- return <div role="status" data-live-connection={notice} className="flex items-center justify-center gap-2 border-b border-hairline/40 bg-panel px-3 py-1.5 text-[13px] text-ink-secondary">
- {notice === "offline" ? <CloudOff size={14} aria-hidden="true" /> : <Loader2 size={14} className="animate-spin" aria-hidden="true" />}
- <span>{t(notice === "offline" ? "liveConnection.offline" : "liveConnection.reconnecting")}</span>
+ return <div role="status" data-live-connection={notice} className="flex flex-wrap items-center justify-center gap-2 border-b border-hairline/40 bg-panel px-3 py-1.5 text-[13px] text-ink-secondary">
+ {notice === "offline" ? <CloudOff size={14} className="shrink-0" aria-hidden="true" /> : <Loader2 size={14} className="shrink-0 animate-spin" aria-hidden="true" />}
+ <span>{t(notice === "offline" ? "liveConnection.offline" : help ? "liveConnection.help" : "liveConnection.reconnecting")}</span>
+ {notice === "reconnecting" && help && <button type="button" onClick={retryLiveEvents} className="shrink-0 rounded px-2 py-1 text-ink underline underline-offset-2 hover:bg-raised focus-visible:outline-2 focus-visible:outline-offset-2">
+ {t("liveConnection.retry")}
+ </button>}
</div>;
}
diff --git a/src/lib/live-events.test.ts b/src/lib/live-events.test.ts
index c79afc69c4..19981eb8e0 100644
--- a/src/lib/live-events.test.ts
+++ b/src/lib/live-events.test.ts
@@ -6,6 +6,7 @@ import {
listenLiveFrames,
liveEventsUrl,
openLiveEvents,
+ retryLiveEvents,
shouldReconnectLiveEvents,
type LiveEventSourceLike,
type LiveEventsPlatform,
@@ -123,6 +124,57 @@ describe("the chat's status line", () => {
});
describe("live events supervisor", () => {
+ it("retries immediately with its saved cursor, cancels backoff, and removes the retry listener on stop", () => {
+ const test = harness();
+ vi.stubGlobal("window", {
+ dispatchEvent: (event: Event) => test.windowTarget.emit(event.type),
+ addEventListener: test.windowTarget.addEventListener.bind(test.windowTarget),
+ removeEventListener: test.windowTarget.removeEventListener.bind(test.windowTarget),
+ });
+ const onFrame = vi.fn();
+ const stop = openLiveEvents({ onFrame, onSnapshotRequired: async () => true }, test.platform);
+ try {
+ test.sources[0].open();
+ test.sources[0].message({ kind: "message" }, "run00000:4");
+ test.sources[0].error();
+ retryLiveEvents();
+ expect(test.sources).toHaveLength(2);
+ expect(test.sources[1].url).toBe("/api/events?since=run00000%3A4&screens=off");
+ test.sources[1].open();
+ test.sources[1].message({ kind: "ping" });
+ vi.advanceTimersByTime(500);
+ expect(test.sources).toHaveLength(2);
+ expect(onFrame).toHaveBeenCalledOnce();
+ stop();
+ retryLiveEvents();
+ expect(test.sources).toHaveLength(2);
+ expect(test.windowTarget.count("omb:retry-live-events")).toBe(0);
+ } finally {
+ stop();
+ vi.unstubAllGlobals();
+ }
+ });
+
+ it.each(["offline", "hidden", "asleep"])("does not manually reconnect while %s", (state) => {
+ const test = harness();
+ vi.stubGlobal("window", {
+ dispatchEvent: (event: Event) => test.windowTarget.emit(event.type),
+ addEventListener: test.windowTarget.addEventListener.bind(test.windowTarget),
+ removeEventListener: test.windowTarget.removeEventListener.bind(test.windowTarget),
+ });
+ const stop = openLiveEvents({ onFrame: vi.fn(), onSnapshotRequired: async () => true }, test.platform);
+ try {
+ if (state === "offline") test.setOnline(false);
+ if (state === "hidden") test.setVisible(false);
+ if (state === "asleep") test.sources[0].message({ kind: "power", state: "sleeping" });
+ retryLiveEvents();
+ expect(test.sources).toHaveLength(1);
+ } finally {
+ stop();
+ vi.unstubAllGlobals();
+ }
+ });
+
it("uses data pings as liveness without forwarding them", () => {
const test = harness();
const onFrame = vi.fn();
diff --git a/src/lib/live-events.ts b/src/lib/live-events.ts
index c54c4b0470..cd53a75652 100644
--- a/src/lib/live-events.ts
+++ b/src/lib/live-events.ts
@@ -8,6 +8,11 @@ export const LIVE_EVENTS_PATH = "/api/events";
export const LIVE_EVENTS_STALE_MS = 40_000;
export const LIVE_EVENTS_RETRY_MIN_MS = 500;
export const LIVE_EVENTS_RETRY_MAX_MS = 10_000;
+const RETRY_LIVE_EVENTS = "omb:retry-live-events";
+
+export function retryLiveEvents(): void {
+ window.dispatchEvent(new Event(RETRY_LIVE_EVENTS));
+}
export interface LiveFrame {
kind: string;
@@ -377,6 +382,9 @@ export function openLiveEvents(
const onVisibilityChange = () => {
if (platform.isVisible()) recover();
};
+ const onRetry = () => {
+ if (platform.isVisible() && !cloudAsleep()) reconnectNow(true);
+ };
const onScreenWish = () => queueMicrotask(() => {
if (source && cursor && !pendingSnapshot && screensWanted() !== sourceScreens) reconnectNow(false);
@@ -388,6 +396,7 @@ export function openLiveEvents(
const stopPower = onCloudPower((asleep) => { if (!asleep) reconnectNow(false); });
platform.windowTarget?.addEventListener("online", onOnline);
platform.windowTarget?.addEventListener("focus", onFocus);
+ platform.windowTarget?.addEventListener(RETRY_LIVE_EVENTS, onRetry);
platform.documentTarget?.addEventListener("visibilitychange", onVisibilityChange);
return () => {
@@ -400,6 +409,7 @@ export function openLiveEvents(
closeSource();
platform.windowTarget?.removeEventListener("online", onOnline);
platform.windowTarget?.removeEventListener("focus", onFocus);
+ platform.windowTarget?.removeEventListener(RETRY_LIVE_EVENTS, onRetry);
platform.documentTarget?.removeEventListener("visibilitychange", onVisibilityChange);
};
}
diff --git a/src/locales/en.json b/src/locales/en.json
index 6454005cd9..7aec61d764 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -7157,6 +7157,8 @@
"team.sharedHeader": "Shared · everyone in {team} can see",
"team.sharedHeaderPeople": "Shared · only people added and admins can see",
"liveConnection.reconnecting": "Reconnecting… New replies show up once the connection is back.",
+ "liveConnection.help": "Still unable to connect. Check your connection, sign-in, and any ad or tracker blocker for this site, then retry.",
+ "liveConnection.retry": "Try again",
"liveConnection.offline": "You're offline. New replies show up once you're back online.",
"team.ownHelperHeader": "Own helper · other members can't see it",
"chat.page.usage": "{pages} of {limit} pages",