Skip to content

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

15.4 KB
Raw Blame
/* eslint-disable @typescript-eslint/no-use-before-define */
import Header from "@/src/components/layouts/header";
import { Button } from "@/src/components/ui/button";
import { InfoTooltip } from "@/src/components/ui/InfoTooltip/InfoTooltip";
import { ConnectedMembershipInvitesSettingsTable } from "@/src/features/rbac/components/MembershipInvitesSettingsTable/ConnectedMembershipInvitesSettingsTable";
import { ConnectedMembersSettingsTable } from "@/src/features/rbac/components/MembersSettingsTable/ConnectedMembersSettingsTable";
import { useHasOrganizationAccess } from "@/src/features/rbac/utils/checkOrganizationAccess";
import { JSONView } from "@/src/components/ui/CodeJsonViewer";
import { Card } from "@/src/components/ui/card";
import RenameOrganization from "@/src/features/organizations/components/RenameOrganization";
import { DeleteOrganizationDialogController } from "@/src/features/organizations/components/DeleteOrganizationDialogController";
import { SettingsDangerZone } from "@/src/components/SettingsDangerZone";
import { BillingSettings } from "@/src/ee/features/billing/components/BillingSettings";
import { OrganizationUsageBreakdown } from "@/src/features/organization-usage/components/OrganizationUsageBreakdown";
import { useHasEntitlement, usePlan } from "@/src/features/entitlements/hooks";
import { SSOSettings } from "@/src/ee/features/sso-settings/components/SSOSettings";
import { type CloudConfigSchema, isCloudPlan } from "@langfuse/shared";
import { Building2 } from "@langfuse/icons";
import { useQueryProjectOrOrganization } from "@/src/features/projects/hooks";
import { LlmConnectionEmptyState } from "@/src/features/public-api/components/LlmConnectionEmptyState/LlmConnectionEmptyState";
import { ApiKeyList } from "@/src/features/public-api/components/ApiKeyList";
import AIFeatureSwitch from "@/src/features/organizations/components/AIFeatureSwitch";
import { useIsCloudBillingAvailable } from "@/src/ee/features/billing/utils/isCloudBilling";
import { env } from "@/src/env.mjs";
import { OrgAuditLogsSettingsPage } from "@/src/ee/features/audit-log-viewer/OrgAuditLogsSettingsPage";
import { useV4UpgradeUiFlag } from "@/src/features/v4-migration/useV4UpgradeUiEnabled";
import { OrganizationFeaturePreviewsSettings } from "@/src/features/feature-flags/components/OrganizationFeaturePreviewsSettings";
import { GatewayAnalyticsPage } from "@/src/features/ai-gateway/components/GatewayAnalyticsPage/GatewayAnalyticsPage";
import { GatewayNotDeployedNotice } from "@/src/features/ai-gateway/components/GatewayNotDeployedNotice/GatewayNotDeployedNotice";
import { GatewaySetupPage } from "@/src/features/ai-gateway/components/GatewaySetupPage/GatewaySetupPage";
import { GatewayApiKeysPage } from "@/src/features/ai-gateway/components/GatewayApiKeysPage/GatewayApiKeysPage";
import { GatewayModelsPage } from "@/src/features/ai-gateway/components/GatewayModelsPage/GatewayModelsPage";
import { useGatewaySettingsAccess } from "@/src/features/ai-gateway/hooks/useGatewaySettingsAccess";
import { useGatewayKeyReminder } from "@/src/features/ai-gateway/hooks/useGatewayKeyReminder";
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 Setup first.";

type OrganizationSettingsPage = {
  title: string;
  slug: string;
  section?: string;
  show?: boolean | (() => boolean);
  disabledReason?: string;
  indicator?: boolean;
  cmdKKeywords?: string[];
} & ({ content: React.ReactNode } | { href: string });

