Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ function makeRequest(model: string, models?: string[]): GatewayRequest {
describe('applyGatewayModelsFallback', () => {
it.each<ProviderId>(['openrouter', 'vercel'])(
'sets Opus as the Fable fallback for the %s provider',
providerId => {
async providerId => {
const request = makeRequest('anthropic/claude-fable-5', ['caller/fallback']);

applyGatewayModelsFallback(providerId, 'anthropic/claude-fable-5', request);
await applyGatewayModelsFallback(providerId, 'anthropic/claude-fable-5', request);

expect(request.body.models).toEqual([
'anthropic/claude-fable-5',
Expand All @@ -33,18 +33,18 @@ describe('applyGatewayModelsFallback', () => {
}
);

it('removes caller-provided fallbacks for Fable on other providers', () => {
it('removes caller-provided fallbacks for Fable on other providers', async () => {
const request = makeRequest('anthropic/claude-fable-5', ['caller/fallback']);

applyGatewayModelsFallback('martian', 'anthropic/claude-fable-5', request);
await applyGatewayModelsFallback('martian', 'anthropic/claude-fable-5', request);

expect(request.body.models).toBeUndefined();
});

it('removes caller-provided fallbacks for other models', () => {
it('removes caller-provided fallbacks for other models', async () => {
const request = makeRequest('openai/gpt-4o', ['caller/fallback']);

applyGatewayModelsFallback('openrouter', 'openai/gpt-4o', request);
await applyGatewayModelsFallback('openrouter', 'openai/gpt-4o', request);

expect(request.body.models).toBeUndefined();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
rewriteChatCompletionsOneOfAsAnyOf,
isFriendliChatCompletionsRequest,
} from '@/lib/ai-gateway/schema-rewrite';
import { isFreeModel } from '@/lib/ai-gateway/is-free-model';

export function getPreferredProviderOrder(requestedModel: string): string[] {
if (isClaudeModel(requestedModel)) {
Expand Down Expand Up @@ -97,12 +98,16 @@ export function applyPreferredProvider(
}
}

export function applyGatewayModelsFallback(
export async function applyGatewayModelsFallback(
Comment thread
chrarnoldus marked this conversation as resolved.
providerId: ProviderId,
requestedModel: string,
requestToMutate: GatewayRequest
) {
if (isFableModel(requestedModel) && (providerId === 'openrouter' || providerId === 'vercel')) {
if (
!(await isFreeModel(requestedModel)) &&
isFableModel(requestedModel) &&
(providerId === 'openrouter' || providerId === 'vercel')
) {
requestToMutate.body.models = [requestedModel, CLAUDE_OPUS_CURRENT_MODEL_ID];
return;
}
Expand All @@ -122,7 +127,7 @@ export async function applyProviderSpecificLogic(
sessionId: string | null,
taskId: string | null
) {
applyGatewayModelsFallback(provider.id, requestedModel, requestToMutate);
await applyGatewayModelsFallback(provider.id, requestedModel, requestToMutate);
applyTrackingIds(requestToMutate, provider, userId, taskId);

sanitizeBinaryToolResults(requestToMutate);
Expand Down