feat: support guest session (#919)
This commit is contained in:
parent
24cb2ce19b
commit
9279135355
34 changed files with 741 additions and 288 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
import { cookies } from 'next/headers';
|
||||
import { notFound } from 'next/navigation';
|
||||
import { notFound, redirect } from 'next/navigation';
|
||||
|
||||
import { auth } from '@/app/(auth)/auth';
|
||||
import { Chat } from '@/components/chat';
|
||||
import { getChatById, getMessagesByChatId } from '@/lib/db/queries';
|
||||
import { DataStreamHandler } from '@/components/data-stream-handler';
|
||||
import { DEFAULT_CHAT_MODEL } from '@/lib/ai/models';
|
||||
import { DBMessage } from '@/lib/db/schema';
|
||||
import { Attachment, UIMessage } from 'ai';
|
||||
import type { DBMessage } from '@/lib/db/schema';
|
||||
import type { Attachment, UIMessage } from 'ai';
|
||||
|
||||
export default async function Page(props: { params: Promise<{ id: string }> }) {
|
||||
const params = await props.params;
|
||||
|
|
@ -20,8 +20,12 @@ export default async function Page(props: { params: Promise<{ id: string }> }) {
|
|||
|
||||
const session = await auth();
|
||||
|
||||
if (!session) {
|
||||
redirect('/api/auth/guest');
|
||||
}
|
||||
|
||||
if (chat.visibility === 'private') {
|
||||
if (!session || !session.user) {
|
||||
if (!session.user) {
|
||||
return notFound();
|
||||
}
|
||||
|
||||
|
|
@ -59,6 +63,7 @@ export default async function Page(props: { params: Promise<{ id: string }> }) {
|
|||
selectedChatModel={DEFAULT_CHAT_MODEL}
|
||||
selectedVisibilityType={chat.visibility}
|
||||
isReadonly={session?.user?.id !== chat.userId}
|
||||
session={session}
|
||||
/>
|
||||
<DataStreamHandler id={id} />
|
||||
</>
|
||||
|
|
@ -73,6 +78,7 @@ export default async function Page(props: { params: Promise<{ id: string }> }) {
|
|||
selectedChatModel={chatModelFromCookie.value}
|
||||
selectedVisibilityType={chat.visibility}
|
||||
isReadonly={session?.user?.id !== chat.userId}
|
||||
session={session}
|
||||
/>
|
||||
<DataStreamHandler id={id} />
|
||||
</>
|
||||
|
|
|
|||
|
|
@ -4,8 +4,16 @@ import { Chat } from '@/components/chat';
|
|||
import { DEFAULT_CHAT_MODEL } from '@/lib/ai/models';
|
||||
import { generateUUID } from '@/lib/utils';
|
||||
import { DataStreamHandler } from '@/components/data-stream-handler';
|
||||
import { auth } from '../(auth)/auth';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
export default async function Page() {
|
||||
const session = await auth();
|
||||
|
||||
if (!session) {
|
||||
redirect('/api/auth/guest');
|
||||
}
|
||||
|
||||
const id = generateUUID();
|
||||
|
||||
const cookieStore = await cookies();
|
||||
|
|
@ -21,6 +29,7 @@ export default async function Page() {
|
|||
selectedChatModel={DEFAULT_CHAT_MODEL}
|
||||
selectedVisibilityType="private"
|
||||
isReadonly={false}
|
||||
session={session}
|
||||
/>
|
||||
<DataStreamHandler id={id} />
|
||||
</>
|
||||
|
|
@ -36,6 +45,7 @@ export default async function Page() {
|
|||
selectedChatModel={modelIdFromCookie.value}
|
||||
selectedVisibilityType="private"
|
||||
isReadonly={false}
|
||||
session={session}
|
||||
/>
|
||||
<DataStreamHandler id={id} />
|
||||
</>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue