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