export function useOrganizationSettingsPages(): OrganizationSettingsPage[] {
  const { organization } = useQueryProjectOrOrganization();
  const showBillingSettings = useHasEntitlement("cloud-billing");
  const hasAdminApiEntitlement = useHasEntitlement("admin-api");
  const hasOrgApiKeyAccess = useHasOrganizationAccess({
    organizationId: organization?.id,
    scope: "organization:CRUD_apiKeys",
  });
  const showOrgApiKeySettings = hasAdminApiEntitlement && hasOrgApiKeyAccess;
  const hasAuditLogsEntitlement = useHasEntitlement("audit-logs");
  const canReadSettings = useHasOrganizationAccess({
    organizationId: organization?.id,
    scope: "organizationSettings:read",
  });
  const canUpdateOrganization = useHasOrganizationAccess({
    organizationId: organization?.id,
    scope: "organization:update",
  });
  const gatewayAccess = useGatewaySettingsAccess(organization?.id);
  const gatewayKeyReminder = useGatewayKeyReminder(organization?.id);
  const canReadOrganizationLlmConnections = useHasOrganizationAccess({
    organizationId: organization?.id,
    scope: "organizationLlmApiKeys:read",
  });
  const canReadUsage = useHasOrganizationAccess({
    organizationId: organization?.id,
    scope: "organizationUsage:read",
  });
  const plan = usePlan();
  const isLangfuseCloud = isCloudPlan(plan) ?? false;
  const isCloudBillingAvailable = useIsCloudBillingAvailable();
  const showV4Migration = useV4UpgradeUiFlag();

  if (!organization) return [];

  const showBillingPage =
    showBillingSettings && isCloudBillingAvailable && canReadSettings;

  return getOrganizationSettingsPages({
    organization,
    showBillingSettings: showBillingPage,
    // The billing page embeds the usage breakdown, so the usage page only
    // covers deployments without one (self-hosted).
    showUsageSettings: canReadUsage && !showBillingPage,
    showOrgApiKeySettings,
    showAuditLogs: hasAuditLogsEntitlement && canReadSettings,
    showSsoSettings: isLangfuseCloud && canReadSettings,
    showV4Migration: showV4Migration && canReadSettings,
    showGeneralSettings: canReadSettings,
    showMembers: canReadSettings,
    showProjects: canReadSettings || organization.projects.length > 0,
    showAiGatewayManagement: gatewayAccess.showManagement,
    showAiGatewayNotDeployedNotice: gatewayAccess.showNotDeployedNotice,
    showAiGatewayModels: gatewayAccess.showModels,
    showAiGatewayApiKeys: gatewayAccess.showApiKeys,
    gatewayKeyReminder,
    /** Only managers get disabled entries; others see a notice on the page. */
    gatewaySetupDisabledReason:
      gatewayAccess.showManagement &&
      gatewayAccess.isIngestionConfigured === false
        ? GATEWAY_SETUP_REQUIRED_REASON
        : undefined,
    showOrganizationLlmConnections: canReadOrganizationLlmConnections,
    showFeaturePreviews:
      canUpdateOrganization && organization.id !== env.NEXT_PUBLIC_DEMO_ORG_ID,
  });
}

