feat: support guest session (#919)

This commit is contained in:
Jeremy 2025-04-25 23:40:15 -07:00 committed by GitHub
parent 24cb2ce19b
commit 9279135355
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
34 changed files with 741 additions and 288 deletions

View file

@ -5,11 +5,12 @@ import {
smoothStream,
streamText,
} from 'ai';
import { auth } from '@/app/(auth)/auth';
import { auth, type UserType } from '@/app/(auth)/auth';
import { systemPrompt } from '@/lib/ai/prompts';
import {
deleteChatById,
getChatById,
getMessageCountByUserId,
saveChat,
saveMessages,
} from '@/lib/db/queries';
@ -25,6 +26,7 @@ import { requestSuggestions } from '@/lib/ai/tools/request-suggestions';
import { getWeather } from '@/lib/ai/tools/get-weather';
import { isProductionEnvironment } from '@/lib/constants';
import { myProvider } from '@/lib/ai/providers';
import { entitlementsByUserType } from '@/lib/ai/entitlements';
export const maxDuration = 60;
@ -46,6 +48,22 @@ export async function POST(request: Request) {
return new Response('Unauthorized', { status: 401 });
}
const userType: UserType = session.user.type;
const messageCount = await getMessageCountByUserId({
id: session.user.id,
differenceInHours: 24,
});
if (messageCount > entitlementsByUserType[userType].maxMessagesPerDay) {
return new Response(
'You have exceeded your maximum number of messages for the day! Please try again later.',
{
status: 429,
},
);
}
const userMessage = getMostRecentUserMessage(messages);
if (!userMessage) {