Skip to content

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

Web: open a conversation from an https link, and keep it through sign-in

Purv Kabaria78e743d21747
diff --git a/src/hooks/use-external-chat-links.test.ts b/src/hooks/use-external-chat-links.test.ts
index 7094a009b8..c616f93272 100644
--- a/src/hooks/use-external-chat-links.test.ts
+++ b/src/hooks/use-external-chat-links.test.ts
@@ -81,6 +81,20 @@ describe("useExternalChatLinks", () => {
]);
});
+ it("opens a thread named in the web address once the bots have loaded, and only once", () => {
+ const replaceState = vi.fn();
+ vi.stubGlobal("window", { location: { pathname: "/", search: "?thread=scout-main&bot=scout", hash: "" }, history: { replaceState } });
+ const dispatch = vi.fn();
+ render(loading, dispatch);
+ expect(fixture.link).toBe("openmausbot://thread/scout-main?bot=scout");
+ expect(replaceState).toHaveBeenCalledWith(null, "", "/");
+ expect(dispatch).not.toHaveBeenCalled();
+
+ render(loaded, dispatch);
+ expect(dispatch).toHaveBeenCalled();
+ expect(fixture.link).toBeNull();
+ });
+
it("does nothing without the desktop bridge", () => {
vi.stubGlobal("window", {});
const dispatch = vi.fn();
diff --git a/src/hooks/use-external-chat-links.ts b/src/hooks/use-external-chat-links.ts
index a4e0a971b3..c18f32c90a 100644
--- a/src/hooks/use-external-chat-links.ts
+++ b/src/hooks/use-external-chat-links.ts
@@ -1,12 +1,17 @@
import { useEffect, useState } from "react";
import { openChatLink } from "@/components/ThreadRefs";
import { useShowThreads } from "@/lib/thread-preferences";
+import { takeThreadLinkFromLocation } from "@/lib/thread-refs";
import { openThread, type Action, type AppState, type ThreadTarget } from "@/state/store";
export function useExternalChatLinks(state: AppState, dispatch: (action: Action) => void): void {
const [link, setLink] = useState<string | null>(null);
const showThreads = useShowThreads();
- useEffect(() => window.ogb?.onChatLink?.(setLink), []);
+ useEffect(() => {
+ const fromAddress = takeThreadLinkFromLocation();
+ if (fromAddress) setLink(fromAddress);
+ return window.ogb?.onChatLink?.(setLink);
+ }, []);
useEffect(() => {
if (!link || !state.hydrated) return;
setLink(null);
diff --git a/src/lib/phone-pairing.test.ts b/src/lib/phone-pairing.test.ts
index 6937fb41ba..9c723ecbf6 100644
--- a/src/lib/phone-pairing.test.ts
+++ b/src/lib/phone-pairing.test.ts
@@ -173,6 +173,13 @@ describe("the ?desktop-settings=phone request", () => {
expect(pairedDestination("?next=//evil.example")).toBe("/");
expect(pairedDestination("")).toBe("/");
});
+
+ it("keeps a linked thread through sign-in, and nothing else with it", () => {
+ expect(pairedDestination("?thread=t-1&bot=scout")).toBe("/?thread=t-1&bot=scout");
+ expect(pairedDestination("?thread=t%201&x=1")).toBe("/?thread=t+1");
+ expect(pairedDestination("?bot=scout")).toBe("/");
+ expect(pairedDestination("?thread=t-1&next=/settings")).toBe("/settings");
+ });
});
describe("the person's Cloud as a destination on this computer", () => {
diff --git a/src/lib/phone-pairing.ts b/src/lib/phone-pairing.ts
index 7b24f5d740..65e6c265f5 100644
--- a/src/lib/phone-pairing.ts
+++ b/src/lib/phone-pairing.ts
@@ -116,10 +116,16 @@ export function takePhonePairingRequest(href: string): string | null {
return `${url.pathname}${url.search}${url.hash}`;
}
-/** Where a page signed in by the app goes next: the phone pairing, or back to a path of this same server. */
+/** Where a page signed in by the app goes next: the phone pairing, back to a path of this same server, or a linked thread. */
export function pairedDestination(search: string): string {
const params = new URLSearchParams(search);
if (params.get("desktop-settings") === "phone") return "/?desktop-settings=phone";
const next = params.get("next");
- return next && /^\/(?!\/)/.test(next) && !next.startsWith("/pair") && !/[\\\s]/.test(next) ? next : "/";
+ if (next && /^\/(?!\/)/.test(next) && !next.startsWith("/pair") && !/[\\\s]/.test(next)) return next;
+ const thread = params.get("thread");
+ if (thread === null) return "/";
+ const keep = new URLSearchParams({ thread });
+ const bot = params.get("bot");
+ if (bot !== null) keep.set("bot", bot);
+ return `/?${keep}`;
}
diff --git a/src/lib/thread-refs.test.ts b/src/lib/thread-refs.test.ts
index b354179b59..4801045b1e 100644
--- a/src/lib/thread-refs.test.ts
+++ b/src/lib/thread-refs.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import {
parseThreadRefUrl,
@@ -7,6 +7,7 @@ import {
resolveThreadRefs,
serializeThreadRefs,
splitThreadRefsForDisplay,
+ takeThreadLinkFromLocation,
threadRefUrl,
threadTokenFromPaste,
threadTokenSpacing,
@@ -117,6 +118,32 @@ describe("remarkThreadRefs", () => {
});
});
+describe("a thread named in the web app's address", () => {
+ afterEach(() => vi.unstubAllGlobals());
+ const at = (search: string, hash = "") => {
+ const replaceState = vi.fn();
+ vi.stubGlobal("window", { location: { pathname: "/", search, hash }, history: { replaceState } });
+ return replaceState;
+ };
+
+ it("becomes the chat link it stands for and leaves the address with everything else", () => {
+ const replaceState = at("?lang=fr&thread=t%201&bot=scout", "#top");
+ expect(takeThreadLinkFromLocation()).toBe("openmausbot://thread/t%201?bot=scout");
+ expect(replaceState).toHaveBeenCalledWith(null, "", "/?lang=fr#top");
+ expect(parseThreadRefUrl("openmausbot://thread/t%201?bot=scout")).toEqual({ threadId: "t 1", botId: "scout" });
+ });
+
+ it("works without a bot, and does nothing when no thread is named", () => {
+ at("?thread=t-1");
+ expect(takeThreadLinkFromLocation()).toBe("openmausbot://thread/t-1");
+ const untouched = at("?bot=scout");
+ expect(takeThreadLinkFromLocation()).toBeNull();
+ expect(untouched).not.toHaveBeenCalled();
+ vi.stubGlobal("window", {});
+ expect(takeThreadLinkFromLocation()).toBeNull();
+ });
+});
+
describe("canonical thread links", () => {
const uuid = "8b1b1d62-9d3c-4a1e-9f2a-3c5d7e9b1a04";
const uuidThreads = [scout(uuid, "QA PR 245"), ada("0f0e0d0c-0b0a-4909-8807-060504030201", "Release notes")];
diff --git a/src/lib/thread-refs.ts b/src/lib/thread-refs.ts
index 906c2d4d0c..40dbdb659a 100644
--- a/src/lib/thread-refs.ts
+++ b/src/lib/thread-refs.ts
@@ -124,6 +124,21 @@ export function threadRefUrl(ref: { botId: string; threadId: string }): string {
return THREAD_URL_PREFIX + encodeURIComponent(ref.threadId) + "?bot=" + encodeURIComponent(ref.botId);
}
+/** `?thread=<threadId>&bot=<botId>` on the web app's address: a chat link that works outside the app. */
+export function takeThreadLinkFromLocation(): string | null {
+ const location = window.location;
+ if (!location) return null;
+ const params = new URLSearchParams(location.search);
+ const thread = params.get("thread");
+ if (thread === null) return null;
+ const bot = params.get("bot");
+ params.delete("thread");
+ params.delete("bot");
+ const search = params.toString();
+ window.history.replaceState(null, "", location.pathname + (search ? `?${search}` : "") + location.hash);
+ return THREAD_URL_PREFIX + encodeURIComponent(thread) + (bot === null ? "" : "?bot=" + encodeURIComponent(bot));
+}
+
export function parseThreadRefUrl(value: string): ThreadRefAddress | null {
const raw = value.trim();
if (!raw.toLowerCase().startsWith(THREAD_URL_PREFIX)) return null;