🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(support): ensure correct assignment of tenant id (#8909)
Michael Fröhlichee80b102bca9
diff --git a/web/src/features/entitlements/constants/entitlements.ts b/web/src/features/entitlements/constants/entitlements.tsindex da305f777c..683f5cb158 100644--- a/web/src/features/entitlements/constants/entitlements.ts+++ b/web/src/features/entitlements/constants/entitlements.ts@@ -15,14 +15,12 @@ const entitlements = ["scheduled-blob-exports","prompt-protected-labels","admin-api",- "in-app-support",] as const;export type Entitlement = (typeof entitlements)[number];const cloudAllPlansEntitlements: Entitlement[] = ["cloud-billing","trace-deletion",- "in-app-support",];const selfHostedAllPlansEntitlements: Entitlement[] = [diff --git a/web/src/features/support-chat/IntroSection.tsx b/web/src/features/support-chat/IntroSection.tsxindex ddda5cefeb..b16d4398e6 100644--- a/web/src/features/support-chat/IntroSection.tsx+++ b/web/src/features/support-chat/IntroSection.tsx@@ -16,7 +16,8 @@ import { Separator } from "@/src/components/ui/separator";import { usePlan } from "@/src/features/entitlements/hooks";import { isCloudPlan } from "@langfuse/shared";import { useUiCustomization } from "@/src/ee/features/ui-customization/useUiCustomization";-import { useHasEntitlement } from "@/src/features/entitlements/hooks";++import { env } from "@/src/env.mjs";type SupportType = "in-app-support" | "custom" | "community";@@ -27,7 +28,14 @@ export function IntroSection({displayDensity?: "default" | "compact";}) {const uiCustomization = useUiCustomization();- const hasInAppSupportEntitlement = useHasEntitlement("in-app-support");++ // Note: We previously added an entitlement for in-app support, but removed it for now.+ // The issue was that on global routes e.g., https://langfuse.com/setup, the entitlement+ // hook would not have access to an org or project an therefore no plan, always returning+ // false if asked. However on these pages, the in-app-chat should be available.+ // Therefore we now check for whether wer are in a cloud deployment instead.+ // const hasInAppSupportEntitlement = useHasEntitlement("in-app-support");+ const hasInAppSupportEntitlement = !!env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;const plan = usePlan();const supportType: SupportType = useMemo(() => {diff --git a/web/src/features/support-chat/SupportFormSection.tsx b/web/src/features/support-chat/SupportFormSection.tsxindex b93294cfc8..7672eaea3e 100644--- a/web/src/features/support-chat/SupportFormSection.tsx+++ b/web/src/features/support-chat/SupportFormSection.tsx@@ -33,7 +33,7 @@ import {SelectValue,} from "@/src/components/ui/select";import { Textarea } from "@/src/components/ui/textarea";-import useProjectIdFromURL from "@/src/hooks/useProjectIdFromURL";+import { useQueryProjectOrOrganization } from "@/src/features/projects/hooks";import { useMemo, useState } from "react";import {@@ -54,7 +54,7 @@ export function SupportFormSection({onCancel: () => void;onSuccess: () => void;}) {- const projectId = useProjectIdFromURL();+ const { organization, project } = useQueryProjectOrOrganization();// Tracks whether we've already warned about a short messageconst [warnedShortOnce, setWarnedShortOnce] = useState(false);@@ -189,7 +189,8 @@ export function SupportFormSection({integrationType: parsed.integrationType,message: parsed.message,url: window.location.href,- projectId,+ organizationId: organization?.id,+ projectId: project?.id,browserMetadata: {userAgent: navigator.userAgent,platform: navigator.platform,diff --git a/web/src/features/support-chat/plain/plainClient.ts b/web/src/features/support-chat/plain/plainClient.tsindex d421a01a67..cb8a3a6e7e 100644--- a/web/src/features/support-chat/plain/plainClient.ts+++ b/web/src/features/support-chat/plain/plainClient.ts@@ -88,7 +88,7 @@ function unwrap<T>(label: string, res: { data?: T; error?: unknown }): T {return res.data;}-function tenantExternalId(orgId: string, region: string) {+export function generateTenantExternalId(orgId: string, region: string) {return `cloud_${region}_org_${orgId}`;}@@ -163,7 +163,7 @@ export async function syncTenantsAndTiers(await Promise.all(user.organizations.map(async (org: Organization) => {- const extId = tenantExternalId(org.id, region);+ const extId = generateTenantExternalId(org.id, region);const upsertTenantRes = await client.upsertTenant({identifier: { externalId: extId },name: `${region} - ${org.name}`,@@ -257,7 +257,7 @@ export async function syncCustomerTenantMemberships(.filter((id) => id?.startsWith(regionPrefix));const targetTenantIds: string[] = user.organizations.map((org) =>- tenantExternalId(org.id, region),+ generateTenantExternalId(org.id, region),);const toRemove = existingTenantIdsInRegion.filter(@@ -379,6 +379,7 @@ export async function createSupportThread(url?: string;integrationType?: string;attachmentIds?: string[];+ tenantExternalId?: string;},): Promise<CreateSupportThreadResult> {const { client } = ctx;@@ -402,6 +403,9 @@ export async function createSupportThread(customerIdentifier: { emailAddress: input.email },components,threadFields,+ tenantIdentifier: input.tenantExternalId+ ? { externalId: input.tenantExternalId }+ : undefined,attachmentIds: attachmentIds.length ? attachmentIds : undefined,});@@ -416,6 +420,9 @@ export async function createSupportThread(title: input.title,customerIdentifier: { emailAddress: input.email },components,+ tenantIdentifier: input.tenantExternalId+ ? { externalId: input.tenantExternalId }+ : undefined,attachmentIds: attachmentIds.length ? attachmentIds : undefined,});diff --git a/web/src/features/support-chat/trpc/plainRouter.ts b/web/src/features/support-chat/trpc/plainRouter.tsindex 85c57deaf9..a3207a342c 100644--- a/web/src/features/support-chat/trpc/plainRouter.ts+++ b/web/src/features/support-chat/trpc/plainRouter.ts@@ -20,6 +20,7 @@ import {createAttachmentUploadUrls,createSupportThread as plainCreateSupportThread,createThreadEvent,+ generateTenantExternalId,syncTenantsAndTiers,syncCustomerTenantMemberships,type SessionUser,@@ -35,6 +36,7 @@ const CreateSupportThreadInput = z.object({topic: TopicSchema,message: z.string().trim().min(1),url: z.string().url().optional(),+ organizationId: z.string().optional(),projectId: z.string().optional(),browserMetadata: z.record(z.any()).optional(),integrationType: z.string().optional(),@@ -132,33 +134,89 @@ export const plainRouter = createTRPCRouter({});}+ const currentSupportRequestContext = {+ organizationId: input.organizationId,+ projectId: input.projectId,+ region: env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION,+ plan: undefined as string | undefined,+ tenantExternalId: undefined as string | undefined,+ };++ // Validate that, if organizationId is provided the user has access to it+ if (input.organizationId) {+ const organization = ctx.session.user.organizations.find(+ (o) => o.id === input.organizationId,+ );++ if (!organization) {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message: "Organization not found.",+ });+ }++ currentSupportRequestContext.plan = organization.plan;++ if (input.projectId) {+ // Validate that, if projectId is provided the user has access to it+ if (!organization.projects?.some((p) => p.id === input.projectId)) {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message: "Project not found.",+ });+ }+ }+ }++ // Validate that, if organizationId is NOT provided the user has access to the project+ if (!input.organizationId && input.projectId) {+ const organization = deriveOrganizationFromProject(+ ctx.session.user as SessionUser,+ input.projectId,+ );+ if (!organization) {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message: "Organization not found.",+ });+ }++ currentSupportRequestContext.plan = organization.plan;+ currentSupportRequestContext.organizationId = organization.id;+ }++ if (+ currentSupportRequestContext.organizationId &&+ currentSupportRequestContext.region+ ) {+ currentSupportRequestContext.tenantExternalId =+ generateTenantExternalId(+ currentSupportRequestContext.organizationId,+ currentSupportRequestContext.region,+ );+ }+const plain = initPlain({ apiKey: env.PLAIN_API_KEY });// (1) Ensure customerconst customerId = await ensureCustomer(plain, { email, fullName });// (2) Ensure tenants/tiers and sync memberships — best-effort- const region = env.NEXT_PUBLIC_LANGFUSE_CLOUD_REGION;const demoOrgId = env.NEXT_PUBLIC_DEMO_ORG_ID;- if (region) {+ if (currentSupportRequestContext.region) {await syncTenantsAndTiers(plain, {user: ctx.session.user as SessionUser,- region,+ region: currentSupportRequestContext.region,demoOrgId,});await syncCustomerTenantMemberships(plain, {email,customerId,user: ctx.session.user as SessionUser,- region,+ region: currentSupportRequestContext.region,});}- // Domain prep- const derivedOrganization = deriveOrganizationFromProject(- ctx.session.user as SessionUser,- input.projectId,- );const { topLevel, subtype } = splitTopic(input.topic);// (3) Create thread (with fallback inside)@@ -172,6 +230,7 @@ export const plainRouter = createTRPCRouter({topicTopLevel: topLevel,topicSubtype: subtype,url: input.url,+ tenantExternalId: currentSupportRequestContext.tenantExternalId,integrationType: input.integrationType,attachmentIds: input.attachmentIds ?? [],});@@ -184,11 +243,11 @@ export const plainRouter = createTRPCRouter({buildPlainEventSupportRequestMetadataComponents({userEmail: email,url: input.url,- organizationId: derivedOrganization?.id,- projectId: input.projectId,+ organizationId: currentSupportRequestContext.organizationId,+ projectId: currentSupportRequestContext.projectId,version: VERSION,- plan: derivedOrganization?.plan,- cloudRegion: region,+ plan: currentSupportRequestContext.plan,+ cloudRegion: currentSupportRequestContext.region,browserMetadata: input.browserMetadata,});