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

29
lib/ai/entitlements.ts Normal file
View file

@ -0,0 +1,29 @@
import type { UserType } from '@/app/(auth)/auth';
import type { ChatModel } from './models';
interface Entitlements {
maxMessagesPerDay: number;
availableChatModelIds: Array<ChatModel['id']>;
}
export const entitlementsByUserType: Record<UserType, Entitlements> = {
/*
* For users without an account
*/
guest: {
maxMessagesPerDay: 20,
availableChatModelIds: ['chat-model', 'chat-model-reasoning'],
},
/*
* For users with an account
*/
regular: {
maxMessagesPerDay: 100,
availableChatModelIds: ['chat-model', 'chat-model-reasoning'],
},
/*
* TODO: For users with an account and a paid membership
*/
};

View file

@ -1,6 +1,6 @@
export const DEFAULT_CHAT_MODEL: string = 'chat-model';
interface ChatModel {
export interface ChatModel {
id: string;
name: string;
description: string;

View file

@ -1,11 +1,13 @@
import { generateDummyPassword } from './db/utils';
export const isProductionEnvironment = process.env.NODE_ENV === 'production';
export const isDevelopmentEnvironment = process.env.NODE_ENV === 'development';
export const isTestEnvironment = Boolean(
process.env.PLAYWRIGHT_TEST_BASE_URL ||
process.env.PLAYWRIGHT ||
process.env.CI_PLAYWRIGHT,
);
export const guestRegex = /^guest-\d+$/;
export const DUMMY_PASSWORD = generateDummyPassword();

View file

@ -3,6 +3,7 @@ import 'server-only';
import {
and,
asc,
count,
desc,
eq,
gt,
@ -27,6 +28,7 @@ import {
type Chat,
} from './schema';
import type { ArtifactKind } from '@/components/artifact';
import { generateUUID } from '../utils';
import { generateHashedPassword } from './utils';
// Optionally, if not using email/pass login, you can
@ -57,6 +59,21 @@ export async function createUser(email: string, password: string) {
}
}
export async function createGuestUser() {
const email = `guest-${Date.now()}`;
const password = generateHashedPassword(generateUUID());
try {
return await db.insert(user).values({ email, password }).returning({
id: user.id,
email: user.email,
});
} catch (error) {
console.error('Failed to create guest user in database');
throw error;
}
}
export async function saveChat({
id,
userId,
@ -422,3 +439,34 @@ export async function updateChatVisiblityById({
throw error;
}
}
export async function getMessageCountByUserId({
id,
differenceInHours,
}: { id: string; differenceInHours: number }) {
try {
const twentyFourHoursAgo = new Date(
Date.now() - differenceInHours * 60 * 60 * 1000,
);
const [stats] = await db
.select({ count: count(message.id) })
.from(message)
.innerJoin(chat, eq(message.chatId, chat.id))
.where(
and(
eq(chat.userId, id),
gte(message.createdAt, twentyFourHoursAgo),
eq(message.role, 'user'),
),
)
.execute();
return stats?.count ?? 0;
} catch (error) {
console.error(
'Failed to get message count by user id for the last 24 hours from database',
);
throw error;
}
}