Skip to content

Open source alternative to Semrush and Ahrefs

Gate Sam behind beta opt-in and refresh legal pages (#634)

Ben Senescue432afcb2184
diff --git a/src/client/features/sam/SamBetaGate.tsx b/src/client/features/sam/SamBetaGate.tsx
new file mode 100644
index 00000000..e4d3f059
--- /dev/null
+++ b/src/client/features/sam/SamBetaGate.tsx
@@ -0,0 +1,38 @@
+import { Link } from "@tanstack/react-router";
+import { Sparkles } from "lucide-react";
+
+/**
+ * Shown on the chat route until the user opts into Sam. Sam is the OpenSEO
+ * MCP plus skills wrapped in an in-app chat; the agents people already use
+ * run that same toolset with a more mature harness, so the primary action
+ * points there and Sam is the explicit fallback.
+ */
+export function SamBetaGate({ onContinue }: { onContinue: () => void }) {
+ return (
+ <div className="flex h-full items-center justify-center overflow-auto px-4 py-8 md:px-6">
+ <div className="w-full max-w-lg rounded-2xl border border-base-300 bg-base-100 p-6 md:p-8">
+ <div className="flex size-10 items-center justify-center rounded-full bg-primary/10 text-primary">
+ <Sparkles className="size-5" />
+ </div>
+ <h2 className="mt-4 text-xl font-semibold">Sam is in beta</h2>
+ <div className="mt-3 space-y-3 text-sm leading-relaxed text-base-content/70">
+ <p>
+ Sam is the OpenSEO MCP and skills wrapped in a chat window. The
+ agent you already use, like Claude Code, ChatGPT, Grok Bot, or
+ Hermes, runs that same toolset on a much more capable harness. We
+ recommend using OpenSEO there.
+ </p>
+ <p>You can still use Sam, but it is early and has rough edges.</p>
+ </div>
+ <div className="mt-6 flex flex-wrap items-center gap-3">
+ <Link to="/ai" className="btn btn-primary">
+ Set up your agent
+ </Link>
+ <button type="button" className="btn btn-ghost" onClick={onContinue}>
+ Use Sam anyway
+ </button>
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/src/client/features/sam/SamChat.tsx b/src/client/features/sam/SamChat.tsx
index 94519b94..f9b05ceb 100644
--- a/src/client/features/sam/SamChat.tsx
+++ b/src/client/features/sam/SamChat.tsx
@@ -1,21 +1,23 @@
import { useMutation, useQuery } from "@tanstack/react-query";
import { Link, useNavigate } from "@tanstack/react-router";
-import { Suspense, useCallback, useEffect } from "react";
-import { Brain, Loader2, Plus, Wrench } from "lucide-react";
+import { Suspense, useCallback, useEffect, useRef } from "react";
+import { Brain, Loader2 } from "lucide-react";
import { createSamSession } from "@/serverFunctions/sam";
import {
invalidateSamSessions,
samSessionsQueryOptions,
} from "@/client/features/sam/samQueries";
import { useSamAccess } from "./useSamAccess";
+import { optInToSamBeta, useSamBetaOptIn } from "./samBetaOptIn";
+import { SamBetaGate } from "./SamBetaGate";
import { SamSetupGate } from "./SamSetupGate";
import { SamConversation } from "./SamConversation";
/**
* The SAM route's content: the active conversation, full-width. The chat
* history list lives in the app sidebar's Chat tab (SamSidebarPanel); this
- * component only auto-selects the most recent session on landing and shows the
- * start-a-chat empty state when the project has none.
+ * component gates on the beta opt-in, then lands the user in the most recent
+ * session, creating one when the project has none.
*/
export function SamChat({
projectId,
@@ -25,6 +27,7 @@ export function SamChat({
activeSessionId: string | undefined;
}) {
const navigate = useNavigate();
+ const optedIn = useSamBetaOptIn();
const access = useSamAccess(projectId);
const sessionsQuery = useQuery(samSessionsQueryOptions(projectId));
const sessions = sessionsQuery.data ?? [];
@@ -40,21 +43,47 @@ export function SamChat({
[navigate, projectId],
);
+ // The ref (not isPending) guards the auto-create below: React can re-run the
+ // effect before the mutation state updates, and it resets on settle so
+ // archiving the last chat starts a fresh one.
+ const creating = useRef(false);
const createSession = useMutation({
mutationFn: () => createSamSession({ data: { projectId } }),
onSuccess: ({ id }) => {
invalidateSamSessions(projectId);
goToSession(id);
},
+ onSettled: () => {
+ creating.current = false;
+ },
});
- // Default to the most recent session once they load; if none exist, leave the
- // empty state so the user can start one explicitly.
+ // Landing without a session: open the most recent one, or start a fresh
+ // chat when the project has none.
const firstSessionId = sessions[0]?.id;
+ const { mutate: createSessionMutate } = createSession;
useEffect(() => {
- if (activeSessionId || !firstSessionId) return;
- goToSession(firstSessionId);
- }, [activeSessionId, firstSessionId, goToSession]);
+ if (activeSessionId || !optedIn || access.showSetupGate) return;
+ if (firstSessionId) {
+ goToSession(firstSessionId);
+ return;
+ }
+ if (!sessionsQuery.isSuccess || creating.current) return;
+ creating.current = true;
+ createSessionMutate();
+ }, [
+ activeSessionId,
+ optedIn,
+ access.showSetupGate,
+ firstSessionId,
+ sessionsQuery.isSuccess,
+ goToSession,
+ createSessionMutate,
+ ]);
+
+ if (!optedIn) {
+ return <SamBetaGate onContinue={optInToSamBeta} />;
+ }
// SAM cannot answer a turn without OPENROUTER_API_KEY, so surface setup
// instructions instead of letting a chat fail mid-stream. Only shown once the
@@ -74,53 +103,9 @@ export function SamChat({
);
}
- if (activeSessionId) {
- const activeTitle = sessions.find(
- (session) => session.id === activeSessionId,
- )?.title;
- return (
- <div className="flex h-full min-h-0 flex-col">
- {/* Session title + the shortest path to inspect or correct the shared
- memory SAM reads and writes during the conversation. */}
- <div className="flex items-center justify-between gap-3 border-b border-base-300 px-5 py-3.5">
- <span className="truncate text-sm font-medium text-base-content/80">
- {activeTitle ?? "Chat"}
- </span>
- <Link
- to="/p/$projectId/context"
- params={{ projectId }}
- className="flex shrink-0 items-center gap-1.5 text-xs text-base-content/60 transition-colors hover:text-base-content"
- >
- <Brain className="size-3.5" />
- Project memory
- </Link>
- </div>
- <div className="flex min-h-0 flex-1">
- {/* useAgentChat suspends while it fetches the session's history; this
- boundary keeps that suspension inside the chat panel instead of
- letting it bubble up and swap out the whole shell — which read as
- a full page refresh on every session switch. */}
- <Suspense
- fallback={
- <div className="flex flex-1 items-center justify-center">
- <Loader2 className="size-5 animate-spin text-base-content/40" />
- </div>
- }
- >
- <SamConversation
- key={activeSessionId}
- projectId={projectId}
- sessionId={activeSessionId}
- />
- </Suspense>
- </div>
- </div>
- );
- }
-
- if (sessionsQuery.isLoading) {
- // Sessions are still loading; the auto-select effect will redirect into
- // the most recent one. Show a loader instead of flashing the empty state.
+ if (!activeSessionId) {
+ // Sessions are loading or a fresh chat is being created; the effect above
+ // redirects into it.
return (
<div className="flex h-full items-center justify-center">
<Loader2 className="size-5 animate-spin text-base-content/40" />
@@ -128,31 +113,45 @@ export function SamChat({
);
}
+ const activeTitle = sessions.find(
+ (session) => session.id === activeSessionId,
+ )?.title;
return (
- <div className="flex h-full flex-col items-center justify-center gap-4 p-6 text-center">
- <div className="flex size-12 items-center justify-center rounded-full bg-primary/10 text-primary">
- <Wrench className="size-6" />
+ <div className="flex h-full min-h-0 flex-col">
+ {/* Session title + the shortest path to inspect or correct the shared
+ memory SAM reads and writes during the conversation. */}
+ <div className="flex items-center justify-between gap-3 border-b border-base-300 px-5 py-3.5">
+ <span className="truncate text-sm font-medium text-base-content/80">
+ {activeTitle ?? "Chat"}
+ </span>
+ <Link
+ to="/p/$projectId/context"
+ params={{ projectId }}
+ className="flex shrink-0 items-center gap-1.5 text-xs text-base-content/60 transition-colors hover:text-base-content"
+ >
+ <Brain className="size-3.5" />
+ Project memory
+ </Link>
</div>
- <div className="space-y-1">
- <p className="text-lg font-medium">What should we work on?</p>
- <p className="max-w-sm text-sm text-base-content/60">
- SAM is your in-app SEO agent with access to every OpenSEO research
- tool. Start a chat to get going.
- </p>
+ <div className="flex min-h-0 flex-1">
+ {/* useAgentChat suspends while it fetches the session's history; this
+ boundary keeps that suspension inside the chat panel instead of
+ letting it bubble up and swap out the whole shell — which read as
+ a full page refresh on every session switch. */}
+ <Suspense
+ fallback={
+ <div className="flex flex-1 items-center justify-center">
+ <Loader2 className="size-5 animate-spin text-base-content/40" />
+ </div>
+ }
+ >
+ <SamConversation
+ key={activeSessionId}
+ projectId={projectId}
+ sessionId={activeSessionId}
+ />
+ </Suspense>
</div>
- <button
- type="button"
- className="btn btn-primary btn-sm gap-1"
- disabled={createSession.isPending}
- onClick={() => createSession.mutate()}
- >
- {createSession.isPending ? (
- <Loader2 className="size-4 animate-spin" />
- ) : (
- <Plus className="size-4" />
- )}
- New chat
- </button>
</div>
);
}
diff --git a/src/client/features/sam/SamSidebarPanel.tsx b/src/client/features/sam/SamSidebarPanel.tsx
index 37d3e863..29718ac4 100644
--- a/src/client/features/sam/SamSidebarPanel.tsx
+++ b/src/client/features/sam/SamSidebarPanel.tsx
@@ -7,6 +7,7 @@ import {
invalidateSamSessions,
samSessionsQueryOptions,
} from "@/client/features/sam/samQueries";
+import { useSamBetaOptIn } from "./samBetaOptIn";
const BETA_NOTICE_DISMISSED_KEY = "sam-beta-notice-dismissed";
@@ -76,6 +77,7 @@ export function SamSidebarPanel({
const navigate = useNavigate();
const location = useLocation();
const activeSessionId = (location.search as { s?: string }).s;
+ const optedIn = useSamBetaOptIn();
const sessionsQuery = useQuery(samSessionsQueryOptions(projectId));
const sessions = sessionsQuery.data ?? [];
@@ -108,6 +110,16 @@ export function SamSidebarPanel({
},
});
+ // Until the user opts in, the chat route shows SamBetaGate; the tab just
+ // points there instead of offering a chat list that can't be used yet.
+ if (!optedIn) {
+ return (
+ <p className="px-4 py-6 text-center text-xs text-base-content/50">
+ Sam is in beta and opt-in. Open Chat to read more and decide.
+ </p>
+ );
+ }
+
return (
<div className="flex min-h-0 flex-1 flex-col">
<div className="px-2 pb-1">
diff --git a/src/client/features/sam/samBetaOptIn.ts b/src/client/features/sam/samBetaOptIn.ts
new file mode 100644
index 00000000..1d142965
--- /dev/null
+++ b/src/client/features/sam/samBetaOptIn.ts
@@ -0,0 +1,28 @@
+import { useSyncExternalStore } from "react";
+import { captureClientEvent } from "@/client/lib/posthog";
+
+// Sam is opt-in while in beta: the chat route shows SamBetaGate until the user
+// explicitly chooses to continue. Per browser on purpose — this is a temporary
+// nudge toward the MCP path, not an account setting worth a schema change.
+const STORAGE_KEY = "sam-beta-opt-in";
+
+// A tiny external store so the chat panel and the sidebar's Chat tab flip
+// together the moment the user opts in.
+const listeners = new Set<() => void>();
+const subscribe = (listener: () => void) => {
+ listeners.add(listener);
+ return () => listeners.delete(listener);
+};
+const read = () => localStorage.getItem(STORAGE_KEY) === "1";
+
+export function optInToSamBeta() {
+ localStorage.setItem(STORAGE_KEY, "1");
+ captureClientEvent("sam:beta_opt_in");
+ listeners.forEach((listener) => listener());
+}
+
+// The app tree is client-only (see root ClientOnly), so the server snapshot is
+// never rendered; it only has to exist.
+export function useSamBetaOptIn(): boolean {
+ return useSyncExternalStore(subscribe, read, () => false);
+}