Spaces:
Runtime error
Runtime error
File size: 1,493 Bytes
5da61b4 0bfcf81 5da61b4 0bfcf81 1b66f8d 0bfcf81 1b66f8d 0bfcf81 5da61b4 0bfcf81 1b66f8d 5da61b4 0bfcf81 1b66f8d 5da61b4 1b66f8d 5da61b4 1b66f8d 7b62aec b94fb23 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import type { RequestHandler } from "./$types";
import { collections } from "$lib/server/database";
import { ObjectId } from "mongodb";
import { error, redirect } from "@sveltejs/kit";
import { base } from "$app/paths";
import { z } from "zod";
import type { Message } from "$lib/types/Message";
export const POST: RequestHandler = async (input) => {
const body = await input.request.text();
let title = "";
let messages: Message[] = [];
let fromShareId: string | undefined;
if (body) {
fromShareId = z.object({ fromShare: z.string().optional() }).parse(JSON.parse(body)).fromShare;
if (fromShareId) {
const conversation = await collections.sharedConversations.findOne({
_id: fromShareId,
});
if (!conversation) {
throw error(404, "Conversation not found");
}
title = conversation.title;
messages = conversation.messages;
}
}
const res = await collections.conversations.insertOne({
_id: new ObjectId(),
title:
title ||
"Untitled " +
((await collections.conversations.countDocuments({ sessionId: input.locals.sessionId })) +
1),
messages,
createdAt: new Date(),
updatedAt: new Date(),
sessionId: input.locals.sessionId,
...(fromShareId ? { meta: { fromShareId } } : {}),
});
return new Response(
JSON.stringify({
conversationId: res.insertedId.toString(),
}),
{ headers: { "Content-Type": "application/json" } }
);
};
export const GET: RequestHandler = async () => {
throw redirect(302, base || "/");
};
|