Skip to content

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

feat(ai-gateway): gate gateway settings until an ingestion project is set (#19073)

Tobias Wochinger538e23c0df18
diff --git a/web/src/__tests__/organization-settings-pages.clienttest.tsx b/web/src/__tests__/organization-settings-pages.clienttest.tsx
index 719b5cc909..89fc9134db 100644
--- a/web/src/__tests__/organization-settings-pages.clienttest.tsx
+++ b/web/src/__tests__/organization-settings-pages.clienttest.tsx
@@ -106,6 +106,11 @@ vi.mock("@/src/features/feature-flags/hooks/useIsFeatureEnabled", () => ({
default: vi.fn(),
}));
+const getSetupStatusQuery = vi.hoisted(() => vi.fn());
+vi.mock("@/src/utils/api", () => ({
+ api: { aiGateway: { getSetupStatus: { useQuery: getSetupStatusQuery } } },
+}));
+
vi.mock("@/src/features/ai-gateway", () => ({
GatewayApiKeysPage: () => null,
GatewayConfigurationPage: () => null,
@@ -134,6 +139,10 @@ describe("useOrganizationSettingsPages", () => {
vi.mocked(useIsCloudBillingAvailable).mockReturnValue(false);
vi.mocked(useV4UpgradeUiFlag).mockReturnValue(false);
vi.mocked(useIsFeatureEnabled).mockReturnValue(false);
+ getSetupStatusQuery.mockReturnValue({
+ data: { isConfigured: true },
+ isPending: false,
+ });
});
it("hides organization API key settings without organization api key access", () => {
@@ -265,6 +274,50 @@ describe("useOrganizationSettingsPages", () => {
"ai-gateway-api-keys",
]);
expect(gatewayPages.every((page) => page.show === true)).toBe(true);
+ expect(gatewayPages.every((page) => !page.disabledReason)).toBe(true);
+ });
+
+ it("disables every AI Gateway page except Configuration for managers until ingestion is set up", () => {
+ vi.mocked(useHasOrganizationAccess).mockImplementation(
+ ({ scope }) => scope === "gateway:manage" || scope === "gateway:read",
+ );
+ vi.mocked(useIsFeatureEnabled).mockReturnValue(true);
+ getSetupStatusQuery.mockReturnValue({
+ data: { isConfigured: false },
+ isPending: false,
+ });
+
+ const { result } = renderHook(() => useOrganizationSettingsPages());
+
+ expect(
+ result.current
+ .filter((page) => page.slug.startsWith("ai-gateway"))
+ .map((page) => [page.slug, !!page.disabledReason]),
+ ).toEqual([
+ ["ai-gateway", false],
+ ["ai-gateway-providers", true],
+ ["ai-gateway-models", true],
+ ["ai-gateway-api-keys", true],
+ ]);
+ });
+
+ it("keeps Models enabled for readers when ingestion is not set up", () => {
+ vi.mocked(useHasOrganizationAccess).mockImplementation(
+ ({ scope }) => scope === "gateway:read",
+ );
+ vi.mocked(useIsFeatureEnabled).mockReturnValue(true);
+ getSetupStatusQuery.mockReturnValue({
+ data: { isConfigured: false },
+ isPending: false,
+ });
+
+ const { result } = renderHook(() => useOrganizationSettingsPages());
+ const models = result.current.find(
+ (page) => page.slug === "ai-gateway-models",
+ );
+
+ expect(models?.show).toBe(true);
+ expect(models?.disabledReason).toBeUndefined();
});
it("shows the usage page instead of the billing page without cloud billing", () => {
diff --git a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
index 947ca90b54..2cc2202266 100644
--- a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
+++ b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
@@ -29,6 +29,7 @@ import {
import { GatewayModelsService } from "@/src/features/ai-gateway/server/provider/models/gatewayModelsService";
import { GatewayApiKeyService } from "@/src/features/ai-gateway/server/apiKey/gatewayApiKeyService";
import { GatewayConfigService } from "@/src/features/ai-gateway/server/config/gatewayConfigService";
+import { getGatewaySetupRedirect } from "@/src/features/ai-gateway/server/gatewaySetupRedirect";
import {
GatewayModelCatalogService,
GatewayProviderService,
@@ -309,6 +310,103 @@ describe("AI gateway control plane", () => {
},
);
+ it("reports the ingestion setup status to gateway readers", async () => {
+ const owner = await prepare();
+ const viewer = await createCallerForOrganizationRole(owner, Role.VIEWER);
+ const noAccess = await createCallerForOrganizationRole(owner, Role.NONE);
+
+ await expect(
+ viewer.aiGateway.getSetupStatus({ orgId: owner.org.id }),
+ ).resolves.toEqual({ isConfigured: false });
+ await expect(
+ noAccess.aiGateway.getSetupStatus({ orgId: owner.org.id }),
+ ).rejects.toMatchObject({ code: "FORBIDDEN" });
+
+ await owner.caller.aiGateway.updateConfig({
+ orgId: owner.org.id,
+ defaultIngestionProjectId: owner.project.id,
+ ingestionMode: "USAGE",
+ });
+ await expect(
+ viewer.aiGateway.getSetupStatus({ orgId: owner.org.id }),
+ ).resolves.toEqual({ isConfigured: true });
+
+ await prisma.project.update({
+ where: { id: owner.project.id },
+ data: { deletedAt: new Date() },
+ });
+ await expect(
+ viewer.aiGateway.getSetupStatus({ orgId: owner.org.id }),
+ ).resolves.toEqual({ isConfigured: false });
+ });
+
+ it("redirects gateway managers to Configuration until ingestion is set up", async () => {
+ const owner = await prepare();
+ const memberSession = {
+ ...owner.session,
+ user: {
+ ...owner.session.user,
+ organizations: owner.session.user.organizations.map((org) => ({
+ ...org,
+ role: Role.MEMBER,
+ })),
+ },
+ };
+ const redirectFor = (session: Session, slug: string) =>
+ getGatewaySetupRedirect({
+ prisma,
+ session,
+ organizationId: owner.org.id,
+ slug,
+ });
+ const configurationPath = `/organization/${owner.org.id}/settings/ai-gateway`;
+
+ await expect(
+ redirectFor(owner.session, "ai-gateway-providers"),
+ ).resolves.toBe(configurationPath);
+ await expect(redirectFor(owner.session, "ai-gateway")).resolves.toBeNull();
+ await expect(
+ redirectFor(memberSession, "ai-gateway-models"),
+ ).resolves.toBeNull();
+
+ await owner.caller.aiGateway.updateConfig({
+ orgId: owner.org.id,
+ defaultIngestionProjectId: owner.project.id,
+ ingestionMode: "USAGE",
+ });
+ await expect(
+ redirectFor(owner.session, "ai-gateway-providers"),
+ ).resolves.toBeNull();
+ });
+
+ it("requires an ingestion project unless ingestion mode is NONE", async () => {
+ const { caller, org } = await prepare();
+ const originalCloudRegion = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;
+ try {
+ Object.assign(env, { NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: undefined });
+ await expect(
+ caller.aiGateway.updateConfig({
+ orgId: org.id,
+ defaultIngestionProjectId: null,
+ ingestionMode: "USAGE",
+ }),
+ ).rejects.toThrow("default ingestion project is required");
+
+ await caller.aiGateway.updateConfig({
+ orgId: org.id,
+ defaultIngestionProjectId: null,
+ ingestionMode: "NONE",
+ });
+ await expect(
+ caller.aiGateway.getSetupStatus({ orgId: org.id }),
+ ).resolves.toEqual({ isConfigured: true });
+ } finally {
+ Object.assign(env, {
+ NEXT_PUBLIC_LANGFUSE_CLOUD_REGION: originalCloudRegion,
+ });
+ }
+ });
+
it.each([
{ adminRole: Role.OWNER, readRole: Role.MEMBER },
{ adminRole: Role.ADMIN, readRole: Role.VIEWER },
diff --git a/web/src/components/PagedSettingsContainer.tsx b/web/src/components/PagedSettingsContainer.tsx
index 7a5708e9ea..396a7ce271 100644
--- a/web/src/components/PagedSettingsContainer.tsx
+++ b/web/src/components/PagedSettingsContainer.tsx
@@ -2,6 +2,7 @@ import { cn } from "@/src/utils/tailwind";
import { ArrowUpRight } from "@langfuse/icons";
import Link from "next/link";
import { type ReactNode } from "react";
+import { Tooltip } from "@/src/components/design-system/Tooltip/Tooltip";
import {
Select,
SelectContent,
@@ -18,6 +19,8 @@ type SettingsPage = {
slug: string;
section?: string;
show?: boolean | (() => boolean);
+ /** Lists the page but blocks opening it; shown as the entry's tooltip */
+ disabledReason?: string;
} & (
| { content: ReactNode }
| {
@@ -49,20 +52,23 @@ export const PagedSettingsContainer = ({
);
const contentPages = availablePages.filter((page) => "content" in page);
- const currentPage =
- contentPages.find((page) => page.slug === activeSlug) ?? contentPages[0];
+ const enabledContentPages = contentPages.filter(
+ (page) => !page.disabledReason,
+ );
+ const requestedPage = contentPages.find((page) => page.slug === activeSlug);
+ const currentPage = requestedPage?.disabledReason
+ ? (enabledContentPages.find(
+ (page) => page.section === requestedPage.section,
+ ) ?? enabledContentPages[0])
+ : (requestedPage ?? enabledContentPages[0]);
const onChange = (newSlug: string) => {
- const pathSegments = router.asPath.split("?")[0].split("/");
- if (pathSegments[pathSegments.length - 1] !== "settings")
- pathSegments.pop();
- if (newSlug !== "index") pathSegments.push(newSlug);
- router.push(pathSegments.join("/"));
+ router.push(settingsPath(router.asPath, newSlug));
};
const handleSelectPage = (key: string) => {
const page = availablePages.find((p) => pageKey(p) === key);
- if (!page) return;
+ if (!page || page.disabledReason) return;
if ("href" in page) router.push(page.href);
else onChange(page.slug);
};
@@ -104,7 +110,11 @@ export const PagedSettingsContainer = ({
<SelectLabel>{group.section}</SelectLabel>
) : null}
{group.pages.map((page) => (
- <SelectItem key={pageKey(page)} value={pageKey(page)}>
+ <SelectItem
+ key={pageKey(page)}
+ value={pageKey(page)}
+ disabled={!!page.disabledReason}
+ >
{page.title}
{"href" in page && !page.internal && (
<ArrowUpRight
@@ -142,28 +152,19 @@ export const PagedSettingsContainer = ({
))
: null}
{group.pages.map((page) =>
- "href" in page ? (
- <Link
+ page.disabledReason ? (
+ <DisabledNavEntry
key={pageKey(page)}
- href={page.href}
- className="hover:bg-muted hover:text-foreground flex h-8 flex-row items-center gap-2 rounded-sm px-2"
- >
- {page.title}
- {!page.internal && (
- <ArrowUpRight size="base" __rawClassName="inline" />
- )}
- </Link>
+ title={page.title}
+ reason={page.disabledReason}
+ />
) : (
- <span
+ <NavEntry
key={pageKey(page)}
- onClick={() => onChange(page.slug)}
- className={cn(
- "hover:bg-muted hover:text-foreground flex h-8 cursor-pointer items-center rounded-sm px-2",
- page === currentPage && "bg-muted text-primary font-bold",
- )}
- >
- {page.title}
- </span>
+ page={page}
+ isActive={page === currentPage}
+ onSelect={onChange}
+ />
),
)}
</div>
@@ -182,6 +183,74 @@ export const PagedSettingsContainer = ({
);
};
+function NavEntry({
+ page,
+ isActive,
+ onSelect,
+}: {
+ page: SettingsPage;
+ isActive: boolean;
+ onSelect: (slug: string) => void;
+}) {
+ if ("href" in page) {
+ return (
+ <Link
+ href={page.href}
+ className="hover:bg-muted hover:text-foreground flex h-8 flex-row items-center gap-2 rounded-sm px-2"
+ >
+ {page.title}
+ {!page.internal && <ArrowUpRight size="base" __rawClassName="inline" />}
+ </Link>
+ );
+ }
+ function handleSelect() {
+ onSelect(page.slug);
+ }
+
+ return (
+ <span
+ onClick={handleSelect}
+ className={cn(
+ "hover:bg-muted hover:text-foreground flex h-8 cursor-pointer items-center rounded-sm px-2",
+ isActive && "bg-muted text-primary font-bold",
+ )}
+ >
+ {page.title}
+ </span>
+ );
+}
+
+function DisabledNavEntry({
+ title,
+ reason,
+}: {
+ title: string;
+ reason: string;
+}) {
+ return (
+ <Tooltip label={reason} placement="right" hoverableContent={false}>
+ {({ getTriggerProps }) => (
+ <span
+ {...getTriggerProps()}
+ role="link"
+ aria-disabled="true"
+ tabIndex={0}
+ className="text-muted-foreground/50 flex h-8 w-fit cursor-not-allowed items-center rounded-sm px-2"
+ >
+ {title}
+ </span>
+ )}
+ </Tooltip>
+ );
+}
+
+function settingsPath(asPath: string, slug: string) {
+ const pathSegments = asPath.split("?")[0].split("/");
+ if (pathSegments[pathSegments.length - 1] !== "settings") pathSegments.pop();
+ if (slug !== "index") pathSegments.push(slug);
+ return pathSegments.join("/");
+}
+
function groupPagesBySection<T extends { section?: string }>(pages: T[]) {
return pages.reduce<Array<{ section: string | undefined; pages: T[] }>>(
(groups, page) => {
diff --git a/web/src/features/ai-gateway/components/GatewayConfigurationPage/GatewayConfigurationPage.tsx b/web/src/features/ai-gateway/components/GatewayConfigurationPage/GatewayConfigurationPage.tsx
index aeb244502e..854ed2d107 100644
--- a/web/src/features/ai-gateway/components/GatewayConfigurationPage/GatewayConfigurationPage.tsx
+++ b/web/src/features/ai-gateway/components/GatewayConfigurationPage/GatewayConfigurationPage.tsx
@@ -8,6 +8,7 @@ import {
GatewayConfigurationView,
type IngestionMode,
} from "@/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView";
+import { IngestionProjectSetupPlaceholder } from "@/src/features/ai-gateway/components/GatewayConfigurationPage/components/IngestionProjectSetupPlaceholder/IngestionProjectSetupPlaceholder";
import { showSuccessToast } from "@/src/features/notifications";
import { useLangfuseCloudRegion } from "@/src/features/organizations";
import { api, reportNonTrpcError } from "@/src/utils/api";
@@ -46,7 +47,10 @@ export function GatewayConfigurationPage({
defaultIngestionProjectId: params.projectId,
ingestionMode: params.ingestionMode,
});
- await utils.aiGateway.getConfig.invalidate({ orgId: organizationId });
+ await Promise.all([
+ utils.aiGateway.getConfig.invalidate({ orgId: organizationId }),
+ utils.aiGateway.getSetupStatus.invalidate({ orgId: organizationId }),
+ ]);
showSuccessToast({
operation: "gateway_configuration.save",
title: "Gateway configuration saved",
@@ -80,13 +84,61 @@ export function GatewayConfigurationPage({
}
const { config, gatewayBaseUrl } = configQuery.data;
+ const initialIngestionMode = config?.ingestionMode ?? "USAGE";
+
+ async function handleCreateProject(params: {
+ projectName: string;
+ ingestionMode: IngestionMode;
+ }) {
+ try {
+ const project = await createProject.mutateAsync({
+ orgId: organizationId,
+ name: params.projectName,
+ });
+ await session.update();
+ await saveGatewayConfig({
+ projectId: project.id,
+ ingestionMode: params.ingestionMode,
+ });
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-configuration");
+ }
+ }
+
+ async function handleSelectProject(projectId: string) {
+ try {
+ await saveGatewayConfig({
+ projectId,
+ ingestionMode: initialIngestionMode,
+ });
+ } catch (error) {
+ reportNonTrpcError(error, "ai-gateway-configuration");
+ }
+ }
+
+ /** Cloud always captures traffic, so it needs a project; self-hosted can use `NONE`. */
+ const showSetupPlaceholder =
+ isLangfuseCloud &&
+ !hasActiveIngestionProject(config?.defaultIngestionProject, organizationId);
+ if (showSetupPlaceholder) {
+ return (
+ <IngestionProjectSetupPlaceholder
+ ingestionMode={initialIngestionMode}
+ isSaving={createProject.isPending || updateConfig.isPending}
+ onCreate={handleCreateProject}
+ projects={projects.filter((project) => !project.deletedAt)}
+ onSelectProject={handleSelectProject}
+ />
+ );
+ }
+
return (
<GatewayConfigurationView
key={`${config?.updatedAt?.toISOString() ?? "new"}:${config?.defaultIngestionProjectId ?? "none"}:${config?.ingestionMode ?? "USAGE"}`}
projects={projects}
gatewayBaseUrl={gatewayBaseUrl}
initialProjectId={config?.defaultIngestionProjectId ?? null}
- initialIngestionMode={config?.ingestionMode ?? "USAGE"}
+ initialIngestionMode={initialIngestionMode}
availableIngestionModes={
isLangfuseCloud ? CLOUD_INGESTION_MODES : SELF_HOSTED_INGESTION_MODES
}
@@ -99,18 +151,7 @@ export function GatewayConfigurationPage({
reportNonTrpcError(error, "ai-gateway-configuration");
}
}}
- onCreateProject={async ({ projectName, ingestionMode }) => {
- try {
- const project = await createProject.mutateAsync({
- orgId: organizationId,
- name: projectName,
- });
- await session.update();
- await saveGatewayConfig({ projectId: project.id, ingestionMode });
- } catch (error) {
- reportNonTrpcError(error, "ai-gateway-configuration");
- }
- }}
+ onCreateProject={handleCreateProject}
/>
);
}
@@ -129,3 +170,10 @@ function ConfigurationSkeleton() {
</div>
);
}
+
+function hasActiveIngestionProject(
+ project: { orgId: string; deletedAt: Date | null } | null | undefined,
+ organizationId: string,
+) {
+ return !!project && !project.deletedAt && project.orgId === organizationId;
+}
diff --git a/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/CreateIngestionProjectDialogController/CreateIngestionProjectDialogController.tsx b/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/CreateIngestionProjectDialogController/CreateIngestionProjectDialogController.tsx
new file mode 100644
index 0000000000..39d7f95d5a
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/CreateIngestionProjectDialogController/CreateIngestionProjectDialogController.tsx
@@ -0,0 +1,112 @@
+import { useId, type ReactNode } from "react";
+import { zodResolver } from "@hookform/resolvers/zod";
+import { useForm } from "react-hook-form";
+import * as z from "zod";
+
+import { Dialog } from "@/src/components/design-system/Dialog/Dialog";
+import { DialogController } from "@/src/components/design-system/DialogController/DialogController";
+import { FormField } from "@/src/components/design-system/FormField/FormField";
+import { Input } from "@/src/components/design-system/Input/Input";
+import { DEFAULT_GATEWAY_INGESTION_PROJECT_NAME } from "@/src/features/ai-gateway/constants/gatewayConfig";
+import type { GatewayIngestionMode } from "@langfuse/shared";
+
+const createIngestionProjectSchema = z.object({
+ projectName: z.string().trim().min(1, "Enter a project name."),
+});
+
+export type CreateIngestionProjectDialogControllerProps = {
+ ingestionMode: GatewayIngestionMode;
+ isSaving: boolean;
+ onCreate: (values: {
+ projectName: string;
+ ingestionMode: GatewayIngestionMode;
+ }) => void | Promise<void>;
+ children: (control: { openDialog: () => void }) => ReactNode;
+};
+
+export function CreateIngestionProjectDialogController({
+ ingestionMode,
+ isSaving,
+ onCreate,
+ children,
+}: CreateIngestionProjectDialogControllerProps) {
+ function handleBeforeClose() {
+ return !isSaving;
+ }
+
+ return (
+ <DialogController
+ onBeforeClose={handleBeforeClose}
+ renderDialog={() => (
+ <CreateIngestionProjectDialog
+ ingestionMode={ingestionMode}
+ isSaving={isSaving}
+ onCreate={onCreate}
+ />
+ )}
+ >
+ {children}
+ </DialogController>
+ );
+}
+
+function CreateIngestionProjectDialog({
+ ingestionMode,
+ isSaving,
+ onCreate,
+}: Omit<CreateIngestionProjectDialogControllerProps, "children">) {
+ const formId = useId();
+ const form = useForm<CreateIngestionProjectValues>({
+ resolver: zodResolver(createIngestionProjectSchema),
+ defaultValues: { projectName: DEFAULT_GATEWAY_INGESTION_PROJECT_NAME },
+ });
+
+ function handleSubmit({ projectName }: CreateIngestionProjectValues) {
+ return onCreate({ projectName, ingestionMode });
+ }
+
+ return (
+ <Dialog
+ title="Create ingestion project"
+ actions={[
+ {
+ label: "Create project",
+ type: "submit",
+ form: formId,
+ loading: isSaving,
+ disabled: isSaving,
+ },
+ ]}
+ >
+ <form id={formId} onSubmit={form.handleSubmit(handleSubmit)}>
+ <Dialog.Body>
+ <FormField
+ control={form.control}
+ name="projectName"
+ label="Project name"
+ description="Project access follows organization roles and can be restricted in project settings."
+ >
+ {({ error, id, inputDescribedById, ...field }) => (
+ <div className="ph-no-capture">
+ <Input
+ id={id}
+ name={field.name}
+ value={field.value}
+ onChange={field.onChange}
+ onBlur={field.onBlur}
+ ref={field.ref}
+ error={error !== undefined}
+ aria-describedby={inputDescribedById}
+ />
+ </div>
+ )}
+ </FormField>
+ </Dialog.Body>
+ </form>
+ </Dialog>
+ );
+}
+
+type CreateIngestionProjectValues = z.infer<
+ typeof createIngestionProjectSchema
+>;
diff --git a/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView.tsx b/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView.tsx
index c7c3d528fe..e96949c51f 100644
--- a/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView.tsx
+++ b/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/GatewayConfigurationView/GatewayConfigurationView.tsx
@@ -12,15 +12,6 @@ import {
import Header from "@/src/components/layouts/header";
import { Alert } from "@/src/components/design-system/Alert/Alert";
import { Button } from "@/src/components/ui/button";
-import {
- DialogBody,
- DialogController,
- DialogFooter,
- DialogHeader,
- DialogTitle,
-} from "@/src/components/ui/dialog";
-import { Input } from "@/src/components/ui/input";
-import { Label } from "@/src/components/ui/label";
import { CardDescription, CardTitle } from "@/src/components/ui/card";
import {
Select,
@@ -29,7 +20,13 @@ import {
SelectTrigger,
SelectValue,
} from "@/src/components/ui/select";
-import { DEFAULT_GATEWAY_INGESTION_PROJECT_NAME } from "@/src/features/ai-gateway/constants/gatewayConfig";
+import { Button as DesignSystemButton } from "@/src/components/design-system/Button/Button";
+import { CreateIngestionProjectDialogController } from "@/src/features/ai-gateway/components/GatewayConfigurationPage/components/CreateIngestionProjectDialogController/CreateIngestionProjectDialogController";
+import {
+ GATEWAY_CONFIGURATION_DESCRIPTION,
+ GATEWAY_CONFIGURATION_TITLE,
+} from "@/src/features/ai-gateway/constants/gatewayConfig";
+import { isGatewayIngestionConfigured } from "@/src/features/ai-gateway/fns/isGatewayIngestionConfigured";
import { useCopyToClipboard } from "@/src/hooks/useCopyToClipboard";
import { cn } from "@/src/utils/tailwind";
@@ -110,14 +107,21 @@ export function GatewayConfigurationView({
);
const isDirty =
projectId !== initialProjectId || ingestionMode !== initialIngestionMode;
+ const isConfigured = isGatewayIngestionConfigured({
+ ingestionMode,
+ hasActiveIngestionProject: projectId !== null,
+ });
+
+ function handleUseExistingProject() {
+ setShowExistingProjects(true);
+ }
return (
<div className="flex flex-col gap-6">
<div>
- <Header title="AI Gateway configuration" />
+ <Header title={GATEWAY_CONFIGURATION_TITLE} />
<p className="text-muted-foreground text-sm">
- Configure how requests routed through the organization gateway are
- ingested into Langfuse.
+ {GATEWAY_CONFIGURATION_DESCRIPTION}
</p>
</div>
@@ -152,29 +156,40 @@ export function GatewayConfigurationView({
))}
</SelectContent>
</Select>
- <CreateIngestionProjectDialog
+ <CreateIngestionProjectDialogController
ingestionMode={ingestionMode}
isSaving={isSaving}
onCreate={onCreateProject}
- triggerLabel="or create a new project"
- />
+ >
+ {({ openDialog }) => (
+ <DesignSystemButton
+ text="or create a new project"
+ variant="text"
+ onClick={openDialog}
+ />
+ )}
+ </CreateIngestionProjectDialogController>
</div>
) : (
<div className="flex flex-row items-center gap-2">
- <CreateIngestionProjectDialog
+ <CreateIngestionProjectDialogController
ingestionMode={ingestionMode}
isSaving={isSaving}
onCreate={onCreateProject}
- triggerLabel="Create ingestion project"
- showIcon
- />
- <button
- type="button"
- className="text-primary text-sm hover:underline"
- onClick={() => setShowExistingProjects(true)}
>
- or use an existing project
- </button>
+ {({ openDialog }) => (
+ <DesignSystemButton
+ text="Create ingestion project"
+ icon={Plus}
+ onClick={openDialog}
+ />
+ )}
+ </CreateIngestionProjectDialogController>
+ <DesignSystemButton
+ text="or use an existing project"
+ variant="text"
+ onClick={handleUseExistingProject}
+ />
</div>
)}
<p className="text-muted-foreground mt-2 text-xs">
@@ -234,9 +249,14 @@ export function GatewayConfigurationView({
</Alert>
) : null}
- <div className="flex justify-end">
+ <div className="flex items-center justify-end gap-3">
+ {isConfigured ? null : (
+ <p className="text-muted-foreground text-xs">
+ Select an ingestion project to capture gateway traffic.
+ </p>
+ )}
<Button
- disabled={!isDirty || isSaving}
+ disabled={!isDirty || !isConfigured || isSaving}
loading={isSaving}
onClick={() => onSave({ projectId, ingestionMode })}
>
@@ -247,89 +267,6 @@ export function GatewayConfigurationView({
);
}
-function CreateIngestionProjectDialog({
- ingestionMode,
- isSaving,
- onCreate,
- triggerLabel,
- showIcon = false,
-}: {
- ingestionMode: IngestionMode;
- isSaving: boolean;
- onCreate: (values: {
- projectName: string;
- ingestionMode: IngestionMode;
- }) => void | Promise<void>;
- triggerLabel: string;
- showIcon?: boolean;
-}) {
- const [projectName, setProjectName] = useState(
- DEFAULT_GATEWAY_INGESTION_PROJECT_NAME,
- );
-
- return (
- <DialogController
- size="default"
- closeOnInteractionOutside={false}
- onBeforeClose={() => !isSaving}
- onDismiss={() => setProjectName(DEFAULT_GATEWAY_INGESTION_PROJECT_NAME)}
- renderContent={() => (
- <>
- <DialogHeader>
- <DialogTitle>Create ingestion project</DialogTitle>
- </DialogHeader>
- <DialogBody>
- <div>
- <Label htmlFor="gateway-project-name">Project name</Label>
- <Input
- id="gateway-project-name"
- className="ph-no-capture mt-1.5"
- value={projectName}
- onChange={(event) => setProjectName(event.target.value)}
- />
- <p className="text-muted-foreground mt-1.5 text-xs">
- Project access follows organization roles and can be restricted
- in project settings.
- </p>
- </div>
- </DialogBody>
- <DialogFooter>
- <Button
- disabled={!projectName.trim() || isSaving}
- loading={isSaving}
- onClick={() =>
- onCreate({
- projectName: projectName.trim(),
- ingestionMode,
- })
- }
- >
- Create project
- </Button>
- </DialogFooter>
- </>
- )}
- >
- {({ openDialog }) =>
- showIcon ? (
- <Button onClick={openDialog}>
- <Plus size="base" __rawClassName="mr-1.5" />
- {triggerLabel}
- </Button>
- ) : (
- <button
- type="button"
- className="text-primary text-sm hover:underline"
- onClick={openDialog}
- >
- {triggerLabel}
- </button>
- )
- }
- </DialogController>
- );
-}
-
function GatewayUrl({ gatewayBaseUrl }: { gatewayBaseUrl: string }) {
const { copy, isCopied } = useCopyToClipboard();
return (
diff --git a/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/IngestionProjectSetupPlaceholder/IngestionProjectSetupPlaceholder.tsx b/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/IngestionProjectSetupPlaceholder/IngestionProjectSetupPlaceholder.tsx
new file mode 100644
index 0000000000..790136fd54
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayConfigurationPage/components/IngestionProjectSetupPlaceholder/IngestionProjectSetupPlaceholder.tsx
@@ -0,0 +1,70 @@
+import { FolderInput, Plus } from "@langfuse/icons";
+
+import Header from "@/src/components/layouts/header";
+import {
+ CreateIngestionProjectDialogController,
+ type CreateIngestionProjectDialogControllerProps,
+} from "@/src/features/ai-gateway/components/GatewayConfigurationPage/components/CreateIngestionProjectDialogController/CreateIngestionProjectDialogController";
+import { GatewaySetupEmptyState } from "@/src/features/ai-gateway/components/GatewaySetupEmptyState/GatewaySetupEmptyState";
+import {
+ GATEWAY_CONFIGURATION_DESCRIPTION,
+ GATEWAY_CONFIGURATION_TITLE,
+} from "@/src/features/ai-gateway/constants/gatewayConfig";
+
+export type IngestionProjectSetupPlaceholderProps = Pick<
+ CreateIngestionProjectDialogControllerProps,
+ "ingestionMode" | "isSaving" | "onCreate"
+> & {
+ projects: ReadonlyArray<{ id: string; name: string }>;
+ onSelectProject: (projectId: string) => void;
+};
+
+export function IngestionProjectSetupPlaceholder({
+ ingestionMode,
+ isSaving,
+ onCreate,
+ projects,
+ onSelectProject,
+}: IngestionProjectSetupPlaceholderProps) {
+ return (
+ <div className="flex flex-col gap-6">
+ <div>
+ <Header title={GATEWAY_CONFIGURATION_TITLE} />
+ <p className="text-muted-foreground text-sm">
+ {GATEWAY_CONFIGURATION_DESCRIPTION}
+ </p>
+ </div>
+ <CreateIngestionProjectDialogController
+ ingestionMode={ingestionMode}
+ isSaving={isSaving}
+ onCreate={onCreate}
+ >
+ {({ openDialog }) => (
+ <GatewaySetupEmptyState
+ icon={FolderInput}
+ title="Set up an ingestion project first"
+ description="Gateway traffic needs a project to be recorded in. Credentials, models and API keys become available once it is set."
+ actions={[
+ {
+ type: "button",
+ label: "Create ingestion project",
+ icon: Plus,
+ onClick: openDialog,
+ },
+ {
+ type: "menu",
+ label: "Use existing project",
+ items: projects.map((project) => ({
+ id: project.id,
+ title: project.name,
+ })),
+ onSelect: onSelectProject,
+ disabled: isSaving,
+ },
+ ]}
+ />
+ )}
+ </CreateIngestionProjectDialogController>
+ </div>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewayModelsPage/GatewayModelsPage.tsx b/web/src/features/ai-gateway/components/GatewayModelsPage/GatewayModelsPage.tsx
index 0eadfee2e2..d80874e072 100644
--- a/web/src/features/ai-gateway/components/GatewayModelsPage/GatewayModelsPage.tsx
+++ b/web/src/features/ai-gateway/components/GatewayModelsPage/GatewayModelsPage.tsx
@@ -1,9 +1,10 @@
import Header from "@/src/components/layouts/header";
import { Alert } from "@/src/components/design-system/Alert/Alert";
+import { GatewayNotSetUpNotice } from "@/src/features/ai-gateway/components/GatewayNotSetUpNotice/GatewayNotSetUpNotice";
import { Button } from "@/src/components/ui/button";
import { Skeleton } from "@/src/components/ui/skeleton";
import { ConnectedGatewayModelsTable } from "@/src/features/ai-gateway/components/GatewayModelsPage/components/ConnectedGatewayModelsTable/ConnectedGatewayModelsTable";
-import { useHasOrganizationAccess } from "@/src/features/rbac";
+import { useGatewaySettingsAccess } from "@/src/features/ai-gateway/hooks/useGatewaySettingsAccess";
import { api } from "@/src/utils/api";
import { aggregateModels } from "./fns/aggregateModels";
@@ -12,10 +13,31 @@ export function GatewayModelsPage({
}: {
organizationId: string;
}) {
- const canManageGateway = useHasOrganizationAccess({
- organizationId,
- scope: "gateway:manage",
- });
+ const { canManageGateway, isIngestionConfigured, isSetupStatusPending } =
+ useGatewaySettingsAccess(organizationId);
+
+ if (isSetupStatusPending) {
+ return <ModelsSkeleton />;
+ }
+ /** Managers are redirected to Configuration by the settings route. */
+ if (!canManageGateway && isIngestionConfigured === false) {
+ return <GatewayNotSetUpNotice title="Models" />;
+ }
+ return (
+ <GatewayModels
+ organizationId={organizationId}
+ canManageGateway={canManageGateway}
+ />
+ );
+}
+
+function GatewayModels({
+ organizationId,
+ canManageGateway,
+}: {
+ organizationId: string;
+ canManageGateway: boolean;
+}) {
const connectionsQuery = api.aiGateway.listModelConnections.useQuery({
orgId: organizationId,
});
diff --git a/web/src/features/ai-gateway/components/GatewayNotSetUpNotice/GatewayNotSetUpNotice.tsx b/web/src/features/ai-gateway/components/GatewayNotSetUpNotice/GatewayNotSetUpNotice.tsx
new file mode 100644
index 0000000000..61f10c1a72
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewayNotSetUpNotice/GatewayNotSetUpNotice.tsx
@@ -0,0 +1,21 @@
+import { Lock } from "@langfuse/icons";
+
+import Header from "@/src/components/layouts/header";
+import { GatewaySetupEmptyState } from "@/src/features/ai-gateway/components/GatewaySetupEmptyState/GatewaySetupEmptyState";
+
+export type GatewayNotSetUpNoticeProps = {
+ title: string;
+};
+
+export function GatewayNotSetUpNotice({ title }: GatewayNotSetUpNoticeProps) {
+ return (
+ <div className="flex flex-col gap-4">
+ <Header title={title} />
+ <GatewaySetupEmptyState
+ icon={Lock}
+ title="The AI Gateway isn't set up yet"
+ description="Ask your organization admin to set up the AI Gateway."
+ />
+ </div>
+ );
+}
diff --git a/web/src/features/ai-gateway/components/GatewaySetupEmptyState/GatewaySetupEmptyState.stories.tsx b/web/src/features/ai-gateway/components/GatewaySetupEmptyState/GatewaySetupEmptyState.stories.tsx
new file mode 100644
index 0000000000..b37268c9d8
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewaySetupEmptyState/GatewaySetupEmptyState.stories.tsx
@@ -0,0 +1,57 @@
+import { FolderInput, Lock, Plus } from "@langfuse/icons";
+import { expect, fn, userEvent, within } from "storybook/test";
+
+import preview from "@/.storybook/preview";
+import { GatewaySetupEmptyState } from "./GatewaySetupEmptyState";
+
+const meta = preview.meta({ component: GatewaySetupEmptyState });
+
+export const Default = meta.story({
+ args: {
+ icon: Lock,
+ title: "The AI Gateway isn't set up yet",
+ description: "Ask your organization admin to set up the AI Gateway.",
+ },
+});
+
+export const WithActions = meta.story({
+ args: {
+ icon: FolderInput,
+ title: "Set up an ingestion project first",
+ description:
+ "Gateway traffic needs a project to be recorded in. Credentials, models and API keys become available once it is set.",
+ actions: [
+ {
+ type: "button",
+ label: "Create ingestion project",
+ icon: Plus,
+ onClick: fn(),
+ },
+ {
+ type: "menu",
+ label: "Use existing project",
+ items: [
+ { id: "production", title: "Production" },
+ { id: "staging", title: "Staging" },
+ ],
+ onSelect: fn(),
+ },
+ ],
+ },
+});
+
+export const SelectsMenuItem = WithActions.extend({
+ name: "(Test) Selects Menu Item",
+ play: async ({ args, canvasElement }) => {
+ const canvas = within(canvasElement);
+ const body = within(canvasElement.ownerDocument.body);
+
+ await userEvent.click(
+ canvas.getByRole("button", { name: "Use existing project" }),
+ );
+ await userEvent.click(body.getByRole("menuitem", { name: "Staging" }));
+
+ const menuAction = args.actions?.find((action) => action.type === "menu");
+ await expect(menuAction?.onSelect).toHaveBeenCalledWith("staging");
+ },
+});
diff --git a/web/src/features/ai-gateway/components/GatewaySetupEmptyState/GatewaySetupEmptyState.tsx b/web/src/features/ai-gateway/components/GatewaySetupEmptyState/GatewaySetupEmptyState.tsx
new file mode 100644
index 0000000000..fc307e25ea
--- /dev/null
+++ b/web/src/features/ai-gateway/components/GatewaySetupEmptyState/GatewaySetupEmptyState.tsx
@@ -0,0 +1,93 @@
+import type { IconComponent } from "@langfuse/icons";
+
+import { Button } from "@/src/components/design-system/Button/Button";
+import { DropdownIndicator } from "@/src/components/design-system/DropdownIndicator/DropdownIndicator";
+import { DropdownMenu } from "@/src/components/design-system/DropdownMenu/DropdownMenu";
+import { Button as MenuTriggerButton } from "@/src/components/ui/button";
+
+export type GatewaySetupEmptyStateProps = {
+ icon: IconComponent;
+ title: string;
+ description: string;
+ actions?: ReadonlyArray<GatewaySetupEmptyStateAction>;
+};
+
+export function GatewaySetupEmptyState({
+ icon: Icon,
+ title,
+ description,
+ actions = [],
+}: GatewaySetupEmptyStateProps) {
+ return (
+ <div className="border-border bg-muted/30 flex flex-col items-center justify-center gap-3 rounded-md border border-dashed px-6 py-10 text-center">
+ <span className="bg-background flex size-8 items-center justify-center rounded-md border">
+ <Icon size="base" color="muted" aria-hidden="true" />
+ </span>
+ <div className="flex max-w-md flex-col gap-1">
+ <p className="text-sm font-bold">{title}</p>
+ <p className="text-muted-foreground text-sm">{description}</p>
+ </div>
+ {actions.length > 0 ? (
+ <div className="flex flex-wrap items-center justify-center gap-2">
+ {actions.map((action) =>
+ action.type === "menu" ? (
+ <MenuAction key={action.label} action={action} />
+ ) : (
+ <Button
+ key={action.label}
+ text={action.label}
+ icon={action.icon}
+ onClick={action.onClick}
+ />
+ ),
+ )}
+ </div>
+ ) : null}
+ </div>
+ );
+}
+
+function MenuAction({ action }: { action: MenuActionDefinition }) {
+ const isDisabled = action.disabled || action.items.length === 0;
+ return (
+ <DropdownMenu
+ disabled={isDisabled}
+ items={action.items.map((item) => ({
+ type: "item",
+ id: item.id,
+ title: item.title,
+ onClick: () => action.onSelect(item.id),
+ }))}
+ >
+ {({ getTriggerProps }) => (
+ /** The design-system Button does not forward floating-ui trigger handlers. */
+ <MenuTriggerButton
+ variant="outline"
+ className="gap-1.5"
+ disabled={isDisabled}
+ {...getTriggerProps()}
+ >
+ {action.label}
+ <DropdownIndicator nudge />
+ </MenuTriggerButton>
+ )}
+ </DropdownMenu>
+ );
+}
+
+type MenuActionDefinition = {
+ type: "menu";
+ label: string;
+ items: ReadonlyArray<{ id: string; title: string }>;
+ onSelect: (id: string) => void;
+ disabled?: boolean;
+};
+
+type GatewaySetupEmptyStateAction =
+ | {
+ type: "button";
+ label: string;
+ onClick: () => void;
+ icon?: IconComponent;
+ }
+ | MenuActionDefinition;
diff --git a/web/src/features/ai-gateway/constants/gatewayConfig.ts b/web/src/features/ai-gateway/constants/gatewayConfig.ts
index c54fcdfea3..8d929e734c 100644
--- a/web/src/features/ai-gateway/constants/gatewayConfig.ts
+++ b/web/src/features/ai-gateway/constants/gatewayConfig.ts
@@ -1 +1,5 @@
export const DEFAULT_GATEWAY_INGESTION_PROJECT_NAME = "AI Gateway Ingestion";
+
+export const GATEWAY_CONFIGURATION_TITLE = "AI Gateway configuration";
+export const GATEWAY_CONFIGURATION_DESCRIPTION =
+ "Configure how requests routed through the organization gateway are ingested into Langfuse.";
diff --git a/web/src/features/ai-gateway/fns/isGatewayIngestionConfigured.ts b/web/src/features/ai-gateway/fns/isGatewayIngestionConfigured.ts
new file mode 100644
index 0000000000..e8cf18b3ff
--- /dev/null
+++ b/web/src/features/ai-gateway/fns/isGatewayIngestionConfigured.ts
@@ -0,0 +1,12 @@
+import type { GatewayIngestionMode } from "@langfuse/shared";
+
+/** Capturing ingestion modes need a project to record into; `NONE` (self-hosted only) does not. */
+export function isGatewayIngestionConfigured({
+ ingestionMode,
+ hasActiveIngestionProject,
+}: {
+ ingestionMode: GatewayIngestionMode;
+ hasActiveIngestionProject: boolean;
+}) {
+ return ingestionMode === "NONE" || hasActiveIngestionProject;
+}
diff --git a/web/src/features/ai-gateway/hooks/useGatewaySettingsAccess.ts b/web/src/features/ai-gateway/hooks/useGatewaySettingsAccess.ts
new file mode 100644
index 0000000000..df09f0de0a
--- /dev/null
+++ b/web/src/features/ai-gateway/hooks/useGatewaySettingsAccess.ts
@@ -0,0 +1,49 @@
+import { env } from "@/src/env.mjs";
+import { canReadGatewayModels } from "@/src/features/ai-gateway/fns/canReadGatewayModels";
+import { useIsFeatureEnabled } from "@/src/features/feature-flags";
+import { useHasOrganizationAccess } from "@/src/features/rbac";
+import { api } from "@/src/utils/api";
+
+/**
+ * Single source for who sees which gateway settings and whether setup is done.
+ * `isIngestionConfigured` stays undefined while loading so callers never gate
+ * on a status they have not received yet.
+ */
+export function useGatewaySettingsAccess(organizationId: string | undefined) {
+ const canManageGateway = useHasOrganizationAccess({
+ organizationId,
+ scope: "gateway:manage",
+ });
+ const canReadGateway = useHasOrganizationAccess({
+ organizationId,
+ scope: "gateway:read",
+ });
+ const isAiGatewayEnabled = useIsFeatureEnabled("aiGateway", {
+ enableForAdmins: false,
+ organizationId,
+ });
+
+ const showManagement = isAiGatewayEnabled && canManageGateway;
+ const showModels =
+ isAiGatewayEnabled &&
+ organizationId !== undefined &&
+ canReadGatewayModels({
+ organizationId,
+ demoOrganizationId: env.NEXT_PUBLIC_DEMO_ORG_ID,
+ canReadGateway,
+ canManageGateway,
+ });
+
+ const setupStatus = api.aiGateway.getSetupStatus.useQuery(
+ { orgId: organizationId ?? "" },
+ { enabled: organizationId !== undefined && (showManagement || showModels) },
+ );
+
+ return {
+ canManageGateway,
+ showManagement,
+ showModels,
+ isIngestionConfigured: setupStatus.data?.isConfigured,
+ isSetupStatusPending: setupStatus.isPending,
+ };
+}
diff --git a/web/src/features/ai-gateway/server/config/gatewayConfigService.ts b/web/src/features/ai-gateway/server/config/gatewayConfigService.ts
index 0d0ac123e5..20dc8267a8 100644
--- a/web/src/features/ai-gateway/server/config/gatewayConfigService.ts
+++ b/web/src/features/ai-gateway/server/config/gatewayConfigService.ts
@@ -6,6 +6,7 @@ import type {
import { auditLog } from "@/src/features/audit-logs/server";
import type { OrgAuthedContext } from "@/src/server/api/trpc";
import { invalidateGatewayResolveCacheForOrganization } from "@/src/features/ai-gateway/server/resolve/gatewayResolveCache";
+import { isGatewayIngestionConfigured } from "@/src/features/ai-gateway/fns/isGatewayIngestionConfigured";
import { GatewayConfigRepository } from "./gatewayConfigRepository";
export class GatewayConfigService {
@@ -19,12 +20,35 @@ export class GatewayConfigService {
return this.repository.getConfig(organizationId);
}
+ async getSetupStatus(organizationId: string) {
+ const config = await this.getConfig(organizationId);
+ const project = config?.defaultIngestionProject;
+ return {
+ isConfigured: isGatewayIngestionConfigured({
+ ingestionMode: config?.ingestionMode ?? "USAGE",
+ hasActiveIngestionProject:
+ !!project && !project.deletedAt && project.orgId === organizationId,
+ }),
+ };
+ }
+
async updateConfig(params: {
organizationId: string;
defaultIngestionProjectId: string | null;
ingestionMode: GatewayIngestionMode;
session: OrgAuthedContext["session"];
}) {
+ if (
+ !isGatewayIngestionConfigured({
+ ingestionMode: params.ingestionMode,
+ hasActiveIngestionProject: !!params.defaultIngestionProjectId,
+ })
+ ) {
+ throw new InvalidRequestError(
+ "A default ingestion project is required unless ingestion mode is NONE",
+ );
+ }
+
const before = await this.getConfig(params.organizationId);
if (params.defaultIngestionProjectId) {
diff --git a/web/src/features/ai-gateway/server/gatewaySetupRedirect.ts b/web/src/features/ai-gateway/server/gatewaySetupRedirect.ts
new file mode 100644
index 0000000000..0f8d2a079c
--- /dev/null
+++ b/web/src/features/ai-gateway/server/gatewaySetupRedirect.ts
@@ -0,0 +1,44 @@
+import { type Session } from "next-auth";
+import { type PrismaClient } from "@langfuse/shared/src/db";
+
+import { hasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
+import { isGatewayEnabledForOrganization } from "@/src/features/ai-gateway/server/availability";
+import { GatewayConfigService } from "@/src/features/ai-gateway/server/config/gatewayConfigService";
+
+const SETUP_GATED_SETTINGS_SLUGS = new Set([
+ "ai-gateway-providers",
+ "ai-gateway-models",
+ "ai-gateway-api-keys",
+]);
+
+/**
+ * Gateway managers cannot open provider, model or API key settings before an
+ * ingestion project is configured; they are sent to the configuration page.
+ * Readers are not redirected because their Models page explains the state.
+ */
+export async function getGatewaySetupRedirect(params: {
+ prisma: PrismaClient;
+ session: Session | null;
+ organizationId: string;
+ slug: string;
+}): Promise<string | null> {
+ const { prisma, session, organizationId, slug } = params;
+ if (
+ !SETUP_GATED_SETTINGS_SLUGS.has(slug) ||
+ !isGatewayEnabledForOrganization(organizationId) ||
+ !hasOrganizationAccess({
+ session,
+ organizationId,
+ scope: "gateway:manage",
+ })
+ ) {
+ return null;
+ }
+
+ const { isConfigured } = await new GatewayConfigService(
+ prisma,
+ ).getSetupStatus(organizationId);
+ return isConfigured
+ ? null
+ : `/organization/${encodeURIComponent(organizationId)}/settings/ai-gateway`;
+}
diff --git a/web/src/features/ai-gateway/server/router.ts b/web/src/features/ai-gateway/server/router.ts
index 8871935b8e..73d793acd6 100644
--- a/web/src/features/ai-gateway/server/router.ts
+++ b/web/src/features/ai-gateway/server/router.ts
@@ -104,6 +104,14 @@ export const aiGatewayRouter = createTRPCRouter({
};
}),
+ /** Readable by every gateway reader; exposes only whether setup is done, not the config itself. */
+ getSetupStatus: protectedOrganizationProcedure
+ .input(organizationInput)
+ .query(async ({ input, ctx }) => {
+ requireGatewayRead({ session: ctx.session, orgId: input.orgId });
+ return new GatewayConfigService(ctx.prisma).getSetupStatus(input.orgId);
+ }),
+
updateConfig: protectedOrganizationProcedure
.input(
organizationInput.extend({
diff --git a/web/src/features/organizations/OrganizationSettingsPage.tsx b/web/src/features/organizations/OrganizationSettingsPage.tsx
index 3bc75cf1e5..1413fba294 100644
--- a/web/src/features/organizations/OrganizationSettingsPage.tsx
+++ b/web/src/features/organizations/OrganizationSettingsPage.tsx
@@ -24,23 +24,26 @@ import { env } from "@/src/env.mjs";
import { OrgAuditLogsSettingsPage } from "@/src/ee/features/audit-log-viewer";
import { useV4UpgradeUiFlag } from "@/src/features/v4-migration/useV4UpgradeUiEnabled";
import { OrganizationFeaturePreviewsSettings } from "@/src/features/feature-flags/components/OrganizationFeaturePreviewsSettings";
-import { useIsFeatureEnabled } from "@/src/features/feature-flags";
import {
GatewayApiKeysPage,
GatewayConfigurationPage,
GatewayModelsPage,
GatewayProvidersPage,
} from "@/src/features/ai-gateway";
-import { canReadGatewayModels } from "@/src/features/ai-gateway/fns/canReadGatewayModels";
+import { useGatewaySettingsAccess } from "@/src/features/ai-gateway/hooks/useGatewaySettingsAccess";
import useSessionStorage from "@/src/components/useSessionStorage";
import { api } from "@/src/utils/api";
import { ConnectedLLMApiKeySettingsTable } from "@/src/features/public-api/components/LLMApiKeySettingsTable/ConnectedLLMApiKeySettingsTable";
+const GATEWAY_SETUP_REQUIRED_REASON =
+ "Set up an ingestion project in Configuration first.";
+
type OrganizationSettingsPage = {
title: string;
slug: string;
section?: string;
show?: boolean | (() => boolean);
+ disabledReason?: string;
cmdKKeywords?: string[];
} & ({ content: React.ReactNode } | { href: string });
@@ -58,18 +61,11 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {
organizationId: organization?.id,
scope: "organization:update",
});
- const canManageGateway = useHasOrganizationAccess({
- organizationId: organization?.id,
- scope: "gateway:manage",
- });
+ const gatewayAccess = useGatewaySettingsAccess(organization?.id);
const canReadOrganizationLlmConnections = useHasOrganizationAccess({
organizationId: organization?.id,
scope: "organizationLlmApiKeys:read",
});
- const canReadGateway = useHasOrganizationAccess({
- organizationId: organization?.id,
- scope: "gateway:read",
- });
const canReadUsage = useHasOrganizationAccess({
organizationId: organization?.id,
scope: "organizationUsage:read",
@@ -78,10 +74,6 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {
const isLangfuseCloud = isCloudPlan(plan) ?? false;
const isCloudBillingAvailable = useIsCloudBillingAvailable();
const showV4Migration = useV4UpgradeUiFlag();
- const isAiGatewayEnabled = useIsFeatureEnabled("aiGateway", {
- enableForAdmins: false,
- organizationId: organization?.id,
- });
if (!organization) return [];
@@ -97,15 +89,14 @@ export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {
showAuditLogs,
isLangfuseCloud,
showV4Migration,
- showAiGatewayManagement: canManageGateway && isAiGatewayEnabled,
- showAiGatewayModels:
- isAiGatewayEnabled &&
- canReadGatewayModels({
- organizationId: organization.id,
- demoOrganizationId: env.NEXT_PUBLIC_DEMO_ORG_ID,
- canReadGateway,
- canManageGateway,
- }),
+ showAiGatewayManagement: gatewayAccess.showManagement,
+ showAiGatewayModels: gatewayAccess.showModels,
+ /** Only managers get disabled entries; readers see a notice on Models. */
+ gatewaySetupDisabledReason:
+ gatewayAccess.showManagement &&
+ gatewayAccess.isIngestionConfigured === false
+ ? GATEWAY_SETUP_REQUIRED_REASON
+ : undefined,
showOrganizationLlmConnections: canReadOrganizationLlmConnections,
showFeaturePreviews:
canUpdateOrganization && organization.id !== env.NEXT_PUBLIC_DEMO_ORG_ID,
@@ -122,6 +113,7 @@ const getOrganizationSettingsPages = ({
showV4Migration,
showAiGatewayManagement,
showAiGatewayModels,
+ gatewaySetupDisabledReason,
showOrganizationLlmConnections,
showFeaturePreviews,
}: {
@@ -144,6 +136,7 @@ const getOrganizationSettingsPages = ({
showV4Migration: boolean;
showAiGatewayManagement: boolean;
showAiGatewayModels: boolean;
+ gatewaySetupDisabledReason: string | undefined;
showOrganizationLlmConnections: boolean;
showFeaturePreviews: boolean;
}): OrganizationSettingsPage[] => [
@@ -348,6 +341,7 @@ const getOrganizationSettingsPages = ({
],
content: <GatewayProvidersPage organizationId={organization.id} />,
show: showAiGatewayManagement,
+ disabledReason: gatewaySetupDisabledReason,
},
{
title: "Models",
@@ -356,6 +350,7 @@ const getOrganizationSettingsPages = ({
cmdKKeywords: ["gateway", "models", "discovery", "sync"],
content: <GatewayModelsPage organizationId={organization.id} />,
show: showAiGatewayModels,
+ disabledReason: gatewaySetupDisabledReason,
},
{
title: "Gateway keys",
@@ -364,6 +359,7 @@ const getOrganizationSettingsPages = ({
cmdKKeywords: ["gateway", "api", "keys", "metadata", "credentials"],
content: <GatewayApiKeysPage organizationId={organization.id} />,
show: showAiGatewayManagement,
+ disabledReason: gatewaySetupDisabledReason,
},
];
diff --git a/web/src/features/settings/SettingsPage.tsx b/web/src/features/settings/SettingsPage.tsx
index 9a51e980a6..8702968755 100644
--- a/web/src/features/settings/SettingsPage.tsx
+++ b/web/src/features/settings/SettingsPage.tsx
@@ -143,6 +143,7 @@ export function buildSettingsPages({
slug: page.slug,
section: page.section,
show: page.show,
+ disabledReason: page.disabledReason,
href: organizationSettingsPath(organizationId, page.slug),
internal: true,
};
diff --git a/web/src/pages/organization/[organizationId]/settings/[page].tsx b/web/src/pages/organization/[organizationId]/settings/[page].tsx
index 664d81798f..0279bc8083 100644
--- a/web/src/pages/organization/[organizationId]/settings/[page].tsx
+++ b/web/src/pages/organization/[organizationId]/settings/[page].tsx
@@ -1,5 +1,29 @@
+import { type GetServerSideProps } from "next";
+import { prisma } from "@langfuse/shared/src/db";
+
import { SettingsPage } from "@/src/features/settings";
+import { getGatewaySetupRedirect } from "@/src/features/ai-gateway/server/gatewaySetupRedirect";
+import { getServerAuthSession } from "@/src/server/auth";
export default function OrganizationSettingsRoute() {
return <SettingsPage scope="organization" />;
}
+
+export const getServerSideProps: GetServerSideProps = async (ctx) => {
+ const organizationId = ctx.params?.organizationId;
+ const slug = ctx.params?.page;
+ if (typeof organizationId !== "string" || typeof slug !== "string") {
+ return { props: {} };
+ }
+
+ const session = await getServerAuthSession({ req: ctx.req, res: ctx.res });
+ const destination = await getGatewaySetupRedirect({
+ prisma,
+ session,
+ organizationId,
+ slug,
+ });
+ return destination
+ ? { redirect: { destination, permanent: false } }
+ : { props: {} };
+};