Skip to content

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

fix(ai-gateway): stop blocking team invites on sequential SFDC syncs (#19559)

Tobias Wochinger618aac2a3ffa
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 d0ac687b67..94ae6cd659 100644
--- a/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
+++ b/web/src/__tests__/server/ai-gateway-control-plane.servertest.ts
@@ -49,6 +49,7 @@ function queuedNotifications(): SharedServer.NotificationEventType[] {
}
import { env } from "@/src/env.mjs";
+import * as sfdc from "@/src/ee/features/sfdc-sync/server";
import { appRouter } from "@/src/server/api/root";
import {
createInnerTRPCContext,
@@ -677,6 +678,36 @@ describe("AI gateway control plane", () => {
);
});
+ it("adds invitees with an account without waiting for their SFDC sync", async () => {
+ const owner = await prepare();
+ const user = await prisma.user.create({
+ data: { email: `gateway-account-${randomUUID()}@example.test` },
+ });
+ cleanupUsers.push(user.id);
+ const setUserRole = vi.fn(() => new Promise<boolean>(() => {}));
+ const getSfdcService = vi
+ .spyOn(sfdc, "getSfdcService")
+ .mockReturnValue({ setUserRole } as never);
+
+ try {
+ const result = await owner.caller.aiGateway.inviteTeam({
+ orgId: owner.org.id,
+ emails: [user.email!],
+ notifyMembers: false,
+ });
+
+ expect(result).toMatchObject({ invitedCount: 1, failed: [] });
+ expect(setUserRole).toHaveBeenCalledWith({
+ orgId: owner.org.id,
+ userId: user.id,
+ email: user.email,
+ role: Role.AI_GATEWAY,
+ });
+ } finally {
+ getSfdcService.mockRestore();
+ }
+ });
+
it("reports typed members who cannot use the gateway and skips the sender", async () => {
const owner = await prepare();
Object.assign(env, {
diff --git a/web/src/ee/features/sfdc-sync/server/index.ts b/web/src/ee/features/sfdc-sync/server/index.ts
index 3a18098e05..aac0e1c5c8 100644
--- a/web/src/ee/features/sfdc-sync/server/index.ts
+++ b/web/src/ee/features/sfdc-sync/server/index.ts
@@ -6,4 +6,4 @@ export {
} from "./sfdcService";
export { syncOrgPlanChangeToSfdc } from "./planChangeSync";
export { deriveLeadSourceFromMemberships } from "./leadSource";
-export type { SfdcLeadSource } from "./sfdcService";
+export type { SetUserRoleInput, SfdcLeadSource } from "./sfdcService";
diff --git a/web/src/features/ai-gateway/server/team/gatewayTeamService.ts b/web/src/features/ai-gateway/server/team/gatewayTeamService.ts
index a694b67707..8c478194af 100644
--- a/web/src/features/ai-gateway/server/team/gatewayTeamService.ts
+++ b/web/src/features/ai-gateway/server/team/gatewayTeamService.ts
@@ -12,6 +12,11 @@ import {
} from "@langfuse/shared/src/server";
import { env } from "@/src/env.mjs";
+import {
+ getSfdcService,
+ type SetUserRoleInput,
+ type SfdcService,
+} from "@/src/ee/features/sfdc-sync/server";
import {
inviteOrganizationMember,
OrganizationMembersService,
@@ -60,7 +65,10 @@ type GatewayTeamContext = Pick<OrgAuthedContext, "session" | "prisma">;
export class GatewayTeamService {
private readonly members: OrganizationMembersService;
- constructor(prisma: PrismaClient) {
+ constructor(
+ prisma: PrismaClient,
+ private readonly sfdc: SfdcService | null = getSfdcService(),
+ ) {
this.members = new OrganizationMembersService(prisma);
}
@@ -157,6 +165,7 @@ export class GatewayTeamService {
inviterEmail: ctx.session.user.email ?? "",
} as const;
const emailJobs: GatewayInvitationEmail[] = [];
+ const sfdcUserRoles: SetUserRoleInput[] = [];
const emails = normalizeEmails(params.emails);
const existingMembers = new Map(
@@ -211,6 +220,9 @@ export class GatewayTeamService {
audience: { kind: "invitee", hasAccount: userExists },
});
},
+ syncSfdcUserRole: async (input) => {
+ sfdcUserRoles.push(input);
+ },
});
invited.add(email);
} catch (error) {
@@ -227,6 +239,9 @@ export class GatewayTeamService {
failed.push({ email, message: message ?? "Invitation failed" });
}
}
+ // Started before anything else can throw: the memberships are saved, and a
+ // retry would find these users as members and never sync them again.
+ this.syncSfdcUserRoles(sfdcUserRoles);
if (params.notifyMembers && sendsEmail) {
const members = await this.members.findMembersWithScope({
@@ -277,4 +292,19 @@ export class GatewayTeamService {
notifiedCount: notified.size,
};
}
+
+ /**
+ * Each sync is a Mulesoft round trip of seconds, so a large team would hold
+ * the response for minutes. They run in the background instead; each sync
+ * logs its own delivery failures.
+ */
+ private syncSfdcUserRoles(inputs: SetUserRoleInput[]) {
+ const { sfdc } = this;
+ if (!sfdc) return;
+ Promise.all(inputs.map((input) => sfdc.setUserRole(input))).catch(
+ (error) => {
+ logger.error("Failed to sync AI Gateway invitees to SFDC", { error });
+ },
+ );
+ }
}
diff --git a/web/src/features/rbac/server/inviteOrganizationMember.ts b/web/src/features/rbac/server/inviteOrganizationMember.ts
index ceb4f9aaf3..839d12f7b4 100644
--- a/web/src/features/rbac/server/inviteOrganizationMember.ts
+++ b/web/src/features/rbac/server/inviteOrganizationMember.ts
@@ -2,7 +2,10 @@ import { TRPCError } from "@trpc/server";
import { Prisma, type PrismaClient, Role } from "@langfuse/shared";
import { auditLog } from "@/src/features/audit-logs/server";
-import { getSfdcService } from "@/src/ee/features/sfdc-sync/server";
+import {
+ getSfdcService,
+ type SetUserRoleInput,
+} from "@/src/ee/features/sfdc-sync/server";
import {
createWithinEntitlementLimit,
hasEntitlement,
@@ -84,6 +87,12 @@ type InviteOrganizationMemberParams = {
orgName: string;
userExists: boolean;
}) => Promise<void>;
+ /**
+ * Links an existing user to the org in SFDC, one Mulesoft round trip that
+ * can take seconds. Defaults to syncing before returning; batch callers
+ * collect the syncs and run them outside their invite loop.
+ */
+ syncSfdcUserRole?: (input: SetUserRoleInput) => Promise<unknown>;
};
/**
@@ -94,6 +103,7 @@ export async function inviteOrganizationMember({
ctx,
input,
sendInvitationEmail,
+ syncSfdcUserRole = syncSfdcUserRoleNow,
}: InviteOrganizationMemberParams) {
/**
* Create a new organization membership
@@ -327,7 +337,7 @@ export async function inviteOrganizationMember({
after: orgMembership,
});
// SFDC: link existing lead to org as a member.
- await getSfdcService()?.setUserRole({
+ await syncSfdcUserRole({
orgId: input.orgId,
userId: user.id,
email: user.email,
@@ -406,3 +416,7 @@ export async function inviteOrganizationMember({
}
}
}
+
+function syncSfdcUserRoleNow(input: SetUserRoleInput) {
+ return getSfdcService()?.setUserRole(input) ?? Promise.resolve(false);
+}