Skip to content
Merged
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
22 changes: 17 additions & 5 deletions app/lib/.server/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export async function chatAction({ request }: ActionFunctionArgs) {
});
}
if (centitokensUsed >= centitokensQuota) {
if (!isPaidPlan && !hasApiKeySet(body.userApiKey)) {
if (!isPaidPlan && !hasApiKeySetForProvider(body.userApiKey, body.modelProvider)) {
// If they're not on a paid plan and don't have an API key set, return an error.
logger.error(`No tokens available for ${deploymentName}: ${centitokensUsed} of ${centitokensQuota}`);
return new Response(
Expand All @@ -109,7 +109,7 @@ export async function chatAction({ request }: ActionFunctionArgs) {
status: 402,
},
);
} else if (hasApiKeySet(body.userApiKey)) {
} else if (hasApiKeySetForProvider(body.userApiKey, body.modelProvider)) {
// If they have an API key set, use it. Otherwise, they use Convex tokens.
useUserApiKey = true;
}
Expand Down Expand Up @@ -207,11 +207,23 @@ export async function chatAction({ request }: ActionFunctionArgs) {
}
}

// Returns whether or not the user has an API key set
function hasApiKeySet(
// Returns whether or not the user has an API key set for a given provider
function hasApiKeySetForProvider(
userApiKey:
| { preference: 'always' | 'quotaExhausted'; value?: string; openai?: string; xai?: string; google?: string }
| undefined,
provider: ModelProvider,
) {
return Object.keys(userApiKey || {}).length > 1;
switch (provider) {
case 'Anthropic':
return userApiKey?.value !== undefined;
case 'OpenAI':
return userApiKey?.openai !== undefined;
case 'XAI':
return userApiKey?.xai !== undefined;
case 'Google':
return userApiKey?.google !== undefined;
default:
return false;
}
}