const getOrganizationSettingsPages = ({
  organization,
  showBillingSettings,
  showUsageSettings,
  showOrgApiKeySettings,
  showAuditLogs,
  showSsoSettings,
  showV4Migration,
  showGeneralSettings,
  showMembers,
  showProjects,
  showAiGatewayManagement,
  showAiGatewayNotDeployedNotice,
  showAiGatewayModels,
  showAiGatewayApiKeys,
  gatewayKeyReminder,
  gatewaySetupDisabledReason,
  showOrganizationLlmConnections,
  showFeaturePreviews,
}: {
  organization: {
    id: string;
    name: string;
    metadata: Record<string, unknown>;
    cloudConfig?: CloudConfigSchema | null;
    projects: Array<{
      id: string;
      name: string;
      deletedAt?: Date | string | null;
    }>;
  };
  showBillingSettings: boolean;
  showUsageSettings: boolean;
  showOrgApiKeySettings: boolean;
  showAuditLogs: boolean;
  showSsoSettings: boolean;
  showV4Migration: boolean;
  showGeneralSettings: boolean;
  showMembers: boolean;
  showProjects: boolean;
  showAiGatewayManagement: boolean;
  showAiGatewayNotDeployedNotice: boolean;
  showAiGatewayModels: boolean;
  showAiGatewayApiKeys: boolean;
  gatewayKeyReminder: boolean;
  gatewaySetupDisabledReason: string | undefined;
  showOrganizationLlmConnections: boolean;
  showFeaturePreviews: boolean;
}): OrganizationSettingsPage[] => [
  {
    title: "General",
    slug: "index",
    section: "Organization",
    cmdKKeywords: ["name", "id", "delete"],
    content: (
      <div className="flex flex-col gap-6">
        <RenameOrganization />
        <div>
          <Header title="Debug Information" />
          <Card className="mb-4 p-3">
            <JSONView
              title="Metadata"
              json={{
                name: organization.name,
                id: organization.id,
                ...organization.metadata,
                ...(env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION && {
                  cloudRegion: env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION,
                }),
                ...(organization.cloudConfig?.clickhouse?.organizationId && {
                  clickhouseOrganizationId:
                    organization.cloudConfig.clickhouse.organizationId,
                }),
              }}
              borderless
            />
          </Card>
        </div>
        <AIFeatureSwitch />
        <SettingsDangerZone
          items={[
            {
              title: "Delete this organization",
              description:
                "Once you delete an organization, there is no going back. Please be certain.",
              button: (
                <DeleteOrganizationDialogController>
                  {({ disabled, openDialog }) => (
                    <Button
                      variant="destructive-secondary"
                      disabled={disabled !== undefined}
                      onClick={openDialog}
                    >
                      Delete Organization
                    </Button>
                  )}
                </DeleteOrganizationDialogController>
              ),
            },
          ]}
        />
      </div>
    ),
    show: showGeneralSettings,
  },
  {
    title: "Feature Previews",
    slug: "feature-previews",
    section: "Organization",
    cmdKKeywords: ["feature", "preview", "flags", "beta"],
    content: <OrganizationFeaturePreviewsSettings orgId={organization.id} />,
    show: showFeaturePreviews,
  },
  {
    title: "API Keys",
    slug: "api-keys",
    section: "Organization",
    content: (
      <div className="flex flex-col gap-6">
        <ApiKeyList entityId={organization.id} scope="organization" />
      </div>
    ),
    show: showOrgApiKeySettings,
  },
  {
    title: "LLM Connections",
    slug: "llm-connections",
    section: "Organization",
    cmdKKeywords: ["llm", "models", "providers", "credentials"],
    content: (
      <div>
        <ConnectedLLMApiKeySettingsTable
          owner={{
            scope: "organization",
            organizationId: organization.id,
          }}
          toolbarContent={
            <div className="flex items-center gap-1">
              <Header title="Organization LLM Connections" className="mb-0!" />
              <InfoTooltip label="About organization connections">
                Connections are available to all current and future projects.
                Project connections with the same provider take precedence.
              </InfoTooltip>
            </div>
          }
          emptyState={
            <LlmConnectionEmptyState
              icon={Building2}
              title="No organization connections"
              description={`Organization connections are shared with every project in ${organization.name}.`}
            />
          }
        />
      </div>
    ),
    show: showOrganizationLlmConnections,
  },
  {
    title: "Members",
    slug: "members",
    section: "Organization",
    cmdKKeywords: ["invite", "user", "rbac"],
    content: <OrganizationMembersSettings orgId={organization.id} />,
    show: showMembers,
  },
  {
    title: "Audit Logs",
    slug: "audit-logs",
    section: "Organization",
    cmdKKeywords: ["audit", "logs", "history", "changes"],
    content: <OrgAuditLogsSettingsPage orgId={organization.id} />,
    show: showAuditLogs,
  },
  {
    title: "Usage",
    slug: "usage",
    section: "Organization",
    cmdKKeywords: ["usage", "units", "traces", "observations", "scores"],
    content: (
      <div>
        <Header title="Usage" />
        <OrganizationUsageBreakdown orgId={organization.id} />
      </div>
    ),
    show: showUsageSettings,
  },
  {
    title: "Billing",
    slug: "billing",
    section: "Organization",
    cmdKKeywords: ["payment", "subscription", "plan", "invoice"],
    content: <BillingSettings />,
    show: showBillingSettings,
  },
  {
    title: "SSO",
    slug: "sso",
    section: "Organization",
    cmdKKeywords: [
      "sso",
      "login",
      "auth",
      "okta",
      "saml",
      "azure",
      "domain",
      "dns",
      "txt",
      "verify",
    ],
    content: <SSOSettings orgId={organization.id} />,
    show: showSsoSettings,
  },
  {
    title: "Projects",
    slug: "projects",
    section: "Organization",
    href: `/organization/${organization.id}`,
    show: showProjects,
  },
  {
    title: "v4 Migration",
    slug: "v4-migration",
    section: "Organization",
    href: "/v4-migration",
    show: showV4Migration,
  },
  {
    title: "Setup",
    slug: "ai-gateway",
    section: "AI Gateway",
    cmdKKeywords: [
      "gateway",
      "llm",
      "setup",
      "configuration",
      "ingestion",
      "providers",
      "credentials",
      "openai",
      "anthropic",
    ],
    content: showAiGatewayNotDeployedNotice ? (
      <GatewayNotDeployedNotice />
    ) : (
      <GatewaySetupPage
        organizationId={organization.id}
        projects={organization.projects}
      />
    ),
    show: showAiGatewayManagement || showAiGatewayNotDeployedNotice,
  },
  {
    title: "Models",
    slug: "ai-gateway-models",
    section: "AI Gateway",
    cmdKKeywords: ["gateway", "models", "discovery", "sync"],
    content: <GatewayModelsPage organizationId={organization.id} />,
    show: showAiGatewayModels,
    disabledReason: gatewaySetupDisabledReason,
  },
  {
    title: "Gateway keys",
    slug: "ai-gateway-api-keys",
    section: "AI Gateway",
    cmdKKeywords: ["gateway", "api", "keys", "metadata", "credentials"],
    content: <GatewayApiKeysPage organizationId={organization.id} />,
    show: showAiGatewayApiKeys,
    indicator: gatewayKeyReminder,
    disabledReason: gatewaySetupDisabledReason,
  },
  {
    title: "Analytics",
    slug: "ai-gateway-analytics",
    section: "AI Gateway",
    cmdKKeywords: ["gateway", "analytics", "cost", "usage", "consumption"],
    content: <GatewayAnalyticsPage organizationId={organization.id} />,
    show: showAiGatewayManagement,
    disabledReason: gatewaySetupDisabledReason,
  },
];

function OrganizationMembersSettings({ orgId }: { orgId: string }) {
  const [invitesPagination, setInvitesPagination] = useSessionStorage(
    `orgInvites_${orgId}_pagination`,
    { pageIndex: 0, pageSize: 10 },
  );

  const hasViewAccess = useHasOrganizationAccess({
    organizationId: orgId,
    scope: "organizationMembers:read",
  });

  const membershipInvites = api.members.allInvitesFromOrg.useQuery(
    {
      orgId,
      page: invitesPagination.pageIndex,
      limit: invitesPagination.pageSize,
    },
    { enabled: hasViewAccess },
  );

  return (
    <div className="flex flex-col gap-6">
      <div>
        <Header title="Organization Members" />
        <ConnectedMembersSettingsTable orgId={orgId} />
      </div>
      {hasViewAccess && (
        <div
          className={
            membershipInvites.data?.totalCount === 0 ? "hidden" : undefined
          }
        >
          <Header title="Membership Invites" />
          <ConnectedMembershipInvitesSettingsTable
            orgId={orgId}
            query={membershipInvites}
            paginationState={invitesPagination}
            setPaginationState={setInvitesPagination}
          />
        </div>
      )}
    </div>
  );
}