import { auth } from '@/auth'; import { createKVChat } from '@/lib/kv/chat'; import { ChatEntity, MessageBase } from '@/lib/types'; import { nanoid } from '@/lib/utils'; import { revalidatePath } from 'next/cache'; /** * @param req * @returns */ export async function POST(req: Request): Promise { const session = await auth(); const user = session?.user?.email ?? 'anonymous'; // if (!email) { // return new Response('Unauthorized', { // status: 401, // }); // } try { const { id, url, initMessages } = (await req.json()) as { id?: string; url: string; initMessages?: MessageBase[]; }; const payload: ChatEntity = { url, id: id ?? nanoid(), user, messages: initMessages ?? [], }; await createKVChat(payload); revalidatePath('/chat', 'layout'); return Response.json(payload); } catch (error) { return new Response((error as Error).message, { status: 400, }); } }