Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions apps/api/src/core/resolve-tier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading