File size: 994 Bytes
c3e8f3d
84c9f51
d0a1b70
c3e8f3d
973f0d8
c3e8f3d
d0a1b70
 
 
 
 
f80b091
84c9f51
38448fc
 
 
 
 
c3e8f3d
26c4b30
6b8f69a
 
 
26c4b30
 
 
 
6b8f69a
 
84c9f51
26c4b30
 
 
84c9f51
26c4b30
973f0d8
 
26c4b30
 
 
 
 
 
c3e8f3d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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<Response> {
  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,
    });
  }
}