From f1f9afa572a19446abdcc3e836cdd95dbabcf03b Mon Sep 17 00:00:00 2001 From: pedrofrxncx Date: Sun, 23 Aug 2026 19:28:04 -0300 Subject: [PATCH] perf(core): parallelize resolveTier's independent storage reads --- apps/api/src/core/resolve-tier.ts | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/apps/api/src/core/resolve-tier.ts b/apps/api/src/core/resolve-tier.ts index 254f68c16d..8330953413 100644 --- a/apps/api/src/core/resolve-tier.ts +++ b/apps/api/src/core/resolve-tier.ts @@ -125,24 +125,30 @@ export async function resolveTier( const orgId = ctx.organization?.id; if (!orgId) throw new Error("resolveTier called without an organization"); - const settings = await ctx.storage.organizationSettings.get(orgId); - const orgSlot = settings?.simple_mode?.tiers?.[tier] ?? null; - // Per-user override, opt-in and chat-tiers-only (see ResolveTierOptions). // Skipping the read entirely when it can't win also keeps a dispatch from // issuing three throwaway queries for image/web_search/deep_research. const userId = ctx.auth?.user?.id; const chatTier = ChatTierSchema.safeParse(tier); + const applyUserPrefs = Boolean( + opts?.applyUserPrefs && chatTier.success && userId, + ); + + // These three reads are independent — fetch them concurrently. + const [settings, userPrefs, allKeys] = await Promise.all([ + ctx.storage.organizationSettings.get(orgId), + applyUserPrefs + ? ctx.storage.userModelPreferences.get(userId as string, orgId) + : Promise.resolve(null), + ctx.storage.aiProviderKeys.list({ organizationId: orgId }), + ]); + + const orgSlot = settings?.simple_mode?.tiers?.[tier] ?? null; const userSlot = - opts?.applyUserPrefs && chatTier.success && userId - ? ((await ctx.storage.userModelPreferences.get(userId, orgId))?.tiers?.[ - chatTier.data - ] ?? null) + applyUserPrefs && chatTier.success + ? (userPrefs?.tiers?.[chatTier.data] ?? null) : null; - - const keys = ( - await ctx.storage.aiProviderKeys.list({ organizationId: orgId }) - ).filter((key) => isHostedProviderId(key.providerId)); + const keys = allKeys.filter((key) => isHostedProviderId(key.providerId)); // Prefer the user override, then the org slot; take the first whose key is // still live. A slot pointing at a deleted key is skipped so resolution