feat(server,web,mobile): fall back when the default provider is out of usage #26164
Mergedmaria-rcksmerged 17 commits into
main fromrefs/pull/26164/headmerged by maria-rcks View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/textGeneration/TextGeneration.ts line 104OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -58,0 +84,21 @@84+ resolveInstance(registry, operation, modelSelection.instanceId).pipe(85+ Effect.flatMap((service) => invoke(service, modelSelection)),86+ );87+ const retry = Effect.fn("TextGeneration.retryLimitedProvider")(function* () {88+ const settings = yield* serverSettings.getSettings.pipe(89+ Effect.mapError(90+ (cause) =>91+ new TextGenerationError({92+ operation,93+ detail: "Could not read fallback model settings.",94+ cause,95+ }),96+ ),97+ );98+ const fallback = fallbackTextGenerationModelSelection(99+ settings.providerInstances,100+ (instanceId) => {101+ const provider = snapshots.find((candidate) => candidate.instanceId === instanceId);102+ return (103+ instanceId !== selection.instanceId &&104+ provider?.enabled === true &&apps/web/src/components/chat/DraftHeroHeadline.tsx line 207OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -188,10 +195,13 @@188195 );189196 if (!hasExplicitComposerModelSelection(currentDraft)) {190197 applyStickyState(draftId);191- const environmentSettings = environments.find(198+ const environmentConfig = environments.find(192199 (environment) => environment.environmentId === project.environmentId,193- )?.serverConfig?.settings;194- const defaultModelSelection = environmentSettings200+ )?.serverConfig;201+ const environmentSettings = environmentConfig?.settings;202+ const defaultSelection = environmentSettings195203 ? resolveProjectSettings(environmentSettings, project.id, project).settings196204 .defaultModelSelection197205 : project.defaultModelSelection;206+ const defaultModelSelection = resolveNewThreadModelSelection({207+ defaultSelection: resolveDefaultProviderModelSelection(packages/shared/src/usageLimits.ts lines 55 to 58OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -19,0 +38,21 @@38+ readonly fallbackSelection: ModelSelection | null;39+ readonly providers: readonly ServerProvider[];40+ readonly now: number;41+ readonly settings?: ServerSettings;42+ readonly projectId?: ProjectId | null;43+}) {44+ const selection = input.explicitSelection ?? input.defaultSelection;45+ const unchanged = { selection, fallback: null };46+ if (input.explicitSelection || !selection || !input.fallbackSelection) return unchanged;47+ const provider = input.providers.find((entry) => entry.instanceId === selection.instanceId);48+ const window = exhaustedUsageWindow(provider, input.now);49+ const fallback = input.providers.find(50+ (entry) => entry.instanceId === input.fallbackSelection?.instanceId,51+ );52+ if (53+ !provider ||54+ !window ||55+ !fallback?.enabled ||56+ !fallback.installed ||57+ !isProviderAvailable(fallback) ||58+ exhaustedUsageWindow(fallback, input.now) ||
- apps/web/src/components/settings/ProjectDefaultsSettings.tsx line 272OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -252,3 +260,13 @@252260 />253261 );254262263+ const fallbackRow = !isProjectScope ? (264+ <SettingsRow265+ serverScoped266+ settingKeys={["fallbackModelSelection"]}267+ mixed={mixedFallback}268+ id="fallback-model"269+ title="Fallback model"270+ description="Used for new threads when the default model's provider is out of usage."271+ resetAction={272+ settings.fallbackModelSelection !== null ? (packages/shared/src/usageLimits.ts lines 81 to 96Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -19,0 +46,21 @@46+ if (input.explicitSelection || !selection || !input.fallbackSelection) return unchanged;47+ const provider = input.providers.find((entry) => entry.instanceId === selection.instanceId);48+ const window = exhaustedUsageWindow(provider, input.now);49+ const fallback = input.providers.find(50+ (entry) => entry.instanceId === input.fallbackSelection?.instanceId,51+ );52+ if (53+ !provider ||54+ !window ||55+ !fallback?.enabled ||56+ !fallback.installed ||57+ !isProviderAvailable(fallback) ||58+ exhaustedUsageWindow(fallback, input.now) ||59+ (input.settings &&60+ !resolveProjectProviderInstanceEnabled(61+ input.settings,62+ input.projectId ?? null,63+ fallback.instanceId,64+ ))65+ )66+ return unchanged;
- packages/shared/src/usageLimits.ts line 25OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -17,3 +21,5 @@1721 const HOUR = 60 * MINUTE;1822 const DAY = 24 * HOUR;192324+export function exhaustedUsageWindow(provider: ServerProvider | undefined, now: number) {25+ return provider?.usageLimits?.windows.find(
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- packages/shared/src/usageLimits.ts line 44Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -19,0 +26,21 @@26+ now: number,27+ model?: string,28+) {29+ const exhausted = (provider?.usageLimits?.windows ?? []).filter(30+ (window) =>31+ window.usedPercent !== undefined &&32+ window.usedPercent >= 100 &&33+ (!window.resetsAt ||34+ !Number.isFinite(Date.parse(window.resetsAt)) ||35+ Date.parse(window.resetsAt) > now),36+ );37+ if (model === undefined) return exhausted[0];38+ // Some drivers report separate allowances per model family; only the ones this model draws from count.39+ switch (provider?.driver) {40+ case "cursor": {41+ const api = exhausted.find((window) => window.id === "apiPercentUsed");42+ const auto = exhausted.find((window) => window.id === "autoPercentUsed");43+ return /^(auto|default|grok|composer)/.test(model) ? auto && api : api;44+ }45+ case "antigravity": {46+ const family = model.match(/^(gemini|claude|gpt)-/)?.[1];
- apps/server/src/textGeneration/TextGeneration.ts line 110OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -58,0 +90,21 @@90+ Effect.mapError(91+ (cause) =>92+ new TextGenerationError({93+ operation,94+ detail: "Could not read fallback model settings.",95+ cause,96+ }),97+ ),98+ );99+ const fallback = fallbackTextGenerationModelSelection(100+ settings.providerInstances,101+ (instanceId) => {102+ const provider = snapshots.find((candidate) => candidate.instanceId === instanceId);103+ return (104+ instanceId !== selection.instanceId &&105+ provider?.enabled === true &&106+ provider.installed &&107+ provider.auth.status !== "unauthenticated" &&108+ isProviderAvailable(provider) &&109+ isProviderTextGenerationCapable(provider) &&110+ !exhaustedUsageWindow(provider, now)
- @@ -58,0 +92,21 @@92+ const settings = yield* serverSettings.getSettings.pipe(93+ Effect.mapError(94+ (cause) =>95+ new TextGenerationError({96+ operation,97+ detail: "Could not read fallback model settings.",98+ cause,99+ }),100+ ),101+ );102+ const fallback = fallbackTextGenerationModelSelection(103+ settings.providerInstances,104+ (instanceId, driver) => {105+ const provider = snapshots.find((candidate) => candidate.instanceId === instanceId);106+ return (107+ instanceId !== selection.instanceId &&108+ provider?.enabled === true &&109+ provider.installed &&110+ provider.auth.status !== "unauthenticated" &&111+ isProviderAvailable(provider) &&112+ isProviderTextGenerationCapable(provider) &&
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
maria-rcks merged this861a51a
maria-rcks closed this
Sign in to join this conversation.











macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed