Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(onboarding): skip starter org for AI Gateway invitees, land them on the overview (#19647)

Tobias Wochinger59a5cc5adb5a
diff --git a/web/src/__tests__/server/onboarding-router.servertest.ts b/web/src/__tests__/server/onboarding-router.servertest.ts
index fee3b39605..2a3917972b 100644
--- a/web/src/__tests__/server/onboarding-router.servertest.ts
+++ b/web/src/__tests__/server/onboarding-router.servertest.ts
@@ -443,6 +443,118 @@ describe("onboarding router", () => {
});
});
+ describe("AI Gateway invitations", () => {
+ const originalGatewayAvailability = {
+ NODE_ENV: env.NODE_ENV,
+ NEXT_PUBLIC_PREVIEW_PR_URL: env.NEXT_PUBLIC_PREVIEW_PR_URL,
+ LANGFUSE_AI_GATEWAY_DEPLOYED: env.LANGFUSE_AI_GATEWAY_DEPLOYED,
+ LANGFUSE_AI_GATEWAY_ENABLED: env.LANGFUSE_AI_GATEWAY_ENABLED,
+ LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST:
+ env.LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST,
+ };
+
+ afterEach(() => {
+ Object.assign(env, originalGatewayAvailability);
+ });
+
+ it("lands invitees on the overview instead of creating starter resources", async () => {
+ const { organization, memberships, complete } =
+ await signUpWithGatewayInvitation({ gatewayEnabled: true });
+
+ expect(memberships).toEqual([
+ { orgId: organization.id, role: Role.AI_GATEWAY },
+ ]);
+ expect(await complete()).toEqual({
+ redirectTo: "/",
+ surveyCreated: true,
+ });
+ });
+
+ it("creates starter resources when the gateway was turned off for the inviting org", async () => {
+ const { organization, memberships } = await signUpWithGatewayInvitation({
+ gatewayEnabled: false,
+ });
+
+ expect(memberships).toHaveLength(2);
+ expect(memberships).toContainEqual({
+ orgId: organization.id,
+ role: Role.AI_GATEWAY,
+ });
+ expect(memberships).toContainEqual(
+ expect.objectContaining({ role: Role.OWNER }),
+ );
+ });
+
+ /** signUpWithGatewayInvitation signs up a user holding an AI_GATEWAY invitation for a fresh org */
+ async function signUpWithGatewayInvitation({
+ gatewayEnabled,
+ }: {
+ gatewayEnabled: boolean;
+ }) {
+ const userId = randomUUID();
+ const email = `invited-gateway-${userId}@example.com`;
+ createdUserIds.push(userId);
+
+ const user = await prisma.user.create({
+ data: { id: userId, email, name: "Gale Test" },
+ });
+ const organization = await prisma.organization.create({
+ data: { name: `Gateway Org ${userId}` },
+ });
+ createdOrgIds.push(organization.id);
+ await prisma.project.create({
+ data: { name: `Gateway Org Project ${userId}`, orgId: organization.id },
+ });
+ await prisma.membershipInvitation.create({
+ data: { orgId: organization.id, email, orgRole: Role.AI_GATEWAY },
+ });
+
+ // Development and preview enable the gateway everywhere, so only the
+ // org allowlist decides here.
+ Object.assign(env, {
+ NODE_ENV: "test",
+ NEXT_PUBLIC_PREVIEW_PR_URL: undefined,
+ LANGFUSE_AI_GATEWAY_DEPLOYED: "true",
+ LANGFUSE_AI_GATEWAY_ENABLED: "false",
+ LANGFUSE_AI_GATEWAY_ORGANIZATION_ID_ALLOWLIST: gatewayEnabled
+ ? [organization.id]
+ : [],
+ });
+ await createProjectMembershipsOnSignup(user, {
+ userWasJustCreated: true,
+ });
+
+ const memberships = await prisma.organizationMembership.findMany({
+ where: {
+ userId,
+ ...(env.NEXT_PUBLIC_DEMO_ORG_ID
+ ? { orgId: { not: env.NEXT_PUBLIC_DEMO_ORG_ID } }
+ : {}),
+ },
+ select: { orgId: true, role: true },
+ });
+ createdOrgIds.push(
+ ...memberships
+ .map((membership) => membership.orgId)
+ .filter((orgId) => orgId !== organization.id),
+ );
+
+ const caller = appRouter.createCaller({
+ ...createInnerTRPCContext({
+ session: makeSession({ userId, email, name: "Gale Test" }),
+ headers: {},
+ }),
+ prisma,
+ });
+
+ return {
+ organization,
+ memberships,
+ complete: () => caller.onboarding.complete(),
+ };
+ }
+ });
+
it("falls back to manual project creation when a real org exists without a project", async () => {
const userId = randomUUID();
const email = `org-only-${userId}@example.com`;
diff --git a/web/src/__tests__/server/unit/onboardingService.servertest.ts b/web/src/__tests__/server/unit/onboardingService.servertest.ts
index 04a21e3981..4bddd875b4 100644
--- a/web/src/__tests__/server/unit/onboardingService.servertest.ts
+++ b/web/src/__tests__/server/unit/onboardingService.servertest.ts
@@ -142,6 +142,25 @@ describe("resolveOnboardingRedirectTarget", () => {
redirectTo: "/project/project-1",
});
});
+
+ it("lands AI Gateway members on the overview when no org has a usable project", async () => {
+ const result = await resolveOnboardingRedirectTarget({
+ prisma: makePrisma([
+ makeMembership({ orgId: "none-org", role: Role.NONE, projects: [] }),
+ makeMembership({
+ orgId: "gateway-org",
+ role: Role.AI_GATEWAY,
+ projects: [{ id: "project-1" }],
+ }),
+ ]),
+ userId: "user-1",
+ });
+
+ expect(result).toEqual({
+ redirectTo: "/",
+ orgId: "gateway-org",
+ });
+ });
});
describe("getSignupOnboardingStatus", () => {
diff --git a/web/src/__tests__/server/unit/redirectGatewayOnlyLanding.servertest.ts b/web/src/__tests__/server/unit/redirectGatewayOnlyLanding.servertest.ts
deleted file mode 100644
index 531c6cff55..0000000000
--- a/web/src/__tests__/server/unit/redirectGatewayOnlyLanding.servertest.ts
+++ /dev/null
@@ -1,87 +0,0 @@
-import { type GetServerSidePropsContext } from "next";
-import { type Session } from "next-auth";
-
-import { redirectGatewayOnlyLanding } from "@/src/features/organizations/server/redirectGatewayOnlyLanding";
-import { getServerAuthSession } from "@/src/server/auth";
-
-vi.mock("@/src/server/auth", () => ({ getServerAuthSession: vi.fn() }));
-
-describe("redirectGatewayOnlyLanding", () => {
- it("sends gateway-only members to their organization settings", async () => {
- mockSession([gatewayOrg({ aiGateway: true })]);
-
- expect(await landOn({ organizationId: "gateway-org" })).toEqual({
- redirect: {
- destination: "/organization/gateway-org/settings",
- permanent: false,
- },
- });
- });
-
- it("sends members whose only org is gateway-only to its settings", async () => {
- mockSession([gatewayOrg({ aiGateway: true })]);
-
- expect(await landOn({})).toEqual({
- redirect: {
- destination: "/organization/gateway-org/settings",
- permanent: false,
- },
- });
- });
-
- it("keeps gateway-only members on the page when the AI Gateway is off for their org", async () => {
- mockSession([gatewayOrg({ aiGateway: false })]);
-
- expect(await landOn({ organizationId: "gateway-org" })).toEqual({
- props: {},
- });
- });
-
- it("keeps members who can read the org settings on the page", async () => {
- mockSession([{ ...gatewayOrg({ aiGateway: true }), role: "MEMBER" }]);
-
- expect(await landOn({ organizationId: "gateway-org" })).toEqual({
- props: {},
- });
- });
-
- it("keeps users with several orgs on the overview", async () => {
- mockSession([
- gatewayOrg({ aiGateway: true }),
- { ...gatewayOrg({ aiGateway: true }), id: "other-org" },
- ]);
-
- expect(await landOn({})).toEqual({ props: {} });
- });
-});
-
-/** landOn runs the redirect for a request with the given query */
-function landOn(query: Record<string, string>) {
- return redirectGatewayOnlyLanding({
- req: {},
- res: {},
- query,
- } as unknown as GetServerSidePropsContext);
-}
-
-/** mockSession signs in a user who belongs to the given orgs */
-function mockSession(organizations: Organization[]) {
- vi.mocked(getServerAuthSession).mockResolvedValue({
- expires: "",
- user: { id: "user-1", admin: false, organizations },
- } as unknown as Session);
-}
-
-/** gatewayOrg is an org where the user holds the AI_GATEWAY role and sees no projects */
-function gatewayOrg(featureFlags: { aiGateway: boolean }) {
- return {
- id: "gateway-org",
- name: "Gateway Org",
- role: "AI_GATEWAY",
- plan: "oss",
- projects: [],
- featureFlags,
- } as unknown as Organization;
-}
-
-type Organization = NonNullable<Session["user"]>["organizations"][number];
diff --git a/web/src/features/auth/lib/createProjectMembershipsOnSignup.ts b/web/src/features/auth/lib/createProjectMembershipsOnSignup.ts
index 1655dc239b..99afdf86d9 100644
--- a/web/src/features/auth/lib/createProjectMembershipsOnSignup.ts
+++ b/web/src/features/auth/lib/createProjectMembershipsOnSignup.ts
@@ -8,6 +8,7 @@ import { shouldAutoEnableV4 } from "@/src/features/events/lib/v4Rollout";
import { getSfdcService } from "@/src/ee/features/sfdc-sync/server/sfdcService";
import { canCreateOrganizations } from "@/src/features/organizations/server/canCreateOrganizations";
import { provisionStarterOrganizationForNewUser } from "@/src/features/onboarding/server/onboardingService";
+import { isGatewayEnabledForOrganization } from "@/src/features/ai-gateway/server/availability";
import { projectRoleAccessRights } from "@langfuse/shared";
import { type AdClickIds } from "@/src/features/auth/lib/signupAttribution";
@@ -326,14 +327,19 @@ async function processMembershipInvitations(email: string, userId: string) {
});
if (invitationsForUser.length === 0) return false;
- const joinedReadableRealProjectViaInvitation = invitationsForUser.some(
+ const joinedRealOrganizationViaInvitation = invitationsForUser.some(
(invitation) => {
+ if (invitation.orgId === env.NEXT_PUBLIC_DEMO_ORG_ID) return false;
+ // Gateway-only members see no projects but belong in the inviting org,
+ // where they create their AI Gateway keys, as long as it still has the
+ // gateway enabled.
if (
- invitation.orgId === env.NEXT_PUBLIC_DEMO_ORG_ID ||
- !invitation.projectId
+ invitation.orgRole === Role.AI_GATEWAY &&
+ isGatewayEnabledForOrganization(invitation.orgId)
) {
- return false;
+ return true;
}
+ if (!invitation.projectId) return false;
const projectRole = invitation.projectRole ?? invitation.orgRole;
return projectRoleAccessRights[projectRole].includes("project:read");
@@ -386,5 +392,5 @@ async function processMembershipInvitations(email: string, userId: string) {
),
);
- return joinedReadableRealProjectViaInvitation;
+ return joinedRealOrganizationViaInvitation;
}
diff --git a/web/src/features/onboarding/server/onboardingService.ts b/web/src/features/onboarding/server/onboardingService.ts
index 165d095f0f..170a83782c 100644
--- a/web/src/features/onboarding/server/onboardingService.ts
+++ b/web/src/features/onboarding/server/onboardingService.ts
@@ -213,6 +213,18 @@ const getRedirectTargetForMemberships = (
};
}
+ // Gateway-only members land on the overview, which shows their gateway setup.
+ const gatewayOrganization = organizationMemberships.find(
+ (membership) => membership.role === Role.AI_GATEWAY,
+ );
+
+ if (gatewayOrganization) {
+ return {
+ redirectTo: "/",
+ orgId: gatewayOrganization.organization.id,
+ };
+ }
+
const firstOrganization = organizationMemberships[0];
if (firstOrganization) {
diff --git a/web/src/features/organizations/server/redirectGatewayOnlyLanding.ts b/web/src/features/organizations/server/redirectGatewayOnlyLanding.ts
deleted file mode 100644
index d570e6722d..0000000000
--- a/web/src/features/organizations/server/redirectGatewayOnlyLanding.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-import { type GetServerSideProps } from "next";
-
-import { gatewayOnlyLanding } from "@/src/features/organizations/utils/gatewayOnlyLanding";
-import { getServerAuthSession } from "@/src/server/auth";
-
-/** redirectGatewayOnlyLanding sends members who can only use the AI Gateway to their org settings */
-export const redirectGatewayOnlyLanding: GetServerSideProps = async (ctx) => {
- const session = await getServerAuthSession({ req: ctx.req, res: ctx.res });
- const landing = gatewayOnlyLanding(session, ctx.query.organizationId);
- if (!landing?.canUseGateway) return { props: {} };
- return {
- redirect: {
- destination: `/organization/${encodeURIComponent(landing.organizationId)}/settings`,
- permanent: false,
- },
- };
-};
diff --git a/web/src/pages/index.tsx b/web/src/pages/index.tsx
index a6e7ac7cf8..10e69c71f5 100644
--- a/web/src/pages/index.tsx
+++ b/web/src/pages/index.tsx
@@ -1,7 +1,5 @@
import { OrganizationProjectOverview } from "@/src/features/organizations/components/OrganizationProjectOverview";
-export { redirectGatewayOnlyLanding as getServerSideProps } from "@/src/features/organizations/server/redirectGatewayOnlyLanding";
-
export default function Home() {
return <OrganizationProjectOverview />;
}
diff --git a/web/src/pages/organization/[organizationId]/index.tsx b/web/src/pages/organization/[organizationId]/index.tsx
index 3b29cd4706..cf6cb506ad 100644
--- a/web/src/pages/organization/[organizationId]/index.tsx
+++ b/web/src/pages/organization/[organizationId]/index.tsx
@@ -1,2 +1 @@
export { OrganizationProjectOverview as default } from "@/src/features/organizations/components/OrganizationProjectOverview";
-export { redirectGatewayOnlyLanding as getServerSideProps } from "@/src/features/organizations/server/redirectGatewayOnlyLanding";