File size: 822 Bytes
1b66f8d
 
 
7b62aec
 
1b66f8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b62aec
 
 
 
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
import type { RequestHandler } from './$types';
import { collections } from '$lib/server/database';
import { ObjectId } from 'mongodb';
import { redirect } from '@sveltejs/kit';
import { base } from '$app/paths';

export const POST: RequestHandler = async (input) => {
	const res = await collections.conversations.insertOne({
		_id: new ObjectId(),
		title:
			'Untitled ' +
			((await collections.conversations.countDocuments({ sessionId: input.locals.sessionId })) + 1),
		messages: [],
		createdAt: new Date(),
		updatedAt: new Date(),
		sessionId: input.locals.sessionId
	});

	return new Response(
		JSON.stringify({
			conversationId: res.insertedId.toString()
		}),
		{ headers: { 'Content-Type': 'application/json' } }
	);
};

export const GET: RequestHandler = async () => {
	throw redirect(301, base || '/');
};